-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.sh
executable file
·154 lines (138 loc) · 4.59 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
#!/bin/bash
scriptName="$(basename ${BASH_SOURCE[0]})"
scriptDir="$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )"
projectName="$(basename $scriptDir)"
projectNameUpper=$(printf '%s\n' "$projectName" | awk '{ print toupper($0) }')
print_help() {
echo "$scriptName - Configure, build, and install $(basename $scriptDir)"
echo "Usage: $scriptName [OPTION [ARGUMENT]]"
echo "-h : print this help and exit"
echo "-p : package after building"
echo "-t build-type : build type [Debug, Release, RelWithDebInfo] (default: Release)"
echo "-b build-dir : build directory"
echo "-i install-dir : install directory (python site package by default)"
echo "-o [opts] : options/arguments to pass on to CMake. Semicolon (;) delimited, or defined repeatedly."
}
if ! command -v python3 &>/dev/null; then
echo "Error: $scriptName requires python3, but could not find it on the system"
exit 1
fi
# Function for getting the module paths associated with the current interpreter
get_site_packages_dir() {
echo $(python3 -c 'import sysconfig; print(sysconfig.get_paths()["purelib"])')
}
# Default arguments
package=0
buildType="Release"
buildDir="$scriptDir/build"
installDir=$(get_site_packages_dir)
generator="Unix Makefiles"
cCacheFlag=""
cmakeArguments=""
cc="gcc"
cxx="g++"
while getopts ":h p t: b: i: o:" arg; do
case "$arg" in
h) # Print help and exit without doing anything
print_help
exit 0
;;
p) # Package after building
package=1
;;
t) # Set build type
buildType="$OPTARG"
[[ "${buildType}" = "Debug" || "${buildType}" = "RelWithDebInfo" || "${buildType}" = "Release" ]] || (print_help && echo "Invalid build type: $buildType" && exit 1)
;;
b) # Set build directory
buildDir="$OPTARG"
;;
i) # Set install directory
installDir="$OPTARG"
;;
o) # Append CMake arguments
cmakeArguments="$cmakeArguments;$OPTARG"
;;
\?) # Unrecognized argument
print_help
echo "Error: unrecognized argument -$OPTARG"
exit 1
;;
esac
done
case "$(uname -s)" in
Linux*)
compilerFlags=""
;;
Darwin*)
# Set clang from homebrew
if ! command -v brew &> /dev/null; then
echo "Error: $script_name requires Homebrew"
exit 1
fi
if ! brew list llvm >/dev/null 2>&1; then
echo "Error: missing dependency: llvm"
echo "Consider running 'brew install llvm'"
exit 1
fi
toolchainRoot="$(brew --prefix llvm)"
toolchainBin="${toolchainRoot}/bin"
toolchainLib="${toolchainRoot}/lib"
toolchainInclude="${toolchainRoot}/include"
export cc="$toolchainBin/clang"
export cxx="$toolchainBin/clang++"
;;
\?)
echo "Error: unsupported OS $(uname -s)"
exit 1
esac
# Create or clear the build directory
if ! [ -d "$buildDir" ]; then
mkdir -p "$buildDir"
else
rm -f "$buildDir/cmake_install.cmake"
rm -f "$buildDir/CMakeCache.txt"
rm -rf "$buildDir/CMakeFiles"
fi
# Check optional dependency - ninja
if command -v ninja &>/dev/null; then
generator="Ninja"
fi
# Check optional dependency - ccache
if command -v ccache &>/dev/null; then
cCacheFlag="-DCMAKE_CXX_COMPILER_LAUNCHER:STRING=ccache"
fi
# Generate with CMake
if ! cmake \
"-H$scriptDir" \
"-B$buildDir" \
"-DCMAKE_INSTALL_PREFIX:STRING=$installDir" \
"-G${generator}" \
"-DCMAKE_BUILD_TYPE:STRING=$buildType" \
"-DCMAKE_C_COMPILER:STRING=$cc" \
"-DCMAKE_CXX_COMPILER:STRING=$cxx" \
"-DCMAKE_COLOR_DIAGNOSTICS:BOOL=ON" \
"$cCacheFlag" \
$(echo $cmakeArguments | tr '\;' '\n') \
; then
exit 1
fi
# Build and install
if ! cmake --build "$buildDir" --config "$buildType" --target install -j; then
exit 1
fi
# Package
if [ $package -eq 1 ]; then
cd "$buildDir"
if [ "$generator" = "Ninja" ]; then
echo ninja package
ninja package
ninja package_source
else
echo make package
make package
make package_source
fi
fi
# Success
exit 0