Apply Upstream code (2021-04-06)
[platform/upstream/connectedhomeip.git] / gn_build.sh
1 #!/usr/bin/env bash
2 #
3 # Copyright (c) 2020 Project CHIP Authors
4 #
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
8 #
9 #     http://www.apache.org/licenses/LICENSE-2.0
10 #
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
16 #
17
18 set -e
19
20 CHIP_ROOT="$(dirname "$0")"
21
22 _chip_red() {
23     echo -e "\033[0;31m$*\033[0m"
24 }
25
26 _chip_yellow() {
27     echo -e "\033[0;33m$*\033[0m"
28 }
29
30 _chip_banner() {
31     _chip_yellow '.--------------------------------'
32     _chip_yellow "-- $1"
33     _chip_yellow "'--------------------------------"
34 }
35
36 _chip_banner "Environment bringup"
37
38 git -C "$CHIP_ROOT" submodule update --init
39
40 # TODO: Fix pigweed to bootstrap if necessary in activate.sh.
41 echo
42 echo "NB: If this fails run \"source scripts/bootstrap.sh\""
43
44 source "$CHIP_ROOT/scripts/activate.sh"
45
46 _chip_banner "Instructions"
47
48 echo
49 echo 'To activate existing build environment in your shell, run (do this first):'
50 echo source "$CHIP_ROOT/scripts/activate.sh"
51
52 echo
53 echo 'To re-create the build environment from scratch, run:'
54 echo source "$CHIP_ROOT/scripts/bootstrap.sh"
55
56 echo
57 echo 'To build a debug build:'
58 echo gn gen "$CHIP_ROOT/out/debug" --args=\''target_os="all"'"$extra_args"\'
59 echo ninja -C "$CHIP_ROOT/out/debug"
60
61 echo
62 echo 'To run tests (idempotent):'
63 echo ninja -C "$CHIP_ROOT/out/debug" check
64
65 echo
66 echo 'To build & test an optimized build:'
67 echo gn gen "$CHIP_ROOT/out/release" --args=\''target_os="all" is_debug=false'"$extra_args"\'
68 echo ninja -C "$CHIP_ROOT/out/release" check
69
70 echo
71 echo 'To build a custom build (for help run "gn args --list out/debug")'
72 echo gn args "$CHIP_ROOT/out/custom"
73 echo ninja -C "$CHIP_ROOT/out/custom"
74
75 extra_args=""
76 user_args=""
77 ninja_args=()
78
79 while getopts :d:j:k:l:nt:vw: opt; do
80     case "$opt" in
81         [nv])
82             ninja_args+=("-$opt")
83             ;;
84         [djkltw])
85             ninja_args+=("-$opt" "$OPTARG")
86             ;;
87         '?')
88             printf '\nError: unknown option -%s\n' "$OPTARG"
89             printf 'Usage: %s [ninja-options] [gn-args]\n' "$0"
90             exit 1
91             ;;
92     esac
93 done
94 shift $((OPTIND - 1))
95
96 for arg; do
97     case $arg in
98         enable_qpg6100_builds=true)
99             qpg6100_enabled=1
100             ;;
101         enable_efr32_builds=true)
102             efr32_enabled=1
103             ;;
104     esac
105     user_args+=" $arg"
106 done
107
108 # Android SDK setup
109 android_sdk_args=""
110
111 if [[ -d "${ANDROID_NDK_HOME}/toolchains" && -d "${ANDROID_HOME}/platforms" ]]; then
112     android_sdk_args+="android_sdk_root=\"$ANDROID_HOME\" android_ndk_root=\"$ANDROID_NDK_HOME\""
113     extra_args+=" $android_sdk_args enable_android_builds=true"
114 else
115     echo
116     echo "Hint: Set \$ANDROID_HOME and \$ANDROID_NDK_HOME to enable building for Android"
117 fi
118
119 echo
120
121 # EFR32 SDK setup
122 if [[ -z "$efr32_enabled" ]]; then
123     echo "Hint: Pass enable_efr32_builds=true to enable building for EFR32"
124 else
125     echo 'To build the EFR32 lock sample as a standalone project':
126     echo "(cd $CHIP_ROOT/examples/lock-app/efr32; gn gen out/debug; ninja -C out/debug)"
127 fi
128
129 # K32W SDK setup
130 k32w_sdk_args=""
131
132 if [[ -d "$K32W061_SDK_ROOT" ]]; then
133     k32w_sdk_args+="k32w_sdk_root=\"$K32W061_SDK_ROOT\""
134     extra_args+=" $k32w_sdk_args enable_k32w_builds=true"
135 fi
136
137 echo
138 if [[ ! -d "$K32W061_SDK_ROOT" ]]; then
139     echo "Hint: Set \$K32W061_SDK_ROOT to enable building for K32W061"
140 else
141     echo 'To build the K32W lock sample as a standalone project':
142     echo "(cd $CHIP_ROOT/examples/lock-app/k32w; gn gen out/debug --args='$k32w_sdk_args'; ninja -C out/debug)"
143 fi
144 echo
145
146 if [[ -z "$qpg6100_enabled" ]]; then
147     echo "Hint: Pass enable_qpg6100_builds=true to this script to enable building for QPG6100"
148 else
149     echo 'To build the QPG6100 lock sample as a standalone project:'
150     echo "(cd $CHIP_ROOT/examples/lock-app/qpg6100; gn gen out/debug; ninja -C out/debug)"
151 fi
152
153 echo
154
155 _chip_banner "Build: GN configure"
156
157 gn --root="$CHIP_ROOT" gen --check --fail-on-unused-args "$CHIP_ROOT/out/debug" --args='target_os="all"'"$extra_args$user_args"
158 gn --root="$CHIP_ROOT" gen --check --fail-on-unused-args "$CHIP_ROOT/out/release" --args='target_os="all" is_debug=false'"$extra_arg$user_args"
159
160 _chip_banner "Build: Ninja build"
161
162 time ninja -C "$CHIP_ROOT/out/debug" "${ninja_args[@]}" all check