1#! /bin/sh
2
3# Find name for a directory to build stuff in.
4# Usage
5#     ./findhost.sh $host <args to ./configure>
6# OR  ./findhost.sh $host --short
7
8# The idea here is that I want to name the directory by the Linux
9# distribution or other OS variant, and tag on parts that indicate
10# key configuration flags. This is necessary because different versions
11# even of the same basic system (eg Linux) are not in general mutually
12# binary compatible. Things are MUCH better in that respect than they
13# were a decade ago, but I can still not be 100% confident that a
14# system configured and built on Fedora will run on Ubuntu (or vice versa).
15# For the Mac I can not be certain that something built for one release
16# of the operating system will be suitable for the previous or next
17# release... etc.
18
19# I want this script to be one I can launch from anywhere, but at least
20# some of its sub-scripts will not be so generous. So find out where it
21# lives so that other locations can be found relative to that.
22
23if test "x$SED" = "x"
24then
25  if test -x /opt/sfw/bin/gsed
26  then SED=/opt/sfw/bin/gsed
27  elif test -x /usr/local/bin/gsed
28  then SED=/usr/local/bin/gsed
29  elif test -x /usr/bin/gsed
30  then SED=/usr/bin/gsed
31  else SED=sed
32  fi
33fi
34
35here="$0";while test -L "$here";do here=`ls -ld "$here" | sed 's/.*-> //'`;done
36here=`dirname "$here"`
37here=`cd "$here"; pwd -P`
38
39host=$1
40shift
41
42case $host in
43*apple-darwin*)
44# There are TWO things going on here. One is that the GNU script
45# "config.guess" seems to have changes recently between reporting i386 and
46# i686, so I wan to normalise. The second is that the build for the Mac
47# creates a "fat" universal binary that should run on either powerpc or
48# Intel Macs, and so to tag the build with one or the other architecture
49# may could as clumsy. This is an archaic issue now no longer used!
50  host=`echo $host | $SED -e s/i386/universal/`
51  host=`echo $host | $SED -e s/i686/universal/`
52  host=`echo $host | $SED -e s/powerpc/universal/`
53# Well the above is a bit "Historical", but with Apple "m1" (arm64)
54# being releases a whole fresh tranche of transition is upon us!
55  case $* in
56  *--with-mac-universal*)
57    host=`echo $host | $SED -e s/x86_64/universal/`
58    host=`echo $host | $SED -e s/aarch64/universal/`
59    ;;
60  *--with-mac-x86_64*)
61    host=`echo $host | $SED -e s/aarch64/x86_64/`
62    ;;
63  *--with-mac-arm64*)
64# Trying --with-mac-arm64 when running on an Intel-mac will probably not work.
65    host=`echo $host | $SED -e s/x86_65/aarch64/`
66    ;;
67  esac
68  ;;
69*)
70# The following line may help on FreeBSD where AC_CANONICAL_HOST
71# and config.guess apppear to have differing ideas. And in that case
72# please remember to use GNU make not the vanilla one.
73  host=`echo $host | $SED -e s/amd64/x86_64/`
74  ;;
75esac
76
77if test "x$1" = "x--short"
78then
79  variant=`"$here/findos.sh" short`
80else
81  variant=`"$here/findos.sh"`
82fi
83
84# I once tried to do special things for when running under msys
85# to use mingw - but I now withdraw that. For a build on Windows
86# please run under a cygwin bash shell. Here if the host is a cygwin variant
87# I map it to say just "windows" and will tend to end up using a mingw
88# compiler. Later on if "--with-cygwin" is set I will unwiind that.
89case $host in
90x86_64*cygwin* | x86_64*mingw*)
91  host="x86_64-pc-windows"
92  ;;
93*CYGWIN* | *Cygwin* | *cygwin* | *mingw*)
94  host="i686-pc-windows"
95  ;;
96esac
97
98# The exact transformations I apply are a matter of taste, I think. I
99# put things in a state that I view as tidy. Specifically instead of
100# calling something say i386-pc-linux-gnu I will call it i686-pc-suse103
101# or i686-pc-fedora7 indicating a concrete distribution. I want to do
102# that because Linux binaries do not guarantee compatibility between
103# distributions or even from release to release within a distribution.
104# Well things are a LOT better than they used to be, but I still do not
105# believe it is perfect. Hence the mere tag "linux-gnu" is inadequate.
106#
107# I find (HORROR) that different versions of "config.guess" can give
108# different results even on a single machine. Specifically on some OpenSuSE
109# systems I see x86_64-suse-linux-gnu with some copies of config.guess but
110# x86_64-unknown-linux-gnu using others. And there seem to be cases where
111# I may see "-pc-" in the middle rather than "-unknown-". This can really
112# lead to confusion. so I normalise cases I spot down to "-unknown-" which
113# seemd the safest base state to be in. Oh dear what a mess!
114
115if test "x$variant" != "xunknown"
116then
117  host=`echo $host | $SED -e s/-suse-linux/-unknown-linux/`
118  host=`echo $host | $SED -e s/-pc-linux/-unknown-linux/`
119  host=`echo $host | $SED -e s/linux-gnu/$variant/`
120  host=`echo $host | $SED -e s/apple/$variant/`
121fi
122
123# If the host name as found so far ended up with various unusual characters
124# in it that could upset some of my scripts, so I do a little filtering here.
125#host=`echo $host | $SED -e 's/[()<>{}[\];&\*?\$]/_/g'`
126host=`echo $host | $SED -e 's/[()<>{};&\*?\$|[]/_/g'`
127host=`echo $host | $SED -e 's/\]/_/g'`
128
129
130if test "x$1" = "x--short"
131then
132  echo $host
133  exit 0
134fi
135
136debug=
137test=
138arithlib=
139conservative=
140nothrow=
141m32=
142m64=
143nogui=
144original=$host
145fox=
146wx=
147
148# The decoding here is NOT PERFECT and will not be fully compatible with
149# all that the configure script does, but it should be sufficient for
150# common usage.
151
152for a in $*
153do
154  case $a in
155  --enable-debug=no | --disable-debug)
156    debug=
157    ;;
158  --enable-debug)
159    debug=-debug
160    ;;
161  --enable-test=no | --disable-test)
162    test=
163    ;;
164  --enable-test*)
165    test=-test
166    ;;
167  --enable-experiment=no | disable-experiment)
168    experiment=
169    ;;
170  --enable-experiment*)
171    experiment=-experiment
172    ;;
173  --with-arithlib=no | --without-arithlib)
174    arithlib=
175    ;;
176  --with-arithlib*)
177    arithlib=-arithlib
178    ;;
179  --enable-conservative=no | --disable-conservative)
180    conservative=
181    ;;
182  --with-throw=no | --without-throw)
183    nothrow=-nothrow
184    ;;
185  --enable-conservative*)
186    conservative=-conservative
187    ;;
188  --with-throw=no | --without-throw)
189    nothrow=nothrow
190    ;;
191  --with-mingw64=no | --without-mingw64)
192    host=$original
193    ;;
194  --with-mingw64*)
195    host=x86_64-pc-windows
196    ;;
197  --with-cygwin=no | --without-cygwin)
198    host=$original
199    ;;
200  --with-cygwin*)
201# Here I will map the host identity to signify 32 or 64-bit cygwin
202# in a way that I hope will be relevant.
203    case $host
204    in
205    *x86_64*)
206      host=x86_64-pc-cygwin
207      ;;
208    intel-*)
209      ;;
210    *)
211      host=i686-pc-cygwin
212      ;;
213    esac
214    ;;
215  --with-m32=no | --without-m32)
216    m32=
217    ;;
218  --with-m32*)
219    m32=-m32
220    m64=
221    ;;
222  --with-m64=no | --without-m64)
223    m64=
224    ;;
225  --with-m64*)
226    m64=-m64
227    m32=
228    ;;
229  --without-fox | --with-fox=no | --without-wx | --with-wx=no | --without-gui | --with-gui=no)
230    fox=
231    wx=
232    nogui=-nogui
233    ;;
234  --with-fox*)
235    fox=-fox
236    wx=
237    nogui=
238    ;;
239  --with-wx*)
240    fox=
241    wx=-wx
242    nogui=
243    ;;
244  esac
245done
246
247echo $host$m32$m64$nogui$fox$wx$test$experiment$arithlib$conservative$nothrow$debug
248
249exit 0
250