2017-04-28 Hristian Kirtchev <kirtchev@adacore.com>
[official-gcc.git] / gcc / genmultilib
blob2501f547a938ade63b738b8ad91b4bd317487b8e
1 #!/bin/sh
2 # Generates multilib.h.
3 # Copyright (C) 1994-2017 Free Software Foundation, Inc.
5 #This file is part of GCC.
7 #GCC is free software; you can redistribute it and/or modify it under
8 #the terms of the GNU General Public License as published by the Free
9 #Software Foundation; either version 3, or (at your option) any later
10 #version.
12 #GCC is distributed in the hope that it will be useful, but WITHOUT
13 #ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 #FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 #for more details.
17 #You should have received a copy of the GNU General Public License
18 #along with GCC; see the file COPYING3. If not see
19 #<http://www.gnu.org/licenses/>.
21 # This shell script produces a header file which the gcc driver
22 # program uses to pick which library to use based on the machine
23 # specific options that it is given.
25 # The first argument is a list of sets of options. The elements in
26 # the list are separated by spaces. Within an element, the options
27 # are separated by slashes or pipes. No leading dash is used on the
28 # options.
29 # Each option in a set separated by slashes is mutually incompatible
30 # with all other options
31 # in the set.
32 # Each option in a set separated by pipes will be used for the library
33 # compilation and any of the options in the set will be sufficient
34 # for it to be triggered.
36 # The optional second argument is a list of subdirectory names. If
37 # the second argument is non-empty, there must be as many elements in
38 # the second argument as there are options in the first argument. The
39 # elements in the second list are separated by spaces. If the second
40 # argument is empty, the option names will be used as the directory
41 # names.
43 # The optional third argument is a list of options which are
44 # identical. The elements in the list are separated by spaces. Each
45 # element must be of the form OPTION=OPTION. The first OPTION should
46 # appear in the first argument, and the second should be a synonym for
47 # it. Question marks are replaced with equal signs in both options.
49 # The optional fourth argument is a list of multilib directory
50 # combinations that should not be built.
52 # The optional fifth argument is a list of options that should be
53 # used whenever building multilib libraries.
55 # The optional sixth argument is a list of exclusions used internally by
56 # the compiler similar to exceptions. The difference being that exclusions
57 # allow matching default options that genmultilib does not know about and
58 # is done at runtime as opposed to being sorted out at compile time.
59 # Each element in the list is a separate exclusion rule. Each rule is
60 # a list of options (sans preceding '-') separated by a '/'. The options
61 # on the rule are grouped as an AND operation, and all options much match
62 # for the rule to exclude a set. Options can be preceded with a '!' to
63 # match a logical NOT.
65 # The optional seventh argument is a list of OS subdirectory names.
66 # The format is either the same as of the second argument, or a set of
67 # mappings. When it is the same as the second argument, it describes
68 # the multilib directories using OS conventions, rather than GCC
69 # conventions. When it is a set of mappings of the form gccdir=osdir,
70 # the left side gives the GCC convention and the right gives the
71 # equivalent OS defined location. If the osdir part begins with a !,
72 # the os directory names are used exclusively. Use the mapping when
73 # there is no one-to-one equivalence between GCC levels and the OS.
75 # The optional eighth argument which intends to reduce the effort to write
76 # so many MULTILIB_EXCEPTIONS rules. This option defines a series of option
77 # combinations that we actually required.
78 # For some cases, the generated option combinations are far more than what
79 # we need, we have to write a lot of rules to screen out combinations we
80 # don't need. If we missed some rules, the unexpected libraries will be built.
81 # Now with this argument, one can simply give what combinations are needed.
82 # It is pretty straigtforward.
83 # This argument can be used together with MULTILIB_EXCEPTIONS and will take
84 # effect after the MULTILIB_EXCEPTIONS.
86 # The optional ninth argument is the multiarch name.
88 # The optional tenth argument specifies how to reuse multilib for different
89 # option sets.
91 # The last option should be "yes" if multilibs are enabled. If it is not
92 # "yes", all GCC multilib dir names will be ".".
94 # The output looks like
95 # #define MULTILIB_MATCHES "\
96 # SUBDIRECTORY OPTIONS;\
97 # ...
98 # "
99 # The SUBDIRECTORY is the subdirectory to use. The OPTIONS are
100 # multiple options separated by spaces. Each option may start with an
101 # exclamation point. gcc will consider each line in turn. If none of
102 # the options beginning with an exclamation point are present, and all
103 # of the other options are present, that subdirectory will be used.
104 # The order of the subdirectories is such that they can be created in
105 # order; that is, a subdirectory is preceded by all its parents.
107 # Here is an example (this is from the actual sparc64 case):
108 # genmultilib 'm64/m32 mno-app-regs|mcmodel=medany' '64 32 alt'
109 # 'mcmodel?medany=mcmodel?medmid' 'm32/mno-app-regs* m32/mcmodel=*'
110 # '' 'm32/!m64/mno-app-regs m32/!m64/mcmodel=medany'
111 # '../lib64 ../lib32 alt' '' '' '' yes
112 # This produces:
113 # ". !m64 !m32 !mno-app-regs !mcmodel=medany;",
114 # "64:../lib64 m64 !m32 !mno-app-regs !mcmodel=medany;",
115 # "32:../lib32 !m64 m32 !mno-app-regs !mcmodel=medany;",
116 # "alt !m64 !m32 mno-app-regs mcmodel=medany;",
117 # "alt !m64 !m32 mno-app-regs !mcmodel=medany;",
118 # "alt !m64 !m32 !mno-app-regs mcmodel=medany;",
119 # "64/alt:../lib64/alt m64 !m32 mno-app-regs mcmodel=medany;",
120 # "64/alt:../lib64/alt m64 !m32 mno-app-regs !mcmodel=medany;",
121 # "64/alt:../lib64/alt m64 !m32 !mno-app-regs mcmodel=medany;",
123 # The effect is that `gcc -mno-app-regs' (for example) will append "alt"
124 # to the directory name when searching for libraries or startup files and
125 # `gcc -m32 -mcmodel=medany' (for example) will append "32/alt". Also note
126 # that exclusion above is moot, unless the compiler had a default of -m32,
127 # which would mean that all of the "alt" directories (not the 64/alt ones)
128 # would be ignored (not generated, nor used) since the exclusion also
129 # matches the multilib_default args.
131 # Copy the positional parameters into variables.
132 options=$1
133 dirnames=$2
134 matches=$3
135 exceptions=$4
136 extra=$5
137 exclusions=$6
138 osdirnames=$7
139 multilib_required=$8
140 multiarch=$9
141 multilib_reuse=${10}
142 enable_multilib=${11}
144 echo "static const char *const multilib_raw[] = {"
146 mkdir tmpmultilib.$$ || exit 1
147 # Use cd ./foo to avoid CDPATH output.
148 cd ./tmpmultilib.$$ || exit 1
150 # What we want to do is select all combinations of the sets in
151 # options. Each combination which includes a set of mutually
152 # exclusive options must then be output multiple times, once for each
153 # item in the set. Selecting combinations is a recursive process.
154 # Since not all versions of sh support functions, we achieve recursion
155 # by creating a temporary shell script which invokes itself.
156 rm -f tmpmultilib
157 cat >tmpmultilib <<\EOF
158 #!/bin/sh
159 # This recursive script basically outputs all combinations of its
160 # input arguments, handling mutually exclusive sets of options by
161 # repetition. When the script is called, ${initial} is the list of
162 # options which should appear before all combinations this will
163 # output. The output looks like a list of subdirectory names with
164 # leading and trailing slashes.
165 if [ "$#" != "0" ]; then
166 first=$1
167 shift
168 case "$first" in
169 *\|*)
170 all=${initial}`echo $first | sed -e 's_|_/_'g`
171 first=`echo $first | sed -e 's_|_ _'g`
172 echo ${all}/
173 initial="${initial}${all}/" ./tmpmultilib $@
174 ./tmpmultilib $first $@ | grep -v "^${all}"
177 for opt in `echo $first | sed -e 's|/| |'g`; do
178 echo ${initial}${opt}/
179 done
180 ./tmpmultilib $@
181 for opt in `echo $first | sed -e 's|/| |'g`; do
182 initial="${initial}${opt}/" ./tmpmultilib $@
183 done
184 esac
187 chmod +x tmpmultilib
189 combination_space=`initial=/ ./tmpmultilib ${options}`
190 combinations="$combination_space"
192 # If there exceptions, weed them out now
193 if [ -n "${exceptions}" ]; then
194 cat >tmpmultilib2 <<\EOF
195 #!/bin/sh
196 # This recursive script weeds out any combination of multilib
197 # switches that should not be generated. The output looks like
198 # a list of subdirectory names with leading and trailing slashes.
200 for opt in $@; do
201 case "$opt" in
204 for except in ${exceptions}; do
205 echo " /${except}/) : ;;" >> tmpmultilib2
206 done
208 cat >>tmpmultilib2 <<\EOF
209 *) echo ${opt};;
210 esac
211 done
213 chmod +x tmpmultilib2
214 combinations=`./tmpmultilib2 ${combinations}`
217 # If the MULTILIB_REQUIRED list are provided,
218 # filter out combinations not in this list.
219 if [ -n "${multilib_required}" ]; then
220 cat >tmpmultilib2 <<\EOF
221 #!/bin/sh
222 # This recursive script weeds out any combination of multilib
223 # switches that not in the expected list.
225 for opt in $@; do
226 case "$opt" in
229 for expect in ${multilib_required}; do
230 echo " /${expect}/) echo \${opt};;" >> tmpmultilib2
231 done
233 cat >>tmpmultilib2 <<\EOF
234 *) ;;
235 esac
236 done
239 chmod +x tmpmultilib2
240 combinations=`./tmpmultilib2 ${combinations}`
244 # Construct a sed pattern which will convert option names to directory
245 # names.
246 todirnames=
247 if [ -n "${dirnames}" ]; then
248 set x ${dirnames}
249 shift
250 for set in ${options}; do
251 for opts in `echo ${set} | sed -e 's|/| |'g`; do
252 patt="/"
253 for opt in `echo ${opts} | sed -e 's_|_ _'g`; do
254 if [ "$1" != "${opt}" ]; then
255 todirnames="${todirnames} -e s|/${opt}/|/${1}/|g"
256 patt="${patt}${1}/"
257 if [ "${patt}" != "/${1}/" ]; then
258 todirnames="${todirnames} -e s|${patt}|/${1}/|g"
261 done
262 shift
263 done
264 done
267 # Construct a sed pattern which will convert option names to OS directory
268 # names.
269 toosdirnames=
270 defaultosdirname=
271 defaultosdirname2=
272 if [ -n "${multiarch}" ]; then
273 defaultosdirname=::${multiarch}
275 if [ -n "${osdirnames}" ]; then
276 set x ${osdirnames}
277 shift
278 while [ $# != 0 ] ; do
279 case "$1" in
280 .=*)
281 defaultosdirname=`echo $1 | sed 's|^.=|:|'`
282 if [ -n "${multiarch}" ]; then
283 defaultosdirname=${defaultosdirname}:${multiarch}
285 case "$defaultosdirname" in
286 ::*) ;;
288 defaultosdirname2=${defaultosdirname}
289 defaultosdirname=
291 esac
292 shift
294 *=*)
295 patt=`echo $1 | sed -e 's|=|/$=/|'`
296 toosdirnames="${toosdirnames} -e s=^/${patt}/="
297 shift
300 break
302 esac
303 done
305 if [ $# != 0 ]; then
306 for set in ${options}; do
307 for opts in `echo ${set} | sed -e 's|/| |'g`; do
308 patt="/"
309 for opt in `echo ${opts} | sed -e 's_|_ _'g`; do
310 if [ "$1" != "${opt}" ]; then
311 toosdirnames="${toosdirnames} -e s|/${opt}/|/${1}/|g"
312 patt="${patt}${1}/"
313 if [ "${patt}" != "/${1}/" ]; then
314 toosdirnames="${toosdirnames} -e s|${patt}|/${1}/|g"
317 done
318 shift
319 done
320 done
324 # We need another recursive shell script to correctly handle positive
325 # matches. If we are invoked as
326 # genmultilib "opt1 opt2" "" "opt1=nopt1 opt2=nopt2"
327 # we must output
328 # opt1/opt2 opt1 opt2
329 # opt1/opt2 nopt1 opt2
330 # opt1/opt2 opt1 nopt2
331 # opt1/opt2 nopt1 nopt2
332 # In other words, we must output all combinations of matches.
333 rm -f tmpmultilib2
334 cat >tmpmultilib2 <<\EOF
335 #!/bin/sh
336 # The positional parameters are a list of matches to consider.
337 # ${dirout} is the directory name and ${optout} is the current list of
338 # options.
339 if [ "$#" = "0" ]; then
340 echo "\"${dirout} ${optout};\","
341 else
342 first=$1
343 shift
344 dirout="${dirout}" optout="${optout}" ./tmpmultilib2 $@
345 l=`echo ${first} | sed -e 's/=.*$//' -e 's/?/=/g'`
346 r=`echo ${first} | sed -e 's/^.*=//' -e 's/?/=/g'`
347 if expr " ${optout} " : ".* ${l} .*" > /dev/null; then
348 newopt=`echo " ${optout} " | sed -e "s/ ${l} / ${r} /" -e 's/^ //' -e 's/ $//'`
349 dirout="${dirout}" optout="${newopt}" ./tmpmultilib2 $@
353 chmod +x tmpmultilib2
355 # Start with the current directory, which includes only negations.
356 optout=
357 for set in ${options}; do
358 for opt in `echo ${set} | sed -e 's_[/|]_ _g'`; do
359 optout="${optout} !${opt}"
360 done
361 done
362 optout=`echo ${optout} | sed -e 's/^ //'`
363 echo "\".${defaultosdirname} ${optout};\","
364 [ -n "${defaultosdirname2}" ] && echo "\".${defaultosdirname2} ${optout};\","
366 # This part of code convert an option combination to
367 # its corresponding directory names.
368 # The directory names will be deduced from MULTILIB_DIRNAMES,
369 # MULTILIB_OSDIRNAMES or the option combination itself.
370 rm -rf tmpmultilib3
371 cat >tmpmultilib3 <<\EOF
372 #!/bin/sh
374 dirout=
375 combo=$1
376 todirnames=$2
377 toosdirnames=$3
378 enable_multilib=$4
380 if [ -n "${todirnames}" ]; then
381 dirout=`echo ${combo} | sed ${todirnames}`
382 else
383 dirout=`echo ${combo} | sed -e 's/=/-/g'`
385 # Remove the leading and trailing slashes.
386 dirout=`echo ${dirout} | sed -e 's|^/||' -e 's|/*:/*|:|' -e 's|/$||g'`
388 # Use the OS directory names rather than the option names.
389 if [ -n "${toosdirnames}" ]; then
390 osdirout=`echo ${combo} | sed ${toosdirnames}`
391 # Remove the leading and trailing slashes.
392 osdirout=`echo ${osdirout} | sed -e 's|^/||' -e 's|/*:/*|:|' -e 's|/$||g'`
393 if [ "x${enable_multilib}" != xyes ]; then
394 dirout=".:${osdirout}"
395 disable_multilib=yes
396 else
397 case "${osdirout}" in
399 dirout=`echo ${osdirout} | sed 's/^!//'`
402 dirout="${dirout}:${osdirout}"
404 esac
406 else
407 if [ "x${enable_multilib}" != xyes ]; then
408 # genmultilib with --disable-multilib should be
409 # called with '' '' '' '' '' '' '' no
410 # if MULTILIB_OSDIRNAMES is empty.
411 exit 1
414 echo "${dirout}"
416 chmod +x tmpmultilib3
418 # Script to look through the options and output each option that is present,
419 # and negate each option that is not present.
420 rm -rf tmpmultilib4
421 cat > tmpmultilib4 <<\EOF
422 #!/bin/sh
424 optout=
425 combo=$1
426 options=$2
428 for set in ${options}; do
429 setopts=`echo ${set} | sed -e 's_[/|]_ _g'`
430 for opt in ${setopts}; do
431 if expr "${combo} " : ".*/${opt}/.*" > /dev/null; then
432 optout="${optout} ${opt}"
433 else
434 optout="${optout} !${opt}"
436 done
437 done
438 optout=`echo ${optout} | sed -e 's/^ //'`
439 echo "${optout}"
441 chmod +x tmpmultilib4
443 # Work over the list of combinations. We have to translate each one
444 # to use the directory names rather than the option names, we have to
445 # include the information in matches, and we have to generate the
446 # correct list of options and negations.
447 for combo in ${combinations}; do
448 # Use the directory names rather than the option names.
449 dirout=`./tmpmultilib3 "${combo}" "${todirnames}" "${toosdirnames}" "${enable_multilib}"`
451 # Look through the options. We must output each option that is
452 # present, and negate each option that is not present.
453 optout=`./tmpmultilib4 "${combo}" "${options}"`
455 # Output the line with all appropriate matches.
456 dirout="${dirout}" optout="${optout}" ./tmpmultilib2
457 done
459 # Terminate the list of string.
460 echo "NULL"
461 echo "};"
463 # Output rules used for multilib reuse.
464 echo ""
465 echo "static const char *const multilib_reuse_raw[] = {"
466 for rrule in ${multilib_reuse}; do
467 # The left part of the rule are the options we used to build multilib.
468 # The right part of the rule are the options that can reuse this multilib.
469 combo=`echo ${rrule} | sed -e 's/=.*$//' -e 's/\./=/g'`
470 copts=`echo ${rrule} | sed -e 's/^.*=//' -e 's/\./=/g'`
471 # The variable ${combinations} are the option combinations we will build
472 # multilib from. If the combination in the left part of reuse rule isn't
473 # in this variable, it means no multilib will be built for current reuse
474 # rule. Thus the reuse purpose specified by current rule is meaningless.
475 if expr "${combinations} " : ".*/${combo}/.*" > /dev/null; then
476 if expr "${combination_space} " : ".*/${copts}/.*" > /dev/null; then
477 combo="/${combo}/"
478 dirout=`./tmpmultilib3 "${combo}" "${todirnames}" "${toosdirnames}" "${enable_multilib}"`
479 copts="/${copts}/"
480 optout=`./tmpmultilib4 "${copts}" "${options}"`
481 # Output the line with all appropriate matches.
482 dirout="${dirout}" optout="${optout}" ./tmpmultilib2
483 else
484 echo "The rule ${rrule} contains an option absent from MULTILIB_OPTIONS." >&2
485 exit 1
487 else
488 echo "The rule ${rrule} is trying to reuse nonexistent multilib." >&2
489 exit 1
491 done
493 # Terminate the list of string.
494 echo "NULL"
495 echo "};"
497 # Output all of the matches now as option and that is the same as that, with
498 # a semicolon trailer. Include all of the normal options as well.
499 # Note, the format of the matches is reversed compared
500 # to what we want, so switch them around.
501 echo ""
502 echo "static const char *const multilib_matches_raw[] = {"
503 for match in ${matches}; do
504 l=`echo ${match} | sed -e 's/=.*$//' -e 's/?/=/g'`
505 r=`echo ${match} | sed -e 's/^.*=//' -e 's/?/=/g'`
506 echo "\"${r} ${l};\","
507 done
508 for set in ${options}; do
509 for opt in `echo ${set} | sed -e 's_[/|]_ _'g`; do
510 echo "\"${opt} ${opt};\","
511 done
512 done
513 echo "NULL"
514 echo "};"
516 # Output the default options now
517 echo ""
518 echo "static const char *multilib_extra = \"${extra}\";"
520 # Output the exclusion rules now
521 echo ""
522 echo "static const char *const multilib_exclusions_raw[] = {"
523 for rule in ${exclusions}; do
524 s=`echo ${rule} | sed -e 's,/, ,g'`
525 echo "\"${s};\","
526 done
527 echo "NULL"
528 echo "};"
530 # Output the options now
531 moptions=`echo ${options} | sed -e 's,[ ][ ]*, ,g'`
532 echo ""
533 echo "static const char *multilib_options = \"${moptions}\";"
535 # Finally output the disable flag if specified
536 if [ "x${disable_multilib}" = xyes ]; then
537 echo ""
538 echo "#define DISABLE_MULTILIB 1"
541 cd ..
542 rm -r tmpmultilib.$$
544 exit 0