build.sh (4641B)
1 #!/bin/bash 2 3 # Copyright (c) Microsoft Corporation. 4 # Licensed under the MIT License. 5 6 # Synopsis: Simplifies build commands for the SFS Client. 7 # 8 # Description: This script will contain the build commands for the SFS Client. The default build folder will be "<git_root>/build". 9 # Use this on non-Windows platforms in a bash session. 10 # 11 # Example: 12 # $ ./scripts/build.sh 13 # 14 15 # Ensures script stops on errors 16 set -e 17 18 if [[ "${BASH_SOURCE[0]}" != "${0}" ]]; then 19 error "Script is being sourced, it should be executed instead." 20 return 1 21 fi 22 23 COLOR_RED="\033[1;31m" 24 COLOR_YELLOW="\033[1;33m" 25 BOLD_DEFAULT_COLOR="\033[1m" 26 NO_COLOR="\033[0m" 27 28 error() { echo -e "${COLOR_RED}$*${NO_COLOR}" >&2; exit 1; } 29 warn() { echo -e "${COLOR_YELLOW}$*${NO_COLOR}"; } 30 31 clean=false 32 enable_test_overrides="OFF" 33 build_tests="ON" 34 build_samples="ON" 35 build_type="Debug" 36 37 usage() { echo -e "Usage: $0 [-c|--clean] [-b|--build-type {Debug,Release}] [-t|--enable-test-overrides] 38 [--build-tests {${BOLD_DEFAULT_COLOR}ON${NO_COLOR}, OFF}] [--build-samples {${BOLD_DEFAULT_COLOR}ON${NO_COLOR}, OFF}]" 1>&2; exit 1; } 39 40 # Make sure when adding a new option to check if it requires CMake regeneration 41 42 if ! opts=$(getopt \ 43 --longoptions "clean,build-type:,enable-test-overrides,build-tests:,build-samples:" \ 44 --name "$(basename "$0")" \ 45 --options "cb:t" \ 46 -- "$@" 47 ); then 48 usage 49 fi 50 51 eval set "--$opts" 52 53 while [ $# -gt 0 ]; do 54 case "$1" in 55 -c|--clean) 56 clean=true 57 shift 1 58 ;; 59 -b|--build-type) 60 shift 1 61 build_type=$1 62 case "$build_type" in 63 Debug|Release) 64 ;; 65 *) 66 usage 67 ;; 68 esac 69 shift 1 70 ;; 71 -t|--enable-test-overrides) 72 enable_test_overrides="ON" 73 shift 1 74 ;; 75 --build-tests) 76 shift 1 77 build_tests=$1 78 case "$build_tests" in 79 ON|OFF) 80 ;; 81 *) 82 usage 83 ;; 84 esac 85 shift 1 86 ;; 87 --build-samples) 88 shift 1 89 build_samples=$1 90 case "$build_samples" in 91 ON|OFF) 92 ;; 93 *) 94 usage 95 ;; 96 esac 97 shift 1 98 ;; 99 *) 100 break 101 ;; 102 esac 103 done 104 105 script_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" > /dev/null 2>&1 && pwd)" 106 git_root=$(git -C "$script_dir" rev-parse --show-toplevel) 107 108 vcpkg_dir="$git_root/vcpkg" 109 if [ ! -d "$vcpkg_dir" ]; then 110 error "vcpkg not found at $git_root/vcpkg. Source the setup.sh script first." 111 fi 112 113 build_folder="$git_root/build" 114 if $clean && [ -d "$build_folder" ]; then 115 warn "Cleaning build folder before build..." 116 rm -r -f "$build_folder" 117 fi 118 119 regenerate=false 120 cmake_cache_file="$build_folder/CMakeCache.txt" 121 122 test_cmake_cache_value_no_match() { 123 local cmake_cache_file=$1 124 local pattern=$2 125 local expected_value=$3 126 127 value=$(sed -nr "s/$pattern/\1/p" "$cmake_cache_file") 128 if [ -n "$value" ] && [ "$value" == "$expected_value" ]; then 129 return 1 130 fi 131 return 0 132 } 133 134 if [ -f "$cmake_cache_file" ]; then 135 # Regenerate if one of the build options is set to a different value than the one passed in 136 if test_cmake_cache_value_no_match "$cmake_cache_file" "^CMAKE_BUILD_TYPE:STRING=(.*)$" "$build_type"; then 137 regenerate=true 138 fi 139 if test_cmake_cache_value_no_match "$cmake_cache_file" "^SFS_ENABLE_TEST_OVERRIDES:BOOL=(.*)$" "$enable_test_overrides"; then 140 regenerate=true 141 fi 142 if test_cmake_cache_value_no_match "$cmake_cache_file" "^SFS_BUILD_TESTS:BOOL=(.*)$" "$build_tests"; then 143 regenerate=true 144 fi 145 if test_cmake_cache_value_no_match "$cmake_cache_file" "^SFS_BUILD_SAMPLES:BOOL=(.*)$" "$build_samples"; then 146 regenerate=true 147 fi 148 fi 149 150 # Configure cmake if build folder doesn't exist or if the build must be regenerated. 151 # This creates build targets that will be used by the build command 152 if [ ! -d "$build_folder" ] || $regenerate ; then 153 cmake \ 154 -S "$git_root" \ 155 -B "$build_folder" \ 156 -DCMAKE_BUILD_TYPE="$build_type" \ 157 -DSFS_ENABLE_TEST_OVERRIDES="$enable_test_overrides" \ 158 -DSFS_BUILD_TESTS="$build_tests" \ 159 -DSFS_BUILD_SAMPLES="$build_samples" 160 fi 161 162 # This is the build command. If any CMakeLists.txt files change, this will also reconfigure before building 163 cmake --build "$build_folder"