Ben Clayton | feb42e5 | 2022-04-07 19:22:21 +0000 | [diff] [blame] | 1 | #!/usr/bin/env bash |
Austin Eng | cc2516a | 2023-10-17 20:57:54 +0000 | [diff] [blame] | 2 | # Copyright 2022 The Dawn & Tint Authors |
Ben Clayton | feb42e5 | 2022-04-07 19:22:21 +0000 | [diff] [blame] | 3 | # |
Austin Eng | cc2516a | 2023-10-17 20:57:54 +0000 | [diff] [blame] | 4 | # Redistribution and use in source and binary forms, with or without |
| 5 | # modification, are permitted provided that the following conditions are met: |
Ben Clayton | feb42e5 | 2022-04-07 19:22:21 +0000 | [diff] [blame] | 6 | # |
Austin Eng | cc2516a | 2023-10-17 20:57:54 +0000 | [diff] [blame] | 7 | # 1. Redistributions of source code must retain the above copyright notice, this |
| 8 | # list of conditions and the following disclaimer. |
Ben Clayton | feb42e5 | 2022-04-07 19:22:21 +0000 | [diff] [blame] | 9 | # |
Austin Eng | cc2516a | 2023-10-17 20:57:54 +0000 | [diff] [blame] | 10 | # 2. Redistributions in binary form must reproduce the above copyright notice, |
| 11 | # this list of conditions and the following disclaimer in the documentation |
| 12 | # and/or other materials provided with the distribution. |
| 13 | # |
| 14 | # 3. Neither the name of the copyright holder nor the names of its |
| 15 | # contributors may be used to endorse or promote products derived from |
| 16 | # this software without specific prior written permission. |
| 17 | # |
| 18 | # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 19 | # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 20 | # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 21 | # DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE |
| 22 | # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 23 | # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
| 24 | # SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
| 25 | # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
| 26 | # OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 27 | # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
Ben Clayton | feb42e5 | 2022-04-07 19:22:21 +0000 | [diff] [blame] | 28 | |
| 29 | set -e # Fail on any error. |
| 30 | |
| 31 | SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}")" >/dev/null 2>&1 && pwd )" |
Ben Clayton | 138ddcc | 2022-10-13 12:55:42 +0000 | [diff] [blame] | 32 | ROOT_DIR="$( cd "$SCRIPT_DIR/.." >/dev/null 2>&1 && pwd )" |
| 33 | |
| 34 | POSSIBLE_BUILD_SYSTEMS="[gn|cmake]" |
| 35 | POSSIBLE_BUILD_TYPES="[debug|release]" |
| 36 | POSSIBLE_BUILD_ARCHS="[native|x86]" |
| 37 | |
| 38 | BUILD_SYSTEM="" |
| 39 | BUILD_TYPE="" |
| 40 | BUILD_ARCH="" |
Ben Clayton | feb42e5 | 2022-04-07 19:22:21 +0000 | [diff] [blame] | 41 | |
| 42 | function show_usage() { |
Ben Clayton | 138ddcc | 2022-10-13 12:55:42 +0000 | [diff] [blame] | 43 | echo "setup-build $POSSIBLE_BUILD_SYSTEMS $POSSIBLE_BUILD_TYPES $POSSIBLE_BUILD_ARCHS" |
Ben Clayton | feb42e5 | 2022-04-07 19:22:21 +0000 | [diff] [blame] | 44 | echo |
| 45 | echo "creates a build directory in <dawn>/out using either GN or CMake, then" |
| 46 | echo "updates the '<dawn>/out/active' symlink to point to the build directory" |
Ben Clayton | 138ddcc | 2022-10-13 12:55:42 +0000 | [diff] [blame] | 47 | if [[ ! -z "$1" ]]; then |
| 48 | echo |
| 49 | echo "$1" |
| 50 | fi |
Ben Clayton | feb42e5 | 2022-04-07 19:22:21 +0000 | [diff] [blame] | 51 | exit 1 |
| 52 | } |
| 53 | |
Ben Clayton | 138ddcc | 2022-10-13 12:55:42 +0000 | [diff] [blame] | 54 | function set_build_system() { |
| 55 | if [[ ! -z "$BUILD_SYSTEM" ]]; then |
| 56 | echo "conflicting build systems $BUILD_SYSTEM and $1" |
| 57 | exit 1 |
| 58 | fi |
| 59 | BUILD_SYSTEM=$1 |
| 60 | } |
| 61 | |
| 62 | function set_build_type() { |
| 63 | if [[ ! -z "$BUILD_TYPE" ]]; then |
| 64 | echo "conflicting build types $BUILD_TYPE and $1" |
| 65 | exit 1 |
| 66 | fi |
| 67 | BUILD_TYPE=$1 |
| 68 | } |
| 69 | |
| 70 | function set_build_arch() { |
| 71 | if [[ ! -z "$BUILD_ARCH" ]]; then |
| 72 | echo "conflicting build architectures $BUILD_ARCH and $1" |
| 73 | exit 1 |
| 74 | fi |
| 75 | BUILD_ARCH=$1 |
| 76 | } |
| 77 | |
| 78 | for arg in "$@"; do |
| 79 | lowered_arg=$(echo "$arg" | tr '[:upper:]' '[:lower:]') # lowercase |
| 80 | case $lowered_arg in |
| 81 | "gn") |
| 82 | set_build_system $lowered_arg |
| 83 | ;; |
| 84 | "cmake") |
| 85 | set_build_system $lowered_arg |
| 86 | ;; |
| 87 | "debug") |
| 88 | set_build_type $lowered_arg |
| 89 | ;; |
| 90 | "release") |
| 91 | set_build_type $lowered_arg |
| 92 | ;; |
| 93 | "x86") |
| 94 | set_build_arch $lowered_arg |
| 95 | ;; |
| 96 | "native") |
| 97 | ;; |
| 98 | "--help" | "-help" | "-h") |
| 99 | show_usage |
| 100 | ;; |
| 101 | *) |
| 102 | show_usage "unknown argument '$arg'" |
| 103 | ;; |
| 104 | esac |
| 105 | done |
| 106 | |
| 107 | if [[ -z "$BUILD_SYSTEM" ]]; then |
| 108 | show_usage "build system $POSSIBLE_BUILD_SYSTEMS is required" |
| 109 | fi |
| 110 | |
| 111 | if [[ -z "$BUILD_TYPE" ]]; then |
| 112 | show_usage "build type $POSSIBLE_BUILD_TYPES required" |
| 113 | fi |
| 114 | |
| 115 | BUILD_DIR="$BUILD_SYSTEM-$BUILD_TYPE" |
| 116 | if [[ ! -z "$BUILD_ARCH" ]]; then |
| 117 | BUILD_DIR+="-$BUILD_ARCH" |
| 118 | fi |
| 119 | |
Ben Clayton | feb42e5 | 2022-04-07 19:22:21 +0000 | [diff] [blame] | 120 | function generate() { |
Ben Clayton | feb42e5 | 2022-04-07 19:22:21 +0000 | [diff] [blame] | 121 | pushd "$ROOT_DIR" > /dev/null |
Ben Clayton | 446166c | 2022-04-29 20:18:45 +0000 | [diff] [blame] | 122 | mkdir -p "out/$BUILD_DIR" |
| 123 | rm -fr "out/active" || true |
| 124 | ln -s "$BUILD_DIR" "out/active" |
Ben Clayton | 138ddcc | 2022-10-13 12:55:42 +0000 | [diff] [blame] | 125 | "$@" |
Ben Clayton | feb42e5 | 2022-04-07 19:22:21 +0000 | [diff] [blame] | 126 | popd > /dev/null |
| 127 | } |
| 128 | |
| 129 | case $BUILD_SYSTEM in |
| 130 | "gn") |
Ben Clayton | 138ddcc | 2022-10-13 12:55:42 +0000 | [diff] [blame] | 131 | GN_ARGS="" |
Ben Clayton | feb42e5 | 2022-04-07 19:22:21 +0000 | [diff] [blame] | 132 | case $BUILD_TYPE in |
| 133 | "debug") |
Ben Clayton | 138ddcc | 2022-10-13 12:55:42 +0000 | [diff] [blame] | 134 | GN_ARGS+="is_debug=true" |
Ben Clayton | feb42e5 | 2022-04-07 19:22:21 +0000 | [diff] [blame] | 135 | ;; |
| 136 | "release") |
Ben Clayton | 138ddcc | 2022-10-13 12:55:42 +0000 | [diff] [blame] | 137 | GN_ARGS+="is_debug=false" |
Ben Clayton | feb42e5 | 2022-04-07 19:22:21 +0000 | [diff] [blame] | 138 | ;; |
| 139 | *) |
Ben Clayton | 138ddcc | 2022-10-13 12:55:42 +0000 | [diff] [blame] | 140 | show_usage "invalid build type '$BUILD_TYPE'" |
Ben Clayton | feb42e5 | 2022-04-07 19:22:21 +0000 | [diff] [blame] | 141 | ;; |
| 142 | esac |
Ben Clayton | 138ddcc | 2022-10-13 12:55:42 +0000 | [diff] [blame] | 143 | case $BUILD_ARCH in |
| 144 | "") |
| 145 | ;; |
| 146 | "x86") |
| 147 | GN_ARGS+=" target_cpu=\"x86\"" |
| 148 | ;; |
| 149 | *) |
| 150 | show_usage "invalid build architecture '$BUILD_ARCH'" |
| 151 | ;; |
| 152 | esac |
Ben Clayton | 7674986 | 2023-09-29 18:36:29 +0000 | [diff] [blame] | 153 | if [ -f "$ROOT_DIR/out/$BUILD_DIR/args.gn" ]; then |
| 154 | generate "gn" "gen" "out/active" # keep existing args |
| 155 | else |
| 156 | generate "gn" "gen" "out/active" "--args=$GN_ARGS" |
| 157 | fi |
Ben Clayton | feb42e5 | 2022-04-07 19:22:21 +0000 | [diff] [blame] | 158 | ;; |
| 159 | "cmake") |
Ben Clayton | 138ddcc | 2022-10-13 12:55:42 +0000 | [diff] [blame] | 160 | CMAKE_FLAGS=() |
| 161 | CMAKE_FLAGS+=("-DTINT_BUILD_GLSL_WRITER=1") |
| 162 | CMAKE_FLAGS+=("-DTINT_BUILD_HLSL_WRITER=1") |
| 163 | CMAKE_FLAGS+=("-DTINT_BUILD_MSL_WRITER=1") |
| 164 | CMAKE_FLAGS+=("-DTINT_BUILD_SPV_WRITER=1") |
| 165 | CMAKE_FLAGS+=("-DTINT_BUILD_WGSL_WRITER=1") |
Ben Clayton | 2f215d1 | 2024-03-23 10:18:00 +0000 | [diff] [blame] | 166 | CMAKE_FLAGS+=("-DTINT_BUILD_TINTD=1") |
Ben Clayton | 9d3ce3d | 2024-03-04 14:22:22 +0000 | [diff] [blame] | 167 | CMAKE_FLAGS+=("-DDAWN_BUILD_NODE_BINDINGS=1") |
Ben Clayton | 446166c | 2022-04-29 20:18:45 +0000 | [diff] [blame] | 168 | if [[ -x $(command -v ccache) ]]; then |
Ben Clayton | 138ddcc | 2022-10-13 12:55:42 +0000 | [diff] [blame] | 169 | CMAKE_FLAGS+=("-DCMAKE_CXX_COMPILER_LAUNCHER=ccache") |
Ben Clayton | 446166c | 2022-04-29 20:18:45 +0000 | [diff] [blame] | 170 | fi |
Ben Clayton | feb42e5 | 2022-04-07 19:22:21 +0000 | [diff] [blame] | 171 | case $BUILD_TYPE in |
| 172 | "debug") |
Ben Clayton | 138ddcc | 2022-10-13 12:55:42 +0000 | [diff] [blame] | 173 | CMAKE_FLAGS+=("-DCMAKE_BUILD_TYPE=Debug") |
Ben Clayton | feb42e5 | 2022-04-07 19:22:21 +0000 | [diff] [blame] | 174 | ;; |
| 175 | "release") |
Ben Clayton | 138ddcc | 2022-10-13 12:55:42 +0000 | [diff] [blame] | 176 | CMAKE_FLAGS+=("-DCMAKE_BUILD_TYPE=RelWithDebInfo") |
Ben Clayton | feb42e5 | 2022-04-07 19:22:21 +0000 | [diff] [blame] | 177 | ;; |
| 178 | *) |
Ben Clayton | 138ddcc | 2022-10-13 12:55:42 +0000 | [diff] [blame] | 179 | show_usage "invalid build type '$BUILD_TYPE'" |
Ben Clayton | feb42e5 | 2022-04-07 19:22:21 +0000 | [diff] [blame] | 180 | ;; |
| 181 | esac |
Ben Clayton | 138ddcc | 2022-10-13 12:55:42 +0000 | [diff] [blame] | 182 | case $BUILD_ARCH in |
| 183 | "") |
| 184 | ;; |
| 185 | "x86") |
| 186 | CMAKE_FLAGS+=("-DCMAKE_CXX_FLAGS=-m32") |
| 187 | CMAKE_FLAGS+=("-DCMAKE_C_FLAGS=-m32") |
| 188 | ;; |
| 189 | *) |
| 190 | show_usage "invalid build architecture '$BUILD_ARCH'" |
| 191 | ;; |
| 192 | esac |
| 193 | generate "cmake" \ |
| 194 | "-S" "." \ |
| 195 | "-B" "out/active" \ |
| 196 | "-GNinja" \ |
| 197 | "${CMAKE_FLAGS[@]}" |
Ben Clayton | feb42e5 | 2022-04-07 19:22:21 +0000 | [diff] [blame] | 198 | ;; |
| 199 | *) |
Ben Clayton | 138ddcc | 2022-10-13 12:55:42 +0000 | [diff] [blame] | 200 | echo "invalid build system '$BUILD_SYSTEM'" |
Ben Clayton | feb42e5 | 2022-04-07 19:22:21 +0000 | [diff] [blame] | 201 | show_usage |
| 202 | ;; |
| 203 | esac |