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 : require shared
34#
35#  static linkage
36#   -l : require static
37#   -s : require static
38#   -d : ignore
39# ===========================================================================
40
41
42# script name
43SELF_NAME="$(basename $0)"
44BUILD_DIR="$(dirname $0)"
45
46# parameters and common functions
47source "${0%dlib.sh}cmn.sh"
48
49# discover tool chain
50case "$LD" in
51g*)
52    source "${0%dlib.sh}gcc.sh"
53    ;;
54c*)
55    source "${0%dlib.sh}clang.sh"
56    ;;
57 *)
58    echo "$SELF_NAME: unrecognized ld tool - '$LD'"
59    exit 5
60esac
61
62# DLIB_CMD was started in tool-specific source
63CMD="$DLIB_CMD $LDFLAGS"
64
65# tack on object files
66CMD="$CMD $OBJS"
67
68# list of static libraries used to create dynamic lib
69SLIBS=''
70
71# initial dependency upon Makefile and vers file
72DEPS="$SRCDIR/Makefile"
73if [ "$LIBS" != "" ]
74then
75    # tack on paths
76    DIRS="$LDIRS:$XDIRS"
77    while [ "$DIRS" != "" ]
78    do
79        DIR="${DIRS%%:*}"
80        [ "$DIR" != "" ] && CMD="$CMD -L$DIR"
81        DIRS="${DIRS#$DIR}"
82        DIRS="${DIRS#:}"
83    done
84
85    # update LD_LIBRARY_PATH
86    unset LD_LIBRARY_PATH
87    export LD_LIBRARY_PATH="$LDIRS:$XDIRS"
88
89    # tack on libraries, finding as we go
90    for LIB in $LIBS
91    do
92
93        # strip off switch
94        LIBNAME="${LIB#-[lsd]}"
95
96        # look at linkage
97        case "$LIB" in
98        -ldl|-ddl)
99
100            # always load libdl as shared library
101            load-ref-symbols
102            load-dynamic
103#            CMD="$CMD -ldl"
104            ;;
105
106        -l*)
107
108            # normal or dynamic linkage
109            FOUND=0
110            if [ $STATIC -eq 0 ]
111            then
112                find-lib $LIBNAME.so $LDIRS
113                if [ "$LIBPATH" != "" ]
114                then
115
116                    # found it
117                    FOUND=1
118
119                    # load normally
120                    load-ref-symbols
121                    load-dynamic
122                    CMD="$CMD -l$LIBNAME"
123
124                fi
125            fi
126
127            # try static only
128            if [ $FOUND -eq 0 ]
129            then
130                find-lib $LIBNAME.a $LDIRS
131                if [ "$LIBPATH" != "" ]
132                then
133
134                    # found it
135                    FOUND=1
136
137                    # add it to dependencies
138                    DEPS="$DEPS $LIBPATH"
139                    SLIBS="$SLIBS $(dirname $LIBPATH)/lib$LIBNAME.a"
140
141                    # load static
142                    load-static
143                    load-all-symbols
144                    CMD="$CMD -l$LIBNAME"
145
146                fi
147            fi
148
149            # not found within our directories
150            if [ $FOUND -eq 0 ]
151            then
152
153                if [ $STATICSYSLIBS -eq 1 ]
154                then
155                    case "$LIBNAME" in
156                    z|bz2)
157                        # set load to static
158                        load-static
159                        load-all-symbols
160                        ;;
161
162                    *)
163                        # set load to dynamic
164                        load-ref-symbols
165                        load-dynamic
166                        ;;
167
168                    esac
169                else
170                    # set load to normal
171                    load-ref-symbols
172                    load-dynamic
173                fi
174
175                CMD="$CMD -l$LIBNAME"
176            fi
177            ;;
178
179        -s*)
180
181            # force static load
182            FOUND=0
183            find-lib $LIBNAME.a $LDIRS
184            if [ "$LIBPATH" != "" ]
185            then
186
187                # found it
188                FOUND=1
189
190                # add it to dependencies
191                DEPS="$DEPS $LIBPATH"
192                SLIBS="$SLIBS $(dirname $LIBPATH)/lib$LIBNAME.a"
193
194                # load static
195                load-static
196                load-all-symbols
197                CMD="$CMD -l$LIBNAME"
198
199            fi
200
201            # not found within our directories
202            if [ $FOUND -eq 0 ]
203            then
204
205                if [ $STATIC -eq 1 ] || [ $STATICSYSLIBS -eq 1 ]
206                then
207                    # set load to static
208                    load-static
209                    load-all-symbols
210                else
211
212                    case "$LIBNAME" in
213                    z|bz2)
214                        # set load to dynamic
215                        load-ref-symbols
216                        load-dynamic
217                        ;;
218
219                    *)
220                        # set load to static
221                        load-static
222                        load-all-symbols
223                        ;;
224                    esac
225                fi
226
227                CMD="$CMD -l$LIBNAME"
228            fi
229            ;;
230
231        -d*)
232
233            # only dynamic linkage
234            FOUND=0
235            if [ $STATIC -eq 0 ]
236            then
237                find-lib $LIBNAME.so $LDIRS
238                if [ "$LIBPATH" != "" ]
239                then
240
241                    # found it
242                    FOUND=1
243
244                    # load normally
245                    load-ref-symbols
246                    load-dynamic
247                    CMD="$CMD -l$LIBNAME"
248
249                fi
250            fi
251
252            # not found within our directories
253            if [ $FOUND -eq 0 ]
254            then
255                # set load to normal
256                load-ref-symbols
257                load-dynamic
258                CMD="$CMD -l$LIBNAME"
259            fi
260            ;;
261
262        esac
263
264    done
265fi
266
267# put state back to normal
268load-ref-symbols
269load-dynamic
270
271# add in pthreads
272if [ $THREADS -ne 0 ]
273then
274    CMD="$CMD -lpthread"
275fi
276
277# add in xml
278if [ $HAVE_XML -ne 0 ]
279then
280    CMD="$CMD -lxml2"
281fi
282
283# add in math library
284if [ $HAVE_M -ne 0 ]
285then
286    CMD="$CMD -lm"
287fi
288
289# produce shared library
290echo "$CMD"
291$CMD || exit $?
292
293# produce dependencies
294if [ "$DEPFILE" != "" ]
295then
296    echo "$TARG: $DEPS" > "$DEPFILE"
297fi
298
299if [ $CHECKSUM -eq 1 ]
300then
301    SCM_DIR="${BUILD_DIR%/*}/scm"
302    LOGFILE="$SCM_DIR/scm.log"
303    MSG=">>>>> scm: calling the collect script from ld.linux.dlib.sh <<<<<<"
304    #echo "$MSG"
305    echo "$MSG" >> $LOGFILE
306
307    "$BUILD_DIR/scm-collect.sh" "$OBJS" "$SLIBS" | sort -u > "$TARG.md5"
308fi