How to customize compile options only for app sources?

In our repository we have multiple apps in our application directory with shared code in our subsys directory.  We want to compile all of our code with certain compile options.  For example, we want to compile with -O0 when debugging, add more warning checks, etc.

We cannot build the NCS and Zephyr code with the same compile options because the applications do not fit in flash when Zephyr sources are also compiled with -O0, zephyr has many warnings with certain warning checks, etc.

I have tried adding this to the bottom of an application CMakeLists.txt file, but it does not seem to have any effect:

target_compile_options(app PRIVATE -O0)
Is there a way to specific target compile options, such as '-O0' just for our app sources and our subsys sources?
Parents
  • Hi,

     

    The problem with adding this line to the CFLAGS:

    target_compile_options(app PRIVATE -O0)

    is that the build system already appends optimisation level to your CFLAGS.

    Here's an output of that based on hello_world (long line, sorry...):

    compile_commands.json:  "command": "/opt/zephyr-sdk/zephyr-sdk-0.16.5/arm-zephyr-eabi/bin/arm-zephyr-eabi-gcc -DKERNEL -DNRF9160_XXAA -DNRF_SKIP_FICR_NS_COPY_TO_RAM -DPICOLIBC_INTEGER_PRINTF_SCANF -D_FORTIFY_SOURCE=1 -D_POSIX_C_SOURCE=200809 -D__LINUX_ERRNO_EXTENSIONS__ -D__PROGRAM_START -D__ZEPHYR__=1 -I/opt/ncs/zephyr/include -I/opt/ncs/zephyr/samples/hello_world/build/zephyr/include/generated -I/opt/ncs/zephyr/soc/arm/nordic_nrf/nrf91 -I/opt/ncs/zephyr/soc/arm/nordic_nrf/common/. -I/opt/ncs/nrf/include -I/opt/ncs/nrf/tests/include -I/opt/ncs/modules/hal/cmsis/CMSIS/Core/Include -I/opt/ncs/zephyr/modules/cmsis/. -I/opt/ncs/modules/hal/nordic/nrfx -I/opt/ncs/modules/hal/nordic/nrfx/drivers/include -I/opt/ncs/modules/hal/nordic/nrfx/mdk -I/opt/ncs/zephyr/modules/hal_nordic/nrfx/. -isystem /opt/ncs/nrfxlib/crypto/nrf_cc310_platform/include -fno-strict-aliasing -Os -imacros /opt/ncs/zephyr/samples/hello_world/build/zephyr/include/generated/autoconf.h -fno-printf-return-value -fno-common -g -gdwarf-4 -fdiagnostics-color=always -mcpu=cortex-m33 -mthumb -mabi=aapcs -mfp16-format=ieee --sysroot=/opt/zephyr-sdk/zephyr-sdk-0.16.5/arm-zephyr-eabi/arm-zephyr-eabi -imacros /opt/ncs/zephyr/include/zephyr/toolchain/zephyr_stdint.h -Wall -Wformat -Wformat-security -Wno-format-zero-length -Wno-pointer-sign -Wpointer-arith -Wexpansion-to-defined -Wno-unused-but-set-variable -Werror=implicit-int -fno-pic -fno-pie -fno-asynchronous-unwind-tables -ftls-model=local-exec -fno-reorder-functions --param=min-pagesize=0 -fno-defer-pop -fmacro-prefix-map=/opt/ncs/zephyr/samples/hello_world=CMAKE_SOURCE_DIR -fmacro-prefix-map=/opt/ncs/zephyr=ZEPHYR_BASE -fmacro-prefix-map=/opt/ncs=WEST_TOPDIR -ffunction-sections -fdata-sections --specs=picolibc.specs -std=c99 -O0 -o CMakeFiles/app.dir/src/main.c.obj -c /opt/ncs/zephyr/samples/hello_world/src/main.c",

    Both -O0 and -Os  is appended to the compile commands.

     

    What you can do is to add gcc pragmas to the sections/files that you want to optimise differently:

    #pragma GCC push_options
    #pragma GCC optimize ("Og")
    
    ...
    
    #pragma GCC pop_options

     

    Kind regards,

    Håkon

Reply
  • Hi,

     

    The problem with adding this line to the CFLAGS:

    target_compile_options(app PRIVATE -O0)

    is that the build system already appends optimisation level to your CFLAGS.

    Here's an output of that based on hello_world (long line, sorry...):

    compile_commands.json:  "command": "/opt/zephyr-sdk/zephyr-sdk-0.16.5/arm-zephyr-eabi/bin/arm-zephyr-eabi-gcc -DKERNEL -DNRF9160_XXAA -DNRF_SKIP_FICR_NS_COPY_TO_RAM -DPICOLIBC_INTEGER_PRINTF_SCANF -D_FORTIFY_SOURCE=1 -D_POSIX_C_SOURCE=200809 -D__LINUX_ERRNO_EXTENSIONS__ -D__PROGRAM_START -D__ZEPHYR__=1 -I/opt/ncs/zephyr/include -I/opt/ncs/zephyr/samples/hello_world/build/zephyr/include/generated -I/opt/ncs/zephyr/soc/arm/nordic_nrf/nrf91 -I/opt/ncs/zephyr/soc/arm/nordic_nrf/common/. -I/opt/ncs/nrf/include -I/opt/ncs/nrf/tests/include -I/opt/ncs/modules/hal/cmsis/CMSIS/Core/Include -I/opt/ncs/zephyr/modules/cmsis/. -I/opt/ncs/modules/hal/nordic/nrfx -I/opt/ncs/modules/hal/nordic/nrfx/drivers/include -I/opt/ncs/modules/hal/nordic/nrfx/mdk -I/opt/ncs/zephyr/modules/hal_nordic/nrfx/. -isystem /opt/ncs/nrfxlib/crypto/nrf_cc310_platform/include -fno-strict-aliasing -Os -imacros /opt/ncs/zephyr/samples/hello_world/build/zephyr/include/generated/autoconf.h -fno-printf-return-value -fno-common -g -gdwarf-4 -fdiagnostics-color=always -mcpu=cortex-m33 -mthumb -mabi=aapcs -mfp16-format=ieee --sysroot=/opt/zephyr-sdk/zephyr-sdk-0.16.5/arm-zephyr-eabi/arm-zephyr-eabi -imacros /opt/ncs/zephyr/include/zephyr/toolchain/zephyr_stdint.h -Wall -Wformat -Wformat-security -Wno-format-zero-length -Wno-pointer-sign -Wpointer-arith -Wexpansion-to-defined -Wno-unused-but-set-variable -Werror=implicit-int -fno-pic -fno-pie -fno-asynchronous-unwind-tables -ftls-model=local-exec -fno-reorder-functions --param=min-pagesize=0 -fno-defer-pop -fmacro-prefix-map=/opt/ncs/zephyr/samples/hello_world=CMAKE_SOURCE_DIR -fmacro-prefix-map=/opt/ncs/zephyr=ZEPHYR_BASE -fmacro-prefix-map=/opt/ncs=WEST_TOPDIR -ffunction-sections -fdata-sections --specs=picolibc.specs -std=c99 -O0 -o CMakeFiles/app.dir/src/main.c.obj -c /opt/ncs/zephyr/samples/hello_world/src/main.c",

    Both -O0 and -Os  is appended to the compile commands.

     

    What you can do is to add gcc pragmas to the sections/files that you want to optimise differently:

    #pragma GCC push_options
    #pragma GCC optimize ("Og")
    
    ...
    
    #pragma GCC pop_options

     

    Kind regards,

    Håkon

Children
Related