How to use EXCLUDE_FROM_ALL in CMake? - cmake

I'm trying to understand how to use EXCLUDE_FROM_ALL in CMakeList.txt. I've seen this example:
add_library(foo STATIC EXCLUDE_FROM_ALL "src/foo.cpp")
The only thing I understood from the documentation is that this property is a boolean value. But what does it do, and how do I use it in add_executable(), for example?
Thank you.

Related

CMake: How to make dependence on package optional?

I would like MPI to be optional for using my code.
I currently have a Cmake project that includes MPI as follows in the CMakeLists.txt:
find_package(MPI REQUIRED)
My some of my .cpp and .h files have the following:
#define MPI_available true
On a system where MPI is not available, one can remove the first statement and change the second statement into #define MPI_available false. Then the code is structured such that it doesn't attempt to include ``mpi.h''. However, I would prefer CMAKE to detect the presence/absence of MPI, and set the MPI_available flag for compiling the source files accordingly.
I believe setting
find_package(MPI QUIET)
will ensure that cmake does not choke when it cannot find MPI. Is this the preferred way of getting where I want to be? If so, how to link this to the flag? If not, what should I do to make MPI optional when cmake-> making the code.
Call to find_package(someLib) sets a variable someLib_FOUND. You can use it to check whether the someLib is available, then you can set a compile definitions. Something like:
find_package(MPI)
if(NOT MPI_FOUND)
target_compile_definitions(yourTarget PUBLIC MPI_available=0)
endif()
Calling cmake's default FindMPI.cmake module through find_package(MPI) already ensures that cmake will define the variable MPI_FOUND to reflect whether mpi was found or not.
Afterwards, you can use MPI_FOUND to achieve your goal. For example, you can generate your project's config.h with calls to cmake's config_file to define macros such as MPI_available, and update your code accordingly.

Build twice same library

So I'm building a library, it's basically same code base, but I need to build it twice, the only difference is the second show compile with a -D option, and they need to produce two different artifact. Currently I have this:
add_library(foo STATIC sources...)
add_library(foo.ex STATIC sources...)
target_compile_definitions(foo.ex PUBLIC FOO)
Is this the best way? Is there any other better ways to do?
Is this the best way? Is there any other better ways to do?
Yes, I think this is the best way. I would only refactor and places sources in a common variable so they are not repeated:
set(sources sources...)
add_library(foo STATIC ${sources})
add_library(foo.ex STATIC ${sources})
target_compile_definitions(foo.ex PUBLIC FOO)

What are legal items in the INTERFACE_LINK_LIBRARIES property?

My expectations are, that the items in the target property INTERFACE_LINK_LIBRARIES are other targets. However when I use on Linux for the official Threads package.
find_package(Threads)
get_property(libs TARGET Threads::Threads PROPERTY INTERFACE_LINK_LIBRARIES)
libs ist set to -lpthread, which seems to be a linker flag, not a target.
Is this correct?
That property is populated by the command target_link_libraries(), and its documentation lists what can be specified:
A library target name
A full path to a library file
A plain library name
A link flag
Keywords debug, optimized, or general
Therefore, link flags are allowed here, even if discouraged by the CMake documentation.

What happens for C/C++ builds if CMAKE_BUILD_TYPE is empty?

When the variable CMAKE_BUILD_TYPE is empty which compiler flags are used? Does the compiler then simply use CMAKE_CXX_FLAGS and CMAKE_C_FLAGS for C++ and C?
The default will be "empty" or "Debug" depending on the compiler. The value of the variable will be only of interest in places where SOME_VAR_${CONFIG} is used. So to answer your question. From my understanding the debug flags could be added. The documentation (http://www.cmake.org/cmake/help/v3.3/variable/CMAKE_BUILD_TYPE.html) is unfortunately not so clear.
As a comment on https://blog.kitware.com/cmake-and-the-default-build-type/ says, the default (empty) build type is targeted at Linux distributions which want to use the same compiler flags for multiple packages.

cmake: compiler flag for all build types

is there a way to add a compiler flag independently on what the build type is? Or must I append it to all the CMAKE_C_FLAGS_* variables?
You can simply set CMAKE_C_FLAGS (or CMAKE_CXX_FLAGS for C++). They are applied for all configurations.