add gn stuff for xcode iOS/MacOS (#2157)

* add gn stuff for ios

* restyled

* rebase, use target_default config for cflags, cxxflags, defines, ldflags instead of getenv()

* chugga

* sheesh

* restyled

* re-restyled

* fixup where we expect to find encoder.h

* restyle

* move app stuff around more

* restyle

* use <app/chip-zcl...> everywhere

* restyle
diff --git a/BUILD.gn b/BUILD.gn
index bb862d3..6f2c8ba 100644
--- a/BUILD.gn
+++ b/BUILD.gn
@@ -43,6 +43,7 @@
       "${chip_root}/src/lib/shell",
       "${chip_root}/src/lib/support",
       "${chip_root}/src/lwip:all",
+      "${chip_root}/src/setup_payload",
       "${chip_root}/src/system",
       "${chip_root}/src/transport",
       "${mbedtls_root}:mbedtls",
diff --git a/config/ios/args.gni b/config/ios/args.gni
new file mode 100644
index 0000000..9a879fd
--- /dev/null
+++ b/config/ios/args.gni
@@ -0,0 +1,20 @@
+# Copyright (c) 2020 Project CHIP Authors
+#
+# 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.
+
+import("//build_overrides/chip.gni")
+
+chip_system_config_clock = "gettimeofday"
+chip_device_platform = "darwin"
+
+chip_project_config_include_dirs = [ "${chip_root}/config/ios" ]
diff --git a/configure.ac b/configure.ac
index a91c801..0545565 100644
--- a/configure.ac
+++ b/configure.ac
@@ -2165,7 +2165,7 @@
 enable_chipwifi_wpa="no"
 
 # At this time, enable this only for linux, and only if the GIO package are installed. Currently, WiFi and Thread
-# are depending on the gdbus-internal configured by internal BLuez package. 
+# are depending on the gdbus-internal configured by internal BLuez package.
 # TODO: Refacrot BLuez to use common gdbus lib from GIO.
 
 if test "${with_device_layer}" = "linux" && test "${CHIP_ENABLE_GIO}" = 1 && test "${CONFIG_BLE_PLATFORM_BLUEZ}" = 1; then
@@ -2474,7 +2474,6 @@
 src/Makefile
 src/include/Makefile
 src/app/Makefile
-src/app/chip-zcl-zpro/Makefile
 src/app/clusters/Makefile
 src/app/util/Makefile
 src/ble/Makefile
diff --git a/examples/chip-tool/main.cpp b/examples/chip-tool/main.cpp
index 8a30416..164ad14 100644
--- a/examples/chip-tool/main.cpp
+++ b/examples/chip-tool/main.cpp
@@ -40,7 +40,7 @@
 
 #include <controller/CHIPDeviceController.h>
 
-#include "chip-zcl/chip-zcl-zpro-codec.h"
+#include <app/chip-zcl-zpro-codec.h>
 
 // Delay, in seconds, between sends for the echo case.
 #define SEND_DELAY 5
diff --git a/examples/chip-tool/server.cpp b/examples/chip-tool/server.cpp
index 2131a2a..398e794 100644
--- a/examples/chip-tool/server.cpp
+++ b/examples/chip-tool/server.cpp
@@ -6,11 +6,11 @@
 #include <cassert>
 
 #include "attribute-storage.h"
-#include "chip-zcl/chip-zcl-zpro-codec.h"
 #include "gen/attribute-id.h"
 #include "gen/cluster-id.h"
 #include "gen/znet-bookkeeping.h"
 #include "util.h"
+#include <app/chip-zcl-zpro-codec.h>
 
 static const unsigned char local_private_key[] = { 0xc6, 0x1a, 0x2f, 0x89, 0x36, 0x67, 0x2b, 0x26, 0x12, 0x47, 0x4f,
                                                    0x11, 0x0e, 0x34, 0x15, 0x81, 0x81, 0x12, 0xfc, 0x36, 0xeb, 0x65,
diff --git a/examples/lock-app/nrfconnect/main/Server.cpp b/examples/lock-app/nrfconnect/main/Server.cpp
index 67a6f6a..5086630 100644
--- a/examples/lock-app/nrfconnect/main/Server.cpp
+++ b/examples/lock-app/nrfconnect/main/Server.cpp
@@ -18,9 +18,9 @@
 #include "Server.h"
 
 #include "attribute-storage.h"
-#include "chip-zcl/chip-zcl-zpro-codec.h"
 #include "gen/znet-bookkeeping.h"
 #include "util.h"
+#include <app/chip-zcl-zpro-codec.h>
 
 #include <inet/IPAddress.h>
 #include <inet/InetError.h>
diff --git a/examples/platform/nrf528xx/app/Server.cpp b/examples/platform/nrf528xx/app/Server.cpp
index b841d91..6ba9df9 100644
--- a/examples/platform/nrf528xx/app/Server.cpp
+++ b/examples/platform/nrf528xx/app/Server.cpp
@@ -48,9 +48,9 @@
 #endif
 
 #include "attribute-storage.h"
-#include "chip-zcl/chip-zcl-zpro-codec.h"
 #include "gen/znet-bookkeeping.h"
 #include "util.h"
+#include <app/chip-zcl-zpro-codec.h>
 
 using namespace ::chip;
 using namespace ::chip::Inet;
diff --git a/examples/wifi-echo/server/esp32/main/DataModelHandler.cpp b/examples/wifi-echo/server/esp32/main/DataModelHandler.cpp
index bf5b63f..3692cd6 100644
--- a/examples/wifi-echo/server/esp32/main/DataModelHandler.cpp
+++ b/examples/wifi-echo/server/esp32/main/DataModelHandler.cpp
@@ -28,11 +28,11 @@
 #include "LEDWidget.h"
 
 #include "attribute-storage.h"
-#include "chip-zcl/chip-zcl-zpro-codec.h"
 #include "gen/attribute-id.h"
 #include "gen/cluster-id.h"
 #include "gen/znet-bookkeeping.h"
 #include "util.h"
+#include <app/chip-zcl-zpro-codec.h>
 
 using namespace ::chip;
 
diff --git a/gn/build/config/BUILDCONFIG.gn b/gn/build/config/BUILDCONFIG.gn
index 5d049a0..933c522 100644
--- a/gn/build/config/BUILDCONFIG.gn
+++ b/gn/build/config/BUILDCONFIG.gn
@@ -78,6 +78,8 @@
   } else {
     assert(false, "Unsupported target_cpu: ${current_cpu}")
   }
+} else if (target_os == "ios") {
+  _default_toolchain = "//build/toolchain/ios:ios_${target_cpu}"
 } else {
   assert(false, "No toolchain specified, please specify custom_toolchain")
 }
diff --git a/gn/build/config/compiler/BUILD.gn b/gn/build/config/compiler/BUILD.gn
index 2a9bf9e..b6677b3 100644
--- a/gn/build/config/compiler/BUILD.gn
+++ b/gn/build/config/compiler/BUILD.gn
@@ -16,6 +16,7 @@
 import("//build_overrides/pigweed.gni")
 
 import("//build/config/compiler/compiler.gni")
+import("//build/config/target.gni")
 
 declare_args() {
   # Enable -Werror. This can be disabled if using a different compiler
@@ -23,7 +24,10 @@
   treat_warnings_as_errors = true
 }
 
-if (current_cpu == "arm" || current_cpu == "arm64") {
+_is_embedded_arm = current_os != "ios" && current_os != "mac_os" &&
+                   (current_cpu == "arm" || current_cpu == "arm64")
+
+if (_is_embedded_arm) {
   import("//build/config/arm.gni")
 } else if (current_cpu == "x86" || current_cpu == "x86_64") {
   import("//build/config/x86.gni")
@@ -41,7 +45,7 @@
 
 config("abi_default") {
   cflags = []
-  if (current_cpu == "arm" || current_cpu == "arm64") {
+  if (_is_embedded_arm) {
     if (arm_arch != "") {
       cflags += [ "-march=${arm_arch}" ]
     }
@@ -91,6 +95,16 @@
   ldflags = cflags
 }
 
+config("target_default") {
+  if (current_toolchain == default_toolchain) {
+    defines = target_defines
+    cflags = target_cflags
+    cflags_c = target_cflags_c
+    cflags_cc = target_cflags_cc
+    ldflags = target_ldflags
+  }
+}
+
 config("optimize_zero") {
   cflags = [ "-O0" ]
   ldflags = cflags
@@ -158,7 +172,7 @@
     ":disabled_warnings",
   ]
 
-  if (current_os != "mac") {
+  if (current_os != "mac" && current_os != "ios") {
     ldflags = [ "-Wl,--fatal-warnings" ]
   }
 }
diff --git a/gn/build/config/defaults.gni b/gn/build/config/defaults.gni
index cf04479..a5dddc9 100644
--- a/gn/build/config/defaults.gni
+++ b/gn/build/config/defaults.gni
@@ -69,6 +69,9 @@
   # Defaults fuzzing configs.
   default_configs_fuzzing = [ "//build/config/compiler:fuzzing_default" ]
 
+  # Defaults target-specific configs.
+  default_configs_target = [ "//build/config/compiler:target_default" ]
+
   # Extra default configs.
   default_configs_extra = []
 }
@@ -90,6 +93,7 @@
 default_configs += default_configs_aliasing
 default_configs += default_configs_sanitize
 default_configs += default_configs_fuzzing
+default_configs += default_configs_target
 default_configs += default_configs_extra
 
 executable_default_configs = []
diff --git a/gn/build/config/target.gni b/gn/build/config/target.gni
new file mode 100644
index 0000000..c1bf0b7
--- /dev/null
+++ b/gn/build/config/target.gni
@@ -0,0 +1,22 @@
+# Copyright (c) 2020 Project CHIP Authors
+#
+# 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.
+
+declare_args() {
+  # Global defines for target build. This only applies to the default toolchain.
+  target_defines = []
+  target_cflags = []
+  target_cflags_cc = []
+  target_cflags_c = []
+  target_ldflags = []
+}
diff --git a/gn/build/toolchain/ios/BUILD.gn b/gn/build/toolchain/ios/BUILD.gn
new file mode 100644
index 0000000..b53be0e
--- /dev/null
+++ b/gn/build/toolchain/ios/BUILD.gn
@@ -0,0 +1,32 @@
+# Copyright 2020 The Pigweed Authors
+# Copyright (c) 2020 Project CHIP Authors
+#
+# 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.
+
+import("//build/toolchain/gcc_toolchain.gni")
+
+gcc_toolchain("ios_arm") {
+  toolchain_args = {
+    current_os = "ios"
+    current_cpu = "arm"
+    is_clang = false
+  }
+}
+
+gcc_toolchain("ios_arm64") {
+  toolchain_args = {
+    current_os = "ios"
+    current_cpu = "arm64"
+    is_clang = false
+  }
+}
diff --git a/src/Makefile.am b/src/Makefile.am
index 858c0d8..688a49f 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -93,7 +93,7 @@
     inet                            \
     lib                             \
     setup_payload                   \
-		$(CONTROLLER_SUBDIRS)           \
+    $(CONTROLLER_SUBDIRS)           \
     $(SETUP_PAYLOAD_SUBDIRS)        \
     crypto                          \
     platform                        \
diff --git a/src/app/BUILD.gn b/src/app/BUILD.gn
index 397bc2d..244eda8 100644
--- a/src/app/BUILD.gn
+++ b/src/app/BUILD.gn
@@ -17,18 +17,18 @@
 config("app_config") {
   include_dirs = [
     "gen",
-    "chip-zcl",
+    "util",
     ".",
     "${target_gen_dir}/include",
   ]
 }
 
-static_library("common") {
-  output_name = "libCHIPDataModelCommon"
+static_library("app") {
+  output_name = "libCHIPDataModel"
 
   sources = [
-    "chip-zcl-zpro/command-encoder/decoder.c",
-    "chip-zcl-zpro/command-encoder/encoder.c",
+    "decoder.c",
+    "encoder.c",
   ]
 
   public_deps = [
@@ -40,20 +40,8 @@
   public_configs = [ ":app_config" ]
 }
 
-group("chip") {
-  public_deps = [ ":common" ]
-}
-
-group("mock") {
-  public_deps = [ ":common" ]
-}
-
-group("app") {
-  public_deps = [ ":chip" ]
-}
-
 copy("codec_headers") {
-  sources = [ "chip-zcl-zpro/command-encoder/chip-zcl-zpro-codec.h" ]
+  sources = [ "chip-zcl-zpro-codec.h" ]
 
-  outputs = [ "${target_gen_dir}/include/chip-zcl/{{source_file_part}}" ]
+  outputs = [ "${target_gen_dir}/include/app/{{source_file_part}}" ]
 }
diff --git a/src/app/DataModel.am b/src/app/DataModel.am
index 2049e5e..2a5ce5f 100644
--- a/src/app/DataModel.am
+++ b/src/app/DataModel.am
@@ -24,7 +24,7 @@
 #      must be anchored relative to the top build directory.
 #
 
-CHIP_BUILD_DATA_MODEL_SOURCE_FILES                                                  = \
-    @top_builddir@/src/app/chip-zcl-zpro/command-encoder/encoder.c                    \
-    @top_builddir@/src/app/chip-zcl-zpro/command-encoder/decoder.c                    \
+CHIP_BUILD_DATA_MODEL_SOURCE_FILES                    = \
+    @top_builddir@/src/app/encoder.c                    \
+    @top_builddir@/src/app/decoder.c                    \
     $(NULL)
diff --git a/src/app/Makefile.am b/src/app/Makefile.am
index c2eabba..f713439 100644
--- a/src/app/Makefile.am
+++ b/src/app/Makefile.am
@@ -26,15 +26,13 @@
 # Package (e.g. 'make dist') these subdirectories
 
 DIST_SUBDIRS = \
-    chip-zcl-zpro \
     clusters \
     util \
     $(NULL)
 
 # Build (e.g for 'make all') these subdirectories
 
-SUBDIRS                               = \
-    chip-zcl-zpro                       \
+SUBDIRS = \
     $(NULL)
 
 include DataModel.am
@@ -50,4 +48,8 @@
     $(CHIP_BUILD_DATA_MODEL_SOURCE_FILES) \
     $(NULL)
 
+libCHIPDataModel_adir                = $(includedir)/app
+
+libCHIPDataModel_a_HEADERS           = chip-zcl-zpro-codec.h
+
 include $(abs_top_nlbuild_autotools_dir)/automake/post.am
diff --git a/src/app/chip-zcl-zpro/command-encoder/chip-zcl-zpro-codec.h b/src/app/chip-zcl-zpro-codec.h
similarity index 100%
rename from src/app/chip-zcl-zpro/command-encoder/chip-zcl-zpro-codec.h
rename to src/app/chip-zcl-zpro-codec.h
diff --git a/src/app/chip-zcl-zpro/Makefile.am b/src/app/chip-zcl-zpro/Makefile.am
deleted file mode 100644
index 8cdf8b0..0000000
--- a/src/app/chip-zcl-zpro/Makefile.am
+++ /dev/null
@@ -1,31 +0,0 @@
-#
-#    Copyright (c) 2020 Project CHIP Authors
-#
-#    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.
-#
-
-#
-#    Description:
-#      This file exports the CHIP ZCL Application layer API headers
-#       for install and dist targets
-#
-
-include $(abs_top_nlbuild_autotools_dir)/automake/pre.am
-
-chip_zcldir = $(includedir)/chip-zcl
-
-chip_zcl_HEADERS                              = \
-    command-encoder/chip-zcl-zpro-codec.h       \
-    $(NULL)
-
-include $(abs_top_nlbuild_autotools_dir)/automake/post.am
diff --git a/src/app/chip-zcl-zpro/command-encoder/decoder.c b/src/app/decoder.c
similarity index 98%
rename from src/app/chip-zcl-zpro/command-encoder/decoder.c
rename to src/app/decoder.c
index 487bdd2..db55b0e 100644
--- a/src/app/chip-zcl-zpro/command-encoder/decoder.c
+++ b/src/app/decoder.c
@@ -22,7 +22,7 @@
 //                            EmberNodeId source,
 //                            InterPanHeader *interPanHeader)
 
-#include "chip-zcl-zpro-codec.h"
+#include <app/chip-zcl-zpro-codec.h>
 #include <stdio.h>
 #include <string.h>
 
diff --git a/src/app/chip-zcl-zpro/command-encoder/encoder.c b/src/app/encoder.c
similarity index 99%
rename from src/app/chip-zcl-zpro/command-encoder/encoder.c
rename to src/app/encoder.c
index 4ec7ac9..2c7173e 100644
--- a/src/app/chip-zcl-zpro/command-encoder/encoder.c
+++ b/src/app/encoder.c
@@ -15,7 +15,7 @@
  *    limitations under the License.
  */
 
-#include "chip-zcl-zpro-codec.h"
+#include <app/chip-zcl-zpro-codec.h>
 #include <assert.h>
 #include <inttypes.h>
 #include <stdio.h>
diff --git a/src/app/util/types_stub.h b/src/app/util/types_stub.h
index 8ae477f..f0650a5 100644
--- a/src/app/util/types_stub.h
+++ b/src/app/util/types_stub.h
@@ -44,7 +44,7 @@
 
 #include <string.h> // For mem* functions.
 
-#include "chip-zcl/chip-zcl-zpro-codec.h" // For EmberApsFrame
+#include <app/chip-zcl-zpro-codec.h> // For EmberApsFrame
 
 #include "gen/gen_config.h"
 
diff --git a/src/controller/java/CHIPDeviceController-JNI.cpp b/src/controller/java/CHIPDeviceController-JNI.cpp
index ef15812..67fbc26 100644
--- a/src/controller/java/CHIPDeviceController-JNI.cpp
+++ b/src/controller/java/CHIPDeviceController-JNI.cpp
@@ -31,7 +31,7 @@
 #include <support/logging/CHIPLogging.h>
 
 extern "C" {
-#include "chip-zcl-zpro-codec.h"
+#include <app/chip-zcl-zpro-codec.h>
 } // extern "C"
 
 using namespace chip;
diff --git a/src/controller/java/Makefile.am b/src/controller/java/Makefile.am
index 2a71af6..a556298 100644
--- a/src/controller/java/Makefile.am
+++ b/src/controller/java/Makefile.am
@@ -34,7 +34,7 @@
 
 libCHIPController_la_CPPFLAGS                              = \
     -I$(top_srcdir)/src                                      \
-		-I$(top_srcdir)/src/app/chip-zcl-zpro/command-encoder    \
+    -I$(top_srcdir)/src/app/util/command-encoder             \
     -I$(top_srcdir)/src/lib                                  \
     -I$(top_srcdir)/src/lib/core                             \
     -I$(top_srcdir)/src/controller                           \
@@ -84,8 +84,8 @@
 
 CHIPController_jar_JAVA_SRCS                                 = \
     chip/devicecontroller/ChipDeviceController.java            \
-		chip/devicecontroller/ChipDeviceControllerException.java   \
-		chip/devicecontroller/ChipCommandType.java                 \
+    chip/devicecontroller/ChipDeviceControllerException.java   \
+    chip/devicecontroller/ChipCommandType.java                 \
     $(NULL)
 
 CHIPController_jar_JFLAGS                    = -source 8 -target 8
diff --git a/src/darwin/Framework/CHIP.xcodeproj/project.pbxproj b/src/darwin/Framework/CHIP.xcodeproj/project.pbxproj
index 0c497db..921a4ff 100644
--- a/src/darwin/Framework/CHIP.xcodeproj/project.pbxproj
+++ b/src/darwin/Framework/CHIP.xcodeproj/project.pbxproj
@@ -7,8 +7,7 @@
 	objects = {
 
 /* Begin PBXBuildFile section */
-		1E3DE6E124AF86F50028DC40 /* libDeviceLayer.a in Frameworks */ = {isa = PBXBuildFile; fileRef = 1E3DE6E024AF86F50028DC40 /* libDeviceLayer.a */; };
-		2C4DF09E248B2C60009307CB /* libmbedtls.a in Frameworks */ = {isa = PBXBuildFile; fileRef = 2C4DF09D248B2C60009307CB /* libmbedtls.a */; };
+		2C4DF09E248B2C60009307CB /* libmbedtls.a in Frameworks */ = {isa = PBXBuildFile; fileRef = 2C4DF09D248B2C60009307CB /* libmbedtls.a */; settings = {ATTRIBUTES = (Required, ); }; };
 		515C401C2486BF43004C4DB3 /* CHIPOnOff.mm in Sources */ = {isa = PBXBuildFile; fileRef = 515C401A2486BF43004C4DB3 /* CHIPOnOff.mm */; };
 		515C401D2486BF43004C4DB3 /* CHIPOnOff.h in Headers */ = {isa = PBXBuildFile; fileRef = 515C401B2486BF43004C4DB3 /* CHIPOnOff.h */; settings = {ATTRIBUTES = (Public, ); }; };
 		991DC0842475F45400C13860 /* CHIPDeviceController.h in Headers */ = {isa = PBXBuildFile; fileRef = 991DC0822475F45400C13860 /* CHIPDeviceController.h */; settings = {ATTRIBUTES = (Public, ); }; };
@@ -26,7 +25,6 @@
 		B2E0D7B9245B0B5C003C5B48 /* CHIPSetupPayload.mm in Sources */ = {isa = PBXBuildFile; fileRef = B2E0D7B0245B0B5C003C5B48 /* CHIPSetupPayload.mm */; };
 		B2F53AF2245B0DCF0010745E /* CHIPSetupPayloadParserTests.m in Sources */ = {isa = PBXBuildFile; fileRef = B2F53AF1245B0DCF0010745E /* CHIPSetupPayloadParserTests.m */; };
 		BA09EB43247477BA00605257 /* libCHIP.a in Frameworks */ = {isa = PBXBuildFile; fileRef = BA09EB3F2474762900605257 /* libCHIP.a */; };
-		BA09EB44247477BC00605257 /* libSetupPayload.a in Frameworks */ = {isa = PBXBuildFile; fileRef = BA09EB402474762900605257 /* libSetupPayload.a */; };
 		BA107AEF2470CFBB004287EB /* chip_xcode_build_connector.sh in Resources */ = {isa = PBXBuildFile; fileRef = BA107AEE2470CFBB004287EB /* chip_xcode_build_connector.sh */; };
 		C4C222C02475A38700984173 /* CHIPLogging.mm in Sources */ = {isa = PBXBuildFile; fileRef = C4C222BF2475A38700984173 /* CHIPLogging.mm */; };
 /* End PBXBuildFile section */
@@ -42,7 +40,6 @@
 /* End PBXContainerItemProxy section */
 
 /* Begin PBXFileReference section */
-		1E3DE6E024AF86F50028DC40 /* libDeviceLayer.a */ = {isa = PBXFileReference; lastKnownFileType = archive.ar; name = libDeviceLayer.a; path = lib/libDeviceLayer.a; sourceTree = BUILD_PRODUCT_DIR; };
 		2C4DF09D248B2C60009307CB /* libmbedtls.a */ = {isa = PBXFileReference; lastKnownFileType = archive.ar; name = libmbedtls.a; path = lib/libmbedtls.a; sourceTree = BUILT_PRODUCTS_DIR; };
 		515C401A2486BF43004C4DB3 /* CHIPOnOff.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = CHIPOnOff.mm; sourceTree = "<group>"; };
 		515C401B2486BF43004C4DB3 /* CHIPOnOff.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = CHIPOnOff.h; sourceTree = "<group>"; };
@@ -64,7 +61,6 @@
 		B2E0D7B0245B0B5C003C5B48 /* CHIPSetupPayload.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = CHIPSetupPayload.mm; sourceTree = "<group>"; };
 		B2F53AF1245B0DCF0010745E /* CHIPSetupPayloadParserTests.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = CHIPSetupPayloadParserTests.m; sourceTree = "<group>"; };
 		BA09EB3F2474762900605257 /* libCHIP.a */ = {isa = PBXFileReference; lastKnownFileType = archive.ar; name = libCHIP.a; path = lib/libCHIP.a; sourceTree = BUILT_PRODUCTS_DIR; };
-		BA09EB402474762900605257 /* libSetupPayload.a */ = {isa = PBXFileReference; lastKnownFileType = archive.ar; name = libSetupPayload.a; path = lib/libSetupPayload.a; sourceTree = BUILT_PRODUCTS_DIR; };
 		BA107AEE2470CFBB004287EB /* chip_xcode_build_connector.sh */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.script.sh; path = chip_xcode_build_connector.sh; sourceTree = "<group>"; };
 		C4C222BF2475A38700984173 /* CHIPLogging.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = CHIPLogging.mm; sourceTree = "<group>"; };
 /* End PBXFileReference section */
@@ -74,10 +70,8 @@
 			isa = PBXFrameworksBuildPhase;
 			buildActionMask = 2147483647;
 			files = (
-				1E3DE6E124AF86F50028DC40 /* libDeviceLayer.a in Frameworks */,
 				BA09EB43247477BA00605257 /* libCHIP.a in Frameworks */,
 				2C4DF09E248B2C60009307CB /* libmbedtls.a in Frameworks */,
-				BA09EB44247477BC00605257 /* libSetupPayload.a in Frameworks */,
 			);
 			runOnlyForDeploymentPostprocessing = 0;
 		};
@@ -149,8 +143,6 @@
 			children = (
 				2C4DF09D248B2C60009307CB /* libmbedtls.a */,
 				BA09EB3F2474762900605257 /* libCHIP.a */,
-				BA09EB402474762900605257 /* libSetupPayload.a */,
-				1E3DE6E024AF86F50028DC40 /* libDeviceLayer.a */,
 			);
 			name = Frameworks;
 			sourceTree = "<group>";
@@ -365,7 +357,6 @@
 				GCC_NO_COMMON_BLOCKS = YES;
 				GCC_OPTIMIZATION_LEVEL = 0;
 				GCC_PREPROCESSOR_DEFINITIONS = (
-					HAVE_CONFIG_H,
 					"DEBUG=1",
 					"$(inherited)",
 				);
@@ -423,7 +414,7 @@
 				ENABLE_STRICT_OBJC_MSGSEND = YES;
 				GCC_C_LANGUAGE_STANDARD = gnu11;
 				GCC_NO_COMMON_BLOCKS = YES;
-				GCC_PREPROCESSOR_DEFINITIONS = HAVE_CONFIG_H;
+				GCC_PREPROCESSOR_DEFINITIONS = "";
 				GCC_WARN_64_TO_32_BIT_CONVERSION = YES;
 				GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR;
 				GCC_WARN_UNDECLARED_SELECTOR = YES;
@@ -443,7 +434,6 @@
 		B20252A22459E34F00F97062 /* Debug macOS */ = {
 			isa = XCBuildConfiguration;
 			buildSettings = {
-				CHIP_PREFIX = "$(BUILT_PRODUCTS_DIR)";
 				CHIP_ROOT = "$(SRCROOT)/../../..";
 				CODE_SIGN_STYLE = Automatic;
 				DEFINES_MODULE = YES;
@@ -452,7 +442,28 @@
 				DYLIB_CURRENT_VERSION = 1;
 				DYLIB_INSTALL_NAME_BASE = "@rpath";
 				ENABLE_BITCODE = NO;
-				HEADER_SEARCH_PATHS = "$(CHIP_PREFIX)/include";
+				GCC_PREPROCESSOR_DEFINITIONS = (
+					"CHIP_PROJECT_CONFIG_INCLUDE=<CHIPProjectConfig.h>",
+					"CHIP_PLATFORM_CONFIG_INCLUDE=<platform/Darwin/CHIPPlatformConfig.h>",
+					"CHIP_DEVICE_PLATFORM_CONFIG_INCLUDE=<platform/Darwin/CHIPDevicePlatformConfig.h>",
+					CONFIG_NETWORK_LAYER_INET,
+					CONFIG_NETWORK_LAYER_BLE,
+					"CHIP_LOGGING_STYLE_EXTERNAL=1",
+					"CHIP_DEVICE_LAYER_TARGET=Darwin",
+					"CHIP_DEVICE_LAYER_TARGET_DARWIN=1",
+                                        "CHIP_DEVICE_LAYER_TARGET_LINUX=0",
+                                        "CHIP_DEVICE_LAYER_TARGET_NRF5=0",
+                                        "CHIP_DEVICE_LAYER_TARGET_EFR32=0",
+					"CHIP_SYSTEM_CONFIG_USE_SOCKETS=1",
+					"$(inherited)",
+				);
+				HEADER_SEARCH_PATHS = (
+					"$(CHIP_ROOT)/src",
+					"$(CHIP_ROOT)/src/include",
+					"$(CHIP_ROOT)/src/lib",
+					"$(CHIP_ROOT)/config/standalone",
+					"$(CHIP_ROOT)/third_party/nlassert/repo/include",
+				);
 				INFOPLIST_FILE = CHIP/Info.plist;
 				INSTALL_PATH = "$(LOCAL_LIBRARY_DIR)/Frameworks";
 				LD_RUNPATH_SEARCH_PATHS = (
@@ -460,7 +471,7 @@
 					"@executable_path/Frameworks",
 					"@loader_path/Frameworks",
 				);
-				LIBRARY_SEARCH_PATHS = "$(CHIP_PREFIX)/lib";
+				LIBRARY_SEARCH_PATHS = "$(TEMP_DIR)/out/lib";
 				ONLY_ACTIVE_ARCH = NO;
 				OTHER_LDFLAGS = "";
 				"OTHER_LDFLAGS[arch=*]" = (
@@ -480,7 +491,6 @@
 		B20252A32459E34F00F97062 /* Release macOS */ = {
 			isa = XCBuildConfiguration;
 			buildSettings = {
-				CHIP_PREFIX = "$(BUILT_PRODUCTS_DIR)";
 				CHIP_ROOT = "$(SRCROOT)/../../..";
 				CODE_SIGN_STYLE = Automatic;
 				DEFINES_MODULE = YES;
@@ -489,7 +499,29 @@
 				DYLIB_CURRENT_VERSION = 1;
 				DYLIB_INSTALL_NAME_BASE = "@rpath";
 				ENABLE_BITCODE = NO;
-				HEADER_SEARCH_PATHS = "$(CHIP_PREFIX)/include";
+				GCC_PREPROCESSOR_DEFINITIONS = (
+					"CHIP_PROJECT_CONFIG_INCLUDE=<CHIPProjectConfig.h>",
+					"CHIP_PLATFORM_CONFIG_INCLUDE=<platform/Darwin/CHIPPlatformConfig.h>",
+					"CHIP_DEVICE_PLATFORM_CONFIG_INCLUDE=<platform/Darwin/CHIPDevicePlatformConfig.h>",
+					CONFIG_NETWORK_LAYER_INET,
+					CONFIG_NETWORK_LAYER_BLE,
+					"CHIP_LOGGING_STYLE_EXTERNAL=1",
+					"CHIP_DEVICE_LAYER_TARGET=Darwin",
+					"CHIP_DEVICE_LAYER_TARGET_DARWIN=1",
+                                        "CHIP_DEVICE_LAYER_TARGET_LINUX=0",
+                                        "CHIP_DEVICE_LAYER_TARGET_NRF5=0",
+                                        "CHIP_DEVICE_LAYER_TARGET_EFR32=0",
+					"CHIP_SYSTEM_CONFIG_USE_SOCKETS=1",
+					"$(inherited)",
+				);
+				HEADER_SEARCH_PATHS = (
+					"$(CHIP_ROOT)/src",
+					"$(CHIP_ROOT)/src/include",
+					"$(CHIP_ROOT)/src/lib",
+					"$(CHIP_ROOT)/src/app",
+					"$(CHIP_ROOT)/config/standalone",
+					"$(CHIP_ROOT)/third_party/nlassert/repo/include",
+				);
 				INFOPLIST_FILE = CHIP/Info.plist;
 				INSTALL_PATH = "$(LOCAL_LIBRARY_DIR)/Frameworks";
 				LD_RUNPATH_SEARCH_PATHS = (
@@ -497,7 +529,7 @@
 					"@executable_path/Frameworks",
 					"@loader_path/Frameworks",
 				);
-				LIBRARY_SEARCH_PATHS = "$(CHIP_PREFIX)/lib";
+				LIBRARY_SEARCH_PATHS = "$(TEMP_DIR)/out/lib";
 				ONLY_ACTIVE_ARCH = NO;
 				OTHER_LDFLAGS = "";
 				"OTHER_LDFLAGS[arch=*]" = (
@@ -590,7 +622,6 @@
 				GCC_NO_COMMON_BLOCKS = YES;
 				GCC_OPTIMIZATION_LEVEL = 0;
 				GCC_PREPROCESSOR_DEFINITIONS = (
-					HAVE_CONFIG_H,
 					"DEBUG=1",
 					"$(inherited)",
 				);
@@ -614,7 +645,6 @@
 			isa = XCBuildConfiguration;
 			buildSettings = {
 				ARCHS = arm64;
-				CHIP_PREFIX = "$(BUILT_PRODUCTS_DIR)";
 				CHIP_ROOT = "$(SRCROOT)/../../..";
 				CODE_SIGN_STYLE = Automatic;
 				DEFINES_MODULE = YES;
@@ -623,7 +653,29 @@
 				DYLIB_CURRENT_VERSION = 1;
 				DYLIB_INSTALL_NAME_BASE = "@rpath";
 				ENABLE_BITCODE = NO;
-				HEADER_SEARCH_PATHS = "$(CHIP_PREFIX)/include";
+				GCC_PREPROCESSOR_DEFINITIONS = (
+					"CHIP_PROJECT_CONFIG_INCLUDE=<CHIPProjectConfig.h>",
+					"CHIP_PLATFORM_CONFIG_INCLUDE=<platform/Darwin/CHIPPlatformConfig.h>",
+					"CHIP_DEVICE_PLATFORM_CONFIG_INCLUDE=<platform/Darwin/CHIPDevicePlatformConfig.h>",
+					CONFIG_NETWORK_LAYER_INET,
+					CONFIG_NETWORK_LAYER_BLE,
+					"CHIP_LOGGING_STYLE_EXTERNAL=1",
+					"CHIP_DEVICE_LAYER_TARGET=Darwin",
+					"CHIP_DEVICE_LAYER_TARGET_DARWIN=1",
+                                        "CHIP_DEVICE_LAYER_TARGET_LINUX=0",
+                                        "CHIP_DEVICE_LAYER_TARGET_NRF5=0",
+                                        "CHIP_DEVICE_LAYER_TARGET_EFR32=0",
+					"CHIP_SYSTEM_CONFIG_USE_SOCKETS=1",
+					"$(inherited)",
+				);
+				HEADER_SEARCH_PATHS = (
+					"$(CHIP_ROOT)/src",
+					"$(CHIP_ROOT)/src/include",
+					"$(CHIP_ROOT)/src/lib",
+					"$(CHIP_ROOT)/src/app",
+					"$(CHIP_ROOT)/config/ios",
+					"$(CHIP_ROOT)/third_party/nlassert/repo/include",
+				);
 				INFOPLIST_FILE = CHIP/Info.plist;
 				INSTALL_PATH = "$(LOCAL_LIBRARY_DIR)/Frameworks";
 				IPHONEOS_DEPLOYMENT_TARGET = 13.4;
@@ -632,7 +684,7 @@
 					"@executable_path/Frameworks",
 					"@loader_path/Frameworks",
 				);
-				LIBRARY_SEARCH_PATHS = "$(CHIP_PREFIX)/lib";
+				LIBRARY_SEARCH_PATHS = "$(TEMP_DIR)/out/lib";
 				ONLY_ACTIVE_ARCH = NO;
 				OTHER_LDFLAGS = "";
 				"OTHER_LDFLAGS[sdk=*]" = (
@@ -706,7 +758,7 @@
 				ENABLE_STRICT_OBJC_MSGSEND = YES;
 				GCC_C_LANGUAGE_STANDARD = gnu11;
 				GCC_NO_COMMON_BLOCKS = YES;
-				GCC_PREPROCESSOR_DEFINITIONS = HAVE_CONFIG_H;
+				GCC_PREPROCESSOR_DEFINITIONS = "";
 				GCC_WARN_64_TO_32_BIT_CONVERSION = YES;
 				GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR;
 				GCC_WARN_UNDECLARED_SELECTOR = YES;
@@ -728,7 +780,6 @@
 			isa = XCBuildConfiguration;
 			buildSettings = {
 				ARCHS = arm64;
-				CHIP_PREFIX = "$(BUILT_PRODUCTS_DIR)";
 				CHIP_ROOT = "$(SRCROOT)/../../..";
 				CODE_SIGN_STYLE = Automatic;
 				DEFINES_MODULE = YES;
@@ -737,7 +788,29 @@
 				DYLIB_CURRENT_VERSION = 1;
 				DYLIB_INSTALL_NAME_BASE = "@rpath";
 				ENABLE_BITCODE = NO;
-				HEADER_SEARCH_PATHS = "$(CHIP_PREFIX)/include";
+				GCC_PREPROCESSOR_DEFINITIONS = (
+					"CHIP_PROJECT_CONFIG_INCLUDE=<CHIPProjectConfig.h>",
+					"CHIP_PLATFORM_CONFIG_INCLUDE=<platform/Darwin/CHIPPlatformConfig.h>",
+					"CHIP_DEVICE_PLATFORM_CONFIG_INCLUDE=<platform/Darwin/CHIPDevicePlatformConfig.h>",
+					CONFIG_NETWORK_LAYER_INET,
+					CONFIG_NETWORK_LAYER_BLE,
+					"CHIP_LOGGING_STYLE_EXTERNAL=1",
+					"CHIP_DEVICE_LAYER_TARGET=Darwin",
+					"CHIP_DEVICE_LAYER_TARGET_DARWIN=1",
+                                        "CHIP_DEVICE_LAYER_TARGET_LINUX=0",
+                                        "CHIP_DEVICE_LAYER_TARGET_NRF5=0",
+                                        "CHIP_DEVICE_LAYER_TARGET_EFR32=0",
+					"CHIP_SYSTEM_CONFIG_USE_SOCKETS=1",
+					"$(inherited)",
+				);
+				HEADER_SEARCH_PATHS = (
+					"$(CHIP_ROOT)/src",
+					"$(CHIP_ROOT)/src/include",
+					"$(CHIP_ROOT)/src/lib",
+					"$(CHIP_ROOT)/src/app",
+					"$(CHIP_ROOT)/config/ios",
+					"$(CHIP_ROOT)/third_party/nlassert/repo/include",
+				);
 				INFOPLIST_FILE = CHIP/Info.plist;
 				INSTALL_PATH = "$(LOCAL_LIBRARY_DIR)/Frameworks";
 				IPHONEOS_DEPLOYMENT_TARGET = 13.4;
@@ -746,7 +819,7 @@
 					"@executable_path/Frameworks",
 					"@loader_path/Frameworks",
 				);
-				LIBRARY_SEARCH_PATHS = "$(CHIP_PREFIX)/lib";
+				LIBRARY_SEARCH_PATHS = "$(TEMP_DIR)/out/lib";
 				ONLY_ACTIVE_ARCH = NO;
 				OTHER_LDFLAGS = "";
 				"OTHER_LDFLAGS[arch=*]" = (
diff --git a/src/darwin/Framework/CHIP/CHIPDeviceController.mm b/src/darwin/Framework/CHIP/CHIPDeviceController.mm
index 6cd810a..be79df3 100644
--- a/src/darwin/Framework/CHIP/CHIPDeviceController.mm
+++ b/src/darwin/Framework/CHIP/CHIPDeviceController.mm
@@ -17,7 +17,7 @@
 
 #import <Foundation/Foundation.h>
 
-#include "chip-zcl/chip-zcl-zpro-codec.h"
+#include <app/chip-zcl-zpro-codec.h>
 
 #import "CHIPDeviceController.h"
 #import "CHIPError.h"
diff --git a/src/darwin/Framework/CHIP/CHIPLogging.mm b/src/darwin/Framework/CHIP/CHIPLogging.mm
index 495921f..b130dee 100644
--- a/src/darwin/Framework/CHIP/CHIPLogging.mm
+++ b/src/darwin/Framework/CHIP/CHIPLogging.mm
@@ -29,7 +29,7 @@
 #import <Foundation/Foundation.h>
 #import <os/log.h>
 
-#include <support/CHIPLogging.h>
+#include <support/logging/CHIPLogging.h>
 
 #if CHIP_ERROR_LOGGING || CHIP_PROGRESS_LOGGING || CHIP_DETAIL_LOGGING
 
diff --git a/src/darwin/Framework/chip_xcode_build_connector.sh b/src/darwin/Framework/chip_xcode_build_connector.sh
index 579fbe0..538ee33 100755
--- a/src/darwin/Framework/chip_xcode_build_connector.sh
+++ b/src/darwin/Framework/chip_xcode_build_connector.sh
@@ -25,97 +25,91 @@
     exit 1
 }
 
+set -e
+
+mkdir -p "$TEMP_DIR"
 export >"$TEMP_DIR/env.sh"
 
-export PKG_CONFIG_PATH="/usr/local/opt/openssl@1.1/lib/pkgconfig"
-
-set -ex
-
-if [ "$ARCHS" = arm64 ]; then
-    target=aarch64-apple-darwin
-else
-    target=$ARCHS-apple-darwin
-fi
+set -x
 
 # these should be set by the Xcode project
 CHIP_ROOT=${CHIP_ROOT:-"$SRCROOT/../../.."}
 CHIP_ROOT=$(cd "$CHIP_ROOT" && pwd)
-CHIP_PREFIX=${CHIP_PREFIX:-"$BUILT_PRODUCTS_DIR"}
 
 [[ -d ${CHIP_ROOT} ]] || die Please set CHIP_ROOT to the location of the CHIP directory
 
-DEFINES=()
-# lots of environment variables passed by xcode to this script
-if [[ ${CONFIGURATION} == Debug* ]]; then
-    configure_OPTIONS+=(--enable-debug)
-    DEFINES+=(-UNDEBUG)
-else
-    DEFINES+=(-UDEBUG)
-fi
+declare -a DEFINES=()
+# lots of environment variables passed by Xcode to this script
+read -r -a DEFINES <<<"$GCC_PREPROCESSOR_DEFINITIONS"
 
-read -r -a GCC_PREPROCESSOR_DEFINITIONS <<<"$GCC_PREPROCESSOR_DEFINITIONS"
+declare target_defines=
+for define in "${DEFINES[@]}"; do
 
-DEFINES+=("${GCC_PREPROCESSOR_DEFINITIONS[@]/#/-D}")
+    # skip over those that GN does for us
+    case "$define" in
+        CHIP_LOGGING_STYLE*)
+            continue
+            ;;
+        CHIP_DEVICE_LAYER*)
+            continue
+            ;;
+        CHIP_*_CONFIG_INCLUDE)
+            continue
+            ;;
+        CHIP_SYSTEM_CONFIG_*)
+            continue
+            ;;
+        CONFIG_NETWORK_LAYER*)
+            continue
+            ;;
+    esac
+    target_defines+=,\"${define//\"/\\\"}\"
+done
+target_defines=[${target_defines:1}]
 
-ARCH_FLAGS="-arch $ARCHS"
-SYSROOT_FLAGS="-isysroot $SDK_DIR"
-COMPILER_FLAGS="$ARCH_FLAGS $SYSROOT_FLAGS ${DEFINES[*]}"
-
-configure_OPTIONS+=(
-    CPP="cc -E"
-    CPPFLAGS="$COMPILER_FLAGS"
-    CFLAGS="$COMPILER_FLAGS"
-    CXXFLAGS="$COMPILER_FLAGS"
-    OBJCFLAGS="$COMPILER_FLAGS"
-    OBJCXXFLAGS="$COMPILER_FLAGS"
-    LDFLAGS="$ARCH_FLAGS"
+declare -a args=(
+    'chip_crypto="mbedtls"'
+    'chip_build_tools=false'
+    'chip_build_tests=false'
+    'chip_logging_style="external"'
+    'chip_ble_project_config_include=""'
+    'chip_device_project_config_include=""'
+    'chip_inet_project_config_include=""'
+    'chip_system_project_config_include=""'
+    'target_cpu="'"$ARCHS"'"'
+    'target_defines='"$target_defines"
+    'default_configs_cosmetic=[]'
+    'target_cflags=["-target","'"$ARCHS"'-'"$LLVM_TARGET_TRIPLE_VENDOR"'-'"$LLVM_TARGET_TRIPLE_OS_VERSION"'", "-DNDEBUG"]'
 )
 
+if [[ ${CONFIGURATION} != Debug* ]]; then
+    args+='is_debug=true'
+fi
+
 [[ ${PLATFORM_FAMILY_NAME} == iOS ]] && {
-    configure_OPTIONS+=(--with-chip-project-includes="$CHIP_ROOT"/config/ios)
+    args+=(
+        'target_os="ios"'
+        'import("//config/ios/args.gni")'
+    )
 }
 
 [[ ${PLATFORM_FAMILY_NAME} == macOS ]] && {
-    configure_OPTIONS+=(--with-chip-project-includes="$CHIP_ROOT"/config/standalone)
+    args+=(
+        'target_os="mac"'
+        'chip_project_config_include_dirs=["'"$CHIP_ROOT"'/config/standalone"]'
+        'import("'"$CHIP_ROOT"'/config/standalone/args.gni")'
+    )
 }
 
-configure_OPTIONS+=(
-    --prefix="$CHIP_PREFIX"
-    --target="$target"
-    --host="$target"
-    --disable-docs
-    --disable-java
-    --disable-python
-    --disable-shared
-    --disable-tests
-    --disable-tools
-    --with-device-layer=darwin
-    --with-logging-style=external
-    --with-chip-system-project-includes=no
-    --with-chip-inet-project-includes=no
-    --with-chip-ble-project-includes=no
-    --with-chip-warm-project-includes=no
-    --with-chip-device-project-includes=no
-    --with-crypto=mbedtls
-)
-
 (
+    # activate.sh isn't 'set -e' safe
+    cd "$CHIP_ROOT" # pushd and popd because we need the env vars from activate
+    set +ex
+    PW_ENVSETUP_QUIET=1 . scripts/activate.sh
+    set -ex
+
     cd "$TEMP_DIR"
-
-    if [[ ! -x config.status || ${here}/${me} -nt config.status ]]; then
-        "$CHIP_ROOT"/bootstrap-configure -C "${configure_OPTIONS[@]}"
-    else
-        while IFS= read -r -d '' makefile_am; do
-            [[ ${makefile_am} -ot ${makefile_am/.am/.in} ]] || {
-                "$CHIP_ROOT"/bootstrap -w make
-                break
-            }
-        done < <(find "$CHIP_ROOT" -name Makefile.am)
-    fi
-
-    make V=1 install-data                   # all the headers
-    make V=1 -C src/lib install             # libCHIP.a
-    make V=1 -C src/platform install        # libDeviceLayer.a
-    make V=1 -C src/setup_payload install   # libSetupPayload.a
-    make V=1 -C third_party/mbedtls install # libmbedtls.a
+    # [[ -f out/build.ninja ]] ?
+    gn --root="$CHIP_ROOT" gen --check out --args="${args[*]}"
+    ninja -v -C out
 )
diff --git a/src/lib/BUILD.gn b/src/lib/BUILD.gn
index e119fc3..4be170c 100644
--- a/src/lib/BUILD.gn
+++ b/src/lib/BUILD.gn
@@ -20,7 +20,7 @@
 
 static_library("lib") {
   public_deps = [
-    "${chip_root}/src/app:chip",
+    "${chip_root}/src/app",
     "${chip_root}/src/ble",
     "${chip_root}/src/controller",
     "${chip_root}/src/crypto",
@@ -28,6 +28,7 @@
     "${chip_root}/src/lib/core",
     "${chip_root}/src/lib/support",
     "${chip_root}/src/platform",
+    "${chip_root}/src/setup_payload",
     "${chip_root}/src/system",
     "${chip_root}/src/transport",
   ]
diff --git a/src/lib/Makefile.am b/src/lib/Makefile.am
index 3f86b9c..6422502 100644
--- a/src/lib/Makefile.am
+++ b/src/lib/Makefile.am
@@ -60,7 +60,7 @@
     -I$(top_srcdir)/src                                       \
     -I$(top_srcdir)/src/include                               \
     -I$(top_srcdir)/src/lib                                   \
-    -I$(top_srcdir)/src/app/chip-zcl                          \
+    -I$(top_srcdir)/src/app/util                              \
     -I$(top_srcdir)/src/app/gen                               \
     $(chip_InetLayer_CPPFLAGS)                                \
     $(NLASSERT_CPPFLAGS)                                      \
diff --git a/src/transport/BLE.h b/src/transport/BLE.h
index 5648756..392f5bf 100644
--- a/src/transport/BLE.h
+++ b/src/transport/BLE.h
@@ -34,6 +34,7 @@
 #include <ble/BleError.h>
 #include <ble/BleLayer.h>
 #include <core/CHIPCore.h>
+#include <support/DLLUtil.h>
 #include <transport/Base.h>
 
 namespace chip {
diff --git a/src/transport/SecureSessionMgr.h b/src/transport/SecureSessionMgr.h
index 533c710..91995f8 100644
--- a/src/transport/SecureSessionMgr.h
+++ b/src/transport/SecureSessionMgr.h
@@ -32,6 +32,8 @@
 #include <core/ReferenceCounted.h>
 #include <inet/IPAddress.h>
 #include <inet/IPEndPointBasis.h>
+#include <support/CodeUtils.h>
+#include <support/DLLUtil.h>
 #include <transport/Base.h>
 #include <transport/PeerConnections.h>
 #include <transport/SecureSession.h>
diff --git a/third_party/mbedtls/BUILD.gn b/third_party/mbedtls/BUILD.gn
index d4df101..9a82581 100644
--- a/third_party/mbedtls/BUILD.gn
+++ b/third_party/mbedtls/BUILD.gn
@@ -109,5 +109,8 @@
     }
 
     public_configs = [ ":mbedtls_config" ]
+
+    output_name = "libmbedtls"
+    output_dir = "${root_out_dir}/lib"
   }
 }
diff --git a/third_party/nlunit-test/BUILD.gn b/third_party/nlunit-test/BUILD.gn
index df627d3..167c4c0 100644
--- a/third_party/nlunit-test/BUILD.gn
+++ b/third_party/nlunit-test/BUILD.gn
@@ -19,6 +19,8 @@
 static_library("nlunit-test") {
   output_name = "libnlunit-test"
 
+  output_dir = "${root_out_dir}/lib"
+
   sources = [
     "repo/src/nlunit-test.c",
     "repo/src/nlunit-test.h",