-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathbuild.sh
More file actions
executable file
·184 lines (164 loc) · 4.41 KB
/
build.sh
File metadata and controls
executable file
·184 lines (164 loc) · 4.41 KB
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
183
184
#!/bin/bash
DISCLAIMER='// main program
// See project README.md for disclaimer and additional information.
// Feabhas Ltd
'
set -o nounset
set -o errexit
CMAKE='cmake'
if ! $CMAKE --version >/dev/null 2>&1; then
echo "Cannot find cmake command: $CMAKE" >&2
exit 1
fi
GENERATOR='Ninja'
BUILD_OPTIONS=
BUILD_VERBOSE='-v'
# Ninja build does not work with C++20 Modules
if [[ -n $(type -p ninja) && ! -f src/CMakeLists.txt ]]; then
echo "Build generator: ninja $(ninja --version)"
elif [[ -n $(type -p make) ]]; then
echo "Build generator: $(make --version | head -1)"
GENERATOR='Unix Makefiles'
BUILD_OPTIONS='--no-print-directory'
BUILD_VERBOSE='VERBOSE=1'
else
echo "Cannot find 'ninja' or GNU 'make' command:" >&2
exit 1
fi
function usage {
cat <<EOT
Usage: ${0#.*/} [options...]
Wrapper around cmake build system.
Generate options:
reset -- generate (always) and build
debug -- generate/build debug version (default)
release -- generate/build release version
--Cnn -- set C langauge version to nn, also -c
--C++nn -- set C langauge version to nn, also -cpp -CPP
-Dvar=val -- define a CMake variable which must have a value
Build options:
--verbose -- show compiler commands also -v
clean -- remove object files and build
test -- run cmake with test target after a build
clang-tidy -- run clang-tidy after a build
Other options:
--c -- generate main.c if it doesn't exist, also -C
--c++ -- generate main.cpp if it doesn't exist, also -cpp -C++
--help -- this help information, also -h -?
Output written to build/debug (or build/release), executables:
build/debug/Application
Generates compile_commands.json used by tools like clang-tidy.
Set clang_tidy options using -DCLANG_TIDY_OPTIONS="options"
Script will generate a missing main.c/main.cpp based on the
hostname: those staring with c- or ac- are C otherwise C++.
EOT
exit 1
}
function main_template {
LANG="${1:-}"
if [[ -z $LANG ]]; then
LANG=cpp
if [[ $HOSTNAME == *c-* ]]; then
LANG=c
elif test -d *c-*_exercises; then
LANG=c
else
for dir in solutions/01* exercises/solutions/01*; do
if [[ -f $dir/main.c || -f $dir/src/main.c ]]; then
LANG=c
break
fi
done
fi
fi
if [[ $LANG = c ]]; then
echo "Generating src/main.c"
cat >src/main.c <<EOT
${DISCLAIMER}
#include <stdio.h>
int main(void)
{
printf("Hello world!\n");
return 0;
}
EOT
else
echo "Generating src/main.cpp"
cat >src/main.cpp <<EOT
${DISCLAIMER}
#include <iostream>
int main()
{
std::cout << "Hello world!\n";
}
EOT
fi
}
BUILD=build
CONFIG=debug
RESET=
CLEAN=
CMAKE_OPTS=
LANG=
VERBOSE=
TEST=
CLANG_TIDY=
for arg; do
case "$arg" in
--help|-h|-\?) usage ;;
--verbose|-v) VERBOSE="$BUILD_VERBOSE" ;;
debug) CONFIG=debug ;;
release) CONFIG=release ;;
test) TEST=1 ;;
clang-tidy) CLANG_TIDY=1 ;;
clean) CLEAN=--clean-first ;;
reset) RESET=1 ;;
--[cC]) LANG=c ;;
--cpp|--CPP|--[cC]++)
LANG=cpp ;;
--[cC][0-9][0-9])
CMAKE_OPTS="$CMAKE_OPTS -DCMAKE_C_STANDARD=${arg#--[cC]}" ;;
--[cC]++[0-9][0-9]|--CPP[0-9][0-9]|--cpp[0-9][0-9])
CMAKE_OPTS="$CMAKE_OPTS -DCMAKE_CXX_STANDARD=${arg#--[cC]??}" ;;
-D*) CMAKE_OPTS="$CMAKE_OPTS $arg" ;;
*)
echo "Unknown option $arg" >&2
usage
;;
esac
done
# generate main.c/main.cpp if required
[[ ! -d src ]] && mkdir src
[[ ! -d include ]] && mkdir include
if [[ ! -f src/main.c && ! -f src/main.cpp ]]; then
main_template $LANG
fi
# force clean generate
FSTAMP='.files.md5'
old=
if [[ -f "$FSTAMP" ]]; then
old=$(cat $FSTAMP)
fi
find include src -name '*.[ch]' -o -name '*.cpp' | md5sum >$FSTAMP
new=$(cat $FSTAMP)
[[ "$old" != "$new" ]] && RESET=1
if [[ -n $RESET && -d $BUILD/$CONFIG ]]; then
rm -rf $BUILD/$CONFIG
elif [[ ! -d $BUILD/$CONFIG ]]; then
RESET=1
fi
# run cmake
if [[ -n $RESET ]]; then
$CMAKE --preset ${CONFIG} -G "$GENERATOR" $CMAKE_OPTS
fi
if $CMAKE --build --preset ${CONFIG} ${CLEAN} -- $BUILD_OPTIONS $VERBOSE
then
if [[ -n $CLANG_TIDY ]]; then
$CMAKE --build --preset clang-tidy
fi
if [[ -n $TEST ]]; then
$CMAKE --build --preset test
fi
else
exit $?
fi