blob: fc2aff22a11dfb10fbdc82869aa5b1e461038be3 [file] [log] [blame]
Ben Claytonfeb42e52022-04-07 19:22:21 +00001#!/usr/bin/env bash
Austin Engcc2516a2023-10-17 20:57:54 +00002# Copyright 2022 The Dawn & Tint Authors
Ben Claytonfeb42e52022-04-07 19:22:21 +00003#
Austin Engcc2516a2023-10-17 20:57:54 +00004# Redistribution and use in source and binary forms, with or without
5# modification, are permitted provided that the following conditions are met:
Ben Claytonfeb42e52022-04-07 19:22:21 +00006#
Austin Engcc2516a2023-10-17 20:57:54 +00007# 1. Redistributions of source code must retain the above copyright notice, this
8# list of conditions and the following disclaimer.
Ben Claytonfeb42e52022-04-07 19:22:21 +00009#
Austin Engcc2516a2023-10-17 20:57:54 +000010# 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 Claytonfeb42e52022-04-07 19:22:21 +000028
29set -e # Fail on any error.
30
31SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}")" >/dev/null 2>&1 && pwd )"
Ben Clayton138ddcc2022-10-13 12:55:42 +000032ROOT_DIR="$( cd "$SCRIPT_DIR/.." >/dev/null 2>&1 && pwd )"
33
34POSSIBLE_BUILD_SYSTEMS="[gn|cmake]"
35POSSIBLE_BUILD_TYPES="[debug|release]"
36POSSIBLE_BUILD_ARCHS="[native|x86]"
37
38BUILD_SYSTEM=""
39BUILD_TYPE=""
40BUILD_ARCH=""
Ben Claytonfeb42e52022-04-07 19:22:21 +000041
42function show_usage() {
Ben Clayton138ddcc2022-10-13 12:55:42 +000043 echo "setup-build $POSSIBLE_BUILD_SYSTEMS $POSSIBLE_BUILD_TYPES $POSSIBLE_BUILD_ARCHS"
Ben Claytonfeb42e52022-04-07 19:22:21 +000044 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 Clayton138ddcc2022-10-13 12:55:42 +000047 if [[ ! -z "$1" ]]; then
48 echo
49 echo "$1"
50 fi
Ben Claytonfeb42e52022-04-07 19:22:21 +000051 exit 1
52}
53
Ben Clayton138ddcc2022-10-13 12:55:42 +000054function 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
62function 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
70function 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
78for 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
105done
106
107if [[ -z "$BUILD_SYSTEM" ]]; then
108 show_usage "build system $POSSIBLE_BUILD_SYSTEMS is required"
109fi
110
111if [[ -z "$BUILD_TYPE" ]]; then
112 show_usage "build type $POSSIBLE_BUILD_TYPES required"
113fi
114
115BUILD_DIR="$BUILD_SYSTEM-$BUILD_TYPE"
116if [[ ! -z "$BUILD_ARCH" ]]; then
117 BUILD_DIR+="-$BUILD_ARCH"
118fi
119
Ben Claytonfeb42e52022-04-07 19:22:21 +0000120function generate() {
Ben Claytonfeb42e52022-04-07 19:22:21 +0000121 pushd "$ROOT_DIR" > /dev/null
Ben Clayton446166c2022-04-29 20:18:45 +0000122 mkdir -p "out/$BUILD_DIR"
123 rm -fr "out/active" || true
124 ln -s "$BUILD_DIR" "out/active"
Ben Clayton138ddcc2022-10-13 12:55:42 +0000125 "$@"
Ben Claytonfeb42e52022-04-07 19:22:21 +0000126 popd > /dev/null
127}
128
129case $BUILD_SYSTEM in
130 "gn")
Ben Clayton138ddcc2022-10-13 12:55:42 +0000131 GN_ARGS=""
Ben Claytonfeb42e52022-04-07 19:22:21 +0000132 case $BUILD_TYPE in
133 "debug")
Ben Clayton138ddcc2022-10-13 12:55:42 +0000134 GN_ARGS+="is_debug=true"
Ben Claytonfeb42e52022-04-07 19:22:21 +0000135 ;;
136 "release")
Ben Clayton138ddcc2022-10-13 12:55:42 +0000137 GN_ARGS+="is_debug=false"
Ben Claytonfeb42e52022-04-07 19:22:21 +0000138 ;;
139 *)
Ben Clayton138ddcc2022-10-13 12:55:42 +0000140 show_usage "invalid build type '$BUILD_TYPE'"
Ben Claytonfeb42e52022-04-07 19:22:21 +0000141 ;;
142 esac
Ben Clayton138ddcc2022-10-13 12:55:42 +0000143 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 Clayton76749862023-09-29 18:36:29 +0000153 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 Claytonfeb42e52022-04-07 19:22:21 +0000158 ;;
159 "cmake")
Ben Clayton138ddcc2022-10-13 12:55:42 +0000160 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 Clayton2f215d12024-03-23 10:18:00 +0000166 CMAKE_FLAGS+=("-DTINT_BUILD_TINTD=1")
Ben Clayton9d3ce3d2024-03-04 14:22:22 +0000167 CMAKE_FLAGS+=("-DDAWN_BUILD_NODE_BINDINGS=1")
Ben Clayton446166c2022-04-29 20:18:45 +0000168 if [[ -x $(command -v ccache) ]]; then
Ben Clayton138ddcc2022-10-13 12:55:42 +0000169 CMAKE_FLAGS+=("-DCMAKE_CXX_COMPILER_LAUNCHER=ccache")
Ben Clayton446166c2022-04-29 20:18:45 +0000170 fi
Ben Claytonfeb42e52022-04-07 19:22:21 +0000171 case $BUILD_TYPE in
172 "debug")
Ben Clayton138ddcc2022-10-13 12:55:42 +0000173 CMAKE_FLAGS+=("-DCMAKE_BUILD_TYPE=Debug")
Ben Claytonfeb42e52022-04-07 19:22:21 +0000174 ;;
175 "release")
Ben Clayton138ddcc2022-10-13 12:55:42 +0000176 CMAKE_FLAGS+=("-DCMAKE_BUILD_TYPE=RelWithDebInfo")
Ben Claytonfeb42e52022-04-07 19:22:21 +0000177 ;;
178 *)
Ben Clayton138ddcc2022-10-13 12:55:42 +0000179 show_usage "invalid build type '$BUILD_TYPE'"
Ben Claytonfeb42e52022-04-07 19:22:21 +0000180 ;;
181 esac
Ben Clayton138ddcc2022-10-13 12:55:42 +0000182 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 Claytonfeb42e52022-04-07 19:22:21 +0000198 ;;
199 *)
Ben Clayton138ddcc2022-10-13 12:55:42 +0000200 echo "invalid build system '$BUILD_SYSTEM'"
Ben Claytonfeb42e52022-04-07 19:22:21 +0000201 show_usage
202 ;;
203esac