ajwood
ajwood

Reputation: 19027

Changing compiler flags on the fly in a CMake project

I've got a large cmake project which is currently completely built for production; the default gcc flags options are something like -fpic -s -O3.

What is the easiest way to recompile a small part of my project with different gcc flags?

But the only way I know how to accomplish this is by regenerating my makefiles with cmake, building the subproject, and regenerating the makefiles again.

$ cmake ../ -DCMAKE_C_FLAGS="-fpic -g"
$ make subproject
$ cmake ../ -DCMAKE_C_FLAGS="-fpic -s -O3"

I'd like to be able to leave cmake out of it, and do something like this:

$ make C_FLAGS="-fpic -g" subproject

Upvotes: 3

Views: 8932

Answers (2)

Alex E
Alex E

Reputation: 307

If you just want to add a flag for building one file (in my case, I'm adding -H to track down a header file), you can do this:

make VERBOSE=1

Then, copy the call to the compiler, add your flag, and call the compiler directly from the shell instead of going through make. My example, notice the -H on the end:

arm-none-eabi-g++  -DARM_MATH_DSP -DARM_MATH_LOOPUNROLL -DCPU_CORTEX_M55=1 -DPLATFORM_HAL=1 -DPRJ_DES_STR="\"ARM ML Embedded Evaluation Kit for MPS3 FPGA and FastModel\"" -DPRJ_VER_STR=\"21.5\" -I/home/ubuntu/contents/ml-embedded-evaluation-kit/dependencies/core-software/drivers/timing_adapter/include -I/home/ubuntu/contents/ml-embedded-evaluation-kit/source -I/home/ubuntu/contents/ml-embedded-evaluation-kit/source/tflite-model -I/home/ubuntu/contents/ml-embedded-evaluation-kit/source/model-parameters -I/home/ubuntu/contents/ml-embedded-evaluation-kit/source/application/hal/include -I/home/ubuntu/contents/ml-embedded-evaluation-kit/source/application/hal/platforms/bare-metal/bsp/cmsis-device/include -I/home/ubuntu/contents/ml-embedded-evaluation-kit/source/application/hal/platforms/bare-metal/bsp/include -I/home/ubuntu/contents/ml-embedded-evaluation-kit/source/application/hal/platforms/bare-metal/bsp/bsp-core/include -I/home/ubuntu/contents/ml-embedded-evaluation-kit/source/application/hal/platforms/bare-metal/bsp/bsp-packs/mps3/include -I/home/ubuntu/contents/ml-embedded-evaluation-kit/source/application/hal/platforms/bare-metal/utils/include -I/home/ubuntu/contents/ml-embedded-evaluation-kit/source/application/hal/platforms/bare-metal/images/include -I/home/ubuntu/contents/ml-embedded-evaluation-kit/source/application/hal/platforms/bare-metal/data_presentation/lcd/include -I/home/ubuntu/contents/ml-embedded-evaluation-kit/source/application/hal/platforms/bare-metal/timer/include -I/home/ubuntu/build/generated/bsp -I/home/ubuntu/contents/ml-embedded-evaluation-kit/dependencies/core-driver/include -I/home/ubuntu/contents/ml-embedded-evaluation-kit/dependencies/cmsis/CMSIS/DSP/Include -I/home/ubuntu/contents/ml-embedded-evaluation-kit/dependencies/cmsis/CMSIS/Core/Include -I/home/ubuntu/contents/ml-embedded-evaluation-kit/dependencies/cmsis/Device/ARM/ARMCM55/Include -I/home/ubuntu/contents/ml-embedded-evaluation-kit/dependencies/cmsis/Device/ARM/ARMCM55/Include/Template  -DARM_NPU=1 -DMPS3_PLATFORM=1 -DLOG_LEVEL=LOG_LEVEL_INFO -DACTIVATION_BUF_SRAM_SZ=0x00400000 -std=c++11 -g   -Wno-unused-parameter -Wno-missing-field-initializers -o CMakeFiles/ethos-u-test.dir/source/main.cpp.obj -c /home/ubuntu/contents/ml-embedded-evaluation-kit/source/main.cpp -H

Upvotes: 0

ComicSansMS
ComicSansMS

Reputation: 54589

You can set the compile flags on a per-target basis using the COMPILE_FLAGS target property.

Note that this cannot be set directly from the command line, so you will have to change your CMakeLists.txt. You can however include an option that allows switching this from the command line:

add_library(subproject...)

set_property(TARGET subproject PROPERTY COMPILE_FLAGS "-fpic")
option(BUILD_SUBPROJECT_WITH_O3 Toggles optimizations for the subproject" OFF)
if(BUILD_SUBPROJECT_WITH_O3)
    set_property(TARGET subproject PROPERTY APPEND COMPILE_FLAGS "-s" "-O3")
else()
    set_property(TARGET subproject PROPERTY APPEND COMPILE_FLAGS "-g")
endif()

Upvotes: 2

Related Questions