blob: 9c2481008af69aec0fb5b62611dfa1349c188cb1 [file] [log] [blame]
#!/usr/bin/env bash
#
# Copyright (c) 2020 Project CHIP Authors
# All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# This script connects Xcode's "Run Script" build phase to a build of CHIP for Apple's environments.
#
# Conventions used in this script:
# * Variables in upper case supplied by Xcode (or other executor), are exported to subprocesses, or
# are upper-case elsewhere in CHIP scripts (e.g. CHIP_ROOT is only used locally,
# but is all uppper). Variables defined herein and used locally are lower-case
#
here=$(cd "${0%/*}" && pwd)
me=${0##*/}
CHIP_ROOT=$(cd "$here/../../.." && pwd)
die() {
echo "$me: *** ERROR: $*"
exit 1
}
# lotsa debug output :-)
set -ex
# helpful debugging, save off environment that Xcode gives us, can source it to
# retry/repro failures from a bash terminal
mkdir -p "$TEMP_DIR"
export >"$TEMP_DIR/env.sh"
declare -a defines=()
# lots of environment variables passed by Xcode to this script
read -r -a defines <<<"$GCC_PREPROCESSOR_DEFINITIONS"
declare target_defines=
for define in "${defines[@]}"; do
# skip over those that GN does for us
case "$define" in
CHIP_HAVE_CONFIG_H)
continue
;;
esac
target_defines+=,\"${define//\"/\\\"}\"
done
target_defines=[${target_defines:1}]
declare target_cpu=
case $PLATFORM_PREFERRED_ARCH in
i386)
target_cpu=x86
;;
x86_64)
target_cpu=x64
;;
armv7)
target_cpu=arm
;;
arm64)
target_cpu=arm64
;;
*)
echo >&2
;;
esac
declare target_cflags='"-target","'"$PLATFORM_PREFERRED_ARCH"'-'"$LLVM_TARGET_TRIPLE_VENDOR"'-'"$LLVM_TARGET_TRIPLE_OS_VERSION"'"'
read -r -a archs <<<"$ARCHS"
for arch in "${archs[@]}"; do
target_cflags+=',"-arch","'"$arch"'"'
done
[[ $ENABLE_BITCODE == YES ]] && {
target_cflags+=',"-flto"'
}
declare -a args=(
'default_configs_cosmetic=[]' # suppress colorization
'chip_crypto="boringssl"'
'chip_build_tools=false'
'chip_build_tests=false'
'chip_log_message_max_size=4096' # might as well allow nice long log messages
'chip_disable_platform_kvs=true'
'target_cpu="'"$target_cpu"'"'
'target_defines='"$target_defines"
'target_cflags=['"$target_cflags"']'
)
[[ $CONFIGURATION != Debug* ]] && args+='is_debug=true'
[[ $PLATFORM_FAMILY_NAME != macOS ]] && {
args+=(
'target_os="ios"'
'import("//config/ios/args.gni")'
)
}
[[ $PLATFORM_FAMILY_NAME == macOS ]] && {
args+=(
'target_os="mac"'
)
}
# search current (or $2) and its parent directories until
# a name match is found, which is output on stdout
find_in_ancestors() {
declare to_find="${1}"
declare dir="${2:-$(pwd)}"
while [[ ! -e ${dir}/${to_find} && -n ${dir} ]]; do
dir=${dir%/*}
done
if [[ ! -e ${dir}/${to_find} ]]; then
printf 'error: find_in_ancestors: %s not found\n' "$to_find" >&2
return 1
fi
printf '%s\n' "$dir/$to_find"
}
# actual build stuff
(
cd "$CHIP_ROOT" # pushd and popd because we need the env vars from activate
if ENV=$(find_in_ancestors chip_xcode_build_connector_env.sh 2>/dev/null); then
. "$ENV"
fi
# there are environments where these bits are unwanted, unnecessary, or impossible
[[ -n $CHIP_NO_SUBMODULES ]] || scripts/checkout_submodules.py --shallow --platform darwin
if [[ -z $CHIP_NO_ACTIVATE ]]; then
# first run bootstrap/activate in an external env to build everything
env -i PW_ENVSETUP_NO_BANNER=1 PW_ENVSETUP_QUIET=1 bash -c '. scripts/activate.sh'
set +ex
# now source activate for env vars
PW_ENVSETUP_NO_BANNER=1 PW_ENVSETUP_QUIET=1 . scripts/activate.sh
set -ex
fi
# put build intermediates in TEMP_DIR
cd "$TEMP_DIR"
# gnerate and build
gn --root="$CHIP_ROOT" gen --check out --args="${args[*]}"
ninja -v -C out
)