]> git.draconx.ca Git - gentoo-draconx.git/blob - sys-devel/gcc/files/awk/fixlafiles.awk-no_gcc_la
03f42dc9165c3d7beabdd3e8fc674a41d5db8051
[gentoo-draconx.git] / sys-devel / gcc / files / awk / fixlafiles.awk-no_gcc_la
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 $
4
5 #
6 # Helper functions
7 #
8 function printn(string) {
9         printf("%s", string)
10 }
11 function einfo(string) {
12         printf(" \033[32;01m*\033[0m %s\n", string)
13 }
14 function einfon(string) {
15         printf(" \033[32;01m*\033[0m %s", string)
16 }
17 function ewarn(string) {
18         printf(" \033[33;01m*\033[0m %s\n", string)
19 }
20 function ewarnn(string) {
21         printf(" \033[33;01m*\033[0m %s", string)
22 }
23 function eerror(string) {
24         printf(" \033[31;01m*\033[0m %s\n", string)
25 }
26
27 #
28 # assert(condition, errmsg)
29 #   assert that a condition is true.  Otherwise exit.
30 #
31 function assert(condition, string) {
32         if (! condition) {
33                 printf("%s:%d: assertion failed: %s\n",
34                        FILENAME, FNR, string) > "/dev/stderr"
35                 _assert_exit = 1
36                 exit 1
37         }
38 }
39
40 #
41 # system(command, return)
42 #   wrapper that normalizes return codes ...
43 #
44 function dosystem(command, ret) {
45         ret = 0
46         ret = system(command)
47         if (ret == 0)
48                 return 1
49         else
50                 return 0
51 }
52
53 BEGIN {
54         #
55         # Get our variables from environment
56         #
57         OLDVER = ENVIRON["OLDVER"]
58         OLDCHOST = ENVIRON["OLDCHOST"]
59
60         if (OLDVER == "") {
61                 eerror("Could not get OLDVER!");
62                 exit 1
63         }
64
65         # Setup some sane defaults
66         LIBCOUNT = 2
67         HAVE_GCC34 = 0
68         DIRLIST[1] = "/lib"
69         DIRLIST[2] = "/usr/lib"
70
71         #
72         # Walk /etc/ld.so.conf to discover all our library paths
73         #
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
78
79                         # Remove any trailing comments
80                         sub(/#.*$/, "", ldsoconf_data)
81                         # Remove any trailing spaces
82                         sub(/[[:space:]]+$/, "", ldsoconf_data)
83
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:]]/)
87
88                         # Now add the rest from ld.so.conf
89                         for (x in nodes) {
90                                 # wtf does this line do ?
91                                 sub(/=.*/, "", nodes[x])
92                                 # Prune trailing /
93                                 sub(/\/$/, "", nodes[x])
94
95                                 if (nodes[x] == "") continue
96
97                                 #
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
103                                 #
104                                 CHILD = 0
105                                 for (y in DIRLIST) {
106                                         if (nodes[x] ~ "^" DIRLIST[y] "(/|$)") {
107                                                 CHILD = 1
108                                                 break
109                                         }
110                                 }
111                                 if (CHILD) continue
112
113                                 DIRLIST[++LIBCOUNT] = nodes[x]
114                         }
115                 }
116         }
117         close(pipe)
118
119         #
120         # Get line from gcc's output containing CHOST
121         #
122         pipe = "gcc -print-file-name=libgcc.a 2>/dev/null"
123         if ((!((pipe) | getline TMP_CHOST)) || (TMP_CHOST == "")) {
124                 close(pipe)
125
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")
130         } else {
131                 # Check pre gcc-3.4.x versions
132                 CHOST = gensub("^.+lib/gcc-lib/([^/]+)/[0-9]+.+$", "\\1", 1, TMP_CHOST)
133
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);
137
138                         if (CHOST == TMP_CHOST || CHOST == "")
139                                 CHOST = ""
140                         else
141                                 HAVE_GCC34 = 1
142                 }
143         }
144         close(pipe)
145
146         if (CHOST == "") {
147                 eerror("Could not get gcc's CHOST!")
148                 exit 1
149         }
150
151         if (OLDCHOST != "")
152                 if (OLDCHOST == CHOST)
153                         OLDCHOST = ""
154
155         GCCLIBPREFIX_OLD = "/usr/lib/gcc-lib/"
156         GCCLIBPREFIX_NEW = "/usr/lib/gcc/"
157
158         if (HAVE_GCC34)
159                 GCCLIBPREFIX = GCCLIBPREFIX_NEW
160         else
161                 GCCLIBPREFIX = GCCLIBPREFIX_OLD
162
163         GCCLIB = GCCLIBPREFIX CHOST
164
165         if (OLDCHOST != "") {
166                 OLDGCCLIB1 = GCCLIBPREFIX_OLD OLDCHOST
167                 OLDGCCLIB2 = GCCLIBPREFIX_NEW OLDCHOST
168         }
169
170         # Get current gcc's version
171         pipe = "gcc -dumpversion"
172         assert(((pipe) | getline NEWVER), "(" pipe ") | getline NEWVER)")
173         close(pipe)
174
175         if (NEWVER == "") {
176                 eerror("Could not get gcc's version!")
177                 exit 1
178         }
179
180         # Nothing to do ?
181         # NB: Do not check for (OLDVER == NEWVER) anymore, as we might need to
182         #     replace libstdc++.la ....
183         if ((OLDVER == "") && (OLDCHOST == ""))
184                 exit 0
185
186         #
187         # Ok, now let's scan for the .la files and actually fix them up
188         #
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)
193                         continue
194
195                 einfo("  [" x "/" LIBCOUNT "] Scanning " DIRLIST[x] " ...")
196
197                 pipe = "find " DIRLIST[x] "/ -name '*.la' 2>/dev/null"
198                 while (((pipe) | getline la_files) > 0) {
199
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)
203                                 continue
204
205                         CHANGED = 0
206                         CHOST_CHANGED = 0
207
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)) {
215                                                 CHANGED = 1
216                                                 CHOST_CHANGED = 1
217                                         }
218                                 }
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))
224                                                 CHANGED = 1
225                                 }
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)
231                                         CHANGED = 1
232                         }
233                         close(la_files)
234
235                         # Do the actual changes in a second loop, as we can then
236                         # verify that CHOST_CHANGED among things is correct ...
237                         if (CHANGED) {
238                                 ewarnn("    FIXING: " la_files " ...[")
239
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")
243
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)
250
251                                                 if (la_data != tmpstr) {
252                                                         printn("c")
253                                                         la_data = tmpstr
254                                                 }
255
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/) ...
260
261                                                         # Catch:
262                                                         #
263                                                         #  dependency_libs=' -L/usr/CHOST/{bin,lib}'
264                                                         #
265                                                         gsub("-L/usr/" OLDCHOST "/",
266                                                              "-L/usr/" CHOST "/", la_data)
267                                                         # Catch:
268                                                         #
269                                                         #  dependency_libs=' -L/usr/lib/gcc-lib/CHOST/VER/../../../../CHOST/lib'
270                                                         #
271                                                         la_data = gensub("(" GCCLIB "/[^[:space:]]+)/" OLDCHOST "/",
272                                                                          "\\1/" CHOST "/", "g", la_data)
273                                                 }
274                                         }
275
276                                         if (OLDVER != NEWVER) {
277                                                 # Catch:
278                                                 #
279                                                 #  dependency_libs=' -L/usr/lib/gcc/CHOST/VER'
280                                                 #
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)
285
286                                                 if (la_data != tmpstr) {
287                                                         # Catch:
288                                                         #
289                                                         #  dependency_libs=' -L/usr/lib/gcc-lib/../../CHOST/lib'
290                                                         #
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)
296                                                         printn("v")
297                                                         la_data = tmpstr
298                                                 }
299                                         }
300
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) {
308                                                 printn("l")
309                                                 la_data = tmpstr
310                                         }
311
312                                         print la_data >> (la_files ".new")
313                                 }
314
315                                 if (CHANGED)
316                                         print "]"
317
318                                 close(la_files)
319                                 close(la_files ".new")
320
321                                 assert(dosystem("mv -f " la_files ".new " la_files),
322                                        "dosystem(\"mv -f " la_files ".new " la_files "\")")
323                         }
324                 }
325
326                 close(pipe)
327         }
328 }
329
330 # vim:ts=4