Skip to content

Commit f7db85e

Browse files
Darwin: Disable static destructors in Matter.framework (project-chip#32745)
* Darwin: Disable static destructors in Matter.framework Also propagate C/C++ flags from Xcode into the build. * Use WARNING_CFLAGS in CI * Remove unused constants to avoid warnings
1 parent c047b59 commit f7db85e

File tree

4 files changed

+59
-42
lines changed

4 files changed

+59
-42
lines changed

.github/workflows/darwin.yaml

+5-5
Original file line numberDiff line numberDiff line change
@@ -36,15 +36,15 @@ jobs:
3636
matrix:
3737
options: # We don't need a full matrix
3838
- flavor: macos-release-availability
39-
arguments: -sdk macosx -configuration Release OTHER_CFLAGS='${inherited} -Werror -Wconversion -Wno-unguarded-availability-new'
39+
arguments: -sdk macosx -configuration Release WARNING_CFLAGS='${inherited} -Werror -Wconversion -Wno-unguarded-availability-new'
4040
- flavor: ios-release
41-
arguments: -sdk iphoneos -configuration Release OTHER_CFLAGS='${inherited} -Werror -Wconversion' GCC_PREPROCESSOR_DEFINITIONS='${inherited} MTR_NO_AVAILABILITY=1'
41+
arguments: -sdk iphoneos -configuration Release WARNING_CFLAGS='${inherited} -Werror -Wconversion' GCC_PREPROCESSOR_DEFINITIONS='${inherited} MTR_NO_AVAILABILITY=1'
4242
- flavor: ios-debug
43-
arguments: -sdk iphoneos -configuration Debug OTHER_CFLAGS='${inherited} -Werror -Wconversion' GCC_PREPROCESSOR_DEFINITIONS='${inherited} MTR_NO_AVAILABILITY=1'
43+
arguments: -sdk iphoneos -configuration Debug WARNING_CFLAGS='${inherited} -Werror -Wconversion' GCC_PREPROCESSOR_DEFINITIONS='${inherited} MTR_NO_AVAILABILITY=1'
4444
- flavor: tvos-debug
45-
arguments: -sdk appletvos -configuration Debug OTHER_CFLAGS='${inherited} -Werror -Wconversion' GCC_PREPROCESSOR_DEFINITIONS='${inherited} MTR_NO_AVAILABILITY=1'
45+
arguments: -sdk appletvos -configuration Debug WARNING_CFLAGS='${inherited} -Werror -Wconversion' GCC_PREPROCESSOR_DEFINITIONS='${inherited} MTR_NO_AVAILABILITY=1'
4646
- flavor: watchos-debug
47-
arguments: -sdk watchos -configuration Debug OTHER_CFLAGS='${inherited} -Werror -Wconversion' GCC_PREPROCESSOR_DEFINITIONS='${inherited} MTR_NO_AVAILABILITY=1'
47+
arguments: -sdk watchos -configuration Debug WARNING_CFLAGS='${inherited} -Werror -Wconversion' GCC_PREPROCESSOR_DEFINITIONS='${inherited} MTR_NO_AVAILABILITY=1'
4848
steps:
4949
- name: Checkout
5050
uses: actions/checkout@v4

src/darwin/Framework/CHIP/MTRDeviceController.mm

-3
Original file line numberDiff line numberDiff line change
@@ -88,13 +88,10 @@
8888
static NSString * const kErrorPairingInit = @"Init failure while creating a pairing delegate";
8989
static NSString * const kErrorPartialDacVerifierInit = @"Init failure while creating a partial DAC verifier";
9090
static NSString * const kErrorPairDevice = @"Failure while pairing the device";
91-
static NSString * const kErrorUnpairDevice = @"Failure while unpairing the device";
9291
static NSString * const kErrorStopPairing = @"Failure while trying to stop the pairing process";
9392
static NSString * const kErrorPreWarmCommissioning = @"Failure while trying to pre-warm the commissioning process";
9493
static NSString * const kErrorOpenPairingWindow = @"Open Pairing Window failed";
95-
static NSString * const kErrorGetPairedDevice = @"Failure while trying to retrieve a paired device";
9694
static NSString * const kErrorNotRunning = @"Controller is not running. Call startup first.";
97-
static NSString * const kInfoStackShutdown = @"Shutting down the Matter Stack";
9895
static NSString * const kErrorSetupCodeGen = @"Generating Manual Pairing Code failed";
9996
static NSString * const kErrorGenerateNOC = @"Generating operational certificate failed";
10097
static NSString * const kErrorKeyAllocation = @"Generating new operational key failed";

src/darwin/Framework/Matter.xcodeproj/project.pbxproj

+8
Original file line numberDiff line numberDiff line change
@@ -2263,6 +2263,10 @@
22632263
IPHONEOS_DEPLOYMENT_TARGET = 14.0;
22642264
LIBRARY_SEARCH_PATHS = "$(TEMP_DIR)/out/lib";
22652265
OTHER_CFLAGS = "-fmacro-prefix-map=$(SRCROOT)/CHIP/=";
2266+
OTHER_CPLUSPLUSFLAGS = (
2267+
"$(OTHER_CFLAGS)",
2268+
"-fno-c++-static-destructors",
2269+
);
22662270
OTHER_LDFLAGS = "";
22672271
"OTHER_LDFLAGS[sdk=*]" = (
22682272
"-framework",
@@ -2429,6 +2433,10 @@
24292433
IPHONEOS_DEPLOYMENT_TARGET = 14.0;
24302434
LIBRARY_SEARCH_PATHS = "$(TEMP_DIR)/out/lib";
24312435
OTHER_CFLAGS = "-fmacro-prefix-map=$(SRCROOT)/CHIP/=";
2436+
OTHER_CPLUSPLUSFLAGS = (
2437+
"$(OTHER_CFLAGS)",
2438+
"-fno-c++-static-destructors",
2439+
);
24322440
OTHER_LDFLAGS = "";
24332441
"OTHER_LDFLAGS[sdk=*]" = (
24342442
"-framework",

src/darwin/Framework/chip_xcode_build_connector.sh

+46-34
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#!/usr/bin/env bash
1+
#!/bin/bash -e
22

33
#
44
# Copyright (c) 2020 Project CHIP Authors
@@ -27,37 +27,51 @@
2727

2828
CHIP_ROOT=$(cd "$(dirname "$0")/../../.." && pwd)
2929

30-
# lotsa debug output :-)
31-
set -ex
30+
function format_gn_str() {
31+
local val="$1"
32+
val="${val//\\/\\\\}" # escape '\'
33+
val="${val//\$/\\\$}" # escape '$'
34+
echo -n "\"${val//\"/\\\"}\"" # escape '"'
35+
}
36+
37+
function format_gn_list() {
38+
local val sep=
39+
echo -n "["
40+
for val in "$@"; do
41+
echo -n "$sep"
42+
format_gn_str "$val"
43+
sep=", "
44+
done
45+
echo "]"
46+
}
3247

3348
# We only have work to do for the `installapi` and `build` phases
3449
[[ "$ACTION" == installhdrs ]] && exit 0
3550

36-
# helpful debugging, save off environment that Xcode gives us, can source it to
37-
# retry/repro failures from a bash terminal
3851
mkdir -p "$TEMP_DIR"
39-
export >"$TEMP_DIR/env.sh"
4052

41-
declare -a defines=()
42-
# lots of environment variables passed by Xcode to this script
43-
read -r -a defines <<<"$GCC_PREPROCESSOR_DEFINITIONS"
44-
45-
declare target_defines=
46-
for define in "${defines[@]}"; do
53+
# For debugging, save off environment that Xcode gives us, can source it to
54+
# retry/repro failures from a bash terminal
55+
#export >"$TEMP_DIR/env.sh"
56+
#set -x
4757

58+
# Forward defines from Xcode (GCC_PREPROCESSOR_DEFINITIONS)
59+
declare -a target_defines=()
60+
read -r -a xcode_defines <<<"$GCC_PREPROCESSOR_DEFINITIONS"
61+
for define in "${xcode_defines[@]}"; do
4862
# skip over those that GN does for us
4963
case "$define" in
50-
CHIP_HAVE_CONFIG_H)
51-
continue
52-
;;
64+
CHIP_HAVE_CONFIG_H) continue ;;
5365
esac
54-
target_defines+=,\"${define//\"/\\\"}\"
66+
target_defines+=("$define")
5567
done
56-
[[ $CHIP_ENABLE_ENCODING_SENTINEL_ENUM_VALUES == YES ]] && {
57-
target_defines+=,\"CHIP_CONFIG_IM_ENABLE_ENCODING_SENTINEL_ENUM_VALUES=1\"
58-
}
59-
target_defines=[${target_defines:1}]
6068

69+
# Forward C/C++ flags (OTHER_C*FLAGS)
70+
declare -a target_cflags=()
71+
read -r -a target_cflags_c <<<"$OTHER_CFLAGS"
72+
read -r -a target_cflags_cc <<<"$OTHER_CPLUSPLUSFLAGS"
73+
74+
# Handle target OS and arch
6175
declare target_arch=
6276
declare target_cpu=
6377
declare target_cflags=
@@ -72,18 +86,12 @@ for arch in "${archs[@]}"; do
7286
*) target_cpu="$arch" ;;
7387
esac
7488
fi
75-
if [ -n "$target_cflags" ]; then
76-
target_cflags+=','
77-
fi
78-
target_cflags+='"-arch","'"$arch"'"'
89+
target_cflags+=(-arch "$arch")
7990
done
8091

81-
[[ $ENABLE_BITCODE == YES ]] && {
82-
if [ -n "$target_cflags" ]; then
83-
target_cflags+=','
84-
fi
85-
target_cflags+='"-flto"'
86-
}
92+
# Translate other options
93+
[[ $CHIP_ENABLE_ENCODING_SENTINEL_ENUM_VALUES == YES ]] && target_defines+=("CHIP_CONFIG_IM_ENABLE_ENCODING_SENTINEL_ENUM_VALUES=1")
94+
[[ $ENABLE_BITCODE == YES ]] && target_cflags+=("-flto")
8795

8896
declare -a args=(
8997
'default_configs_cosmetic=[]' # suppress colorization
@@ -98,10 +106,12 @@ declare -a args=(
98106
'chip_disable_platform_kvs=true'
99107
'enable_fuzz_test_targets=false'
100108
"target_cpu=\"$target_cpu\""
101-
"target_defines=$target_defines"
102-
"target_cflags=[$target_cflags]"
103109
"mac_target_arch=\"$target_arch\""
104110
"mac_deployment_target=\"$LLVM_TARGET_TRIPLE_OS_VERSION$LLVM_TARGET_TRIPLE_SUFFIX\""
111+
"target_defines=$(format_gn_list "${target_defines[@]}")"
112+
"target_cflags=$(format_gn_list "${target_cflags[@]}")"
113+
"target_cflags_c=$(format_gn_list "${target_cflags_c[@]}")"
114+
"target_cflags_cc=$(format_gn_list "${target_cflags_cc[@]}")"
105115
)
106116

107117
case "$CONFIGURATION" in
@@ -197,16 +207,18 @@ find_in_ancestors() {
197207
if [[ -z $CHIP_NO_ACTIVATE ]]; then
198208
# first run bootstrap/activate in an external env to build everything
199209
env -i PW_ENVSETUP_NO_BANNER=1 PW_ENVSETUP_QUIET=1 bash -c '. scripts/activate.sh'
200-
set +ex
201210
# now source activate for env vars
211+
opts="$(set +o)"
212+
set +ex
202213
PW_ENVSETUP_NO_BANNER=1 PW_ENVSETUP_QUIET=1 . scripts/activate.sh
203-
set -ex
214+
eval "$opts"
204215
fi
205216

206217
# put build intermediates in TEMP_DIR
207218
cd "$TEMP_DIR"
208219

209220
# generate and build
221+
set -x
210222
gn --root="$CHIP_ROOT" gen --check out --args="${args[*]}"
211223
exec ninja -v -C out
212224
}

0 commit comments

Comments
 (0)