CMAKE - check if target is compilable - cmake

My project consists of multiple targets. I want to specify compile options for all of them with
command "target_compile_options"
I get a list of targets using macros from CMake - remove a compile flag for a single translation unit:
macro(apply_global_cxx_flags_to_all_targets)
separate_arguments(_global_cxx_flags_list UNIX_COMMAND ${CMAKE_CXX_FLAGS})
get_property(_targets DIRECTORY PROPERTY BUILDSYSTEM_TARGETS)
foreach(_target ${_targets})
target_compile_options(${_target} PUBLIC ${_global_cxx_flags_list})
endforeach()
unset(CMAKE_CXX_FLAGS)
set(_flag_sync_required TRUE)
endmacro()
But I receive errors:
target_compile_options called with non-compilable target type
as some of targets doesn't require compiling. Is there a way to check if project is compilable? I want to use "target_compile_options as I need to remove some options for one subproject (as in mentioned thread)

Is there a way to check if project is compilable?
Yes, get the type of the target and check if it's an executable or a library and if it's not an INTERFACE library.
I want to specify compile options for all of them
Consider using add_compile_options at root folder instead.

Related

Export compiler flags for all the targets in modern CMake

I'm porting one project to modern CMake. The project has a compiler flag that must be set and exported for all the targets. To avoid repeating target_compile_option for all the targets I tried with add_compile_options, but the latter seems to not export the flag so that it is not used by downstream targets linking the upstream libraries of my project. So do I really have to call target_compile_option for all my targets or is there a workaround?
Thanks in advance for any help.
Since the libs are always part of the same project you can define a macro in the top-level CMakeLists.txt and use this macro instead of add_library. This way you can add common logic inside the macro in addition to add_library:
macro(my_add_library TARGET_NAME)
add_library(${TARGET_NAME} ${ARGN})
target_compile_options(${TARGET_NAME} PRIVATE -myOption)
endmacro()
...
my_add_library(mylib1 SHARED foo.cpp bar.cpp)
...
my_add_library(mylib2 STATIC util.cpp)
Note that the my_add_library command is also available in subdirectories you add after defining the macro so you should be able to use it for all the libraries.

Compile headers for errors using cmake

Background
I have a number of headers as part of a library (call it A) and are also used externally from other libraries (call it C).
I'd like to compile as part A's compilation to ensure the headers are self-contained. (Currently, this involves compiling C and if there are issues, re-compile A & make a new release).
Question
What's the best way to compile headers and discard the results? I am only interested in their successful compilation.
I am thinking of copy and rename them as cpp files (it's a C++ project) and then create a library out of them to check errors. But is there a simpler solution?
My aim is to the command-line equivalent of
g++ [compile_flags] -c header.hpp
and check for errors but not interested in the produced files.
I'd like something that works with cmake 3.13.5 (or older).
Thanks
Since I ran into the same question recently, let me show the solution I went with:
When you list the header files in your target's sources and only use relative paths below the current path (i.e. no .. in the paths), you can use a function like the following to compile a separate cpp file for each hpp file.
function(check_headers target)
# build object library used to "compile" the headers
add_library(${target}_headers OBJECT)
target_link_libraries(${target}_headers PRIVATE ${target})
target_include_directories(${target}_headers PRIVATE "${CMAKE_CURRENT_SOURCE_DIR}")
# add a proxy source file for each header in the target source list
get_target_property(TARGET_SOURCES ${target} SOURCES)
foreach(TARGET_SOURCE ${TARGET_SOURCES})
if ("${TARGET_SOURCE}" MATCHES ".*\.hpp$")
set(HEADER_SOURCEFILE "${CMAKE_CURRENT_BINARY_DIR}/${TARGET_SOURCE}.cpp")
file(WRITE "${HEADER_SOURCEFILE}" "#include \"${TARGET_SOURCE}\"")
target_sources(${target}_headers PRIVATE "${HEADER_SOURCEFILE}")
endif()
endforeach()
endfunction()
It adds an object library that compiles each header file in the given target.

CMake compile options for compile test only

I am using CMake to cross compile a C project for an embedded (heterogeneous) multi-core system. The compiler takes an mandatory argument (-t<type>, the target type). This flag has to be set to pass CMake's compiler test. I am adding this flag in a toolchain file as follows:
add_compile_options(-tMYPLATFORMTYPE)
The problem with this approach is, all project files will be compiled with this flag. Is there a way to configure compile flags for the test compilation only, without affecting the main project configuration? (Note: Within the project different files shall have different values for this flag.)
What I am looking for is something like:
set(CMAKE_TRY_COMPILE_COMPILE_OPTIONS "-tMYPLATFORMTYPE")
I could disabled the compile test, but I would prefer to keep it.
You can check the IN_TRY_COMPILE property and set the flag for try-compile configurations only:
get_property(IS_IN_TRY_COMPILE GLOBAL PROPERTY IN_TRY_COMPILE)
if(IS_IN_TRY_COMPILE)
add_compile_options(-tMYPLATFORMTYPE)
endif()

Directing cmake to link against shared object with debug postfix (_d)

I've got a cmake project that pretty much looks like this:
cmake_minimum_required(VERSION 3.0)
SET(CMAKE_DEBUG_POSTFIX "_d")
include_directories(../TransfunctionerProject)
include_directories(../TransmogrifierProject)
set(Libraries
ContinuumTransfunctioner
Transmogrifier
)
set(SourceFiles
Wrapper.cpp
Logger.cpp
)
add_library(Frobnigator SHARED ${SourceFiles})
add_library(FrobnigatorStatic STATIC ${SourceFiles})
set_target_properties(FrobnigatorStatic PROPERTIES OUTPUT_NAME Frobnigator)
target_link_libraries(Frobnigator ${Libraries})
Where ContinuumTransfunctioner and Transmogrifier projects include the debug postfix directive SET(CMAKE_DEBUG_POSTFIX "_d") so that libContinuumTransfunctioner_d.so and libTransmogrifier_d.so both exist.
The problem is that the current project appears to be linking against the static library without the _d suffix and complains:
/usr/bin/ld: cannot find -lContinuumTransfunctioner
The Libraries that you pass into the target_link_libraries call are interpreted as filenames, not as target names.
This is the unfortunate fallback for that call in CMake: If you pass a random string to it, that cannot be interpreted in a meaningful way, CMake will always assume it to be plain library name. Sometimes this is just what you want, but the name has to be an exact match for an existing library. The whole debug postfix magic will be lost here.
What you might have wanted to do was to pass a library target name instead. This will trigger a much smarter handling of the dependency and would solve your problem. However, that only works if the library is a known target in the context of the target_link_libraries call. You can easily check this as follows:
if(TARGET ContinuumTransfunctioner)
message("Library target name")
else()
message("Plain library name")
endif()
target_link_libraries(Frobnigator ContinuumTransfunctioner)
So how do you get to the target name case? This depends on how your build is structured. If the library is being built as part of your CMake run, simply make sure that the corresponding add_library call is performed from a subdirectory that is pulled in via add_subdirectory from the file that performs the target_link_libraries call.
If the library in question is an external dependency, you need to build an imported target that carries all the relevant information where to find the library files (including any potential debug postfixes). This can be a bit cumbersome to do manually, so if you can, you might want to use CMake's packaging mechanism to generate this automatically as part of the library's build process.
Here's the solution, courtesy of the good people on the cmake mailing list:
# Note:
# $<$<CONFIG:Debug>:_d> is called a generator expression.
# It outputs _d if the build is debug.
#
set(Libraries
ContinuumTransfunctioner$<$<CONFIG:Debug>:_d>
Transmogrifier$<$<CONFIG:Debug>:_d>
)

Compile same file with different flags using CMAKE

I want to compile the same .cpp source file into two different target executables and I am using cmake. One will have some instrumentation code and the other won't. That way I can compare the overhead of instrumentation.
I have the instrumentation code separated with #ifdefs so I want to define a value using the -D flag. I see that is possible with
add_definitions(-DINSTRUMENT)
But it looks like this then applies to all the executables created in that directory. I'm wondering if there is a good way to set the definition only for a specific executable target.
You can set the COMPILE_DEFINITIONS property of one of the targets to have target-specific definitions:
set_target_properties (instrumented_target PROPERTIES COMPILE_DEFINITIONS "INSTRUMENT")
Update:
Starting from CMake 2.8.11 you can also use target_compile_definitions for the same purpose.