From 84b72818091a81ce834a648c20a878d62e57d0f8 Mon Sep 17 00:00:00 2001 From: Sebastian Eibl <sebastian.eibl@mpcdf.mpg.de> Date: Tue, 12 Nov 2024 12:57:08 +0100 Subject: [PATCH] example for user intervention --- .gitlab-ci.yml | 4 +++ 11_customization/CMakeLists.txt | 11 ++++++++ 11_customization/README.md | 49 +++++++++++++++++++++++++++++++++ 11_customization/main.cpp | 7 +++++ README.md | 1 + 5 files changed, 72 insertions(+) create mode 100644 11_customization/CMakeLists.txt create mode 100644 11_customization/README.md create mode 100644 11_customization/main.cpp diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index 12610b8..ccd5b7c 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -70,6 +70,10 @@ check-recipes: - cmake --build 10_production - ./10_production/app + - CXXFLAGS="-O3 -g" cmake -S 11_customization -B 11_build + - cmake --build 11_build --verbose + - ./11_build/hallo_world + check-links: before_script: - module purge diff --git a/11_customization/CMakeLists.txt b/11_customization/CMakeLists.txt new file mode 100644 index 0000000..22ee954 --- /dev/null +++ b/11_customization/CMakeLists.txt @@ -0,0 +1,11 @@ +cmake_minimum_required(VERSION 3.20) + +project(11_customization + LANGUAGES CXX) + +########################## +# EXECUTABLE +########################## +add_executable(hallo_world) +target_sources(hallo_world PRIVATE main.cpp) + diff --git a/11_customization/README.md b/11_customization/README.md new file mode 100644 index 0000000..0593f75 --- /dev/null +++ b/11_customization/README.md @@ -0,0 +1,49 @@ +# How can I influence the build process as a user? + +There are situations where you as a user need to influence the build process. +This can be adding custom compiler flags, include directories, linker flags, etc. +**If you are the library author you should consider using CMake for doing these thing. +The following is mainly intended for the end-user.** + +## Automatic flags added by `CMAKE_BUILD_TYPE` + +Targets provide modularization and encapsulation. A target +encapsulates everything needed for a certain build job. It also +is some kind of container that allows efficient use as a building +block. + +| CMAKE_BUILD_TYPE | Compiler Flags | Linker Flags | +|------------------|------------------------------------------|------------------| +| Debug | `-g` | | +| Release | `-O3 -DNDEBUG` | | +| RelWithDebInfo | `-O2 -g -DNDEBUG` | | +| MinSizeRel | `-Os -DNDEBUG` | | + +There is no default build type! If you do not specify a build types no flags will be added. + +## CMake uses common environment variables to modify the compiler and linker commands. +CMake uses environment variables like `CFLAGS`, `CXXFLAGS`, `LDFLAGS`, ... to initialize its internal variables [[Ref]](https://cmake.org/cmake/help/latest/variable/CMAKE_LANG_FLAGS.html#variable:CMAKE_%3CLANG%3E_FLAGS). +**CAUTION! the internal variables are set during the first configure call and are cached. Meaning, subsequent changes to the environment variables will not be followed by CMake. To make CMake aware of this you can either delete and recreate the build folder or use `--fresh`[[Ref]](https://cmake.org/cmake/help/latest/manual/cmake.1.html#cmdoption-cmake-fresh).** + +## Set target properties +| Aim | Command | +|----------------------------------------|-----------------------------------------------------------------------------------------------------------| +| add definitions | [target_compile_definitions](https://cmake.org/cmake/help/latest/command/target_compile_definitions.html) | +| add compile flags | [target_compile_options](https://cmake.org/cmake/help/latest/command/target_compile_options.html) | +| add include directories | [target_include_directories](https://cmake.org/cmake/help/latest/command/target_include_directories.html) | +| add a dependency, link against library | [target_link_libraries](https://cmake.org/cmake/help/latest/command/target_link_libraries.html) | +| add linker options | [target_link_options](https://cmake.org/cmake/help/latest/command/target_link_options.html) | +| add sources | [target_sources](https://cmake.org/cmake/help/latest/command/target_sources.html) | + +## Usage requirements +https://cmake.org/cmake/help/latest/manual/cmake-buildsystem.7.html#transitive-usage-requirements + +| keyword | result | +|---------------|--------------------------------------------------------------| +| **PRIVATE** | required to build the target, but not required when using it | +| **INTERFACE** | not required to build the target, but required when using it | +| **PUBLIC** | required for for building the target and for using it | + +## How to link against a "supported" third-party library? +Find the library with [find_package](https://cmake.org/cmake/help/latest/command/find_package.html) and link against +the imported target with [target_link_libraries](https://cmake.org/cmake/help/latest/command/target_link_libraries.html). \ No newline at end of file diff --git a/11_customization/main.cpp b/11_customization/main.cpp new file mode 100644 index 0000000..28edb98 --- /dev/null +++ b/11_customization/main.cpp @@ -0,0 +1,7 @@ +#include <iostream> + +int main(int /*argc*/, char** /*argv*/) +{ + std::cout << "Hallo World" << std::endl; + exit(EXIT_SUCCESS); +} diff --git a/README.md b/README.md index 43ee1ce..81d412c 100644 --- a/README.md +++ b/README.md @@ -20,6 +20,7 @@ we run them in our CI. * [How to manage all the tests in your project?](08_ctest) * [How to install a project with CMake?](09_install) * [How to add compilation configurations to your project?](10_presets) + * [How can I influence the build process as a user?](11_customization) ## Why CMake? * Automatically search for programs, libraries, and header files -- GitLab