new gcc snapshot
[dottout.git] / net-im / emesene / files / sys-devel / gcc / files / awk / fixlafiles.awk
blobc4798f2508369d6cf72924420e962a0e87d4954a
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 $
6 # Helper functions
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) {
32 if (! condition) {
33 printf("%s:%d: assertion failed: %s\n",
34 FILENAME, FNR, string) > "/dev/stderr"
35 _assert_exit = 1
36 exit 1
41 # system(command, return)
42 # wrapper that normalizes return codes ...
44 function dosystem(command, ret) {
45 ret = 0
46 ret = system(command)
47 if (ret == 0)
48 return 1
49 else
50 return 0
53 BEGIN {
55 # Get our variables from environment
57 OLDVER = ENVIRON["OLDVER"]
58 OLDCHOST = ENVIRON["OLDCHOST"]
60 if (OLDVER == "") {
61 eerror("Could not get OLDVER!");
62 exit 1
65 # Setup some sane defaults
66 LIBCOUNT = 2
67 HAVE_GCC34 = 0
68 DIRLIST[1] = "/lib"
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
89 for (x in nodes) {
90 # wtf does this line do ?
91 sub(/=.*/, "", nodes[x])
92 # Prune trailing /
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
104 CHILD = 0
105 for (y in DIRLIST) {
106 if (nodes[x] ~ "^" DIRLIST[y] "(/|$)") {
107 CHILD = 1
108 break
111 if (CHILD) continue
113 DIRLIST[++LIBCOUNT] = nodes[x]
117 close(pipe)
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 == "")) {
124 close(pipe)
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)
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 == "")
139 CHOST = ""
140 else
141 HAVE_GCC34 = 1
144 close(pipe)
146 if (CHOST == "") {
147 eerror("Could not get gcc's CHOST!")
148 exit 1
151 if (OLDCHOST != "")
152 if (OLDCHOST == CHOST)
153 OLDCHOST = ""
155 GCCLIBPREFIX_OLD = "/usr/lib/gcc-lib/"
156 GCCLIBPREFIX_NEW = "/usr/lib/gcc/"
158 if (HAVE_GCC34)
159 GCCLIBPREFIX = GCCLIBPREFIX_NEW
160 else
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)")
173 close(pipe)
175 if (NEWVER == "") {
176 eerror("Could not get gcc's version!")
177 exit 1
180 # Nothing to do ?
181 if ((OLDVER == NEWVER) && (OLDCHOST == ""))
182 exit 0
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)
191 continue
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)
201 continue
203 CHANGED = 0
204 CHOST_CHANGED = 0
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)) {
213 CHANGED = 1
214 CHOST_CHANGED = 1
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))
222 CHANGED = 1
225 close(la_files)
227 # Do the actual changes in a second loop, as we can then
228 # verify that CHOST_CHANGED among things is correct ...
229 if (CHANGED) {
230 ewarnn(" FIXING: " la_files " ...")
232 if (CHANGED)
233 printn("[")
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) {
247 printn("c")
248 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/) ...
256 # Catch:
258 # dependency_libs=' -L/usr/CHOST/{bin,lib}'
260 gsub("-L/usr/" OLDCHOST "/",
261 "-L/usr/" CHOST "/", la_data)
262 # Catch:
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) {
272 # Catch:
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) {
282 # Catch:
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)
291 printn("v")
292 la_data = tmpstr
296 print la_data >> (la_files ".new")
299 if (CHANGED)
300 print "]"
302 close(la_files)
303 close(la_files ".new")
305 assert(dosystem("mv -f " la_files ".new " la_files),
306 "dosystem(\"mv -f " la_files ".new " la_files "\")")
310 close(pipe)
314 # vim:ts=4