1#!/bin/sh 
2# Generates multilib.h.
3#   Copyright (C) 1994, 1995, 1996, 1997 Free Software Foundation, Inc.
4
5#This file is part of GNU CC.
6
7#GNU CC is free software; you can redistribute it and/or modify
8#it under the terms of the GNU General Public License as published by
9#the Free Software Foundation; either version 2, or (at your option)
10#any later version.
11
12#GNU CC is distributed in the hope that it will be useful,
13#but WITHOUT ANY WARRANTY; without even the implied warranty of
14#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15#GNU General Public License for more details.
16
17#You should have received a copy of the GNU General Public License
18#along with GNU CC; see the file COPYING.  If not, write to
19#the Free Software Foundation, 59 Temple Place - Suite 330,
20#Boston, MA 02111-1307, USA.
21
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.
25
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.  No leading dash is used on the options.
29# Each option in a set is mutually incompatible with all other options
30# in the set.
31
32# The optional second argument is a list of subdirectory names.  If
33# the second argument is non-empty, there must be as many elements in
34# the second argument as there are options in the first argument.  The
35# elements in the second list are separated by spaces.  If the second
36# argument is empty, the option names will be used as the directory
37# names.
38
39# The optional third argument is a list of options which are
40# identical.  The elements in the list are separated by spaces.  Each
41# element must be of the form OPTION=OPTION.  The first OPTION should
42# appear in the first argument, and the second should be a synonym for
43# it.  Question marks are replaced with equal signs in both options.
44
45# The optional fourth argument is a list of multilib directory 
46# combinations that should not be built.
47
48# The optional fifth argument is a list of options that should be
49# used whenever building multilib libraries.
50
51# The output looks like
52#   #define MULTILIB_MATCHES "\
53#   SUBDIRECTORY OPTIONS;\
54#   ...
55#   "
56# The SUBDIRECTORY is the subdirectory to use.  The OPTIONS are
57# multiple options separated by spaces.  Each option may start with an
58# exclamation point.  gcc will consider each line in turn.  If none of
59# the options beginning with an exclamation point are present, and all
60# of the other options are present, that subdirectory will be used.
61# The order of the subdirectories is such that they can be created in
62# order; that is, a subdirectory is preceded by all its parents.
63
64# Here is a example (this is simplified from the actual 680x0 case):
65#   genmultilib "m68000/m68020 msoft-float" "m68000 m68020 msoft-float"
66#		"m68000=mc68000"
67# This produces:
68#   ". !m68000 !mc68000 !m68020 !msoft-float;",
69#   "m68000 m68000 !m68020 !msoft-float;",
70#   "m68000 mc60000 !m68020 !msoft-float;",
71#   "m68020 !m68000 !mc68000 m68020 !msoft-float;",
72#   "msoft-float !m68000 !mc68000 !m68020 msoft-float;",
73#   "m68000/msoft-float m68000 !m68020 msoft-float;",
74#   "m68000/msoft-float mc68000 !m68020 msoft-float;",
75#   "m68020/msoft-float !m68000 !mc68000 m68020 msoft-float;",
76#
77# The effect is that `gcc -msoft-float' (for example) will append
78# msoft-float to the directory name when searching for libraries or
79# startup files, and `gcc -m68000 -msoft-float' (for example) will
80# append m68000/msoft-float.
81
82# Copy the positional parameters into variables.
83options=$1
84dirnames=$2
85matches=$3
86exceptions=$4
87extra=$5
88
89echo "static char *multilib_raw[] = {"
90
91# What we want to do is select all combinations of the sets in
92# options.  Each combination which includes a set of mutually
93# exclusive options must then be output multiple times, once for each
94# item in the set.  Selecting combinations is a recursive process.
95# Since not all versions of sh support functions, we achieve recursion
96# by creating a temporary shell script which invokes itself.
97rm -f tmpmultilib
98cat >tmpmultilib <<\EOF
99#!/bin/sh
100# This recursive script basically outputs all combinations of its
101# input arguments, handling mutually exclusive sets of options by
102# repetition.  When the script is called, ${initial} is the list of
103# options which should appear before all combinations this will
104# output.  The output looks like a list of subdirectory names with
105# leading and trailing slashes.
106if [ "$#" != "0" ]; then
107  first=$1
108  shift
109  for opt in `echo $first | sed -e 's|/| |'g`; do
110    echo ${initial}${opt}/
111  done
112  ./tmpmultilib $@
113  for opt in `echo $first | sed -e 's|/| |'g`; do
114    initial="${initial}${opt}/" ./tmpmultilib $@
115  done
116fi
117EOF
118chmod +x tmpmultilib
119
120combinations=`initial=/ ./tmpmultilib ${options}`
121
122rm -f tmpmultilib
123
124# If there exceptions, weed them out now
125if [ -n "${exceptions}" ]; then
126  rm -f tmpmultilib2
127  cat >tmpmultilib2 <<\EOF
128#!/bin/sh
129# This recursive script weeds out any combination of multilib
130# switches that should not be generated.  The output looks like
131# a list of subdirectory names with leading and trailing slashes.
132
133  for opt in $@; do
134    case "$opt" in
135EOF
136
137  for except in ${exceptions}; do
138    echo "      /${except}/) : ;;" >> tmpmultilib2
139  done
140
141cat >>tmpmultilib2 <<\EOF
142      *) echo ${opt};;
143    esac
144  done
145EOF
146  chmod +x tmpmultilib2
147  combinations=`./tmpmultilib2 ${combinations}`
148  rm -f ./tmpmultilib2
149fi
150
151# Construct a sed pattern which will convert option names to directory
152# names.
153todirnames=
154if [ -n "${dirnames}" ]; then
155  set x ${dirnames}
156  shift
157  for set in ${options}; do
158    for opt in `echo ${set} | sed -e 's|/| |'g`; do
159      if [ "$1" != "${opt}" ]; then
160        todirnames="${todirnames} -e s|/${opt}/|/${1}/|g"
161      fi
162      shift
163    done
164  done
165fi
166
167# We need another recursive shell script to correctly handle positive
168# matches.  If we are invoked as
169#   genmultilib "opt1 opt2" "" "opt1=nopt1 opt2=nopt2"
170# we must output
171#   opt1/opt2 opt1 opt2
172#   opt1/opt2 nopt1 opt2
173#   opt1/opt2 opt1 nopt2
174#   opt1/opt2 nopt1 nopt2
175# In other words, we must output all combinations of matches.
176rm -f tmpmultilib2
177cat >tmpmultilib2 <<\EOF
178#!/bin/sh
179# The positional parameters are a list of matches to consider.
180# ${dirout} is the directory name and ${optout} is the current list of
181# options.
182if [ "$#" = "0" ]; then
183  echo "\"${dirout} ${optout};\","
184else
185  first=$1
186  shift
187  dirout="${dirout}" optout="${optout}" ./tmpmultilib2 $@
188  l=`echo ${first} | sed -e 's/=.*$//' -e 's/?/=/g'`
189  r=`echo ${first} | sed -e 's/^.*=//' -e 's/?/=/g'`
190  if expr " ${optout} " : ".* ${l} .*" > /dev/null; then
191    newopt=`echo " ${optout} " | sed -e "s/ ${l} / ${r} /" -e 's/^ //' -e 's/ $//'`
192    dirout="${dirout}" optout="${newopt}" ./tmpmultilib2 $@
193  fi
194fi
195EOF
196chmod +x tmpmultilib2
197
198# Start with the current directory, which includes only negations.
199optout=
200for set in ${options}; do
201  for opt in `echo ${set} | sed -e 's|/| |'g`; do
202    optout="${optout} !${opt}"
203  done
204done
205optout=`echo ${optout} | sed -e 's/^ //'`
206echo "\". ${optout};\","
207
208# Work over the list of combinations.  We have to translate each one
209# to use the directory names rather than the option names, we have to
210# include the information in matches, and we have to generate the
211# correct list of options and negations.
212for combo in ${combinations}; do
213  # Use the directory names rather than the option names.
214  if [ -n "${todirnames}" ]; then
215    dirout=`echo ${combo} | sed ${todirnames}`
216  else
217    dirout=${combo}
218  fi
219  # Remove the leading and trailing slashes.
220  dirout=`echo ${dirout} | sed -e 's|^/||' -e 's|/$||g'`
221
222  # Look through the options.  We must output each option that is
223  # present, and negate each option that is not present.
224  optout=
225  for set in ${options}; do
226    setopts=`echo ${set} | sed -e 's|/| |g'`
227    for opt in ${setopts}; do
228      if expr "${combo} " : ".*/${opt}/.*" > /dev/null; then
229	optout="${optout} ${opt}"
230      else
231	optout="${optout} !${opt}"
232      fi
233    done
234  done
235  optout=`echo ${optout} | sed -e 's/^ //'`
236
237  # Output the line with all appropriate matches.
238  dirout="${dirout}" optout="${optout}" ./tmpmultilib2
239done
240
241# Terminate the list of string.
242echo "NULL"
243echo "};"
244
245# Output all of the matches now as option and that is the same as that, with
246# a semicolon trailer.  Include all of the normal options as well.
247# Note, the format of the matches is reversed compared
248# to what we want, so switch them around.
249echo ""
250echo "static char *multilib_matches_raw[] = {"
251for match in ${matches}; do
252  l=`echo ${match} | sed -e 's/=.*$//' -e 's/?/=/g'`
253  r=`echo ${match} | sed -e 's/^.*=//' -e 's/?/=/g'`
254  echo "\"${r} ${l};\","
255done
256for set in ${options}; do
257  for opt in `echo ${set} | sed -e 's|/| |'g`; do
258    echo "\"${opt} ${opt};\","
259  done
260done
261echo "NULL"
262echo "};"
263
264# Output the default options now
265echo ""
266echo "static char *multilib_extra = \"${extra}\";"
267rm -f tmpmultilib2
268
269exit 0
270