From fab44f903fecf3f33689612bfa2ee69711d216e8 Mon Sep 17 00:00:00 2001
From: Karsten Sperling <ksperling@apple.com>
Date: Wed, 27 Mar 2024 20:28:40 +1300
Subject: [PATCH 1/3] Darwin: Disable static destructors in Matter.framework

Also propagate C/C++ flags from Xcode into the build.
---
 .../Matter.xcodeproj/project.pbxproj          |  8 ++
 .../Framework/chip_xcode_build_connector.sh   | 80 +++++++++++--------
 2 files changed, 54 insertions(+), 34 deletions(-)

diff --git a/src/darwin/Framework/Matter.xcodeproj/project.pbxproj b/src/darwin/Framework/Matter.xcodeproj/project.pbxproj
index 74ab025151ef5a..a156bb44b1aecd 100644
--- a/src/darwin/Framework/Matter.xcodeproj/project.pbxproj
+++ b/src/darwin/Framework/Matter.xcodeproj/project.pbxproj
@@ -2263,6 +2263,10 @@
 				IPHONEOS_DEPLOYMENT_TARGET = 14.0;
 				LIBRARY_SEARCH_PATHS = "$(TEMP_DIR)/out/lib";
 				OTHER_CFLAGS = "-fmacro-prefix-map=$(SRCROOT)/CHIP/=";
+				OTHER_CPLUSPLUSFLAGS = (
+					"$(OTHER_CFLAGS)",
+					"-fno-c++-static-destructors",
+				);
 				OTHER_LDFLAGS = "";
 				"OTHER_LDFLAGS[sdk=*]" = (
 					"-framework",
@@ -2429,6 +2433,10 @@
 				IPHONEOS_DEPLOYMENT_TARGET = 14.0;
 				LIBRARY_SEARCH_PATHS = "$(TEMP_DIR)/out/lib";
 				OTHER_CFLAGS = "-fmacro-prefix-map=$(SRCROOT)/CHIP/=";
+				OTHER_CPLUSPLUSFLAGS = (
+					"$(OTHER_CFLAGS)",
+					"-fno-c++-static-destructors",
+				);
 				OTHER_LDFLAGS = "";
 				"OTHER_LDFLAGS[sdk=*]" = (
 					"-framework",
diff --git a/src/darwin/Framework/chip_xcode_build_connector.sh b/src/darwin/Framework/chip_xcode_build_connector.sh
index a6d9d1da0b146c..237389a8cef01f 100755
--- a/src/darwin/Framework/chip_xcode_build_connector.sh
+++ b/src/darwin/Framework/chip_xcode_build_connector.sh
@@ -1,4 +1,4 @@
-#!/usr/bin/env bash
+#!/bin/bash -e
 
 #
 #    Copyright (c) 2020 Project CHIP Authors
@@ -27,37 +27,51 @@
 
 CHIP_ROOT=$(cd "$(dirname "$0")/../../.." && pwd)
 
-# lotsa debug output :-)
-set -ex
+function format_gn_str() {
+    local val="$1"
+    val="${val//\\/\\\\}"         # escape '\'
+    val="${val//\$/\\\$}"         # escape '$'
+    echo -n "\"${val//\"/\\\"}\"" # escape '"'
+}
+
+function format_gn_list() {
+    local val sep=
+    echo -n "["
+    for val in "$@"; do
+        echo -n "$sep"
+        format_gn_str "$val"
+        sep=", "
+    done
+    echo "]"
+}
 
 # We only have work to do for the `installapi` and `build` phases
 [[ "$ACTION" == installhdrs ]] && exit 0
 
-# 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
+# For debugging, save off environment that Xcode gives us, can source it to
+# retry/repro failures from a bash terminal
+#export >"$TEMP_DIR/env.sh"
+#set -x
 
+# Forward defines from Xcode (GCC_PREPROCESSOR_DEFINITIONS)
+declare -a target_defines=()
+read -r -a xcode_defines <<<"$GCC_PREPROCESSOR_DEFINITIONS"
+for define in "${xcode_defines[@]}"; do
     # skip over those that GN does for us
     case "$define" in
-        CHIP_HAVE_CONFIG_H)
-            continue
-            ;;
+        CHIP_HAVE_CONFIG_H) continue ;;
     esac
-    target_defines+=,\"${define//\"/\\\"}\"
+    target_defines+=("$define")
 done
-[[ $CHIP_ENABLE_ENCODING_SENTINEL_ENUM_VALUES == YES ]] && {
-    target_defines+=,\"CHIP_CONFIG_IM_ENABLE_ENCODING_SENTINEL_ENUM_VALUES=1\"
-}
-target_defines=[${target_defines:1}]
 
+# Forward C/C++ flags (OTHER_C*FLAGS)
+declare -a target_cflags=()
+read -r -a target_cflags_c <<<"$OTHER_CFLAGS"
+read -r -a target_cflags_cc <<<"$OTHER_CPLUSPLUSFLAGS"
+
+# Handle target OS and arch
 declare target_arch=
 declare target_cpu=
 declare target_cflags=
@@ -72,18 +86,12 @@ for arch in "${archs[@]}"; do
             *) target_cpu="$arch" ;;
         esac
     fi
-    if [ -n "$target_cflags" ]; then
-        target_cflags+=','
-    fi
-    target_cflags+='"-arch","'"$arch"'"'
+    target_cflags+=(-arch "$arch")
 done
 
-[[ $ENABLE_BITCODE == YES ]] && {
-    if [ -n "$target_cflags" ]; then
-        target_cflags+=','
-    fi
-    target_cflags+='"-flto"'
-}
+# Translate other options
+[[ $CHIP_ENABLE_ENCODING_SENTINEL_ENUM_VALUES == YES ]] && target_defines+=("CHIP_CONFIG_IM_ENABLE_ENCODING_SENTINEL_ENUM_VALUES=1")
+[[ $ENABLE_BITCODE == YES ]] && target_cflags+=("-flto")
 
 declare -a args=(
     'default_configs_cosmetic=[]' # suppress colorization
@@ -98,10 +106,12 @@ declare -a args=(
     'chip_disable_platform_kvs=true'
     'enable_fuzz_test_targets=false'
     "target_cpu=\"$target_cpu\""
-    "target_defines=$target_defines"
-    "target_cflags=[$target_cflags]"
     "mac_target_arch=\"$target_arch\""
     "mac_deployment_target=\"$LLVM_TARGET_TRIPLE_OS_VERSION$LLVM_TARGET_TRIPLE_SUFFIX\""
+    "target_defines=$(format_gn_list "${target_defines[@]}")"
+    "target_cflags=$(format_gn_list "${target_cflags[@]}")"
+    "target_cflags_c=$(format_gn_list "${target_cflags_c[@]}")"
+    "target_cflags_cc=$(format_gn_list "${target_cflags_cc[@]}")"
 )
 
 case "$CONFIGURATION" in
@@ -197,16 +207,18 @@ find_in_ancestors() {
     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
+        opts="$(set +o)"
+        set +ex
         PW_ENVSETUP_NO_BANNER=1 PW_ENVSETUP_QUIET=1 . scripts/activate.sh
-        set -ex
+        eval "$opts"
     fi
 
     # put build intermediates in TEMP_DIR
     cd "$TEMP_DIR"
 
     # generate and build
+    set -x
     gn --root="$CHIP_ROOT" gen --check out --args="${args[*]}"
     exec ninja -v -C out
 }

From e93db1c4185c2631ea320b7b8801c8a030025aae Mon Sep 17 00:00:00 2001
From: Karsten Sperling <ksperling@apple.com>
Date: Wed, 27 Mar 2024 21:55:48 +1300
Subject: [PATCH 2/3] Use WARNING_CFLAGS in CI

---
 .github/workflows/darwin.yaml | 10 +++++-----
 1 file changed, 5 insertions(+), 5 deletions(-)

diff --git a/.github/workflows/darwin.yaml b/.github/workflows/darwin.yaml
index 1555032827dd28..a60b3d0d0498aa 100644
--- a/.github/workflows/darwin.yaml
+++ b/.github/workflows/darwin.yaml
@@ -36,15 +36,15 @@ jobs:
             matrix:
                 options: # We don't need a full matrix
                     - flavor: macos-release-availability
-                      arguments: -sdk macosx -configuration Release   OTHER_CFLAGS='${inherited} -Werror -Wconversion -Wno-unguarded-availability-new'
+                      arguments: -sdk macosx -configuration Release   WARNING_CFLAGS='${inherited} -Werror -Wconversion -Wno-unguarded-availability-new'
                     - flavor: ios-release
-                      arguments: -sdk iphoneos -configuration Release OTHER_CFLAGS='${inherited} -Werror -Wconversion' GCC_PREPROCESSOR_DEFINITIONS='${inherited} MTR_NO_AVAILABILITY=1'
+                      arguments: -sdk iphoneos -configuration Release WARNING_CFLAGS='${inherited} -Werror -Wconversion' GCC_PREPROCESSOR_DEFINITIONS='${inherited} MTR_NO_AVAILABILITY=1'
                     - flavor: ios-debug
-                      arguments: -sdk iphoneos -configuration Debug   OTHER_CFLAGS='${inherited} -Werror -Wconversion' GCC_PREPROCESSOR_DEFINITIONS='${inherited} MTR_NO_AVAILABILITY=1'
+                      arguments: -sdk iphoneos -configuration Debug   WARNING_CFLAGS='${inherited} -Werror -Wconversion' GCC_PREPROCESSOR_DEFINITIONS='${inherited} MTR_NO_AVAILABILITY=1'
                     - flavor: tvos-debug
-                      arguments: -sdk appletvos -configuration Debug  OTHER_CFLAGS='${inherited} -Werror -Wconversion' GCC_PREPROCESSOR_DEFINITIONS='${inherited} MTR_NO_AVAILABILITY=1'
+                      arguments: -sdk appletvos -configuration Debug  WARNING_CFLAGS='${inherited} -Werror -Wconversion' GCC_PREPROCESSOR_DEFINITIONS='${inherited} MTR_NO_AVAILABILITY=1'
                     - flavor: watchos-debug
-                      arguments: -sdk watchos -configuration Debug    OTHER_CFLAGS='${inherited} -Werror -Wconversion' GCC_PREPROCESSOR_DEFINITIONS='${inherited} MTR_NO_AVAILABILITY=1'
+                      arguments: -sdk watchos -configuration Debug    WARNING_CFLAGS='${inherited} -Werror -Wconversion' GCC_PREPROCESSOR_DEFINITIONS='${inherited} MTR_NO_AVAILABILITY=1'
         steps:
             - name: Checkout
               uses: actions/checkout@v4

From 1c397d25f70e3b0d00f6d545cca3bfed6887eb89 Mon Sep 17 00:00:00 2001
From: Karsten Sperling <ksperling@apple.com>
Date: Wed, 27 Mar 2024 23:05:56 +1300
Subject: [PATCH 3/3] Remove unused constants to avoid warnings

---
 src/darwin/Framework/CHIP/MTRDeviceController.mm | 3 ---
 1 file changed, 3 deletions(-)

diff --git a/src/darwin/Framework/CHIP/MTRDeviceController.mm b/src/darwin/Framework/CHIP/MTRDeviceController.mm
index d51b5cd7712b5c..1e52592f94d9ce 100644
--- a/src/darwin/Framework/CHIP/MTRDeviceController.mm
+++ b/src/darwin/Framework/CHIP/MTRDeviceController.mm
@@ -88,13 +88,10 @@
 static NSString * const kErrorPairingInit = @"Init failure while creating a pairing delegate";
 static NSString * const kErrorPartialDacVerifierInit = @"Init failure while creating a partial DAC verifier";
 static NSString * const kErrorPairDevice = @"Failure while pairing the device";
-static NSString * const kErrorUnpairDevice = @"Failure while unpairing the device";
 static NSString * const kErrorStopPairing = @"Failure while trying to stop the pairing process";
 static NSString * const kErrorPreWarmCommissioning = @"Failure while trying to pre-warm the commissioning process";
 static NSString * const kErrorOpenPairingWindow = @"Open Pairing Window failed";
-static NSString * const kErrorGetPairedDevice = @"Failure while trying to retrieve a paired device";
 static NSString * const kErrorNotRunning = @"Controller is not running. Call startup first.";
-static NSString * const kInfoStackShutdown = @"Shutting down the Matter Stack";
 static NSString * const kErrorSetupCodeGen = @"Generating Manual Pairing Code failed";
 static NSString * const kErrorGenerateNOC = @"Generating operational certificate failed";
 static NSString * const kErrorKeyAllocation = @"Generating new operational key failed";