1#!/bin/sh
2
3# ***** BEGIN LICENSE BLOCK *****
4# Version: MPL 1.1/GPL 2.0/LGPL 2.1
5#
6# The contents of this file are subject to the Mozilla Public License Version
7# 1.1 (the "License"); you may not use this file except in compliance with
8# the License. You may obtain a copy of the License at
9# http://www.mozilla.org/MPL/
10#
11# Software distributed under the License is distributed on an "AS IS" basis,
12# WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13# for the specific language governing rights and limitations under the
14# License.
15#
16# The Original Code is the MSVC wrappificator.
17#
18# The Initial Developer of the Original Code is
19# Timothy Wall <twalljava@dev.java.net>.
20# Portions created by the Initial Developer are Copyright (C) 2009
21# the Initial Developer. All Rights Reserved.
22#
23# Contributor(s):
24#   Daniel Witte <dwitte@mozilla.com>
25#
26# Alternatively, the contents of this file may be used under the terms of
27# either the GNU General Public License Version 2 or later (the "GPL"), or
28# the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
29# in which case the provisions of the GPL or the LGPL are applicable instead
30# of those above. If you wish to allow use of your version of this file only
31# under the terms of either the GPL or the LGPL, and not to allow others to
32# use your version of this file under the terms of the MPL, indicate your
33# decision by deleting the provisions above and replace them with the notice
34# and other provisions required by the GPL or the LGPL. If you do not delete
35# the provisions above, a recipient may use your version of this file under
36# the terms of any one of the MPL, the GPL or the LGPL.
37#
38# ***** END LICENSE BLOCK *****
39
40#
41# GCC-compatible wrapper for cl.exe and ml.exe. Arguments are given in GCC
42# format and translated into something sensible for cl or ml.
43#
44
45args_orig=$@
46args="-nologo -W3"
47linkargs=
48static_crt=
49debug_crt=
50cl="cl"
51ml="ml"
52safeseh="-safeseh"
53output=
54libpaths=
55libversion=7
56verbose=
57
58while [ $# -gt 0 ]
59do
60  case $1
61  in
62    --verbose)
63      verbose=1
64      shift 1
65    ;;
66    --version)
67      args="-help"
68      shift 1
69    ;;
70    -fexceptions)
71      # Don't enable exceptions for now.
72      #args="$args -EHac"
73      shift 1
74    ;;
75    -m32)
76      shift 1
77    ;;
78    -m64)
79      ml="ml64" # "$MSVC/x86_amd64/ml64"
80      safeseh=
81      shift 1
82    ;;
83    -marm)
84      ml='armasm'
85      safeseh=
86      shift 1
87    ;;
88    -marm64)
89      ml='armasm64'
90      safeseh=
91      shift 1
92    ;;
93    -clang-cl)
94      cl="clang-cl"
95      shift 1
96    ;;
97    -O0)
98      args="$args -Od"
99      shift 1
100    ;;
101    -O*)
102      # Runtime error checks (enabled by setting -RTC1 in the -DFFI_DEBUG
103      # case below) are not compatible with optimization flags and will
104      # cause the build to fail. Therefore, drop the optimization flag if
105      # -DFFI_DEBUG is also set.
106      case $args_orig in
107        *-DFFI_DEBUG*)
108          args="$args"
109        ;;
110        *)
111          # The ax_cc_maxopt.m4 macro from the upstream autoconf-archive
112          # project doesn't support MSVC and therefore ends up trying to
113          # use -O3. Use the equivalent "max optimization" flag for MSVC
114          # instead of erroring out.
115          case $1 in
116            -O3)
117              args="$args -O2"
118            ;;
119            *)
120              args="$args $1"
121            ;;
122          esac
123          opt="true"
124        ;;
125      esac
126      shift 1
127    ;;
128    -g)
129      # Enable debug symbol generation.
130      args="$args -Zi"
131      shift 1
132    ;;
133    -DFFI_DEBUG)
134      # Enable runtime error checks.
135      args="$args -RTC1"
136      defines="$defines $1"
137      shift 1
138    ;;
139    -DUSE_STATIC_RTL)
140      # Link against static CRT.
141      static_crt=1
142      shift 1
143    ;;
144    -DUSE_DEBUG_RTL)
145      # Link against debug CRT.
146      debug_crt=1
147      shift 1
148    ;;
149    -c)
150      args="$args -c"
151      args="$(echo $args | sed 's%/Fe%/Fo%g')"
152      single="-c"
153      shift 1
154    ;;
155    -D*=*)
156      name="$(echo $1|sed 's/-D\([^=][^=]*\)=.*/\1/g')"
157      value="$(echo $1|sed 's/-D[^=][^=]*=//g')"
158      args="$args -D${name}='$value'"
159      defines="$defines -D${name}='$value'"
160      shift 1
161    ;;
162    -D*)
163      args="$args $1"
164      defines="$defines $1"
165      shift 1
166    ;;
167    -I)
168      p=$(cygpath -m $2)
169      args="$args -I$p"
170      includes="$includes -I$p"
171      shift 2
172    ;;
173    -I*)
174      p=$(cygpath -m ${1#-I})
175      args="$args -I$p"
176      includes="$includes -I$p"
177      shift 1
178    ;;
179    -L)
180      p=$(cygpath -m $2)
181      linkargs="$linkargs -LIBPATH:$p"
182      shift 2
183    ;;
184    -L*)
185      p=$(cygpath -m ${1#-L})
186      linkargs="$linkargs -LIBPATH:$p"
187      shift 1
188    ;;
189    -link)
190      # add next argument verbatim to linker args
191      linkargs="$linkargs $2"
192      shift 2
193      ;;
194    -l*)
195      case $1
196      in
197        -lffi)
198          linkargs="$linkargs lib${1#-l}-${libversion}.lib"
199          ;;
200        *)
201          # ignore other libraries like -lm, hope they are
202          # covered by MSVCRT
203          # linkargs="$linkargs ${1#-l}.lib"
204          ;;
205      esac
206      shift 1
207    ;;
208    -W|-Wextra)
209      # TODO map extra warnings
210      shift 1
211    ;;
212    -Wall)
213      # -Wall on MSVC is overzealous, and we already build with -W3. Nothing
214      # to do here.
215      shift 1
216    ;;
217    -pedantic)
218      # libffi tests -pedantic with -Wall, so drop it also.
219      shift 1
220    ;;
221    -warn)
222      # ignore -warn all from libtool as well.
223      if test "$2" = "all"; then
224        shift 2
225      else
226        args="$args -warn"
227        shift 1
228      fi
229    ;;
230    -Werror)
231      args="$args -WX"
232      shift 1
233    ;;
234    -W*)
235      # TODO map specific warnings
236      shift 1
237    ;;
238    -S)
239      args="$args -FAs"
240      shift 1
241    ;;
242    -o)
243      outdir="$(dirname $2)"
244      base="$(basename $2|sed 's/\.[^.]*//g')"
245      if [ -n "$single" ]; then
246        output="-Fo$2"
247      else
248        output="-Fe$2"
249      fi
250      armasm_output="-o $2"
251      if [ -n "$assembly" ]; then
252        args="$args $output"
253      else
254        args="$args $output -Fd$outdir/$base -Fp$outdir/$base -Fa$outdir/$base"
255      fi
256      shift 2
257    ;;
258    *.S)
259      src=$1
260      assembly="true"
261      shift 1
262    ;;
263    *.c)
264      args="$args $1"
265      shift 1
266    ;;
267    *)
268      # Assume it's an MSVC argument, and pass it through.
269      args="$args $1"
270      shift 1
271    ;;
272  esac
273done
274
275if [ -n "$linkargs" ]; then
276
277    # If -Zi is specified, certain optimizations are implicitly disabled
278    # by MSVC. Add back those optimizations if this is an optimized build.
279    # NOTE: These arguments must come after all others.
280    if [ -n "$opt" ]; then
281	linkargs="$linkargs -OPT:REF -OPT:ICF -INCREMENTAL:NO"
282    fi
283
284    args="$args -link $linkargs"
285fi
286
287if [ -n "$static_crt" ]; then
288    md=-MT
289else
290    md=-MD
291fi
292
293if [ -n "$debug_crt" ]; then
294    md="${md}d"
295fi
296
297if [ -n "$assembly" ]; then
298    if [ -z "$outdir" ]; then
299      outdir="."
300    fi
301    ppsrc="$outdir/$(basename $src|sed 's/.S$/.asm/g')"
302
303    if [ $ml = "armasm" ]; then
304      defines="$defines -D_M_ARM"
305    fi
306
307    if [ $ml = "armasm64" ]; then
308      defines="$defines -D_M_ARM64"
309    fi
310
311    if test -n "$verbose"; then
312      echo "$cl -nologo -EP $includes $defines $src > $ppsrc"
313    fi
314
315    "$cl" -nologo -EP $includes $defines $src > $ppsrc || exit $?
316    output="$(echo $output | sed 's%/F[dpa][^ ]*%%g')"
317    if [ $ml = "armasm" ]; then
318      args="-nologo -g -oldit $armasm_output $ppsrc -errorReport:prompt"
319    elif [ $ml = "armasm64" ]; then
320      args="-nologo -g $armasm_output $ppsrc -errorReport:prompt"
321    else
322      args="-nologo $safeseh $single $output $ppsrc"
323    fi
324
325    if test -n "$verbose"; then
326      echo "$ml $args"
327    fi
328
329    eval "\"$ml\" $args"
330    result=$?
331
332    # required to fix ml64 broken output?
333    #mv *.obj $outdir
334else
335    args="$md $args"
336
337    if test -n "$verbose"; then
338      echo "$cl $args"
339    fi
340    # Return an error code of 1 if an invalid command line parameter is passed
341    # instead of just ignoring it. Any output that is not a warning or an
342    # error is filtered so this command behaves more like gcc. cl.exe prints
343    # the name of the compiled file otherwise, which breaks the dejagnu checks
344    # for excess warnings and errors.
345    eval "(\"$cl\" $args 2>&1 1>&3 | \
346          awk '{print \$0} /D9002/ {error=1} END{exit error}' >&2) 3>&1 | \
347          awk '/warning|error/'"
348    result=$?
349fi
350
351exit $result
352
353# vim: noai:ts=4:sw=4
354