Skip to content

Commit

Permalink
improve build script
Browse files Browse the repository at this point in the history
  • Loading branch information
matekelemen committed Oct 5, 2023
1 parent 1a75870 commit 10d7eaa
Showing 1 changed file with 42 additions and 5 deletions.
47 changes: 42 additions & 5 deletions build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,13 @@ 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 "-c compiler : C++ compiler to use"
echo "-o [opts] : options/arguments to pass on to CMake. Semicolon (;) delimited, or defined repeatedly."
}

if ! command -v python3 &>/dev/null; then
Expand All @@ -30,11 +31,13 @@ package=0
buildType="Release"
buildDir="$scriptDir/build"
installDir=$(get_site_packages_dir)
compiler=g++
generator="Unix Makefiles"
cCacheFlag=""
cmakeArguments=""
cc="gcc"
cxx="g++"

while getopts "hpt:b:i:c:" arg; do
while getopts ":h p t: b: i: c: o:" arg; do
case "$arg" in
h) # Print help and exit without doing anything
print_help
Expand All @@ -53,8 +56,8 @@ while getopts "hpt:b:i:c:" arg; do
i) # Set install directory
installDir="$OPTARG"
;;
c) # Set C++ compiler
compiler="$OPTARG"
o) # Append CMake arguments
cmakeArguments="$cmakeArguments;$OPTARG"
;;
\?) # Unrecognized argument
print_help
Expand All @@ -64,6 +67,35 @@ while getopts "hpt:b:i:c:" arg; do
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"
Expand All @@ -89,8 +121,11 @@ if ! cmake \
"-B$buildDir" \
"-DCMAKE_INSTALL_PREFIX:STRING=$installDir" \
"-G${generator}" \
"-DCMAKE_C_COMPILER:STRING=$cc" \
"-DCMAKE_CXX_COMPILER:STRING=$cxx" \
"-DCMAKE_COLOR_DIAGNOSTICS:BOOL=ON" \
"$cCacheFlag" \
$(echo $cmakeArguments | tr '\;' '\n') \
; then
exit 1
fi
Expand All @@ -100,13 +135,15 @@ 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
Expand Down

0 comments on commit 10d7eaa

Please sign in to comment.