Skip to content

Commit

Permalink
Change clang-format config file
Browse files Browse the repository at this point in the history
Use "line_width": 99
  • Loading branch information
ClausKlein committed Sep 9, 2024
1 parent 84cc7f4 commit 771e774
Show file tree
Hide file tree
Showing 97 changed files with 745 additions and 1,707 deletions.
39 changes: 39 additions & 0 deletions .cmake-format.json
Original file line number Diff line number Diff line change
@@ -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
}
}
34 changes: 9 additions & 25 deletions .github/toolchain/clang-on-windows-toolchain.cmake
Original file line number Diff line number Diff line change
@@ -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(
Expand All @@ -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}")
12 changes: 3 additions & 9 deletions CMakeGraphVizOptions.cmake
Original file line number Diff line number Diff line change
@@ -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)
38 changes: 12 additions & 26 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
)

Expand Down Expand Up @@ -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}`")
Expand All @@ -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
Expand All @@ -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)
Expand Down
File renamed without changes.
90 changes: 17 additions & 73 deletions chapter01/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -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()

Expand Down
31 changes: 8 additions & 23 deletions chapter01/policy_example/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
20 changes: 6 additions & 14 deletions chapter02/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,36 +14,28 @@ 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)
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()
Loading

0 comments on commit 771e774

Please sign in to comment.