1#!/bin/sh 
2# Generates multilib.h.
3#   Copyright (C) 1994-2020 Free Software Foundation, Inc.
4
5#This file is part of GCC.
6
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.
11
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.
16
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/>.  
20
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.
24
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.
35
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.
42
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.
48
49# The optional fourth argument is a list of multilib directory 
50# combinations that should not be built.
51
52# The optional fifth argument is a list of options that should be
53# used whenever building multilib libraries.
54
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.
64
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.
74
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.
85
86# The optional ninth argument is the multiarch name.
87
88# The optional tenth argument specifies how to reuse multilib for different
89# option sets.
90
91# The last option should be "yes" if multilibs are enabled.  If it is not
92# "yes", all GCC multilib dir names will be ".".
93
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.
106
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;",
122#
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.
130
131# Copy the positional parameters into variables.
132options=$1
133dirnames=$2
134matches=$3
135exceptions=$4
136extra=$5
137exclusions=$6
138osdirnames=$7
139multilib_required=$8
140multiarch=$9
141multilib_reuse=${10}
142enable_multilib=${11}
143
144echo "static const char *const multilib_raw[] = {"
145
146mkdir tmpmultilib.$$ || exit 1
147# Use cd ./foo to avoid CDPATH output.
148cd ./tmpmultilib.$$ || exit 1
149
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.
156rm -f tmpmultilib
157cat >tmpmultilib <<EOF
158#!${CONFIG_SHELL:-/bin/sh}
159EOF
160cat >>tmpmultilib <<\EOF
161# This recursive script basically outputs all combinations of its
162# input arguments, handling mutually exclusive sets of options by
163# repetition.  When the script is called, ${initial} is the list of
164# options which should appear before all combinations this will
165# output.  The output looks like a list of subdirectory names with
166# leading and trailing slashes.
167if [ "$#" != "0" ]; then
168  first=$1
169  shift
170  case "$first" in
171  *\|*)
172    all=${initial}`echo $first | sed -e 's_|_/_'g`
173    first=`echo $first | sed -e 's_|_ _'g`
174    echo ${all}/
175    initial="${initial}${all}/" ./tmpmultilib $@
176    ./tmpmultilib $first $@ | grep -v "^${all}"
177    ;;
178  *)
179    for opt in `echo $first | sed -e 's|/| |'g`; do
180      echo ${initial}${opt}/
181    done
182    ./tmpmultilib $@
183    for opt in `echo $first | sed -e 's|/| |'g`; do
184      initial="${initial}${opt}/" ./tmpmultilib $@
185    done
186  esac
187fi
188EOF
189chmod +x tmpmultilib
190
191combinations=`initial=/ ./tmpmultilib ${options}`
192
193# If there exceptions, weed them out now
194if [ -n "${exceptions}" ]; then
195  cat >tmpmultilib2 <<EOF
196#!${CONFIG_SHELL:-/bin/sh}
197EOF
198  cat >>tmpmultilib2 <<\EOF
199# This recursive script weeds out any combination of multilib
200# switches that should not be generated.  The output looks like
201# a list of subdirectory names with leading and trailing slashes.
202
203  for opt in $@; do
204    case "$opt" in
205EOF
206
207  for except in ${exceptions}; do
208    echo "      /${except}/) : ;;" >> tmpmultilib2
209  done
210
211cat >>tmpmultilib2 <<\EOF
212      *) echo ${opt};;
213    esac
214  done
215EOF
216  chmod +x tmpmultilib2
217  combinations=`./tmpmultilib2 ${combinations}`
218fi
219
220# If the MULTILIB_REQUIRED list are provided,
221# filter out combinations not in this list.
222if [ -n "${multilib_required}" ]; then
223  cat >tmpmultilib2 <<\EOF
224#!/bin/sh
225# This recursive script weeds out any combination of multilib
226# switches that not in the expected list.
227
228  for opt in $@; do
229    case "$opt" in
230EOF
231
232  for expect in ${multilib_required}; do
233    echo "      /${expect}/) echo \${opt};;" >> tmpmultilib2
234  done
235
236cat >>tmpmultilib2 <<\EOF
237      *) ;;
238    esac
239  done
240EOF
241
242   chmod +x tmpmultilib2
243   combinations=`./tmpmultilib2 ${combinations}`
244
245fi
246
247# Construct a sed pattern which will convert option names to directory
248# names.
249todirnames=
250if [ -n "${dirnames}" ]; then
251  set x ${dirnames}
252  shift
253  for set in ${options}; do
254    for opts in `echo ${set} | sed -e 's|/| |'g`; do
255      patt="/"
256      for opt in `echo ${opts} | sed -e 's_|_ _'g`; do
257        if [ "$1" != "${opt}" ]; then
258          todirnames="${todirnames} -e s|/${opt}/|/${1}/|g"
259	  patt="${patt}${1}/"
260	  if [ "${patt}" != "/${1}/" ]; then
261	    todirnames="${todirnames} -e s|${patt}|/${1}/|g"
262          fi
263        fi
264      done
265      shift
266    done
267  done
268fi
269
270# Construct a sed pattern which will convert option names to OS directory
271# names.
272toosdirnames=
273defaultosdirname=
274defaultosdirname2=
275if [ -n "${multiarch}" ]; then
276  defaultosdirname=::${multiarch}
277fi
278if [ -n "${osdirnames}" ]; then
279  set x ${osdirnames}
280  shift
281  while [ $# != 0 ] ; do
282    case "$1" in
283      .=*)
284        defaultosdirname=`echo $1 | sed 's|^.=|:|'`
285	if [ -n "${multiarch}" ]; then
286	  defaultosdirname=${defaultosdirname}:${multiarch}
287	fi
288	case "$defaultosdirname" in
289	  ::*) ;;
290	  *)
291	    defaultosdirname2=${defaultosdirname}
292	    defaultosdirname=
293	    ;;
294	esac
295	shift
296	;;
297      *=*)
298	patt=`echo $1 | sed -e 's|=|/$=/|'`
299        toosdirnames="${toosdirnames} -e s=^/${patt}/="
300	shift
301        ;;
302      *)
303        break
304	;;
305    esac
306  done
307  
308  if [ $# != 0 ]; then
309    for set in ${options}; do
310      for opts in `echo ${set} | sed -e 's|/| |'g`; do
311        patt="/"
312        for opt in `echo ${opts} | sed -e 's_|_ _'g`; do
313          if [ "$1" != "${opt}" ]; then
314            toosdirnames="${toosdirnames} -e s|/${opt}/|/${1}/|g"
315	    patt="${patt}${1}/"
316	    if [ "${patt}" != "/${1}/" ]; then
317	      toosdirnames="${toosdirnames} -e s|${patt}|/${1}/|g"
318            fi
319          fi
320        done
321        shift
322      done
323    done
324  fi
325fi
326
327# We need another recursive shell script to correctly handle positive
328# matches.  If we are invoked as
329#   genmultilib "opt1 opt2" "" "opt1=nopt1 opt2=nopt2"
330# we must output
331#   opt1/opt2 opt1 opt2
332#   opt1/opt2 nopt1 opt2
333#   opt1/opt2 opt1 nopt2
334#   opt1/opt2 nopt1 nopt2
335# In other words, we must output all combinations of matches.
336rm -f tmpmultilib2
337cat >tmpmultilib2 <<EOF
338#!${CONFIG_SHELL:-/bin/sh}
339EOF
340cat >>tmpmultilib2 <<\EOF
341# The positional parameters are a list of matches to consider.
342# ${dirout} is the directory name and ${optout} is the current list of
343# options.
344if [ "$#" = "0" ]; then
345  echo "\"${dirout} ${optout};\","
346else
347  first=$1
348  shift
349  dirout="${dirout}" optout="${optout}" ./tmpmultilib2 $@
350  l=`echo ${first} | sed -e 's/=.*$//' -e 's/?/=/g'`
351  r=`echo ${first} | sed -e 's/^.*=//' -e 's/?/=/g'`
352  if expr " ${optout} " : ".* ${l} .*" > /dev/null; then
353    newopt=`echo " ${optout} " | sed -e "s/ ${l} / ${r} /" -e 's/^ //' -e 's/ $//'`
354    dirout="${dirout}" optout="${newopt}" ./tmpmultilib2 $@
355  fi
356fi
357EOF
358chmod +x tmpmultilib2
359
360# Start with the current directory, which includes only negations.
361optout=
362for set in ${options}; do
363  for opt in `echo ${set} | sed -e 's_[/|]_ _g'`; do
364    optout="${optout} !${opt}"
365  done
366done
367optout=`echo ${optout} | sed -e 's/^ //'`
368echo "\".${defaultosdirname} ${optout};\","
369[ -n "${defaultosdirname2}" ] && echo "\".${defaultosdirname2} ${optout};\","
370
371# This part of code convert an option combination to
372# its corresponding directory names.
373# The directory names will be deduced from MULTILIB_DIRNAMES,
374# MULTILIB_OSDIRNAMES or the option combination itself.
375rm -rf tmpmultilib3
376cat >tmpmultilib3 <<\EOF
377#!/bin/sh
378
379dirout=
380combo=$1
381todirnames=$2
382toosdirnames=$3
383enable_multilib=$4
384
385if [ -n "${todirnames}" ]; then
386  dirout=`echo ${combo} | sed ${todirnames}`
387else
388  dirout=`echo ${combo} | sed -e 's/=/-/g'`
389fi
390# Remove the leading and trailing slashes.
391dirout=`echo ${dirout} | sed -e 's|^/||' -e 's|/*:/*|:|' -e 's|/$||g'`
392
393# Use the OS directory names rather than the option names.
394if [ -n "${toosdirnames}" ]; then
395  osdirout=`echo ${combo} | sed ${toosdirnames}`
396  # Remove the leading and trailing slashes.
397  osdirout=`echo ${osdirout} | sed -e 's|^/||' -e 's|/*:/*|:|' -e 's|/$||g'`
398  if [ "x${enable_multilib}" != xyes ]; then
399    dirout=".:${osdirout}"
400    disable_multilib=yes
401  else
402    case "${osdirout}" in
403      !*)
404	dirout=`echo ${osdirout} | sed 's/^!//'`
405	;;
406       *)
407	dirout="${dirout}:${osdirout}"
408	;;
409    esac
410  fi
411else
412  if [ "x${enable_multilib}" != xyes ]; then
413    # genmultilib with --disable-multilib should be
414    # called with '' '' '' '' '' '' '' no
415    # if MULTILIB_OSDIRNAMES is empty.
416    exit 1
417  fi
418fi
419echo "${dirout}"
420EOF
421chmod +x tmpmultilib3
422
423# Script to look through the options and output each option that is present,
424# and negate each option that is not present.
425rm -rf tmpmultilib4
426cat > tmpmultilib4 <<\EOF
427#!/bin/sh
428
429optout=
430combo=$1
431options=$2
432
433for set in ${options}; do
434  setopts=`echo ${set} | sed -e 's_[/|]_ _g'`
435  for opt in ${setopts}; do
436    if expr "${combo} " : ".*/${opt}/.*" > /dev/null; then
437      optout="${optout} ${opt}"
438    else
439      optout="${optout} !${opt}"
440    fi
441  done
442done
443optout=`echo ${optout} | sed -e 's/^ //'`
444echo "${optout}"
445EOF
446chmod +x tmpmultilib4
447
448# Work over the list of combinations.  We have to translate each one
449# to use the directory names rather than the option names, we have to
450# include the information in matches, and we have to generate the
451# correct list of options and negations.
452for combo in ${combinations}; do
453  # Use the directory names rather than the option names.
454  dirout=`./tmpmultilib3 "${combo}" "${todirnames}" "${toosdirnames}" "${enable_multilib}"`
455
456  # Look through the options.  We must output each option that is
457  # present, and negate each option that is not present.
458  optout=`./tmpmultilib4 "${combo}" "${options}"`
459
460  # Output the line with all appropriate matches.
461  dirout="${dirout}" optout="${optout}" ./tmpmultilib2
462done
463
464# Terminate the list of string.
465echo "NULL"
466echo "};"
467
468# Generate a regular expression to validate option combinations.
469options_re=
470for set in ${options}; do
471  for opt in `echo ${set} | sed -e 's_[/|]_ _g' -e 's/+/./g' `; do
472    options_re="${options_re}${options_re:+|}${opt}"
473  done
474done
475options_re="^/((${options_re})/)*\$"
476
477# Output rules used for multilib reuse.
478echo ""
479echo "static const char *const multilib_reuse_raw[] = {"
480for rrule in ${multilib_reuse}; do
481  # The left part of the rule are the options we used to build multilib.
482  # The right part of the rule are the options that can reuse this multilib.
483  combo=`echo ${rrule} | sed -e 's/=.*$//' -e 's/\([^\\]\)\./\1=/g' -e 's/\\\././g'`
484  copts=`echo ${rrule} | sed -e 's/^.*=//' -e 's/\([^\\]\)\./\1=/g' -e 's/\\\././g'`
485  # The variable ${combinations} are the option combinations we will build
486  # multilib from.  If the combination in the left part of reuse rule isn't
487  # in this variable, it means no multilib will be built for current reuse
488  # rule.  Thus the reuse purpose specified by current rule is meaningless.
489  if expr "${combinations} " : ".*/${combo}/.*" > /dev/null; then
490    if echo "/${copts}/" | grep -E "${options_re}" > /dev/null; then
491      combo="/${combo}/"
492      dirout=`./tmpmultilib3 "${combo}" "${todirnames}" "${toosdirnames}" "${enable_multilib}"`
493      copts="/${copts}/"
494      optout=`./tmpmultilib4 "${copts}" "${options}"`
495      # Output the line with all appropriate matches.
496      dirout="${dirout}" optout="${optout}" ./tmpmultilib2
497    else
498      echo "The rule ${rrule} contains an option absent from MULTILIB_OPTIONS." >&2
499      exit 1
500    fi
501  else
502    echo "The rule ${rrule} is trying to reuse nonexistent multilib." >&2
503    exit 1
504  fi
505done
506
507# Terminate the list of string.
508echo "NULL"
509echo "};"
510
511# Output all of the matches now as option and that is the same as that, with
512# a semicolon trailer.  Include all of the normal options as well.
513# Note, the format of the matches is reversed compared
514# to what we want, so switch them around.
515echo ""
516echo "static const char *const multilib_matches_raw[] = {"
517for match in ${matches}; do
518  l=`echo ${match} | sed -e 's/=.*$//' -e 's/?/=/g'`
519  r=`echo ${match} | sed -e 's/^.*=//' -e 's/?/=/g'`
520  echo "\"${r} ${l};\","
521done
522for set in ${options}; do
523  for opt in `echo ${set} | sed -e 's_[/|]_ _'g`; do
524    echo "\"${opt} ${opt};\","
525  done
526done
527echo "NULL"
528echo "};"
529
530# Output the default options now
531echo ""
532echo "static const char *multilib_extra = \"${extra}\";"
533
534# Output the exclusion rules now
535echo ""
536echo "static const char *const multilib_exclusions_raw[] = {"
537for rule in ${exclusions}; do
538  s=`echo ${rule} | sed -e 's,/, ,g'`
539  echo "\"${s};\","
540done
541echo "NULL"
542echo "};"
543
544# Output the options now
545moptions=`echo ${options} | sed -e 's,[ 	][ 	]*, ,g'`
546echo ""
547echo "static const char *multilib_options = \"${moptions}\";"
548
549# Finally output the disable flag if specified
550if [ "x${disable_multilib}" = xyes ]; then
551  echo ""
552  echo "#define DISABLE_MULTILIB  1"
553fi
554
555cd ..
556rm -r tmpmultilib.$$
557
558exit 0
559