-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.sh
executable file
·163 lines (121 loc) · 3.9 KB
/
build.sh
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
#!/bin/bash
# Replace spaces with \n = One command per line.
_cmd_list=$(echo $* | tr ' ' '\n')
_build_mode=""
_build_arch=0
_this_sh_path="$( cd "$(dirname "$0")" ; pwd -P )"
_cmakelist_dir=$_this_sh_path
_do_clean=0
_test_mode="OFF"
for _cmd in $_cmd_list
do
case $_cmd in
-c|-clean)
let _do_clean=1
;;
-tc|-total-clean)
let _do_clean=2
;;
-d|-debug)
if [[ $_build_mode == "release" ]]; then
echo "$0: Can't build for debug and release modes at the same time."
exit
fi
_build_mode="debug"
;;
-r|-release)
if [[ $_build_mode == "release" ]]; then
echo "$0: Can't build for debug and release modes at the same time."
exit
fi
_build_mode="release"
;;
-a32)
if [ $_build_arch == 64 ]; then
echo "$0: Can't build for 32-bit and 64-bit architectures " \
"at the same time."
exit
fi
let _build_arch=32
;;
-a64)
if [ $_build_arch == 32 ]; then
echo "$0: Can't build for 32-bit and 64-bit architectures " \
"at the same time."
exit
fi
let _build_arch=64
;;
-t|-test)
_test_mode="ON"
;;
-h|-help)
echo "Usage: $0 [OPTION]..."
echo "Run CMake and make in the build directory."
echo -e "Build in debug mode and no tests if none of -d -r -t is specified.\n"
echo -e "Example: $0 -tc -r -t\n"
echo "Optional arguments:"
echo -e " -d, -debug\tBuild in DEBUG mode."
echo -e " -r, -release\tBuild in RELEASE mode."
echo -e " -t, -test\tBuild tests."
echo -e " -c, -clean\tRemove the CMakeCache.txt file."
echo -e " -tc, -total-clean\tRemove the entire build directory."
echo -e " -h, -help\tDisplay this help and exit."
echo -e "\nReport bugs to: dartzon@gmail.com"
exit
;;
*)
echo "$0: invalid option -- '$_cmd'"
echo -e "Try '$0 -h' for more information."
exit
;;
esac
done
if [ -z $_build_mode ]; then
_build_mode="release"
fi
if [ $_build_arch == 0 ]; then
_build_arch=64
fi
# Assemble the correct build dir path.
_build_dir=build/$_build_mode/$_build_arch
# Clean build directory if requested.
case $_do_clean in
1)
if [ -f $_build_dir/CMakeCache.txt ]; then
rm $_build_dir/CMakeCache.txt
echo "$_build_mode $_build_arch CMakeCache.txt removed"
fi
;;
2)
if [ -e $_build_dir ]; then
rm -r $_build_dir
echo "$_build_mode $_build_arch build directory removed"
fi
esac
# Create build directory if it doesn't exist.
if [ ! -e $_build_dir ]; then
mkdir -p $_build_dir
fi
cd $_build_dir
# Run cmake if necessary.
if [ ! -e $_build_dir ] || [ ! -f $_build_dir/CMakeCache.txt ]; then
# Use clang compiler.
export CC=clang
export CXX=clang++
cmake -G "Unix Makefiles" "$_cmakelist_dir" -DCMAKE_BUILD_TYPE="$_build_mode" \
-DCMAKE_EXPORT_COMPILE_COMMANDS=1 -DBUILD_TESTS="$_test_mode"
fi
# Generate .dir-locals.el
_project_name=$(cat $_this_sh_path/CMakeLists.txt | grep "project(" | cut -d'(' -f2 | cut -d')' -f1)
source $_this_sh_path/.generate-dir-locals.sh $_build_mode $_build_arch \
$_this_sh_path $_project_name $*
# Place .clang-format in the project's root.
ln -sf ~/.dotfiles/not-installable/clang-tools-config/clang-format $_this_sh_path/.clang-format
# Place .clang-tidy in the project's root.
ln -sf ~/.dotfiles/not-installable/clang-tools-config/clang-tidy $_this_sh_path/.clang-tidy
# Run make.
_jobs_count=$(grep -c ^processor /proc/cpuinfo)
make -j$_jobs_count
# Copy the compilation database to the project root for cquery.
ln -sf $_build_dir/compile_commands.json $_this_sh_path/compile_commands.json