1# build system -- ttcdt <dev@triptico.com> public domain
2
3# get working dir
4DIR="$1"
5[ "$DIR" = "" ] && DIR="."
6
7# load info built by config.sh
8[ -f ${DIR}/.build.sh ] && . ${DIR}/.build.sh
9
10[ "$CC" = "" ] && CC="cc"
11[ "$AR" = "" ] && AR="ar"
12[ "$CCLINK" = "" ] && CCLINK="$CC"
13
14# building functions
15
16dep() {
17    local dir=$1 ; shift
18    local target=$1 ; shift
19    local ret=1
20
21    for d in $* ; do
22        # fail immediately if any dependency does not exist
23        case $d in
24        -*) true ;;
25        *)  [ ! -f "${dir}/${d}" ] && echo "${dir}/${d} ???" && exit 1 ;;
26        esac
27
28        # account if any dependency is greater than the target
29        [ ! -f "${target}" -o "${dir}/${d}" -nt "${target}" ] && ret=0
30    done
31
32    return $ret
33}
34
35do_cc() {
36    local target=$1 ; shift
37    local src=$1 ; shift
38
39    if dep "${DIR}" $target $src $* ; then
40        echo $CC $CFLAGS $src
41        $CC -I${DIR} $CFLAGS -c "${DIR}/${src}" -o $target || exit 1
42    fi
43}
44
45do_cpp() {
46    local target=$1 ; shift
47    local src=$1 ; shift
48
49    if dep "${DIR}" $target $src $* ; then
50        echo $CPP $CFLAGS $src
51        $CPP $CFLAGS -c "${DIR}/${src}" -o $target || exit 1
52    fi
53}
54
55do_flex() {
56    local target=$1 ; shift
57    local inter=$1 ; shift
58    local src=$1 ; shift
59
60    if dep "${DIR}" $target $src $* ; then
61        echo flex $src
62        flex "${DIR}/${src}" || exit 1
63        echo $CC $inter
64        $CC -I${DIR} $CFLAGS -c ${inter} -o $target || exit 1
65    fi
66}
67
68do_yacc() {
69    local target=$1 ; shift
70    local inter=$1 ; shift
71    local src=$1 ; shift
72
73    if dep "${DIR}" $target $src $* ; then
74        echo $YACC $src
75        $YACC -d "${DIR}/${src}" || exit 1
76        echo $CC $CFLAGS $inter
77        $CC -I${DIR} $CFLAGS -c ${inter} -o $target || exit 1
78    fi
79}
80
81do_ar() {
82    local target=$1 ; shift
83
84    if dep "." $target $* ; then
85        echo $AR $target
86        $AR r $target $* || exit 1
87    fi
88}
89
90do_tar() {
91    local target=$1 ; shift
92
93    if dep "." $target $* ; then
94        echo tar $target
95        $TAR cf $target $*
96    fi
97}
98
99do_objbin() {
100    local target=$1 ; shift
101    local src=$1 ; shift
102
103    if dep "." $target $src ; then
104        echo objbin $target
105        $LD -r -b binary $src -o $target
106    fi
107}
108
109do_link() {
110    local target=$1 ; shift
111
112    if dep "." $target $* ; then
113        echo link ${LDFLAGS} $target
114        $CCLINK ${CFLAGS} $* ${LDFLAGS} -o $target || exit 1
115    fi
116}
117
118do_moc() {
119    local target=$1 ; shift
120    local src=$1 ; shift
121
122    if dep "${DIR}" $target $src $* ; then
123        echo $MOC $src
124        $MOC -o $target "${DIR}/${src}" || exit 1
125    fi
126}
127
128do_windres() {
129    local target=$1 ; shift
130    local src=$1 ; shift
131
132    if dep "${DIR}" $target $src $* ; then
133        echo $WINDRES $src
134        $WINDRES "${DIR}/${src}" $target || exit 1
135    fi
136}
137
138. ${DIR}/local-build.sh
139