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# prepare script name
27SELF_NAME="$(basename $0)"
28SCRIPT_BASE="${0%.sh}"
29
30# os
31OS="$1"
32shift
33
34# binary compiler
35CC="$1"
36shift
37
38# configuration
39unset TARG
40unset ARGS
41CHECKSUM=0
42OBJX=o
43
44while [ $# -ne 0 ]
45do
46
47    case "$1" in
48    --cflags)
49        ARGS="$ARGS $2"
50        shift
51        ;;
52
53    --checksum)
54        CHECKSUM=1
55        ;;
56
57    --objx)
58        OBJX="$2"
59        shift
60        ;;
61
62    -o*)
63        ARGS="$ARGS $1"
64        ARG="${1#-o}"
65        if [ "$ARG" = "" ]
66        then
67            ARGS="$ARGS $2"
68            ARG="$2"
69            shift
70        fi
71        TARG="$ARG"
72        ;;
73
74    *)
75        ARGS="$ARGS $1"
76        ;;
77
78    esac
79
80    shift
81done
82
83# *** START SCM-code ***************************************
84if [ $CHECKSUM -eq 1 ]
85then
86    if [ "$TARG" = "" ]
87    then
88        echo "$SELF_NAME: no target specified"
89        exit 5
90    fi
91
92    if [ "$TARG" = "${TARG%.$OBJX}" ]
93    then
94        echo "$SELF_NAME: malformed target"
95        exit 6
96    fi
97
98    TARG="${TARG%.$OBJX}"
99fi
100# *** END SCM-code ***************************************
101
102CMD="$CC $ARGS"
103echo "$CMD"
104$CMD || exit $?
105
106# *** START SCM-code ***************************************
107if [ $CHECKSUM -eq 1 ] && [ "$OS" = "linux" ]
108then
109    CMD="strip $TARG.$OBJX -o $TARG.stripped.$OBJX"
110    echo "$CMD"
111    if $CMD
112    then
113        MD5RES=`md5sum -b $TARG.stripped.$OBJX`
114        STATUS=$?
115        rm -f "$TARG.stripped.$OBJX" || true
116        if [ $STATUS -eq 0 ]
117            then
118            MD5VALUE=${MD5RES:0:32}
119            echo "$TARG.$OBJX=$MD5VALUE" > "$TARG.$OBJX.md5"
120            else
121            exit $STATUS
122        fi
123    else
124        STATUS=$?
125        rm "$TARG.$OBJX"
126        exit $STATUS
127    fi
128fi
129# *** END SCM-code ***************************************
130