diff --git a/.cmake-format.py b/.cmake-format.py deleted file mode 100644 index 4891c4d..0000000 --- a/.cmake-format.py +++ /dev/null @@ -1,153 +0,0 @@ -with section("format"): - - # Disable formatting entirely, making cmake-format a no-op - disable = False - - # How wide to allow formatted cmake files - line_width = 80 - - # How many spaces to tab for indent - tab_size = 2 - - # If true, lines are indented using tab characters (utf-8 0x09) instead of - # space characters (utf-8 0x20). In cases where the layout would - # require a fractional tab character, the behavior of the fractional - # indentation is governed by - use_tabchars = False - - # If is True, then the value of this variable indicates how - # fractional indentions are handled during whitespace replacement. If set to - # 'use-space', fractional indentation is left as spaces (utf-8 0x20). If set - # to `round-up` fractional indentation is replaced with a single tab character - # (utf-8 0x09) effectively shifting the column to the next tabstop - fractional_tab_policy = 'use-space' - - # If an argument group contains more than this many sub-groups (parg or kwarg - # groups) then force it to a vertical layout. - max_subgroups_hwrap = 2 - - # If a positional argument group contains more than this many arguments, then - # force it to a vertical layout. - max_pargs_hwrap = 6 - - # If a cmdline positional group consumes more than this many lines without - # nesting, then invalidate the layout (and nest) - max_rows_cmdline = 2 - - # If true, separate flow control names from their parentheses with a space - separate_ctrl_name_with_space = False - - # If true, separate function names from parentheses with a space - separate_fn_name_with_space = False - - # If a statement is wrapped to more than one line, than dangle the closing - # parenthesis on its own line. - dangle_parens = False - - # If the trailing parenthesis must be 'dangled' on its on line, then align it - # to this reference: `prefix`: the start of the statement, `prefix-indent`: - # the start of the statement, plus one indentation level, `child`: align to - # the column of the arguments - dangle_align = 'prefix' - - # If the statement spelling length (including space and parenthesis) is - # smaller than this amount, then force reject nested layouts. - min_prefix_chars = 4 - - # If the statement spelling length (including space and parenthesis) is larger - # than the tab width by more than this amount, then force reject un-nested - # layouts. - max_prefix_chars = 10 - - # If a candidate layout is wrapped horizontally but it exceeds this many - # lines, then reject the layout. - max_lines_hwrap = 2 - - # What style line endings to use in the output. - line_ending = 'unix' - - # Format command names consistently as 'lower' or 'upper' case - command_case = 'canonical' - - # Format keywords consistently as 'lower' or 'upper' case - keyword_case = 'upper' - - # A list of command names which should always be wrapped - always_wrap = [] - - # If true, the argument lists which are known to be sortable will be sorted - # lexicographicall - enable_sort = True - - # If true, the parsers may infer whether or not an argument list is sortable - # (without annotation). - autosort = False - - # By default, if cmake-format cannot successfully fit everything into the - # desired linewidth it will apply the last, most agressive attempt that it - # made. If this flag is True, however, cmake-format will print error, exit - # with non-zero status code, and write-out nothing - require_valid_layout = False - - # A dictionary mapping layout nodes to a list of wrap decisions. See the - # documentation for more information. - layout_passes = {} - -# ------------------------------------------------ -# Options affecting comment reflow and formatting. -# ------------------------------------------------ -with section("markup"): - - # What character to use for bulleted lists - bullet_char = '-' - - # What character to use as punctuation after numerals in an enumerated list - enum_char = '.' - - # If comment markup is enabled, don't reflow the first comment block in each - # listfile. Use this to preserve formatting of your copyright/license - # statements. - first_comment_is_literal = False - - # If comment markup is enabled, don't reflow any comment block which matches - # this (regex) pattern. Default is `None` (disabled). - literal_comment_pattern = None - - # Regular expression to match preformat fences in comments default= - # ``r'^\s*([`~]{3}[`~]*)(.*)$'`` - fence_pattern = '^\\s*([`~]{3}[`~]*)(.*)$' - - # Regular expression to match rulers in comments default= - # ``r'^\s*[^\w\s]{3}.*[^\w\s]{3}$'`` - ruler_pattern = '^\\s*[^\\w\\s]{3}.*[^\\w\\s]{3}$' - - # If a comment line matches starts with this pattern then it is explicitly a - # trailing comment for the preceeding argument. Default is '#<' - explicit_trailing_pattern = '#<' - - # If a comment line starts with at least this many consecutive hash - # characters, then don't lstrip() them off. This allows for lazy hash rulers - # where the first hash char is not separated by space - hashruler_min_length = 10 - - # If true, then insert a space between the first hash char and remaining hash - # chars in a hash ruler, and normalize its length to fill the column - canonicalize_hashrulers = True - - # enable comment markup parsing and reflow - enable_markup = True - -# ------------------------------- -# Options affecting file encoding -# ------------------------------- -with section("encode"): - - # If true, emit the unicode byte-order mark (BOM) at the start of the file - emit_byteorder_mark = False - - # Specify the encoding of the input file. Defaults to utf-8 - input_encoding = 'utf-8' - - # Specify the encoding of the output file. Defaults to utf-8. Note that cmake - # only claims to support utf-8 so be careful when using anything else - output_encoding = 'utf-8' diff --git a/.gersemirc b/.gersemirc new file mode 100644 index 0000000..e8f3a38 --- /dev/null +++ b/.gersemirc @@ -0,0 +1,4 @@ +indent: 2 +line_length: 80 +list_expansion: favour-inlining +warn_about_unknown_commands: false diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 217f749..276fc66 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -28,10 +28,10 @@ repos: args: [--autofix, --no-sort-keys] - id: requirements-txt-fixer - id: trailing-whitespace - - repo: https://github.com/cheshirekow/cmake-format-precommit - rev: v0.6.13 + - repo: https://github.com/BlankSpruce/gersemi + rev: 0.23.2 hooks: - - id: cmake-format + - id: gersemi - repo: https://github.com/doublify/pre-commit-clang-format rev: 62302476d0da01515660132d76902359bed0f782 hooks: diff --git a/CMakeLists.txt b/CMakeLists.txt index 9817511..52518f1 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -5,7 +5,8 @@ project( VERSION 0.0.1 DESCRIPTION "Tool configuration template" HOMEPAGE_URL https://github.com/b1ackviking/cpp-project-template - LANGUAGES CXX) + LANGUAGES CXX +) include(cmake/defaults.cmake) include(FindThreads) diff --git a/cmake/defaults.cmake b/cmake/defaults.cmake index f4b292d..618d7ac 100644 --- a/cmake/defaults.cmake +++ b/cmake/defaults.cmake @@ -1,8 +1,10 @@ option(ENABLE_CPPCHECK "Enable static analysis with cppcheck" OFF) option(ENABLE_CLANG_TIDY "Enable static analysis with clang-tidy" OFF) -option(ENABLE_IPO - "Enable Interprocedural Optimization, aka Link Time Optimization (LTO)" - OFF) +option( + ENABLE_IPO + "Enable Interprocedural Optimization, aka Link Time Optimization (LTO)" + OFF +) option(ENABLE_CACHE "Enable cache if available" OFF) option(ENABLE_ASAN "Enable address sanitizer" OFF) if(CMAKE_CXX_COMPILER_ID MATCHES ".*Clang|GNU") @@ -10,8 +12,11 @@ if(CMAKE_CXX_COMPILER_ID MATCHES ".*Clang|GNU") option(ENABLE_LSAN "Enable leak sanitizer" OFF) option(ENABLE_UBSAN "Enable undefined behavior sanitizer" OFF) option(ENABLE_TSAN "Enable thread sanitizer" OFF) - option(ENABLE_FORTIFY_SOURCE - "Enable -D_FORTIFY_SOURCE=3 (requires optimized build)" OFF) + option( + ENABLE_FORTIFY_SOURCE + "Enable -D_FORTIFY_SOURCE=3 (requires optimized build)" + OFF + ) endif() option(ENABLE_HARDENINGS "Enable hardenings" OFF) @@ -32,13 +37,21 @@ list(APPEND CMAKE_CTEST_ARGUMENTS --parallel) # Set a default build type if none was specified if(NOT CMAKE_BUILD_TYPE AND NOT CMAKE_CONFIGURATION_TYPES) message( - STATUS "Setting build type to 'RelWithDebInfo' as none was specified.") - set(CMAKE_BUILD_TYPE - RelWithDebInfo - CACHE STRING "Choose the type of build." FORCE) + STATUS + "Setting build type to 'RelWithDebInfo' as none was specified." + ) + set( + CMAKE_BUILD_TYPE + RelWithDebInfo + CACHE STRING + "Choose the type of build." + FORCE + ) # Set the possible values of build type for cmake-gui, ccmake - set_property(CACHE CMAKE_BUILD_TYPE PROPERTY STRINGS "Debug" "Release" - "MinSizeRel" "RelWithDebInfo") + set_property( + CACHE CMAKE_BUILD_TYPE + PROPERTY STRINGS "Debug" "Release" "MinSizeRel" "RelWithDebInfo" + ) endif() # This function will prevent in-source builds @@ -61,14 +74,16 @@ assure_out_of_source_builds() if(ENABLE_CPPCHECK) find_program(CPPCHECK cppcheck) if(CPPCHECK) - set(CMAKE_CXX_CPPCHECK - ${CPPCHECK} - -v - --enable=all - --inline-suppr - --error-exitcode=42 - --suppress=missingIncludeSystem - --suppress=unmatchedSuppression) + set( + CMAKE_CXX_CPPCHECK + ${CPPCHECK} + -v + --enable=all + --inline-suppr + --error-exitcode=42 + --suppress=missingIncludeSystem + --suppress=unmatchedSuppression + ) set(CMAKE_C_CPPCHECK ${CMAKE_CXX_CPPCHECK}) else() message(SEND_ERROR "Cppcheck requested but executable not found") @@ -78,8 +93,11 @@ endif() if(ENABLE_CLANG_TIDY) find_program(CLANGTIDY clang-tidy) if(CLANGTIDY) - set(CMAKE_CXX_CLANG_TIDY ${CLANGTIDY} - -extra-arg=-Wno-unknown-warning-option) + set( + CMAKE_CXX_CLANG_TIDY + ${CLANGTIDY} + -extra-arg=-Wno-unknown-warning-option + ) set(CMAKE_C_CLANG_TIDY ${CMAKE_CXX_CLANG_TIDY}) else() message(SEND_ERROR "clang-tidy requested but executable not found") @@ -97,16 +115,17 @@ if(ENABLE_IPO) endif() if(ENABLE_CACHE) - set(CACHE_OPTION - "ccache" - CACHE STRING "Compiler cache to be used") + set(CACHE_OPTION "ccache" CACHE STRING "Compiler cache to be used") set(CACHE_OPTION_VALUES "ccache" "sccache") set_property(CACHE CACHE_OPTION PROPERTY STRINGS ${CACHE_OPTION_VALUES}) list(FIND CACHE_OPTION_VALUES ${CACHE_OPTION} CACHE_OPTION_INDEX) if(${CACHE_OPTION_INDEX} EQUAL -1) - message(STATUS "Using custom compiler cache system: '${CACHE_OPTION}', " - "explicitly supported entries are ${CACHE_OPTION_VALUES}") + message( + STATUS + "Using custom compiler cache system: '${CACHE_OPTION}', " + "explicitly supported entries are ${CACHE_OPTION_VALUES}" + ) endif() find_program(CACHE_BINARY ${CACHE_OPTION}) @@ -115,13 +134,17 @@ if(ENABLE_CACHE) set(CMAKE_CXX_COMPILER_LAUNCHER ${CACHE_BINARY}) else() message( - WARNING "${CACHE_OPTION} is enabled but was not found. Not using it") + WARNING + "${CACHE_OPTION} is enabled but was not found. Not using it" + ) endif() endif() # Apply the default set of warnings to the target function(set_target_warnings target_name) - set(msvc_warnings + set( + msvc_warnings + # gersemi: off /W4 # Baseline reasonable warnings /w14242 # 'identifier': conversion from 'type1' to 'type1', possible loss # of data @@ -153,8 +176,11 @@ function(set_target_warnings target_name) /w14928 # illegal copy-initialization; more than one user-defined # conversion has been implicitly applied /permissive- # standards conformance mode for MSVC compiler. + # gersemi: on ) - set(clang_warnings + set( + clang_warnings + # gersemi: off -Wall -Wextra # reasonable and standard -Wpedantic # warn if non-standard C++ is used @@ -174,9 +200,12 @@ function(set_target_warnings target_name) -Wdouble-promotion # warn if float is implicit promoted to double -Wformat=2 # warn on security issues around functions that format output # (ie printf) + # gersemi: on ) - set(gcc_warnings - ${clang_warnings} + set( + gcc_warnings + ${clang_warnings} + # gersemi: off -Wmisleading-indentation # warn if indentation implies blocks where blocks # do not exist -Wduplicated-cond # warn if if / else chain has duplicated conditions @@ -184,6 +213,7 @@ function(set_target_warnings target_name) -Wlogical-op # warn about logical operations being used where bitwise were # probably wanted -Wuseless-cast # warn if you perform a cast to the same type + # gersemi: on ) if(MSVC) set(warnings ${msvc_warnings}) @@ -194,7 +224,8 @@ function(set_target_warnings target_name) else() message( AUTHOR_WARNING - "No compiler warnings set for '${CMAKE_CXX_COMPILER_ID}' compiler.") + "No compiler warnings set for '${CMAKE_CXX_COMPILER_ID}' compiler." + ) endif() target_compile_options(${target_name} INTERFACE ${warnings}) endfunction() @@ -223,8 +254,11 @@ function(enable_sanitizers target_name) if(ENABLE_TSAN) if("address" IN_LIST sanitizers OR "leak" IN_LIST sanitizers) - message(WARNING "Thread sanitizer does not work with Address " - "and Leak sanitizer enabled") + message( + WARNING + "Thread sanitizer does not work with Address " + "and Leak sanitizer enabled" + ) else() list(APPEND sanitizers "thread") endif() @@ -234,17 +268,23 @@ function(enable_sanitizers target_name) if(list_of_sanitizers AND NOT list_of_sanitizers STREQUAL "") if(MSVC) target_compile_options( - ${target_name} INTERFACE /fsanitize=${list_of_sanitizers} /Zi - /INCREMENTAL:NO) + ${target_name} + INTERFACE /fsanitize=${list_of_sanitizers} /Zi /INCREMENTAL:NO + ) target_compile_definitions( - ${target_name} INTERFACE _DISABLE_VECTOR_ANNOTATION - _DISABLE_STRING_ANNOTATION) + ${target_name} + INTERFACE _DISABLE_VECTOR_ANNOTATION _DISABLE_STRING_ANNOTATION + ) target_link_options(${target_name} INTERFACE /INCREMENTAL:NO) else() - target_compile_options(${target_name} - INTERFACE -fsanitize=${list_of_sanitizers}) - target_link_options(${target_name} INTERFACE - -fsanitize=${list_of_sanitizers}) + target_compile_options( + ${target_name} + INTERFACE -fsanitize=${list_of_sanitizers} + ) + target_link_options( + ${target_name} + INTERFACE -fsanitize=${list_of_sanitizers} + ) endif() endif() endfunction() @@ -257,16 +297,19 @@ function(enable_hardenings target_name) target_link_options( ${target_name} INTERFACE - /NXCOMPAT - /CETCOMPAT - /DYNAMICBASE - /LARGEADDRESSAWARE - /HIGHENTROPYVA) + /NXCOMPAT + /CETCOMPAT + /DYNAMICBASE + /LARGEADDRESSAWARE + /HIGHENTROPYVA + ) elseif(CMAKE_CXX_COMPILER_ID MATCHES ".*Clang|GNU") target_compile_definitions(${target_name} INTERFACE _GLIBCXX_ASSERTIONS) if(ENABLE_FORTIFY_SOURCE) - target_compile_options(${target_name} INTERFACE -U_FORTIFY_SOURCE - -D_FORTIFY_SOURCE=3) + target_compile_options( + ${target_name} + INTERFACE -U_FORTIFY_SOURCE -D_FORTIFY_SOURCE=3 + ) endif() if(LINUX) target_link_options(${target_name} INTERFACE -Wl,-z,noexecstack) @@ -280,8 +323,10 @@ function(enable_hardenings target_name) check_cxx_compiler_flag(-fstack-protector-strong STACK_PROTECTOR) if(STACK_PROTECTOR) - target_compile_options(${target_name} - INTERFACE -fstack-protector-strong) + target_compile_options( + ${target_name} + INTERFACE -fstack-protector-strong + ) endif() check_cxx_compiler_flag(-fcf-protection CF_PROTECTION) @@ -291,8 +336,10 @@ function(enable_hardenings target_name) check_cxx_compiler_flag(-fstack-clash-protection CLASH_PROTECTION) if(CLASH_PROTECTION AND (LINUX OR CMAKE_CXX_COMPILER_ID MATCHES "GNU")) - target_compile_options(${target_name} - INTERFACE -fstack-clash-protection) + target_compile_options( + ${target_name} + INTERFACE -fstack-clash-protection + ) endif() endif() endif() diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index f739b27..2acdf22 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -2,11 +2,15 @@ project(example) find_package(fmt CONFIG REQUIRED) add_executable(${PROJECT_NAME}) -target_link_libraries(${PROJECT_NAME} PRIVATE ${CMAKE_THREAD_LIBS_INIT} - warnings options math fmt::fmt) +target_link_libraries( + ${PROJECT_NAME} + PRIVATE ${CMAKE_THREAD_LIBS_INIT} warnings options math fmt::fmt +) target_sources(${PROJECT_NAME} PRIVATE main.cpp) include(GNUInstallDirs) -install(TARGETS ${PROJECT_NAME} RUNTIME_DEPENDENCY_SET - ${PROJECT_NAME}_runtime_dependencies) +install( + TARGETS ${PROJECT_NAME} + RUNTIME_DEPENDENCY_SET ${PROJECT_NAME}_runtime_dependencies +) install(RUNTIME_DEPENDENCY_SET ${PROJECT_NAME}_runtime_dependencies) diff --git a/test/math/CMakeLists.txt b/test/math/CMakeLists.txt index d59ca6a..afa6a88 100644 --- a/test/math/CMakeLists.txt +++ b/test/math/CMakeLists.txt @@ -2,8 +2,12 @@ project(math-test) add_executable(${PROJECT_NAME}) target_sources(${PROJECT_NAME} PRIVATE math-test.cpp) -target_link_libraries(${PROJECT_NAME} PRIVATE warnings options GTest::gtest - GTest::gtest_main math) +target_link_libraries( + ${PROJECT_NAME} + PRIVATE warnings options GTest::gtest GTest::gtest_main math +) gtest_discover_tests( - ${PROJECT_NAME} XML_OUTPUT_DIR ${CMAKE_CURRENT_BINARY_DIR}/junit-reports/ - DISCOVERY_TIMEOUT 15) + ${PROJECT_NAME} + XML_OUTPUT_DIR ${CMAKE_CURRENT_BINARY_DIR}/junit-reports/ + DISCOVERY_TIMEOUT 15 +)