[CMake] Is there an idiomatic way of setting platform specific compiler options in a generic way?

Kuba Ober kuba at mareimbrium.org
Wed Feb 20 17:55:03 EST 2019


I am developing platform support for an embedded target (Z8 Encore!), and it mostly works – but I’d like to make it easier to set the compiler and linker flags, as there’s a ton of them: and they are nothing like GNU flags, so they only apply when building for that particular target. 

Thus a question: is there a preferred way for the user to set platform-specific flags without having to know them exactly? I’m thinking of having some sort of platform-agnostic means of selecting compile and link flags, and then translating those for the platform the target is generated for.

Specifically, I want to avoid having the following in CMakeLists for the project:

if("${COMPILER_ID}" strequal "ez8cc")
target_compile_options(target1 PRIVATE -const:RAM)
elseif("${COMPILER_ID}" strequal "gcc" and "${CMAKE_SYSTEM_PROCESSOR}" strequal "somecpu")
target_compile_definitions(target1 PRIVATE ram rom far near) # those shall be empty
endif()

I’d like to do something like this instead:

set_target_properties(target1 PROPERTIES  OPT_CONST RAM OPT_PACK TIGHT …)

Then, a process_target_opts() function called at the end of CMakeLists would iterate the targets, and for each target call process_${COMPILER_ID}_opts() (if such function would be present or issue a warning otherwise). That function then converts the OPT_ options to platform-specific flags, based on cpu and/or compiler.

Is it something that would be more-or-less idiomatic, or is there another preferred way of doing it? I really don’t want to list the options manually for each target/platform combo, as they’d differ quite a bit, and I have lots of targets (hundreds).

I’d appreciate any hints.

Cheers, Kuba Ober



More information about the CMake mailing list