aboutsummaryrefslogtreecommitdiffstats
path: root/gcc-4.9/gcc/genmultilib
blob: 9e5e49b69fd8406612a653355814f6a42224f693 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
#!/bin/sh 
# Generates multilib.h.
#   Copyright (C) 1994-2014 Free Software Foundation, Inc.

#This file is part of GCC.

#GCC is free software; you can redistribute it and/or modify it under
#the terms of the GNU General Public License as published by the Free
#Software Foundation; either version 3, or (at your option) any later
#version.

#GCC is distributed in the hope that it will be useful, but WITHOUT
#ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
#FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
#for more details.

#You should have received a copy of the GNU General Public License
#along with GCC; see the file COPYING3.  If not see
#<http://www.gnu.org/licenses/>.  

# This shell script produces a header file which the gcc driver
# program uses to pick which library to use based on the machine
# specific options that it is given.

# The first argument is a list of sets of options.  The elements in
# the list are separated by spaces.  Within an element, the options
# are separated by slashes or pipes.  No leading dash is used on the
# options.
# Each option in a set separated by slashes is mutually incompatible
# with all other options
# in the set.
# Each option in a set separated by pipes will be used for the library
# compilation and any of the options in the set will be sufficient
# for it to be triggered.

# The optional second argument is a list of subdirectory names.  If
# the second argument is non-empty, there must be as many elements in
# the second argument as there are options in the first argument.  The
# elements in the second list are separated by spaces.  If the second
# argument is empty, the option names will be used as the directory
# names.

# The optional third argument is a list of options which are
# identical.  The elements in the list are separated by spaces.  Each
# element must be of the form OPTION=OPTION.  The first OPTION should
# appear in the first argument, and the second should be a synonym for
# it.  Question marks are replaced with equal signs in both options.

# The optional fourth argument is a list of multilib directory 
# combinations that should not be built.

# The optional fifth argument is a list of options that should be
# used whenever building multilib libraries.

# The optional sixth argument is a list of exclusions used internally by
# the compiler similar to exceptions. The difference being that exclusions
# allow matching default options that genmultilib does not know about and
# is done at runtime as opposed to being sorted out at compile time.
# Each element in the list is a separate exclusion rule. Each rule is
# a list of options (sans preceding '-') separated by a '/'. The options
# on the rule are grouped as an AND operation, and all options much match
# for the rule to exclude a set. Options can be preceded with a '!' to
# match a logical NOT.

# The optional seventh argument is a list of OS subdirectory names.
# The format is either the same as of the second argument, or a set of
# mappings. When it is the same as the second argument, it describes
# the multilib directories using OS conventions, rather than GCC
# conventions.  When it is a set of mappings of the form gccdir=osdir,
# the left side gives the GCC convention and the right gives the
# equivalent OS defined location.  If the osdir part begins with a !,
# the os directory names are used exclusively.  Use the mapping when
# there is no one-to-one equivalence between GCC levels and the OS.

# The optional eighth argument which intends to reduce the effort to write
# so many MULTILIB_EXCEPTIONS rules. This option defines a series of option
# combinations that we actually required.
# For some cases, the generated option combinations are far more than what
# we need, we have to write a lot of rules to screen out combinations we
# don't need. If we missed some rules, the unexpected libraries will be built.
# Now with this argument, one can simply give what combinations are needed.
# It is pretty straigtforward.
# This argument can be used together with MULTILIB_EXCEPTIONS and will take
# effect after the MULTILIB_EXCEPTIONS.

# The optional ninth argument is the multiarch name.

# The optional tenth argument specifies how to reuse multilib for different
# option sets.

# The last option should be "yes" if multilibs are enabled.  If it is not
# "yes", all GCC multilib dir names will be ".".

# The output looks like
#   #define MULTILIB_MATCHES "\
#   SUBDIRECTORY OPTIONS;\
#   ...
#   "
# The SUBDIRECTORY is the subdirectory to use.  The OPTIONS are
# multiple options separated by spaces.  Each option may start with an
# exclamation point.  gcc will consider each line in turn.  If none of
# the options beginning with an exclamation point are present, and all
# of the other options are present, that subdirectory will be used.
# The order of the subdirectories is such that they can be created in
# order; that is, a subdirectory is preceded by all its parents.

# Here is an example (this is from the actual sparc64 case):
#   genmultilib 'm64/m32 mno-app-regs|mcmodel=medany' '64 32 alt'
#		'mcmodel?medany=mcmodel?medmid' 'm32/mno-app-regs* m32/mcmodel=*'
#		'' 'm32/!m64/mno-app-regs m32/!m64/mcmodel=medany'
#		'../lib64 ../lib32 alt' '' '' '' yes
# This produces:
#   ". !m64 !m32 !mno-app-regs !mcmodel=medany;",
#   "64:../lib64 m64 !m32 !mno-app-regs !mcmodel=medany;",
#   "32:../lib32 !m64 m32 !mno-app-regs !mcmodel=medany;",
#   "alt !m64 !m32 mno-app-regs mcmodel=medany;",
#   "alt !m64 !m32 mno-app-regs !mcmodel=medany;",
#   "alt !m64 !m32 !mno-app-regs mcmodel=medany;",
#   "64/alt:../lib64/alt m64 !m32 mno-app-regs mcmodel=medany;",
#   "64/alt:../lib64/alt m64 !m32 mno-app-regs !mcmodel=medany;",
#   "64/alt:../lib64/alt m64 !m32 !mno-app-regs mcmodel=medany;",
#
# The effect is that `gcc -mno-app-regs' (for example) will append "alt"
# to the directory name when searching for libraries or startup files and
# `gcc -m32 -mcmodel=medany' (for example) will append "32/alt". Also note
# that exclusion above is moot, unless the compiler had a default of -m32,
# which would mean that all of the "alt" directories (not the 64/alt ones)
# would be ignored (not generated, nor used) since the exclusion also
# matches the multilib_default args.

# Copy the positional parameters into variables.
options=$1
dirnames=$2
matches=$3
exceptions=$4
extra=$5
exclusions=$6
osdirnames=$7
multilib_required=$8
multiarch=$9
multilib_reuse=${10}
enable_multilib=${11}

echo "static const char *const multilib_raw[] = {"

mkdir tmpmultilib.$$ || exit 1
# Use cd ./foo to avoid CDPATH output.
cd ./tmpmultilib.$$ || exit 1

# What we want to do is select all combinations of the sets in
# options.  Each combination which includes a set of mutually
# exclusive options must then be output multiple times, once for each
# item in the set.  Selecting combinations is a recursive process.
# Since not all versions of sh support functions, we achieve recursion
# by creating a temporary shell script which invokes itself.
rm -f tmpmultilib
cat >tmpmultilib <<\EOF
#!/bin/sh
# This recursive script basically outputs all combinations of its
# input arguments, handling mutually exclusive sets of options by
# repetition.  When the script is called, ${initial} is the list of
# options which should appear before all combinations this will
# output.  The output looks like a list of subdirectory names with
# leading and trailing slashes.
if [ "$#" != "0" ]; then
  first=$1
  shift
  case "$first" in
  *\|*)
    all=${initial}`echo $first | sed -e 's_|_/_'g`
    first=`echo $first | sed -e 's_|_ _'g`
    echo ${all}/
    initial="${initial}${all}/" ./tmpmultilib $@
    ./tmpmultilib $first $@ | grep -v "^${all}"
    ;;
  *)
    for opt in `echo $first | sed -e 's|/| |'g`; do
      echo ${initial}${opt}/
    done
    ./tmpmultilib $@
    for opt in `echo $first | sed -e 's|/| |'g`; do
      initial="${initial}${opt}/" ./tmpmultilib $@
    done
  esac
fi
EOF
chmod +x tmpmultilib

combinations=`initial=/ ./tmpmultilib ${options}`

# If there exceptions, weed them out now
if [ -n "${exceptions}" ]; then
  cat >tmpmultilib2 <<\EOF
#!/bin/sh
# This recursive script weeds out any combination of multilib
# switches that should not be generated.  The output looks like
# a list of subdirectory names with leading and trailing slashes.

  for opt in $@; do
    case "$opt" in
EOF

  for except in ${exceptions}; do
    echo "      /${except}/) : ;;" >> tmpmultilib2
  done

cat >>tmpmultilib2 <<\EOF
      *) echo ${opt};;
    esac
  done
EOF
  chmod +x tmpmultilib2
  combinations=`./tmpmultilib2 ${combinations}`
fi

# If the MULTILIB_REQUIRED list are provided,
# filter out combinations not in this list.
if [ -n "${multilib_required}" ]; then
  cat >tmpmultilib2 <<\EOF
#!/bin/sh
# This recursive script weeds out any combination of multilib
# switches that not in the expected list.

  for opt in $@; do
    case "$opt" in
EOF

  for expect in ${multilib_required}; do
    echo "      /${expect}/) echo \${opt};;" >> tmpmultilib2
  done

cat >>tmpmultilib2 <<\EOF
      *) ;;
    esac
  done
EOF

   chmod +x tmpmultilib2
   combinations=`./tmpmultilib2 ${combinations}`

fi

# Construct a sed pattern which will convert option names to directory
# names.
todirnames=
if [ -n "${dirnames}" ]; then
  set x ${dirnames}
  shift
  for set in ${options}; do
    for opts in `echo ${set} | sed -e 's|/| |'g`; do
      patt="/"
      for opt in `echo ${opts} | sed -e 's_|_ _'g`; do
        if [ "$1" != "${opt}" ]; then
          todirnames="${todirnames} -e s|/${opt}/|/${1}/|g"
	  patt="${patt}${1}/"
	  if [ "${patt}" != "/${1}/" ]; then
	    todirnames="${todirnames} -e s|${patt}|/${1}/|g"
          fi
        fi
      done
      shift
    done
  done
fi

# Construct a sed pattern which will convert option names to OS directory
# names.
toosdirnames=
defaultosdirname=
defaultosdirname2=
if [ -n "${multiarch}" ]; then
  defaultosdirname=::${multiarch}
fi
if [ -n "${osdirnames}" ]; then
  set x ${osdirnames}
  shift
  while [ $# != 0 ] ; do
    case "$1" in
      .=*)
        defaultosdirname=`echo $1 | sed 's|^.=|:|'`
	if [ -n "${multiarch}" ]; then
	  defaultosdirname=${defaultosdirname}:${multiarch}
	fi
	case "$defaultosdirname" in
	  ::*) ;;
	  *)
	    defaultosdirname2=${defaultosdirname}
	    defaultosdirname=
	    ;;
	esac
	shift
	;;
      *=*)
	patt=`echo $1 | sed -e 's|=|/$=/|'`
        toosdirnames="${toosdirnames} -e s=^/${patt}/="
	shift
        ;;
      *)
        break
	;;
    esac
  done
  
  if [ $# != 0 ]; then
    for set in ${options}; do
      for opts in `echo ${set} | sed -e 's|/| |'g`; do
        patt="/"
        for opt in `echo ${opts} | sed -e 's_|_ _'g`; do
          if [ "$1" != "${opt}" ]; then
            toosdirnames="${toosdirnames} -e s|/${opt}/|/${1}/|g"
	    patt="${patt}${1}/"
	    if [ "${patt}" != "/${1}/" ]; then
	      toosdirnames="${toosdirnames} -e s|${patt}|/${1}/|g"
            fi
          fi
        done
        shift
      done
    done
  fi
fi

# We need another recursive shell script to correctly handle positive
# matches.  If we are invoked as
#   genmultilib "opt1 opt2" "" "opt1=nopt1 opt2=nopt2"
# we must output
#   opt1/opt2 opt1 opt2
#   opt1/opt2 nopt1 opt2
#   opt1/opt2 opt1 nopt2
#   opt1/opt2 nopt1 nopt2
# In other words, we must output all combinations of matches.
rm -f tmpmultilib2
cat >tmpmultilib2 <<\EOF
#!/bin/sh
# The positional parameters are a list of matches to consider.
# ${dirout} is the directory name and ${optout} is the current list of
# options.
if [ "$#" = "0" ]; then
  echo "\"${dirout} ${optout};\","
else
  first=$1
  shift
  dirout="${dirout}" optout="${optout}" ./tmpmultilib2 $@
  l=`echo ${first} | sed -e 's/=.*$//' -e 's/?/=/g'`
  r=`echo ${first} | sed -e 's/^.*=//' -e 's/?/=/g'`
  if expr " ${optout} " : ".* ${l} .*" > /dev/null; then
    newopt=`echo " ${optout} " | sed -e "s/ ${l} / ${r} /" -e 's/^ //' -e 's/ $//'`
    dirout="${dirout}" optout="${newopt}" ./tmpmultilib2 $@
  fi
fi
EOF
chmod +x tmpmultilib2

# Start with the current directory, which includes only negations.
optout=
for set in ${options}; do
  for opt in `echo ${set} | sed -e 's_[/|]_ _g'`; do
    optout="${optout} !${opt}"
  done
done
optout=`echo ${optout} | sed -e 's/^ //'`
echo "\".${defaultosdirname} ${optout};\","
[ -n "${defaultosdirname2}" ] && echo "\".${defaultosdirname2} ${optout};\","

# This part of code convert an option combination to
# its corresponding directory names.
# The directory names will be deduced from MULTILIB_DIRNAMES,
# MULTILIB_OSDIRNAMES or the option combination itself.
rm -rf tmpmultilib3
cat >tmpmultilib3 <<\EOF
#!/bin/sh

dirout=
combo=$1
todirnames=$2
toosdirnames=$3
enable_multilib=$4

if [ -n "${todirnames}" ]; then
  dirout=`echo ${combo} | sed ${todirnames}`
else
  dirout=`echo ${combo} | sed -e 's/=/-/g'`
fi
# Remove the leading and trailing slashes.
dirout=`echo ${dirout} | sed -e 's|^/||' -e 's|/*:/*|:|' -e 's|/$||g'`

# Use the OS directory names rather than the option names.
if [ -n "${toosdirnames}" ]; then
  osdirout=`echo ${combo} | sed ${toosdirnames}`
  # Remove the leading and trailing slashes.
  osdirout=`echo ${osdirout} | sed -e 's|^/||' -e 's|/*:/*|:|' -e 's|/$||g'`
  if [ "x${enable_multilib}" != xyes ]; then
    dirout=".:${osdirout}"
    disable_multilib=yes
  else
    case "${osdirout}" in
      !*)
	dirout=`echo ${osdirout} | sed 's/^!//'`
	;;
       *)
	dirout="${dirout}:${osdirout}"
	;;
    esac
  fi
else
  if [ "x${enable_multilib}" != xyes ]; then
    # genmultilib with --disable-multilib should be
    # called with '' '' '' '' '' '' '' no
    # if MULTILIB_OSDIRNAMES is empty.
    exit 1
  fi
fi
echo "${dirout}"
EOF
chmod +x tmpmultilib3

# Script to look through the options and output each option that is present,
# and negate each option that is not present.
rm -rf tmpmultilib4
cat > tmpmultilib4 <<\EOF
#!/bin/sh

optout=
combo=$1
options=$2

for set in ${options}; do
  setopts=`echo ${set} | sed -e 's_[/|]_ _g'`
  for opt in ${setopts}; do
    if expr "${combo} " : ".*/${opt}/.*" > /dev/null; then
      optout="${optout} ${opt}"
    else
      optout="${optout} !${opt}"
    fi
  done
done
optout=`echo ${optout} | sed -e 's/^ //'`
echo "${optout}"
EOF
chmod +x tmpmultilib4

# Work over the list of combinations.  We have to translate each one
# to use the directory names rather than the option names, we have to
# include the information in matches, and we have to generate the
# correct list of options and negations.
for combo in ${combinations}; do
  # Use the directory names rather than the option names.
  dirout=`./tmpmultilib3 "${combo}" "${todirnames}" "${toosdirnames}" "${enable_multilib}"`

  # Look through the options.  We must output each option that is
  # present, and negate each option that is not present.
  optout=`./tmpmultilib4 "${combo}" "${options}"`

  # Output the line with all appropriate matches.
  dirout="${dirout}" optout="${optout}" ./tmpmultilib2
done

# Terminate the list of string.
echo "NULL"
echo "};"

# Output rules used for multilib reuse.
echo ""
echo "static const char *const multilib_reuse_raw[] = {"
for rrule in ${multilib_reuse}; do
  # The left part of the rule are the options we used to build multilib.
  # The right part of the rule are the options that can reuse this multilib.
  combo=`echo ${rrule} | sed -e 's/=.*$//' -e 's/\./=/g'`
  copts=`echo ${rrule} | sed -e 's/^.*=//' -e 's/\./=/g'`
  # The variable ${combinations} are the option combinations we will build
  # multilib from.  If the combination in the left part of reuse rule isn't
  # in this variable, it means no multilib will be built for current reuse
  # rule.  Thus the reuse purpose specified by current rule is meaningless.
  if expr "${combinations} " : ".*/${combo}/.*" > /dev/null; then
    combo="/${combo}/"
    dirout=`./tmpmultilib3 "${combo}" "${todirnames}" "${toosdirnames}" "${enable_multilib}"`
    copts="/${copts}/"
    optout=`./tmpmultilib4 "${copts}" "${options}"`
    # Output the line with all appropriate matches.
    dirout="${dirout}" optout="${optout}" ./tmpmultilib2
  else
    echo "The rule ${rrule} is trying to reuse nonexistent multilib."
    exit 1
  fi
done

# Terminate the list of string.
echo "NULL"
echo "};"

# Output all of the matches now as option and that is the same as that, with
# a semicolon trailer.  Include all of the normal options as well.
# Note, the format of the matches is reversed compared
# to what we want, so switch them around.
echo ""
echo "static const char *const multilib_matches_raw[] = {"
for match in ${matches}; do
  l=`echo ${match} | sed -e 's/=.*$//' -e 's/?/=/g'`
  r=`echo ${match} | sed -e 's/^.*=//' -e 's/?/=/g'`
  echo "\"${r} ${l};\","
done
for set in ${options}; do
  for opt in `echo ${set} | sed -e 's_[/|]_ _'g`; do
    echo "\"${opt} ${opt};\","
  done
done
echo "NULL"
echo "};"

# Output the default options now
echo ""
echo "static const char *multilib_extra = \"${extra}\";"

# Output the exclusion rules now
echo ""
echo "static const char *const multilib_exclusions_raw[] = {"
for rule in ${exclusions}; do
  s=`echo ${rule} | sed -e 's,/, ,g'`
  echo "\"${s};\","
done
echo "NULL"
echo "};"

# Output the options now
moptions=`echo ${options} | sed -e 's,[ 	][ 	]*, ,g'`
echo ""
echo "static const char *multilib_options = \"${moptions}\";"

# Finally output the disable flag if specified
if [ "x${disable_multilib}" = xyes ]; then
  echo ""
  echo "#define DISABLE_MULTILIB  1"
fi

cd ..
rm -r tmpmultilib.$$

exit 0