1#! /bin/sh
2# set -x
3#
4# %CopyrightBegin%
5#
6# Copyright Ericsson AB 2002-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="kernel32.lib advapi32.lib"
25gdi_libs="gdi32.lib user32.lib comctl32.lib comdlg32.lib shell32.lib"
26DEFAULT_LIBRARIES="$kernel_libs $gdi_libs"
27
28CMD=""
29STDLIB=MSVCRT.LIB
30DEBUG_BUILD=false
31STDLIB_FORCED=false
32BUILD_DLL=false
33OUTPUT_FILENAME=""
34
35while test -n "$1" ; do
36    x="$1"
37    case "$x" in
38	-dll| -DLL)
39	    BUILD_DLL=true;;
40	-L/*|-L.*)
41	    y=`echo $x | sed 's,^-L\(.*\),\1,g'`;
42	    MPATH=`cygpath -m $y`;
43	    CMD="$CMD -libpath:\"$MPATH\"";;
44	-lMSVCRT|-lmsvcrt)
45	    STDLIB_FORCED=true;
46	    STDLIB=MSVCRT.LIB;;
47	-lMSVCRTD|-lmsvcrtd)
48	    STDLIB_FORCED=true;
49	    STDLIB=MSVCRTD.LIB;;
50	-lLIBCMT|-llibcmt)
51	    STDLIB_FORCED=true;
52	    STDLIB=LIBCMT.LIB;;
53	-lLIBCMTD|-llibcmtd)
54	    STDLIB_FORCED=true;
55	    STDLIB=LIBCMTD.LIB;;
56	-lsocket)
57	    DEFAULT_LIBRARIES="$DEFAULT_LIBRARIES WS2_32.LIB IPHLPAPI.LIB";;
58	-l*)
59	    y=`echo $x | sed 's,^-l\(.*\),\1,g'`;
60	    MPATH=`cygpath -m $y`;
61	    CMD="$CMD \"${MPATH}.lib\"";;
62	-g)
63	    DEBUG_BUILD=true;;
64	-pdb:none|-incremental:no)
65	    ;;
66	-implib:*)
67	    y=`echo $x | sed 's,^-implib:\(.*\),\1,g'`;
68	    MPATH=`cygpath -m $y`;
69	    CMD="$CMD -implib:\"${MPATH}\"";;
70	-def:*)
71	    y=`echo $x | sed 's,^-def:\(.*\),\1,g'`;
72	    MPATH=`cygpath -m $y`;
73	    CMD="$CMD -def:\"${MPATH}\"";;
74	-o)
75	    shift
76	    MPATH=`cygpath -m $1`;
77	    OUTPUT_FILENAME="$MPATH";;
78	-o/*)
79	    y=`echo $x | sed 's,^-[Io]\(/.*\),\1,g'`;
80	    MPATH=`cygpath -m $y`;
81	    OUTPUT_FILENAME="$MPATH";;
82	/*)
83	    MPATH=`cygpath -m $x`;
84	    CMD="$CMD \"$MPATH\"";;
85	*)
86	    y=`echo $x | sed 's,",\\\",g'`;
87	    CMD="$CMD \"$y\"";;
88    esac
89    shift
90done
91if [ $DEBUG_BUILD = true ]; then
92    linktype="-debug -pdb:none"
93    if [ $STDLIB_FORCED = false ]; then
94	STDLIB=MSVCRTD.LIB
95    fi
96fi
97# Generate a PDB
98linkadd_pdb=""
99case "$OUTPUT_FILENAME" in
100    *.exe|*.EXE)
101	    fn=`echo "$OUTPUT_FILENAME" | sed 's,[eE][xX][eE]$,,g'`;
102	    linkadd_pdb="-pdb:\"${fn}pdb\"";;
103    *.dll|*.DLL)
104	    fn=`echo "$OUTPUT_FILENAME" | sed 's,[dD][lL][lL]$,,g'`;
105	    linkadd_pdb="-pdb:\"${fn}pdb\"";;
106    "")
107	    linkadd_pdb="-pdb:\"a.pdb\"";;
108    *)
109	    linkadd_pdb="-pdb:\"${OUTPUT_FILENAME}.pdb\"";;
110esac
111
112    linktype="-debug $linkadd_pdb"
113
114CHMOD_FILE=""
115
116if [ $BUILD_DLL = true ];then
117    case "$OUTPUT_FILENAME" in
118	*.exe|*.EXE)
119	    echo "Warning, output set to .exe when building DLL" >&2
120	    CHMOD_FILE="$OUTPUT_FILENAME";
121	    CMD="-dll -out:\"$OUTPUT_FILENAME\" $CMD";
122	    OUTPUTRES="${OUTPUT_FILENAME}\;2";
123	    MANIFEST="${OUTPUT_FILENAME}.manifest";;
124	*.dll|*.DLL)
125	    CMD="-dll -out:\"$OUTPUT_FILENAME\" $CMD";
126	    OUTPUTRES="${OUTPUT_FILENAME}\;2";
127	    MANIFEST="${OUTPUT_FILENAME}.manifest";;
128	"")
129	    CMD="-dll -out:\"a.dll\" $CMD";
130	    OUTPUTRES="a.dll\;2";
131	    MANIFEST="a.dll.manifest";;
132	*)
133	    CMD="-dll -out:\"${OUTPUT_FILENAME}.dll\" $CMD";
134	    OUTPUTRES="${OUTPUT_FILENAME}.dll\;2";
135	    MANIFEST="${OUTPUT_FILENAME}.dll.manifest";;
136    esac
137else
138    case "$OUTPUT_FILENAME" in
139	*.exe|*.EXE)
140	    CHMOD_FILE="$OUTPUT_FILENAME";
141	    CMD="-out:\"$OUTPUT_FILENAME\" $CMD";
142	    OUTPUTRES="${OUTPUT_FILENAME}\;1"
143	    MANIFEST="${OUTPUT_FILENAME}.manifest";;
144	*.dll|*.DLL)
145	    echo "Warning, output set to .dll when building EXE" >&2
146	    CMD="-out:\"$OUTPUT_FILENAME\" $CMD";
147	    OUTPUTRES="${OUTPUT_FILENAME}\;1";
148	    MANIFEST="${OUTPUT_FILENAME}.manifest";;
149	"")
150	    CHMOD_FILE="a.exe";
151	    CMD="-out:\"a.exe\" $CMD";
152	    OUTPUTRES="a.exe\;1";
153	    MANIFEST="a.exe.manifest";;
154	*)
155	    CMD="-out:\"${OUTPUT_FILENAME}.exe\" $CMD";
156	    OUTPUTRES="${OUTPUT_FILENAME}.exe\;1";
157	    MANIFEST="${OUTPUT_FILENAME}.exe.manifest";;
158    esac
159fi
160
161p=$$
162CMD="$linktype -nologo -incremental:no -largeaddressaware $CMD $STDLIB $DEFAULT_LIBRARIES"
163if [ "X$LD_SH_DEBUG_LOG" != "X" ]; then
164    echo ld.sh "$SAVE" >>$LD_SH_DEBUG_LOG
165    echo link.exe $CMD >>$LD_SH_DEBUG_LOG
166fi
167eval link.exe "$CMD"  >/tmp/link.exe.${p}.1 2>/tmp/link.exe.${p}.2
168RES=$?
169CMANIFEST=`cygpath $MANIFEST`
170if [ "$RES" = "0" -a -f "$CMANIFEST" ]; then
171    # Add stuff to manifest to turn off "virtualization"
172    sed -n -i '1h;1!H;${;g;s,<trustInfo.*</trustInfo>.,,g;p;}' $CMANIFEST
173    sed -i "s/<\/assembly>/ <ms_asmv2:trustInfo xmlns:ms_asmv2=\"urn:schemas-microsoft-com:asm.v2\">\n  <ms_asmv2:security>\n   <ms_asmv2:requestedPrivileges>\n    <ms_asmv2:requestedExecutionLevel level=\"AsInvoker\" uiAccess=\"false\"\/>\n   <\/ms_asmv2:requestedPrivileges>\n  <\/ms_asmv2:security>\n <\/ms_asmv2:trustInfo>\n<\/assembly>/" $CMANIFEST
174
175    eval mt.exe -nologo -manifest "$MANIFEST" -outputresource:"$OUTPUTRES" >>/tmp/link.exe.${p}.1 2>>/tmp/link.exe.${p}.2
176    RES=$?
177    if [ "$RES" != "0" ]; then
178	REMOVE=`echo "$OUTPUTRES" | sed 's,\\\;[12]$,,g'`
179	CREMOVE=`cygpath $REMOVE`
180	rm -f "$CREMOVE"
181    fi
182    rm -f "$CMANIFEST"
183fi
184
185# This works around some strange behaviour
186# in cygwin 1.7 Beta on Windows 7 with samba drive.
187# Configure will think the compiler failed if test -x fails,
188# which it might do as we might not be the owner of the
189# file.
190if [ '!' -z "$CHMOD_FILE" -a -s "$CHMOD_FILE" -a '!' -x "$CHMOD_FILE" ]; then
191    chmod +x $CHMOD_FILE
192fi
193
194tail -n +2 /tmp/link.exe.${p}.2 >&2
195cat /tmp/link.exe.${p}.1
196rm -f /tmp/link.exe.${p}.2 /tmp/link.exe.${p}.1
197exit $RES
198