1#!/usr/local/bin/bash
2# ===========================================================================
3#
4#                            PUBLIC DOMAIN NOTICE
5#               National Center for Biotechnology Information
6#
7#  This software/database is a "United States Government Work" under the
8#  terms of the United States Copyright Act.  It was written as part of
9#  the author's official duties as a United States Government employee and
10#  thus cannot be copyrighted.  This software/database is freely available
11#  to the public for use. The National Library of Medicine and the U.S.
12#  Government have not placed any restriction on its use or reproduction.
13#
14#  Although all reasonable efforts have been taken to ensure the accuracy
15#  and reliability of the software and data, the NLM and the U.S.
16#  Government do not and cannot warrant the performance or results that
17#  may be obtained by using this software or data. The NLM and the U.S.
18#  Government disclaim all warranties, express or implied, including
19#  warranties of performance, merchantability or fitness for any particular
20#  purpose.
21#
22#  Please cite the author in any work or product based on this material.
23#
24# ===========================================================================
25
26
27# ===========================================================================
28# input library types, and their handling
29#
30#  normal linkage
31#   -l : find shared or static
32#   -s : require static
33#   -d : ignore - will be dynamically loaded
34#
35#  static linkage
36#   -l : require static
37#   -s : require static
38#   -d : require static
39# ===========================================================================
40
41
42# script name
43SELF_NAME="$(basename $0)"
44
45# parameters and common functions
46source "${0%exe.sh}cmn.sh"
47
48# discover tool chain
49case "$LD" in
50link)
51    source "${0%exe.sh}vc++.sh"
52    ;;
53*)
54    echo "$SELF_NAME: unrecognized ld tool - '$LD'"
55    exit 5
56esac
57
58# EXE_CMD was started in tool-specific source
59CMD="$EXE_CMD $LDFLAGS OLE32.lib Ws2_32.lib"
60
61# if building a static executable against dynamic libraries
62# the main application will substitute for name lookup
63if [ $STATIC -eq 1 ] && [ $DYLD -eq 1 ]
64then
65    CMD="$CMD $LD_EXPORT_GLOBAL $LD_MULTIPLE_DEFS"
66fi
67
68# function to convert static libraries to individual COFF files
69convert-static ()
70{
71    if [ $STATIC -eq 0 ]
72    then
73        CMD="$CMD $2.a"
74    else
75        # list members
76        local path=`cd $(dirname $1);pwd`/$(basename $1) # make sure path is absolute
77        local mbrs="$(ar -t $path)"
78
79        # create sub directory
80        rm -rf "$2" && mkdir "$2"
81        if ! cd "$2"
82        then
83            echo "$SELF_NAME: failed to cd to $2"
84            exit 5
85        fi
86        ar -x "$path"
87
88        # add source files to link
89        local m=
90        for m in $mbrs
91        do
92            CMD="$CMD $2/$m"
93        done
94
95        # return to prior location
96        cd - > /dev/null
97    fi
98}
99
100# tack on object files
101CMD="$CMD $(cygpath -w $OBJS)"
102
103# initial dependency upon Makefile - no vers file on Windows
104DEPS="$SRCDIR/Makefile"
105if [ "$LIBS" != "" ]
106then
107    # tack on paths
108    DIRS="$LDIRS:$XDIRS"
109    while [ "$DIRS" != "" ]
110    do
111        DIR="${DIRS%%:*}"
112        [ "$DIR" != "" ] && CMD="$CMD /LIBPATH:$(cygpath -w $DIR)"
113        DIRS="${DIRS#$DIR}"
114        DIRS="${DIRS#:}"
115    done
116
117    HAVE_KERNEL32=0
118    HAVE_WS2=0
119    HAVE_CLIB=0
120
121    if [ $THREADS -ne 0 ] || [ $KPROC -ne 0 ]
122    then
123        if [ $STATIC -ne 0 ]
124        then
125            CMD="$CMD /MT"
126        else
127            CMD="$CMD /MD"
128        fi
129    fi
130
131    # tack on libraries, finding as we go
132    for xLIB in $LIBS
133    do
134
135        # strip off switch
136        xLIBNAME="${xLIB#-[lsd]}"
137
138        # map xLIBNAME
139        case "$xLIBNAME" in
140        dl)
141            if [ $HAVE_KERNEL32 -ne 1 ]
142            then
143                load-ref-symbols
144                load-dynamic
145                CMD="$CMD Kernel32.lib"
146                HAVE_KERNEL32=1
147            fi
148            continue
149            ;;
150
151        ws2)
152            if [ $HAVE_WS2 -ne 1 ]
153            then
154                load-ref-symbols
155                load-dynamic
156                CMD="$CMD ws2_32.lib"
157                HAVE_WS2=1
158            fi
159            continue
160            ;;
161
162        # redirect libm to link against libc.lib in case of windows
163        # omitting the lib defaults to linking against libc.lib
164        m)
165            if [ $HAVE_CLIB -ne 1 ]
166            then
167                load-ref-symbols
168                load-dynamic
169                HAVE_CLIB=1
170            fi
171            continue
172            ;;
173
174##### TEMPORARY #####
175# use ksproc for kproc
176    kproc)
177        xLIBNAME=ksproc
178        ;;
179#####################
180
181        esac
182
183        # look at linkage
184        case "$xLIB" in
185        -l*)
186
187            # normal or dynamic linkage
188            FOUND=0
189            if [ $STATIC -eq 0 ]
190            then
191                find-lib $xLIBNAME.lib $LDIRS
192                if [ "$xLIBPATH" != "" ]
193                then
194
195                    # found it
196                    FOUND=1
197
198                    # load dynamic
199                    load-dynamic
200                    CMD="$CMD lib$xLIBNAME.lib"
201
202                fi
203            fi
204
205            # try static only
206            if [ $FOUND -eq 0 ]
207            then
208                find-lib $xLIBNAME.a $LDIRS
209                if [ "$xLIBPATH" != "" ]
210                then
211
212                    # found it
213                    FOUND=1
214
215                    # add it to dependencies
216                    DEPS="$DEPS $xLIBPATH"
217
218                    # load static
219                    load-static
220                    [ $STATIC -eq 1 ] && load-all-symbols
221                    convert-static "$xLIBPATH" "lib$xLIBNAME"
222
223                fi
224            fi
225
226            # not found within our directories
227            if [ $FOUND -eq 0 ]
228            then
229                [ $STATIC -eq 1 ] && load-ref-symbols
230                load-dynamic
231
232                CMD="$CMD lib$xLIBNAME.lib"
233            fi
234            ;;
235
236        -s*)
237
238            # force static load
239            FOUND=0
240            find-lib $xLIBNAME.a $LDIRS
241            if [ "$xLIBPATH" != "" ]
242            then
243
244                # found it
245                FOUND=1
246
247                # add it to dependencies
248                DEPS="$DEPS $xLIBPATH"
249
250                # load static
251                load-static
252                [ $STATIC -eq 1 ] && load-all-symbols
253                convert-static "$xLIBPATH" "lib$xLIBNAME"
254
255            fi
256
257            # not found within our directories
258            if [ $FOUND -eq 0 ]
259            then
260                # set load to static
261                load-static
262                [ $STATIC -eq 1 ] && load-all-symbols
263                CMD="$CMD lib$xLIBNAME.lib"
264            fi
265            ;;
266
267        -d*)
268
269            FOUND=0
270            if [ $STATIC -eq 1 ]
271            then
272                find-lib $xLIBNAME.a $LDIRS
273                if [ "$xLIBPATH" != "" ]
274                then
275
276                    # found it
277                    FOUND=1
278
279                    # add it to dependencies
280                    DEPS="$DEPS $xLIBPATH"
281
282                    # load static
283                    load-static
284                    load-all-symbols
285                    convert-static "$xLIBPATH" "lib$xLIBNAME"
286
287                fi
288
289                # not found within our directories
290                if [ $FOUND -eq 0 ]
291                then
292                    load-static
293                    load-all-symbols
294
295                    CMD="$CMD lib$xLIBNAME"
296                fi
297            fi
298            ;;
299
300
301        esac
302
303    done
304fi
305
306# return to normal
307load-ref-symbols
308load-dynamic
309
310# produce shared library
311echo $CMD
312$CMD || exit $?
313
314# produce dependencies
315if [ "$DEPFILE" != "" ]
316then
317    echo "$TARG: $DEPS" > "$DEPFILE"
318fi
319
320# cleanup temporary files
321rm -f $TARG.lib $TARG.exp
322