-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild
executable file
·182 lines (141 loc) · 3.17 KB
/
build
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
#!/bin/sh
set -e -u
buildProductsDir=$( pwd )
projectDir=$( dirname "$0" )
# Make projectDir absolute
case $projectDir in
/*) ;;
*) projectDir="$buildDir/$projectDir"
esac
. "$projectDir/shared.inc"
printHelp()
{
help='
SYNTAX
------
./build <build_style> [options] ...
OPTIONS
-------
-help Show this help screen.
-clean Make a clean build.
-listSytles List available build styples.
'
printf '%s\n' "$help"
exit "$1"
}
clean=
buildStyle=
while [ $# -gt 0 ]
do
case $1 in
-listStyles)
echo
echo "Available Build Styles:"
echo
for style in "$projectDir/cfg"/*.cfg
do
if [ -f "$style" ]
then
stylename=$( basename "$style" )
stylename=${stylename%.cfg}
printf '%s\n' "$stylename"
fi
done
exit 0
;;
-help) printHelp 0 ;;
-clean) clean=true ;;
-*) syntaxError "Unkown option \"$1\"" ;;
*)
if [ -f "$projectDir/cfg/$1.cfg" ]
then
[ -z "$buildStyle" ] \
|| syntaxError "Multiple build styles are not allowed"
buildStyle=$1
else
syntaxError "Unkonwn build style \"$1\""
fi
;;
esac
shift
done
buildStyle=${buildStyle:-"release"}
cfgFile="$projectDir/cfg/$buildStyle.cfg"
srcDir="$projectDir/src/code"
buildDir="$buildProductsDir/build"
objDir="$buildDir/objects"
lastBuildCfg="$buildDir/last.cfg"
if [ -n "$clean" ] \
|| ( [ -f "$lastBuildCfg" ] && ! cmp -s "$lastBuildCfg" "$cfgFile" )
then
rm -r "$buildDir"
fi
[ -d "$buildDir" ] || mkdir -p "$buildDir"
[ -f "$lastBuildCfg" ] || cp "$cfgFile" "$lastBuildCfg"
buildArgs=
parseCfgFile buildArgs "$cfgFile"
srcFileList="$buildDir/sources.txt"
find "$srcDir" -name "*.c" >"$srcFileList"
echo
echo '=== Building ==='
[ -z "$clean" ] || echo "(clean build)"
echo
while IFS='' read -r srcFile
do
echo "Next: ${srcFile#"$projectDir/"}"
objFile="$objDir/${srcFile#"$srcDir/"}.o"
flagsFile=$( findCompileFlags "$srcFile" "$projectDir" )
flagsFileDir=
flagsFileContent=
if [ -n "$flagsFile" ]
then
parseCfgFile flagsFileContent "$flagsFile"
flagsFileDir=$( dirname "$flagsFile" )
fi
if ! objectFileIsOutdated \
"$objFile" "$srcFile" \
"$flagsFileContent" "$flagsFileDir"
then
echo 'Nothing to do.'
echo
continue
fi
objFileDir=$( dirname "$objFile" )
[ -d "$objFileDir" ] || mkdir -p "$objFileDir"
buildCmd="clang -c -o $( quote "$objFile" )"
buildCmd="$buildCmd $flagsFileContent $buildArgs"
buildCmd="$buildCmd $(quote "$srcFile" )"
if [ -n "$flagsFileDir" ]
then
buildCmd="( cd $( quote "$flagsFileDir" ) && $buildCmd )"
fi
if ! eval "$buildCmd"
then
echo "Error: Building failed!" >&2
echo "Build command was: $buildCmd" >&2
exit 1
fi
echo "Created ${objFile#"$buildProductsDir/"}"
echo
done <"$srcFileList"
echo
echo '=== Linking ==='
echo
libFile="$buildProductsDir/$LIBNAME"
if [ ! -f "$libFile" ] \
|| anyFileIsNewerThan "$libFile" "$objDir"
then
objFileList="$buildDir/objects.txt"
find "$objDir" -name "*.o" >"$objFileList"
arCmd="ar -q -s -c $( quote "$libFile" )"
while IFS='' read -r objFile
do
arCmd="$arCmd $( quote "$objFile" )"
done <"$objFileList"
[ -f "$libFile" ] && rm "$libFile"
eval "$arCmd"
echo "Created $LIBNAME"
else
echo "Nothing to do."
fi
echo