gcc/testsuite/
[official-gcc.git] / gcc / genmultilib
blobdc4751b0439b29c557a5549a6c2f4de4ffe50242
1 #!/bin/sh
2 # Generates multilib.h.
3 # Copyright (C) 1994, 1995, 1996, 1997, 1999, 2002, 2007, 2012
4 # Free Software Foundation, Inc.
6 #This file is part of GCC.
8 #GCC is free software; you can redistribute it and/or modify it under
9 #the terms of the GNU General Public License as published by the Free
10 #Software Foundation; either version 3, or (at your option) any later
11 #version.
13 #GCC is distributed in the hope that it will be useful, but WITHOUT
14 #ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 #FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 #for more details.
18 #You should have received a copy of the GNU General Public License
19 #along with GCC; see the file COPYING3. If not see
20 #<http://www.gnu.org/licenses/>.
22 # This shell script produces a header file which the gcc driver
23 # program uses to pick which library to use based on the machine
24 # specific options that it is given.
26 # The first argument is a list of sets of options. The elements in
27 # the list are separated by spaces. Within an element, the options
28 # are separated by slashes or pipes. No leading dash is used on the
29 # options.
30 # Each option in a set separated by slashes is mutually incompatible
31 # with all other options
32 # in the set.
33 # Each option in a set separated by pipes will be used for the library
34 # compilation and any of the options in the set will be sufficient
35 # for it to be triggered.
37 # The optional second argument is a list of subdirectory names. If
38 # the second argument is non-empty, there must be as many elements in
39 # the second argument as there are options in the first argument. The
40 # elements in the second list are separated by spaces. If the second
41 # argument is empty, the option names will be used as the directory
42 # names.
44 # The optional third argument is a list of options which are
45 # identical. The elements in the list are separated by spaces. Each
46 # element must be of the form OPTION=OPTION. The first OPTION should
47 # appear in the first argument, and the second should be a synonym for
48 # it. Question marks are replaced with equal signs in both options.
50 # The optional fourth argument is a list of multilib directory
51 # combinations that should not be built.
53 # The optional fifth argument is a list of options that should be
54 # used whenever building multilib libraries.
56 # The optional sixth argument is a list of exclusions used internally by
57 # the compiler similar to exceptions. The difference being that exclusions
58 # allow matching default options that genmultilib does not know about and
59 # is done at runtime as opposed to being sorted out at compile time.
60 # Each element in the list is a separate exclusion rule. Each rule is
61 # a list of options (sans preceding '-') separated by a '/'. The options
62 # on the rule are grouped as an AND operation, and all options much match
63 # for the rule to exclude a set. Options can be preceded with a '!' to
64 # match a logical NOT.
66 # The optional seventh argument is a list of OS subdirectory names.
67 # The format is either the same as of the second argument, or a set of
68 # mappings. When it is the same as the second argument, it describes
69 # the multilib directories using OS conventions, rather than GCC
70 # conventions. When it is a set of mappings of the form gccdir=osdir,
71 # the left side gives the GCC convention and the right gives the
72 # equivalent OS defined location. If the osdir part begins with a !,
73 # the os directory names are used exclusively. Use the mapping when
74 # there is no one-to-one equivalence between GCC levels and the OS.
76 # The optional eighth argument which intends to reduce the effort to write
77 # so many MULTILIB_EXCEPTIONS rules. This option defines a series of option
78 # combinations that we actually required.
79 # For some cases, the generated option combinations are far more than what
80 # we need, we have to write a lot of rules to screen out combinations we
81 # don't need. If we missed some rules, the unexpected libraries will be built.
82 # Now with this argument, one can simply give what combinations are needed.
83 # It is pretty straigtforward.
84 # This argument can be used together with MULTILIB_EXCEPTIONS and will take
85 # effect after the MULTILIB_EXCEPTIONS.
87 # The last option should be "yes" if multilibs are enabled. If it is not
88 # "yes", all GCC multilib dir names will be ".".
90 # The output looks like
91 # #define MULTILIB_MATCHES "\
92 # SUBDIRECTORY OPTIONS;\
93 # ...
94 # "
95 # The SUBDIRECTORY is the subdirectory to use. The OPTIONS are
96 # multiple options separated by spaces. Each option may start with an
97 # exclamation point. gcc will consider each line in turn. If none of
98 # the options beginning with an exclamation point are present, and all
99 # of the other options are present, that subdirectory will be used.
100 # The order of the subdirectories is such that they can be created in
101 # order; that is, a subdirectory is preceded by all its parents.
103 # Here is an example (this is from the actual sparc64 case):
104 # genmultilib 'm64/m32 mno-app-regs|mcmodel=medany' '64 32 alt'
105 # 'mcmodel?medany=mcmodel?medmid' 'm32/mno-app-regs* m32/mcmodel=*'
106 # '' 'm32/!m64/mno-app-regs m32/!m64/mcmodel=medany'
107 # '../lib64 ../lib32 alt' '' yes
108 # This produces:
109 # ". !m64 !m32 !mno-app-regs !mcmodel=medany;",
110 # "64:../lib64 m64 !m32 !mno-app-regs !mcmodel=medany;",
111 # "32:../lib32 !m64 m32 !mno-app-regs !mcmodel=medany;",
112 # "alt !m64 !m32 mno-app-regs mcmodel=medany;",
113 # "alt !m64 !m32 mno-app-regs !mcmodel=medany;",
114 # "alt !m64 !m32 !mno-app-regs mcmodel=medany;",
115 # "64/alt:../lib64/alt m64 !m32 mno-app-regs mcmodel=medany;",
116 # "64/alt:../lib64/alt m64 !m32 mno-app-regs !mcmodel=medany;",
117 # "64/alt:../lib64/alt m64 !m32 !mno-app-regs mcmodel=medany;",
119 # The effect is that `gcc -mno-app-regs' (for example) will append "alt"
120 # to the directory name when searching for libraries or startup files and
121 # `gcc -m32 -mcmodel=medany' (for example) will append "32/alt". Also note
122 # that exclusion above is moot, unless the compiler had a default of -m32,
123 # which would mean that all of the "alt" directories (not the 64/alt ones)
124 # would be ignored (not generated, nor used) since the exclusion also
125 # matches the multilib_default args.
127 # Copy the positional parameters into variables.
128 options=$1
129 dirnames=$2
130 matches=$3
131 exceptions=$4
132 extra=$5
133 exclusions=$6
134 osdirnames=$7
135 multilib_required=$8
136 enable_multilib=$9
138 echo "static const char *const multilib_raw[] = {"
140 mkdir tmpmultilib.$$ || exit 1
141 # Use cd ./foo to avoid CDPATH output.
142 cd ./tmpmultilib.$$ || exit 1
144 # What we want to do is select all combinations of the sets in
145 # options. Each combination which includes a set of mutually
146 # exclusive options must then be output multiple times, once for each
147 # item in the set. Selecting combinations is a recursive process.
148 # Since not all versions of sh support functions, we achieve recursion
149 # by creating a temporary shell script which invokes itself.
150 rm -f tmpmultilib
151 cat >tmpmultilib <<\EOF
152 #!/bin/sh
153 # This recursive script basically outputs all combinations of its
154 # input arguments, handling mutually exclusive sets of options by
155 # repetition. When the script is called, ${initial} is the list of
156 # options which should appear before all combinations this will
157 # output. The output looks like a list of subdirectory names with
158 # leading and trailing slashes.
159 if [ "$#" != "0" ]; then
160 first=$1
161 shift
162 case "$first" in
163 *\|*)
164 all=${initial}`echo $first | sed -e 's_|_/_'g`
165 first=`echo $first | sed -e 's_|_ _'g`
166 echo ${all}/
167 initial="${initial}${all}/" ./tmpmultilib $@
168 ./tmpmultilib $first $@ | grep -v "^${all}"
171 for opt in `echo $first | sed -e 's|/| |'g`; do
172 echo ${initial}${opt}/
173 done
174 ./tmpmultilib $@
175 for opt in `echo $first | sed -e 's|/| |'g`; do
176 initial="${initial}${opt}/" ./tmpmultilib $@
177 done
178 esac
181 chmod +x tmpmultilib
183 combinations=`initial=/ ./tmpmultilib ${options}`
185 # If there exceptions, weed them out now
186 if [ -n "${exceptions}" ]; then
187 cat >tmpmultilib2 <<\EOF
188 #!/bin/sh
189 # This recursive script weeds out any combination of multilib
190 # switches that should not be generated. The output looks like
191 # a list of subdirectory names with leading and trailing slashes.
193 for opt in $@; do
194 case "$opt" in
197 for except in ${exceptions}; do
198 echo " /${except}/) : ;;" >> tmpmultilib2
199 done
201 cat >>tmpmultilib2 <<\EOF
202 *) echo ${opt};;
203 esac
204 done
206 chmod +x tmpmultilib2
207 combinations=`./tmpmultilib2 ${combinations}`
210 # If the MULTILIB_REQUIRED list are provided,
211 # filter out combinations not in this list.
212 if [ -n "${multilib_required}" ]; then
213 cat >tmpmultilib2 <<\EOF
214 #!/bin/sh
215 # This recursive script weeds out any combination of multilib
216 # switches that not in the expected list.
218 for opt in $@; do
219 case "$opt" in
222 for expect in ${multilib_required}; do
223 echo " /${expect}/) echo \${opt};;" >> tmpmultilib2
224 done
226 cat >>tmpmultilib2 <<\EOF
227 *) ;;
228 esac
229 done
232 chmod +x tmpmultilib2
233 combinations=`./tmpmultilib2 ${combinations}`
237 # Construct a sed pattern which will convert option names to directory
238 # names.
239 todirnames=
240 if [ -n "${dirnames}" ]; then
241 set x ${dirnames}
242 shift
243 for set in ${options}; do
244 for opts in `echo ${set} | sed -e 's|/| |'g`; do
245 patt="/"
246 for opt in `echo ${opts} | sed -e 's_|_ _'g`; do
247 if [ "$1" != "${opt}" ]; then
248 todirnames="${todirnames} -e s|/${opt}/|/${1}/|g"
249 patt="${patt}${1}/"
250 if [ "${patt}" != "/${1}/" ]; then
251 todirnames="${todirnames} -e s|${patt}|/${1}/|g"
254 done
255 shift
256 done
257 done
260 # Construct a sed pattern which will convert option names to OS directory
261 # names.
262 toosdirnames=
263 defaultosdirname=
264 if [ -n "${osdirnames}" ]; then
265 set x ${osdirnames}
266 shift
267 while [ $# != 0 ] ; do
268 case "$1" in
269 .=*)
270 defaultosdirname=`echo $1 | sed 's|^.=|:|'`
271 shift
273 *=*)
274 patt=`echo $1 | sed -e 's|=|/$=/|'`
275 toosdirnames="${toosdirnames} -e s=^/${patt}/="
276 shift
279 break
281 esac
282 done
284 if [ $# != 0 ]; then
285 for set in ${options}; do
286 for opts in `echo ${set} | sed -e 's|/| |'g`; do
287 patt="/"
288 for opt in `echo ${opts} | sed -e 's_|_ _'g`; do
289 if [ "$1" != "${opt}" ]; then
290 toosdirnames="${toosdirnames} -e s|/${opt}/|/${1}/|g"
291 patt="${patt}${1}/"
292 if [ "${patt}" != "/${1}/" ]; then
293 toosdirnames="${toosdirnames} -e s|${patt}|/${1}/|g"
296 done
297 shift
298 done
299 done
303 # We need another recursive shell script to correctly handle positive
304 # matches. If we are invoked as
305 # genmultilib "opt1 opt2" "" "opt1=nopt1 opt2=nopt2"
306 # we must output
307 # opt1/opt2 opt1 opt2
308 # opt1/opt2 nopt1 opt2
309 # opt1/opt2 opt1 nopt2
310 # opt1/opt2 nopt1 nopt2
311 # In other words, we must output all combinations of matches.
312 rm -f tmpmultilib2
313 cat >tmpmultilib2 <<\EOF
314 #!/bin/sh
315 # The positional parameters are a list of matches to consider.
316 # ${dirout} is the directory name and ${optout} is the current list of
317 # options.
318 if [ "$#" = "0" ]; then
319 echo "\"${dirout} ${optout};\","
320 else
321 first=$1
322 shift
323 dirout="${dirout}" optout="${optout}" ./tmpmultilib2 $@
324 l=`echo ${first} | sed -e 's/=.*$//' -e 's/?/=/g'`
325 r=`echo ${first} | sed -e 's/^.*=//' -e 's/?/=/g'`
326 if expr " ${optout} " : ".* ${l} .*" > /dev/null; then
327 newopt=`echo " ${optout} " | sed -e "s/ ${l} / ${r} /" -e 's/^ //' -e 's/ $//'`
328 dirout="${dirout}" optout="${newopt}" ./tmpmultilib2 $@
332 chmod +x tmpmultilib2
334 # Start with the current directory, which includes only negations.
335 optout=
336 for set in ${options}; do
337 for opt in `echo ${set} | sed -e 's_[/|]_ _g'`; do
338 optout="${optout} !${opt}"
339 done
340 done
341 optout=`echo ${optout} | sed -e 's/^ //'`
342 echo "\".${defaultosdirname} ${optout};\","
344 # Work over the list of combinations. We have to translate each one
345 # to use the directory names rather than the option names, we have to
346 # include the information in matches, and we have to generate the
347 # correct list of options and negations.
348 for combo in ${combinations}; do
349 # Use the directory names rather than the option names.
350 if [ -n "${todirnames}" ]; then
351 dirout=`echo ${combo} | sed ${todirnames}`
352 else
353 dirout=`echo ${combo} | sed -e 's/=/-/g'`
355 # Remove the leading and trailing slashes.
356 dirout=`echo ${dirout} | sed -e 's|^/||' -e 's|/$||g'`
358 # Use the OS directory names rather than the option names.
359 if [ -n "${toosdirnames}" ]; then
360 osdirout=`echo ${combo} | sed ${toosdirnames}`
361 # Remove the leading and trailing slashes.
362 osdirout=`echo ${osdirout} | sed -e 's|^/||' -e 's|/$||g'`
363 if [ "x${enable_multilib}" != xyes ]; then
364 dirout=".:${osdirout}"
365 disable_multilib=yes
366 else
367 case "${osdirout}" in
369 dirout=`echo ${osdirout} | sed 's/^!//'`
372 dirout="${dirout}:${osdirout}"
374 esac
376 else
377 if [ "x${enable_multilib}" != xyes ]; then
378 # genmultilib with --disable-multilib should be
379 # called with '' '' '' '' '' '' '' no
380 # if MULTILIB_OSDIRNAMES is empty.
381 exit 1
385 # Look through the options. We must output each option that is
386 # present, and negate each option that is not present.
387 optout=
388 for set in ${options}; do
389 setopts=`echo ${set} | sed -e 's_[/|]_ _g'`
390 for opt in ${setopts}; do
391 if expr "${combo} " : ".*/${opt}/.*" > /dev/null; then
392 optout="${optout} ${opt}"
393 else
394 optout="${optout} !${opt}"
396 done
397 done
398 optout=`echo ${optout} | sed -e 's/^ //'`
400 # Output the line with all appropriate matches.
401 dirout="${dirout}" optout="${optout}" ./tmpmultilib2
402 done
404 # Terminate the list of string.
405 echo "NULL"
406 echo "};"
408 # Output all of the matches now as option and that is the same as that, with
409 # a semicolon trailer. Include all of the normal options as well.
410 # Note, the format of the matches is reversed compared
411 # to what we want, so switch them around.
412 echo ""
413 echo "static const char *const multilib_matches_raw[] = {"
414 for match in ${matches}; do
415 l=`echo ${match} | sed -e 's/=.*$//' -e 's/?/=/g'`
416 r=`echo ${match} | sed -e 's/^.*=//' -e 's/?/=/g'`
417 echo "\"${r} ${l};\","
418 done
419 for set in ${options}; do
420 for opt in `echo ${set} | sed -e 's_[/|]_ _'g`; do
421 echo "\"${opt} ${opt};\","
422 done
423 done
424 echo "NULL"
425 echo "};"
427 # Output the default options now
428 echo ""
429 echo "static const char *multilib_extra = \"${extra}\";"
431 # Output the exclusion rules now
432 echo ""
433 echo "static const char *const multilib_exclusions_raw[] = {"
434 for rule in ${exclusions}; do
435 s=`echo ${rule} | sed -e 's,/, ,g'`
436 echo "\"${s};\","
437 done
438 echo "NULL"
439 echo "};"
441 # Output the options now
442 moptions=`echo ${options} | sed -e 's,[ ][ ]*, ,g'`
443 echo ""
444 echo "static const char *multilib_options = \"${moptions}\";"
446 # Finally output the disable flag if specified
447 if [ "x${disable_multilib}" = xyes ]; then
448 echo ""
449 echo "#define DISABLE_MULTILIB 1"
452 cd ..
453 rm -r tmpmultilib.$$
455 exit 0