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