Showing posts with label cmake. Show all posts
Showing posts with label cmake. Show all posts

Aug 2, 2026

[cmake] phases and generator expressions

Demystifying Modern CMake: Interface Libraries, Generator Expressions, and Build Phases

Category: C++ & Build Systems
Reading Time: 8 min read


If you've been working with C++ in recent years, you've likely encountered CMake code snippets like this header-only library setup:

add_library(vactor INTERFACE)
add_library(vactor::vactor ALIAS vactor)

target_include_directories(vactor INTERFACE
    $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/..>
    $<INSTALL_INTERFACE:include>
)

While this looks short, it packs in three critical Modern CMake concepts: Interface targets, Generator Expressions, and explicit separation of Build vs. Install environments. Let's break down how this works under the hood.


1. Header-Only Libraries with INTERFACE

In traditional CMake, every target compiled source files into .a, .so, or .lib binaries. But header-only C++ libraries don't generate binaries.

By specifying INTERFACE in add_library(vactor INTERFACE), you tell CMake:

"This library has no binary files to compile directly. It only acts as a container for properties (include directories, compile flags, options) that downstream targets will inherit."

How does CMake know which header file to compile?
It doesn't—and doesn't need to! CMake doesn't pass header files directly to compilers. Instead, it passes directory flags (like -I /path/to/headers) to the compiler. The compiler's C++ preprocessor then finds #include <vactor/vactor.hpp> when parsing code.


2. Why the ALIAS Pattern?

Creating an alias target like add_library(vactor::vactor ALIAS vactor) is a modern best practice:

  • Namespaced Uniformity: When installing libraries and importing them via find_package(), CMake targets are usually namespaced (Package::Target). Using an ALIAS ensures internal subprojects use the exact same target name as external consumers.
  • Early Error Catching: If you misspell an un-namespaced target in target_link_libraries(), CMake might assume it's a raw system library name and defer the error to link-time. Namespaced targets with :: trigger immediate CMake configure-time errors.

3. Decoding Generator Expressions ($<...>)

The syntax $<KEYWORD:VALUE> is a CMake Generator Expression. Standard variables like ${MY_VAR} are evaluated sequentially when CMake reads the script. Generator expressions, however, are deferred evaluation rules.

In our snippet:

  • $<BUILD_INTERFACE:...>: Evaluates to the given path ONLY when building within the local source tree or as a subproject via add_subdirectory().
  • $<INSTALL_INTERFACE:...>: Evaluates to the given path ONLY when exported and imported via find_package().

Without this separation, absolute build directory paths from your local machine would leak into installed config files, breaking the build on end-user machines!


4. The Four Phases of CMake

To fully grasp generator expressions, it helps to understand that CMake execution spans four separate execution phases:

  1. Configure Phase (cmake -S . -B build)
    CMake reads CMakeLists.txt line-by-line, runs system checks, evaluates standard variables (${VAR}) and if() logic, constructing the internal target graph.
  2. Generate Phase (End of cmake -S . -B build)
    CMake evaluates all $<...> generator expressions using the full target graph and writes native build files (Ninja rules, Makefiles, or MSBuild .vcxproj files).
  3. Build Phase (cmake --build build)
    The underlying build tool (Ninja, Make, MSVC) executes the compiler (g++/clang++) using the generated command-line flags.
  4. Runtime Phase
    The user runs the compiled executable. CMake is no longer active.

Summary: Generator Expression Quick Reference

Generator Expression Description / Usage
$<CONFIG:Debug> Evaluates to 1 if current target configuration is Debug, else 0.
$<COMPILE_LANGUAGE:CXX> Evaluates to 1 if current file is compiled with C++ compiler.
$<TARGET_FILE:target> Returns full output path to the target binary file.
$<CXX_COMPILER_ID:GNU> Evaluates to 1 if using GCC compiler.

By leveraging INTERFACE targets and generator expressions, you ensure your C++ libraries stay portable, clean, and easy to consume across both local and installed environments!


Comprehensive Guide to CMake Generator Expressions ($<...>)

CMake Generator Expressions are evaluated during build system generation (the Generate Phase) [cite: 1.1.4, 1.2.1]. They allow conditional compilation, string transformation, target queries, and cross-platform flag customization [cite: 1.1.2, 1.2.5].


1. Conditional & Logical Expressions

Boolean generator expressions evaluate to 1 (true) or 0 (false) [cite: 1.2.5]. They are most commonly used inside conditional output blocks like $<$<CONDITION>:true_string> [cite: 1.1.4, 1.2.5].

Conditional Output

Expression Description
$<$<CONDITION>:string> Evaluates to string if CONDITION is 1, otherwise evaluates to an empty string [cite: 1.1.4, 1.2.5].
$<IF:condition,true_val,false_val> Evaluates to true_val if condition is 1, or false_val if 0 [cite: 1.1.4].

Logical Operators

Expression Description
$<BOOL:string> Converts string to 0 or 1 using standard CMake boolean logic (e.g., OFF, FALSE, 0, empty evaluate to 0) [cite: 1.1.1, 1.2.5].
$<AND:cond1,cond2,...> Evaluates to 1 if all conditions evaluate to 1, otherwise 0 [cite: 1.2.5].
$<OR:cond1,cond2,...> Evaluates to 1 if at least one condition evaluates to 1, otherwise 0 [cite: 1.2.5].
$<NOT:condition> Evaluates to 0 if condition is 1, otherwise 1 [cite: 1.2.5].

2. Comparisons (Strings, Numbers, Versions)

String & List Comparisons

Expression Description
$<STREQUAL:str1,str2> 1 if str1 and str2 are equal (case-sensitive), else 0 [cite: 1.2.5].
$<IN_LIST:item,list> 1 if item is present in the semicolon-separated list, else 0 [cite: 1.2.5].

Numeric Comparisons

Expression Description
$<EQUAL:val1,val2> 1 if numbers val1 and val2 are equal, else 0 [cite: 1.2.5].
$<LESS:val1,val2> 1 if val1 is strictly less than val2, else 0.
$<GREATER:val1,val2> 1 if val1 is strictly greater than val2, else 0.
$<LESS_EQUAL:val1,val2> 1 if val1 is less than or equal to val2, else 0.
$<GREATER_EQUAL:val1,val2> 1 if val1 is greater than or equal to val2, else 0.

Version Comparisons

Expression Description
$<VERSION_EQUAL:v1,v2> 1 if version v1 equals v2, else 0 [cite: 1.2.5].
$<VERSION_LESS:v1,v2> 1 if version v1 is less than v2, else 0 [cite: 1.2.5].
$<VERSION_GREATER:v1,v2> 1 if version v1 is greater than v2, else 0 [cite: 1.2.5].
$<VERSION_LESS_EQUAL:v1,v2> 1 if v1 is less than or equal to v2, else 0 [cite: 1.2.5].
$<VERSION_GREATER_EQUAL:v1,v2> 1 if v1 is greater than or equal to v2, else 0 [cite: 1.2.5].

3. Platform, Compiler & Language Queries

These queries allow writing cross-platform CMake configurations that adjust compiler flags and build settings automatically [cite: 1.2.5].

Compiler & Platform Identification

Expression Description
$<CONFIG:config_name> 1 if current build configuration matches config_name (e.g., Debug, Release) [cite: 1.2.5].
$<PLATFORM_ID:id_list> 1 if the host/target platform matches any ID in the list (e.g., Linux, Windows, Darwin) [cite: 1.2.5].
$<C_COMPILER_ID:id_list> 1 if the C compiler matches an ID in the list (e.g., GNU, Clang, MSVC) [cite: 1.2.5].
$<CXX_COMPILER_ID:id_list> 1 if the C++ compiler matches an ID in the list [cite: 1.2.5].
$<C_COMPILER_VERSION:ver> 1 if C compiler version matches ver [cite: 1.2.5].
$<CXX_COMPILER_VERSION:ver> 1 if C++ compiler version matches ver [cite: 1.2.5].
$<COMPILE_LANGUAGE:lang> 1 if the source file currently being compiled uses language lang (e.g., C, CXX, CUDA) [cite: 1.2.1, 1.2.3].
$<COMPILE_LANG_AND_ID:lang,ids> 1 if language matches lang AND compiler ID matches ids.
$<COMPILE_FEATURES:features> 1 if all specified compile features are available for the target [cite: 1.2.2].

4. Target & Artifact Queries

These expressions extract build artifact metadata, output filenames, and locations dynamically across all platforms [cite: 1.1.3].

File Paths & Artifact Names

Expression Description
$<TARGET_FILE:target> Full path to the main binary file produced by target (e.g., /usr/lib/libfoo.so or C:/app.exe) [cite: 1.1.3, 1.2.1].
$<TARGET_FILE_NAME:target> Filename of the target binary file (e.g., app.exe).
$<TARGET_FILE_DIR:target> Directory containing the target binary file [cite: 1.2.1].
$<TARGET_LINKER_FILE:target> Full path to the file used for linking against target (.lib, .a, .so) [cite: 1.2.1].
$<TARGET_LINKER_FILE_NAME:target> Filename of the linker file.
$<TARGET_LINKER_FILE_DIR:target> Directory containing the linker file.
$<TARGET_SONAME_FILE:target> Full path to the file with soname (.so.1) [cite: 1.2.1].
$<TARGET_PDB_FILE:target> Full path to the Visual Studio .pdb debug symbols file.

Property Queries & Existence

Expression Description
$<TARGET_PROPERTY:target,prop> Value of property prop on target [cite: 1.2.1].
$<TARGET_PROPERTY:prop> Value of property prop on the target being evaluated [cite: 1.2.1].
$<TARGET_NAME_IF_EXISTS:target> Returns target if target exists, else empty string.
$<TARGET_EXISTS:target> 1 if target exists, else 0.
$<TARGET_GENEX_EVAL:target,expr> Evaluates expr in the context of target [cite: 1.2.1].

5. String & List Manipulations

String Transformations

Expression Description
$<LOWER_CASE:string> Converts string to lowercase [cite: 1.1.1].
$<UPPER_CASE:string> Converts string to uppercase [cite: 1.1.1].
$<MAKE_C_IDENTIFIER:string> Converts string into a valid C identifier (replaces non-alphanumeric chars with _).

List Transformations & Operations

Expression Description
$<JOIN:list,glue> Joins elements in list with the delimiter string glue [cite: 1.2.1].
$<REMOVE_DUPLICATES:list> Removes duplicate entries from list.
$<FILTER:list,operator,regex> Filters list entries using an INCLUDE or EXCLUDE regex operator.
$<LIST:ACTION,list,...> Executes list sub-commands (LENGTH, GET, SUBLIST, FIND, TRANSFORM, etc.) [cite: 1.1.1].

6. Path & Interface Expressions

Path Operations

Expression Description
$<PATH:HAS_PARENT_PATH,path> 1 if path has a parent directory, else 0.
$<PATH:GET_FILENAME,path> Extracts the filename portion from path.
$<PATH:GET_PARENT_PATH,path> Extracts the parent directory path from path.
$<PATH:NORMAL_PATH,path> Returns normalized clean path (resolving . and ..).

Build & Install Interfaces

Expression Description
$<BUILD_INTERFACE:paths...> Included only when building within the source/build tree [cite: 1.1.4].
$<INSTALL_INTERFACE:paths...> Included only when consumed from an installed package via find_package() [cite: 1.1.1, 1.1.4].

7. Escaping & Special Characters

Because CMake uses characters like , and > for parsing generator expressions, escaping expressions are required when passing literal special characters inside generator expressions [cite: 1.1.2, 1.2.5].

Generator Expression Literal Evaluated String
$<ANGLE-R> >
$<COMMA> ,
$<SEMICOLON> ;
$<LOWER_THAN> <
$<GREATER_THAN> >

8. Summary Example

Combining multiple generator expressions for modern target configuration:

target_compile_options(my_app PRIVATE
    # Enable strict warnings for GCC/Clang only in Debug build
    $<$<AND:$<OR:$<CXX_COMPILER_ID:GNU>,$<CXX_COMPILER_ID:Clang>>,$<CONFIG:Debug>>:-Wall;-Wextra;-Werror>

    # Disable exceptions when compiling C++ code on MSVC
    $<$<AND:$<CXX_COMPILER_ID:MSVC>,$<COMPILE_LANGUAGE:CXX>>:/EHa->
)



Complete Reference: Built-in CMake Generator Expressions ($<...>)

CMake Generator Expressions are evaluated during build system generation (the Generate Phase). They allow conditional compilation, string transformation, target queries, and cross-platform flag customization.


1. Conditional & Logical Keys

Evaluates conditions or performs boolean operations (0 or 1).

Expression Description
$<$<CONDITION>:value> Conditional output (outputs value if CONDITION is 1).
$<IF:cond,true_val,false_val> Conditional branch selector.
$<BOOL:string> Converts string to boolean 0 or 1.
$<AND:cond1,cond2,...> Logical AND operator.
$<OR:cond1,cond2,...> Logical OR operator.
$<NOT:cond> Logical NOT operator.

2. Comparison Keys

String & List Comparisons

Expression Description
$<STREQUAL:str1,str2> Case-sensitive equality check.
$<EQUAL:str1,str2> Same as STREQUAL (string comparison).
$<IN_LIST:item,list> Checks if item exists inside a CMake list.

Numeric Comparisons

Expression Description
$<EQUAL:num1,num2> Numeric equality.
$<LESS:num1,num2> Numeric less than (<).
$<GREATER:num1,num2> Numeric greater than (>).
$<LESS_EQUAL:num1,num2> Numeric less than or equal (<=).
$<GREATER_EQUAL:num1,num2> Numeric greater than or equal (>=).

Version Comparisons

Expression Description
$<VERSION_EQUAL:v1,v2> Version string equality (=).
$<VERSION_LESS:v1,v2> Version string less than (<).
$<VERSION_GREATER:v1,v2> Version string greater than (>).
$<VERSION_LESS_EQUAL:v1,v2> Version string less than or equal (<=).
$<VERSION_GREATER_EQUAL:v1,v2> Version string greater than or equal (>=).

3. Platform, Compiler & Query Keys

Build Environment & Platform

Expression Description
$<CONFIG:cfg_list> Checks build configuration (e.g., Debug, Release).
$<PLATFORM_ID:id_list> Checks target platform ID (e.g., Linux, Windows, Darwin).
$<POLICY:policy_id> Checks CMake policy status (NEW/OLD).

Compiler Identification

Expression Description
$<C_COMPILER_ID:id_list> Checks C compiler ID (e.g., GNU, Clang, MSVC).
$<CXX_COMPILER_ID:id_list> Checks C++ compiler ID.
$<CUDA_COMPILER_ID:id_list> Checks CUDA compiler ID.
$<OBJC_COMPILER_ID:id_list> Checks Objective-C compiler ID.
$<OBJCXX_COMPILER_ID:id_list> Checks Objective-C++ compiler ID.
$<Fortran_COMPILER_ID:id_list> Checks Fortran compiler ID.
$<HIP_COMPILER_ID:id_list> Checks HIP compiler ID.
$<C_COMPILER_VERSION:ver> Checks C compiler version.
$<CXX_COMPILER_VERSION:ver> Checks C++ compiler version.
$<CUDA_COMPILER_VERSION:ver> Checks CUDA compiler version.
$<OBJC_COMPILER_VERSION:ver> Checks Objective-C compiler version.
$<OBJCXX_COMPILER_VERSION:ver> Checks Objective-C++ compiler version.
$<Fortran_COMPILER_VERSION:ver> Checks Fortran compiler version.
$<HIP_COMPILER_VERSION:ver> Checks HIP compiler version.

Language & Features

Expression Description
$<COMPILE_LANGUAGE:lang> Active source file language (e.g., C, CXX, CUDA).
$<COMPILE_LANG_AND_ID:lang,compiler_ids> Checks language AND compiler ID simultaneously.
$<COMPILE_FEATURES:features> Checks required target compile features.
$<LINK_LANGUAGE:lang> Linker language used for the binary target.
$<LINK_LANG_AND_ID:lang,compiler_ids> Checks link language AND linker/compiler ID.

4. Target & Artifact Property Keys

Paths & File Locations

Expression Description
$<TARGET_FILE:target> Full path to primary binary.
$<TARGET_FILE_NAME:target> Filename of primary binary.
$<TARGET_FILE_DIR:target> Directory containing primary binary.
$<TARGET_FILE_BASE_NAME:target> Base filename without prefix/extension.
$<TARGET_FILE_PREFIX:target> Target output prefix (e.g., lib).
$<TARGET_FILE_SUFFIX:target> Target output suffix (e.g., .so, .exe).
$<TARGET_LINKER_FILE:target> Full path to linker library file (.a, .lib, .so).
$<TARGET_LINKER_FILE_NAME:target> Filename of link library file.
$<TARGET_LINKER_FILE_DIR:target> Directory containing link library file.
$<TARGET_LINKER_FILE_BASE_NAME:target> Base filename of link library.
$<TARGET_LINKER_FILE_PREFIX:target> Linker library prefix.
$<TARGET_LINKER_FILE_SUFFIX:target> Linker library suffix.
$<TARGET_SONAME_FILE:target> Full path to binary file with soname (.so.1).
$<TARGET_SONAME_FILE_NAME:target> Filename of soname file.
$<TARGET_SONAME_FILE_DIR:target> Directory containing soname file.
$<TARGET_PDB_FILE:target> Full path to Visual Studio PDB debug file.
$<TARGET_PDB_FILE_NAME:target> Filename of MSVC PDB file.
$<TARGET_PDB_FILE_DIR:target> Directory containing MSVC PDB file.
$<TARGET_PDB_FILE_BASE_NAME:target> Base name of MSVC PDB file.
$<TARGET_BUNDLE_DIR:target> Directory of macOS Application Bundle.
$<TARGET_BUNDLE_CONTENT_DIR:target> Content directory of macOS Application Bundle (Contents/).

Target Metadata & Properties

Expression Description
$<TARGET_PROPERTY:target,prop> Retrieves property prop from target.
$<TARGET_PROPERTY:prop> Retrieves property prop from current target.
$<TARGET_EXISTS:target> Checks if a target exists (1 or 0).
$<TARGET_NAME_IF_EXISTS:target> Returns target name if exists, else empty string.
$<TARGET_GENEX_EVAL:target,expr> Evaluates expression in context of target.
$<GENEX_EVAL:expr> Evaluates expression recursively.
$<TARGET_POLICY:policy_id> Evaluates policy in target context.
$<TARGET_OBJECTS:target> List of .o/.obj object files from an OBJECT library target.
$<LINK_ONLY:target> Includes link options for target without inheriting compile interface definitions.

5. String & List Manipulation Keys

String Transformations

Expression Description
$<LOWER_CASE:str> Lowercase conversion.
$<UPPER_CASE:str> Uppercase conversion.
$<MAKE_C_IDENTIFIER:str> Converts string into C-style identifier.

List Transformations

Expression Description
$<JOIN:list,glue> Joins list with separator string.
$<REMOVE_DUPLICATES:list> Removes duplicates from list.
`$<FILTER:list,INCLUDE\ EXCLUDE,regex>` Filters list using regular expressions.
$<LIST:action,list,...> Generic list operations (LENGTH, GET, SUBLIST, FIND, TRANSFORM, etc.).

6. Path & Interface Keys

Path Operations

Expression Description
$<PATH:HAS_PARENT_PATH,path> Checks parent directory existence.
$<PATH:GET_FILENAME,path> Extracts filename component.
$<PATH:GET_EXTENSION,path> Extracts extension component.
$<PATH:GET_STEM,path> Extracts filename without extension.
$<PATH:GET_RELATIVE_PART,path> Extracts relative portion.
$<PATH:GET_PARENT_PATH,path> Extracts parent path.
$<PATH:GET_ROOT_NAME,path> Extracts root drive/server name.
$<PATH:GET_ROOT_DIRECTORY,path> Extracts root path directory.
$<PATH:GET_ROOT_PATH,path> Extracts full root path.
$<PATH:NORMAL_PATH,path> Normalizes path syntax.
$<PATH:RELATIVE_PATH,path,base_dir> Computes relative path.
$<PATH:ABSOLUTE_PATH,path,base_dir> Computes absolute path.

Target Interfaces

Expression Description
$<BUILD_INTERFACE:paths...> Used during local build/subproject tree context.
$<INSTALL_INTERFACE:paths...> Used when package is installed and imported via find_package().
$<BUILD_LOCAL_INTERFACE:paths...> Used only in current build tree (not inherited transitivity).

7. Escaping & Output Characters

Special keys used to pass reserved syntactic characters inside generator expressions.

Generator Expression Description
$<ANGLE-R> Right angle bracket >
$<COMMA> Literal comma ,
$<SEMICOLON> Literal semicolon ;
$<LOWER_THAN> Left angle bracket <
$<GREATER_THAN> Right angle bracket >

Jun 19, 2022

[CMake] Modern CMake in style

Reference:
Modern CMake Modules - Bret Brown - CppCon 2021
CppCon 2017: Mathieu Ropert “Using Modern CMake Patterns to Enforce a Good Modular Design”


$ clang++ main.cpp -o vsdmars -Wreturn-type
is same as:
target_compile_options(
    vsdmars PRIVATE
    $<$<COMPILE_LANG_AND_ID:CXX,Clang,GNU>:-Werror=return-type>
)

Find Modules

Loaded by the find_package()
Find<PackageName>.cmake // can have find_library(...) command


Use Modern CMake

  1. declare module with ADD_LIBRARY or ADD_EXECUTABLE
  2. declare build flags with TARGET_xxx()
  3. declare dependencies with TARGET_LINK_LIBRARIES
  4. Specify what is PUBLIC and what is PRIVATE
  5. Consider people are using your library through CMake


Don't

  1. breaking cmake target model
  2. breaking cmake features
    dev workflows
    functions
    standard modules
  3. making changes in source directories
  4. avoid version control operations
  5. toolchain details
    linkers
    compiler
  6. build requires magic flags like -DMAGIC_VAR
  7. Hijacking CMake variables
  8. Requiring special build targets
  9. Expecting certain environments
$ cmake && cmake --build && ctest && cmake --install


Write a module

name the file

vsdmars.cmake // include(vsdmars)
FindVsdmars.cmake // find_package(vsdmars REQUIRED)
vsdmarsConfig.cmake // find_package(vsdmars REQUIRED) <--- Most powerful

e.g.
vsdmarsConfig.cmake
function(target_needs_news)
    cmake_parse_arguments(parsed
        ""             #   options
        "TARGET"       # one-value keywords
        ""             # multi-value keywords
        ${ARGN}        # strings to parse
    )
    # TODO: defensive argument parsing
    set(target ${parsed_TARGET})
    message(VERBOSE "done, ${target}")
endfunction()


Defensive argument parsing

if(parsed_UNPARSED_ARGUMENTS)
    message(FATAL_ERROR
        "BAD ARGUMENT: ${parsed_UNPARSED_ARGUMENTS}"
    )
endif()

if(parsed_KEYWORDS_MISSING_VALUES)
    message(...)
endif()


Essential for CMake modules

  1. API documentation
    README
  2. Testing the cmake module we just wrote
    https://crascit.com/2016/10/18/test-fixtures-with-cmake-ctest/
  3. message() with verbosity
$ cmake --log-level=verbose


Installing a CMake modules

  1. share/cmake/vsdmars/vsdmarsConfig.cmake
  2. ship version files as well if needed


CMakeLists.txt for CMake modules

cmake_minimum_required(VERSION 3.21)
project(vsdmars LANGUAGES NONE) # if module is lang agnostic
enable_testing() # every CMakeLists.txt should have this

install(
    FILES vsdmarsConfig.cmake
    DESTINATION share/cmake/vsdmars
    COMPONENT vsdmars
)


Development Setup

CMAKE_GENERATOR=Ninja
CMAKE_TOOLCHAIN_FILE=path/to/ToolChain.cmake
CMAKE_BUILD_TYPE=Debug
DESTDIR=your/staging/dir


Development Workflow

$ git clone ...
$ cmake -B build -S vsdmars
$ cmake --build build
$ ctest --test-dir build
$ cmake --instal build \
    --prefix /opt/...
    --component vsdmars



Integration Workflow

$ git clone ...
$ cmake -B cxx-build \
    -S cxx-project \
    -DVSDMARS_DIR=./vsdmars/
$ cmake --build cxx-build
$ ctest --ctest-dir cxx-build
$ cmake --install cxx-build \
    --prefix /opt/vsdmars \
    --component cxx-project


Build flags don't scale

  • every change in public flags has to be propagated upwards
  • Most people give up and put every include director in a common/root build file.
  • Correct way of doing so is to put all 3rd party
    my/include/namespace
    into
    ./include/my/include/namespace


Modern build systems

  • Forbid/report circular and hidden dependencies
  • Help developer reason at module level
  • Do more than build as you are told


How?

  • Define your module build flags
  • Define your module dependencies
  • Keep out of other modules internals
  • Each module has a set of private flags
  • Each module has a set of public flags(required to build against its interface)
  • Build interfaces are transitive
  • Private means .cpp includes other headers
  • Public means .h includes other headers
  • Build flags are not gone but encapsulated
  • Can still with CPPFLAGS, CXXFLAGS and LDFLAGS, and external flags are not our concern anymore
  • Declare module with ADD_LIBRARY or ADD_EXECUTABLE
  • Declare build flags with TARGET_xxx()
  • Declare dependencies with TARGET_LINK_LIBRARIES
  • Specify what is PUBLIC and PRIVATE


Global setup

cmake_minimum_required(VERSION 3)
add_compile_options(-Wall -Werror etc.) // including flags effect ABI
add_library(vsdmarsLib src/xxx.cpp)



Declare flags

target_include_directories(vsdmarsLib PUBLIC include)
target_include_directories(vsdmarsLib PRIVATE src)

if (SOME_SETTING)
    target_compile_definitions(vsdmarsLIB
        PUBLIC WITH_SOME_SETTING
endif()


Declare dependencies

target_link_libraries(vsdmarsLib PUBLIC abc)
target_link_libraries(vsdmarsLib PRIVATE xyz)


Header only libraries

# nothing to build actually, just export headers during install/being pulled by other modules
add_library(vsdmarsLibHeaderOnly INTERFACE)
target_include_directories(vsdmarsLibHeaderOnly INTERFACE include)
target_link_libraries(vsdmarsLibHeaderOnly INTERFACE Boost::Boost)



Antipatterns



3rd party

cmake_minimum_required(VERSION 3.5)
find_package(GTest)
add_executable(foo ...)
target_link_libraries(foo GTest::GTest GTest::Main)
find_library(XXX_LIB xxx HINTS ${XXX_DIR}/lib)
add_library(xxx SHARED IMPORTED LOCATION ${XXX_LIB})
target_include_directories(xxx INTERFACE ${XXX_DIR}/include)
target_link_libraries(xxx INTERFACE Boost::boost)

May 28, 2021

[cmake] commands


Reference:
https://cprieto.com/posts/2016/10/cmake-out-of-source-build.html



out-of-dir build:
$ cmake --build build_dir --clean-first


Debugging find module:
$cmake -DCMAKE_FIND_DEBUG_MODE=ON -G Ninja ../

Show compile commands:
$ cmake -DCMAKE_EXPORT_COMPILE_COMMANDS=ON path/to/llvm/sources

Change compiler:
$ -DCMAKE_C_COMPILER=/path/to/clang
$ -DCMAKE_CXX_COMPILER=/path/to/clang++

Oct 11, 2016

[cmake] best practice



FetchContent

Reference:
https://cmake.org/cmake/help/latest/guide/using-dependencies/index.html
The major usage for downloading 3rd party library from source control.



Antipattern:

  • set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -pedantic -Wextra")
  • Debug build (Adding -g manually)
    • Pass -DCMAKE_BUILD_TYPE=debug to CMake
    • CMAKE_BUILD_TYPE / CMAKE_CONFIGURATION_TYPES
      • This variable is only meaningful to single-configuration generators (such as Makefile Generators and Ninja)
      • There are many per-config properties and variables (usually following clean SOME_VAR_<CONFIG> order conventions), such as CMAKE_C_FLAGS_<CONFIG>, specified as uppercase: CMAKE_C_FLAGS_[DEBUG|RELEASE|RELWITHDEBINFO|MINSIZEREL].
        • DEBUG
        • RELEASE
        • RELWITHDEBINFO
        • MINSIZEREL
  • use find_library instead of writing:
  • Do not do:
    • add_executable(myexe myexe.c myexe.h) // gcc/clang will find xxx.c 's xxx.h/hpp automatically.
    • The only exception to this rule is when you generate header files as part of your build.
  • Do not repeat this:
  • Do not 'Invoking make targets' intently. 
    • Invoke targets directly in CMake.
      Most custom commands have a DEPENDS option that can be used to invoke other targets.
  • Do Out-of-source build.
  • Do not call out sed or shell pipelines in Cmakelists.txt. Call cmake in shell script.
  • Do not invoke cmake multiple times. It's just _not_ necessary.
    Ninja or make will detect src code changes.

Compiler and platform test results are cached in CMakeCache.txt
You can modify this file and re-run CMake.
CMake also provides graphical cache editors.


cmake[<options>]<path> 
  • If the specified path contains a CMakeCache.txt, it is treated as a build directory where the build system is reconfigured and regenerated. 
  • Otherwise the specified path is treated as the source directory and the current working directory is treated as build directory.
cmake-E<command>[<options>...] // cmake command mode
  • Run cmake -E help for a summary of commands.
  • $ cmake -E make_directory build
    $ cmake -E chdir cmake -E time cmake
    $ cmake -E time cmake build
use cmake to run the build system
  • $ cmake --build build \ --target mylibrary \ --config Release \ --clean-first
cmake as a scripting language
  • Resist the bash/perl/python temptation!



Parameters needed:

  • cmake_minimum_required(VERSION 3.2 FATAL_ERROR)
    • Prefer the latest version of CMake.
  • project(<name> VERSION <version> LANGUAGES CXX)
    • CMake sets several variables based on project()
    • Call to project() must be direct, not through a function/macro/include
    • CMake will add a call to project() if not found on the top level
  • add_subdirectory(<sourcedir> [<binarydir>])
    • Embed projects as sub-projects.
    • CMake creates a Makefile/Solution for each sub-project.
    • The sub-project does not have to reside in a sub-folder.
  • Make sure that all your projects can be built both standalone and as a sub-project of another project
    • Don’t assume that your project root is the build root.
    • Don’t modify global compile/link flags. 
    • Don’t make any global changes!
  • find_package(Qt5 REQUIRED COMPONENTS Widgets)
    • Finds preinstalled dependencies
    • Can set some variables and define imported targets. 
    • Supports components
  • Add an executable target
  • Add a library target
  • Always add namespace aliases for libraries.
    https://stackoverflow.com/questions/46567646/cmake-usefulness-of-aliases
  • Don’t make libraries STATIC/SHARED unless they cannot be built otherwise.
  • Leave the control of BUILD_SHARED_LIBS to your clients!
  • PRIVATE Only used for this target
    PUBLIC Used for this target and all targets that link against it
    INTERFACE Only used for targets that link against this library

  • target_link_libraries()
    • Set libraries as dependency.

  • Things to follow
  • target_include_directories()
  • Things to follow
  • target_compile_definitions()
  • Things to follow
    • Avoid the add_definitions() command.
    • Avoid adding definitions to CMAKE_<LANG>_FLAGS

  • target_compile_options()
    • Set compile options/flags.
    • if (CMAKE_COMPILER_IS_GNUCXX) target_compile_options(foo PUBLIC -fno-elide-constructors)
      endif()
  • Things to follow
    • Wrap compiler specific options in an appropriate condition.
    • Avoid the add_compile_options() command. 
    • Avoid adding options to CMAKE_<LANG>_FLAGS.

  • target_compile_features()
    • Set required compiler features.
    • target_compile_features(
      foo PUBLIC
      cxx_auto_type
      cxx_range_for
      PRIVATE
      cxx_variadic_templates)
    • CMake will add required compile flags.
    • Errors if the compiler is not supported.
  • Things to follow
    • Don’t add -std=c++11 to CMAKE_<LANG>_FLAGS. ∙
    • Don’t pass -std=c++11 to target_compile_options().

best practices:

  • goal: no custom variables: 
    • # BAD
      set(PROJECT foobar)
      set(LIBRARIES foo)
      if(WIN32)
      list(APPEND LIBRARIES bar)
      endif()
      target_link_libraries(${Project} ${LIBRARIES} )
    • # GOOD
      target_link_libraries(foobar PRIVATE my::foo)

      if(WIN32)
      target_link_libraries(foobar PRIVATE my::bar)
      endif()
  • don't use file(glob)!
    • # BAD
      file(GLOB sources ”*.cpp”)
      add_library(mylib ${sources})
    • file(GLOB) is useful in scripting mode. 
    • Don’t use it in configure mode
  • list all sources explicitly (Explicit is better than implicit)
    • add_library(
      mylib
      main.cpp
      file1.cpp
      file2.cpp)
  • goal: no custom functions
    • Experience taught me: Most of the time it is a bad idea.

Jun 23, 2012

[CMake] information q&a

autoheader discussion
autoheader discussion - 2
CMake linking problem
some variable to notice:
CMAKE_EXE_LINKER_FLAGS
pkg-config usage:
g++ -Wall $(pkg-config --cflags --libs gstreamer-0.10) main.cpp -o MPEG4GStreamer

usage of FIND_PACKAGE_HANDLE_STANDARD_ARGS


Some files under /Modules to look into:
CMakePackageConfigHelpers.cmake
WriteBasicConfigVersionFile.cmake(obsolete)

some Findxxx.cmake files

Aug 28, 2011

[Cmake] CMAKE_INSTALL_PREFIX issue


I want to setup a default value for CMAKE_INSTALL_PREFIX
if the user did not indicate a value.

so I put:

IF (NOT CMAKE_INSTALL_PREFIX)
  SET(CMAKE_INSTALL_PREFIX "/opt/${PACKAGE_NAME}-${PACKAGE_VERSION}"
CACHE PATH "Install path prefix" FORCE)
ELSE (NOT CMAKE_INSTALL_PREFIX)
  MESSAGE("CMAKE_INSTALL_PREFIX already set to: ${CMAKE_INSTALL_PREFIX}")
ENDIF (NOT CMAKE_INSTALL_PREFIX)

but is seems that CMAKE_INSTALL_PREFIX is always set in cache
BEFORE I test this :((

What is the good way to change the default value of CMAKE_INSTALL_PREFIX
besides the usual:

cmake -DCMAKE_INSTALL_PREFIX=/opt/ ../mysource

I wanted the cmake run as simple as possible like

cmake /path/to/source
--------

This question has come up several times and I've been deferring a fix
until a project-specified initial cache feature is implemented.  That
feature has been delayed because it is tricky.

I've just added a fix to CVS CMake for this.  It will be in 2.4.4 (the
upcoming patch release).  Basically your code needs to know whether the
current value of CMAKE_INSTALL_PREFIX was set by the user on the command
line or initialized by CMake.  Now when CMake initializes the variable
it also sets CMAKE_INSTALL_PREFIX_INITIALIZED_TO_DEFAULT to true.  This
enables projects to change the default install prefix like this:

IF(CMAKE_INSTALL_PREFIX_INITIALIZED_TO_DEFAULT)
  SET(CMAKE_INSTALL_PREFIX
    "/opt/foo" CACHE PATH "FOO install prefix" FORCE
    )
ENDIF(CMAKE_INSTALL_PREFIX_INITIALIZED_TO_DEFAULT)

----
> Does it do this for all internal cmake variables or only for CMAKE_INSTALL_PREFIX ?

Just the install prefix.  I don't want to do it for every variable.  For
that we need a PreLoad.cmake feature (which is partially implemented but
not really working) to have project-specific initial cache entries.  I
just put this one in as a hack because so many users have asked for this
specific capability and it is a simple change before the 2.4.4 release.

-Brad
-----
Try dropping the Boolean logic and FORCE.  i.e., use just

SET(CMAKE_INSTALL_PREFIX "/opt/${PACKAGE_NAME}-${PACKAGE_VERSION}"
CACHE PATH "Install path prefix")

That style works for us to provide a default option that can be changed by
the user.  I assume that style will also work to override a variable that has
already been cached, but I don't know for sure.

Alan
=======================================
CMAKE_INSTALL_PREFIX: cmake ../kdelibs -DCMAKE_INSTALL_PREFIX=/opt/kde4 is the equivalent to ./configure --prefix=/opt/kde4

[Cmake] How Find_package works

usually include directory should be like this:
include_directories(${BZIP_INCLUDE_DIRS})


libraries are like this:
set(LIBS ${LIBS} ${LibXML++_LIBRARIES})


the package is found , the variable looks like this:
BZIP2_FOUND


---
the library/executable 's link libraries:
target_link_libraries(exampleProgram ${LIBS})


our own package.cmake files:
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/cmake/Modules/")
set this before find_package, of course.
also read:
http://vsdmars.blogspot.com/2011/08/cmakekde-cmakeliststxt-for-building-kde.html


find package's components:
find_package(Qt COMPONENTS QtOpenGL QtXml REQUIRED)


we can omit the REQUIRED keyword, if the package is optional for your project. In this case, you can use the __FOUND variable (e.g. Qt_QtXml_FOUND) 


Same:

find_package(Qt COMPONENTS QtOpenGL QtXml REQUIRED)
find_package(Qt REQUIRED COMPONENTS QtOpenGL QtXml)
find_package(Qt REQUIRED QtOpenGL QtXml)




If you only require some components of a package, and want to use others only, if they are available, you can call find_package twice:
find_package(Qt COMPONENTS QtXml REQUIRED)
find_package(Qt COMPONENTS QtOpenGL)
Alternatively, you can invoke find_package once with all components, but without theREQUIRED keyword and then explicitly check the required components:
find_package(Qt COMPONENTS QtOpenGL QtXml)
if ( NOT Qt_FOUND OR NOT QtXml_FOUND )
  message(FATAL_ERROR "Package Qt and component QtXml required, but not found!")
endif( NOT Qt_FOUND OR NOT QtXml_FOUND )
---
Find_Package steps:
First CMake checks all directories in ${CMAKE_MODULE_PATH}, then it looks in its own module directory /share/cmake-x.y/Modules/.
If no such file is found, it looks for Config.cmake or -config.cmake, which are supposed to be installed by libraries (but there are currently not yet many libraries which install them) and that don't do detection, but rather just contain hardcoded values for the installed library.


No matter which mode is used, if the package has been found, a set of variables will be defined:
  • _FOUND
  • _INCLUDE_DIRS or _INCLUDES
  • _LIBRARIES or _LIBRARIES or _LIBS
  • _DEFINITIONS
All this takes place in the Find.cmake file.

Now, in the CMakeLists.txt file in the top level directory of your code (the client code that is actually going to make use of the library , we check for the variable _FOUND to see whether the package has been found or not. For most packages the resulting variables use the name of the package all uppercased, e.g. LIBFOO_FOUND, for some packages the exact case of the package is used, e.g. LibFoo_FOUND. If this variable is found, then, we pass the_INCLUDE_DIRS to the include_directories() command and _LIBRARIES to the target_link_libraries() command of CMake.

pkg-config is used.







Aug 26, 2011

[CMake] command line

cmake --help-module-list


cmake --help-module FindKDE4
---
module path : /usr/share/cmake/Modules/
---
use cmake and make VERBOSE=1 to verify that the correct flags are being passed to the compiler and linker.


pkg-config : used for finding library information

[CMake] CMakeLists.txt Template and explained

cmake_minimum_required (VERSION 2.8)
project (KDE Project)


# The version number.
set (Tutorial_VERSION_MAJOR 1)
set (Tutorial_VERSION_MINOR 0)

# configure a header file to pass some of the CMake settings
# to the source code
configure_file (
  "${PROJECT_SOURCE_DIR}/TutorialConfig.h.in"
  "${PROJECT_BINARY_DIR}/TutorialConfig.h"
)

#In TutorialConfig.h.in : this is used for mapping previous {
# set (Tutorial_VERSION_MAJOR 1)
# set (Tutorial_VERSION_MINOR 0)
# }
# Cmake will generate a TutorialConfig.h under PROJECT_BINARY_DIR
# So we need to use include_directories to include this path for g++ to use as a include search path.
#define Tutorial_VERSION_MAJOR @Tutorial_VERSION_MAJOR@
#define Tutorial_VERSION_MINOR @Tutorial_VERSION_MINOR@
#

#
# Include path that g++ will include it as the .h search path
include_directories("${PROJECT_BINARY_DIR}")

# In MathFunctions directory, we need another CmakeLists file contain
# add_library(MathFunctions mysqrt.cxx)
# MathFunctions is a directory, mysqrt.cxx is the .cxx file in that directory ready for compile.


# ask g++ to include  MathFunctions directory for header search.
include_directories ("${PROJECT_SOURCE_DIR}/MathFunctions")

# ask g++ to build this directory
#The CMakeLists.txt file in the specified source directory will be processed immediately by CMake before processing in the current input file continues beyond this command.
add_subdirectory (MathFunctions)


#-------------------
# should we use our own math functions?
option (USE_MYMATH
        "Use tutorial provided math implementation" ON)
# in TutorialConfig.h.in we write:
# #cmakedefine USE_MYMATH
# to make our source file use this USE_MYMATH define also! (defined in TutorialConfig.h, so our source code should include this file)



# add the MathFunctions library?
#
if (USE_MYMATH)
  include_directories ("${PROJECT_SOURCE_DIR}/MathFunctions")
  add_subdirectory (MathFunctions)
  set (EXTRA_LIBS ${EXTRA_LIBS} MathFunctions)
endif (USE_MYMATH)

add_executable(RunMe main.cpp)
# ask g++ to use -lMathFunction , that is, to use this library to build RunMe application.
target_link_libraries (RunMe ${EXTRA_LIBS})



#---INSTALL----  make install will do the following:
# In MathFunctions directory's CMakeLists file we add:
# install (TARGETS MathFunctions DESTINATION bin)
# install (FILES MathFunctions.h DESTINATION include)

#for RunMe application:
install (TARGETS RunMe DESTINATION bin)
install (FILES "${PROJECT_BINARY_DIR}/TutorialConfig.h"      
         DESTINATION include)

# The CMake variable CMAKE_INSTALL_PREFIX is used to determine the root of where the files will be installed.



#---TEST----
# TutorialRuns : Variable
# RunMe : Application
# 25 : arguments
add_test (TutorialRuns RunMe 25)


set_tests_properties (TutorialRuns
  PROPERTIES PASS_REGULAR_EXPRESSION "25 is 5")

#---TEST Macro ---
#define a macro to simplify adding tests, then use it
macro (do_test arg result)
  add_test (TutorialComp${arg} Tutorial ${arg})
  set_tests_properties (TutorialComp${arg}
    PROPERTIES PASS_REGULAR_EXPRESSION ${result})
endmacro (do_test)

# do a bunch of result based tests
do_test (25 "25 is 5")
do_test (-25 "-25 is 0")