Change output to be a sequence of short strings
[gcc.git] / gcc / genmultilib
1 #!/bin/sh
2 # Generates multilib.h.
3 # Copyright (C) 1994, 1995 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 output looks like
49 # #define MULTILIB_MATCHES "\
50 # SUBDIRECTORY OPTIONS;\
51 # ...
52 # "
53 # The SUBDIRECTORY is the subdirectory to use. The OPTIONS are
54 # multiple options separated by spaces. Each option may start with an
55 # exclamation point. gcc will consider each line in turn. If none of
56 # the options beginning with an exclamation point are present, and all
57 # of the other options are present, that subdirectory will be used.
58 # The order of the subdirectories is such that they can be created in
59 # order; that is, a subdirectory is preceded by all its parents.
60
61 # Here is a example (this is simplified from the actual 680x0 case):
62 # genmultilib "m68000/m68020 msoft-float" "m68000 m68020 msoft-float"
63 # "m68000=mc68000"
64 # This produces:
65 # ". !m68000 !mc68000 !m68020 !msoft-float;",
66 # "m68000 m68000 !m68020 !msoft-float;",
67 # "m68000 mc60000 !m68020 !msoft-float;",
68 # "m68020 !m68000 !mc68000 m68020 !msoft-float;",
69 # "msoft-float !m68000 !mc68000 !m68020 msoft-float;",
70 # "m68000/msoft-float m68000 !m68020 msoft-float;",
71 # "m68000/msoft-float mc68000 !m68020 msoft-float;",
72 # "m68020/msoft-float !m68000 !mc68000 m68020 msoft-float;",
73 #
74 # The effect is that `gcc -msoft-float' (for example) will append
75 # msoft-float to the directory name when searching for libraries or
76 # startup files, and `gcc -m68000 -msoft-float' (for example) will
77 # append m68000/msoft-float.
78
79 # Copy the positional parameters into variables.
80 options=$1
81 dirnames=$2
82 matches=$3
83 exceptions=$4
84
85 # What we want to do is select all combinations of the sets in
86 # options. Each combination which includes a set of mutually
87 # exclusive options must then be output multiple times, once for each
88 # item in the set. Selecting combinations is a recursive process.
89 # Since not all versions of sh support functions, we achieve recursion
90 # by creating a temporary shell script which invokes itself.
91 rm -f tmpmultilib
92 cat >tmpmultilib <<\EOF
93 #!/bin/sh
94 # This recursive script basically outputs all combinations of its
95 # input arguments, handling mutually exclusive sets of options by
96 # repetition. When the script is called, ${initial} is the list of
97 # options which should appear before all combinations this will
98 # output. The output looks like a list of subdirectory names with
99 # leading and trailing slashes.
100 if [ "$#" != "0" ]; then
101 first=$1
102 shift
103 for opt in `echo $first | sed -e 's|/| |'g`; do
104 echo ${initial}${opt}/
105 done
106 ./tmpmultilib $@
107 for opt in `echo $first | sed -e 's|/| |'g`; do
108 initial="${initial}${opt}/" ./tmpmultilib $@
109 done
110 fi
111 EOF
112 chmod +x tmpmultilib
113
114 combinations=`initial=/ ./tmpmultilib ${options}`
115
116 rm -f tmpmultilib
117
118 # If there exceptions, weed them out now
119 if [ -n "${exceptions}" ]; then
120 rm -f tmpmultilib2
121 cat >tmpmultilib2 <<\EOF
122 #!/bin/sh
123 # This recursive script weeds out any combination of multilib
124 # switches that should not be generated. The output looks like
125 # a list of subdirectory names with leading and trailing slashes.
126
127 for opt in $@; do
128 case "$opt" in
129 EOF
130
131 for except in ${exceptions}; do
132 echo " /${except}/) : ;;" >> tmpmultilib2
133 done
134
135 cat >>tmpmultilib2 <<\EOF
136 *) echo ${opt};;
137 esac
138 done
139 EOF
140 chmod +x tmpmultilib2
141 combinations=`./tmpmultilib2 ${combinations}`
142 rm -f ./tmpmultilib2
143 fi
144
145 # Construct a sed pattern which will convert option names to directory
146 # names.
147 todirnames=
148 if [ -n "${dirnames}" ]; then
149 set x ${dirnames}
150 shift
151 for set in ${options}; do
152 for opt in `echo ${set} | sed -e 's|/| |'g`; do
153 if [ "$1" != "${opt}" ]; then
154 todirnames="${todirnames} -e s|/${opt}/|/${1}/|g"
155 fi
156 shift
157 done
158 done
159 fi
160
161 # Construct a sed pattern which will add negations based on the
162 # matches. The semicolons are easier than getting the shell to accept
163 # quoted spaces when expanding a variable.
164 matchnegations=
165 for i in ${matches}; do
166 l=`echo $i | sed -e 's/=.*$//' -e 's/?/=/g'`
167 r=`echo $i | sed -e 's/^.*=//' -e 's/?/=/g'`
168 matchnegations="${matchnegations} -e s/;!${l};/;!${l};!${r};/"
169 done
170
171 # We need another recursive shell script to correctly handle positive
172 # matches. If we are invoked as
173 # genmultilib "opt1 opt2" "" "opt1=nopt1 opt2=nopt2"
174 # we must output
175 # opt1/opt2 opt1 opt2
176 # opt1/opt2 nopt1 opt2
177 # opt1/opt2 opt1 nopt2
178 # opt1/opt2 nopt1 nopt2
179 # In other words, we must output all combinations of matches.
180 rm -f tmpmultilib2
181 cat >tmpmultilib2 <<\EOF
182 #!/bin/sh
183 # The positional parameters are a list of matches to consider.
184 # ${dirout} is the directory name and ${optout} is the current list of
185 # options.
186 if [ "$#" = "0" ]; then
187 echo "\"${dirout} ${optout};\","
188 else
189 first=$1
190 shift
191 dirout="${dirout}" optout="${optout}" ./tmpmultilib2 $@
192 l=`echo ${first} | sed -e 's/=.*$//' -e 's/?/=/g'`
193 r=`echo ${first} | sed -e 's/^.*=//' -e 's/?/=/g'`
194 if expr " ${optout} " : ".* ${l} .*" > /dev/null; then
195 newopt=`echo " ${optout} " | sed -e "s/ ${l} / ${r} /" -e 's/^ //' -e 's/ $//'`
196 dirout="${dirout}" optout="${newopt}" ./tmpmultilib2 $@
197 fi
198 fi
199 EOF
200 chmod +x tmpmultilib2
201
202 # Start with the current directory, which includes only negations.
203 optout=
204 for set in ${options}; do
205 for opt in `echo ${set} | sed -e 's|/| |'g`; do
206 optout="${optout} !${opt}"
207 done
208 done
209 optout=`echo ${optout} | sed -e 's/^ //'`
210 if [ -n "${matchnegations}" ]; then
211 optout=`echo ";${optout};" | sed -e 's/ /;/g' ${matchnegations} -e 's/^;//' -e 's/;$//' -e 's/;/ /g'`
212 fi
213 echo "\". ${optout};\","
214
215 # Work over the list of combinations. We have to translate each one
216 # to use the directory names rather than the option names, we have to
217 # include the information in matches, and we have to generate the
218 # correct list of options and negations.
219 for combo in ${combinations}; do
220 # Use the directory names rather than the option names.
221 if [ -n "${todirnames}" ]; then
222 dirout=`echo ${combo} | sed ${todirnames}`
223 else
224 dirout=${combo}
225 fi
226 # Remove the leading and trailing slashes.
227 dirout=`echo ${dirout} | sed -e 's|^/||' -e 's|/$||g'`
228
229 # Look through the options. We must output each option that is
230 # present, and negate each option that is not present.
231 optout=
232 for set in ${options}; do
233 setopts=`echo ${set} | sed -e 's|/| |g'`
234 for opt in ${setopts}; do
235 if expr "${combo} " : ".*/${opt}/.*" > /dev/null; then
236 optout="${optout} ${opt}"
237 else
238 optout="${optout} !${opt}"
239 fi
240 done
241 done
242 optout=`echo ${optout} | sed -e 's/^ //'`
243
244 # Add any negations of matches.
245 if [ -n "${matchnegations}" ]; then
246 optout=`echo ";${optout};" | sed -e 's/ /;/g' ${matchnegations} -e 's/^;//' -e 's/;$//' -e 's/;/ /g'`
247 fi
248
249 # Output the line with all appropriate matches.
250 dirout="${dirout}" optout="${optout}" ./tmpmultilib2 ${matches}
251 done
252
253 # Terminate the list of string.
254 echo "NULL"
255
256 rm -f tmpmultilib2
257
258 exit 0