1 # Copyright 1999-2005 Gentoo Foundation
2 # Distributed under the terms of the GNU General Public License v2
3 # $Header: /var/cvsroot/gentoo-x86/sys-devel/gcc/files/awk/fixlafiles.awk,v 1.14 2005/09/24 07:31:28 vapier Exp $
8 function printn
(string
) {
9 system("echo -n \"" string
"\"")
11 function einfo
(string
) {
12 system("echo -e \" \\e[32;01m*\\e[0m " string
"\"")
14 function einfon
(string
) {
15 system("echo -ne \" \\e[32;01m*\\e[0m " string
"\"")
17 function ewarn
(string
) {
18 system("echo -e \" \\e[33;01m*\\e[0m " string
"\"")
20 function ewarnn
(string
) {
21 system("echo -ne \" \\e[33;01m*\\e[0m " string
"\"")
23 function eerror
(string
) {
24 system("echo -e \" \\e[31;01m*\\e[0m " string
"\"")
28 # assert(condition, errmsg)
29 # assert that a condition is true. Otherwise exit.
31 function assert
(condition
, string
) {
33 printf("%s:%d: assertion failed: %s\n",
34 FILENAME, FNR, string
) > "/dev/stderr"
41 # system(command, return)
42 # wrapper that normalizes return codes ...
44 function dosystem
(command
, ret
) {
55 # Get our variables from environment
57 OLDVER =
ENVIRON["OLDVER"]
58 OLDCHOST =
ENVIRON["OLDCHOST"]
61 eerror
("Could not get OLDVER!");
65 # Setup some sane defaults
69 DIRLIST
[2] =
"/usr/lib"
72 # Walk /etc/ld.so.conf to discover all our library paths
74 pipe =
"cat /etc/ld.so.conf | sort 2>/dev/null"
75 while(((pipe
) | getline ldsoconf_data
) > 0) {
76 if (ldsoconf_data !~
/^
[[:space
:]]*#/) {
77 if (ldsoconf_data ==
"") continue
79 # Remove any trailing comments
80 sub(/#.*$/, "", ldsoconf_data)
81 # Remove any trailing spaces
82 sub(/[[:space
:]]+$
/, "", ldsoconf_data
)
84 # If there's more than one path per line, split
85 # it up as if they were sep lines
86 split(ldsoconf_data
, nodes
, /[:,[:space
:]]/)
88 # Now add the rest from ld.so.conf
90 # wtf does this line do ?
91 sub(/=.
*/, "", nodes
[x
])
93 sub(/\
/$
/, "", nodes
[x
])
95 if (nodes
[x
] ==
"") continue
98 # Drop the directory if its a child directory of
99 # one that was already added ...
100 # For example, if we have:
101 # /usr/lib /usr/libexec /usr/lib/mozilla /usr/lib/nss
102 # We really just want to save /usr/lib /usr/libexec
106 if (nodes
[x
] ~
"^" DIRLIST
[y
] "(/|$)") {
113 DIRLIST
[++LIBCOUNT
] = nodes
[x
]
120 # Get line from gcc's output containing CHOST
122 pipe =
"gcc -print-file-name=libgcc.a 2>/dev/null"
123 if ((!
((pipe
) | getline TMP_CHOST
)) || (TMP_CHOST ==
"")) {
126 # If we fail to get the CHOST, see if we can get the CHOST
127 # portage thinks we are using ...
128 pipe =
"/usr/bin/portageq envvar 'CHOST'"
129 assert
(((pipe
) | getline CHOST
), "(" pipe
") | getline CHOST")
131 # Check pre gcc-3.4.x versions
132 CHOST = gensub
("^.+lib/gcc-lib/([^/]+)/[0-9]+.+$", "\\1", 1, TMP_CHOST
)
134 if (CHOST == TMP_CHOST
|| CHOST ==
"") {
135 # Check gcc-3.4.x or later
136 CHOST = gensub
("^.+lib/gcc/([^/]+)/[0-9]+.+$", "\\1", 1, TMP_CHOST
);
138 if (CHOST == TMP_CHOST
|| CHOST ==
"")
147 eerror
("Could not get gcc's CHOST!")
152 if (OLDCHOST == CHOST
)
155 GCCLIBPREFIX_OLD =
"/usr/lib/gcc-lib/"
156 GCCLIBPREFIX_NEW =
"/usr/lib/gcc/"
159 GCCLIBPREFIX = GCCLIBPREFIX_NEW
161 GCCLIBPREFIX = GCCLIBPREFIX_OLD
163 GCCLIB = GCCLIBPREFIX CHOST
165 if (OLDCHOST
!= "") {
166 OLDGCCLIB1 = GCCLIBPREFIX_OLD OLDCHOST
167 OLDGCCLIB2 = GCCLIBPREFIX_NEW OLDCHOST
170 # Get current gcc's version
171 pipe =
"gcc -dumpversion"
172 assert
(((pipe
) | getline NEWVER
), "(" pipe
") | getline NEWVER)")
176 eerror
("Could not get gcc's version!")
181 if ((OLDVER == NEWVER
) && (OLDCHOST ==
""))
185 # Ok, now let's scan for the .la files and actually fix them up
187 for (x =
1; x
<= LIBCOUNT
; x
++) {
188 # Do nothing if the target dir is gcc's internal library path
189 if (DIRLIST
[x
] ~ GCCLIBPREFIX_OLD
||
190 DIRLIST
[x
] ~ GCCLIBPREFIX_NEW
)
193 einfo
(" [" x
"/" LIBCOUNT
"] Scanning " DIRLIST
[x
] " ...")
195 pipe =
"find " DIRLIST
[x
] "/ -name '*.la' 2>/dev/null"
196 while (((pipe
) | getline la_files
) > 0) {
198 # Do nothing if the .la file is located in gcc's internal lib path
199 if (la_files ~ GCCLIBPREFIX_OLD
||
200 la_files ~ GCCLIBPREFIX_NEW
)
206 # See if we need to fix the .la file
207 while ((getline la_data
< (la_files
)) > 0) {
208 if (OLDCHOST
!= "") {
209 if ((gsub(OLDGCCLIB1
"[/[:space:]]+",
210 GCCLIB
, la_data
) > 0) ||
211 (gsub(OLDGCCLIB2
"[/[:space:]]+",
212 GCCLIB
, la_data
) > 0)) {
217 if (OLDVER
!= NEWVER
) {
218 if ((gsub(GCCLIBPREFIX_OLD CHOST
"/" OLDVER
"[/[:space:]]*",
219 GCCLIB
"/" NEWVER
, la_data
) > 0) ||
220 (gsub(GCCLIBPREFIX_NEW CHOST
"/" OLDVER
"[/[:space:]]*",
221 GCCLIB
"/" NEWVER
, la_data
) > 0))
227 # Do the actual changes in a second loop, as we can then
228 # verify that CHOST_CHANGED among things is correct ...
230 ewarnn
(" FIXING: " la_files
" ...")
235 # Clear the temp file (removing rather than '>foo' is better
236 # out of a security point of view?)
237 dosystem
("rm -f " la_files
".new")
239 while ((getline la_data
< (la_files
)) > 0) {
240 if (OLDCHOST
!= "") {
241 tmpstr = gensub
(OLDGCCLIB1
"([/[:space:]]+)",
242 GCCLIB
"\\1", "g", la_data
)
243 tmpstr = gensub
(OLDGCCLIB2
"([/[:space:]]+)",
244 GCCLIB
"\\1", "g", tmpstr
)
246 if (la_data
!= tmpstr
) {
251 if (CHOST_CHANGED
> 0) {
252 # We try to be careful about CHOST changes outside
253 # the gcc library path (meaning we cannot match it
254 # via /GCCLIBPREFIX CHOST/) ...
258 # dependency_libs=' -L/usr/CHOST/{bin,lib}'
260 gsub("-L/usr/" OLDCHOST
"/",
261 "-L/usr/" CHOST
"/", la_data
)
264 # dependency_libs=' -L/usr/lib/gcc-lib/CHOST/VER/../../../../CHOST/lib'
266 la_data = gensub
("(" GCCLIB
"/[^[:space:]]+)/" OLDCHOST
"/",
267 "\\1/" CHOST
"/", "g", la_data
)
271 if (OLDVER
!= NEWVER
) {
274 # dependency_libs=' -L/usr/lib/gcc/CHOST/VER'
276 tmpstr = gensub
(GCCLIBPREFIX_OLD CHOST
"/" OLDVER
"([/[:space:]]+)",
277 GCCLIB
"/" NEWVER
"\\1", "g", la_data
)
278 tmpstr = gensub
(GCCLIBPREFIX_NEW CHOST
"/" OLDVER
"([/[:space:]]+)",
279 GCCLIB
"/" NEWVER
"\\1", "g", tmpstr
)
281 if (la_data
!= tmpstr
) {
284 # dependency_libs=' -L/usr/lib/gcc-lib/../../CHOST/lib'
286 # in cases where we have gcc34
287 tmpstr = gensub
(GCCLIBPREFIX_OLD
"(../../" CHOST
"/lib)",
288 GCCLIBPREFIX
"\\1", "g", tmpstr
)
289 tmpstr = gensub
(GCCLIBPREFIX_NEW
"(../../" CHOST
"/lib)",
290 GCCLIBPREFIX
"\\1", "g", tmpstr
)
296 print la_data
>> (la_files
".new")
303 close(la_files
".new")
305 assert
(dosystem
("mv -f " la_files
".new " la_files
),
306 "dosystem(\"mv -f " la_files
".new " la_files
"\")")