xref: /netbsd-src/crypto/external/bsd/heimdal/dist/compile (revision dd75ac5b443e967e26b4d18cc8cd5eb98512bfbf)
1929c70cbSchristos#! /bin/sh
2929c70cbSchristos# Wrapper for compilers which do not understand '-c -o'.
3929c70cbSchristos
4*dd75ac5bSchristosscriptversion=2018-03-07.03; # UTC
5929c70cbSchristos
6*dd75ac5bSchristos# Copyright (C) 1999-2021 Free Software Foundation, Inc.
7929c70cbSchristos# Written by Tom Tromey <tromey@cygnus.com>.
8929c70cbSchristos#
9929c70cbSchristos# This program is free software; you can redistribute it and/or modify
10929c70cbSchristos# it under the terms of the GNU General Public License as published by
11929c70cbSchristos# the Free Software Foundation; either version 2, or (at your option)
12929c70cbSchristos# any later version.
13929c70cbSchristos#
14929c70cbSchristos# This program is distributed in the hope that it will be useful,
15929c70cbSchristos# but WITHOUT ANY WARRANTY; without even the implied warranty of
16929c70cbSchristos# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17929c70cbSchristos# GNU General Public License for more details.
18929c70cbSchristos#
19929c70cbSchristos# You should have received a copy of the GNU General Public License
20*dd75ac5bSchristos# along with this program.  If not, see <https://www.gnu.org/licenses/>.
21929c70cbSchristos
22929c70cbSchristos# As a special exception to the GNU General Public License, if you
23929c70cbSchristos# distribute this file as part of a program that contains a
24929c70cbSchristos# configuration script generated by Autoconf, you may include it under
25929c70cbSchristos# the same distribution terms that you use for the rest of that program.
26929c70cbSchristos
27929c70cbSchristos# This file is maintained in Automake, please report
28929c70cbSchristos# bugs to <bug-automake@gnu.org> or send patches to
29929c70cbSchristos# <automake-patches@gnu.org>.
30929c70cbSchristos
31929c70cbSchristosnl='
32929c70cbSchristos'
33929c70cbSchristos
34929c70cbSchristos# We need space, tab and new line, in precisely that order.  Quoting is
35929c70cbSchristos# there to prevent tools from complaining about whitespace usage.
36929c70cbSchristosIFS=" ""	$nl"
37929c70cbSchristos
38929c70cbSchristosfile_conv=
39929c70cbSchristos
40929c70cbSchristos# func_file_conv build_file lazy
41929c70cbSchristos# Convert a $build file to $host form and store it in $file
42929c70cbSchristos# Currently only supports Windows hosts. If the determined conversion
43929c70cbSchristos# type is listed in (the comma separated) LAZY, no conversion will
44929c70cbSchristos# take place.
45929c70cbSchristosfunc_file_conv ()
46929c70cbSchristos{
47929c70cbSchristos  file=$1
48929c70cbSchristos  case $file in
49929c70cbSchristos    / | /[!/]*) # absolute file, and not a UNC file
50929c70cbSchristos      if test -z "$file_conv"; then
51929c70cbSchristos	# lazily determine how to convert abs files
52929c70cbSchristos	case `uname -s` in
53929c70cbSchristos	  MINGW*)
54929c70cbSchristos	    file_conv=mingw
55929c70cbSchristos	    ;;
56*dd75ac5bSchristos	  CYGWIN* | MSYS*)
57929c70cbSchristos	    file_conv=cygwin
58929c70cbSchristos	    ;;
59929c70cbSchristos	  *)
60929c70cbSchristos	    file_conv=wine
61929c70cbSchristos	    ;;
62929c70cbSchristos	esac
63929c70cbSchristos      fi
64929c70cbSchristos      case $file_conv/,$2, in
65929c70cbSchristos	*,$file_conv,*)
66929c70cbSchristos	  ;;
67929c70cbSchristos	mingw/*)
68929c70cbSchristos	  file=`cmd //C echo "$file " | sed -e 's/"\(.*\) " *$/\1/'`
69929c70cbSchristos	  ;;
70*dd75ac5bSchristos	cygwin/* | msys/*)
71929c70cbSchristos	  file=`cygpath -m "$file" || echo "$file"`
72929c70cbSchristos	  ;;
73929c70cbSchristos	wine/*)
74929c70cbSchristos	  file=`winepath -w "$file" || echo "$file"`
75929c70cbSchristos	  ;;
76929c70cbSchristos      esac
77929c70cbSchristos      ;;
78929c70cbSchristos  esac
79929c70cbSchristos}
80929c70cbSchristos
81929c70cbSchristos# func_cl_dashL linkdir
82929c70cbSchristos# Make cl look for libraries in LINKDIR
83929c70cbSchristosfunc_cl_dashL ()
84929c70cbSchristos{
85929c70cbSchristos  func_file_conv "$1"
86929c70cbSchristos  if test -z "$lib_path"; then
87929c70cbSchristos    lib_path=$file
88929c70cbSchristos  else
89929c70cbSchristos    lib_path="$lib_path;$file"
90929c70cbSchristos  fi
91929c70cbSchristos  linker_opts="$linker_opts -LIBPATH:$file"
92929c70cbSchristos}
93929c70cbSchristos
94929c70cbSchristos# func_cl_dashl library
95929c70cbSchristos# Do a library search-path lookup for cl
96929c70cbSchristosfunc_cl_dashl ()
97929c70cbSchristos{
98929c70cbSchristos  lib=$1
99929c70cbSchristos  found=no
100929c70cbSchristos  save_IFS=$IFS
101929c70cbSchristos  IFS=';'
102929c70cbSchristos  for dir in $lib_path $LIB
103929c70cbSchristos  do
104929c70cbSchristos    IFS=$save_IFS
105929c70cbSchristos    if $shared && test -f "$dir/$lib.dll.lib"; then
106929c70cbSchristos      found=yes
107929c70cbSchristos      lib=$dir/$lib.dll.lib
108929c70cbSchristos      break
109929c70cbSchristos    fi
110929c70cbSchristos    if test -f "$dir/$lib.lib"; then
111929c70cbSchristos      found=yes
112929c70cbSchristos      lib=$dir/$lib.lib
113929c70cbSchristos      break
114929c70cbSchristos    fi
115929c70cbSchristos    if test -f "$dir/lib$lib.a"; then
116929c70cbSchristos      found=yes
117929c70cbSchristos      lib=$dir/lib$lib.a
118929c70cbSchristos      break
119929c70cbSchristos    fi
120929c70cbSchristos  done
121929c70cbSchristos  IFS=$save_IFS
122929c70cbSchristos
123929c70cbSchristos  if test "$found" != yes; then
124929c70cbSchristos    lib=$lib.lib
125929c70cbSchristos  fi
126929c70cbSchristos}
127929c70cbSchristos
128929c70cbSchristos# func_cl_wrapper cl arg...
129929c70cbSchristos# Adjust compile command to suit cl
130929c70cbSchristosfunc_cl_wrapper ()
131929c70cbSchristos{
132929c70cbSchristos  # Assume a capable shell
133929c70cbSchristos  lib_path=
134929c70cbSchristos  shared=:
135929c70cbSchristos  linker_opts=
136929c70cbSchristos  for arg
137929c70cbSchristos  do
138929c70cbSchristos    if test -n "$eat"; then
139929c70cbSchristos      eat=
140929c70cbSchristos    else
141929c70cbSchristos      case $1 in
142929c70cbSchristos	-o)
143929c70cbSchristos	  # configure might choose to run compile as 'compile cc -o foo foo.c'.
144929c70cbSchristos	  eat=1
145929c70cbSchristos	  case $2 in
146929c70cbSchristos	    *.o | *.[oO][bB][jJ])
147929c70cbSchristos	      func_file_conv "$2"
148929c70cbSchristos	      set x "$@" -Fo"$file"
149929c70cbSchristos	      shift
150929c70cbSchristos	      ;;
151929c70cbSchristos	    *)
152929c70cbSchristos	      func_file_conv "$2"
153929c70cbSchristos	      set x "$@" -Fe"$file"
154929c70cbSchristos	      shift
155929c70cbSchristos	      ;;
156929c70cbSchristos	  esac
157929c70cbSchristos	  ;;
158929c70cbSchristos	-I)
159929c70cbSchristos	  eat=1
160929c70cbSchristos	  func_file_conv "$2" mingw
161929c70cbSchristos	  set x "$@" -I"$file"
162929c70cbSchristos	  shift
163929c70cbSchristos	  ;;
164929c70cbSchristos	-I*)
165929c70cbSchristos	  func_file_conv "${1#-I}" mingw
166929c70cbSchristos	  set x "$@" -I"$file"
167929c70cbSchristos	  shift
168929c70cbSchristos	  ;;
169929c70cbSchristos	-l)
170929c70cbSchristos	  eat=1
171929c70cbSchristos	  func_cl_dashl "$2"
172929c70cbSchristos	  set x "$@" "$lib"
173929c70cbSchristos	  shift
174929c70cbSchristos	  ;;
175929c70cbSchristos	-l*)
176929c70cbSchristos	  func_cl_dashl "${1#-l}"
177929c70cbSchristos	  set x "$@" "$lib"
178929c70cbSchristos	  shift
179929c70cbSchristos	  ;;
180929c70cbSchristos	-L)
181929c70cbSchristos	  eat=1
182929c70cbSchristos	  func_cl_dashL "$2"
183929c70cbSchristos	  ;;
184929c70cbSchristos	-L*)
185929c70cbSchristos	  func_cl_dashL "${1#-L}"
186929c70cbSchristos	  ;;
187929c70cbSchristos	-static)
188929c70cbSchristos	  shared=false
189929c70cbSchristos	  ;;
190929c70cbSchristos	-Wl,*)
191929c70cbSchristos	  arg=${1#-Wl,}
192929c70cbSchristos	  save_ifs="$IFS"; IFS=','
193929c70cbSchristos	  for flag in $arg; do
194929c70cbSchristos	    IFS="$save_ifs"
195929c70cbSchristos	    linker_opts="$linker_opts $flag"
196929c70cbSchristos	  done
197929c70cbSchristos	  IFS="$save_ifs"
198929c70cbSchristos	  ;;
199929c70cbSchristos	-Xlinker)
200929c70cbSchristos	  eat=1
201929c70cbSchristos	  linker_opts="$linker_opts $2"
202929c70cbSchristos	  ;;
203929c70cbSchristos	-*)
204929c70cbSchristos	  set x "$@" "$1"
205929c70cbSchristos	  shift
206929c70cbSchristos	  ;;
207929c70cbSchristos	*.cc | *.CC | *.cxx | *.CXX | *.[cC]++)
208929c70cbSchristos	  func_file_conv "$1"
209929c70cbSchristos	  set x "$@" -Tp"$file"
210929c70cbSchristos	  shift
211929c70cbSchristos	  ;;
212929c70cbSchristos	*.c | *.cpp | *.CPP | *.lib | *.LIB | *.Lib | *.OBJ | *.obj | *.[oO])
213929c70cbSchristos	  func_file_conv "$1" mingw
214929c70cbSchristos	  set x "$@" "$file"
215929c70cbSchristos	  shift
216929c70cbSchristos	  ;;
217929c70cbSchristos	*)
218929c70cbSchristos	  set x "$@" "$1"
219929c70cbSchristos	  shift
220929c70cbSchristos	  ;;
221929c70cbSchristos      esac
222929c70cbSchristos    fi
223929c70cbSchristos    shift
224929c70cbSchristos  done
225929c70cbSchristos  if test -n "$linker_opts"; then
226929c70cbSchristos    linker_opts="-link$linker_opts"
227929c70cbSchristos  fi
228929c70cbSchristos  exec "$@" $linker_opts
229929c70cbSchristos  exit 1
230929c70cbSchristos}
231929c70cbSchristos
232929c70cbSchristoseat=
233929c70cbSchristos
234929c70cbSchristoscase $1 in
235929c70cbSchristos  '')
236929c70cbSchristos     echo "$0: No command.  Try '$0 --help' for more information." 1>&2
237929c70cbSchristos     exit 1;
238929c70cbSchristos     ;;
239929c70cbSchristos  -h | --h*)
240929c70cbSchristos    cat <<\EOF
241929c70cbSchristosUsage: compile [--help] [--version] PROGRAM [ARGS]
242929c70cbSchristos
243929c70cbSchristosWrapper for compilers which do not understand '-c -o'.
244929c70cbSchristosRemove '-o dest.o' from ARGS, run PROGRAM with the remaining
245929c70cbSchristosarguments, and rename the output as expected.
246929c70cbSchristos
247929c70cbSchristosIf you are trying to build a whole package this is not the
248929c70cbSchristosright script to run: please start by reading the file 'INSTALL'.
249929c70cbSchristos
250929c70cbSchristosReport bugs to <bug-automake@gnu.org>.
251929c70cbSchristosEOF
252929c70cbSchristos    exit $?
253929c70cbSchristos    ;;
254929c70cbSchristos  -v | --v*)
255929c70cbSchristos    echo "compile $scriptversion"
256929c70cbSchristos    exit $?
257929c70cbSchristos    ;;
258929c70cbSchristos  cl | *[/\\]cl | cl.exe | *[/\\]cl.exe | \
259929c70cbSchristos  icl | *[/\\]icl | icl.exe | *[/\\]icl.exe )
260929c70cbSchristos    func_cl_wrapper "$@"      # Doesn't return...
261929c70cbSchristos    ;;
262929c70cbSchristosesac
263929c70cbSchristos
264929c70cbSchristosofile=
265929c70cbSchristoscfile=
266929c70cbSchristos
267929c70cbSchristosfor arg
268929c70cbSchristosdo
269929c70cbSchristos  if test -n "$eat"; then
270929c70cbSchristos    eat=
271929c70cbSchristos  else
272929c70cbSchristos    case $1 in
273929c70cbSchristos      -o)
274929c70cbSchristos	# configure might choose to run compile as 'compile cc -o foo foo.c'.
275929c70cbSchristos	# So we strip '-o arg' only if arg is an object.
276929c70cbSchristos	eat=1
277929c70cbSchristos	case $2 in
278929c70cbSchristos	  *.o | *.obj)
279929c70cbSchristos	    ofile=$2
280929c70cbSchristos	    ;;
281929c70cbSchristos	  *)
282929c70cbSchristos	    set x "$@" -o "$2"
283929c70cbSchristos	    shift
284929c70cbSchristos	    ;;
285929c70cbSchristos	esac
286929c70cbSchristos	;;
287929c70cbSchristos      *.c)
288929c70cbSchristos	cfile=$1
289929c70cbSchristos	set x "$@" "$1"
290929c70cbSchristos	shift
291929c70cbSchristos	;;
292929c70cbSchristos      *)
293929c70cbSchristos	set x "$@" "$1"
294929c70cbSchristos	shift
295929c70cbSchristos	;;
296929c70cbSchristos    esac
297929c70cbSchristos  fi
298929c70cbSchristos  shift
299929c70cbSchristosdone
300929c70cbSchristos
301929c70cbSchristosif test -z "$ofile" || test -z "$cfile"; then
302929c70cbSchristos  # If no '-o' option was seen then we might have been invoked from a
303929c70cbSchristos  # pattern rule where we don't need one.  That is ok -- this is a
304929c70cbSchristos  # normal compilation that the losing compiler can handle.  If no
305929c70cbSchristos  # '.c' file was seen then we are probably linking.  That is also
306929c70cbSchristos  # ok.
307929c70cbSchristos  exec "$@"
308929c70cbSchristosfi
309929c70cbSchristos
310929c70cbSchristos# Name of file we expect compiler to create.
311929c70cbSchristoscofile=`echo "$cfile" | sed 's|^.*[\\/]||; s|^[a-zA-Z]:||; s/\.c$/.o/'`
312929c70cbSchristos
313929c70cbSchristos# Create the lock directory.
314929c70cbSchristos# Note: use '[/\\:.-]' here to ensure that we don't use the same name
315929c70cbSchristos# that we are using for the .o file.  Also, base the name on the expected
316929c70cbSchristos# object file name, since that is what matters with a parallel build.
317929c70cbSchristoslockdir=`echo "$cofile" | sed -e 's|[/\\:.-]|_|g'`.d
318929c70cbSchristoswhile true; do
319929c70cbSchristos  if mkdir "$lockdir" >/dev/null 2>&1; then
320929c70cbSchristos    break
321929c70cbSchristos  fi
322929c70cbSchristos  sleep 1
323929c70cbSchristosdone
324929c70cbSchristos# FIXME: race condition here if user kills between mkdir and trap.
325929c70cbSchristostrap "rmdir '$lockdir'; exit 1" 1 2 15
326929c70cbSchristos
327929c70cbSchristos# Run the compile.
328929c70cbSchristos"$@"
329929c70cbSchristosret=$?
330929c70cbSchristos
331929c70cbSchristosif test -f "$cofile"; then
332929c70cbSchristos  test "$cofile" = "$ofile" || mv "$cofile" "$ofile"
333929c70cbSchristoselif test -f "${cofile}bj"; then
334929c70cbSchristos  test "${cofile}bj" = "$ofile" || mv "${cofile}bj" "$ofile"
335929c70cbSchristosfi
336929c70cbSchristos
337929c70cbSchristosrmdir "$lockdir"
338929c70cbSchristosexit $ret
339929c70cbSchristos
340929c70cbSchristos# Local Variables:
341929c70cbSchristos# mode: shell-script
342929c70cbSchristos# sh-indentation: 2
343*dd75ac5bSchristos# eval: (add-hook 'before-save-hook 'time-stamp)
344929c70cbSchristos# time-stamp-start: "scriptversion="
345929c70cbSchristos# time-stamp-format: "%:y-%02m-%02d.%02H"
346929c70cbSchristos# time-stamp-time-zone: "UTC0"
347929c70cbSchristos# time-stamp-end: "; # UTC"
348929c70cbSchristos# End:
349