1#! /bin/sh
2# Script to generate SYSROOT_SUFFIX_SPEC equivalent to MULTILIB_OSDIRNAMES
3# Arguments are MULTILIB_OSDIRNAMES, MULTILIB_OPTIONS and MULTILIB_MATCHES.
4
5# Copyright (C) 2018-2020 Free Software Foundation, Inc.
6# Contributed by C-SKY Microsystems and Mentor Graphics.
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#      > t-sysroot-suffix.h
34
35# The three options exactly correspond to the variables of the same
36# names defined in the tmake_file fragments.
37
38# Example:
39#   sh ./gcc/config/print-sysroot-suffix.sh "a=A" "a b/c/d" ""
40# =>
41#   #undef SYSROOT_SUFFIX_SPEC
42#   #define SYSROOT_SUFFIX_SPEC "" \
43#   "%{a:" \
44#     "%{b:A/b/;" \
45#     "c:A/c/;" \
46#     "d:A/d/;" \
47#     ":A/};" \
48#   ":}"
49
50# The script uses temporary subscripts in order to permit a recursive
51# algorithm without the use of functions.
52
53set -e
54
55dirnames="$1"
56options="$2"
57matches="$3"
58
59cat > print-sysroot-suffix3.sh <<\EOF
60#! /bin/sh
61# Print all the multilib matches for this option
62result="$1"
63EOF
64for x in $matches; do
65  l=`echo $x | sed -e 's/=.*$//' -e 's/?/=/g'`
66  r=`echo $x | sed -e 's/^.*=//' -e 's/?/=/g'`
67  echo "[ \"\$1\" = \"$l\" ] && result=\"\$result|$r\"" >> print-sysroot-suffix3.sh
68done
69echo 'echo $result' >> print-sysroot-suffix3.sh
70chmod +x print-sysroot-suffix3.sh
71
72cat > print-sysroot-suffix2.sh <<\EOF
73#! /bin/sh
74# Recursive script to enumerate all multilib combinations, match against
75# multilib directories and output a spec string of the result.
76# Will fold identical trees.
77
78padding="$1"
79optstring="$2"
80shift 2
81n="\" \\
82$padding\""
83if [ $# = 0 ]; then
84EOF
85
86pat=
87for x in $dirnames; do
88#  p=`echo $x | sed -e 's,=!,/$=/,'`
89  p=`echo $x | sed -e 's/=//g'`
90#  pat="$pat -e 's=^//$p='"
91   pat="$pat -e 's/$p/g'"
92done
93echo '  optstring=`echo "/$optstring" | sed '"$pat\`" >> print-sysroot-suffix2.sh
94cat >> print-sysroot-suffix2.sh <<\EOF
95  case $optstring in
96  //*)
97    ;;
98  *)
99    echo "$optstring"
100    ;;
101  esac
102else
103  thisopt="$1"
104  shift
105  bit=
106  lastcond=
107  result=
108  for x in `echo "$thisopt" | sed -e 's,/, ,g'`; do
109    case $x in
110EOF
111for x in `echo "$options" | sed -e 's,/, ,g'`; do
112  match=`./print-sysroot-suffix3.sh "$x"`
113  echo "$x) optmatch=\"$match\" ;;" >> print-sysroot-suffix2.sh
114done
115cat >> print-sysroot-suffix2.sh <<\EOF
116    esac
117    bit=`"$0" "$padding  " "$optstring$x/" "$@"`
118    if [ -z "$lastopt" ]; then
119      lastopt="$optmatch"
120    else
121      if [ "$lastbit" = "$bit" ]; then
122	lastopt="$lastopt|$optmatch"
123      else
124	result="$result$lastopt:$lastbit;$n"
125	lastopt="$optmatch"
126      fi
127    fi
128    lastbit="$bit"
129  done
130  bit=`"$0" "$padding  " "$optstring" "$@"`
131  if [ "$bit" = "$lastbit" ]; then
132    if [ -z "$result" ]; then
133      echo "$bit"
134    else
135      echo "$n%{$result:$bit}"
136    fi
137  else
138    echo "$n%{$result$lastopt:$lastbit;$n:$bit}"
139  fi
140fi
141EOF
142chmod +x ./print-sysroot-suffix2.sh
143result=`./print-sysroot-suffix2.sh \"\" \"\" $options`
144echo "#undef SYSROOT_SUFFIX_SPEC"
145echo "#define SYSROOT_SUFFIX_SPEC \"$result\""
146rm print-sysroot-suffix2.sh
147rm print-sysroot-suffix3.sh
148