xref: /netbsd-src/external/public-domain/sqlite/dist/compile (revision 8b8b4e78f4f7215a0fd3cffa533e98cb1d127de7)
1*8b8b4e78Schristos#! /bin/sh
2*8b8b4e78Schristos# Wrapper for compilers which do not understand '-c -o'.
3*8b8b4e78Schristos
4*8b8b4e78Schristosscriptversion=2018-03-07.03; # UTC
5*8b8b4e78Schristos
6*8b8b4e78Schristos# Copyright (C) 1999-2021 Free Software Foundation, Inc.
7*8b8b4e78Schristos# Written by Tom Tromey <tromey@cygnus.com>.
8*8b8b4e78Schristos#
9*8b8b4e78Schristos# This program is free software; you can redistribute it and/or modify
10*8b8b4e78Schristos# it under the terms of the GNU General Public License as published by
11*8b8b4e78Schristos# the Free Software Foundation; either version 2, or (at your option)
12*8b8b4e78Schristos# any later version.
13*8b8b4e78Schristos#
14*8b8b4e78Schristos# This program is distributed in the hope that it will be useful,
15*8b8b4e78Schristos# but WITHOUT ANY WARRANTY; without even the implied warranty of
16*8b8b4e78Schristos# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17*8b8b4e78Schristos# GNU General Public License for more details.
18*8b8b4e78Schristos#
19*8b8b4e78Schristos# You should have received a copy of the GNU General Public License
20*8b8b4e78Schristos# along with this program.  If not, see <https://www.gnu.org/licenses/>.
21*8b8b4e78Schristos
22*8b8b4e78Schristos# As a special exception to the GNU General Public License, if you
23*8b8b4e78Schristos# distribute this file as part of a program that contains a
24*8b8b4e78Schristos# configuration script generated by Autoconf, you may include it under
25*8b8b4e78Schristos# the same distribution terms that you use for the rest of that program.
26*8b8b4e78Schristos
27*8b8b4e78Schristos# This file is maintained in Automake, please report
28*8b8b4e78Schristos# bugs to <bug-automake@gnu.org> or send patches to
29*8b8b4e78Schristos# <automake-patches@gnu.org>.
30*8b8b4e78Schristos
31*8b8b4e78Schristosnl='
32*8b8b4e78Schristos'
33*8b8b4e78Schristos
34*8b8b4e78Schristos# We need space, tab and new line, in precisely that order.  Quoting is
35*8b8b4e78Schristos# there to prevent tools from complaining about whitespace usage.
36*8b8b4e78SchristosIFS=" ""	$nl"
37*8b8b4e78Schristos
38*8b8b4e78Schristosfile_conv=
39*8b8b4e78Schristos
40*8b8b4e78Schristos# func_file_conv build_file lazy
41*8b8b4e78Schristos# Convert a $build file to $host form and store it in $file
42*8b8b4e78Schristos# Currently only supports Windows hosts. If the determined conversion
43*8b8b4e78Schristos# type is listed in (the comma separated) LAZY, no conversion will
44*8b8b4e78Schristos# take place.
45*8b8b4e78Schristosfunc_file_conv ()
46*8b8b4e78Schristos{
47*8b8b4e78Schristos  file=$1
48*8b8b4e78Schristos  case $file in
49*8b8b4e78Schristos    / | /[!/]*) # absolute file, and not a UNC file
50*8b8b4e78Schristos      if test -z "$file_conv"; then
51*8b8b4e78Schristos	# lazily determine how to convert abs files
52*8b8b4e78Schristos	case `uname -s` in
53*8b8b4e78Schristos	  MINGW*)
54*8b8b4e78Schristos	    file_conv=mingw
55*8b8b4e78Schristos	    ;;
56*8b8b4e78Schristos	  CYGWIN* | MSYS*)
57*8b8b4e78Schristos	    file_conv=cygwin
58*8b8b4e78Schristos	    ;;
59*8b8b4e78Schristos	  *)
60*8b8b4e78Schristos	    file_conv=wine
61*8b8b4e78Schristos	    ;;
62*8b8b4e78Schristos	esac
63*8b8b4e78Schristos      fi
64*8b8b4e78Schristos      case $file_conv/,$2, in
65*8b8b4e78Schristos	*,$file_conv,*)
66*8b8b4e78Schristos	  ;;
67*8b8b4e78Schristos	mingw/*)
68*8b8b4e78Schristos	  file=`cmd //C echo "$file " | sed -e 's/"\(.*\) " *$/\1/'`
69*8b8b4e78Schristos	  ;;
70*8b8b4e78Schristos	cygwin/* | msys/*)
71*8b8b4e78Schristos	  file=`cygpath -m "$file" || echo "$file"`
72*8b8b4e78Schristos	  ;;
73*8b8b4e78Schristos	wine/*)
74*8b8b4e78Schristos	  file=`winepath -w "$file" || echo "$file"`
75*8b8b4e78Schristos	  ;;
76*8b8b4e78Schristos      esac
77*8b8b4e78Schristos      ;;
78*8b8b4e78Schristos  esac
79*8b8b4e78Schristos}
80*8b8b4e78Schristos
81*8b8b4e78Schristos# func_cl_dashL linkdir
82*8b8b4e78Schristos# Make cl look for libraries in LINKDIR
83*8b8b4e78Schristosfunc_cl_dashL ()
84*8b8b4e78Schristos{
85*8b8b4e78Schristos  func_file_conv "$1"
86*8b8b4e78Schristos  if test -z "$lib_path"; then
87*8b8b4e78Schristos    lib_path=$file
88*8b8b4e78Schristos  else
89*8b8b4e78Schristos    lib_path="$lib_path;$file"
90*8b8b4e78Schristos  fi
91*8b8b4e78Schristos  linker_opts="$linker_opts -LIBPATH:$file"
92*8b8b4e78Schristos}
93*8b8b4e78Schristos
94*8b8b4e78Schristos# func_cl_dashl library
95*8b8b4e78Schristos# Do a library search-path lookup for cl
96*8b8b4e78Schristosfunc_cl_dashl ()
97*8b8b4e78Schristos{
98*8b8b4e78Schristos  lib=$1
99*8b8b4e78Schristos  found=no
100*8b8b4e78Schristos  save_IFS=$IFS
101*8b8b4e78Schristos  IFS=';'
102*8b8b4e78Schristos  for dir in $lib_path $LIB
103*8b8b4e78Schristos  do
104*8b8b4e78Schristos    IFS=$save_IFS
105*8b8b4e78Schristos    if $shared && test -f "$dir/$lib.dll.lib"; then
106*8b8b4e78Schristos      found=yes
107*8b8b4e78Schristos      lib=$dir/$lib.dll.lib
108*8b8b4e78Schristos      break
109*8b8b4e78Schristos    fi
110*8b8b4e78Schristos    if test -f "$dir/$lib.lib"; then
111*8b8b4e78Schristos      found=yes
112*8b8b4e78Schristos      lib=$dir/$lib.lib
113*8b8b4e78Schristos      break
114*8b8b4e78Schristos    fi
115*8b8b4e78Schristos    if test -f "$dir/lib$lib.a"; then
116*8b8b4e78Schristos      found=yes
117*8b8b4e78Schristos      lib=$dir/lib$lib.a
118*8b8b4e78Schristos      break
119*8b8b4e78Schristos    fi
120*8b8b4e78Schristos  done
121*8b8b4e78Schristos  IFS=$save_IFS
122*8b8b4e78Schristos
123*8b8b4e78Schristos  if test "$found" != yes; then
124*8b8b4e78Schristos    lib=$lib.lib
125*8b8b4e78Schristos  fi
126*8b8b4e78Schristos}
127*8b8b4e78Schristos
128*8b8b4e78Schristos# func_cl_wrapper cl arg...
129*8b8b4e78Schristos# Adjust compile command to suit cl
130*8b8b4e78Schristosfunc_cl_wrapper ()
131*8b8b4e78Schristos{
132*8b8b4e78Schristos  # Assume a capable shell
133*8b8b4e78Schristos  lib_path=
134*8b8b4e78Schristos  shared=:
135*8b8b4e78Schristos  linker_opts=
136*8b8b4e78Schristos  for arg
137*8b8b4e78Schristos  do
138*8b8b4e78Schristos    if test -n "$eat"; then
139*8b8b4e78Schristos      eat=
140*8b8b4e78Schristos    else
141*8b8b4e78Schristos      case $1 in
142*8b8b4e78Schristos	-o)
143*8b8b4e78Schristos	  # configure might choose to run compile as 'compile cc -o foo foo.c'.
144*8b8b4e78Schristos	  eat=1
145*8b8b4e78Schristos	  case $2 in
146*8b8b4e78Schristos	    *.o | *.[oO][bB][jJ])
147*8b8b4e78Schristos	      func_file_conv "$2"
148*8b8b4e78Schristos	      set x "$@" -Fo"$file"
149*8b8b4e78Schristos	      shift
150*8b8b4e78Schristos	      ;;
151*8b8b4e78Schristos	    *)
152*8b8b4e78Schristos	      func_file_conv "$2"
153*8b8b4e78Schristos	      set x "$@" -Fe"$file"
154*8b8b4e78Schristos	      shift
155*8b8b4e78Schristos	      ;;
156*8b8b4e78Schristos	  esac
157*8b8b4e78Schristos	  ;;
158*8b8b4e78Schristos	-I)
159*8b8b4e78Schristos	  eat=1
160*8b8b4e78Schristos	  func_file_conv "$2" mingw
161*8b8b4e78Schristos	  set x "$@" -I"$file"
162*8b8b4e78Schristos	  shift
163*8b8b4e78Schristos	  ;;
164*8b8b4e78Schristos	-I*)
165*8b8b4e78Schristos	  func_file_conv "${1#-I}" mingw
166*8b8b4e78Schristos	  set x "$@" -I"$file"
167*8b8b4e78Schristos	  shift
168*8b8b4e78Schristos	  ;;
169*8b8b4e78Schristos	-l)
170*8b8b4e78Schristos	  eat=1
171*8b8b4e78Schristos	  func_cl_dashl "$2"
172*8b8b4e78Schristos	  set x "$@" "$lib"
173*8b8b4e78Schristos	  shift
174*8b8b4e78Schristos	  ;;
175*8b8b4e78Schristos	-l*)
176*8b8b4e78Schristos	  func_cl_dashl "${1#-l}"
177*8b8b4e78Schristos	  set x "$@" "$lib"
178*8b8b4e78Schristos	  shift
179*8b8b4e78Schristos	  ;;
180*8b8b4e78Schristos	-L)
181*8b8b4e78Schristos	  eat=1
182*8b8b4e78Schristos	  func_cl_dashL "$2"
183*8b8b4e78Schristos	  ;;
184*8b8b4e78Schristos	-L*)
185*8b8b4e78Schristos	  func_cl_dashL "${1#-L}"
186*8b8b4e78Schristos	  ;;
187*8b8b4e78Schristos	-static)
188*8b8b4e78Schristos	  shared=false
189*8b8b4e78Schristos	  ;;
190*8b8b4e78Schristos	-Wl,*)
191*8b8b4e78Schristos	  arg=${1#-Wl,}
192*8b8b4e78Schristos	  save_ifs="$IFS"; IFS=','
193*8b8b4e78Schristos	  for flag in $arg; do
194*8b8b4e78Schristos	    IFS="$save_ifs"
195*8b8b4e78Schristos	    linker_opts="$linker_opts $flag"
196*8b8b4e78Schristos	  done
197*8b8b4e78Schristos	  IFS="$save_ifs"
198*8b8b4e78Schristos	  ;;
199*8b8b4e78Schristos	-Xlinker)
200*8b8b4e78Schristos	  eat=1
201*8b8b4e78Schristos	  linker_opts="$linker_opts $2"
202*8b8b4e78Schristos	  ;;
203*8b8b4e78Schristos	-*)
204*8b8b4e78Schristos	  set x "$@" "$1"
205*8b8b4e78Schristos	  shift
206*8b8b4e78Schristos	  ;;
207*8b8b4e78Schristos	*.cc | *.CC | *.cxx | *.CXX | *.[cC]++)
208*8b8b4e78Schristos	  func_file_conv "$1"
209*8b8b4e78Schristos	  set x "$@" -Tp"$file"
210*8b8b4e78Schristos	  shift
211*8b8b4e78Schristos	  ;;
212*8b8b4e78Schristos	*.c | *.cpp | *.CPP | *.lib | *.LIB | *.Lib | *.OBJ | *.obj | *.[oO])
213*8b8b4e78Schristos	  func_file_conv "$1" mingw
214*8b8b4e78Schristos	  set x "$@" "$file"
215*8b8b4e78Schristos	  shift
216*8b8b4e78Schristos	  ;;
217*8b8b4e78Schristos	*)
218*8b8b4e78Schristos	  set x "$@" "$1"
219*8b8b4e78Schristos	  shift
220*8b8b4e78Schristos	  ;;
221*8b8b4e78Schristos      esac
222*8b8b4e78Schristos    fi
223*8b8b4e78Schristos    shift
224*8b8b4e78Schristos  done
225*8b8b4e78Schristos  if test -n "$linker_opts"; then
226*8b8b4e78Schristos    linker_opts="-link$linker_opts"
227*8b8b4e78Schristos  fi
228*8b8b4e78Schristos  exec "$@" $linker_opts
229*8b8b4e78Schristos  exit 1
230*8b8b4e78Schristos}
231*8b8b4e78Schristos
232*8b8b4e78Schristoseat=
233*8b8b4e78Schristos
234*8b8b4e78Schristoscase $1 in
235*8b8b4e78Schristos  '')
236*8b8b4e78Schristos     echo "$0: No command.  Try '$0 --help' for more information." 1>&2
237*8b8b4e78Schristos     exit 1;
238*8b8b4e78Schristos     ;;
239*8b8b4e78Schristos  -h | --h*)
240*8b8b4e78Schristos    cat <<\EOF
241*8b8b4e78SchristosUsage: compile [--help] [--version] PROGRAM [ARGS]
242*8b8b4e78Schristos
243*8b8b4e78SchristosWrapper for compilers which do not understand '-c -o'.
244*8b8b4e78SchristosRemove '-o dest.o' from ARGS, run PROGRAM with the remaining
245*8b8b4e78Schristosarguments, and rename the output as expected.
246*8b8b4e78Schristos
247*8b8b4e78SchristosIf you are trying to build a whole package this is not the
248*8b8b4e78Schristosright script to run: please start by reading the file 'INSTALL'.
249*8b8b4e78Schristos
250*8b8b4e78SchristosReport bugs to <bug-automake@gnu.org>.
251*8b8b4e78SchristosEOF
252*8b8b4e78Schristos    exit $?
253*8b8b4e78Schristos    ;;
254*8b8b4e78Schristos  -v | --v*)
255*8b8b4e78Schristos    echo "compile $scriptversion"
256*8b8b4e78Schristos    exit $?
257*8b8b4e78Schristos    ;;
258*8b8b4e78Schristos  cl | *[/\\]cl | cl.exe | *[/\\]cl.exe | \
259*8b8b4e78Schristos  icl | *[/\\]icl | icl.exe | *[/\\]icl.exe )
260*8b8b4e78Schristos    func_cl_wrapper "$@"      # Doesn't return...
261*8b8b4e78Schristos    ;;
262*8b8b4e78Schristosesac
263*8b8b4e78Schristos
264*8b8b4e78Schristosofile=
265*8b8b4e78Schristoscfile=
266*8b8b4e78Schristos
267*8b8b4e78Schristosfor arg
268*8b8b4e78Schristosdo
269*8b8b4e78Schristos  if test -n "$eat"; then
270*8b8b4e78Schristos    eat=
271*8b8b4e78Schristos  else
272*8b8b4e78Schristos    case $1 in
273*8b8b4e78Schristos      -o)
274*8b8b4e78Schristos	# configure might choose to run compile as 'compile cc -o foo foo.c'.
275*8b8b4e78Schristos	# So we strip '-o arg' only if arg is an object.
276*8b8b4e78Schristos	eat=1
277*8b8b4e78Schristos	case $2 in
278*8b8b4e78Schristos	  *.o | *.obj)
279*8b8b4e78Schristos	    ofile=$2
280*8b8b4e78Schristos	    ;;
281*8b8b4e78Schristos	  *)
282*8b8b4e78Schristos	    set x "$@" -o "$2"
283*8b8b4e78Schristos	    shift
284*8b8b4e78Schristos	    ;;
285*8b8b4e78Schristos	esac
286*8b8b4e78Schristos	;;
287*8b8b4e78Schristos      *.c)
288*8b8b4e78Schristos	cfile=$1
289*8b8b4e78Schristos	set x "$@" "$1"
290*8b8b4e78Schristos	shift
291*8b8b4e78Schristos	;;
292*8b8b4e78Schristos      *)
293*8b8b4e78Schristos	set x "$@" "$1"
294*8b8b4e78Schristos	shift
295*8b8b4e78Schristos	;;
296*8b8b4e78Schristos    esac
297*8b8b4e78Schristos  fi
298*8b8b4e78Schristos  shift
299*8b8b4e78Schristosdone
300*8b8b4e78Schristos
301*8b8b4e78Schristosif test -z "$ofile" || test -z "$cfile"; then
302*8b8b4e78Schristos  # If no '-o' option was seen then we might have been invoked from a
303*8b8b4e78Schristos  # pattern rule where we don't need one.  That is ok -- this is a
304*8b8b4e78Schristos  # normal compilation that the losing compiler can handle.  If no
305*8b8b4e78Schristos  # '.c' file was seen then we are probably linking.  That is also
306*8b8b4e78Schristos  # ok.
307*8b8b4e78Schristos  exec "$@"
308*8b8b4e78Schristosfi
309*8b8b4e78Schristos
310*8b8b4e78Schristos# Name of file we expect compiler to create.
311*8b8b4e78Schristoscofile=`echo "$cfile" | sed 's|^.*[\\/]||; s|^[a-zA-Z]:||; s/\.c$/.o/'`
312*8b8b4e78Schristos
313*8b8b4e78Schristos# Create the lock directory.
314*8b8b4e78Schristos# Note: use '[/\\:.-]' here to ensure that we don't use the same name
315*8b8b4e78Schristos# that we are using for the .o file.  Also, base the name on the expected
316*8b8b4e78Schristos# object file name, since that is what matters with a parallel build.
317*8b8b4e78Schristoslockdir=`echo "$cofile" | sed -e 's|[/\\:.-]|_|g'`.d
318*8b8b4e78Schristoswhile true; do
319*8b8b4e78Schristos  if mkdir "$lockdir" >/dev/null 2>&1; then
320*8b8b4e78Schristos    break
321*8b8b4e78Schristos  fi
322*8b8b4e78Schristos  sleep 1
323*8b8b4e78Schristosdone
324*8b8b4e78Schristos# FIXME: race condition here if user kills between mkdir and trap.
325*8b8b4e78Schristostrap "rmdir '$lockdir'; exit 1" 1 2 15
326*8b8b4e78Schristos
327*8b8b4e78Schristos# Run the compile.
328*8b8b4e78Schristos"$@"
329*8b8b4e78Schristosret=$?
330*8b8b4e78Schristos
331*8b8b4e78Schristosif test -f "$cofile"; then
332*8b8b4e78Schristos  test "$cofile" = "$ofile" || mv "$cofile" "$ofile"
333*8b8b4e78Schristoselif test -f "${cofile}bj"; then
334*8b8b4e78Schristos  test "${cofile}bj" = "$ofile" || mv "${cofile}bj" "$ofile"
335*8b8b4e78Schristosfi
336*8b8b4e78Schristos
337*8b8b4e78Schristosrmdir "$lockdir"
338*8b8b4e78Schristosexit $ret
339*8b8b4e78Schristos
340*8b8b4e78Schristos# Local Variables:
341*8b8b4e78Schristos# mode: shell-script
342*8b8b4e78Schristos# sh-indentation: 2
343*8b8b4e78Schristos# eval: (add-hook 'before-save-hook 'time-stamp)
344*8b8b4e78Schristos# time-stamp-start: "scriptversion="
345*8b8b4e78Schristos# time-stamp-format: "%:y-%02m-%02d.%02H"
346*8b8b4e78Schristos# time-stamp-time-zone: "UTC0"
347*8b8b4e78Schristos# time-stamp-end: "; # UTC"
348*8b8b4e78Schristos# End:
349