CMAKE: how to set the compiler flags that contains a given destination path and file name

1.5k views Asked by At

I am working on a CMAKE build system that cross compile ARM CortexM4 using Keil ARMCC toolset in Windows

I set the c compiler flags as the following

set(CMAKE_C_FLAGS  "-c --c99 --cpu=Cortex-M4.fp.sp --apcs=interwork --split_sections")

However, ARMCC compiler has options that requires to input a destination path and a file name. For instance, for each c source file, besides the object file output, I need to also output the source browser information and source dependence by adding the compiler options with the follow format:

--omf_browse=my_output_path/the_current_source_file.crf and --depend my_output_path/the_current_source_file.d

How can i add the above options to the CMAKE_C_FLAGS?

thanks

1

There are 1 answers

2
Dmitry Sokolov On BEST ANSWER

The simple method is to set the property COMPILE_OPTIONS for each needed source file.

Here is the test CMakeLists.txt

project(TestSourceProp)

set(SOURCE_FILES
    main.c
    file.c
)

add_executable(${PROJECT_NAME} ${SOURCE_FILES})

foreach(SRC_ IN LISTS SOURCE_FILES)
    get_filename_component(SRC_BASENAME_ ${SRC_} NAME_WE)
    set_source_files_properties(${SRC_} PROPERTIES COMPILE_OPTIONS "--omf_browse=${SRC_BASENAME_}.crf;--depend=${SRC_BASENAME_}.d")
    get_source_file_property(SRC_PROP_ ${SRC_} COMPILE_OPTIONS)
    message(STATUS "${SRC_}: ${SRC_PROP_}")
endforeach()

The output:

...
-- main.c: --omf_browse=main.crf;--depend=main.d
-- file.c: --omf_browse=file.crf;--depend=file.d
-- Configuring done

Makefile:

CMakeFiles/TestSourceProp.dir/main.c.obj: ../main.c
    @$(CMAKE_COMMAND) -E cmake_echo_color ...
    gcc $(C_DEFINES) $(C_INCLUDES) $(C_FLAGS) --omf_browse=main.crf --depend=main.d -o CMakeFiles/TestSourceProp.dir/main.c.obj -c ...

Another way, I think, is to create a custom CMake-toolchain file.