1#! /bin/sh
2#
3# %CopyrightBegin%
4#
5# Copyright Ericsson AB 2006-2016. All Rights Reserved.
6#
7# Licensed under the Apache License, Version 2.0 (the "License");
8# you may not use this file except in compliance with the License.
9# You may obtain a copy of the License at
10#
11#     http://www.apache.org/licenses/LICENSE-2.0
12#
13# Unless required by applicable law or agreed to in writing, software
14# distributed under the License is distributed on an "AS IS" BASIS,
15# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16# See the License for the specific language governing permissions and
17# limitations under the License.
18#
19# %CopyrightEnd%
20#
21TOOLDIR=$ERL_TOP/erts/etc/win32/cygwin_tools/mingw
22COFFIX=$TOOLDIR/coffix
23WTOOLDIR=`(cygpath -d $TOOLDIR 2>/dev/null || cygpath -w $TOOLDIR)`
24
25# Do primitive 'make'
26newer_exe=`find $TOOLDIR -newer $COFFIX.c -name coffix.exe -print`
27if [ -z $newer_exe ]; then
28    echo recompiling $COFFIX.exe
29    cl.exe -Fe${WTOOLDIR}\\coffix.exe ${WTOOLDIR}\\coffix.c
30    rm -f $COFFIX.obj coffix.obj $COFFIX.pdb coffix.pdb
31fi
32
33# Try to find out the output filename and remove it from command line
34CMD=""
35OUTFILE=""
36INFILE=""
37SKIP_COFFIX=false
38while test -n "$1" ; do
39    x="$1"
40    case "$x" in
41	-o/*)
42	    OUTFILE=`echo $x | sed 's,^-[Io]\(/.*\),\1,g'`;;
43	-o)
44	    shift
45	    OUTFILE=$1;;
46	-MM)
47	    SKIP_COFFIX=true
48	    CMD="$CMD \"$x\"";;
49	*.c)
50	    INFILE="$INFILE $x";
51	    CMD="$CMD \"$x\"";;
52	*)
53	    CMD="$CMD \"$x\"";;
54    esac
55    shift
56done
57if [ -z "$INFILE" ]; then
58    echo 'emu_cc.sh: please give an input filename for the compiler' >&2
59    exit 1
60fi
61if [ -z "$OUTFILE" ]; then
62    OUTFILE=`echo $INFILE | sed 's,\.c$,.o,'`
63fi
64
65if [ $SKIP_COFFIX = false ]; then
66    n=`echo $INFILE | wc -w`;
67    if [ $n -gt 1 ]; then
68	echo "emu_cc.sh:Error, multiple sources, one object output.";
69	exit 1;
70    fi
71    TEMPFILE=/tmp/tmp_emu_cc$$.o
72    if [ "X$EMU_CC_SH_DEBUG_LOG" != "X" ]; then
73	echo "gcc -o $TEMPFILE -D__WIN32__ -DWIN32 -DWINDOWS -fomit-frame-pointer $CMD" >> $EMU_CC_SH_DEBUG_LOG 2>&1
74    fi
75    eval gcc -o $TEMPFILE -D__WIN32__ -DWIN32 -DWINDOWS -fomit-frame-pointer $CMD
76    RES=$?
77    if [ $RES = 0 ]; then
78	$COFFIX.exe -e `(cygpath -d $TEMPFILE 2>/dev/null || cygpath -w $TEMPFILE)`
79	RES=$?
80	if [ $RES = 0 ]; then
81	    cp $TEMPFILE $OUTFILE
82	else
83	    echo "emu_cc.sh: fatal: coffix failed!" >&2
84	fi
85    fi
86    rm -f $TEMPFILE
87    exit $RES
88else
89    eval gcc -D__WIN32__ -DWIN32 -DWINDOWS -fomit-frame-pointer $CMD 2>/dev/null
90    exit $?
91fi
92