CMake per file optimizations

2019-06-08 09:36发布

Elsewhere the question has been asked, "How do I turn off optimizations on one file?" The answer is usually something like this:

cmake_minimum_required( VERSION 3.8 )

project( Hello )

add_executable( hello hello.c foo.c bar.c )

set( CMAKE_C_FLAGS_RELEASE "" )
set( CMAKE_CXX_FLAGS_RELEASE "" )

set_source_files_properties( hello.c 
                PROPERTIES
                COMPILE_FLAGS -O0 )

This works unless you invoke cmake like this:

cmake -GNinja -DCMAKE_BUILD_TYPE=Release ../hello

And you get this in your build.ninja

FLAGS = -O3 -DNDEBUG   -O0

Checking the documentation on COMPILE_FLAGS

Additional flags to be added when compiling this source file.

This makes sense, it is added to the list of COMPILE_FLAGS, it does not override existing compiler flags.

So, within CMake how can you override the optimisation level on a single file and being able to compile the rest of the project in Release? Otherwise you can force the compile to CMAKE_BUILD_TYPE="" which is the default behavior, but that somewhat defeats a selling point of Cmake.

标签: cmake
1条回答
Summer. ? 凉城
2楼-- · 2019-06-08 10:12

You can't overwrite compiler options with the makefile CMake generators on source file level. Options are always appended (see my answer at Is Cmake set variable recursive? for the complete formula).

This is - as far as I know - only supported with the Visual Studio solution/project generators. These generators have flag tables to identify flags that are in the same group/that does overwrite a previous defined flag.

So yours is more like a feature request to also add compiler option tables to CMake's makefile generators.


Alternatives

I just wanted to add some crazy CMake magic I came up with as a workaround. Add the following to your main CMakeLists.txt after the project() command:

if (CMAKE_BUILD_TYPE)
    define_property(
        SOURCE
        PROPERTY COMPILE_FLAGS 
        INHERITED 
        BRIEF_DOCS "brief-doc"
        FULL_DOCS  "full-doc"
    )
    string(TOUPPER ${CMAKE_BUILD_TYPE} _build_type)
    set_directory_properties(PROPERTIES COMPILE_FLAGS "${CMAKE_CXX_FLAGS_${_build_type}}")
    set(CMAKE_CXX_FLAGS_${_build_type} "")
endif()    

This example moves the CMAKE_CXX_FLAGS_<build type> content into an new COMPILE_FLAGS directory property that is then linked to COMPILE_FLAGS source file property via define_property(... INHERITED ...).

Now the build type specific flags are only defined in COMPILE_FLAGS for each source file and you can overwrite/change them e.g. with the code snippet from your example:

set_source_files_properties( 
    hello.c 
    PROPERTIES
    COMPILE_FLAGS -O0 
)

References

查看更多
登录 后发表回答