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-no_gcc_la,v 1.3 2009/03/31 07:59:19 drizzt Exp $
8 function printn(string) {
11 function einfo(string) {
12 printf(" \033[32;01m*\033[0m %s\n", string)
14 function einfon(string) {
15 printf(" \033[32;01m*\033[0m %s", string)
17 function ewarn(string) {
18 printf(" \033[33;01m*\033[0m %s\n", string)
20 function ewarnn(string) {
21 printf(" \033[33;01m*\033[0m %s", string)
23 function eerror(string) {
24 printf(" \033[31;01m*\033[0m %s\n", 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 # NB: Do not check for (OLDVER == NEWVER) anymore, as we might need to
182 # replace libstdc++.la ....
183 if ((OLDVER == "") && (OLDCHOST == ""))
187 # Ok, now let's scan for the .la files and actually fix them up
189 for (x = 1; x <= LIBCOUNT; x++) {
190 # Do nothing if the target dir is gcc's internal library path
191 if (DIRLIST[x] ~ GCCLIBPREFIX_OLD ||
192 DIRLIST[x] ~ GCCLIBPREFIX_NEW)
195 einfo(" [" x "/" LIBCOUNT "] Scanning " DIRLIST[x] " ...")
197 pipe = "find " DIRLIST[x] "/ -name '*.la' 2>/dev/null"
198 while (((pipe) | getline la_files) > 0) {
200 # Do nothing if the .la file is located in gcc's internal lib path
201 if (la_files ~ GCCLIBPREFIX_OLD ||
202 la_files ~ GCCLIBPREFIX_NEW)
208 # See if we need to fix the .la file
209 while ((getline la_data < (la_files)) > 0) {
210 if (OLDCHOST != "") {
211 if ((gsub(OLDGCCLIB1 "[/[:space:]]+",
212 GCCLIB, la_data) > 0) ||
213 (gsub(OLDGCCLIB2 "[/[:space:]]+",
214 GCCLIB, la_data) > 0)) {
219 if (OLDVER != NEWVER) {
220 if ((gsub(GCCLIBPREFIX_OLD CHOST "/" OLDVER "[/[:space:]]*",
221 GCCLIB "/" NEWVER, la_data) > 0) ||
222 (gsub(GCCLIBPREFIX_NEW CHOST "/" OLDVER "[/[:space:]]*",
223 GCCLIB "/" NEWVER, la_data) > 0))
226 # We now check if we have libstdc++.la, as we remove the
227 # libtool linker scripts for gcc ...
228 # We do this last, as we only match the new paths
229 if (gsub(GCCLIB "/" NEWVER "/libstdc\\+\\+\\.la",
230 "-lstdc++", la_data) > 0)
235 # Do the actual changes in a second loop, as we can then
236 # verify that CHOST_CHANGED among things is correct ...
238 ewarnn(" FIXING: " la_files " ...[")
240 # Clear the temp file (removing rather than '>foo' is better
241 # out of a security point of view?)
242 dosystem("rm -f " la_files ".new")
244 while ((getline la_data < (la_files)) > 0) {
245 if (OLDCHOST != "") {
246 tmpstr = gensub(OLDGCCLIB1 "([/[:space:]]+)",
247 GCCLIB "\\1", "g", la_data)
248 tmpstr = gensub(OLDGCCLIB2 "([/[:space:]]+)",
249 GCCLIB "\\1", "g", tmpstr)
251 if (la_data != tmpstr) {
256 if (CHOST_CHANGED > 0) {
257 # We try to be careful about CHOST changes outside
258 # the gcc library path (meaning we cannot match it
259 # via /GCCLIBPREFIX CHOST/) ...
263 # dependency_libs=' -L/usr/CHOST/{bin,lib}'
265 gsub("-L/usr/" OLDCHOST "/",
266 "-L/usr/" CHOST "/", la_data)
269 # dependency_libs=' -L/usr/lib/gcc-lib/CHOST/VER/../../../../CHOST/lib'
271 la_data = gensub("(" GCCLIB "/[^[:space:]]+)/" OLDCHOST "/",
272 "\\1/" CHOST "/", "g", la_data)
276 if (OLDVER != NEWVER) {
279 # dependency_libs=' -L/usr/lib/gcc/CHOST/VER'
281 tmpstr = gensub(GCCLIBPREFIX_OLD CHOST "/" OLDVER "([/[:space:]]+)",
282 GCCLIB "/" NEWVER "\\1", "g", la_data)
283 tmpstr = gensub(GCCLIBPREFIX_NEW CHOST "/" OLDVER "([/[:space:]]+)",
284 GCCLIB "/" NEWVER "\\1", "g", tmpstr)
286 if (la_data != tmpstr) {
289 # dependency_libs=' -L/usr/lib/gcc-lib/../../CHOST/lib'
291 # in cases where we have gcc34
292 tmpstr = gensub(GCCLIBPREFIX_OLD "(../../" CHOST "/lib)",
293 GCCLIBPREFIX "\\1", "g", tmpstr)
294 tmpstr = gensub(GCCLIBPREFIX_NEW "(../../" CHOST "/lib)",
295 GCCLIBPREFIX "\\1", "g", tmpstr)
301 # We now check if we have libstdc++.la, as we remove the
302 # libtool linker scripts for gcc and any referencese in any
303 # libtool linker scripts.
304 # We do this last, as we only match the new paths
305 tmpstr = gensub(GCCLIB "/" NEWVER "/libstdc\\+\\+\\.la",
306 "-lstdc++", "g", la_data);
307 if (la_data != tmpstr) {
312 print la_data >> (la_files ".new")
319 close(la_files ".new")
321 assert(dosystem("mv -f " la_files ".new " la_files),
322 "dosystem(\"mv -f " la_files ".new " la_files "\")")