1#! /bin/sh
2# Script to generate SYSROOT_SUFFIX_SPEC equivalent to MULTILIB_OSDIRNAMES
3# Arguments are MULTILIB_OSDIRNAMES, MULTILIB_OPTIONS, MULTILIB_MATCHES,
4# and MULTILIB_REUSE.
5
6# Copyright (C) 2009-2020 Free Software Foundation, Inc.
7
8# This file is part of GCC.
9
10# GCC is free software; you can redistribute it and/or modify it under
11# the terms of the GNU General Public License as published by the Free
12# Software Foundation; either version 3, or (at your option) any later
13# version.
14
15# GCC is distributed in the hope that it will be useful, but WITHOUT
16# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
17# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
18# for more details.
19
20# You should have received a copy of the GNU General Public License
21# along with GCC; see the file COPYING3.  If not see
22# <http://www.gnu.org/licenses/>.
23
24# This shell script produces a header file fragment that defines
25# SYSROOT_SUFFIX_SPEC.  It assumes that the sysroots will have the same
26# structure and names used by the multilibs.
27
28# Invocation:
29#   print-sysroot-suffix.sh \
30#          MULTILIB_OSDIRNAMES \
31#          MULTILIB_OPTIONS \
32#          MULTILIB_MATCHES \
33#          MULTILIB_REUSE
34#      > t-sysroot-suffix.h
35
36# The four options exactly correspond to the variables of the same
37# names defined in the t-sysroot-suffix tmake_file fragment.
38
39# Example:
40#   sh ./gcc/config/print-sysroot-suffix.sh "a=A" "a b/c/d" ""
41# =>
42#   #undef SYSROOT_SUFFIX_SPEC
43#   #define SYSROOT_SUFFIX_SPEC "" \
44#   "%{a:" \
45#     "%{b:A/b/;" \
46#     "c:A/c/;" \
47#     "d:A/d/;" \
48#     ":A/};" \
49#   ":}"
50
51# The script uses temporary subscripts in order to permit a recursive
52# algorithm without the use of functions.
53
54set -e
55
56dirnames="$1"
57options="$2"
58matches="$3"
59reuse="$4"
60
61cat > print-sysroot-suffix3.sh <<\EOF
62#! /bin/sh
63# Print all the multilib matches for this option
64result="$1"
65EOF
66for x in $matches; do
67  l=`echo $x | sed -e 's/=.*$//' -e 's/?/=/g'`
68  r=`echo $x | sed -e 's/^.*=//' -e 's/?/=/g'`
69  echo "[ \"\$1\" = \"$l\" ] && result=\"\$result|$r\"" >> print-sysroot-suffix3.sh
70done
71echo 'echo $result' >> print-sysroot-suffix3.sh
72chmod +x print-sysroot-suffix3.sh
73
74cat > print-sysroot-suffix2.sh <<\EOF
75#! /bin/sh
76# Recursive script to enumerate all multilib combinations, match against
77# multilib directories and output a spec string of the result.
78# Will fold identical trees.
79
80padding="$1"
81optstring="$2"
82shift 2
83n="\" \\
84$padding\""
85if [ $# = 0 ]; then
86  case $optstring in
87EOF
88for x in $reuse; do
89  l=`echo $x | sed -e 's/=.*$//' -e 's/\./=/g'`
90  r=`echo $x | sed -e 's/^.*=//' -e 's/\./=/g'`
91  echo "/$r/) optstring=\"/$l/\" ;;" >> print-sysroot-suffix2.sh
92done
93echo "  esac" >> print-sysroot-suffix2.sh
94
95pat=
96for x in $dirnames; do
97  p=`echo $x | sed -e 's,=!,/$=/,'`
98  pat="$pat -e 's=^//$p='"
99done
100echo '  optstring=`echo "/$optstring" | sed '"$pat\`" >> print-sysroot-suffix2.sh
101cat >> print-sysroot-suffix2.sh <<\EOF
102  case $optstring in
103  //*)
104    ;;
105  *)
106    echo "$optstring"
107    ;;
108  esac
109else
110  thisopt="$1"
111  shift
112  bit=
113  lastcond=
114  result=
115  for x in `echo "$thisopt" | sed -e 's,/, ,g'`; do
116    case $x in
117EOF
118for x in `echo "$options" | sed -e 's,/, ,g'`; do
119  match=`./print-sysroot-suffix3.sh "$x"`
120  echo "$x) optmatch=\"$match\" ;;" >> print-sysroot-suffix2.sh
121done
122cat >> print-sysroot-suffix2.sh <<\EOF
123    esac
124    bit=`"$0" "$padding  " "$optstring$x/" "$@"`
125    if [ -z "$lastopt" ]; then
126      lastopt="$optmatch"
127    else
128      if [ "$lastbit" = "$bit" ]; then
129	lastopt="$lastopt|$optmatch"
130      else
131	result="$result$lastopt:$lastbit;$n"
132	lastopt="$optmatch"
133      fi
134    fi
135    lastbit="$bit"
136  done
137  bit=`"$0" "$padding  " "$optstring" "$@"`
138  if [ "$bit" = "$lastbit" ]; then
139    if [ -z "$result" ]; then
140      echo "$bit"
141    else
142      echo "$n%{$result:$bit}"
143    fi
144  else
145    echo "$n%{$result$lastopt:$lastbit;$n:$bit}"
146  fi
147fi
148EOF
149
150chmod +x ./print-sysroot-suffix2.sh
151result=`./print-sysroot-suffix2.sh "" "/" $options`
152echo "#undef SYSROOT_SUFFIX_SPEC"
153echo "#define SYSROOT_SUFFIX_SPEC \"$result\""
154rm print-sysroot-suffix2.sh
155rm print-sysroot-suffix3.sh
156