From 771e774b8bc0f032e0abd7f8e9e4bd83119827a0 Mon Sep 17 00:00:00 2001 From: ClausKlein Date: Mon, 9 Sep 2024 10:49:13 +0200 Subject: [PATCH] Change clang-format config file Use "line_width": 99 --- .cmake-format.json | 39 ++++++ .../clang-on-windows-toolchain.cmake | 34 ++--- CMakeGraphVizOptions.cmake | 12 +- CMakeLists.txt | 38 ++---- cmake-format.py => _cmake-format.py | 0 chapter01/CMakeLists.txt | 90 +++---------- chapter01/policy_example/CMakeLists.txt | 31 ++--- chapter02/CMakeLists.txt | 20 +-- chapter02/component1/CMakeLists.txt | 12 +- chapter02/component2/CMakeLists.txt | 12 +- chapter02/component_interface/CMakeLists.txt | 5 +- chapter02/driver_app/CMakeLists.txt | 13 +- chapter02/simple_example/CMakeLists.txt | 4 +- chapter03/CMakeLists.txt | 8 +- chapter03/hello_header_only/CMakeLists.txt | 5 +- chapter03/hello_object_lib/CMakeLists.txt | 11 +- chapter03/hello_shared_lib/CMakeLists.txt | 46 ++----- .../CMakeLists.txt | 14 +- chapter03/hello_static_lib/CMakeLists.txt | 23 ++-- .../hello_world_standalone/CMakeLists.txt | 3 +- chapter04/CMakeLists.txt | 5 +- chapter04/ex01_executable/CMakeLists.txt | 20 ++- chapter04/ex02_static/CMakeLists.txt | 24 ++-- .../ex05_config_file_package/CMakeLists.txt | 71 +++++------ chapter04/ex05_consumer/CMakeLists.txt | 49 +++---- chapter04/ex06_pack/CMakeLists.txt | 20 +-- chapter04/ex06_pack/executable/CMakeLists.txt | 14 +- chapter04/ex06_pack/library/CMakeLists.txt | 23 ++-- .../ex07_pack_nsis_standalone/CMakeLists.txt | 29 ++--- .../executable/CMakeLists.txt | 14 +- .../library/CMakeLists.txt | 23 ++-- chapter05/CMakeLists.txt | 26 ++-- chapter05/conan_example/CMakeLists.txt | 23 +--- chapter05/external_project/CMakeLists.txt | 24 +--- .../fetch_content_example/CMakeLists.txt | 5 +- chapter05/find_module/CMakeLists.txt | 23 +--- chapter05/find_module/cmake/FindObscure.cmake | 47 ++----- chapter05/find_module/dep/CMakeLists.txt | 19 +-- chapter05/find_package_example/CMakeLists.txt | 26 ++-- chapter05/vcpkg_example/CMakeLists.txt | 5 +- chapter06/CMakeLists.txt | 11 +- chapter06/ex01_doxdocgen/CMakeLists.txt | 111 +++++----------- chapter06/ex02_doxplantuml/CMakeLists.txt | 59 +++------ chapter07/clang_tidy_example/CMakeLists.txt | 32 ++--- chapter07/coverage_example/CMakeLists.txt | 25 +--- chapter07/cppcheck_example/CMakeLists.txt | 23 +--- chapter07/cpplint_example/CMakeLists.txt | 24 +--- chapter07/custom_build_type/CMakeLists.txt | 120 +++++------------- chapter07/fixture_example/CMakeLists.txt | 45 ++----- chapter07/iwyu_example/CMakeLists.txt | 17 +-- chapter07/lwyu_example/CMakeLists.txt | 26 +--- chapter07/pass_fail_criteria/CMakeLists.txt | 20 +-- .../resource_group_example/CMakeLists.txt | 56 +++----- .../simple_sanitizer_example/CMakeLists.txt | 18 +-- chapter07/simple_test/CMakeLists.txt | 14 +- .../test_discovery_example/CMakeLists.txt | 9 +- chapter07/test_labels/CMakeLists.txt | 58 ++------- chapter07/timeout_example/CMakeLists.txt | 19 +-- .../CMakeLists.txt | 37 ++---- .../configure_file_example/CMakeLists.txt | 40 ++---- .../create_source_file_example/CMakeLists.txt | 17 +-- .../cmake/WriteMain.cmake | 28 +--- .../custom_command_example/CMakeLists.txt | 10 +- .../cmake/CreateSha256.cmake | 33 +---- .../CMakeLists.txt | 12 +- .../cmake/CreateSha256.cmake | 33 +---- .../custom_target_example/CMakeLists.txt | 21 +-- .../execute_process_example/CMakeLists.txt | 31 ++--- chapter09/docker_minimal/CMakeLists.txt | 5 +- chapter10/ex01_external_deps/CMakeLists.txt | 22 +--- chapter10/ex01_external_deps/superbuild.cmake | 33 ++--- .../CMakeLists.txt | 9 +- .../superbuild.cmake | 29 ++--- chapter10/ex03_simple_qt_app/CMakeLists.txt | 37 ++---- chapter10/ex03_simple_qt_app/superbuild.cmake | 65 +++------- chapter11/CMakeLists.txt | 3 +- chapter11/app_bundle_example/CMakeLists.txt | 12 +- .../CMakeLists.txt | 12 +- .../app_bundle_storyboard/CMakeLists.txt | 16 +-- chapter11/framework_example/CMakeLists.txt | 19 +-- chapter11/hello_world_apple/CMakeLists.txt | 22 +--- chapter12/CMakeLists.txt | 4 +- chapter12/android-example/CMakeLists.txt | 19 +-- chapter12/check_push_pop/CMakeLists.txt | 26 +--- chapter12/emulator-example/CMakeLists.txt | 5 +- .../emulator-example/arm64.toolchain.cmake | 16 +-- chapter12/iOS_example/CMakeLists.txt | 19 +-- chapter12/iOS_example/ios.toolchain.cmake | 20 +-- chapter13/ex01_git_utility/cmake/git.cmake | 21 +-- chapter13/ex02_envfile_utility/CMakeLists.txt | 8 +- .../cmake/envfile-utils.cmake | 66 ++-------- .../test-executable/CMakeLists.txt | 16 +-- .../ex03_external_cmake_module/CMakeLists.txt | 9 +- chapter14/precompiled_headers/CMakeLists.txt | 18 +-- chapter14/unity_build/CMakeLists.txt | 14 +- chapter14/unity_build_group/CMakeLists.txt | 27 +--- requirements.txt | 21 +++ 97 files changed, 745 insertions(+), 1707 deletions(-) create mode 100644 .cmake-format.json rename cmake-format.py => _cmake-format.py (100%) create mode 100644 requirements.txt diff --git a/.cmake-format.json b/.cmake-format.json new file mode 100644 index 0000000..6f0dcea --- /dev/null +++ b/.cmake-format.json @@ -0,0 +1,39 @@ +{ + "format": { + "disable": false, + "line_width": 99, + "tab_size": 4, + "use_tabchars": false, + "fractional_tab_policy": "use-space", + "max_subgroups_hwrap": 4, + "max_pargs_hwrap": 6, + "max_rows_cmdline": 2, + "separate_ctrl_name_with_space": false, + "separate_fn_name_with_space": false, + "dangle_parens": true, + "dangle_align": "prefix", + "min_prefix_chars": 4, + "max_prefix_chars": 10, + "max_lines_hwrap": 2, + "command_case": "lower", + "keyword_case": "upper", + "always_wrap": [ + "add-librariy", + "externalproject_add", + "fetchcontent_declare", + "generate_export_header", + "install", + "project", + "set_property", + "set_target_properties", + "target_include_directories", + "target_link_libraries", + "target_sources" + ], + "enable_sort": true, + "autosort": false + }, + "markup": { + "first_comment_is_literal": true + } +} diff --git a/.github/toolchain/clang-on-windows-toolchain.cmake b/.github/toolchain/clang-on-windows-toolchain.cmake index d49fc25..5893250 100644 --- a/.github/toolchain/clang-on-windows-toolchain.cmake +++ b/.github/toolchain/clang-on-windows-toolchain.cmake @@ -1,36 +1,21 @@ # Copyied from https://github.com/friendlyanon/cmake-init-clang-on-windows cmake_minimum_required(VERSION 3.25...3.30) -set(CMAKE_C_COMPILER - clang -) -set(CMAKE_CXX_COMPILER - clang++ -) +set(CMAKE_C_COMPILER clang) +set(CMAKE_CXX_COMPILER clang++) # Tell Clang the triplet to target -set(CMAKE_C_COMPILER_TARGET - x86_64-pc-windows -) -set(CMAKE_CXX_COMPILER_TARGET - x86_64-pc-windows -) +set(CMAKE_C_COMPILER_TARGET x86_64-pc-windows) +set(CMAKE_CXX_COMPILER_TARGET x86_64-pc-windows) # https://github.com/actions/virtual-environments/blob/main/images/win/Windows2019-Readme.md#workloads-components-and-extensions -set(windows_kit_version - 10.0.21381.0 -) +set(windows_kit_version 10.0.21381.0) # https://github.com/actions/virtual-environments/blob/main/images/win/Windows2019-Readme.md#microsoft-visual-c -set(msvc_version - 14.29.30135 -) +set(msvc_version 14.29.30135) -# You have to find it out on your own by running cl.exe, windows-2019 has this -# one -set(cl_version - 19.29.30137 -) +# You have to find it out on your own by running cl.exe, windows-2019 has this one +set(cl_version 19.29.30137) # Clang needs to use MSVC's system .lib files add_link_options( @@ -48,6 +33,5 @@ include_directories( "C:/Program Files (x86)/Windows Kits/10/include/${windows_kit_version}/um" ) -# Tell Clang what version of cl.exe to emulate, so it sets the defines up -# correctly +# Tell Clang what version of cl.exe to emulate, so it sets the defines up correctly add_compile_options("-fms-compatibility-version=${cl_version}") diff --git a/CMakeGraphVizOptions.cmake b/CMakeGraphVizOptions.cmake index 6842b64..0710092 100644 --- a/CMakeGraphVizOptions.cmake +++ b/CMakeGraphVizOptions.cmake @@ -1,9 +1,3 @@ -set(GRAPHVIZ_GRAPH_NAME - "CMake Best Practices" -) -set(GRAPHVIZ_GENERATE_PER_TARGET - FALSE -) -set(GRAPHVIZ_GENERATE_DEPENDERS - FALSE -) +set(GRAPHVIZ_GRAPH_NAME "CMake Best Practices") +set(GRAPHVIZ_GENERATE_PER_TARGET FALSE) +set(GRAPHVIZ_GENERATE_DEPENDERS FALSE) diff --git a/CMakeLists.txt b/CMakeLists.txt index c465ad1..607d089 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -3,17 +3,16 @@ # # SPDX-License-Identifier: MIT -# Specifies the minimum required CMake version to build this project (and -# subprojects) Subprojects may override this value by specifying another version -# requirement in their CMakeLists file (e.g. require a later version of CMake) +# Specifies the minimum required CMake version to build this project (and subprojects) Subprojects +# may override this value by specifying another version requirement in their CMakeLists file (e.g. +# require a later version of CMake) cmake_minimum_required(VERSION 3.25...3.30) # The main project project( "CMakeBestPractices" VERSION 1.0 - DESCRIPTION - "The 'build-all' instructions for all examples for the book CMake Best Practices" + DESCRIPTION "The 'build-all' instructions for all examples for the book CMake Best Practices" LANGUAGES CXX ) @@ -55,22 +54,14 @@ if(NOT DEFINED CMAKE_CXX_STANDARD) endif() # cmake-format: on -# Set the default build type for single-config generators. The build type -# variable is still overridable from outside. -set(CMAKE_BUILD_TYPE - "Debug" - CACHE STRING "Default build type for single-config generators" -) +# Set the default build type for single-config generators. The build type variable is still +# overridable from outside. +set(CMAKE_BUILD_TYPE "Debug" CACHE STRING "Default build type for single-config generators") -set(CMAKE_EXPORT_COMPILE_COMMANDS - TRUE -) +set(CMAKE_EXPORT_COMPILE_COMMANDS TRUE) # Print generator type and build type (if applicable) -get_property( - is_multi_config_generator GLOBAL - PROPERTY GENERATOR_IS_MULTI_CONFIG -) +get_property(is_multi_config_generator GLOBAL PROPERTY GENERATOR_IS_MULTI_CONFIG) if(NOT is_multi_config_generator) message(STATUS "Using a single-config generator (${CMAKE_GENERATOR})") message(STATUS "Current build type is `${CMAKE_BUILD_TYPE}`") @@ -80,12 +71,8 @@ endif() find_program(CCACHE_PROGRAM ccache) if(CCACHE_PROGRAM) - set(CMAKE_C_COMPILER_LAUNCHER - ${CCACHE_PROGRAM} - ) - set(CMAKE_CXX_COMPILER_LAUNCHER - ${CCACHE_PROGRAM} - ) + set(CMAKE_C_COMPILER_LAUNCHER ${CCACHE_PROGRAM}) + set(CMAKE_CXX_COMPILER_LAUNCHER ${CCACHE_PROGRAM}) endif() # Add chapter examples to root project @@ -97,8 +84,7 @@ add_subdirectory(chapter05) add_subdirectory(chapter06) add_subdirectory(chapter07) add_subdirectory(chapter08) -# the chapter 10 examples are intended to be built standalone -# add_subdirectory(chapter10) +# the chapter 10 examples are intended to be built standalone add_subdirectory(chapter10) # add chapter 11 examples only on Apple platforms if(APPLE) diff --git a/cmake-format.py b/_cmake-format.py similarity index 100% rename from cmake-format.py rename to _cmake-format.py diff --git a/chapter01/CMakeLists.txt b/chapter01/CMakeLists.txt index 3bc6423..ced5ad8 100644 --- a/chapter01/CMakeLists.txt +++ b/chapter01/CMakeLists.txt @@ -15,20 +15,13 @@ project( add_subdirectory(simple_executable) add_subdirectory(policy_example) -option( - CHAPTER1_PRINT_LANGUAGE_EXAMPLES - "Print examples for each language" - OFF -) +option(CHAPTER1_PRINT_LANGUAGE_EXAMPLES "Print examples for each language" OFF) include(CMakeDependentOption) # create an option CHAPTER1_PRINT_HELLO_WORLD that is only visible of # CHAPTER1_PRINT_LANGUAGE_EXAMPLES is set to ON cmake_dependent_option( - CHAPTER1_PRINT_HELLO_WORLD - "Print a nice greeting from chapter1" - ON - CHAPTER1_PRINT_LANGUAGE_EXAMPLES - ON + CHAPTER1_PRINT_HELLO_WORLD "Print a nice greeting from chapter1" ON + CHAPTER1_PRINT_LANGUAGE_EXAMPLES ON ) mark_as_advanced(CHAPTER1_PRINT_LANGUAGE_EXAMPLES) @@ -37,90 +30,41 @@ if(CHAPTER1_PRINT_LANGUAGE_EXAMPLES) message(STATUS "Chapter1: welcome to chapter 1!") endif() # examples for list operations - set(MYLIST - abc - def - ghi - ) + set(MYLIST abc def ghi) message(STATUS "Chapter1: MYLIST is: ${MYLIST}") - set(MYLIST - "abc;def;ghi" - ) + set(MYLIST "abc;def;ghi") message(STATUS "Chapter1: MYLIST is: ${MYLIST}") - list( - APPEND - MYLIST - "xyz" - ) + list(APPEND MYLIST "xyz") message(STATUS "Chapter1: MYLIST is: ${MYLIST}") # find the string abc in MYLIST and store it in ABC_INDEX - list( - FIND - MYLIST - abc - ABC_INDEX - ) - list( - GET - MYLIST - ${ABC_INDEX} - ABC - ) + list(FIND MYLIST abc ABC_INDEX) + list(GET MYLIST ${ABC_INDEX} ABC) message( STATUS "Chapter1: Index of 'def' in MYLIST is ${ABC_INDEX} the value at the index is '${ABC}'" ) # add MYVAR as cache variable with the value foo - set(ch1_MYVAR - foo - CACHE STRING "Variable foo that configures bar" - ) + set(ch1_MYVAR foo CACHE STRING "Variable foo that configures bar") # set a property for this directory - set_property( - DIRECTORY - PROPERTY CHAPTER1_PROPERTY "Hello from chapter 1" - ) - get_property( - CH1_PROPERTY_VARIABLE - DIRECTORY - PROPERTY CHAPTER1_PROPERTY - ) - message( - STATUS - "Chapter1: Property value for CHAPTER1_PROPERTy is '${CH1_PROPERTY_VARIABLE}'" - ) + set_property(DIRECTORY PROPERTY CHAPTER1_PROPERTY "Hello from chapter 1") + get_property(CH1_PROPERTY_VARIABLE DIRECTORY PROPERTY CHAPTER1_PROPERTY) + message(STATUS "Chapter1: Property value for CHAPTER1_PROPERTy is '${CH1_PROPERTY_VARIABLE}'") # loop examples - set(MYVAR - 0 - ) - while( - MYVAR - LESS - "5" - ) + set(MYVAR 0) + while(MYVAR LESS "5") message(STATUS "Chapter1: MYVAR is '${MYVAR}'") - math( - EXPR - MYVAR - "${MYVAR}+1" - ) + math(EXPR MYVAR "${MYVAR}+1") endwhile() - foreach( - ITEM IN - LISTS MYLIST - ) + foreach(ITEM IN LISTS MYLIST) message(STATUS "Chapter1: ITEM from MYLIST is '${ITEM}'") endforeach() - foreach( - ITEM - RANGE 0 5 - ) + foreach(ITEM RANGE 0 5) message(STATUS "Chapter1: ITEM from RANGE is '${ITEM}'") endforeach() diff --git a/chapter01/policy_example/CMakeLists.txt b/chapter01/policy_example/CMakeLists.txt index 6be9670..bdd9a53 100644 --- a/chapter01/policy_example/CMakeLists.txt +++ b/chapter01/policy_example/CMakeLists.txt @@ -7,31 +7,16 @@ cmake_minimum_required(VERSION 3.25...3.30) project( ch1_policy_example VERSION 1.0 - DESCRIPTION - "A simple C++ project to demonstrate creating a standalone executables" + DESCRIPTION "A simple C++ project to demonstrate creating a standalone executables" LANGUAGES CXX ) -# set policy CMP0121 to the old behavior. CMP0121 is a policy that detects if -# index variables for the list() commands are in a valid format this policy was -# introduced with cmake varsion 3.21 -cmake_policy( - SET - CMP0121 - OLD -) +# set policy CMP0121 to the old behavior. CMP0121 is a policy that detects if index variables for +# the list() commands are in a valid format this policy was introduced with cmake varsion 3.21 +cmake_policy(SET CMP0121 OLD) # Create a sample list -list( - APPEND - MYLIST - "abc;def;ghi" -) -# This command will fail if the policy is set to NEW or if the call to -# cmake_policy above is removed as CMake 3.21 sets this policy automatically -list( - GET - MYLIST - "any" - OUT_VAR -) +list(APPEND MYLIST "abc;def;ghi") +# This command will fail if the policy is set to NEW or if the call to cmake_policy above is +# removed as CMake 3.21 sets this policy automatically +list(GET MYLIST "any" OUT_VAR) diff --git a/chapter02/CMakeLists.txt b/chapter02/CMakeLists.txt index ed2d07a..a50c636 100644 --- a/chapter02/CMakeLists.txt +++ b/chapter02/CMakeLists.txt @@ -14,27 +14,20 @@ project( message(STATUS "Current build type is `${CMAKE_BUILD_TYPE}`") # Option to exclude driver application from build. -set(CHAPTER2_BUILD_DRIVER_APPLICATION - TRUE +set(CHAPTER2_BUILD_DRIVER_APPLICATION TRUE CACHE BOOL "Whether to include driver application in build. Default: True" ) # Hide this option from GUI's by default. mark_as_advanced(CHAPTER2_BUILD_DRIVER_APPLICATION) -# MSVC requires these two flags to be set in order to link driver executable -# against shared library. The topic is about symbol visibility will be further -# discussed in Chapter 3. -if(CMAKE_SYSTEM_NAME - STREQUAL - "Windows" -) +# MSVC requires these two flags to be set in order to link driver executable against shared +# library. The topic is about symbol visibility will be further discussed in Chapter 3. +if(CMAKE_SYSTEM_NAME STREQUAL "Windows") message( VERBOSE "Windows environment detected, overriding default symbol visibility to `visible` instead of `hidden`" ) - set(CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS - TRUE - ) + set(CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS TRUE) endif() add_subdirectory(simple_example) @@ -42,8 +35,7 @@ add_subdirectory(component_interface) add_subdirectory(component1) add_subdirectory(component2) -# Include driver_app subdirectory if CHAPTER2_BUILD_DRIVER_APPLICATION flag is -# enabled. +# Include driver_app subdirectory if CHAPTER2_BUILD_DRIVER_APPLICATION flag is enabled. if(CHAPTER2_BUILD_DRIVER_APPLICATION) add_subdirectory(driver_app) endif() diff --git a/chapter02/component1/CMakeLists.txt b/chapter02/component1/CMakeLists.txt index e36048f..2b8c21a 100644 --- a/chapter02/component1/CMakeLists.txt +++ b/chapter02/component1/CMakeLists.txt @@ -12,8 +12,8 @@ project( add_library( ch2_framework_component1 STATIC -) # Create a static library target named ch2_framework_component1 (retrieved - # from project name variable) +) # Create a static library target named ch2_framework_component1 (retrieved from project name + # variable) target_include_directories( ch2_framework_component1 PUBLIC ${CMAKE_CURRENT_LIST_DIR}/include/ @@ -21,17 +21,13 @@ target_include_directories( target_sources( ch2_framework_component1 PRIVATE src/component1.cpp -) # Add src/component1.cpp file as sources to the target with PRIVATE - # visibility. +) # Add src/component1.cpp file as sources to the target with PRIVATE visibility. target_link_libraries( ch2_framework_component1 PUBLIC ch2_framework_component_interface ) # Link against ch2_framework_component_interface -target_compile_features( - ch2_framework_component1 - PUBLIC cxx_std_17 -) +target_compile_features(ch2_framework_component1 PUBLIC cxx_std_17) # Make specified target(s) installable. Separate them to components. install( diff --git a/chapter02/component2/CMakeLists.txt b/chapter02/component2/CMakeLists.txt index 5aa808d..684330a 100644 --- a/chapter02/component2/CMakeLists.txt +++ b/chapter02/component2/CMakeLists.txt @@ -11,8 +11,8 @@ project( add_library( ch2_framework_component2 SHARED -) # Create a shared library target named ch2_framework_component2 (retrieved - # from project name variable) +) # Create a shared library target named ch2_framework_component2 (retrieved from project name + # variable) target_include_directories( ch2_framework_component2 PUBLIC ${CMAKE_CURRENT_LIST_DIR}/include/ @@ -20,17 +20,13 @@ target_include_directories( target_sources( ch2_framework_component2 PRIVATE src/component2.cpp -) # Add src/component2.cpp file as sources to the target with PRIVATE - # visibility. +) # Add src/component2.cpp file as sources to the target with PRIVATE visibility. target_link_libraries( ch2_framework_component2 PUBLIC ch2_framework_component_interface ) # Link against ch2_framework_component_interface -target_compile_features( - ch2_framework_component2 - PUBLIC cxx_std_17 -) +target_compile_features(ch2_framework_component2 PUBLIC cxx_std_17) # Make specified target(s) installable. Separate them to components. install( diff --git a/chapter02/component_interface/CMakeLists.txt b/chapter02/component_interface/CMakeLists.txt index ccc9288..803c1de 100644 --- a/chapter02/component_interface/CMakeLists.txt +++ b/chapter02/component_interface/CMakeLists.txt @@ -15,10 +15,7 @@ target_include_directories( INTERFACE include/ ) -target_compile_features( - ch2_framework_component_interface - INTERFACE cxx_std_17 -) +target_compile_features(ch2_framework_component_interface INTERFACE cxx_std_17) # Make specified target(s) installable. Separate them to components. install( diff --git a/chapter02/driver_app/CMakeLists.txt b/chapter02/driver_app/CMakeLists.txt index 133737b..e68f7c0 100644 --- a/chapter02/driver_app/CMakeLists.txt +++ b/chapter02/driver_app/CMakeLists.txt @@ -2,9 +2,8 @@ # # SPDX-License-Identifier: MIT -# Project directive is used for creating a child project for the targets we're -# going to create for the component. This will allow us to use project() -# variables in target creation. +# Project directive is used for creating a child project for the targets we're going to create for +# the component. This will allow us to use project() variables in target creation. project( ch2_driver_application VERSION 1.0 @@ -12,8 +11,7 @@ project( LANGUAGES CXX ) -add_executable(ch2_driver_application) # Create an executable target named with - # the same name as +add_executable(ch2_driver_application) # Create an executable target named with the same name as # project name (ch2.driver_application) target_sources( ch2_driver_application @@ -25,10 +23,7 @@ target_link_libraries( PRIVATE ch2_framework_component1 ch2_framework_component2 ) # Link target against ch2_framework_component1 and ch2_framework_component2 -target_compile_features( - ch2_driver_application - PUBLIC cxx_std_17 -) +target_compile_features(ch2_driver_application PUBLIC cxx_std_17) install( TARGETS ch2_driver_application diff --git a/chapter02/simple_example/CMakeLists.txt b/chapter02/simple_example/CMakeLists.txt index 5156f5e..e3b3860 100644 --- a/chapter02/simple_example/CMakeLists.txt +++ b/chapter02/simple_example/CMakeLists.txt @@ -13,9 +13,7 @@ project( ) # Generate compile_commands.json for better IDE integration -set(CMAKE_EXPORT_COMPILE_COMMANDS - ON -) +set(CMAKE_EXPORT_COMPILE_COMMANDS ON) # Add an executable target named `chapter2_simple_example` add_executable(chapter2_simple_example) diff --git a/chapter03/CMakeLists.txt b/chapter03/CMakeLists.txt index d00c69f..68a3509 100644 --- a/chapter03/CMakeLists.txt +++ b/chapter03/CMakeLists.txt @@ -27,11 +27,9 @@ target_sources( chapter3 PRIVATE src/main.cpp ) -# link libraries "hello" and "hello_header_only" to the example executable the -# libraries are described in the subdirectories +# link libraries "hello" and "hello_header_only" to the example executable the libraries are +# described in the subdirectories target_link_libraries( chapter3 - PRIVATE ch3_hello_header_only - ch3_hello_shared - ch3_hello_object + PRIVATE ch3_hello_header_only ch3_hello_shared ch3_hello_object ) diff --git a/chapter03/hello_header_only/CMakeLists.txt b/chapter03/hello_header_only/CMakeLists.txt index 90dfab9..c6af138 100644 --- a/chapter03/hello_header_only/CMakeLists.txt +++ b/chapter03/hello_header_only/CMakeLists.txt @@ -20,7 +20,4 @@ target_include_directories( INTERFACE include/ ) # expose the minimum C++ standard needed to compile this library -target_compile_features( - ch3_hello_header_only - INTERFACE cxx_std_17 -) +target_compile_features(ch3_hello_header_only INTERFACE cxx_std_17) diff --git a/chapter03/hello_object_lib/CMakeLists.txt b/chapter03/hello_object_lib/CMakeLists.txt index 791cea5..15139ba 100644 --- a/chapter03/hello_object_lib/CMakeLists.txt +++ b/chapter03/hello_object_lib/CMakeLists.txt @@ -7,8 +7,7 @@ cmake_minimum_required(VERSION 3.25...3.30) project( ch3_hello_object VERSION 1.0.0 - DESCRIPTION - "A simple C++ project to demonstrate creating executables and libraries in CMake" + DESCRIPTION "A simple C++ project to demonstrate creating executables and libraries in CMake" LANGUAGES CXX ) @@ -21,12 +20,8 @@ target_sources( PRIVATE src/hello_object.cpp ) -# define the C++ standard needed to compile this library and make it visible to -# dependers -target_compile_features( - ch3_hello_object - PUBLIC cxx_std_17 -) +# define the C++ standard needed to compile this library and make it visible to dependers +target_compile_features(ch3_hello_object PUBLIC cxx_std_17) # set the include directories target_include_directories( diff --git a/chapter03/hello_shared_lib/CMakeLists.txt b/chapter03/hello_shared_lib/CMakeLists.txt index 9d2761b..ee025b8 100644 --- a/chapter03/hello_shared_lib/CMakeLists.txt +++ b/chapter03/hello_shared_lib/CMakeLists.txt @@ -9,33 +9,23 @@ cmake_minimum_required(VERSION 3.25...3.30) project( ch3_hello_shared VERSION 1.0.0 - DESCRIPTION - "A simple C++ project to demonstrate creating executables and libraries in CMake" + DESCRIPTION "A simple C++ project to demonstrate creating executables and libraries in CMake" LANGUAGES CXX ) -# set the postfix "d" for the resulting .so or .dll files when building the -# library in debug mode -set(CMAKE_DEBUG_POSTFIX - d -) +# set the postfix "d" for the resulting .so or .dll files when building the library in debug mode +set(CMAKE_DEBUG_POSTFIX d) # add the library target add_library(ch3_hello_shared SHARED) # Add an alis for the library target -add_library( - ch3_hello_shared::ch3_hello_shared - ALIAS - ch3_hello_shared -) +add_library(ch3_hello_shared::ch3_hello_shared ALIAS ch3_hello_shared) -# set properties for the target. VERSION set the library version to the project -# version * SOVERSION set the compatibility version for the library to the -# major number of the version +# set properties for the target. VERSION set the library version to the project version * SOVERSION +# set the compatibility version for the library to the major number of the version set_target_properties( ch3_hello_shared - PROPERTIES VERSION ${PROJECT_VERSION} - SOVERSION ${PROJECT_VERSION_MAJOR} + PROPERTIES VERSION ${PROJECT_VERSION} SOVERSION ${PROJECT_VERSION_MAJOR} ) # add sources to the library target @@ -44,12 +34,8 @@ target_sources( PRIVATE src/hello.cpp src/internal.cpp ) -# define the C++ standard needed to compile this library and make it visible to -# dependers -target_compile_features( - ch3_hello_shared - PUBLIC cxx_std_17 -) +# define the C++ standard needed to compile this library and make it visible to dependers +target_compile_features(ch3_hello_shared PUBLIC cxx_std_17) # set the include directories target_include_directories( @@ -69,20 +55,16 @@ set_property( TARGET ch3_hello_shared PROPERTY VISIBILITY_INLINES_HIDDEN TRUE ) -# this command generates a header file in the CMAKE_CURRENT_BINARY_DIR which -# sets the visibility attributes according to the compiler settings +# this command generates a header file in the CMAKE_CURRENT_BINARY_DIR which sets the visibility +# attributes according to the compiler settings generate_export_header( - ch3_hello_shared - EXPORT_FILE_NAME - export/hello/export_hello.hpp + ch3_hello_shared EXPORT_FILE_NAME export/hello/export_hello.hpp ) -# Add CMAKE_CURRENT_BINARY_DIR to the include path so the generated header can -# be found +# Add CMAKE_CURRENT_BINARY_DIR to the include path so the generated header can be found target_include_directories( ch3_hello_shared PUBLIC "${CMAKE_CURRENT_BINARY_DIR}/export" ) # An alternative but not recommended way export the symbols is to use -# set(CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS TRUE) Which exports all dll symbols by -# default +# set(CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS TRUE) Which exports all dll symbols by default diff --git a/chapter03/hello_simple_library_example/CMakeLists.txt b/chapter03/hello_simple_library_example/CMakeLists.txt index af7818b..f899fdc 100644 --- a/chapter03/hello_simple_library_example/CMakeLists.txt +++ b/chapter03/hello_simple_library_example/CMakeLists.txt @@ -7,27 +7,19 @@ cmake_minimum_required(VERSION 3.25...3.30) project( ch3_hello VERSION 1.0.0 - DESCRIPTION - "A simple C++ project to demonstrate creating executables and libraries in CMake" + DESCRIPTION "A simple C++ project to demonstrate creating executables and libraries in CMake" LANGUAGES CXX ) # add the library target add_library(ch3_hello) -add_library( - ch3_hello::ch3_hello - ALIAS - ch3_hello -) +add_library(ch3_hello::ch3_hello ALIAS ch3_hello) target_sources( ch3_hello PRIVATE src/hello.cpp ) -target_compile_features( - ch3_hello - PUBLIC cxx_std_17 -) +target_compile_features(ch3_hello PUBLIC cxx_std_17) # set the include directories target_include_directories( diff --git a/chapter03/hello_static_lib/CMakeLists.txt b/chapter03/hello_static_lib/CMakeLists.txt index c826778..372476e 100644 --- a/chapter03/hello_static_lib/CMakeLists.txt +++ b/chapter03/hello_static_lib/CMakeLists.txt @@ -7,28 +7,25 @@ cmake_minimum_required(VERSION 3.25...3.30) project( ch3_hello_static VERSION 1.0.0 - DESCRIPTION - "A simple C++ project to demonstrate creating executables and libraries in CMake" + DESCRIPTION "A simple C++ project to demonstrate creating executables and libraries in CMake" LANGUAGES CXX ) # add the library target add_library(ch3_hello_static STATIC) -# Set the output of the target to `hello` so it will be saved as hello.dll on -# windows and libhello.a on linux +# Set the output of the target to `hello` so it will be saved as hello.dll on windows and +# libhello.a on linux set_target_properties( ch3_hello_static PROPERTIES OUTPUT_NAME hello ) -# set properties for the target. VERSION set the library version to the project -# version * SOVERSION set the compatibility version for the library to the -# major number of the version +# set properties for the target. VERSION set the library version to the project version * SOVERSION +# set the compatibility version for the library to the major number of the version set_target_properties( ch3_hello_static - PROPERTIES VERSION ${PROJECT_VERSION} - SOVERSION ${PROJECT_VERSION_MAJOR} + PROPERTIES VERSION ${PROJECT_VERSION} SOVERSION ${PROJECT_VERSION_MAJOR} ) # add sources to the library target @@ -37,12 +34,8 @@ target_sources( PRIVATE src/hello.cpp src/internal.cpp ) -# define the C++ standard needed to compile this library and make it visible to -# dependers -target_compile_features( - ch3_hello_static - PUBLIC cxx_std_17 -) +# define the C++ standard needed to compile this library and make it visible to dependers +target_compile_features(ch3_hello_static PUBLIC cxx_std_17) # set the include directories target_include_directories( diff --git a/chapter03/hello_world_standalone/CMakeLists.txt b/chapter03/hello_world_standalone/CMakeLists.txt index 2a4cf82..ffe2ee6 100644 --- a/chapter03/hello_world_standalone/CMakeLists.txt +++ b/chapter03/hello_world_standalone/CMakeLists.txt @@ -7,8 +7,7 @@ cmake_minimum_required(VERSION 3.25...3.30) project( ch3_hello_world_standalone VERSION 1.0 - DESCRIPTION - "A simple C++ project to demonstrate creating a standalone executables" + DESCRIPTION "A simple C++ project to demonstrate creating a standalone executables" LANGUAGES CXX ) diff --git a/chapter04/CMakeLists.txt b/chapter04/CMakeLists.txt index b325338..b30311c 100644 --- a/chapter04/CMakeLists.txt +++ b/chapter04/CMakeLists.txt @@ -17,6 +17,5 @@ add_subdirectory(ex02_static) add_subdirectory(ex03_file) add_subdirectory(ex04_directory) add_subdirectory(ex05_config_file_package) -# ex05_consumer, ex06_pack, ex07_pack_nsis_standalone are intentionally left out -# of the project since they are intended be standalone projects independent from -# the root project. +# ex05_consumer, ex06_pack, ex07_pack_nsis_standalone are intentionally left out of the project +# since they are intended be standalone projects independent from the root project. diff --git a/chapter04/ex01_executable/CMakeLists.txt b/chapter04/ex01_executable/CMakeLists.txt index 68a6f12..342ef26 100644 --- a/chapter04/ex01_executable/CMakeLists.txt +++ b/chapter04/ex01_executable/CMakeLists.txt @@ -21,15 +21,13 @@ target_sources( PRIVATE src/main.cpp ) -# Request compile features for target named `ch4_ex01_executable`. Requested -# `cxx_std_17` feature determines the minimum C++ standard required to build -# this target. It's PRIVATE, since it is a build-time requirement only. -target_compile_features( - ch4_ex01_executable - PRIVATE cxx_std_17 -) +# Request compile features for target named `ch4_ex01_executable`. Requested `cxx_std_17` feature +# determines the minimum C++ standard required to build this target. It's PRIVATE, since it is a +# build-time requirement only. +target_compile_features(ch4_ex01_executable PRIVATE cxx_std_17) -# Make executable target `ch4_ex01_executable` installable. As mentioned before -# in Chapter 4 content, this will only install the output artifacts produced by -# the target. -install(TARGETS ch4_ex01_executable) +# Make executable target `ch4_ex01_executable` installable. As mentioned before in Chapter 4 +# content, this will only install the output artifacts produced by the target. +install( + TARGETS ch4_ex01_executable +) diff --git a/chapter04/ex02_static/CMakeLists.txt b/chapter04/ex02_static/CMakeLists.txt index c80cdfe..b9f9ff9 100644 --- a/chapter04/ex02_static/CMakeLists.txt +++ b/chapter04/ex02_static/CMakeLists.txt @@ -27,23 +27,21 @@ target_include_directories( PUBLIC include ) -# Request compile features for target named `ch4_ex02_static`. Requested -# `cxx_std_17` feature determines the minimum C++ standard required to build -# this target. It's PRIVATE, since it is a build-time requirement only. -target_compile_features( - ch4_ex02_static - PRIVATE cxx_std_17 -) +# Request compile features for target named `ch4_ex02_static`. Requested `cxx_std_17` feature +# determines the minimum C++ standard required to build this target. It's PRIVATE, since it is a +# build-time requirement only. +target_compile_features(ch4_ex02_static PRIVATE cxx_std_17) include(GNUInstallDirs) # Defines the ${CMAKE_INSTALL_INCLUDEDIR} variable. -# Make executable target `ch4_ex02_static` installable. As mentioned before in -# Chapter 4 content, this will only install the output artifacts produced by the -# target. -install(TARGETS ch4_ex02_static) +# Make executable target `ch4_ex02_static` installable. As mentioned before in Chapter 4 content, +# this will only install the output artifacts produced by the target. +install( + TARGETS ch4_ex02_static +) -# Install the header files. Since header files are not listed as output -# artifacts, they have to be installed separately. +# Install the header files. Since header files are not listed as output artifacts, they have to be +# installed separately. install( DIRECTORY include/ DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}" diff --git a/chapter04/ex05_config_file_package/CMakeLists.txt b/chapter04/ex05_config_file_package/CMakeLists.txt index f14ce59..312b925 100644 --- a/chapter04/ex05_config_file_package/CMakeLists.txt +++ b/chapter04/ex05_config_file_package/CMakeLists.txt @@ -14,10 +14,9 @@ project( include(GNUInstallDirs) # Defines the ${CMAKE_INSTALL_INCLUDEDIR} variable. -# This is the default install directory for config-file package cmake files It -# is relative to install prefix. -set(ch4_ex05_lib_INSTALL_CMAKEDIR - cmake +# This is the default install directory for config-file package cmake files It is relative to +# install prefix. +set(ch4_ex05_lib_INSTALL_CMAKEDIR cmake CACHE PATH "Installation directory for config-file package cmake files" ) @@ -38,45 +37,36 @@ target_sources( ) # cmake-format: on -# Files in a PRIVATE or PUBLIC FILE_SET set are marked as source files for the -# purposes of IDE integration. Additionally, files in HEADERS file sets have -# their HEADER_FILE_ONLY property set to TRUE. Files in an INTERFACE or PUBLIC -# FILE_SET can be installed with the install(TARGETS) command, and exported with -# the install(EXPORT) and export() commands. +# Files in a PRIVATE or PUBLIC FILE_SET set are marked as source files for the purposes of IDE +# integration. Additionally, files in HEADERS file sets have their HEADER_FILE_ONLY property set to +# TRUE. Files in an INTERFACE or PUBLIC FILE_SET can be installed with the install(TARGETS) +# command, and exported with the install(EXPORT) and export() commands. -# Request compile features for target named `ch4_ex05_lib`. Requested -# `cxx_std_17` feature determines the minimum C++ standard required to build -# this target. It's PRIVATE, since it is a build-time requirement only. -target_compile_features( - ch4_ex05_lib - PUBLIC cxx_std_17 -) +# Request compile features for target named `ch4_ex05_lib`. Requested `cxx_std_17` feature +# determines the minimum C++ standard required to build this target. It's PRIVATE, since it is a +# build-time requirement only. +target_compile_features(ch4_ex05_lib PUBLIC cxx_std_17) -# Until now, it is just regular target definition ... interesting stuff begins -# after this line +# Until now, it is just regular target definition ... interesting stuff begins after this line -# Make executable target `ch4_ex05_lib` installable. As mentioned before in -# Chapter 4 content, this will only install the output artifacts produced by the -# target. +# Make executable target `ch4_ex05_lib` installable. As mentioned before in Chapter 4 content, this +# will only install the output artifacts produced by the target. install( TARGETS ch4_ex05_lib EXPORT - ch4_ex05_lib_export # Associate installed target files of the targets in - # TARGETS parameter with export name of - # ch4_ex05_lib_export Note that this does not export - # anything yet, it just defines a name and context - # for the exports. The install(EXPORT) call does the - # actual target exporting. + ch4_ex05_lib_export # Associate installed target files of the targets in TARGETS parameter + # with export name of ch4_ex05_lib_export Note that this does not + # export anything yet, it just defines a name and context for the + # exports. The install(EXPORT) call does the actual target exporting. # Will be appended to INTERFACE_INCLUDE_DIRECTORIES of exported targets. INCLUDES FILE_SET HEADERS DESTINATION ${CMAKE_INSTALL_INCLUDEDIR} ) -# Generate the required import code for the content in into -# ch4_ex05_lib-config.cmake CMake file. Prefix all target names with -# ch4_05_lib:: while doing so (NAMESPACE). DESTINATION argument recommended to -# be a path that will be picked by find_package() by default ('cmake' in our -# example). +# Generate the required import code for the content in into ch4_ex05_lib-config.cmake +# CMake file. Prefix all target names with ch4_05_lib:: while doing so (NAMESPACE). DESTINATION +# argument recommended to be a path that will be picked by find_package() by default ('cmake' in +# our example). install( EXPORT ch4_ex05_lib_export FILE ch4_ex05_lib-config.cmake @@ -87,21 +77,18 @@ install( # Defines write_basic_package_version_file include(CMakePackageConfigHelpers) -# Create a package version file for the package. This will allow consumers to -# specify version argument to select between several versions of the package via -# specifying VERSION argument to find_package(). We're not explicitly specifying -# the VERSION parameter to allow `write_basic_package_version_file` to get it -# from project(...) version (which is 1.0.1) +# Create a package version file for the package. This will allow consumers to specify version +# argument to select between several versions of the package via specifying VERSION argument to +# find_package(). We're not explicitly specifying the VERSION parameter to allow +# `write_basic_package_version_file` to get it from project(...) version (which is 1.0.1) write_basic_package_version_file( "ch4_ex05_lib-config-version.cmake" - # Package compatibility strategy. SameMajorVersion is essentially `semantic - # versioning`. + # Package compatibility strategy. SameMajorVersion is essentially `semantic versioning`. COMPATIBILITY SameMajorVersion ) -# Install command for deploying Config-file package files into the target -# system. It must be present in the same directory as -# `ch4_ex05_lib-config.cmake` file. +# Install command for deploying Config-file package files into the target system. It must be +# present in the same directory as `ch4_ex05_lib-config.cmake` file. install( FILES "${CMAKE_CURRENT_BINARY_DIR}/ch4_ex05_lib-config-version.cmake" DESTINATION "${ch4_ex05_lib_INSTALL_CMAKEDIR}" diff --git a/chapter04/ex05_consumer/CMakeLists.txt b/chapter04/ex05_consumer/CMakeLists.txt index e2f6e74..0b7dafa 100644 --- a/chapter04/ex05_consumer/CMakeLists.txt +++ b/chapter04/ex05_consumer/CMakeLists.txt @@ -8,8 +8,7 @@ cmake_minimum_required(VERSION 3.25...3.30) project( ch4_ex05_consumer VERSION 1.0 - DESCRIPTION - "Chapter 4 Example 05, Out ot project config-file package consumer" + DESCRIPTION "Chapter 4 Example 05, Out ot project config-file package consumer" LANGUAGES CXX ) @@ -20,41 +19,27 @@ if(NOT PROJECT_IS_TOP_LEVEL) ) endif() -# ############################################################################## -# IMPORTANT NOTE: If ch4_ex05_lib is installed on a non-default install prefix -# this project must be configured with -DCMAKE_PREFIX_PATH:STRING= variable. This enables find_package() to discover the package even -# though it is installed to a custom prefix. -# ############################################################################## - -# Let's find our package. We're explicitly specifying CONFIG argument to not to -# search for MODULE packages. CMake will fail if package is not present since -# REQUIRED argument is given. Also, we're explicitly requesting major version of -# 1. We are able to do this since we've provided a -# -config-version.cmake file too. The packages with no -# -config-version.cmake file are not considered by find_package() when version -# is specified. -find_package( - ch4_ex05_lib - 1 - CONFIG - REQUIRED -) +# ################################################################################################# +# IMPORTANT NOTE: If ch4_ex05_lib is installed on a non-default install prefix this project must be +# configured with -DCMAKE_PREFIX_PATH:STRING= variable. This enables find_package() +# to discover the package even though it is installed to a custom prefix. +# ################################################################################################# + +# Let's find our package. We're explicitly specifying CONFIG argument to not to search for MODULE +# packages. CMake will fail if package is not present since REQUIRED argument is given. Also, we're +# explicitly requesting major version of 1. We are able to do this since we've provided a +# -config-version.cmake file too. The packages with no -config-version.cmake file are not +# considered by find_package() when version is specified. +find_package(ch4_ex05_lib 1 CONFIG REQUIRED) # Define the executable that will consume the ch4_ex05_lib package -add_executable( - ch4_ex05_consumer - src/main.cpp -) +add_executable(ch4_ex05_consumer src/main.cpp) # Set the required C++ standard for the target -target_compile_features( - ch4_ex05_consumer - PRIVATE cxx_std_17 -) +target_compile_features(ch4_ex05_consumer PRIVATE cxx_std_17) -# Link executable to ch4_ex05_lib package found by find_package() before. Target -# is under `ch4_ex05_lib::` namespace since export is made using that namespace. +# Link executable to ch4_ex05_lib package found by find_package() before. Target is under +# `ch4_ex05_lib::` namespace since export is made using that namespace. target_link_libraries( ch4_ex05_consumer PRIVATE ch4_ex05_lib::ch4_ex05_lib diff --git a/chapter04/ex06_pack/CMakeLists.txt b/chapter04/ex06_pack/CMakeLists.txt index 933ed8e..d2b19b9 100644 --- a/chapter04/ex06_pack/CMakeLists.txt +++ b/chapter04/ex06_pack/CMakeLists.txt @@ -22,26 +22,18 @@ endif() add_subdirectory(executable) add_subdirectory(library) -# We will not explicitly specify project name and version here and let CPack to -# get project name and version from the project() -set(CPACK_PACKAGE_VENDOR - "CBP Authors" -) +# We will not explicitly specify project name and version here and let CPack to get project name +# and version from the project() +set(CPACK_PACKAGE_VENDOR "CBP Authors") # Enable DEB, RPM and TBZ2 generators by default -set(CPACK_GENERATOR - "DEB;TBZ2;TGZ;STGZ" -) +set(CPACK_GENERATOR "DEB;TBZ2;TGZ;STGZ") # Use all available cores when parallelism is supported -set(CPACK_THREADS - 0 -) +set(CPACK_THREADS 0) # The DEB generator requires CPACK_DEBIAN_PACKAGE_MAINTAINER value to be set. -set(CPACK_DEBIAN_PACKAGE_MAINTAINER - "CBP Authors" -) +set(CPACK_DEBIAN_PACKAGE_MAINTAINER "CBP Authors") # Enable packaging support for the project include(CPack) diff --git a/chapter04/ex06_pack/executable/CMakeLists.txt b/chapter04/ex06_pack/executable/CMakeLists.txt index 5832a08..3713b4d 100644 --- a/chapter04/ex06_pack/executable/CMakeLists.txt +++ b/chapter04/ex06_pack/executable/CMakeLists.txt @@ -13,16 +13,10 @@ project( ) # Define the executable that will consume the ch4_ex05_lib package -add_executable( - ch4_ex06_executable - src/main.cpp -) +add_executable(ch4_ex06_executable src/main.cpp) # Set the required C++ standard for the target -target_compile_features( - ch4_ex06_executable - PRIVATE cxx_std_17 -) +target_compile_features(ch4_ex06_executable PRIVATE cxx_std_17) # Link executable target to library target target_link_libraries( @@ -30,4 +24,6 @@ target_link_libraries( PRIVATE ch4_ex06_library ) -install(TARGETS ch4_ex06_executable) +install( + TARGETS ch4_ex06_executable +) diff --git a/chapter04/ex06_pack/library/CMakeLists.txt b/chapter04/ex06_pack/library/CMakeLists.txt index d85c427..abd6488 100644 --- a/chapter04/ex06_pack/library/CMakeLists.txt +++ b/chapter04/ex06_pack/library/CMakeLists.txt @@ -13,16 +13,10 @@ project( ) # Define the executable that will consume the ch4_ex05_lib package -add_library( - ch4_ex06_library STATIC - src/lib.cpp -) +add_library(ch4_ex06_library STATIC src/lib.cpp) # Set the required C++ standard for the target -target_compile_features( - ch4_ex06_library - PRIVATE cxx_std_17 -) +target_compile_features(ch4_ex06_library PRIVATE cxx_std_17) # Specify the include directories for the target named `ch4_ex02_static` target_include_directories( @@ -32,13 +26,14 @@ target_include_directories( include(GNUInstallDirs) # Defines the ${CMAKE_INSTALL_INCLUDEDIR} variable. -# Make executable target `ch4_ex02_static` installable. As mentioned before in -# Chapter 4 content, this will only install the output artifacts produced by the -# target. -install(TARGETS ch4_ex06_library) +# Make executable target `ch4_ex02_static` installable. As mentioned before in Chapter 4 content, +# this will only install the output artifacts produced by the target. +install( + TARGETS ch4_ex06_library +) -# Install the header files. Since header files are not listed as output -# artifacts, they have to be installed separately. +# Install the header files. Since header files are not listed as output artifacts, they have to be +# installed separately. install( DIRECTORY ${PROJECT_SOURCE_DIR}/include/ DESTINATION ${CMAKE_INSTALL_INCLUDEDIR} diff --git a/chapter04/ex07_pack_nsis_standalone/CMakeLists.txt b/chapter04/ex07_pack_nsis_standalone/CMakeLists.txt index 0c96cf4..569ce33 100644 --- a/chapter04/ex07_pack_nsis_standalone/CMakeLists.txt +++ b/chapter04/ex07_pack_nsis_standalone/CMakeLists.txt @@ -25,13 +25,8 @@ add_subdirectory(library) if(WIN32) # if it is a debug build, we want to install debug libraries with the # InstallRequiredSystemLibraries module - if(CMAKE_BUILD_TYPE - STREQUAL - "Debug" - ) - set(CMAKE_INSTALL_DEBUG_LIBRARIES - TRUE - ) + if(CMAKE_BUILD_TYPE STREQUAL "Debug") + set(CMAKE_INSTALL_DEBUG_LIBRARIES TRUE) endif() # this includes compiler provided runtime libraries on windows include(InstallRequiredSystemLibraries) @@ -48,19 +43,13 @@ install( ".*system32/.*\\.dll" ) -# We will not explicitly specify project name and version here and let CPack to -# get project name and version from the project() -set(CPACK_PACKAGE_VENDOR - "CBP Authors" -) +# We will not explicitly specify project name and version here and let CPack to get project name +# and version from the project() +set(CPACK_PACKAGE_VENDOR "CBP Authors") # Enable NSIS and TBZ2 generators by default -set(CPACK_GENERATOR - "NSIS;TBZ2" -) +set(CPACK_GENERATOR "NSIS;TBZ2") # Use all available cores when parallelism is supported -set(CPACK_THREADS - 0 -) -# The DEB generator requires CPACK_DEBIAN_PACKAGE_MAINTAINER value to be set. -# Enable packaging support for the project +set(CPACK_THREADS 0) +# The DEB generator requires CPACK_DEBIAN_PACKAGE_MAINTAINER value to be set. Enable packaging +# support for the project include(CPack) diff --git a/chapter04/ex07_pack_nsis_standalone/executable/CMakeLists.txt b/chapter04/ex07_pack_nsis_standalone/executable/CMakeLists.txt index b54c489..4a93e53 100644 --- a/chapter04/ex07_pack_nsis_standalone/executable/CMakeLists.txt +++ b/chapter04/ex07_pack_nsis_standalone/executable/CMakeLists.txt @@ -13,16 +13,10 @@ project( ) # Define the executable that will consume the ch4_ex05_lib package -add_executable( - ch4_ex07_executable - src/main.cpp -) +add_executable(ch4_ex07_executable src/main.cpp) # Set the required C++ standard for the target -target_compile_features( - ch4_ex07_executable - PRIVATE cxx_std_17 -) +target_compile_features(ch4_ex07_executable PRIVATE cxx_std_17) # Link executable target to library target target_link_libraries( @@ -30,4 +24,6 @@ target_link_libraries( PRIVATE ch4_ex07_library ) -install(TARGETS ch4_ex07_executable) +install( + TARGETS ch4_ex07_executable +) diff --git a/chapter04/ex07_pack_nsis_standalone/library/CMakeLists.txt b/chapter04/ex07_pack_nsis_standalone/library/CMakeLists.txt index 361feee..239774a 100644 --- a/chapter04/ex07_pack_nsis_standalone/library/CMakeLists.txt +++ b/chapter04/ex07_pack_nsis_standalone/library/CMakeLists.txt @@ -13,16 +13,10 @@ project( ) # Define the executable that will consume the ch4_ex05_lib package -add_library( - ch4_ex07_library SHARED - src/lib.cpp -) +add_library(ch4_ex07_library SHARED src/lib.cpp) # Set the required C++ standard for the target -target_compile_features( - ch4_ex07_library - PRIVATE cxx_std_17 -) +target_compile_features(ch4_ex07_library PRIVATE cxx_std_17) # Specify the include directories for the target named `ch4_ex02_static` target_include_directories( @@ -32,13 +26,14 @@ target_include_directories( include(GNUInstallDirs) # Defines the ${CMAKE_INSTALL_INCLUDEDIR} variable. -# Make executable target `ch4_ex02_static` installable. As mentioned before in -# Chapter 4 content, this will only install the output artifacts produced by the -# target. -install(TARGETS ch4_ex07_library) +# Make executable target `ch4_ex02_static` installable. As mentioned before in Chapter 4 content, +# this will only install the output artifacts produced by the target. +install( + TARGETS ch4_ex07_library +) -# Install the header files. Since header files are not listed as output -# artifacts, they have to be installed separately. +# Install the header files. Since header files are not listed as output artifacts, they have to be +# installed separately. install( DIRECTORY ${PROJECT_SOURCE_DIR}/include/ DESTINATION ${CMAKE_INSTALL_INCLUDEDIR} diff --git a/chapter05/CMakeLists.txt b/chapter05/CMakeLists.txt index b7ce15f..16caeb3 100644 --- a/chapter05/CMakeLists.txt +++ b/chapter05/CMakeLists.txt @@ -7,8 +7,7 @@ cmake_minimum_required(VERSION 3.25...3.30) project( "Chapter_5" VERSION 1.0 - DESCRIPTION - "A collection of sample C++ projects to demonstrate using external dependencies" + DESCRIPTION "A collection of sample C++ projects to demonstrate using external dependencies" LANGUAGES CXX ) @@ -17,8 +16,8 @@ add_subdirectory(find_module) add_subdirectory(fetch_content_example) add_subdirectory(external_project) -# Look for the conan command on the host system, if conan is not found exclude -# all conan related examples from the project +# Look for the conan command on the host system, if conan is not found exclude all conan related +# examples from the project find_program(CONAN_EXECUTABLE conan) if(CONAN_EXECUTABLE) @@ -27,22 +26,13 @@ else() message(STATUS "Conan not found. Will not build conan examples") endif() -# Look for the vcpkg executable on the host system, if vcpkg is not found -# exclude all vcpkg-related examples from the project +# Look for the vcpkg executable on the host system, if vcpkg is not found exclude all vcpkg-related +# examples from the project find_program(VCPKG_EXECUTABLE vcpkg) -# if vcpkg not found or toolchain file does not match vcpkg.cmake exclude the -# vcpkg example -if(NOT VCPKG_EXECUTABLE - OR NOT - CMAKE_TOOLCHAIN_FILE - MATCHES - "vcpkg.cmake$" -) - message( - STATUS - "vcpkg not found or vcpkg toolchain not set. Will not build vcpkg example" - ) +# if vcpkg not found or toolchain file does not match vcpkg.cmake exclude the vcpkg example +if(NOT VCPKG_EXECUTABLE OR NOT CMAKE_TOOLCHAIN_FILE MATCHES "vcpkg.cmake$") + message(STATUS "vcpkg not found or vcpkg toolchain not set. Will not build vcpkg example") else() add_subdirectory(vcpkg_example) endif() diff --git a/chapter05/conan_example/CMakeLists.txt b/chapter05/conan_example/CMakeLists.txt index aa6c280..101da54 100644 --- a/chapter05/conan_example/CMakeLists.txt +++ b/chapter05/conan_example/CMakeLists.txt @@ -7,34 +7,21 @@ cmake_minimum_required(VERSION 3.25...3.30) project( conan_conanfile_example VERSION 1.0 - DESCRIPTION - "AC++ project to demonstrate using a conanfile.txt from within CMake" + DESCRIPTION "AC++ project to demonstrate using a conanfile.txt from within CMake" LANGUAGES CXX ) -# Append the current binary dir to CMAKE_PREFIX_PATH so the conan package are -# found -list( - APPEND - CMAKE_PREFIX_PATH - ${CMAKE_CURRENT_BINARY_DIR} -) +# Append the current binary dir to CMAKE_PREFIX_PATH so the conan package are found +list(APPEND CMAKE_PREFIX_PATH ${CMAKE_CURRENT_BINARY_DIR}) # find the just downloaded package -find_package( - fmt - 10.2.1 - REQUIRED -) +find_package(fmt 10.2.1 REQUIRED) # Create a target to build an executable add_executable(conan_conanfile_example) # set the c++ version required -target_compile_features( - conan_conanfile_example - PRIVATE cxx_std_17 -) +target_compile_features(conan_conanfile_example PRIVATE cxx_std_17) # Add source files to 'conan_conanfile_example' target target_sources( conan_conanfile_example diff --git a/chapter05/external_project/CMakeLists.txt b/chapter05/external_project/CMakeLists.txt index 535803d..727d9fa 100644 --- a/chapter05/external_project/CMakeLists.txt +++ b/chapter05/external_project/CMakeLists.txt @@ -19,41 +19,31 @@ externalproject_add( URL https://github.com/bernedom/bertrand/archive/refs/tags/0.0.17.tar.gz URL_HASH MD5=354141c50b8707f2574b69f30cef0238 INSTALL_DIR ${CMAKE_CURRENT_BINARY_DIR}/bertrand_install - CMAKE_CACHE_ARGS -DBERTRAND_BUILD_TESTING:BOOL=OFF - -DCMAKE_INSTALL_PREFIX:PATH= + CMAKE_CACHE_ARGS -DBERTRAND_BUILD_TESTING:BOOL=OFF -DCMAKE_INSTALL_PREFIX:PATH= ) # Retrieve the installation directory of bertrand externalproject_get_property(bertrand INSTALL_DIR) # Populate the installation directory to the variable BERTRAND_INSTALL_DIR -set(BERTRAND_INSTALL_DIR - "${INSTALL_DIR}" -) +set(BERTRAND_INSTALL_DIR "${INSTALL_DIR}") # Add an extra step after the build step to extract and archive the license file externalproject_add_step( - bertrand copy_license - COMMAND - ${CMAKE_COMMAND} -E tar "cvzf" - ${CMAKE_CURRENT_BINARY_DIR}/licenses.tar.gz /LICENSE - DEPENDEES build - ALWAYS YES + bertrand copy_license COMMAND ${CMAKE_COMMAND} -E tar "cvzf" + ${CMAKE_CURRENT_BINARY_DIR}/licenses.tar.gz /LICENSE + DEPENDEES build ALWAYS YES ) # Create a target to build an executable add_executable(ch5_external_project_example) # set the c++ version required -target_compile_features( - ch5_external_project_example - PRIVATE cxx_std_17 -) +target_compile_features(ch5_external_project_example PRIVATE cxx_std_17) # Add source files to 'ch5_external_project_example' target target_sources( ch5_external_project_example PRIVATE src/main.cpp ) -# make the executable to be built depend on the external project to force -# downloading first +# make the executable to be built depend on the external project to force downloading first add_dependencies(ch5_external_project_example bertrand) # make the header file for bertrand available diff --git a/chapter05/fetch_content_example/CMakeLists.txt b/chapter05/fetch_content_example/CMakeLists.txt index 740a461..c2c5a08 100644 --- a/chapter05/fetch_content_example/CMakeLists.txt +++ b/chapter05/fetch_content_example/CMakeLists.txt @@ -27,10 +27,7 @@ fetchcontent_makeavailable(si) add_executable(ch5_fetch_content_example) # set the c++ version required -target_compile_features( - ch5_fetch_content_example - PRIVATE cxx_std_17 -) +target_compile_features(ch5_fetch_content_example PRIVATE cxx_std_17) # Add source files to 'ch5_fetch_content_example' target target_sources( ch5_fetch_content_example diff --git a/chapter05/find_module/CMakeLists.txt b/chapter05/find_module/CMakeLists.txt index 69fc790..463e529 100644 --- a/chapter05/find_module/CMakeLists.txt +++ b/chapter05/find_module/CMakeLists.txt @@ -12,24 +12,16 @@ project( ) # The following example will look for a pre-built library in the folder -# ${CMAKE_CURRENT_BINARY_DIR}/dep. To use it build the project located in `dep` -# first and install it in the directory `dep` of the current source tree +# ${CMAKE_CURRENT_BINARY_DIR}/dep. To use it build the project located in `dep` first and install +# it in the directory `dep` of the current source tree # Tell CMake where to look for find modules -list( - APPEND - CMAKE_MODULE_PATH - "${CMAKE_CURRENT_SOURCE_DIR}/cmake" -) -# try to find the package obscure see the file cmake/FindObscure.cmake for the -# innner workings +list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake") +# try to find the package obscure see the file cmake/FindObscure.cmake for the innner workings find_package(Obscure QUIET) if(NOT Obscure_FOUND) - message( - STATUS - "Prebuilt library 'obscure' not found, skipping find module example" - ) + message(STATUS "Prebuilt library 'obscure' not found, skipping find module example") return() endif() @@ -41,10 +33,7 @@ target_link_libraries( ) # set the c++ version required -target_compile_features( - ch5_find_module_example - PRIVATE cxx_std_17 -) +target_compile_features(ch5_find_module_example PRIVATE cxx_std_17) # Add source files to 'ch5_find_module_example' target target_sources( ch5_find_module_example diff --git a/chapter05/find_module/cmake/FindObscure.cmake b/chapter05/find_module/cmake/FindObscure.cmake index faab2a5..05f0d31 100644 --- a/chapter05/find_module/cmake/FindObscure.cmake +++ b/chapter05/find_module/cmake/FindObscure.cmake @@ -1,58 +1,37 @@ cmake_minimum_required(VERSION 3.25...3.30) -# find the library file libobscure.so obscure.dll or libobscure.1.0.0.so or -# similar Additionally to the default paths look in ${PROJECT_SOURCE_DIR}/dep/ -# in each subdirectory append bin lib build/Release and build/Debug +# find the library file libobscure.so obscure.dll or libobscure.1.0.0.so or similar Additionally to +# the default paths look in ${PROJECT_SOURCE_DIR}/dep/ in each subdirectory append bin lib +# build/Release and build/Debug find_library( - OBSCURE_LIBRARY - NAMES obscure - HINTS ${PROJECT_SOURCE_DIR}/dep/ ${CMAKE_CURRENT_BINARY_DIR}/dep - PATH_SUFFIXES - lib - bin - build/Release - build/Debug + OBSCURE_LIBRARY NAMES obscure HINTS ${PROJECT_SOURCE_DIR}/dep/ ${CMAKE_CURRENT_BINARY_DIR}/dep + PATH_SUFFIXES lib bin build/Release build/Debug ) # find the main header belonging to the obscure lib -find_path( - OBSCURE_INCLUDE_DIR - NAMES obscure/obscure.hpp - HINTS ${PROJECT_SOURCE_DIR}/dep/include/ - ${CMAKE_CURRENT_BINARY_DIR}/dep/include - ${CMAKE_CURRENT_SOURCE_DIR}/dep/include +find_path(OBSCURE_INCLUDE_DIR NAMES obscure/obscure.hpp + HINTS ${PROJECT_SOURCE_DIR}/dep/include/ ${CMAKE_CURRENT_BINARY_DIR}/dep/include + ${CMAKE_CURRENT_SOURCE_DIR}/dep/include ) # use the FindPackageHandleStandardArgs to check if everything was found include(FindPackageHandleStandardArgs) -find_package_handle_standard_args( - Obscure - DEFAULT_MSG - OBSCURE_LIBRARY - OBSCURE_INCLUDE_DIR -) +find_package_handle_standard_args(Obscure DEFAULT_MSG OBSCURE_LIBRARY OBSCURE_INCLUDE_DIR) mark_as_advanced(OBSCURE_LIBRARY OBSCURE_INCLUDE_DIR) # if not building the library itself -if(NOT - TARGET - Obscure::Obscure -) +if(NOT TARGET Obscure::Obscure) # make the library target available - add_library( - Obscure::Obscure - UNKNOWN - IMPORTED - ) + add_library(Obscure::Obscure UNKNOWN IMPORTED) # set the properties so the artifacts of the packages can be found set_target_properties( Obscure::Obscure - PROPERTIES IMPORTED_LOCATION "${OBSCURE_LIBRARY}" - INTERFACE_INCLUDE_DIRECTORIES "${OBSCURE_INCLUDE_DIR}" + PROPERTIES IMPORTED_LOCATION "${OBSCURE_LIBRARY}" INTERFACE_INCLUDE_DIRECTORIES + "${OBSCURE_INCLUDE_DIR}" IMPORTED_LINK_INTERFACE_LANGUAGES "CXX" ) endif() diff --git a/chapter05/find_module/dep/CMakeLists.txt b/chapter05/find_module/dep/CMakeLists.txt index 3c618b6..af41983 100644 --- a/chapter05/find_module/dep/CMakeLists.txt +++ b/chapter05/find_module/dep/CMakeLists.txt @@ -6,21 +6,18 @@ cmake_minimum_required(VERSION 3.25...3.30) project( an_obscure_lib VERSION 1.0.0 - DESCRIPTION - "An example project to compile to use with the find_module example" + DESCRIPTION "An example project to compile to use with the find_module example" LANGUAGES CXX ) # add the library target add_library(obscure SHARED) -# set properties for the target. VERSION set the library version to the project -# version * SOVERSION set the compatibility version for the library to the -# major number of the version +# set properties for the target. VERSION set the library version to the project version * SOVERSION +# set the compatibility version for the library to the major number of the version set_target_properties( obscure - PROPERTIES VERSION ${PROJECT_VERSION} - SOVERSION ${PROJECT_VERSION_MAJOR} + PROPERTIES VERSION ${PROJECT_VERSION} SOVERSION ${PROJECT_VERSION_MAJOR} ) # add sources to the library target @@ -29,12 +26,8 @@ target_sources( PRIVATE src/obscure.cpp src/internal.cpp ) -# define the C++ standard needed to compile this library and make it visible to -# dependers -target_compile_features( - obscure - PUBLIC cxx_std_17 -) +# define the C++ standard needed to compile this library and make it visible to dependers +target_compile_features(obscure PUBLIC cxx_std_17) # set the include directories target_include_directories( diff --git a/chapter05/find_package_example/CMakeLists.txt b/chapter05/find_package_example/CMakeLists.txt index bf5368e..46d8631 100644 --- a/chapter05/find_package_example/CMakeLists.txt +++ b/chapter05/find_package_example/CMakeLists.txt @@ -7,28 +7,21 @@ cmake_minimum_required(VERSION 3.25...3.30) project( ch5_find_package_example VERSION 1.0 - DESCRIPTION - "A C++ project to demonstrate searching third-party dependencies" + DESCRIPTION "A C++ project to demonstrate searching third-party dependencies" LANGUAGES CXX ) -# enable debugging for finding the package (For illustrative purposes) -# set(CMAKE_FIND_DEBUG_MODE TRUE ) +# enable debugging for finding the package (For illustrative purposes) set(CMAKE_FIND_DEBUG_MODE +# TRUE ) -# Search the package OpenSSL and its component SSL. To have the configuration -# fail if not found, add the REQUIRED keyword like this: find_package(OpenSSL -# COMPONENTS SSL REQUIRED) -find_package( - OpenSSL - COMPONENTS SSL -) +# Search the package OpenSSL and its component SSL. To have the configuration fail if not found, +# add the REQUIRED keyword like this: find_package(OpenSSL COMPONENTS SSL REQUIRED) +find_package(OpenSSL COMPONENTS SSL) # disable debugging again set(CMAKE_FIND_DEBUG_MODE FALSE ) if(NOT OPENSSL) - message( - STATUS "OpenSSL package not found; Not building find-package example." - ) + message(STATUS "OpenSSL package not found; Not building find-package example.") return() endif() @@ -36,10 +29,7 @@ endif() add_executable(ch5_find_package_example) # set the c++ version required -target_compile_features( - ch5_find_package_example - PRIVATE cxx_std_17 -) +target_compile_features(ch5_find_package_example PRIVATE cxx_std_17) # Add source files to 'ch5_find_package_example' target target_sources( ch5_find_package_example diff --git a/chapter05/vcpkg_example/CMakeLists.txt b/chapter05/vcpkg_example/CMakeLists.txt index 0dbcb53..c3a303d 100644 --- a/chapter05/vcpkg_example/CMakeLists.txt +++ b/chapter05/vcpkg_example/CMakeLists.txt @@ -21,10 +21,7 @@ endif() add_executable(ch5_vcpkg_example) # set the c++ version required -target_compile_features( - ch5_vcpkg_example - PRIVATE cxx_std_17 -) +target_compile_features(ch5_vcpkg_example PRIVATE cxx_std_17) # Add source files to 'ch5_vcpkg_example' target target_sources( ch5_vcpkg_example diff --git a/chapter06/CMakeLists.txt b/chapter06/CMakeLists.txt index 10561b8..9b77e34 100644 --- a/chapter06/CMakeLists.txt +++ b/chapter06/CMakeLists.txt @@ -4,17 +4,10 @@ cmake_minimum_required(VERSION 3.25...3.30) -option( - ENABLE_DOCUMENTATION - "Enable documentation" - ON -) +option(ENABLE_DOCUMENTATION "Enable documentation" ON) if(NOT ENABLE_DOCUMENTATION) - message( - STATUS - "Documentation is disabled, excluded chapter 6 documentation examples" - ) + message(STATUS "Documentation is disabled, excluded chapter 6 documentation examples") return() endif() diff --git a/chapter06/ex01_doxdocgen/CMakeLists.txt b/chapter06/ex01_doxdocgen/CMakeLists.txt index b6401f7..2e3cfc5 100644 --- a/chapter06/ex01_doxdocgen/CMakeLists.txt +++ b/chapter06/ex01_doxdocgen/CMakeLists.txt @@ -10,12 +10,11 @@ project( LANGUAGES CXX ) -# ############################################################################## +# ################################################################################################# # Doxygen configuration -# ############################################################################## -# Locate Doxygen in the environment. This will set DOXYGEN_FOUND variable as a -# result. When Doxygen is found, DOXYGEN_VERSION variable will be also set to -# found Doxygen version. +# ################################################################################################# +# Locate Doxygen in the environment. This will set DOXYGEN_FOUND variable as a result. When Doxygen +# is found, DOXYGEN_VERSION variable will be also set to found Doxygen version. find_package(Doxygen) if(NOT DOXYGEN_FOUND) @@ -26,60 +25,33 @@ if(NOT DOXYGEN_FOUND) return() endif() -# Override the default doxygen output directory, which is -# CMAKE_CURRENT_BINARY_DIR. -set(DOXYGEN_OUTPUT_DIRECTORY - "${CMAKE_CURRENT_BINARY_DIR}/docs" -) +# Override the default doxygen output directory, which is CMAKE_CURRENT_BINARY_DIR. +set(DOXYGEN_OUTPUT_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/docs") # Enable HTML documentation generation -set(DOXYGEN_GENERATE_HTML - YES -) +set(DOXYGEN_GENERATE_HTML YES) # Enable MAN page generation -set(DOXYGEN_GENERATE_MAN - YES -) +set(DOXYGEN_GENERATE_MAN YES) # Enable markdown support -set(DOXYGEN_MARKDOWN_SUPPORT - YES -) +set(DOXYGEN_MARKDOWN_SUPPORT YES) # Enable autolink support -set(DOXYGEN_AUTOLINK_SUPPORT - YES -) +set(DOXYGEN_AUTOLINK_SUPPORT YES) # Enable dot graph support -set(DOXYGEN_HAVE_DOT - YES -) +set(DOXYGEN_HAVE_DOT YES) # Include collaboration diagrams in class documentation -set(DOXYGEN_COLLABORATION_GRAPH - YES -) +set(DOXYGEN_COLLABORATION_GRAPH YES) # Include class diagrams in class documentation -set(DOXYGEN_CLASS_GRAPH - YES -) +set(DOXYGEN_CLASS_GRAPH YES) # Generate UML-like graphs -set(DOXYGEN_UML_LOOK - YES -) +set(DOXYGEN_UML_LOOK YES) # Include type and parameter information in UML graphs -set(DOXYGEN_DOT_UML_DETAILS - YES -) +set(DOXYGEN_DOT_UML_DETAILS YES) # Max per-line length for graph content -set(DOXYGEN_DOT_WRAP_THRESHOLD - 100 -) +set(DOXYGEN_DOT_WRAP_THRESHOLD 100) # Enables drawing call graphs for the functions -set(DOXYGEN_CALL_GRAPH - YES -) +set(DOXYGEN_CALL_GRAPH YES) # Suppress the output messages -set(DOXYGEN_QUIET - YES -) -# ############################################################################## +set(DOXYGEN_QUIET YES) +# ################################################################################################# # Define an static library target named `ch6_ex01_doxdocgen_lib` add_library(ch6_ex01_doxdocgen_lib STATIC) # Specify source files for target named `ch6_ex01_doxdocgen_lib` @@ -92,23 +64,14 @@ target_include_directories( ch6_ex01_doxdocgen_lib PUBLIC include ) -# Request compile features for target named `ch6_ex01_doxdocgen_lib`. Requested -# `cxx_std_17` feature determines the minimum C++ standard required to build -# this target. It's PRIVATE, since it is a build-time requirement only. -target_compile_features( - ch6_ex01_doxdocgen_lib - PRIVATE cxx_std_17 -) +# Request compile features for target named `ch6_ex01_doxdocgen_lib`. Requested `cxx_std_17` +# feature determines the minimum C++ standard required to build this target. It's PRIVATE, since it +# is a build-time requirement only. +target_compile_features(ch6_ex01_doxdocgen_lib PRIVATE cxx_std_17) # Create an executable target that consumes the library -add_executable( - ch6_ex01_doxdocgen_exe - src/main.cpp -) +add_executable(ch6_ex01_doxdocgen_exe src/main.cpp) # Compile features for the executable target -target_compile_features( - ch6_ex01_doxdocgen_exe - PRIVATE cxx_std_17 -) +target_compile_features(ch6_ex01_doxdocgen_exe PRIVATE cxx_std_17) # Link static library to executable target_link_libraries( ch6_ex01_doxdocgen_exe @@ -116,9 +79,7 @@ target_link_libraries( ) doxygen_add_docs( - ch6_ex01_doxdocgen_generate_docs - "${CMAKE_CURRENT_LIST_DIR}" - ALL + ch6_ex01_doxdocgen_generate_docs "${CMAKE_CURRENT_LIST_DIR}" ALL COMMENT "Generating documentation for Chapter 6 - Example 01 with Doxygen" ) @@ -142,22 +103,14 @@ install( ) # Override the package name -set(CPACK_PACKAGE_NAME - cbp_chapter6_example01 -) -# We will not explicitly specify project name and version here and let CPack to -# get project name and version from the project() -set(CPACK_PACKAGE_VENDOR - "CBP Authors" -) +set(CPACK_PACKAGE_NAME cbp_chapter6_example01) +# We will not explicitly specify project name and version here and let CPack to get project name +# and version from the project() +set(CPACK_PACKAGE_VENDOR "CBP Authors") # Enable DEB, RPM and TBZ2 generators by default -set(CPACK_GENERATOR - "DEB;RPM;TBZ2" -) +set(CPACK_GENERATOR "DEB;RPM;TBZ2") # The DEB generator requires CPACK_DEBIAN_PACKAGE_MAINTAINER value to be set. -set(CPACK_DEBIAN_PACKAGE_MAINTAINER - "CBP Authors" -) +set(CPACK_DEBIAN_PACKAGE_MAINTAINER "CBP Authors") # Enable packaging support for the project include(CPack) diff --git a/chapter06/ex02_doxplantuml/CMakeLists.txt b/chapter06/ex02_doxplantuml/CMakeLists.txt index 7cd987c..6feb522 100644 --- a/chapter06/ex02_doxplantuml/CMakeLists.txt +++ b/chapter06/ex02_doxplantuml/CMakeLists.txt @@ -6,34 +6,28 @@ project( ch6_ex02_doxplantuml VERSION 1.0 - DESCRIPTION - "Chapter 6 Example 02, Doxygen documentation PlantUML generation" + DESCRIPTION "Chapter 6 Example 02, Doxygen documentation PlantUML generation" LANGUAGES CXX ) -# ############################################################################## +# ################################################################################################# # Doxygen configuration -# ############################################################################## +# ################################################################################################# # Locate PlantUML JAR file in the environment find_path( PLANTUML_JAR_PATH NAMES plantuml.jar - HINTS "/usr/share/plantuml" - "C:/Program Files/PlantUML" + HINTS "/usr/share/plantuml" "C:/Program Files/PlantUML" "C:/ProgramData/chocolatey/lib/plantuml/tools" "/usr/local/Cellar/plantuml@1.2021.13/1.2021.13/libexec" ) if(PLANTUML_JAR_PATH-Notfound) - message( - STATUS - "PlantUML JAR file not found. Will not build the plantuml example" - ) + message(STATUS "PlantUML JAR file not found. Will not build the plantuml example") return() endif() -# Locate Doxygen in the environment. This will set DOXYGEN_FOUND variable as a -# result. When Doxygen is found, DOXYGEN_VERSION variable will be also set to -# found Doxygen version. +# Locate Doxygen in the environment. This will set DOXYGEN_FOUND variable as a result. When Doxygen +# is found, DOXYGEN_VERSION variable will be also set to found Doxygen version. find_package(Doxygen) if(NOT DOXYGEN_FOUND) @@ -44,42 +38,23 @@ if(NOT DOXYGEN_FOUND) return() endif() -# Override the default doxygen output directory, which is -# CMAKE_CURRENT_BINARY_DIR. -set(DOXYGEN_OUTPUT_DIRECTORY - "${CMAKE_CURRENT_BINARY_DIR}/docs" -) +# Override the default doxygen output directory, which is CMAKE_CURRENT_BINARY_DIR. +set(DOXYGEN_OUTPUT_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/docs") # Enable HTML documentation generation -set(DOXYGEN_GENERATE_HTML - YES -) +set(DOXYGEN_GENERATE_HTML YES) # Enable autolink support -set(DOXYGEN_AUTOLINK_SUPPORT - YES -) +set(DOXYGEN_AUTOLINK_SUPPORT YES) # Tell the Doxygen the PlantUML JAR path -set(DOXYGEN_PLANTUML_JAR_PATH - "${PLANTUML_JAR_PATH}" -) +set(DOXYGEN_PLANTUML_JAR_PATH "${PLANTUML_JAR_PATH}") # Suppress the output messages -set(DOXYGEN_QUIET - YES -) -# ############################################################################## +set(DOXYGEN_QUIET YES) +# ################################################################################################# # Create an executable target that consumes the library -add_executable( - ch6_ex02_doxplantuml - src/main.cpp -) +add_executable(ch6_ex02_doxplantuml src/main.cpp) # Compile features for the executable target -target_compile_features( - ch6_ex02_doxplantuml - PRIVATE cxx_std_17 -) +target_compile_features(ch6_ex02_doxplantuml PRIVATE cxx_std_17) doxygen_add_docs( - ch6_ex02_doxdocgen_generate_docs - "${CMAKE_CURRENT_LIST_DIR}" - ALL + ch6_ex02_doxdocgen_generate_docs "${CMAKE_CURRENT_LIST_DIR}" ALL COMMENT "Generating documentation for Chapter 6 - Example 02 with Doxygen" ) diff --git a/chapter07/clang_tidy_example/CMakeLists.txt b/chapter07/clang_tidy_example/CMakeLists.txt index 05091b0..016e39e 100644 --- a/chapter07/clang_tidy_example/CMakeLists.txt +++ b/chapter07/clang_tidy_example/CMakeLists.txt @@ -11,27 +11,17 @@ project( LANGUAGES CXX ) -# if clang-tidy is not already set, it is searched for here If using a CMake -# version above 3.19 it is recommended to configure clang-tidy in a preset +# if clang-tidy is not already set, it is searched for here If using a CMake version above 3.19 it +# is recommended to configure clang-tidy in a preset if(NOT CMAKE_CXX_CLANG_TIDY) # Search for clang-tidy and if found configure - find_program( - CLANG_TIDY_EXECUTABLE - NAMES clang-tidy - ) + find_program(CLANG_TIDY_EXECUTABLE NAMES clang-tidy) if(CLANG_TIDY_EXECUTABLE) - option( - ENABLE_CLANG_TIDY - "Enable CLANG_TIDY" - ON - ) + option(ENABLE_CLANG_TIDY "Enable CLANG_TIDY" ON) - list( - APPEND - CLANG_TIDY_COMMAND - "${CLANG_TIDY_EXECUTABLE}" - "-checks=-*,modernize-use-nullptr,-extra-arg=-std=c++17" + list(APPEND CLANG_TIDY_COMMAND "${CLANG_TIDY_EXECUTABLE}" + "-checks=-*,modernize-use-nullptr,-extra-arg=-std=c++17" ) endif() @@ -43,15 +33,9 @@ target_sources( ch7_clang_tidy_example PRIVATE src/main.cpp ) -target_compile_features( - ch7_clang_tidy_example - PRIVATE cxx_std_17 -) +target_compile_features(ch7_clang_tidy_example PRIVATE cxx_std_17) # if clang tidy was found enable it for the target if(ENABLE_CLANG_TIDY) - set_target_properties( - ch7_clang_tidy_example - PROPERTIES CXX_CLANG_TIDY "${CLANG_TIDY_COMMAND}" - ) + set_target_properties(ch7_clang_tidy_example PROPERTIES CXX_CLANG_TIDY "${CLANG_TIDY_COMMAND}") endif() diff --git a/chapter07/coverage_example/CMakeLists.txt b/chapter07/coverage_example/CMakeLists.txt index 9baac00..c469b21 100644 --- a/chapter07/coverage_example/CMakeLists.txt +++ b/chapter07/coverage_example/CMakeLists.txt @@ -7,8 +7,7 @@ cmake_minimum_required(VERSION 3.25...3.30) project( "ch7_coverage_example" VERSION 1.0 - DESCRIPTION - "A simple C++ project to demonstrate generating coverage information" + DESCRIPTION "A simple C++ project to demonstrate generating coverage information" LANGUAGES CXX ) # include CTest module for defining tests later @@ -20,26 +19,16 @@ target_include_directories( ch7_coverage_example PUBLIC include ) -target_compile_features( - ch7_coverage_example - PUBLIC cxx_std_17 -) +target_compile_features(ch7_coverage_example PUBLIC cxx_std_17) target_sources( ch7_coverage_example PRIVATE src/coverage_example/coverage_example.cpp ) -add_executable( - ch7_coverage_example_test - src/coverage_test.cpp +add_executable(ch7_coverage_example_test src/coverage_test.cpp) +target_link_libraries( + ch7_coverage_example_test ch7_coverage_example ) -target_link_libraries(ch7_coverage_example_test ch7_coverage_example) -target_compile_features( - ch7_coverage_example_test - PRIVATE cxx_std_17 -) -add_test( - NAME code_coverage_test - COMMAND ch7_coverage_example_test -) +target_compile_features(ch7_coverage_example_test PRIVATE cxx_std_17) +add_test(NAME code_coverage_test COMMAND ch7_coverage_example_test) diff --git a/chapter07/cppcheck_example/CMakeLists.txt b/chapter07/cppcheck_example/CMakeLists.txt index e24f168..701a059 100644 --- a/chapter07/cppcheck_example/CMakeLists.txt +++ b/chapter07/cppcheck_example/CMakeLists.txt @@ -7,26 +7,18 @@ cmake_minimum_required(VERSION 3.25...3.30) project( "ch7_cppcheck_example" VERSION 1.0 - DESCRIPTION - "A simple C++ project to demonstrate using cppcheck on an executable" + DESCRIPTION "A simple C++ project to demonstrate using cppcheck on an executable" LANGUAGES CXX ) -find_program( - CPPCHECK_EXECUTABLE - NAMES cppcheck -) +find_program(CPPCHECK_EXECUTABLE NAMES cppcheck) # search for the cppcheck executable if it exists enable it if(CPPCHECK_EXECUTABLE) - option( - ENABLE_CPPCHECK - "Enable cppcheck" - ON - ) + option(ENABLE_CPPCHECK "Enable cppcheck" ON) - # setting the flags for cppcheck, if --error-exitcode=1 is enabled - # compilation will fail if an error is detected + # setting the flags for cppcheck, if --error-exitcode=1 is enabled compilation will fail if an + # error is detected list( APPEND CPPCHECK_COMMAND @@ -47,8 +39,5 @@ target_sources( ) # Enable cppcheck for the target if(ENABLE_CPPCHECK) - set_target_properties( - ch7_cppcheck_example - PROPERTIES CXX_CPPCHECK "${CPPCHECK_COMMAND}" - ) + set_target_properties(ch7_cppcheck_example PROPERTIES CXX_CPPCHECK "${CPPCHECK_COMMAND}") endif() diff --git a/chapter07/cpplint_example/CMakeLists.txt b/chapter07/cpplint_example/CMakeLists.txt index e32091c..74c88b5 100644 --- a/chapter07/cpplint_example/CMakeLists.txt +++ b/chapter07/cpplint_example/CMakeLists.txt @@ -7,35 +7,21 @@ cmake_minimum_required(VERSION 3.25...3.30) project( "ch7_cpplint_example" VERSION 1.0 - DESCRIPTION - "A simple C++ project to demonstrate using cppcheck on an executable" + DESCRIPTION "A simple C++ project to demonstrate using cppcheck on an executable" LANGUAGES CXX ) -find_program( - CPPLINT_EXECUTABLE - NAMES cppcheck -) +find_program(CPPLINT_EXECUTABLE NAMES cppcheck) if(CPPLINT_EXECUTABLE) - option( - ENABLE_CPPLINT - "Enable cppcheck" - ON - ) + option(ENABLE_CPPLINT "Enable cppcheck" ON) message(STATUS "CPPlint enabled") endif() -set(CPPLINT_COMMAND - "" -) +set(CPPLINT_COMMAND "") if(ENABLE_CPPLINT) - list( - APPEND - CPPLINT_COMMAND - "${CPPLINT_EXECUTABLE}" - ) + list(APPEND CPPLINT_COMMAND "${CPPLINT_EXECUTABLE}") endif() add_executable(ch7_cpplint_example) diff --git a/chapter07/custom_build_type/CMakeLists.txt b/chapter07/custom_build_type/CMakeLists.txt index bebae2d..85815ed 100644 --- a/chapter07/custom_build_type/CMakeLists.txt +++ b/chapter07/custom_build_type/CMakeLists.txt @@ -14,94 +14,48 @@ project( include(CTest) # Check if the currenct generator is a multi-configuration generator or not -get_property( - isMultiConfig GLOBAL - PROPERTY GENERATOR_IS_MULTI_CONFIG -) +get_property(isMultiConfig GLOBAL PROPERTY GENERATOR_IS_MULTI_CONFIG) -# For multi-config generators add the build configuration "Coverage" to the list -# of available build configs +# For multi-config generators add the build configuration "Coverage" to the list of available build +# configs if(isMultiConfig) - if(NOT - "Coverage" - IN_LIST - CMAKE_CONFIGURATION_TYPES - ) - list( - APPEND - CMAKE_CONFIGURATION_TYPES - Coverage - ) + if(NOT "Coverage" IN_LIST CMAKE_CONFIGURATION_TYPES) + list(APPEND CMAKE_CONFIGURATION_TYPES Coverage) endif() else() - # If a single configuration set describe the cache variable CMAKE_BUILD_TYPE - # with the allowed values for build types - set(allowedBuildTypes - Debug - Release - Coverage - RelWithDebugInfo - MinSizeRel - ) - set_property( - CACHE CMAKE_BUILD_TYPE - PROPERTY STRINGS "${allowedBuildTypes}" - ) + # If a single configuration set describe the cache variable CMAKE_BUILD_TYPE with the allowed + # values for build types + set(allowedBuildTypes Debug Release Coverage RelWithDebugInfo MinSizeRel) + set_property(CACHE CMAKE_BUILD_TYPE PROPERTY STRINGS "${allowedBuildTypes}") # if the build type is not set force it to debug if(NOT CMAKE_BUILD_TYPE) - set(CMAKE_BUILD_TYPE - Debug - CACHE STRING - "" - FORCE - ) + set(CMAKE_BUILD_TYPE Debug CACHE STRING "" FORCE) # If the build type is unknown fail configuration with an error - elseif( - NOT - CMAKE_BUILD_TYPE - IN_LIST - allowedBuildTypes - ) + elseif(NOT CMAKE_BUILD_TYPE IN_LIST allowedBuildTypes) message(FATAL_ERROR "Unknown build type: ${CMAKE_BUILD_TYPE}") endif() endif() -# Set various flags for the compiler to enable building coverage The Coverage -# build type is based on the debug build type and usess any flags set in the -# debug type -set(CMAKE_C_FLAGS_COVERAGE - "${CMAKE_C_FLAGS_DEBUG} --coverage" - CACHE STRING "" -) -set(CMAKE_CXX_FLAGS_COVERAGE - "${CMAKE_CXX_FLAGS_DEBUG} --coverage" - CACHE STRING "" -) -set(CMAKE_EXE_LINKER_FLAGS_COVERAGE - "${CMAKE_EXE_LINKER_FLAGS_DEBUG} --coverage" - CACHE STRING "" -) -set(CMAKE_SHARED_LINKER_FLAGS_COVERAGE - "${CMAKE_SHARED_LINKER_FLAGS_DEBUG} --coverage" +# Set various flags for the compiler to enable building coverage The Coverage build type is based +# on the debug build type and usess any flags set in the debug type +set(CMAKE_C_FLAGS_COVERAGE "${CMAKE_C_FLAGS_DEBUG} --coverage" CACHE STRING "") +set(CMAKE_CXX_FLAGS_COVERAGE "${CMAKE_CXX_FLAGS_DEBUG} --coverage" CACHE STRING "") +set(CMAKE_EXE_LINKER_FLAGS_COVERAGE "${CMAKE_EXE_LINKER_FLAGS_DEBUG} --coverage" CACHE STRING "") +set(CMAKE_SHARED_LINKER_FLAGS_COVERAGE "${CMAKE_SHARED_LINKER_FLAGS_DEBUG} --coverage" CACHE STRING + "" +) +set(CMAKE_STATIC_LINKGER_FLAGS_COVERAGE "${CMAKE_STATIC_LINKER_FLAGS_DEBUG} --coverage" CACHE STRING "" ) -set(CMAKE_STATIC_LINKGER_FLAGS_COVERAGE - "${CMAKE_STATIC_LINKER_FLAGS_DEBUG} --coverage" - CACHE STRING "" -) -set(CMAKE_MODULE_LINKER_FLAGS_COVERAGE - "${CMAKE_MODULE_LINKER_FLAGS_DEBUG} --coverage" - CACHE STRING "" +set(CMAKE_MODULE_LINKER_FLAGS_COVERAGE "${CMAKE_MODULE_LINKER_FLAGS_DEBUG} --coverage" CACHE STRING + "" ) -# Mark the flags for the Coverage build type advanced to prevent accidental -# tampering with the flags +# Mark the flags for the Coverage build type advanced to prevent accidental tampering with the +# flags mark_as_advanced( - CMAKE_C_FLAGS_COVERAGE - CMAKE_CXX_FLAGS_COVERAGE - CMAKE_EXE_LINKER_FLAGS_COVERAGE - CMAKE_SHARED_LINKER_FLAGS_COVERAGE - CMAKE_STATIC_LINKGER_FLAGS_COVERAGE + CMAKE_C_FLAGS_COVERAGE CMAKE_CXX_FLAGS_COVERAGE CMAKE_EXE_LINKER_FLAGS_COVERAGE + CMAKE_SHARED_LINKER_FLAGS_COVERAGE CMAKE_STATIC_LINKGER_FLAGS_COVERAGE CMAKE_MODULE_LINKER_FLAGS_COVERAGE ) @@ -117,26 +71,16 @@ target_include_directories( ch7_custom_build_type PUBLIC include ) -target_compile_features( - ch7_custom_build_type - PUBLIC cxx_std_17 -) +target_compile_features(ch7_custom_build_type PUBLIC cxx_std_17) target_sources( ch7_custom_build_type PRIVATE src/coverage_example/coverage_example.cpp ) -add_executable( - ch7_custom_build_type_test - src/coverage_test.cpp +add_executable(ch7_custom_build_type_test src/coverage_test.cpp) +target_link_libraries( + ch7_custom_build_type_test ch7_custom_build_type ) -target_link_libraries(ch7_custom_build_type_test ch7_custom_build_type) -target_compile_features( - ch7_custom_build_type_test - PRIVATE cxx_std_17 -) -add_test( - NAME code_coverage_test - COMMAND ch7_custom_build_type_test -) +target_compile_features(ch7_custom_build_type_test PRIVATE cxx_std_17) +add_test(NAME code_coverage_test COMMAND ch7_custom_build_type_test) diff --git a/chapter07/fixture_example/CMakeLists.txt b/chapter07/fixture_example/CMakeLists.txt index 1f92b36..4d0fd62 100644 --- a/chapter07/fixture_example/CMakeLists.txt +++ b/chapter07/fixture_example/CMakeLists.txt @@ -7,8 +7,7 @@ cmake_minimum_required(VERSION 3.25...3.30) project( "ch7_fixture_sample" VERSION 1.0 - DESCRIPTION - "A simple C++ project to demonstrate how test fixtures can be run." + DESCRIPTION "A simple C++ project to demonstrate how test fixtures can be run." LANGUAGES CXX ) @@ -28,35 +27,17 @@ target_sources( PRIVATE src/server.cpp ) -# The command to start the server is added as a test and then marked as setup -# function for the fixture named ch7_server -add_test( - NAME start_server - COMMAND ch7_fixture_server -) -set_tests_properties( - start_server - PROPERTIES FIXTURES_SETUP ch7_server -) +# The command to start the server is added as a test and then marked as setup function for the +# fixture named ch7_server +add_test(NAME start_server COMMAND ch7_fixture_server) +set_tests_properties(start_server PROPERTIES FIXTURES_SETUP ch7_server) -# The command to stop the server is added as a test and then marked as a cleanup -# function for the fixture named ch7_server -add_test( - NAME stop_server - COMMAND ch7_fixture_server --stop -) -set_tests_properties( - stop_server - PROPERTIES FIXTURES_CLEANUP ch7_server -) +# The command to stop the server is added as a test and then marked as a cleanup function for the +# fixture named ch7_server +add_test(NAME stop_server COMMAND ch7_fixture_server --stop) +set_tests_properties(stop_server PROPERTIES FIXTURES_CLEANUP ch7_server) -# Define the test for the unit under test and tell CMake that the fixture -# 'ch7_server' is required to run it -add_test( - NAME ch7_fixture_test - COMMAND ch7_fixture_sample -) -set_tests_properties( - ch7_fixture_test - PROPERTIES FIXTURES_REQUIRED ch7_server -) +# Define the test for the unit under test and tell CMake that the fixture 'ch7_server' is required +# to run it +add_test(NAME ch7_fixture_test COMMAND ch7_fixture_sample) +set_tests_properties(ch7_fixture_test PROPERTIES FIXTURES_REQUIRED ch7_server) diff --git a/chapter07/iwyu_example/CMakeLists.txt b/chapter07/iwyu_example/CMakeLists.txt index b71f163..c71e3b2 100644 --- a/chapter07/iwyu_example/CMakeLists.txt +++ b/chapter07/iwyu_example/CMakeLists.txt @@ -7,23 +7,15 @@ cmake_minimum_required(VERSION 3.25...3.30) project( "ch7_iwyu_example" VERSION 1.0 - DESCRIPTION - "A simple C++ project to demonstrate using iwyu on an executable" + DESCRIPTION "A simple C++ project to demonstrate using iwyu on an executable" LANGUAGES CXX ) # if the iwyu program is found it is configured -find_program( - INCLUDE_WHAT_YOU_USE_EXECUTABLE - NAMES include-what-you-use iwyu -) +find_program(INCLUDE_WHAT_YOU_USE_EXECUTABLE NAMES include-what-you-use iwyu) if(INCLUDE_WHAT_YOU_USE_EXECUTABLE) - option( - ENABLE_INCLUDE_WHAT_YOU_USE - "Enable iwyu" - ON - ) + option(ENABLE_INCLUDE_WHAT_YOU_USE "Enable iwyu" ON) list( APPEND @@ -47,7 +39,6 @@ target_sources( if(ENABLE_INCLUDE_WHAT_YOU_USE) message(STATUS "Enabling IWYU for example ${INCLUDE_WHAT_YOU_USE_COMMAND}") set_target_properties( - ch7_iwyu_example - PROPERTIES CXX_INCLUDE_WHAT_YOU_USE "${INCLUDE_WHAT_YOU_USE_COMMAND}" + ch7_iwyu_example PROPERTIES CXX_INCLUDE_WHAT_YOU_USE "${INCLUDE_WHAT_YOU_USE_COMMAND}" ) endif() diff --git a/chapter07/lwyu_example/CMakeLists.txt b/chapter07/lwyu_example/CMakeLists.txt index faf33b2..f395973 100644 --- a/chapter07/lwyu_example/CMakeLists.txt +++ b/chapter07/lwyu_example/CMakeLists.txt @@ -7,8 +7,7 @@ cmake_minimum_required(VERSION 3.25...3.30) project( "ch7_lwyu_example" VERSION 1.0 - DESCRIPTION - "A simple C++ project to demonstrate using lwyu on an executable" + DESCRIPTION "A simple C++ project to demonstrate using lwyu on an executable" LANGUAGES CXX ) @@ -18,28 +17,13 @@ find_package(OpenSSL) if(NOT OPENSSL_FOUND) message(STATUS "OpenSSL not found, disabling link what you use example") -elseif( - CMAKE_CXX_COMPILER_ID - STREQUAL - "GNU" - OR CMAKE_CXX_COMPILER_ID - STREQUAL - "Clang" -) - option( - ENABLE_LINK_WHAT_YOU_USE - "Enable link what you use" - ON - ) +elseif(CMAKE_CXX_COMPILER_ID STREQUAL "GNU" OR CMAKE_CXX_COMPILER_ID STREQUAL "Clang") + option(ENABLE_LINK_WHAT_YOU_USE "Enable link what you use" ON) add_executable(ch7_lwyu_example) - target_sources( - ch7_lwyu_example - PRIVATE src/main.cpp - ) + target_sources(ch7_lwyu_example PRIVATE src/main.cpp) target_link_libraries(ch7_lwyu_example OpenSSL::SSL) set_target_properties( - ch7_lwyu_example - PROPERTIES LINK_WHAT_YOU_USE ${ENABLE_LINK_WHAT_YOU_USE} + ch7_lwyu_example PROPERTIES LINK_WHAT_YOU_USE ${ENABLE_LINK_WHAT_YOU_USE} ) else() message( diff --git a/chapter07/pass_fail_criteria/CMakeLists.txt b/chapter07/pass_fail_criteria/CMakeLists.txt index 62f90dd..0bb87f4 100644 --- a/chapter07/pass_fail_criteria/CMakeLists.txt +++ b/chapter07/pass_fail_criteria/CMakeLists.txt @@ -17,18 +17,12 @@ target_sources( PRIVATE src/main.cpp ) -# Define a test and define that instead of the return value the output of stdout -# should be parsed to determine if the test passes or fails if the output -# contains either 'Success' or 'success' then the test passes if the output -# contains "Warning", "warning", "error" or "Error" then the test fails -add_test( - NAME pass_fail_test - COMMAND ch7_pass_fail_criteria ARG1 -) +# Define a test and define that instead of the return value the output of stdout should be parsed +# to determine if the test passes or fails if the output contains either 'Success' or 'success' +# then the test passes if the output contains "Warning", "warning", "error" or "Error" then the +# test fails +add_test(NAME pass_fail_test COMMAND ch7_pass_fail_criteria ARG1) set_tests_properties( - pass_fail_test - PROPERTIES FAIL_REGULAR_EXPRESSION - "[W|w]arning|[E|e]rror" - PASS_REGULAR_EXPRESSION - "[S|s]uccess" + pass_fail_test PROPERTIES FAIL_REGULAR_EXPRESSION "[W|w]arning|[E|e]rror" + PASS_REGULAR_EXPRESSION "[S|s]uccess" ) diff --git a/chapter07/resource_group_example/CMakeLists.txt b/chapter07/resource_group_example/CMakeLists.txt index 07b0a2a..21a747c 100644 --- a/chapter07/resource_group_example/CMakeLists.txt +++ b/chapter07/resource_group_example/CMakeLists.txt @@ -19,56 +19,32 @@ target_sources( ch7_resource_groups PRIVATE src/main.cpp ) -target_compile_features( - ch7_resource_groups - PRIVATE cxx_std_17 -) +target_compile_features(ch7_resource_groups PRIVATE cxx_std_17) -# Add a test with the following resource needs 2 cpus, 500Mb of RAM A total of -# six instances of server resources and 11 instances of client resources split -# into the following groups: 1 group of 1 server and 1 client 1 groups of 1 -# server and 2 clients four grouos of 1 server and two clients -add_test( - NAME resource_test_1 - COMMAND ch7_resource_groups -) +# Add a test with the following resource needs 2 cpus, 500Mb of RAM A total of six instances of +# server resources and 11 instances of client resources split into the following groups: 1 group of +# 1 server and 1 client 1 groups of 1 server and 2 clients four grouos of 1 server and two clients +add_test(NAME resource_test_1 COMMAND ch7_resource_groups) set_property( TEST resource_test_1 - PROPERTY RESOURCE_GROUPS - cpus:2,mem_mb:500 - servers:1,clients:1 - servers:1,clients:2 + PROPERTY RESOURCE_GROUPS cpus:2,mem_mb:500 servers:1,clients:1 servers:1,clients:2 4,servers:1,clients:2 ) -# Add a test with the following resource needs 2 cpus, 500Mb of RAM A total of -# two instances of server resources and 3 instances of client resources split -# into the following groups: 1 group of 1 server and 1 client 1 groups of 1 -# server and 2 clients -add_test( - NAME resource_test_2 - COMMAND ch7_resource_groups extra_arg -) +# Add a test with the following resource needs 2 cpus, 500Mb of RAM A total of two instances of +# server resources and 3 instances of client resources split into the following groups: 1 group of +# 1 server and 1 client 1 groups of 1 server and 2 clients +add_test(NAME resource_test_2 COMMAND ch7_resource_groups extra_arg) set_property( TEST resource_test_2 - PROPERTY RESOURCE_GROUPS - cpus:2,mem_mb:500 - servers:1,clients:1 - servers:1,clients:2 + PROPERTY RESOURCE_GROUPS cpus:2,mem_mb:500 servers:1,clients:1 servers:1,clients:2 ) -# Add a test with the following resource needs 2 cpus, 8096 Mb of RAM A total of -# two instances of server resources and two instances of client resources split -# into the following groups: 1 group of 1 server and 1 client 1 groups of 1 -# server and 1 clients -add_test( - NAME resource_test_3 - COMMAND ch7_resource_groups extra_arg -) +# Add a test with the following resource needs 2 cpus, 8096 Mb of RAM A total of two instances of +# server resources and two instances of client resources split into the following groups: 1 group +# of 1 server and 1 client 1 groups of 1 server and 1 clients +add_test(NAME resource_test_3 COMMAND ch7_resource_groups extra_arg) set_property( TEST resource_test_3 - PROPERTY RESOURCE_GROUPS - cpus:2,mem_mb:8096 - servers:1,clients:1 - servers:1,clients:1 + PROPERTY RESOURCE_GROUPS cpus:2,mem_mb:8096 servers:1,clients:1 servers:1,clients:1 ) diff --git a/chapter07/simple_sanitizer_example/CMakeLists.txt b/chapter07/simple_sanitizer_example/CMakeLists.txt index 9d77a60..f6ed9f3 100644 --- a/chapter07/simple_sanitizer_example/CMakeLists.txt +++ b/chapter07/simple_sanitizer_example/CMakeLists.txt @@ -7,8 +7,7 @@ cmake_minimum_required(VERSION 3.25...3.30) project( "ch7_simple_sanitizer" VERSION 1.0 - DESCRIPTION - "A simple C++ project to demonstrate using a single sanitizer on an executable" + DESCRIPTION "A simple C++ project to demonstrate using a single sanitizer on an executable" LANGUAGES CXX ) @@ -18,17 +17,10 @@ target_sources( PRIVATE src/main.cpp ) -# Since this test contains a memory use after free to illustrate the use of the -# address sanitizer it is not stable and only enabled if the address sanitizer -# is enabled to show how it is working +# Since this test contains a memory use after free to illustrate the use of the address sanitizer +# it is not stable and only enabled if the address sanitizer is enabled to show how it is working if(ENABLE_ADDRESS_SANITIZER_TEST) - add_test( - NAME sanitizer_test - COMMAND ch7_simple_sanitizer - ) + add_test(NAME sanitizer_test COMMAND ch7_simple_sanitizer) # The test will fail because of address sanitizer violations - set_tests_properties( - sanitizer_test - PROPERTIES WILL_FAIL True - ) + set_tests_properties(sanitizer_test PROPERTIES WILL_FAIL True) endif() diff --git a/chapter07/simple_test/CMakeLists.txt b/chapter07/simple_test/CMakeLists.txt index 02815a4..4e11317 100644 --- a/chapter07/simple_test/CMakeLists.txt +++ b/chapter07/simple_test/CMakeLists.txt @@ -21,14 +21,8 @@ target_sources( ) # Add a test called example_test which executes the command `ch7_simple_test` -add_test( - NAME example_test - COMMAND ch7_simple_test -) +add_test(NAME example_test COMMAND ch7_simple_test) -# Add a test called example_test_2 which executes the command `ch7_simple_test` -# with the argument `arg1` -add_test( - NAME example_test_2 - COMMAND ch7_simple_test arg1 -) +# Add a test called example_test_2 which executes the command `ch7_simple_test` with the argument +# `arg1` +add_test(NAME example_test_2 COMMAND ch7_simple_test arg1) diff --git a/chapter07/test_discovery_example/CMakeLists.txt b/chapter07/test_discovery_example/CMakeLists.txt index 9a82e64..2d9de1e 100644 --- a/chapter07/test_discovery_example/CMakeLists.txt +++ b/chapter07/test_discovery_example/CMakeLists.txt @@ -7,8 +7,7 @@ cmake_minimum_required(VERSION 3.25...3.30) project( "ch7_test_discovery_example" VERSION 1.0 - DESCRIPTION - "A simple C++ project to demonstrate test discovery with Catch2 in CMake" + DESCRIPTION "A simple C++ project to demonstrate test discovery with Catch2 in CMake" LANGUAGES CXX ) @@ -21,11 +20,7 @@ fetchcontent_declare( ) fetchcontent_makeavailable(Catch2) -list( - APPEND - CMAKE_MODULE_PATH - ${Catch2_SOURCE_DIR}/contrib/ -) +list(APPEND CMAKE_MODULE_PATH ${Catch2_SOURCE_DIR}/contrib/) include(CTest) include(Catch) diff --git a/chapter07/test_labels/CMakeLists.txt b/chapter07/test_labels/CMakeLists.txt index bc0ca49..32bb1fc 100644 --- a/chapter07/test_labels/CMakeLists.txt +++ b/chapter07/test_labels/CMakeLists.txt @@ -19,50 +19,20 @@ target_sources( ) # Label the test test `labeled_test_1 with the label "example" -add_test( - NAME labeled_test_1 - COMMAND ch7_test_labels -) -set_tests_properties( - labeled_test_1 - PROPERTIES LABELS "example" -) +add_test(NAME labeled_test_1 COMMAND ch7_test_labels) +set_tests_properties(labeled_test_1 PROPERTIES LABELS "example") -# Label the test test `labeled_test_2 with the label "will_fail" and mark that -# this test is expected to return a non-zero value on success -add_test( - NAME labeled_test_2 - COMMAND ch7_test_labels extra_arg -) -set_tests_properties( - labeled_test_2 - PROPERTIES LABELS - "will_fail" - WILL_FAIL - True -) +# Label the test test `labeled_test_2 with the label "will_fail" and mark that this test is +# expected to return a non-zero value on success +add_test(NAME labeled_test_2 COMMAND ch7_test_labels extra_arg) +set_tests_properties(labeled_test_2 PROPERTIES LABELS "will_fail" WILL_FAIL True) -# Label the test test `labeled_test_3 with the label "example" and "will_fail" -# and mark that this test is expected to return a non-zero value on success -add_test( - NAME labeled_test_3 - COMMAND ch7_test_labels extra_arg -) -set_tests_properties( - labeled_test_3 - PROPERTIES LABELS - "example;will_fail" - WILL_FAIL - True -) +# Label the test test `labeled_test_3 with the label "example" and "will_fail" and mark that this +# test is expected to return a non-zero value on success +add_test(NAME labeled_test_3 COMMAND ch7_test_labels extra_arg) +set_tests_properties(labeled_test_3 PROPERTIES LABELS "example;will_fail" WILL_FAIL True) -# Label the test integration_test_1 with the label "integration test" for -# demonstrating filtering through presets -add_test( - NAME integration_test_1 - COMMAND ch7_test_labels -) -set_tests_properties( - labeled_test_1 - PROPERTIES LABELS "example" -) +# Label the test integration_test_1 with the label "integration test" for demonstrating filtering +# through presets +add_test(NAME integration_test_1 COMMAND ch7_test_labels) +set_tests_properties(labeled_test_1 PROPERTIES LABELS "example") diff --git a/chapter07/timeout_example/CMakeLists.txt b/chapter07/timeout_example/CMakeLists.txt index 776329a..7bb8a75 100644 --- a/chapter07/timeout_example/CMakeLists.txt +++ b/chapter07/timeout_example/CMakeLists.txt @@ -17,18 +17,9 @@ target_sources( ch7_test_timeout_example PRIVATE src/main.cpp ) -target_compile_features( - ch7_test_timeout_example - PRIVATE cxx_std_17 -) +target_compile_features(ch7_test_timeout_example PRIVATE cxx_std_17) -# Mark the test timeout_test with a timeout of 10 seconds if the test does not -# complete by then, it is killed and marked as failing -add_test( - NAME timeout_test - COMMAND ch7_test_timeout_example -) -set_tests_properties( - timeout_test - PROPERTIES TIMEOUT 10 -) +# Mark the test timeout_test with a timeout of 10 seconds if the test does not complete by then, it +# is killed and marked as failing +add_test(NAME timeout_test COMMAND ch7_test_timeout_example) +set_tests_properties(timeout_test PROPERTIES TIMEOUT 10) diff --git a/chapter08/chaining_custom_commands_example/CMakeLists.txt b/chapter08/chaining_custom_commands_example/CMakeLists.txt index d310e2b..8b87778 100644 --- a/chapter08/chaining_custom_commands_example/CMakeLists.txt +++ b/chapter08/chaining_custom_commands_example/CMakeLists.txt @@ -7,45 +7,34 @@ cmake_minimum_required(VERSION 3.25...3.30) project( "ch8_create_tar_archive_example" VERSION 1.0 - DESCRIPTION - "A simple C++ project to demonstrate appending two custom commands" + DESCRIPTION "A simple C++ project to demonstrate appending two custom commands" LANGUAGES CXX ) # Create an executable target -add_executable( - ch8_create_tar_archive_example - src/main.cpp -) +add_executable(ch8_create_tar_archive_example src/main.cpp) -# Add a custom command that creates a tar.gz archive from the output of the -# executable target ch8_create_tar_archive_example +# Add a custom command that creates a tar.gz archive from the output of the executable target +# ch8_create_tar_archive_example add_custom_command( OUTPUT ch8_create_tar_archive_example.tar.gz - COMMAND - cmake -E tar czf - ${CMAKE_CURRENT_BINARY_DIR}/ch8_create_tar_archive_example.tar.gz - $ + COMMAND cmake -E tar czf ${CMAKE_CURRENT_BINARY_DIR}/ch8_create_tar_archive_example.tar.gz + $ COMMENT "Creating Archive for ch8_create_tar_archive_example" VERBATIM ) -# Add another custom command that adds another file to the archive, as this is -# using the same OUTPUT as the first custom command the APPEND keyword is needed -# to chain the commands together +# Add another custom command that adds another file to the archive, as this is using the same +# OUTPUT as the first custom command the APPEND keyword is needed to chain the commands together add_custom_command( OUTPUT ch8_create_tar_archive_example.tar.gz - COMMAND - cmake -E tar czf - ${CMAKE_CURRENT_BINARY_DIR}/ch8_create_tar_archive_example.tar.gz - ${CMAKE_CURRENT_SOURCE_DIR}/SomeFile.txt - APPEND + COMMAND cmake -E tar czf ${CMAKE_CURRENT_BINARY_DIR}/ch8_create_tar_archive_example.tar.gz + ${CMAKE_CURRENT_SOURCE_DIR}/SomeFile.txt APPEND ) -# Add a custom target that depends on the output generated by the custom -# commands if this target is built the custom commands are executed. As the -# first command depends on the output of the executable target, the executable -# target will also be built first +# Add a custom target that depends on the output generated by the custom commands if this target is +# built the custom commands are executed. As the first command depends on the output of the +# executable target, the executable target will also be built first add_custom_target( ch8_create_archive ALL DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/ch8_create_tar_archive_example.tar.gz diff --git a/chapter08/configure_file_example/CMakeLists.txt b/chapter08/configure_file_example/CMakeLists.txt index 4582f2a..91d0ae0 100644 --- a/chapter08/configure_file_example/CMakeLists.txt +++ b/chapter08/configure_file_example/CMakeLists.txt @@ -7,55 +7,35 @@ cmake_minimum_required(VERSION 3.25...3.30) project( "ch8_configure_file_example" VERSION 1.0 - DESCRIPTION - "A simple C++ project to demonstrate usage of configure_file and execute_process" + DESCRIPTION "A simple C++ project to demonstrate usage of configure_file and execute_process" LANGUAGES CXX ) # Search git command as it is needed for the example find_package(Git) if(NOT Git_FOUND) - message( - STATUS - "Git command not found; skipping building the example for execute process" - ) + message(STATUS "Git command not found; skipping building the example for execute process") return() endif() # Make writing the version optional -option( - GIT_VERSION_ENABLE - "Define the git version in a header file" - ON -) +option(GIT_VERSION_ENABLE "Define the git version in a header file" ON) if(GIT_VERSION_ENABLE) - # execute a git command at configuration time and store the results in the - # GIT_REVISION variable strip any trailing whitespaces and threat any - # non-zero return value as fatal error + # execute a git command at configuration time and store the results in the GIT_REVISION + # variable strip any trailing whitespaces and threat any non-zero return value as fatal error execute_process( - COMMAND ${GIT_EXECUTABLE} rev-parse --short HEAD - OUTPUT_VARIABLE GIT_REVISION - OUTPUT_STRIP_TRAILING_WHITESPACE - COMMAND_ERROR_IS_FATAL - ANY + COMMAND ${GIT_EXECUTABLE} rev-parse --short HEAD OUTPUT_VARIABLE GIT_REVISION + OUTPUT_STRIP_TRAILING_WHITESPACE COMMAND_ERROR_IS_FATAL ANY WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} ) endif() -# replace the placeholder in the version.h.in file with the variable -# GIT_REVISION -configure_file( - version.h.in - ${CMAKE_CURRENT_BINARY_DIR}/version.h - @ONLY -) +# replace the placeholder in the version.h.in file with the variable GIT_REVISION +configure_file(version.h.in ${CMAKE_CURRENT_BINARY_DIR}/version.h @ONLY) # create an executable target that uses the generated file -add_executable( - ch8_configure_file_example - src/main.cpp -) +add_executable(ch8_configure_file_example src/main.cpp) target_include_directories( ch8_configure_file_example diff --git a/chapter08/create_source_file_example/CMakeLists.txt b/chapter08/create_source_file_example/CMakeLists.txt index 691c32f..958e50d 100644 --- a/chapter08/create_source_file_example/CMakeLists.txt +++ b/chapter08/create_source_file_example/CMakeLists.txt @@ -12,22 +12,15 @@ project( LANGUAGES CXX ) -# A custom command that creates the main.cpp file for the executable target -# below +# A custom command that creates the main.cpp file for the executable target below add_custom_command( OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/main.cpp - COMMAND - cmake -P ${CMAKE_CURRENT_SOURCE_DIR}/cmake/WriteMain.cmake - ${CMAKE_CURRENT_SOURCE_DIR}/message.txt - ${CMAKE_CURRENT_BINARY_DIR}/main.cpp + COMMAND cmake -P ${CMAKE_CURRENT_SOURCE_DIR}/cmake/WriteMain.cmake + ${CMAKE_CURRENT_SOURCE_DIR}/message.txt ${CMAKE_CURRENT_BINARY_DIR}/main.cpp COMMENT "Creating main.cpp" DEPENDS message.txt VERBATIM ) -# As the target depends on the output of the custom command an implicit -# dependency is created -add_executable( - ch8_create_source_file_example - ${CMAKE_CURRENT_BINARY_DIR}/main.cpp -) +# As the target depends on the output of the custom command an implicit dependency is created +add_executable(ch8_create_source_file_example ${CMAKE_CURRENT_BINARY_DIR}/main.cpp) diff --git a/chapter08/create_source_file_example/cmake/WriteMain.cmake b/chapter08/create_source_file_example/cmake/WriteMain.cmake index deb1ca3..ec7bb7f 100644 --- a/chapter08/create_source_file_example/cmake/WriteMain.cmake +++ b/chapter08/create_source_file_example/cmake/WriteMain.cmake @@ -1,26 +1,13 @@ cmake_minimum_required(VERSION 3.25...3.30) -if(CMAKE_ARGC - LESS - 5 -) - message( - FATAL_ERROR "Usage: cmake -P WriteMain.cmake message_file target_file" - ) +if(CMAKE_ARGC LESS 5) + message(FATAL_ERROR "Usage: cmake -P WriteMain.cmake message_file target_file") endif() -set(MESSAGE_FILE - ${CMAKE_ARGV3} -) -set(TARGET_FILE - ${CMAKE_ARGV4} -) +set(MESSAGE_FILE ${CMAKE_ARGV3}) +set(TARGET_FILE ${CMAKE_ARGV4}) -file( - READ - ${MESSAGE_FILE} - MESSAGE -) +file(READ ${MESSAGE_FILE} MESSAGE) set(MAIN_CC_CODE " @@ -32,7 +19,4 @@ int main() { ) # write the hash to a new file -file( - WRITE "${TARGET_FILE}" - "${MAIN_CC_CODE}" -) +file(WRITE "${TARGET_FILE}" "${MAIN_CC_CODE}") diff --git a/chapter08/custom_command_example/CMakeLists.txt b/chapter08/custom_command_example/CMakeLists.txt index 18b1a79..511a361 100644 --- a/chapter08/custom_command_example/CMakeLists.txt +++ b/chapter08/custom_command_example/CMakeLists.txt @@ -13,14 +13,10 @@ project( ) # define a executable target -add_executable( - ch8_custom_command_example - src/main.cpp -) +add_executable(ch8_custom_command_example src/main.cpp) -# add a custom command as post build action to the executable target. the -# command creates a sha256 hash of the output file created by the executable -# target by using cmake in script mode +# add a custom command as post build action to the executable target. the command creates a sha256 +# hash of the output file created by the executable target by using cmake in script mode add_custom_command( TARGET ch8_custom_command_example POST_BUILD diff --git a/chapter08/custom_command_example/cmake/CreateSha256.cmake b/chapter08/custom_command_example/cmake/CreateSha256.cmake index ab20130..0b2b9e5 100644 --- a/chapter08/custom_command_example/cmake/CreateSha256.cmake +++ b/chapter08/custom_command_example/cmake/CreateSha256.cmake @@ -1,36 +1,17 @@ cmake_minimum_required(VERSION 3.25...3.30) -if(CMAKE_ARGC - LESS - 5 -) - message( - FATAL_ERROR - "Usage: cmake -P CreateSha256.cmake file_to_hash target_file" - ) +if(CMAKE_ARGC LESS 5) + message(FATAL_ERROR "Usage: cmake -P CreateSha256.cmake file_to_hash target_file") endif() -set(FILE_TO_HASH - ${CMAKE_ARGV3} -) -set(TARGET_FILE - ${CMAKE_ARGV4} -) +set(FILE_TO_HASH ${CMAKE_ARGV3}) +set(TARGET_FILE ${CMAKE_ARGV4}) -message( - STATUS "Writing sha256 for file '${FILE_TO_HASH}' to file '${TARGET_FILE}'" -) +message(STATUS "Writing sha256 for file '${FILE_TO_HASH}' to file '${TARGET_FILE}'") # Read the source file and generate the hash for it -file( - SHA256 - "${FILE_TO_HASH}" - GENERATED_HASH -) +file(SHA256 "${FILE_TO_HASH}" GENERATED_HASH) message(STATUS "sha256 for file '${FILE_TO_HASH}': ${GENERATED_HASH}") # write the hash to a new file -file( - WRITE "${TARGET_FILE}" - "${GENERATED_HASH}" -) +file(WRITE "${TARGET_FILE}" "${GENERATED_HASH}") diff --git a/chapter08/custom_target_dependency_example/CMakeLists.txt b/chapter08/custom_target_dependency_example/CMakeLists.txt index d1ba7a8..78a5d83 100644 --- a/chapter08/custom_target_dependency_example/CMakeLists.txt +++ b/chapter08/custom_target_dependency_example/CMakeLists.txt @@ -12,10 +12,9 @@ project( LANGUAGES CXX ) -# create a custom target that uses cmake in script mode to create a sha256 hash -# of the output file created by another target using the $" ) -add_executable( - ch8_generate_hash_using_a_target - src/main.cpp -) +add_executable(ch8_generate_hash_using_a_target src/main.cpp) diff --git a/chapter08/custom_target_dependency_example/cmake/CreateSha256.cmake b/chapter08/custom_target_dependency_example/cmake/CreateSha256.cmake index ab20130..0b2b9e5 100644 --- a/chapter08/custom_target_dependency_example/cmake/CreateSha256.cmake +++ b/chapter08/custom_target_dependency_example/cmake/CreateSha256.cmake @@ -1,36 +1,17 @@ cmake_minimum_required(VERSION 3.25...3.30) -if(CMAKE_ARGC - LESS - 5 -) - message( - FATAL_ERROR - "Usage: cmake -P CreateSha256.cmake file_to_hash target_file" - ) +if(CMAKE_ARGC LESS 5) + message(FATAL_ERROR "Usage: cmake -P CreateSha256.cmake file_to_hash target_file") endif() -set(FILE_TO_HASH - ${CMAKE_ARGV3} -) -set(TARGET_FILE - ${CMAKE_ARGV4} -) +set(FILE_TO_HASH ${CMAKE_ARGV3}) +set(TARGET_FILE ${CMAKE_ARGV4}) -message( - STATUS "Writing sha256 for file '${FILE_TO_HASH}' to file '${TARGET_FILE}'" -) +message(STATUS "Writing sha256 for file '${FILE_TO_HASH}' to file '${TARGET_FILE}'") # Read the source file and generate the hash for it -file( - SHA256 - "${FILE_TO_HASH}" - GENERATED_HASH -) +file(SHA256 "${FILE_TO_HASH}" GENERATED_HASH) message(STATUS "sha256 for file '${FILE_TO_HASH}': ${GENERATED_HASH}") # write the hash to a new file -file( - WRITE "${TARGET_FILE}" - "${GENERATED_HASH}" -) +file(WRITE "${TARGET_FILE}" "${GENERATED_HASH}") diff --git a/chapter08/custom_target_example/CMakeLists.txt b/chapter08/custom_target_example/CMakeLists.txt index f8c1211..4a955e7 100644 --- a/chapter08/custom_target_example/CMakeLists.txt +++ b/chapter08/custom_target_example/CMakeLists.txt @@ -7,26 +7,17 @@ cmake_minimum_required(VERSION 3.25...3.30) project( "ch8_custom_target_example" VERSION 1.0 - DESCRIPTION - "A simple C++ project to demonstrate creation of a custom target" + DESCRIPTION "A simple C++ project to demonstrate creation of a custom target" LANGUAGES CXX ) # create a list to be used in the custom command -list( - APPEND - MYLIST - "A" - "B" - "C" -) +list(APPEND MYLIST "A" "B" "C") -# Adds a custom target that prints out a message to stdout. Adding the ALL -# keyword adds the target to the "all" meta-target COMMAND_EXPAND_LIST causes -# the variable ${MYLIST} to be expanded as a string. if this is not set B and C -# will be treated as separate commands +# Adds a custom target that prints out a message to stdout. Adding the ALL keyword adds the target +# to the "all" meta-target COMMAND_EXPAND_LIST causes the variable ${MYLIST} to be expanded as a +# string. if this is not set B and C will be treated as separate commands add_custom_target( ch8_echo_target # XXX ALL - COMMAND cmake -E echo "Hello from a custom target in chapter 8 ${MYLIST}" - COMMAND_EXPAND_LISTS + COMMAND cmake -E echo "Hello from a custom target in chapter 8 ${MYLIST}" COMMAND_EXPAND_LISTS ) diff --git a/chapter08/execute_process_example/CMakeLists.txt b/chapter08/execute_process_example/CMakeLists.txt index de6d783..f121408 100644 --- a/chapter08/execute_process_example/CMakeLists.txt +++ b/chapter08/execute_process_example/CMakeLists.txt @@ -14,33 +14,20 @@ project( # find git executalbe, if not found skip the example find_package(Git) if(NOT Git_FOUND) - message( - STATUS - "Git command not found; skipping building the example for execute process" - ) + message(STATUS "Git command not found; skipping building the example for execute process") return() endif() -# execute a git command at configuration time and store the results in the -# GIT_REVISION variable strip any trailing whitespaces and threat any non-zero -# return value as fatal error +# execute a git command at configuration time and store the results in the GIT_REVISION variable +# strip any trailing whitespaces and threat any non-zero return value as fatal error execute_process( - COMMAND ${GIT_EXECUTABLE} rev-parse --short HEAD - OUTPUT_VARIABLE GIT_REVISION - OUTPUT_STRIP_TRAILING_WHITESPACE - COMMAND_ERROR_IS_FATAL - ANY + COMMAND ${GIT_EXECUTABLE} rev-parse --short HEAD OUTPUT_VARIABLE GIT_REVISION + OUTPUT_STRIP_TRAILING_WHITESPACE COMMAND_ERROR_IS_FATAL ANY WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} ) -add_executable( - ch8_execute_process_example - src/main.cpp -) +add_executable(ch8_execute_process_example src/main.cpp) -# Add a compile definition to the executable target that uses the GIT_REVISION -# variable retrieved with execute_process -target_compile_definitions( - ch8_execute_process_example - PRIVATE VERSION=\"${GIT_REVISION}\" -) +# Add a compile definition to the executable target that uses the GIT_REVISION variable retrieved +# with execute_process +target_compile_definitions(ch8_execute_process_example PRIVATE VERSION=\"${GIT_REVISION}\") diff --git a/chapter09/docker_minimal/CMakeLists.txt b/chapter09/docker_minimal/CMakeLists.txt index b0592b9..1a7db12 100644 --- a/chapter09/docker_minimal/CMakeLists.txt +++ b/chapter09/docker_minimal/CMakeLists.txt @@ -11,7 +11,4 @@ project( LANGUAGES CXX ) -add_executable( - ch9_simple_executable - src/main.cc -) +add_executable(ch9_simple_executable src/main.cc) diff --git a/chapter10/ex01_external_deps/CMakeLists.txt b/chapter10/ex01_external_deps/CMakeLists.txt index 883d643..8380c28 100644 --- a/chapter10/ex01_external_deps/CMakeLists.txt +++ b/chapter10/ex01_external_deps/CMakeLists.txt @@ -12,27 +12,15 @@ project( LANGUAGES CXX ) -option( - CH10_EX01_USE_SUPERBUILD - "Enable superbuild for chapter 10, example 01" - OFF -) +option(CH10_EX01_USE_SUPERBUILD "Enable superbuild for chapter 10, example 01" OFF) if(CH10_EX01_USE_SUPERBUILD) include(superbuild.cmake) else() - # User decided to not to use superbuild. We have to discover the - # dependencies from the current environment as usual. - find_package( - GTest - 1.10.0 - REQUIRED - ) - find_package( - benchmark - 1.6.1 - REQUIRED - ) + # User decided to not to use superbuild. We have to discover the dependencies from the current + # environment as usual. + find_package(GTest 1.10.0 REQUIRED) + find_package(benchmark 1.6.1 REQUIRED) endif() # A simple unit test target diff --git a/chapter10/ex01_external_deps/superbuild.cmake b/chapter10/ex01_external_deps/superbuild.cmake index ed970fd..6852707 100644 --- a/chapter10/ex01_external_deps/superbuild.cmake +++ b/chapter10/ex01_external_deps/superbuild.cmake @@ -2,8 +2,8 @@ # # SPDX-License-Identifier: MIT -# We will utilize the FetchContent module for satisfying the google benchmark -# and google test dependencies. +# We will utilize the FetchContent module for satisfying the google benchmark and google test +# dependencies. include(FetchContent) message( @@ -11,36 +11,29 @@ message( "Chapter 10, example 01 superbuild enabled. Will try to satisfy dependencies for the example." ) -# Declare google benchmark dependency details. We will explicitly require 1.6.1 -# version this time. +# Declare google benchmark dependency details. We will explicitly require 1.6.1 version this time. fetchcontent_declare( benchmark GIT_REPOSITORY https://github.com/google/benchmark.git GIT_TAG v1.6.1 ) -# Declare google test dependency details. We will explicitly require 1.10.0 -# version this time. +# Declare google test dependency details. We will explicitly require 1.10.0 version this time. fetchcontent_declare( GTest GIT_REPOSITORY https://github.com/google/googletest.git GIT_TAG release-1.11.0 ) -# Since Gtest 1.14 has the minimum required C++ standard set to 14 this is -# enforced here +# Since Gtest 1.14 has the minimum required C++ standard set to 14 this is enforced here -# Declaring dependencies via FetchContent_Declare does not make them -# "available", meaning they will not be downloaded nor be installed until we -# call FetchContent_MakeAvailable() or FetchContent_Populate(). +# Declaring dependencies via FetchContent_Declare does not make them "available", meaning they will +# not be downloaded nor be installed until we call FetchContent_MakeAvailable() or +# FetchContent_Populate(). fetchcontent_makeavailable(GTest benchmark) -# To make target names compatible with find_package(...), so we can switch -# between superbuild and find_package_(...) easily. -add_library( - GTest::Main - ALIAS - gtest_main -) -# Google benchmark already defines benchmark::benchmark target, so it is not -# necessary to define an alias for it. +# To make target names compatible with find_package(...), so we can switch between superbuild and +# find_package_(...) easily. +add_library(GTest::Main ALIAS gtest_main) +# Google benchmark already defines benchmark::benchmark target, so it is not necessary to define an +# alias for it. diff --git a/chapter10/ex02_external_deps_with_extproject/CMakeLists.txt b/chapter10/ex02_external_deps_with_extproject/CMakeLists.txt index c1da708..e28aa4f 100644 --- a/chapter10/ex02_external_deps_with_extproject/CMakeLists.txt +++ b/chapter10/ex02_external_deps_with_extproject/CMakeLists.txt @@ -13,11 +13,10 @@ project( LANGUAGES CXX ) -# Since ExternalProject_Add makes the content available in cmake build step, -# there is no clean way to utilize FindPackage(), since FindPackage() requires -# package files to be present in the configure stage. For such scenarios, it is -# better to stick with FetchContent since it allows you to fetch the content in -# configure stage. +# Since ExternalProject_Add makes the content available in cmake build step, there is no clean way +# to utilize FindPackage(), since FindPackage() requires package files to be present in the +# configure stage. For such scenarios, it is better to stick with FetchContent since it allows you +# to fetch the content in configure stage. include(superbuild.cmake) # A simple unit test target diff --git a/chapter10/ex02_external_deps_with_extproject/superbuild.cmake b/chapter10/ex02_external_deps_with_extproject/superbuild.cmake index 68eda67..cc4a25c 100644 --- a/chapter10/ex02_external_deps_with_extproject/superbuild.cmake +++ b/chapter10/ex02_external_deps_with_extproject/superbuild.cmake @@ -2,12 +2,10 @@ # # SPDX-License-Identifier: MIT -# We will utilize the ExternalProject module for satisfying the catch2 -# dependency. +# We will utilize the ExternalProject module for satisfying the catch2 dependency. include(ExternalProject) -# Declare catch2 dependency details. We will explicitly require 2.13.9 version -# this time. +# Declare catch2 dependency details. We will explicitly require 2.13.9 version this time. externalproject_add( catch2_download GIT_REPOSITORY https://github.com/catchorg/Catch2.git @@ -22,24 +20,19 @@ set(CATCH2_INCLUDE_DIR ${CMAKE_CURRENT_BINARY_DIR}/catch2_download-prefix/src/catch2_download/single_include ) -# Since the include directory of the catch2 is not available at the configure -# stage (ExternalProject_Add retrieves and builds the dependency at build stage) -# we'll just go ahead and create the include directory, so we can list that -# directory in INTERFACE_INCLUDE_DIRECTORIES property of the catch2 target. +# Since the include directory of the catch2 is not available at the configure stage +# (ExternalProject_Add retrieves and builds the dependency at build stage) we'll just go ahead and +# create the include directory, so we can list that directory in INTERFACE_INCLUDE_DIRECTORIES +# property of the catch2 target. file(MAKE_DIRECTORY ${CATCH2_INCLUDE_DIR}) # Define an imported library for the catch2 -add_library( - catch2 - IMPORTED - INTERFACE -) -# Make the library dependent on the ExternalProject target, so CMake knows that -# in order to use this target, the ExternalProject target must be built first +add_library(catch2 IMPORTED INTERFACE) +# Make the library dependent on the ExternalProject target, so CMake knows that in order to use +# this target, the ExternalProject target must be built first add_dependencies(catch2 catch2_download) -# Add CATCH2_INCLUDE_DIR to INTERFACE_INCLUDE_DIRECTORIES of the catch2 target, -# so the libraries depending on catch2 will automatically be able to include -# catch2 headers. +# Add CATCH2_INCLUDE_DIR to INTERFACE_INCLUDE_DIRECTORIES of the catch2 target, so the libraries +# depending on catch2 will automatically be able to include catch2 headers. set_target_properties( catch2 PROPERTIES "INTERFACE_INCLUDE_DIRECTORIES" "${CATCH2_INCLUDE_DIR}" diff --git a/chapter10/ex03_simple_qt_app/CMakeLists.txt b/chapter10/ex03_simple_qt_app/CMakeLists.txt index 52f2e63..ec314c2 100644 --- a/chapter10/ex03_simple_qt_app/CMakeLists.txt +++ b/chapter10/ex03_simple_qt_app/CMakeLists.txt @@ -18,40 +18,21 @@ project( if(CH10_EX03_USE_SUPERBUILD) include(superbuild.cmake) else() - set(CMAKE_AUTOMOC - ON - ) - set(CMAKE_AUTORCC - ON - ) - set(CMAKE_AUTOUIC - ON - ) - # User decided to not to use superbuild. We have to discover the - # dependencies from the current environment as usual. We need `Core` and - # Widgets QT components for our simple QT app. - find_package( - Qt6 - COMPONENTS Core Widgets - REQUIRED - ) + set(CMAKE_AUTOMOC ON) + set(CMAKE_AUTORCC ON) + set(CMAKE_AUTOUIC ON) + # User decided to not to use superbuild. We have to discover the dependencies from the current + # environment as usual. We need `Core` and Widgets QT components for our simple QT app. + find_package(Qt6 COMPONENTS Core Widgets REQUIRED) endif() # Define the executable target -add_executable( - ch10_ex03_simple_qt_app - main.cpp -) +add_executable(ch10_ex03_simple_qt_app main.cpp) # Set C++ standard for the executable -target_compile_features( - ch10_ex03_simple_qt_app - PRIVATE cxx_std_17 -) +target_compile_features(ch10_ex03_simple_qt_app PRIVATE cxx_std_17) # Link QT components to the executable target_link_libraries( - ch10_ex03_simple_qt_app - Qt6::Core - Qt6::Widgets + ch10_ex03_simple_qt_app Qt6::Core Qt6::Widgets ) diff --git a/chapter10/ex03_simple_qt_app/superbuild.cmake b/chapter10/ex03_simple_qt_app/superbuild.cmake index 3fe8ffe..f36569b 100644 --- a/chapter10/ex03_simple_qt_app/superbuild.cmake +++ b/chapter10/ex03_simple_qt_app/superbuild.cmake @@ -2,8 +2,8 @@ # # SPDX-License-Identifier: MIT -# We will utilize the FetchContent module for satisfying the google benchmark -# and google test dependencies. +# We will utilize the FetchContent module for satisfying the google benchmark and google test +# dependencies. include(FetchContent) message( @@ -11,59 +11,36 @@ message( "Chapter 10, example 03 superbuild enabled. Will try to satisfy dependencies for the example." ) -set(FETCHCONTENT_QUIET - FALSE -) # Enable message output for FetchContent commands +set(FETCHCONTENT_QUIET FALSE) # Enable message output for FetchContent commands -# May require installing libgl1-mesa-dev libglu1-mesa-dev '^libxcb.*-dev' -# libx11-xcb-dev libglu1-mesa-dev libxrender-dev libxi-dev libxkbcommon-dev -# libxkbcommon-x11-dev packages. (or equivalent) +# May require installing libgl1-mesa-dev libglu1-mesa-dev '^libxcb.*-dev' libx11-xcb-dev +# libglu1-mesa-dev libxrender-dev libxi-dev libxkbcommon-dev libxkbcommon-x11-dev packages. (or +# equivalent) # Only build the following QT submodules: -set(QT_BUILD_SUBMODULES - "qtbase" - CACHE STRING "Submodules to build" -) -set(QT_WILL_BUILD_TOOLS - on -) +set(QT_BUILD_SUBMODULES "qtbase" CACHE STRING "Submodules to build") +set(QT_WILL_BUILD_TOOLS on) # Disable some unnecessary QT features to reduce the build time. -set(QT_FEATURE_sql - off -) -set(QT_FEATURE_network - off -) -set(QT_FEATURE_dbus - off -) -set(QT_FEATURE_testlib - off -) -set(QT_BUILD_STANDALONE_TESTS - off -) -set(QT_BUILD_EXAMPLES - off -) -set(QT_BUILD_TESTS - off -) - -# Declare QT6 dependency details. We will explicitly require 6.3.0 version this -# time. +set(QT_FEATURE_sql off) +set(QT_FEATURE_network off) +set(QT_FEATURE_dbus off) +set(QT_FEATURE_testlib off) +set(QT_BUILD_STANDALONE_TESTS off) +set(QT_BUILD_EXAMPLES off) +set(QT_BUILD_TESTS off) + +# Declare QT6 dependency details. We will explicitly require 6.3.0 version this time. fetchcontent_declare( qt6 GIT_REPOSITORY https://github.com/qt/qt5.git GIT_TAG v6.3.0 GIT_SHALLOW TRUE - GIT_PROGRESS TRUE # Since the clone process is lengthy, show progress of - # download + GIT_PROGRESS TRUE # Since the clone process is lengthy, show progress of download GIT_SUBMODULES qtbase # The only QT submodule we need ) -# Declaring dependencies via FetchContent_Declare does not make them -# "available", meaning they will not be downloaded nor be installed until we -# call FetchContent_MakeAvailable()a or FetchContent_Populate(). +# Declaring dependencies via FetchContent_Declare does not make them "available", meaning they will +# not be downloaded nor be installed until we call FetchContent_MakeAvailable()a or +# FetchContent_Populate(). fetchcontent_makeavailable(qt6) diff --git a/chapter11/CMakeLists.txt b/chapter11/CMakeLists.txt index d6b474a..49f8084 100644 --- a/chapter11/CMakeLists.txt +++ b/chapter11/CMakeLists.txt @@ -7,8 +7,7 @@ cmake_minimum_required(VERSION 3.25...3.30) project( chapter11 VERSION 1.0 - DESCRIPTION - "Examples for chapter 11 of the CMake Best Practices Book - Apple specifics" + DESCRIPTION "Examples for chapter 11 of the CMake Best Practices Book - Apple specifics" LANGUAGES CXX OBJCXX ) diff --git a/chapter11/app_bundle_example/CMakeLists.txt b/chapter11/app_bundle_example/CMakeLists.txt index efc0fe3..3100375 100644 --- a/chapter11/app_bundle_example/CMakeLists.txt +++ b/chapter11/app_bundle_example/CMakeLists.txt @@ -9,14 +9,10 @@ project( ) # Add the executable -add_executable( - ch11_app_bundle_example MACOSX_BUNDLE - src/main.mm -) +add_executable(ch11_app_bundle_example MACOSX_BUNDLE src/main.mm) # Link the Cocoa framework -target_link_libraries(ch11_app_bundle_example "-framework Cocoa") -target_compile_features( - ch11_app_bundle_example - PRIVATE cxx_std_17 +target_link_libraries( + ch11_app_bundle_example "-framework Cocoa" ) +target_compile_features(ch11_app_bundle_example PRIVATE cxx_std_17) diff --git a/chapter11/app_bundle_example_custom_plist/CMakeLists.txt b/chapter11/app_bundle_example_custom_plist/CMakeLists.txt index bb9b19a..8e2d23d 100644 --- a/chapter11/app_bundle_example_custom_plist/CMakeLists.txt +++ b/chapter11/app_bundle_example_custom_plist/CMakeLists.txt @@ -9,15 +9,13 @@ project( ) # Add the executable -add_executable( - ch11_app_bundle_example_custom_plist MACOSX_BUNDLE - src/main.mm -) +add_executable(ch11_app_bundle_example_custom_plist MACOSX_BUNDLE src/main.mm) set_target_properties( ch11_app_bundle_example_custom_plist - PROPERTIES MACOSX_BUNDLE_INFO_PLIST - "${CMAKE_CURRENT_SOURCE_DIR}/Info.plist.in" + PROPERTIES MACOSX_BUNDLE_INFO_PLIST "${CMAKE_CURRENT_SOURCE_DIR}/Info.plist.in" ) # Link the Cocoa framework -target_link_libraries(ch11_app_bundle_example_custom_plist "-framework Cocoa") +target_link_libraries( + ch11_app_bundle_example_custom_plist "-framework Cocoa" +) diff --git a/chapter11/app_bundle_storyboard/CMakeLists.txt b/chapter11/app_bundle_storyboard/CMakeLists.txt index 7ccd25a..340fad6 100644 --- a/chapter11/app_bundle_storyboard/CMakeLists.txt +++ b/chapter11/app_bundle_storyboard/CMakeLists.txt @@ -8,22 +8,16 @@ project( LANGUAGES CXX OBJCXX ) -set(resource_files - ${CMAKE_CURRENT_SOURCE_DIR}/storyboard/main.storyboard -) +set(resource_files ${CMAKE_CURRENT_SOURCE_DIR}/storyboard/main.storyboard) # Add the executable -add_executable( - ch11_app_bundle_storyboard MACOSX_BUNDLE - src/main.mm -) +add_executable(ch11_app_bundle_storyboard MACOSX_BUNDLE src/main.mm) target_sources( ch11_app_bundle_storyboard PRIVATE ${resource_files} ) -set_source_files_properties( - ${resource_files} - PROPERTIES MACOSX_PACKAGE_LOCATION Resources +set_source_files_properties(${resource_files} PROPERTIES MACOSX_PACKAGE_LOCATION Resources) +target_link_libraries( + ch11_app_bundle_storyboard "-framework UIKit" ) -target_link_libraries(ch11_app_bundle_storyboard "-framework UIKit") diff --git a/chapter11/framework_example/CMakeLists.txt b/chapter11/framework_example/CMakeLists.txt index 2a7608c..e8dd36f 100644 --- a/chapter11/framework_example/CMakeLists.txt +++ b/chapter11/framework_example/CMakeLists.txt @@ -6,28 +6,21 @@ cmake_minimum_required(VERSION 3.25...3.30) project( ch11_framework_example VERSION 1.0.0 - DESCRIPTION - "A simple C++ project to demonstrate creating executables and libraries in CMake" + DESCRIPTION "A simple C++ project to demonstrate creating executables and libraries in CMake" LANGUAGES CXX ) # add the library target add_library(ch11_framework_example SHARED) -# set properties for the target. VERSION set the library version to the project -# version * SOVERSION set the compatibility version for the library to the -# major number of the version +# set properties for the target. VERSION set the library version to the project version * SOVERSION +# set the compatibility version for the library to the major number of the version set_target_properties( ch11_framework_example - PROPERTIES FRAMEWORK TRUE - FRAMEWORK_VERSION 1 - PUBLIC_HEADER include/hello.hpp - PRIVATE_HEADER src/internal.hpp -) -target_compile_features( - ch11_framework_example - PRIVATE cxx_std_17 + PROPERTIES FRAMEWORK TRUE FRAMEWORK_VERSION 1 PUBLIC_HEADER include/hello.hpp PRIVATE_HEADER + src/internal.hpp ) +target_compile_features(ch11_framework_example PRIVATE cxx_std_17) # add sources to the library target target_sources( diff --git a/chapter11/hello_world_apple/CMakeLists.txt b/chapter11/hello_world_apple/CMakeLists.txt index 3bccaf9..2b50f0e 100644 --- a/chapter11/hello_world_apple/CMakeLists.txt +++ b/chapter11/hello_world_apple/CMakeLists.txt @@ -9,27 +9,17 @@ project( ) # set some global XCode properties -set(CMAKE_XCODE_ATTRIBUTE_CODE_SIGN_IDENTITY - "Apple Development" - CACHE STRING "" -) -set(CMAKE_XCODE_ATTRIBUTE_DEVELOPMENT_TEAM - "CMake Best Practice Authors" - CACHE STRING "" -) +set(CMAKE_XCODE_ATTRIBUTE_CODE_SIGN_IDENTITY "Apple Development" CACHE STRING "") +set(CMAKE_XCODE_ATTRIBUTE_DEVELOPMENT_TEAM "CMake Best Practice Authors" CACHE STRING "") # Add the executable -add_executable( - ch11_hello_world_apple - src/main.mm -) +add_executable(ch11_hello_world_apple src/main.mm) # Link the Cocoa framework -target_link_libraries(ch11_hello_world_apple "-framework Cocoa") -target_compile_features( - ch11_hello_world_apple - PRIVATE cxx_std_17 +target_link_libraries( + ch11_hello_world_apple "-framework Cocoa" ) +target_compile_features(ch11_hello_world_apple PRIVATE cxx_std_17) # set target properties for macOS set_target_properties( diff --git a/chapter12/CMakeLists.txt b/chapter12/CMakeLists.txt index ef7c19e..ad231f6 100644 --- a/chapter12/CMakeLists.txt +++ b/chapter12/CMakeLists.txt @@ -11,7 +11,7 @@ project( "A collection of sample C++ projects to demonstrate cross-compiling, toolchains and toolchain checks" LANGUAGES CXX ) -# The examples ion android-example and iOS_example are to be used standalone -# with the the respective SDKs and NDKs +# The examples ion android-example and iOS_example are to be used standalone with the the +# respective SDKs and NDKs add_subdirectory(check_source_compiles) add_subdirectory(check_push_pop) diff --git a/chapter12/android-example/CMakeLists.txt b/chapter12/android-example/CMakeLists.txt index 34419b5..e8dc67c 100644 --- a/chapter12/android-example/CMakeLists.txt +++ b/chapter12/android-example/CMakeLists.txt @@ -7,21 +7,18 @@ cmake_minimum_required(VERSION 3.25...3.30) project( androidlib VERSION 1.0.0 - DESCRIPTION - "A simple C++ project to demonstrate creating binaries for android" + DESCRIPTION "A simple C++ project to demonstrate creating binaries for android" LANGUAGES CXX ) # add the library target add_library(androidlib) -# set properties for the target. VERSION set the library version to the project -# version * SOVERSION set the compatibility version for the library to the -# major number of the version +# set properties for the target. VERSION set the library version to the project version * SOVERSION +# set the compatibility version for the library to the major number of the version set_target_properties( androidlib - PROPERTIES VERSION ${PROJECT_VERSION} - SOVERSION ${PROJECT_VERSION_MAJOR} + PROPERTIES VERSION ${PROJECT_VERSION} SOVERSION ${PROJECT_VERSION_MAJOR} ) # add sources to the library target @@ -30,12 +27,8 @@ target_sources( PRIVATE src/hello.cpp src/internal.cpp ) -# define the C++ standard needed to compile this library and make it visible to -# dependers -target_compile_features( - androidlib - PUBLIC cxx_std_17 -) +# define the C++ standard needed to compile this library and make it visible to dependers +target_compile_features(androidlib PUBLIC cxx_std_17) # set the include directories target_include_directories( diff --git a/chapter12/check_push_pop/CMakeLists.txt b/chapter12/check_push_pop/CMakeLists.txt index 4ede4f0..1b07fce 100644 --- a/chapter12/check_push_pop/CMakeLists.txt +++ b/chapter12/check_push_pop/CMakeLists.txt @@ -7,8 +7,7 @@ cmake_minimum_required(VERSION 3.25...3.30) project( ch12_check_push_pop VERSION 1.0 - DESCRIPTION - "Example to show cmake_push_check_state and cmake_pop_check_state" + DESCRIPTION "Example to show cmake_push_check_state and cmake_pop_check_state" LANGUAGES CXX ) @@ -21,33 +20,20 @@ cmake_reset_check_state() # use a checkmodule to check if the current C++ compiler supports the -Wall flag include(CheckCompilerFlag) -check_compiler_flag( - CXX - -Wall - WALL_FLAG_SUPPORTED -) +check_compiler_flag(CXX -Wall WALL_FLAG_SUPPORTED) # if the flag is set, set it as a required flag if(WALL_FLAG_SUPPORTED) - set(CMAKE_REQUIRED_FLAGS - -Wall - ) + set(CMAKE_REQUIRED_FLAGS -Wall) # Preserve -Wall and add more things for extra checks cmake_push_check_state() # set the current include dir as required - set(CMAKE_REQUIRED_INCLUDES - ${CMAKE_CURRENT_SOURCE_DIR}/include - ) + set(CMAKE_REQUIRED_INCLUDES ${CMAKE_CURRENT_SOURCE_DIR}/include) - # use the checkSymbolExists module to check if the symbol `hello` exists in - # the hello.hpp + # use the checkSymbolExists module to check if the symbol `hello` exists in the hello.hpp include(CheckSymbolExists) - check_symbol_exists( - hello - "hello.hpp" - HAVE_HELLO_SYMBOL - ) + check_symbol_exists(hello "hello.hpp" HAVE_HELLO_SYMBOL) # reset the check state cmake_pop_check_state() diff --git a/chapter12/emulator-example/CMakeLists.txt b/chapter12/emulator-example/CMakeLists.txt index c5f606f..27b2a0a 100644 --- a/chapter12/emulator-example/CMakeLists.txt +++ b/chapter12/emulator-example/CMakeLists.txt @@ -25,7 +25,4 @@ target_sources( PRIVATE src/main.cpp ) -add_test( - NAME emulator_test - COMMAND ch12_emulator_example -) +add_test(NAME emulator_test COMMAND ch12_emulator_example) diff --git a/chapter12/emulator-example/arm64.toolchain.cmake b/chapter12/emulator-example/arm64.toolchain.cmake index c94ec7e..5cc76fb 100644 --- a/chapter12/emulator-example/arm64.toolchain.cmake +++ b/chapter12/emulator-example/arm64.toolchain.cmake @@ -1,14 +1,6 @@ -set(CMAKE_SYSTEM_NAME - Linux -) -set(CMAKE_SYSTEM_PROCESSOR - arm -) +set(CMAKE_SYSTEM_NAME Linux) +set(CMAKE_SYSTEM_PROCESSOR arm) -set(CMAKE_CXX_COMPILER - /usr/bin/arm-linux-gnueabihf-g++ -) +set(CMAKE_CXX_COMPILER /usr/bin/arm-linux-gnueabihf-g++) -set(CMAKE_CROSSCOMPILING_EMULATOR - "qemu-arm;-L;/usr/arm-linux-gnueabihf" -) +set(CMAKE_CROSSCOMPILING_EMULATOR "qemu-arm;-L;/usr/arm-linux-gnueabihf") diff --git a/chapter12/iOS_example/CMakeLists.txt b/chapter12/iOS_example/CMakeLists.txt index df9b064..b74ff22 100644 --- a/chapter12/iOS_example/CMakeLists.txt +++ b/chapter12/iOS_example/CMakeLists.txt @@ -7,21 +7,18 @@ cmake_minimum_required(VERSION 3.25...3.30) project( ioslib VERSION 1.0.0 - DESCRIPTION - "A simple C++ project to demonstrate creating a simple ios library" + DESCRIPTION "A simple C++ project to demonstrate creating a simple ios library" LANGUAGES CXX ) # add the library target add_library(ioslib) -# set properties for the target. VERSION set the library version to the project -# version * SOVERSION set the compatibility version for the library to the -# major number of the version +# set properties for the target. VERSION set the library version to the project version * SOVERSION +# set the compatibility version for the library to the major number of the version set_target_properties( ioslib - PROPERTIES VERSION ${PROJECT_VERSION} - SOVERSION ${PROJECT_VERSION_MAJOR} + PROPERTIES VERSION ${PROJECT_VERSION} SOVERSION ${PROJECT_VERSION_MAJOR} ) # add sources to the library target @@ -30,12 +27,8 @@ target_sources( PRIVATE src/hello.cpp src/internal.cpp ) -# define the C++ standard needed to compile this library and make it visible to -# dependers -target_compile_features( - ioslib - PUBLIC cxx_std_17 -) +# define the C++ standard needed to compile this library and make it visible to dependers +target_compile_features(ioslib PUBLIC cxx_std_17) # set the include directories target_include_directories( diff --git a/chapter12/iOS_example/ios.toolchain.cmake b/chapter12/iOS_example/ios.toolchain.cmake index 1ec5d94..848e165 100644 --- a/chapter12/iOS_example/ios.toolchain.cmake +++ b/chapter12/iOS_example/ios.toolchain.cmake @@ -1,15 +1,5 @@ -set(CMAKE_SYSTEM_NAME - iOS -) -set(CMAKE_OSX_ARCHITECTURES - "arm64;x86_64" -) -set(CMAKE_OSX_DEPLOYMENT_TARGET - 13.0 -) -set(CMAKE_XCODE_ATTRIBUTE_ONLY_ACTIVE_ARCH - NO -) -set(CMAKE_IOS_INSTALL_COMBINED - YES -) +set(CMAKE_SYSTEM_NAME iOS) +set(CMAKE_OSX_ARCHITECTURES "arm64;x86_64") +set(CMAKE_OSX_DEPLOYMENT_TARGET 13.0) +set(CMAKE_XCODE_ATTRIBUTE_ONLY_ACTIVE_ARCH NO) +set(CMAKE_IOS_INSTALL_COMBINED YES) diff --git a/chapter13/ex01_git_utility/cmake/git.cmake b/chapter13/ex01_git_utility/cmake/git.cmake index c00e19c..2aec721 100644 --- a/chapter13/ex01_git_utility/cmake/git.cmake +++ b/chapter13/ex01_git_utility/cmake/git.cmake @@ -7,17 +7,12 @@ cmake_minimum_required(VERSION 3.25...3.30) include_guard(DIRECTORY) -find_package( - Git - REQUIRED - QUIET -) +find_package(Git REQUIRED QUIET) macro(git_get_branch_name result_var_name) execute_process( COMMAND "${GIT_EXECUTABLE}" symbolic-ref -q --short HEAD - WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}" - OUTPUT_VARIABLE ${result_var_name} + WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}" OUTPUT_VARIABLE ${result_var_name} OUTPUT_STRIP_TRAILING_WHITESPACE ERROR_QUIET ) endmacro() @@ -25,21 +20,15 @@ endmacro() macro(git_get_head_commit_hash result_var_name) execute_process( COMMAND "${GIT_EXECUTABLE}" rev-parse --verify HEAD - WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}" - OUTPUT_VARIABLE ${result_var_name} + WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}" OUTPUT_VARIABLE ${result_var_name} OUTPUT_STRIP_TRAILING_WHITESPACE ERROR_QUIET ) endmacro() -macro( - git_get_config_value - config_key - result_var_name -) +macro(git_get_config_value config_key result_var_name) execute_process( COMMAND "${GIT_EXECUTABLE}" config --get ${config_key} - WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}" - OUTPUT_VARIABLE ${result_var_name} + WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}" OUTPUT_VARIABLE ${result_var_name} OUTPUT_STRIP_TRAILING_WHITESPACE ) endmacro() diff --git a/chapter13/ex02_envfile_utility/CMakeLists.txt b/chapter13/ex02_envfile_utility/CMakeLists.txt index e6ea8b2..2ade330 100644 --- a/chapter13/ex02_envfile_utility/CMakeLists.txt +++ b/chapter13/ex02_envfile_utility/CMakeLists.txt @@ -12,10 +12,8 @@ project( LANGUAGES CXX ) -# Add cmake folder to the module path, so subsequent include() calls can -# directly include modules under cmake/ folder by specifying the name only. -set(CMAKE_MODULE_PATH - ${CMAKE_MODULE_PATH} ${PROJECT_SOURCE_DIR}/cmake/ -) +# Add cmake folder to the module path, so subsequent include() calls can directly include modules +# under cmake/ folder by specifying the name only. +set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} ${PROJECT_SOURCE_DIR}/cmake/) add_subdirectory(test-executable) diff --git a/chapter13/ex02_envfile_utility/cmake/envfile-utils.cmake b/chapter13/ex02_envfile_utility/cmake/envfile-utils.cmake index 5d95455..eea447c 100644 --- a/chapter13/ex02_envfile_utility/cmake/envfile-utils.cmake +++ b/chapter13/ex02_envfile_utility/cmake/envfile-utils.cmake @@ -7,81 +7,39 @@ cmake_minimum_required(VERSION 3.25...3.30) include_guard(DIRECTORY) -# Read an environment file and declare each environment variable defined in it -# with set(...) cmake function. Used with permission from hadouken project: +# Read an environment file and declare each environment variable defined in it with set(...) cmake +# function. Used with permission from hadouken project: # https://github.com/mustafakemalgilor/hadouken/blob/master/hadouken.cmake function(read_environment_file ENVIRONMENT_FILE_NAME) - file( - STRINGS - ${ENVIRONMENT_FILE_NAME} - KVP_LIST - ENCODING UTF-8 - ) + file(STRINGS ${ENVIRONMENT_FILE_NAME} KVP_LIST ENCODING UTF-8) - foreach( - ENV_VAR_DECL IN - LISTS KVP_LIST - ) + foreach(ENV_VAR_DECL IN LISTS KVP_LIST) # Trim begin and end string(STRIP ENV_VAR_DECL ${ENV_VAR_DECL}) # Skip empty lines string(LENGTH ENV_VAR_DECL ENV_VAR_DECL_LEN) - if(ENV_VAR_DECL_LEN - EQUAL - 0 - ) + if(ENV_VAR_DECL_LEN EQUAL 0) continue() endif() # Skip comments - string( - SUBSTRING ${ENV_VAR_DECL} - 0 - 1 - ENV_VAR_DECL_FC - ) + string(SUBSTRING ${ENV_VAR_DECL} 0 1 ENV_VAR_DECL_FC) - if(ENV_VAR_DECL_FC - STREQUAL - "#" - ) + if(ENV_VAR_DECL_FC STREQUAL "#") continue() endif() # Convert environment variable declaration to cmake list - string( - REPLACE "=" - ";" - ENV_VAR_SPLIT - ${ENV_VAR_DECL} - ) + string(REPLACE "=" ";" ENV_VAR_SPLIT ${ENV_VAR_DECL}) - list( - GET - ENV_VAR_SPLIT - 0 - ENV_VAR_NAME - ) - list( - GET - ENV_VAR_SPLIT - 1 - ENV_VAR_VALUE - ) + list(GET ENV_VAR_SPLIT 0 ENV_VAR_NAME) + list(GET ENV_VAR_SPLIT 1 ENV_VAR_VALUE) # Replace quotes in environment variable values - string( - REPLACE "\"" - "" - ENV_VAR_VALUE - ${ENV_VAR_VALUE} - ) + string(REPLACE "\"" "" ENV_VAR_VALUE ${ENV_VAR_VALUE}) - set(${ENV_VAR_NAME} - ${ENV_VAR_VALUE} - PARENT_SCOPE - ) + set(${ENV_VAR_NAME} ${ENV_VAR_VALUE} PARENT_SCOPE) endforeach() endfunction() diff --git a/chapter13/ex02_envfile_utility/test-executable/CMakeLists.txt b/chapter13/ex02_envfile_utility/test-executable/CMakeLists.txt index 0bdad3a..d340e81 100644 --- a/chapter13/ex02_envfile_utility/test-executable/CMakeLists.txt +++ b/chapter13/ex02_envfile_utility/test-executable/CMakeLists.txt @@ -22,17 +22,13 @@ target_sources( ch13_ex02_envfile_utility_test PRIVATE test.cpp ) -# Request compile features for target named `ch13_ex02_envfile_utility_test`. -# Requested `cxx_std_17` feature determines the minimum C++ standard required to -# build this target. It's PRIVATE, since it is a build-time requirement only. -target_compile_features( - ch13_ex02_envfile_utility_test - PRIVATE cxx_std_17 -) +# Request compile features for target named `ch13_ex02_envfile_utility_test`. Requested +# `cxx_std_17` feature determines the minimum C++ standard required to build this target. It's +# PRIVATE, since it is a build-time requirement only. +target_compile_features(ch13_ex02_envfile_utility_test PRIVATE cxx_std_17) # Expose PROJECT_VERSION and PROJECT_AUTHOR variables as macro definitions target_compile_definitions( - ch13_ex02_envfile_utility_test - PRIVATE TEST_PROJECT_VERSION="${TEST_PROJECT_VERSION}" - TEST_PROJECT_AUTHOR="${TEST_PROJECT_AUTHOR}" + ch13_ex02_envfile_utility_test PRIVATE TEST_PROJECT_VERSION="${TEST_PROJECT_VERSION}" + TEST_PROJECT_AUTHOR="${TEST_PROJECT_AUTHOR}" ) diff --git a/chapter13/ex03_external_cmake_module/CMakeLists.txt b/chapter13/ex03_external_cmake_module/CMakeLists.txt index 3b7cc6d..dd21103 100644 --- a/chapter13/ex03_external_cmake_module/CMakeLists.txt +++ b/chapter13/ex03_external_cmake_module/CMakeLists.txt @@ -8,8 +8,7 @@ cmake_minimum_required(VERSION 3.25...3.30) project( ch13_ex03_external_cmake_module VERSION 1.0 - DESCRIPTION - "Chapter 13 Example 03, external CMake modules (hadouken) example" + DESCRIPTION "Chapter 13 Example 03, external CMake modules (hadouken) example" LANGUAGES CXX ) @@ -28,11 +27,7 @@ fetchcontent_declare( fetchcontent_makeavailable(hadouken) # Add hadouken module directory to CMake module path -list( - APPEND - CMAKE_MODULE_PATH - ${hadouken_SOURCE_DIR}/cmake/modules -) +list(APPEND CMAKE_MODULE_PATH ${hadouken_SOURCE_DIR}/cmake/modules) # Include some CMake modules provided by the Hadouken include(misc/Log) diff --git a/chapter14/precompiled_headers/CMakeLists.txt b/chapter14/precompiled_headers/CMakeLists.txt index 34833cf..5b0f2dc 100644 --- a/chapter14/precompiled_headers/CMakeLists.txt +++ b/chapter14/precompiled_headers/CMakeLists.txt @@ -7,8 +7,7 @@ cmake_minimum_required(VERSION 3.25...3.30) project( ch14_precompiled_headers VERSION 1.0 - DESCRIPTION - "A simple C++ project to demonstrate creating precompiled headers" + DESCRIPTION "A simple C++ project to demonstrate creating precompiled headers" LANGUAGES CXX ) @@ -18,19 +17,10 @@ add_executable(ch14_precompiled_headers) # Add sources to the executable target target_sources( ch14_precompiled_headers - PRIVATE src/main.cpp - src/fibonacci.cpp - src/eratosthenes.cpp - src/pythagoras.cpp + PRIVATE src/main.cpp src/fibonacci.cpp src/eratosthenes.cpp src/pythagoras.cpp ) -# Add headers to the generated precompiled header for target -# ch14_precompiled_headers +# Add headers to the generated precompiled header for target ch14_precompiled_headers target_precompile_headers( - ch14_precompiled_headers - PRIVATE - src/fibonacci.h - - - src/eratosthenes.h + ch14_precompiled_headers PRIVATE src/fibonacci.h src/eratosthenes.h ) diff --git a/chapter14/unity_build/CMakeLists.txt b/chapter14/unity_build/CMakeLists.txt index 560ac3c..2d6ca0c 100644 --- a/chapter14/unity_build/CMakeLists.txt +++ b/chapter14/unity_build/CMakeLists.txt @@ -13,16 +13,11 @@ project( # Create an executable target add_executable(ch14_unity_build) -target_compile_features( - ch14_unity_build - PRIVATE cxx_std_17 -) +target_compile_features(ch14_unity_build PRIVATE cxx_std_17) target_sources( ch14_unity_build - PRIVATE src/main.cpp - src/fibonacci.cpp - src/eratosthenes.cpp + PRIVATE src/main.cpp src/fibonacci.cpp src/eratosthenes.cpp ) # Enable unity build for the target, by default BATCH mode is used set_target_properties( @@ -31,7 +26,4 @@ set_target_properties( ) # exclude eratosthenes.cpp from the unity build -set_source_files_properties( - src/eratosthenes.cpp - PROPERTIES SKIP_UNITY_BUILD_INCLUSION YES -) +set_source_files_properties(src/eratosthenes.cpp PROPERTIES SKIP_UNITY_BUILD_INCLUSION YES) diff --git a/chapter14/unity_build_group/CMakeLists.txt b/chapter14/unity_build_group/CMakeLists.txt index 8cb5a1a..11b6c5e 100644 --- a/chapter14/unity_build_group/CMakeLists.txt +++ b/chapter14/unity_build_group/CMakeLists.txt @@ -15,32 +15,19 @@ add_executable(ch14_unity_build_group) target_sources( ch14_unity_build_group - PRIVATE src/main.cpp - src/fibonacci.cpp - src/eratosthenes.cpp - src/pythagoras.cpp -) -target_compile_features( - ch14_unity_build_group - PRIVATE cxx_std_17 + PRIVATE src/main.cpp src/fibonacci.cpp src/eratosthenes.cpp src/pythagoras.cpp ) +target_compile_features(ch14_unity_build_group PRIVATE cxx_std_17) -# Enable unity build for the target by default BATCH mode is used for unity -# builds, so the build mode is changed to GROUP +# Enable unity build for the target by default BATCH mode is used for unity builds, so the build +# mode is changed to GROUP set_target_properties( ch14_unity_build_group - PROPERTIES UNITY_BUILD True - UNITY_BUILD_MODE GROUP + PROPERTIES UNITY_BUILD True UNITY_BUILD_MODE GROUP ) # Add main.cpp and fibonacci.cpp to the unuty group1 -set_source_files_properties( - src/main.cpp src/fibonacci.cpp - PROPERTIES UNITY_GROUP group1 -) +set_source_files_properties(src/main.cpp src/fibonacci.cpp PROPERTIES UNITY_GROUP group1) # add erathostenes.cpp and pythagoras.cpp to group2 -set_source_files_properties( - src/erathostenes.cpp src/pythagoras.cpp - PROPERTIES UNITY_GROUP group2 -) +set_source_files_properties(src/erathostenes.cpp src/pythagoras.cpp PROPERTIES UNITY_GROUP group2) diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..c8d299f --- /dev/null +++ b/requirements.txt @@ -0,0 +1,21 @@ +# ============================================================================= +# PYTHON PACKAGES (PIP) +# ============================================================================= +# USE (python3): pip3 install -U -r +# ============================================================================= + +### python tools +# isort +# black +# pylint + +### cmake build context +bumpversion>=0.6.0 +check-jsonschema +cmake-format>=0.6.13 +cmake>=3.25 +codespell>=2.3.0 +conan>=2.7.0 +gcovr>=7.2 +ninja>=1.11 +yamllint>=1.35