Skip to content

Commit

Permalink
Change clang-format config file
Browse files Browse the repository at this point in the history
Use "line_width": 85
  • Loading branch information
ClausKlein committed Sep 9, 2024
1 parent 84cc7f4 commit 9364726
Show file tree
Hide file tree
Showing 95 changed files with 694 additions and 1,518 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": 85,
"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)
26 changes: 8 additions & 18 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -55,22 +55,16 @@ 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 +74,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 Down
File renamed without changes.
85 changes: 16 additions & 69 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,44 @@ 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
)
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
29 changes: 8 additions & 21 deletions chapter01/policy_example/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,26 +12,13 @@ project(
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)
18 changes: 6 additions & 12 deletions chapter02/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,27 +14,21 @@ 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)
Expand Down
12 changes: 4 additions & 8 deletions chapter02/component1/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,26 +12,22 @@ 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/
) # Add include/ to include directories with visibility of PUBLIC.
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(
Expand Down
Loading

0 comments on commit 9364726

Please sign in to comment.