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