Problem with Using debug and release mode

I do use platformio(core 6.1.15-home 3.4.4) running on windows 11 using the CLion IDE(2024.2.1) for development.

I managed to make a stm32_nucleo_l476rg to compile and work fine with CPP code but still there are some features or tweaks that I am unable to achieve.

The setup or the build environment is as follows,
PACKAGES:

  • framework-zephyr @ 2.30600.240316 (3.6.0)
  • tool-cmake @ 3.21.3
  • tool-dtc @ 1.4.7
  • tool-ninja @ 1.9.0
  • toolchain-gccarmnoneeabi @ 1.120301.0 (12.3.1)

I have named the issues as follows,

[-1-] I do see a very, very slow compilation time, I mean how do I know ninja is using multiple CPUs or parallel jobs and performing as best as it can?
Taking more than 5 minutes for complete a simple build for almost an empty project with zephyr sound too much inefficient to me.

[-2-] platformio is unable to detect the changes in the .cpp/.c/.hpp/.h (source) files when I do press CLion Debug button, it continues to debug with the old code which is annoying.

[-3-] platformio conducts rebuilding all the files including libraries when I press run button or build button, why PlatformIO do always rebuild all the files and takes too much time instead of rebuilding the changed files and linking the with the old compiled object files?
This always happens for all the run and debug and build (hammer icon) commands, could it be more intelligent in building the files?

[-4-] I want to have two build environments Debug and Release.
With the Debug build environment I do not want any optimisation with specific source file(s) but I need the rest of the files (including the zephyr libraries) with maximum optimisation-similar to release, still I am wondering if that is possible with the debug configuration to cherry pick some files for debugging (applying no optimisation and generating debug symbols) and let rest of the files to get maximum optimization (no debugging), I used eclipse before and it was possible for me to customise optimisation flags for every each of the source code files, I am wondering if that is possible with the platformio as well?

My platformio.ini file looks as follows, how can I proceed in order to achieve aforementioned tweaks.
Thanks

#[env:nucleo_l476rg]
[platformio]
default_envs = debug

[env:nucleo_l476rg]
platform = ststm32@17.5.0
board = nucleo_l476rg
framework = zephyr
build_flags =
    -std=gnu++23
    -std=c++23
build_unflags =
    -fno-rtti
    -std=gnu++11
    -nostdinc
extra_scripts =
    no_atexit.py

[env:release]
build_flags =
    -Os

build_type = release

[env:debug]
debug_build_flags =
    -O0
    -g3
    -ggdb3

debug_init_break =
    b Reset_Handler

debug_speed =
    500

build_type = debug

Issues 1 to 3: I have never observed that in VSCode + PlatformIO extension. Can you cross-check by using that? Note that changing the platformio.ini or CMake files will result in a full rebuild though, as compiler options have changed. Changing only a source file should only cause a single recompile + relink due to that source file.

E.g., in the previously discussed Zephyr project, I add one line to src/main.cpp and press “Build” again.

Processing nucleo_l476rg (platform: ststm32@17.5.0; board: nucleo_l476rg; framework: zephyr)
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Verbose mode can be enabled via `-v, --verbose` option
CONFIGURATION: https://docs.platformio.org/page/boards/ststm32/nucleo_l476rg.html
PLATFORM: ST STM32 (17.5.0) > ST Nucleo L476RG
HARDWARE: STM32L476RGT6 80MHz, 96KB RAM, 1MB Flash
DEBUG: Current (stlink) On-board (stlink) External (blackmagic, cmsis-dap, jlink)
PACKAGES:
 - framework-zephyr @ 2.30600.240316 (3.6.0)
 - tool-cmake @ 3.21.3
 - tool-dtc @ 1.4.7
 - tool-ninja @ 1.9.0
 - toolchain-gccarmnoneeabi @ 1.120301.0 (12.3.1)
LDF: Library Dependency Finder -> https://bit.ly/configure-pio-ldf
LDF Modes: Finder ~ chain, Compatibility ~ soft
Found 0 compatible libraries
Scanning dependencies...
No dependencies
Building in release mode
Compiling .pio\build\nucleo_l476rg\src\main.o
Linking .pio\build\nucleo_l476rg\zephyr\firmware-pre0.elf
c:/users/max/.platformio/packages/toolchain-gccarmnoneeabi/bin/../lib/gcc/arm-none-eabi/12.3.1/../../../../arm-none-eabi/bin/ld.bfd.exe: warning: .pio\build\nucleo_l476rg\zephyr\firmware-pre0.elf has a LOAD segment with RWX permissions
Memory region         Used Size  Region Size  %age Used
           FLASH:       84872 B         1 MB      8.09%
             RAM:       10504 B        96 KB     10.69%
        IDT_LIST:         121 B        32 KB      0.37%
Generating ISR table .pio\build\nucleo_l476rg\zephyr\isr_tables.c
Linking .pio\build\nucleo_l476rg\firmware.elf
c:/users/max/.platformio/packages/toolchain-gccarmnoneeabi/bin/../lib/gcc/arm-none-eabi/12.3.1/../../../../arm-none-eabi/bin/ld.bfd.exe: warning: .pio\build\nucleo_l476rg\firmware.elf has a LOAD segment with RWX permissions
Memory region         Used Size  Region Size  %age Used
           FLASH:       84872 B         1 MB      8.09%
             RAM:       10504 B        96 KB     10.69%
        IDT_LIST:          0 GB        32 KB      0.00%
Checking size .pio\build\nucleo_l476rg\firmware.elf
Advanced Memory Usage is available via "PlatformIO Home > Project Inspect"
RAM:   [=         ]  10.7% (used 10506 bytes from 98304 bytes)
Flash: [=         ]   7.8% (used 82020 bytes from 1048576 bytes)
Building .pio\build\nucleo_l476rg\firmware.bin
================ [SUCCESS] Took 4.82 seconds ================

It only regenerated main.o, then relinked the firmware (in a 2-stage link process). All other object files were reused, 100%.

Issue 4: This was discussed in Granular optimisation settings - #2 by maxgerhardt.