Fix for x86_64 build fail
[platform/upstream/connectedhomeip.git] / src / darwin / Framework / chip_xcode_build_connector.sh
1 #!/usr/bin/env bash
2
3 #
4 #    Copyright (c) 2020 Project CHIP Authors
5 #    All rights reserved.
6 #
7 #    Licensed under the Apache License, Version 2.0 (the "License");
8 #    you may not use this file except in compliance with the License.
9 #    You may obtain a copy of the License at
10 #
11 #        http://www.apache.org/licenses/LICENSE-2.0
12 #
13 #    Unless required by applicable law or agreed to in writing, software
14 #    distributed under the License is distributed on an "AS IS" BASIS,
15 #    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 #    See the License for the specific language governing permissions and
17 #    limitations under the License.
18 #
19
20 # This script connects Xcode's "Run Script" build phase to a build of CHIP for Apple's environments.
21 #
22 # Conventions used in this script:
23 #  * Variables in upper case supplied by Xcode (or other executor), are exported to subprocesses, or
24 #      are upper-case elsewhere in CHIP scripts (e.g. CHIP_ROOT is only used locally,
25 #      but is all uppper). Variables defined herein and used locally are lower-case
26 #
27
28 here=$(cd "${0%/*}" && pwd)
29 me=${0##*/}
30
31 CHIP_ROOT=$(cd "$here/../../.." && pwd)
32
33 die() {
34     echo "$me: *** ERROR: $*"
35     exit 1
36 }
37
38 # lotsa debug output :-)
39 set -ex
40
41 # helpful debugging, save off environment that Xcode gives us, can source it to
42 #  retry/repro failures from a bash terminal
43 mkdir -p "$TEMP_DIR"
44 export >"$TEMP_DIR/env.sh"
45
46 declare -a defines=()
47 # lots of environment variables passed by Xcode to this script
48 read -r -a defines <<<"$GCC_PREPROCESSOR_DEFINITIONS"
49
50 declare target_defines=
51 for define in "${defines[@]}"; do
52
53     # skip over those that GN does for us
54     case "$define" in
55         CHIP_DEVICE_LAYER*)
56             continue
57             ;;
58         CHIP_*_CONFIG_INCLUDE)
59             continue
60             ;;
61         CHIP_SYSTEM_CONFIG_*)
62             continue
63             ;;
64         CONFIG_NETWORK_LAYER*)
65             continue
66             ;;
67         CHIP_CRYPTO_*)
68             continue
69             ;;
70     esac
71     target_defines+=,\"${define//\"/\\\"}\"
72 done
73 target_defines=[${target_defines:1}]
74
75 declare target_cflags='"-target","'"$PLATFORM_PREFERRED_ARCH"'-'"$LLVM_TARGET_TRIPLE_VENDOR"'-'"$LLVM_TARGET_TRIPLE_OS_VERSION"'"'
76
77 read -r -a archs <<<"$ARCHS"
78
79 for arch in "${archs[@]}"; do
80     target_cflags+=',"-arch","'"$arch"'"'
81 done
82
83 [[ $ENABLE_BITCODE == YES ]] && {
84     target_cflags+=',"-flto"'
85 }
86
87 declare -a args=(
88     'default_configs_cosmetic=[]' # suppress colorization
89     'chip_crypto="mbedtls"'
90     'chip_logging_style="darwin"'
91     'chip_build_tools=false'
92     'chip_build_tests=false'
93     'chip_ble_project_config_include=""'
94     'chip_device_project_config_include=""'
95     'chip_inet_project_config_include=""'
96     'chip_system_project_config_include=""'
97     'target_cpu="'"$PLATFORM_PREFERRED_ARCH"'"'
98     'target_defines='"$target_defines"
99     'target_cflags=['"$target_cflags"']'
100 )
101
102 [[ $CONFIGURATION != Debug* ]] && args+='is_debug=true'
103
104 [[ $PLATFORM_FAMILY_NAME != macOS ]] && {
105     args+=(
106         'target_os="ios"'
107         'import("//config/ios/args.gni")'
108     )
109 }
110
111 [[ $PLATFORM_FAMILY_NAME == macOS ]] && {
112     args+=(
113         'target_os="mac"'
114         'import("//config/standalone/args.gni")'
115         'chip_project_config_include_dirs=["'"$CHIP_ROOT"'/config/standalone"]'
116     )
117 }
118
119 # search current (or $2) and its parent directories until
120 #  a name match is found, which is output on stdout
121 find_in_ancestors() {
122     declare to_find="${1}"
123     declare dir="${2:-$(pwd)}"
124
125     while [[ ! -e ${dir}/${to_find} && -n ${dir} ]]; do
126         dir=${dir%/*}
127     done
128
129     if [[ ! -e ${dir}/${to_find} ]]; then
130         printf 'error: find_in_ancestors: %s not found\n' "$to_find" >&2
131         return 1
132     fi
133     printf '%s\n' "$dir/$to_find"
134 }
135
136 # actual build stuff
137 (
138     cd "$CHIP_ROOT" # pushd and popd because we need the env vars from activate
139
140     if ENV=$(find_in_ancestors chip_xcode_build_connector_env.sh 2>/dev/null); then
141         . "$ENV"
142     fi
143
144     # there are environments where these bits are unwanted, unnecessary, or impossible
145     [[ -n $CHIP_NO_SUBMODULES ]] || git submodule update --init
146     if [[ -z $CHIP_NO_ACTIVATE ]]; then
147         # first run bootstrap/activate in an external env to build everything
148         env -i PW_ENVSETUP_NO_BANNER=1 PW_ENVSETUP_QUIET=1 bash -c '. scripts/activate.sh'
149         set +ex
150         # now source activate for env vars
151         PW_ENVSETUP_NO_BANNER=1 PW_ENVSETUP_QUIET=1 . scripts/activate.sh
152         set -ex
153     fi
154
155     # put build intermediates in TEMP_DIR
156     cd "$TEMP_DIR"
157
158     # gnerate and build
159     gn --root="$CHIP_ROOT" gen --check out --args="${args[*]}"
160     ninja -v -C out
161 )