Allow size optimization in "debug" builds (#17708)

Some platforms have forced release builds because otherwise they don't
fit in flash. Currently assertions and optimization level are coupled
into a single is_debug argument.

Decouple these in the hope that it enables at least some of these
platforms to develop with debug asserts enabled.
diff --git a/build/config/compiler/BUILD.gn b/build/config/compiler/BUILD.gn
index 8e868be..5826917 100644
--- a/build/config/compiler/BUILD.gn
+++ b/build/config/compiler/BUILD.gn
@@ -174,10 +174,15 @@
   ldflags = cflags
 }
 
+config("optimize_debug") {
+  cflags = [ "-O${optimize_debug_level}" ]
+  ldflags = cflags
+}
+
 config("optimize_default") {
   if (is_debug) {
     if (optimize_debug) {
-      configs = [ "$dir_pw_build:optimize_debugging" ]
+      configs = [ ":optimize_debug" ]
     } else {
       configs = [ ":optimize_zero" ]
     }
diff --git a/build/config/compiler/compiler.gni b/build/config/compiler/compiler.gni
index f5b6414..6e9f0d3 100644
--- a/build/config/compiler/compiler.gni
+++ b/build/config/compiler/compiler.gni
@@ -23,6 +23,9 @@
   optimize_debug =
       current_os == "freertos" || current_os == "zephyr" || current_os == "mbed"
 
+  # Optimization level for debug. Only has an effect if optimize_debug is true.
+  optimize_debug_level = "g"
+
   # Symbol level for debugging.
   symbol_level = 2
 
diff --git a/config/qpg/chip-gn/args.gni b/config/qpg/chip-gn/args.gni
index 8b6c776..346a1d2 100644
--- a/config/qpg/chip-gn/args.gni
+++ b/config/qpg/chip-gn/args.gni
@@ -18,7 +18,9 @@
 
 chip_device_platform = "qpg"
 
-is_debug = false
+optimize_debug_level = "s"
+lwip_debug = false
+
 chip_enable_openthread = true
 chip_config_network_layer_ble = true
 chip_inet_config_enable_ipv4 = false
diff --git a/examples/all-clusters-app/cc13x2x7_26x2x7/args.gni b/examples/all-clusters-app/cc13x2x7_26x2x7/args.gni
index 3e93cef..abd9bc3 100644
--- a/examples/all-clusters-app/cc13x2x7_26x2x7/args.gni
+++ b/examples/all-clusters-app/cc13x2x7_26x2x7/args.gni
@@ -23,7 +23,8 @@
 
 # Size Optimizations
 # use -Os instead of -Og, LWIP release build
-is_debug = false
+optimize_debug_level = "s"
+lwip_debug = false
 
 #optimize_for_size=true
 chip_enable_ota_requestor = true
@@ -32,7 +33,7 @@
 chip_openthread_ftd = false
 
 # Disable CHIP Logging
-#chip_progress_logging = false
+chip_progress_logging = false
 
 # Dsiable verbose logs for all-clusters app to save Flash
 chip_detail_logging = false
diff --git a/examples/lighting-app/qpg/src/AppTask.cpp b/examples/lighting-app/qpg/src/AppTask.cpp
index ee522c9..53fc963 100644
--- a/examples/lighting-app/qpg/src/AppTask.cpp
+++ b/examples/lighting-app/qpg/src/AppTask.cpp
@@ -54,10 +54,9 @@
 TaskHandle_t sAppTaskHandle;
 QueueHandle_t sAppEventQueue;
 
-bool sIsThreadProvisioned     = false;
-bool sIsThreadEnabled         = false;
-bool sHaveBLEConnections      = false;
-bool sHaveServiceConnectivity = false;
+bool sIsThreadProvisioned = false;
+bool sIsThreadEnabled     = false;
+bool sHaveBLEConnections  = false;
 
 uint8_t sAppEventQueueBuffer[APP_EVENT_QUEUE_SIZE * sizeof(AppEvent)];
 
diff --git a/examples/lock-app/cc13x2x7_26x2x7/args.gni b/examples/lock-app/cc13x2x7_26x2x7/args.gni
index 2b1db21..ebb3a78 100644
--- a/examples/lock-app/cc13x2x7_26x2x7/args.gni
+++ b/examples/lock-app/cc13x2x7_26x2x7/args.gni
@@ -23,7 +23,8 @@
 
 # Size Optimizations
 # use -Os instead of -Og, LWIP release build
-is_debug = false
+optimize_debug_level = "s"
+lwip_debug = false
 
 chip_enable_ota_requestor = true
 
diff --git a/examples/persistent-storage/cc13x2x7_26x2x7/args.gni b/examples/persistent-storage/cc13x2x7_26x2x7/args.gni
index 86ad077..c35354a 100644
--- a/examples/persistent-storage/cc13x2x7_26x2x7/args.gni
+++ b/examples/persistent-storage/cc13x2x7_26x2x7/args.gni
@@ -22,7 +22,8 @@
 ti_simplelink_board = "LP_CC2652R7"
 
 # use -Os instead of -Og
-#is_debug = false
+#optimize_debug_level = "s"
+#lwip_debug = false
 
 # BLE options
 chip_config_network_layer_ble = true
diff --git a/examples/persistent-storage/qpg/args.gni b/examples/persistent-storage/qpg/args.gni
index 20c6596..c798cd3 100644
--- a/examples/persistent-storage/qpg/args.gni
+++ b/examples/persistent-storage/qpg/args.gni
@@ -21,6 +21,8 @@
 chip_system_config_use_open_thread_inet_endpoints = false
 chip_with_lwip = true
 
+lwip_debug = false
+
 declare_args() {
   # Disable lock tracking, since our FreeRTOS configuration does not set
   # INCLUDE_xSemaphoreGetMutexHolder
diff --git a/examples/persistent-storage/qpg/main.cpp b/examples/persistent-storage/qpg/main.cpp
index ee31882..b53692e 100644
--- a/examples/persistent-storage/qpg/main.cpp
+++ b/examples/persistent-storage/qpg/main.cpp
@@ -34,8 +34,6 @@
 
 #define LOG_MODULE_ID 1
 
-static TaskHandle_t sTestTaskHandle;
-
 StackType_t appStack[APP_TASK_STACK_SIZE / sizeof(StackType_t)];
 StaticTask_t appTaskStruct;
 
diff --git a/examples/pump-app/cc13x2x7_26x2x7/args.gni b/examples/pump-app/cc13x2x7_26x2x7/args.gni
index cbf1d65..c2a8875 100644
--- a/examples/pump-app/cc13x2x7_26x2x7/args.gni
+++ b/examples/pump-app/cc13x2x7_26x2x7/args.gni
@@ -23,7 +23,8 @@
 
 # Size Optimizations
 # use -Os instead of -Og, LWIP release build
-is_debug = false
+optimize_debug_level = "s"
+lwip_debug = false
 
 chip_enable_ota_requestor = true
 
diff --git a/examples/pump-controller-app/cc13x2x7_26x2x7/args.gni b/examples/pump-controller-app/cc13x2x7_26x2x7/args.gni
index 2b1db21..ebb3a78 100644
--- a/examples/pump-controller-app/cc13x2x7_26x2x7/args.gni
+++ b/examples/pump-controller-app/cc13x2x7_26x2x7/args.gni
@@ -23,7 +23,8 @@
 
 # Size Optimizations
 # use -Os instead of -Og, LWIP release build
-is_debug = false
+optimize_debug_level = "s"
+lwip_debug = false
 
 chip_enable_ota_requestor = true
 
diff --git a/src/lwip/BUILD.gn b/src/lwip/BUILD.gn
index 3cc14c4..2708e16 100644
--- a/src/lwip/BUILD.gn
+++ b/src/lwip/BUILD.gn
@@ -65,7 +65,7 @@
 
   defines = [ "HAVE_LWIP_UDP_BIND_NETIF=1" ]
   if (lwip_platform != "external") {
-    if (is_debug) {
+    if (lwip_debug) {
       # Automatically enable LWIP_DEBUG for internal is_debug builds.
       defines += [ "LWIP_DEBUG=1" ]
     }
diff --git a/src/platform/CYW30739/args.gni b/src/platform/CYW30739/args.gni
index 43f6ec4..474c15b 100644
--- a/src/platform/CYW30739/args.gni
+++ b/src/platform/CYW30739/args.gni
@@ -44,7 +44,8 @@
 chip_system_config_use_lwip = true
 chip_system_config_use_sockets = false
 
-is_debug = false
+optimize_debug_level = "s"
+lwip_debug = false
 default_configs_optimize = [ "$dir_pw_build:optimize_size" ]
 
 chip_build_tests = false
diff --git a/src/platform/cc13x2_26x2/ConfigurationManagerImpl.cpp b/src/platform/cc13x2_26x2/ConfigurationManagerImpl.cpp
index 9ac4b3c..15a00a5 100644
--- a/src/platform/cc13x2_26x2/ConfigurationManagerImpl.cpp
+++ b/src/platform/cc13x2_26x2/ConfigurationManagerImpl.cpp
@@ -67,7 +67,6 @@
     // Initialize the generic implementation base class.
     err = Internal::GenericConfigurationManagerImpl<CC13X2_26X2Config>::Init();
 
-exit:
     return err;
 }
 
diff --git a/src/platform/cc13x2_26x2/OTAImageProcessorImpl.cpp b/src/platform/cc13x2_26x2/OTAImageProcessorImpl.cpp
index d13b8b6..65b3364 100644
--- a/src/platform/cc13x2_26x2/OTAImageProcessorImpl.cpp
+++ b/src/platform/cc13x2_26x2/OTAImageProcessorImpl.cpp
@@ -99,20 +99,6 @@
     return (status == NVS_STATUS_SUCCESS);
 }
 
-static bool eraseExtFlashHeader(NVS_Handle handle)
-{
-    int_fast16_t status;
-    NVS_Attrs regionAttrs;
-    unsigned int sectors;
-
-    NVS_getAttrs(handle, &regionAttrs);
-    /* calculate the number of sectors to erase */
-    sectors = (sizeof(imgFixedHdr_t) + (regionAttrs.sectorSize - 1)) / regionAttrs.sectorSize;
-    status  = NVS_erase(handle, IMG_START, sectors * regionAttrs.sectorSize);
-
-    return (status == NVS_STATUS_SUCCESS);
-}
-
 /* makes room for the new block if needed */
 static bool writeExtFlashImgPages(NVS_Handle handle, size_t bytesWritten, MutableByteSpan block)
 {
diff --git a/src/platform/qpg/args.gni b/src/platform/qpg/args.gni
index 8507378..4bdd6ff 100644
--- a/src/platform/qpg/args.gni
+++ b/src/platform/qpg/args.gni
@@ -35,7 +35,8 @@
 chip_automation_logging = false
 
 # Use -Os
-is_debug = false
+optimize_debug_level = "s"
+lwip_debug = false
 
 chip_build_tests = false