1#! /bin/sh
2# set -x
3#
4# %CopyrightBegin%
5#
6# Copyright Ericsson AB 2006-2016. All Rights Reserved.
7#
8# Licensed under the Apache License, Version 2.0 (the "License");
9# you may not use this file except in compliance with the License.
10# You may obtain a copy of the License at
11#
12#     http://www.apache.org/licenses/LICENSE-2.0
13#
14# Unless required by applicable law or agreed to in writing, software
15# distributed under the License is distributed on an "AS IS" BASIS,
16# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17# See the License for the specific language governing permissions and
18# limitations under the License.
19#
20# %CopyrightEnd%
21#
22# Save the command line for debug outputs
23SAVE="$@"
24kernel_libs="-lkernel32 -ladvapi32"
25gdi_libs="-lgdi32 -luser32 -lcomctl32 -lcomdlg32 -lshell32"
26DEFAULT_LIBRARIES="$kernel_libs $gdi_libs"
27
28CMD=""
29STDLIB=-lmsvcrt
30DEBUG_BUILD=false
31STDLIB_FORCED=false
32BUILD_DLL=false
33OUTPUT_FILENAME=""
34
35if [ -z "$MINGW_EXE_PATH" ]; then
36    echo "You have to set MINGW_EXE_PATH to run cc.sh" >&2
37    exit 1
38fi
39
40while test -n "$1" ; do
41    x="$1"
42    case "$x" in
43	-dll| -DLL | -shared)
44	    BUILD_DLL=true;;
45	-L/*|-L.*)
46	    y=`echo $x | sed 's,^-L\(.*\),\1,g'`;
47	    MPATH=`cygpath -m $y`;
48	    CMD="$CMD -L \"$MPATH\"";;
49	-lMSVCRT|-lmsvcrt)
50	    STDLIB_FORCED=true;
51	    STDLIB=-lmsvcrt;;
52	-lMSVCRTD|-lmsvcrtd)
53	    STDLIB_FORCED=true;
54	    STDLIB=-lmsvcrtd;;
55	-lLIBCMT|-llibcmt)
56	    STDLIB_FORCED=true;
57	    STDLIB=-llibcmt;;
58	-lLIBCMTD|-llibcmtd)
59	    STDLIB_FORCED=true;
60	    STDLIB=-llibcmtd;;
61	-lsocket)
62	    DEFAULT_LIBRARIES="$DEFAULT_LIBRARIES -lws2_32";;
63	-l*)
64	    y=`echo $x | sed 's,^-l\(.*\),\1,g'`;
65	    MPATH=`cygpath -m $y`;
66	    CMD="$CMD -l\"${MPATH}\"";;
67	-g)
68	    DEBUG_BUILD=true;;
69	-pdb:none|-incremental:no)
70	    ;;
71	-implib:*)
72	    y=`echo $x | sed 's,^-implib:\(.*\),\1,g'`;
73	    MPATH=`cygpath -m $y`;
74	    CMD="$CMD -Xlinker --out-implib -Xlinker \"${MPATH}\"";;
75	-entry:*)
76	    y=`echo $x | sed 's,^-entry:\(.*\),\1,g'`;
77	    CMD="$CMD -Xlinker --entry -Xlinker _$y";;
78	-def:*)
79	    ;;
80	## Ignore -def: for now as ld.sh core dumps...
81	#    y=`echo $x | sed 's,^-def:\(.*\),\1,g'`;
82	#    MPATH=`cygpath -m $y`;
83	#    CMD="$CMD -Xlinker --output-def -Xlinker \"${MPATH}\"";;
84	-o)
85	    shift
86	    MPATH=`cygpath -m $1`;
87	    OUTPUT_FILENAME="$MPATH";;
88	-o/*)
89	    y=`echo $x | sed 's,^-[Io]\(/.*\),\1,g'`;
90	    MPATH=`cygpath -m $y`;
91	    OUTPUT_FILENAME="$MPATH";;
92	/*)
93	    MPATH=`cygpath -m $x`;
94	    CMD="$CMD \"$MPATH\"";;
95	*)
96	    y=`echo $x | sed 's,",\\\",g'`;
97	    CMD="$CMD \"$y\"";;
98    esac
99    shift
100done
101if [ $DEBUG_BUILD = true ]; then
102    linktype="-g"
103    if [ $STDLIB_FORCED = false ]; then
104	STDLIB=-lmsvcrt #d?
105    fi
106else
107    linktype=
108fi
109
110if [ $BUILD_DLL = true ];then
111    case "$OUTPUT_FILENAME" in
112	*.exe|*.EXE)
113	    echo "Warning, output set to .exe when building DLL" >&2
114	    CMD="-shared -o \"$OUTPUT_FILENAME\" $CMD";;
115	*.dll|*.DLL)
116	    CMD="-shared -o \"$OUTPUT_FILENAME\" $CMD";;
117	"")
118	    CMD="-shared -o \"a.dll\" $CMD";;
119	*)
120	    CMD="-shared -o \"${OUTPUT_FILENAME}.dll\" $CMD";;
121    esac
122else
123    case "$OUTPUT_FILENAME" in
124	*.exe|*.EXE)
125	    CMD="-o \"$OUTPUT_FILENAME\" $CMD";;
126	*.dll|*.DLL)
127	    echo "Warning, output set to .dll when building EXE" >&2
128	    CMD="-o \"$OUTPUT_FILENAME\" $CMD";;
129	"")
130	    CMD="-o \"a.exe\" $CMD";;
131	*)
132	    CMD="-o \"${OUTPUT_FILENAME}.exe\" $CMD";;
133    esac
134fi
135
136p=$$
137CMD="$linktype $CMD $STDLIB $DEFAULT_LIBRARIES"
138if [ "X$LD_SH_DEBUG_LOG" != "X" ]; then
139    echo ld.sh "$SAVE" >>$LD_SH_DEBUG_LOG
140    echo $MINGW_EXE_PATH/gcc.exe $CMD >>$LD_SH_DEBUG_LOG
141fi
142eval $MINGW_EXE_PATH/gcc "$CMD"  >/tmp/link.exe.${p}.1 2>/tmp/link.exe.${p}.2
143RES=$?
144#tail +2 /tmp/link.exe.${p}.2 >&2
145cat /tmp/link.exe.${p}.2 >&2
146cat /tmp/link.exe.${p}.1
147rm -f /tmp/link.exe.${p}.2 /tmp/link.exe.${p}.1
148exit $RES
149