1#! /bin/sh
2# set -x
3#
4# %CopyrightBegin%
5#
6# Copyright Ericsson AB 2002-2020. 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=`msys2win_path.sh -m $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=`msys2win_path.sh -m $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=`msys2win_path.sh -m $y`;
70	    CMD="$CMD -implib:\"${MPATH}\"";;
71	-def:*)
72	    y=`echo $x | sed 's,^-def:\(.*\),\1,g'`;
73	    MPATH=`msys2win_path.sh -m $y`;
74	    CMD="$CMD -def:\"${MPATH}\"";;
75	-o)
76	    shift
77	    MPATH=`msys2win_path.sh -m $1`;
78	    OUTPUT_FILENAME="$MPATH";;
79	-o/*)
80	    y=`echo $x | sed 's,^-[Io]\(/.*\),\1,g'`;
81	    MPATH=`msys2win_path.sh -m $y`;
82	    OUTPUT_FILENAME="$MPATH";;
83	/*)
84	    MPATH=`msys2win_path.sh -m $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	    linkadd_pdb="-pdb:\"${fn}pdb\"";;
104    *.dll|*.DLL)
105	    fn=`echo "$OUTPUT_FILENAME" | sed 's,[dD][lL][lL]$,,g'`;
106	    linkadd_pdb="-pdb:\"${fn}pdb\"";;
107    "")
108	    linkadd_pdb="-pdb:\"a.pdb\"";;
109    *)
110	    linkadd_pdb="-pdb:\"${OUTPUT_FILENAME}.pdb\"";;
111esac
112
113    linktype="-debug $linkadd_pdb"
114
115CHMOD_FILE=""
116
117if [ $BUILD_DLL = true ];then
118    case "$OUTPUT_FILENAME" in
119	*.exe|*.EXE)
120	    echo "Warning, output set to .exe when building DLL" >&2
121	    CHMOD_FILE="$OUTPUT_FILENAME";
122	    CMD="-dll -out:\"$OUTPUT_FILENAME\" $CMD";
123	    OUTPUTRES="${OUTPUT_FILENAME}\;2";
124	    MANIFEST="${OUTPUT_FILENAME}.manifest";;
125	*.dll|*.DLL)
126	    CMD="-dll -out:\"$OUTPUT_FILENAME\" $CMD";
127	    OUTPUTRES="${OUTPUT_FILENAME}\;2";
128	    MANIFEST="${OUTPUT_FILENAME}.manifest";;
129	"")
130	    CMD="-dll -out:\"a.dll\" $CMD";
131	    OUTPUTRES="a.dll\;2";
132	    MANIFEST="a.dll.manifest";;
133	*)
134	    CMD="-dll -out:\"${OUTPUT_FILENAME}.dll\" $CMD";
135	    OUTPUTRES="${OUTPUT_FILENAME}.dll\;2";
136	    MANIFEST="${OUTPUT_FILENAME}.dll.manifest";;
137    esac
138else
139    case "$OUTPUT_FILENAME" in
140	*.exe|*.EXE)
141	    CHMOD_FILE="$OUTPUT_FILENAME";
142	    CMD="-out:\"$OUTPUT_FILENAME\" $CMD";
143	    OUTPUTRES="${OUTPUT_FILENAME}\;1"
144	    MANIFEST="${OUTPUT_FILENAME}.manifest";;
145	*.dll|*.DLL)
146	    echo "Warning, output set to .dll when building EXE" >&2
147	    CMD="-out:\"$OUTPUT_FILENAME\" $CMD";
148	    OUTPUTRES="${OUTPUT_FILENAME}\;1";
149	    MANIFEST="${OUTPUT_FILENAME}.manifest";;
150	"")
151	    CHMOD_FILE="a.exe";
152	    CMD="-out:\"a.exe\" $CMD";
153	    OUTPUTRES="a.exe\;1";
154	    MANIFEST="a.exe.manifest";;
155	*)
156	    CMD="-out:\"${OUTPUT_FILENAME}.exe\" $CMD";
157	    OUTPUTRES="${OUTPUT_FILENAME}.exe\;1";
158	    MANIFEST="${OUTPUT_FILENAME}.exe.manifest";;
159    esac
160fi
161
162p=$$
163CMD="$linktype -nologo -incremental:no $CMD $STDLIB $DEFAULT_LIBRARIES"
164if [ "X$LD_SH_DEBUG_LOG" != "X" ]; then
165    echo ld.sh "$SAVE" >>$LD_SH_DEBUG_LOG
166    echo link.exe $CMD >>$LD_SH_DEBUG_LOG
167fi
168eval link.exe "$CMD"  >/tmp/link.exe.${p}.1 2>/tmp/link.exe.${p}.2
169RES=$?
170
171CMANIFEST=`win2msys_path.sh $MANIFEST`
172if [ "$RES" = "0" -a -f "$CMANIFEST" ]; then
173    # Add stuff to manifest to turn off "virtualization"
174    sed -n -i '1h;1!H;${;g;s,<trustInfo.*</trustInfo>.,,g;p;}' $CMANIFEST 2>/dev/null
175    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
176
177    eval mt.exe -nologo -manifest "$MANIFEST" -outputresource:"$OUTPUTRES" >>/tmp/link.exe.${p}.1 2>>/tmp/link.exe.${p}.2
178    RES=$?
179    if [ "$RES" != "0" ]; then
180	REMOVE=`echo "$OUTPUTRES" | sed 's,\\\;[12]$,,g'`
181	CREMOVE=`win2msys_path.sh $REMOVE`
182        ## If Defender or Search are enabled, they will lock just created files
183        ## and then mt will fail :/
184        echo "If you get this error, make sure Windows Defender AND Windows Search is disabled">>/tmp/link.exe.${p}.1
185	rm -f "$CREMOVE"
186    fi
187    rm -f "$CMANIFEST"
188fi
189
190# This works around some strange behaviour
191# in cygwin 1.7 Beta on Windows 7 with samba drive.
192# Configure will think the compiler failed if test -x fails,
193# which it might do as we might not be the owner of the
194# file.
195if [ '!' -z "$CHMOD_FILE" -a -s "$CHMOD_FILE" -a '!' -x "$CHMOD_FILE" ]; then
196    chmod +x $CHMOD_FILE
197fi
198
199tail -n +2 /tmp/link.exe.${p}.2 >&2
200cat /tmp/link.exe.${p}.1
201rm -f /tmp/link.exe.${p}.2 /tmp/link.exe.${p}.1
202exit $RES
203