| 1 | # Build system |
| 2 | |
| 3 | \brief Documents variables to control the build system and provided CMake |
| 4 | modules |
| 5 | |
| 6 | ## Variables passable as CMake arguments |
| 7 | |
| 8 | ### Useful variables provided by CMake itself |
| 9 | * `CMAKE_INSTALL_PREFIX=path`: specifies the final install prefix (temporary |
| 10 | install prefix is set via `make` argument `DESTDIR=path`) |
| 11 | * `CMAKE_BUILD_TYPE=Release/Debug`: specifies whether to do a debug or a release |
| 12 | build |
| 13 | * `BUILD_SHARED_LIBS=ON/OFF`: whether to build shared libraries (`ON`) or static |
| 14 | libraries (`OFF`); it is not possible to build both at the same time within the |
| 15 | same build process |
| 16 | * `CMAKE_SKIP_BUILD_RPATH=OFF`: ensures the rpath is set in the build tree |
| 17 | * `CMAKE_INSTALL_RPATH=rpath`: sets the rpath used when installing |
| 18 | * `CMAKE_CXX_FLAGS`: sets flags to be passed to the C++ compiler |
| 19 | * `CMAKE_FIND_LIBRARY_SUFFIXES`: sets the library suffixes the build script will |
| 20 | consider, e.g., set to `.a;.lib` to prefer static Windows libraries or to |
| 21 | `.dll;.dll.a` to prefer shared Windows libraries |
| 22 | * `CMAKE_FIND_DEBUG_MODE=ON/OFF` or `--debug-find` flag: whether verbose debug |
| 23 | output for finding libraries should be enabled |
| 24 | * variables provided by [GNUInstallDirs](https://cmake.org/cmake/help/latest/module/GNUInstallDirs.html) |
| 25 | |
| 26 | ### Custom variables |
| 27 | The following variables are read by the CMake modules provided by `c++utilities` |
| 28 | and `qtutilities`. |
| 29 | |
| 30 | None of these are enabled or set by default, unless stated otherwise. |
| 31 | |
| 32 | * `LIB_SUFFIX=suffix`: suffix for the library install directory |
| 33 | * `LIB_SUFFIX_32=suffix`: suffix for the library install directory |
| 34 | * used when building for 32-bit platforms |
| 35 | * overrides general `LIB_SUFFIX` when building for 32-bit platforms |
| 36 | * `LIB_SUFFIX_64=suffix`: suffix for the library install directory |
| 37 | * used when building for 64-bit platforms |
| 38 | * overrides general `LIB_SUFFIX` when building for 64-bit platforms |
| 39 | * `SHELL_COMPLETION_ENABLED=ON/OFF`: enables shell completion in general |
| 40 | (enabled by default) |
| 41 | * `BASH_COMPLETION_ENABLED=ON/OFF`: enables Bash completion (enabled by |
| 42 | default) |
| 43 | * `LOGGING_ENABLED=ON/OFF`: enables further logging in some applications |
| 44 | * `FORCE_OLD_ABI=ON/OFF`: forces the use of the old C++ ABI |
| 45 | * sets `_GLIBCXX_USE_CXX11_ABI=0` |
| 46 | * only relevant when using libstdc++ |
| 47 | * `EXCLUDE_TESTS_FROM_ALL=ON/OFF`: excludes tests from the all target |
| 48 | (enabled by default) |
| 49 | * `APPEND_GIT_REVISION=ON/OFF`: whether the build script should attempt to |
| 50 | append the Git revision and the latest commit ID to the version |
| 51 | * displayed via `--help` |
| 52 | * enabled by default but has no effect when the source directory is |
| 53 | not a Git checkout or Git is not installed |
| 54 | * `CLANG_FORMAT_ENABLED=ON/OFF`: enables the tidy target for code formatting via |
| 55 | `clang-format` |
| 56 | * can be made unavailable by setting `META_NO_TIDY` in the project file |
| 57 | * only available if format rules are available |
| 58 | * also enables the tidy check executed via the `check` target |
| 59 | * `CMAKE_FORMAT_ENABLED=ON/OFF`: enables the tidy target for code formatting via |
| 60 | `cmake-format` |
| 61 | * can be made unavailable by setting `META_NO_TIDY` in the project file |
| 62 | * options can be adjusted by setting `META_CMAKE_FORMAT_OPTIONS` in the |
| 63 | project file |
| 64 | * `CLANG_TIDY_ENABLED=ON/OFF`: enables the target `static-check` for static code |
| 65 | analysis with `clang-tidy` |
| 66 | * can be made unavailable by setting `META_NO_STATIC_ANALYSIS` in the |
| 67 | project file |
| 68 | * the variable [CMAKE_<LANG>_CLANG_TIDY](https://cmake.org/cmake/help/latest/variable/CMAKE_LANG_CLANG_TIDY.html#variable:CMAKE_%3CLANG%3E_CLANG_TIDY) |
| 69 | provided by CMake itself provides a similar functionality |
| 70 | * `CLANG_SOURCE_BASED_COVERAGE_ENABLED=ON/OFF`: enables the `coverage` target to |
| 71 | determine source-based test coverage using Clang/llvm |
| 72 | * only available when building with Clang under UNIX |
| 73 | * the coverage report is stored in the build directory |
| 74 | * `ENABLE_INSTALL_TARGETS=ON/OFF`: enables the creation of install targets (enabled |
| 75 | by default) |
| 76 | * `ENABLE_ESCAPE_CODES_BY_DEAULT=ON/OFF`: enables the use of escape codes for formatted |
| 77 | output by default |
| 78 | * enabled by default |
| 79 | * see ApplicationUtilities::NoColorArgument and EscapeCodes::enabled |
| 80 | * has to be set when building `c++utilities`; projects using that build of |
| 81 | `c++utilities` will then use this default |
| 82 | * `USE_NATIVE_FILE_BUFFER=ON/OFF`: use native functions to open file streams |
| 83 | * Allows opening files that have non-ASCII characters in the path under Windows. |
| 84 | * Allows the use of native file descriptors, which is required to open files |
| 85 | from URLs provided by Android's Storage Access Framework. |
| 86 | * Changing this option alters the ABI of `c++utilities` and other libraries |
| 87 | exposing that ABI (e.g., `tagparser` and `passwordfile`). |
| 88 | * This feature is implemented in `c++utilities`, so the option must be specified |
| 89 | when building `c++utilities`. Specifying it only when building, e.g., `tagparser` |
| 90 | has *no* effect. |
| 91 | * `USE_STANDARD_FILESYSTEM=ON/OFF`: enables the use of `std::filesystem` (default) |
| 92 | * Disabling this is required when building for macOS and Android at the time of |
| 93 | writing this documentation. |
| 94 | * Bash completion will not be able to suggest files and directories when disabled. |
| 95 | * `USER_DEFINED_ADDITIONAL_LIBRARIES`: specifies additional libraries to link against |
| 96 | (added after any other libraries to the linker line) |
| 97 | * Use case: Some platforms/standard libraries/compilers require linking against |
| 98 | additional libraries for certain features. Adding additional libraries for |
| 99 | `std::filesystem` is covered by use_standard_filesystem(). However, it makes no |
| 100 | sense to add such a function for every specific platform/feature. For instance, |
| 101 | it seems to be required to add "-latomic" to the linker line for |
| 102 | armv7-none-linux-androideabi/libc++/clang (not for aarch64-none-linux-androidabi), |
| 103 | but it is better to cover such details on the packaging level and only allow passing |
| 104 | such flags here. |
| 105 | * Using `CMAKE_EXE_LINKER_FLAGS` or `CMAKE_SHARED_LINKER_FLAGS` is often not helpful |
| 106 | because the additional flags need to be added at the end of the linker line most |
| 107 | of the time. |
| 108 | * `CONFIGURATION_NAME`: specifies a name to be incorporated into install paths as a |
| 109 | *suffix* |
| 110 | * Builds with different configuration names can be installed alongside within the |
| 111 | same install prefix. |
| 112 | * Use cases: |
| 113 | * Installing static and shared libraries within the same prefix. |
| 114 | * Installing different versions of the library within the same prefix. |
| 115 | * Installing the version linked against Qt 5 and the version linked against |
| 116 | Qt 6 within the same prefix (when Qt 6 is released). |
| 117 | * Does not affect library names, because their filenames might differ anyway |
| 118 | between different configurations (e.g., static vs. shared libraries). |
| 119 | * Set `CONFIGURATION_TARGET_SUFFIX` in accordance so library names are affected |
| 120 | as well. |
| 121 | * Set `CONFIGURATION_PACKAGE_SUFFIX` when building consuming libraries to *use* |
| 122 | libraries (and their headers and other files) built with `CONFIGURATION_NAME`. |
| 123 | * `NAMESPACE`: specifies a name to be incorporated into install paths as a *prefix* |
| 124 | * Builds with different namespaces can be installed alongside within the same |
| 125 | install prefix. |
| 126 | * This may be used by packagers who want to give the package a more unique |
| 127 | name, e.g., Debian is using `NAMESPACE=martchus` for c++utilities and |
| 128 | qtutilities. Supposedly, this should be avoided for developer-facing packaging |
| 129 | like vcpkg, as it is likely not expected by those users. |
| 130 | * Set `PACKAGE_NAMESPACE_PREFIX` when building consuming libraries to *use* |
| 131 | libraries (and their headers and other files) built with `NAMESPACE`. |
| 132 | * `ENABLE_WARNINGS`: enables GCC/Clang warnings I consider useful |
| 133 | * `TREAT_WARNINGS_AS_ERRORS`: treat CCC/Clang warnings as errors |
| 134 | * `ENABLE_DEVEL_DEFAULTS`: enables defaults I find useful for development (warnings, |
| 135 | treat warnings as errors, build tests as part of all/default target, enable tidy |
| 136 | targets, build by default shared libs when targeting GNU/Linux, …) |
| 137 | |
| 138 | #### Variables for specifying the location of 3rd party dependencies |
| 139 | The build script tries to find the required dependencies at standard locations |
| 140 | using the CMake functions |
| 141 | [`find_library`](https://cmake.org/cmake/help/latest/command/find_library.html) |
| 142 | and |
| 143 | [`find_package`](https://cmake.org/cmake/help/latest/command/find_package.html). |
| 144 | The behavior of those functions can be controlled by setting some variables, e.g., |
| 145 | using a toolchain file. Check out the CMake documentation for this. |
| 146 | |
| 147 | If the detection does not work as expected or a library from a non-standard |
| 148 | location should be used, one can also just pass the library path directly by specifying |
| 149 | the relevant CMake cache variable on the CMake invocation. |
| 150 | |
| 151 | #### Windows specific |
| 152 | * `FORCE_UTF8_CODEPAGE=ON/OFF`: forces the use of the UTF-8 codepage in the terminal |
| 153 | * `WINDOWS_RESOURCES_ENABLED=ON/OFF`: enables creating resources for |
| 154 | application metadata and icon (enabled by default) |
| 155 | |
| 156 | #### macOS specific |
| 157 | * `BUNDLE_INSTALL_DESTINATION=/some/path`: specifies the install destination for |
| 158 | application bundles; if not specified, the default bin directory is used |
| 159 | |
| 160 | #### Qt specific |
| 161 | * `WIDGETS_GUI=ON/OFF`: enables Qt Widgets GUI for projects where it is |
| 162 | available and optional |
| 163 | * `QUICK_GUI=ON/OFF`: enables Qt Quick GUI for projects where it is available |
| 164 | and optional |
| 165 | * `QT_PACKAGE_PREFIX=Qt5`: sets the prefix for Qt packages, by default `Qt5` |
| 166 | * use `QT_PACKAGE_PREFIX=Qt6` to build against Qt 6 |
| 167 | * `KF_PACKAGE_PREFIX=KF5`: sets the prefix for KDE Frameworks packages, by |
| 168 | default `KF5` |
| 169 | * `ENABLE_QT_TRANSLATIONS=ON/OFF`: enables translations for Qt applications, |
| 170 | enabled by default |
| 171 | * `BUILTIN_TRANSLATIONS=ON/OFF`: enables built-in translations instead of |
| 172 | installing translations as separate files |
| 173 | * `BUILTIN_TRANSLATIONS_OF_QT=ON/OFF`: enables built-in translations also for Qt's |
| 174 | own translation files when building an application |
| 175 | * enabled by default when `BUILTIN_TRANSLATIONS` is enabled |
| 176 | * useful when deploying Qt itself anyway, e.g., Windows builds |
| 177 | * better turned off when using system Qt and the system provides a translations |
| 178 | package for Qt, e.g., GNU/Linux builds |
| 179 | * when building a library, Qt's translations are never built-in |
| 180 | * has no effect when `BUILTIN_TRANSLATIONS` is disabled |
| 181 | * `BUILTIN_ICON_THEMES=breeze;breeze-dark;...`: specifies icon themes to |
| 182 | be built-in |
| 183 | * `BUILTIN_ICON_THEMES_IN_LIBRARIES=breeze;breeze-dark;...`: same as above but |
| 184 | also affects libraries |
| 185 | * `SVG_SUPPORT=ON/OFF`: enables linking against the static SVG image format |
| 186 | plugin |
| 187 | * enabled by default |
| 188 | * requires the Qt Svg module |
| 189 | * only relevant when using static Qt |
| 190 | * `SVG_ICON_SUPPORT=ON/OFF`: enables linking against the static SVG icon engine |
| 191 | plugin provided by the Qt Svg module |
| 192 | * enabled by default |
| 193 | * requires the Qt Svg module |
| 194 | * only relevant when using static Qt |
| 195 | * required to use the Breeze icon theme (or any other SVG icon theme) |
| 196 | * `IMAGE_FORMAT_SUPPORT`: enables linking against the specified static image |
| 197 | format plugins |
| 198 | * comma-separated list |
| 199 | * by default set to "Gif;ICO;Jpeg", so support for Gif, ICO, and Jpeg is |
| 200 | enabled by default |
| 201 | * note that PNG support is not provided via a plugin, so it should be |
| 202 | always available and not be affected by this option |
| 203 | * further image formats require building the plugins contained by the |
| 204 | additional `qtimageformats` repository |
| 205 | * only relevant when using static Qt |
| 206 | * `QT_PLUGIN_DIR`: specifies the directory to install Qt plugins to |
| 207 | * When installing plugins to a non-standard directory (e.g., during development), |
| 208 | set the `QT_PLUGIN_PATH` environment variable accordingly so the plugin can |
| 209 | be found. |
| 210 | * The install directory defaults to `qmake -query QT_INSTALL_PLUGINS` unless |
| 211 | that directory would be outside the specified `CMAKE_INSTALL_PREFIX`. |
| 212 | * `WEBVIEW_PROVIDER=auto/webkit/webengine/none`: specifies the Qt module to use |
| 213 | for the web view |
| 214 | * `JS_PROVIDER=qml/script/none`: specifies the Qt module to use |
| 215 | for the JavaScript engine |
| 216 | * `WEBVIEW_PROVIDER=webengine/webkit/none`: specifies the Qt module to use |
| 217 | for the built-in web view |
| 218 | |
| 219 | |
| 220 | ## Variables to be set in the project file |
| 221 | The following variables are read by the CMake modules provided by c++utilities |
| 222 | and qtutilities. |
| 223 | |
| 224 | ### Metadata and overall project configuration |
| 225 | * `META_PROJECT_NAME=name`: specifies the project name, which is used as the |
| 226 | application/library name; must not contain spaces |
| 227 | * `META_APP_NAME=The Name`: specifies a more readable version of the project |
| 228 | name used, for instance, in the about dialog and desktop file |
| 229 | * `META_APP_AUTHOR`: specifies the author shown, for instance, in the about |
| 230 | dialog |
| 231 | * `META_APP_DESCRIPTION`: specifies a description shown, for instance, in the about |
| 232 | dialog and desktop file |
| 233 | * `META_GENERIC_NAME`: specifies a generic name for the desktop file |
| 234 | * `META_VERSION_MAJOR/MINOR/PATCH=number`: specifies the application/library |
| 235 | version; the default is 0 |
| 236 | * `META_PROJECT_TYPE=application/library/plugin/qtplugin`: specifies whether |
| 237 | to build an application, a library, or a plugin |
| 238 | * `META_CXX_STANDARD=11/14/..`: specifies the C++ version; the default is 14 |
| 239 | * `META_NO_TIDY`: disables the availability of enabling formatting via |
| 240 | `CLANG_FORMAT_ENABLED` for this project |
| 241 | * `META_NO_INSTALL_TARGETS`: the project is not meant to be installed, e.g., a |
| 242 | private test helper; prevents the creation of install targets |
| 243 | * `META_ADD_DEFAULT_CPP_UNIT_TEST_APPLICATION`: implicitly add a source file |
| 244 | with a main()-entry point to the test target for running CppUnit test cases |
| 245 | * `TLS_SUPPORT`: enables TLS support |
| 246 | * enabled by default for projects using Qt Network; should be disabled |
| 247 | when using Qt Network but no TLS is required |
| 248 | * currently only relevant when using static Qt (6.2.0 or newer) to link |
| 249 | against an appropriate TLS plugin for TLS support in Qt Network |
| 250 | * `META_TLS_PLUGINS`: specifies an additional list of Qt TLS plugins to |
| 251 | check before the known TLS plugins |
| 252 | * can be used to give other TLS plugins precedence over the plugins |
| 253 | that are checked by default |
| 254 | |
| 255 | ### Files |
| 256 | * `HEADER_FILES`/`SRC_FILES`: specifies C++ header/source files |
| 257 | * `TEST_HEADER_FILES`/`TEST_SRC_FILES`: specifies C++ header/source files of the |
| 258 | tests |
| 259 | * `TS_FILES`: specifies Qt translations |
| 260 | * `RES_FILES`: specifies Qt resource files |
| 261 | * `DBUS_FILES`: specifies files for Qt DBus |
| 262 | * `WIDGETS_HEADER_FILES`/`WIDGETS_SRC_FILES`: specifies C++ header/source files |
| 263 | only required for the Qt Widgets GUI |
| 264 | * `QML_HEADER_FILES`/`QML_SRC_FILES`/`QML_RES_FILES`: specifies C++ |
| 265 | header/source files and Qt resource files only required for the Qt Quick GUI |
| 266 | * `EXCLUDED_FILES`: specifies C++ files that are part of the project (and therefore |
| 267 | should be formatted by the `tidy` target, added to the documentation, and considered |
| 268 | for translations) but are excluded from the actual build; when adding files |
| 269 | conditionally to the other `_FILES` variables, be sure to add them at least |
| 270 | to `EXCLUDED_FILES` |
| 271 | * `DOC_FILES`: additional markdown files to be included in the documentation |
| 272 | created via Doxygen; the first file is used as the main page |
| 273 | * `REQUIRED_ICONS`: names of the icons required by the application and the |
| 274 | used libraries (can be generated with |
| 275 | [findicons](https://github.com/Martchus/findicons)) |
| 276 | * `CMAKE_MODULE_FILES`/`CMAKE_TEMPLATE_FILES`: specifies CMake modules/templates |
| 277 | provided by the project; those files are installed so they can be used by |
| 278 | other projects |
| 279 | |
| 280 | ### Additional build variables |
| 281 | * `META_PRIVATE_COMPILE_DEFINITIONS`/`META_PUBLIC_COMPILE_DEFINITIONS`: specifies |
| 282 | private/public compile definitions |
| 283 | * `META_PRIVATE_COMPILE_OPTIONS`/`META_PUBLIC_COMPILE_OPTIONS`: specifies |
| 284 | private/public compile options |
| 285 | * `META_SOVERSION`: specifies the soversion for libraries (defaults to |
| 286 | `META_VERSION_MAJOR`) |
| 287 | * `PUBLIC_LIBRARIES`/`PRIVATE_LIBRARIES`: specifies the public/private libraries |
| 288 | to link against |
| 289 | * `PUBLIC_INCLUDE_DIRS`/`PRIVATE_INCLUDE_DIRS`: specifies the public/private include |
| 290 | directories |
| 291 | * `ADDITIONAL_QT_MODULES`: specifies additional Qt modules to link against |
| 292 | * `ADDITIONAL_KF_MODULES`: specifies additional KDE frameworks modules to link |
| 293 | against |
| 294 | * `META_QT_VERSION`: specifies the minimum Qt version to require |
| 295 | |
| 296 | ## Provided modules |
| 297 | c++utilities and qtutilities provide CMake modules to reduce boilerplate code |
| 298 | in the CMake files of my projects. Those modules implement the functionality |
| 299 | controlled by the variables documented above. The most important modules are: |
| 300 | |
| 301 | * `BaseConfig`: does basic configuration, reads most of the `META`-variables |
| 302 | * `QtConfig`: does basic Qt-related configuration, reads most of the Qt-specific |
| 303 | variables documented above |
| 304 | * `QtGuiConfig`: does Qt-related configuration for building a Qt Widgets or |
| 305 | Qt Quick based GUI application/library |
| 306 | * must be included *before* `QtConfig` |
| 307 | * `WebViewProviderConfig`: configures the webview provider |
| 308 | * used by Tag Editor and Syncthing Tray to select between Qt WebEngine, |
| 309 | Qt WebKit, or disabling the built-in webview |
| 310 | * `LibraryTarget`: does further configuration for building dynamic and static |
| 311 | libraries and plugins; `META_PROJECT_TYPE` can be left empty or set explicitly |
| 312 | to `library` |
| 313 | * `AppTarget`: does further configuration for building an application; |
| 314 | `META_PROJECT_TYPE` must be set to `application` |
| 315 | * `ShellCompletion`: enables shell completion |
| 316 | * only works when using the argument parser provided by the |
| 317 | `ApplicationUtilities::ArgumentParser` class, of course |
| 318 | * `TestTarget`: adds the test target `check` |
| 319 | * the `check` target is *not* required by the target `all` |
| 320 | * the test target is comprised of files specified in the `TEST_HEADER_FILES`/`TEST_SRC_FILES` |
| 321 | variables |
| 322 | * the test target will automatically link against `cppunit`, which is the test |
| 323 | framework used by most of my projects; set `META_NO_CPP_UNIT=OFF` in the project |
| 324 | file to prevent this |
| 325 | * `TestUtilities`: provides the `configure_test_target` macro for creating test |
| 326 | targets manually |
| 327 | * see the `CMakeLists.txt` of `qtutilities` for an example |
| 328 | * `AndroidApk`: adds a target to create an APK package for Android using |
| 329 | androiddeployqt; ignored on other platforms |
| 330 | * `Doxygen`: adds a target to generate documentation using Doxygen |
| 331 | * `WindowsResources`: handles the creation of Windows resources to set application |
| 332 | metadata and icon; ignored on other platforms |
| 333 | * `ConfigHeader`: generates `resources/config.h`; must be included as the last |
| 334 | module (when all configuration is done) |
| 335 | |
| 336 | Since those modules make use of the variables explained above, the modules must |
| 337 | be included *after* setting those variables. The inclusion order of the modules |
| 338 | matters as well. |
| 339 | |
| 340 | For an example, check out the project file of c++utilities itself. The project |
| 341 | files of [Syncthing Tray](https://github.com/Martchus/syncthingtray) should |
| 342 | cover everything (library, plugin, application, tests, desktop file, Qt |
| 343 | resources and translations, ...). |