1/*
2$VER: Ext_Inst_so.rexx 0.5 (05.12.2020) Extract and install compiled-in shared libraries from a given ELF binary.
3*/
4
5PARSE ARG executable install_path
6
7/*
8Determine REXX interpreter
9*/
10PARSE UPPER VERSION language .
11
12/*
13Check if arguments are available, otherwise quit.
14*/
15IF ~ARG() THEN DO
16	SAY 'No Arguments given!'
17	SAY 'Usage: Ext_Inst_so.rexx EXECUTABLE INSTALL_PATH'
18	EXIT
19END
20
21/*
22If the given filename/path has spaces in it, AmigaDOS/CLI will add extra
23quotation marks to secure a sane working path.
24Get rid of them to make AREXX find the file and remove leading and trailing
25spaces.
26*/
27IF ~EXISTS(executable) THEN DO
28	SAY executable' not available!'
29	EXIT
30END
31ELSE DO
32	executable=STRIP(executable)
33	executable=COMPRESS(executable,'"')
34END
35IF installpath='' THEN DO
36	SAY 'No installation destination given!'
37	EXIT
38END
39ELSE DO
40	install_path=STRIP(install_path)
41	install_path=STRIP(install_path,'T','/')
42	install_path=COMPRESS(install_path,'"')
43	/*
44	Check for destination path and create it, if needed.
45	*/
46	IF ~EXISTS(install_path'/sobjs/') THEN
47		ADDRESS COMMAND 'makedir all 'install_path'/sobjs'
48END
49
50/*
51Save used gcc version. Needed later to install libgcc.so.
52*/
53ADDRESS COMMAND 'gcc -dumpversion >so_dump'
54
55/*
56Create shared objects dump.
57*/
58ADDRESS COMMAND 'readelf -d 'executable' >>so_dump'
59
60/*
61Error check, if I/O went wrong.
62*/
63IF ~OPEN(SO_read,'so_dump','R') THEN DO
64	SAY 'File so_dump opening failed!'
65	EXIT
66END
67
68/*
69Get used gcc version.
70*/
71gcc_version=READLN(SO_read)
72
73/*
74We know that the dumped shared library entries always start at line 4 (line 5
75now with the added gcc version). Skip unneeded lines to speed up processing.
76*/
77working_line=CALL READLN(SO_read)
78working_line=CALL READLN(SO_read)
79working_line=CALL READLN(SO_read)
80
81i=1
82libpaths.i = 'SDK:local/newlib/lib/'
83i=i+1
84libpaths.i = 'SDK:gcc/lib/'
85i=i+1
86libpaths.i = 'SDK:gcc/lib/gcc/ppc-amigaos/'gcc_version'/'
87i=i+1
88
89/*
90VALUE(arg,, 'ENVIRONMENT') is a Regina REXX extension
91*/
92IF POS('REGINA', language) ~= 0 THEN DO
93	prefix = VALUE('PREFIX',, 'ENVIRONMENT')
94	IF prefix <> '' THEN DO
95		libpaths.i = prefix'/lib/'
96		i=i+1
97	END
98	prefix = VALUE('CROSS_PREFIX',, 'ENVIRONMENT')
99	IF prefix ~= '' THEN DO
100		libpaths.i = prefix'/lib/gcc/ppc-amigaos/'gcc_version'/'
101		i=i+1
102		libpaths.i = prefix'/ppc-amigaos/lib/'
103		i=i+1
104	END
105END
106libpaths.0 = i - 1
107
108i=1
109DO WHILE i>0
110	working_line=READLN(SO_read)
111	IF POS('Shared library:', working_line)>0 THEN DO
112		i=1
113		/*
114		We know that the shared library names always start at position 59.
115		*/
116		lib.so=SUBSTR(working_line,59,LASTPOS(']', working_line)-59)
117		/*
118		- Find and install the mandatory shared libraries from their varying
119		  home directories.
120		- libgcc.so is deeply hidden inside the gcc install directory tree
121		  by default. Since people can use different gcc versions we have
122		  determine which to use the correct path.
123		*/
124		DO j = 1 TO libpaths.0
125			IF EXISTS(libpaths.j''lib.so) THEN DO
126				ADDRESS COMMAND 'copy clone quiet' libpaths.j''lib.so install_path'/sobjs/'
127				LEAVE
128			END
129		END
130		IF j > libpaths.0 THEN DO
131			/*
132			If a shared library is not found, abort.
133			*/
134			SAY lib.so' not found! Aborting!'
135			EXIT
136		END
137	END
138	ELSE
139		i=0
140END
141
142/*
143AREXX is doing its own cleaning up of open files.
144Close the file manually anyway.
145*/
146IF ~CLOSE(SO_Read) THEN DO
147	SAY 'File so_dump closing failed!'
148	EXIT
149END
150
151ADDRESS COMMAND 'delete force quiet so_dump'
152
153EXIT
154