Skip to content

Commit

Permalink
Move to CPM, simplify (cpp-best-practices#15)
Browse files Browse the repository at this point in the history
* Remove external dependencies for project_options
* drastically simplify the way options are managed, using simple CMake `option` commands
* move from Conan to CPM (better control over build flags, less likely to accidentally have ABI/ODR issues)
* enhance defaults to focus on safety:
   * C++ hardening flags enabled by default
   * UBSan `minimal-runtime` enabled for shipping binaries when possible, for hardening
   * Static libraries + LTO enabled for better performance and detection of ODR violations
   * _FORTIFY_SOURCE level 3 enabled
   * checked STL for GCC enabled
* basic example of a library added
* automatic enabling of fuzz testing binary when possible
* many various bug fixes to the build system
* projects made with this template should now be fetch-content-able
  • Loading branch information
lefticus authored Apr 19, 2023
1 parent c655234 commit 5bf4f34
Show file tree
Hide file tree
Showing 45 changed files with 1,727 additions and 434 deletions.
5 changes: 5 additions & 0 deletions .clang-tidy
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ WarningsAsErrors: ''
HeaderFilterRegex: ''
FormatStyle: none

CheckOptions:
- key: readability-identifier-length.IgnoredVariableNames
value: 'x|y|z'
- key: readability-identifier-length.IgnoredParameterNames
value: 'x|y|z'



Expand Down
7 changes: 2 additions & 5 deletions .github/actions/setup_cache/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,12 @@ runs:
with:
# You might want to add .ccache to your cache configuration?
path: |
~/vcpkg
./build/vcpkg_installed
${{ env.CONAN_USER_HOME }}
~/.cache/pip
${{ env.HOME }}/.cache/vcpkg/archives
${{ env.XDG_CACHE_HOME }}/vcpkg/archives
${{ env.LOCALAPPDATA }}\vcpkg\archives
${{ env.APPDATA }}\vcpkg\archives
key: ${{ runner.os }}-${{ inputs.compiler }}-${{ inputs.build_type }}-${{ hashFiles('./conanfile.txt')}}-${{ inputs.generator }}-${{ inputs.developer_mode }}-${{ hashFiles('**/CMakeLists.txt') }}-${{ hashFiles('./vcpkg.json')}}
key: ${{ runner.os }}-${{ inputs.compiler }}-${{ inputs.build_type }}-${{ inputs.generator }}-${{ inputs.developer_mode }}-${{ hashFiles('**/CMakeLists.txt') }}-${{ hashFiles('./vcpkg.json')}}
restore-keys: |
${{ runner.os }}-${{ inputs.compiler }}-${{ inputs.build_type }}-${{ hashFiles('./conanfile.txt') }}
${{ runner.os }}-${{ inputs.compiler }}-${{ inputs.build_type }}
57 changes: 33 additions & 24 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,8 @@ on:
- develop

env:
# Conan cache environment variables
CONAN_SYSREQUIRES_MODE: enabled
CONAN_USER_HOME: "${{ github.workspace }}/conan-cache"
CONAN_USER_HOME_SHORT: "${{ github.workspace }}/conan-cache/short"
CLANG_TIDY_VERSION: "13.0.0"
CLANG_TIDY_VERSION: "15.0.2"
VERBOSE: 1

jobs:
Test:
Expand All @@ -35,17 +32,19 @@ jobs:
- macos-10.15
- windows-2019
compiler:
# you can specify the version after `-` like "llvm-13.0.0".
- llvm-13.0.0
# you can specify the version after `-` like "llvm-15.0.2".
- llvm-15.0.2
- gcc-11
generator:
- "Ninja Multi-Config"
build_type:
- Release
- Debug
developer_mode:
package_maintainer_mode:
- ON
- OFF
build_shared:
- OFF

exclude:
# mingw is determined by this author to be too buggy to support
Expand All @@ -57,12 +56,18 @@ jobs:
# if you try to use a compiler that does not have gcov set
- compiler: gcc-11
gcov_executable: gcov
- compiler: llvm-13.0.0
enable_ipo: On

- compiler: llvm-15.0.2
enable_ipo: Off
gcov_executable: "llvm-cov gcov"

- os: macos-10.15
enable_ipo: Off

# Set up preferred package generators, for given build configurations
- build_type: Release
developer_mode: OFF
package_maintainer_mode: OFF
package_generator: TBZ2

# This exists solely to make sure a non-multiconfig build works
Expand All @@ -71,34 +76,45 @@ jobs:
generator: "Unix Makefiles"
build_type: Debug
gcov_executable: gcov
developer_mode: On
package_maintainer_mode: On
enable_ipo: Off

# Windows msvc builds
- os: windows-2022
compiler: msvc
generator: "Visual Studio 17 2022"
build_type: Debug
developer_mode: On
package_maintainer_mode: On
enable_ipo: On

- os: windows-2022
compiler: msvc
generator: "Visual Studio 17 2022"
build_type: Release
developer_mode: On
package_maintainer_mode: On
enable_ipo: On

- os: windows-2022
compiler: msvc
generator: "Visual Studio 17 2022"
build_type: Debug
developer_mode: Off
package_maintainer_mode: Off

- os: windows-2022
compiler: msvc
generator: "Visual Studio 17 2022"
build_type: Release
developer_mode: Off
package_maintainer_mode: Off
package_generator: ZIP

- os: windows-2022
compiler: msvc
generator: "Visual Studio 17 2022"
build_type: Release
package_maintainer_mode: On
enable_ipo: On
build_shared: On


steps:
- name: Check for llvm version mismatches
Expand All @@ -115,7 +131,7 @@ jobs:
with:
compiler: ${{ matrix.compiler }}
build_type: ${{ matrix.build_type }}
developer_mode: ${{ matrix.developer_mode }}
package_maintainer_mode: ${{ matrix.package_maintainer_mode }}
generator: ${{ matrix.generator }}

- name: Setup Cpp
Expand All @@ -126,7 +142,6 @@ jobs:

cmake: true
ninja: true
conan: true
vcpkg: false
ccache: true
clangtidy: ${{ env.CLANG_TIDY_VERSION }}
Expand All @@ -137,15 +152,9 @@ jobs:
gcovr: true
opencppcoverage: true

- name: Cleanup Conan system packages (they are not properly cached)
run: |
conan remove -f '*/system'
# make sure coverage is only enabled for Debug builds, since it sets -O0 to make sure coverage
# has meaningful results
- name: Configure CMake
run: |
cmake -S . -B ./build -G "${{matrix.generator}}" -DCMAKE_BUILD_TYPE:STRING=${{matrix.build_type}} -DENABLE_DEVELOPER_MODE:BOOL=${{matrix.developer_mode}} -DOPT_ENABLE_COVERAGE:BOOL=${{ matrix.build_type == 'Debug' }} -DGIT_SHA:STRING=${{ github.sha }}
cmake -S . -B ./build -G "${{matrix.generator}}" -Dmyproject_ENABLE_IPO=${{matrix.enable_ipo }} -DCMAKE_BUILD_TYPE:STRING=${{matrix.build_type}} -Dmyproject_PACKAGING_MAINTAINER_MODE:BOOL=${{matrix.package_maintainer_mode}} -Dmyproject_ENABLE_COVERAGE:BOOL=${{ matrix.build_type == 'Debug' }} -DGIT_SHA:STRING=${{ github.sha }}
- name: Build
# Execute the build. You can specify a specific target with "--target <NAME>"
Expand Down
19 changes: 4 additions & 15 deletions .github/workflows/codeql-analysis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,6 @@ on:
schedule:
- cron: '38 0 * * 5'

env:
# Conan cache environment variables
CONAN_SYSREQUIRES_MODE: enabled
CONAN_USER_HOME: "${{ github.workspace }}/conan-cache"
CONAN_USER_HOME_SHORT: "${{ github.workspace }}/conan-cache/short"


jobs:
analyze:
Expand All @@ -49,8 +43,8 @@ jobs:
- "Ninja Multi-Config"
build_type:
- Debug
developer_mode:
- OFF
packaging_maintainer_mode:
- ON


steps:
Expand All @@ -61,7 +55,7 @@ jobs:
with:
compiler: ${{ matrix.compiler }}
build_type: ${{ matrix.build_type }}
developer_mode: ${{ matrix.developer_mode }}
packaging_maintainer_mode: ${{ matrix.packaging_maintainer_mode }}
generator: ${{ matrix.generator }}


Expand All @@ -73,7 +67,6 @@ jobs:

cmake: true
ninja: true
conan: true
vcpkg: false
ccache: true
clangtidy: false
Expand All @@ -83,15 +76,11 @@ jobs:
gcovr: false
opencppcoverage: false

- name: Cleanup Conan system packages (they are not properly cached)
run: |
conan remove -f '*/system'
# make sure coverage is only enabled for Debug builds, since it sets -O0 to make sure coverage
# has meaningful results
- name: Configure CMake
run: |
cmake -S . -B ./build -G "${{matrix.generator}}" -DCMAKE_BUILD_TYPE:STRING=${{matrix.build_type}} -DENABLE_DEVELOPER_MODE:BOOL=${{matrix.developer_mode}} -DOPT_ENABLE_COVERAGE:BOOL=${{ matrix.build_type == 'Debug' }}
cmake -S . -B ./build -G "${{matrix.generator}}" -DCMAKE_BUILD_TYPE:STRING=${{matrix.build_type}} -Dmyproject_PACKAGING_MAINTAINER_MODE:BOOL=${{matrix.packaging_maintainer_mode}} -DOPT_ENABLE_COVERAGE:BOOL=${{ matrix.build_type == 'Debug' }}
# Initializes the CodeQL tools for scanning.
- name: Initialize CodeQL
Expand Down
16 changes: 3 additions & 13 deletions .github/workflows/template-janitor.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,8 @@ on:

env:
TEMPLATES_PATH: ".github/template"
CONAN_SYSREQUIRES_MODE: enabled
CONAN_USER_HOME: "${{ github.workspace }}/conan-cache"
CONAN_USER_HOME_SHORT: "${{ github.workspace }}/conan-cache/short"


jobs:

template-cleanup:
Expand Down Expand Up @@ -77,7 +74,7 @@ jobs:
- name: Insert new org and project
run: |
# rename the CMake project to match the github project
sed -i "s/myproject/${{ env.NEW_SAFE_PROJECT }}/gi" CMakeLists.txt configured_files/config.hpp.in src/main.cpp test/CMakeLists.txt fuzz_test/CMakeLists.txt
find src include test fuzz_test -type f -exec sed -i "s/myproject/${{ env.NEW_SAFE_PROJECT }}/gi" CMakeLists.txt ProjectOptions.cmake .github/workflows/ci.yml .github/workflows/codeql-analysis.yml configured_files/config.hpp.in {} +
# Update URL placeholders for project
sed -i "s|%%myurl%%|${{ fromJson(steps.get_repo_meta.outputs.data).html_url }}|gi" CMakeLists.txt
Expand All @@ -86,6 +83,7 @@ jobs:
sed -i "s/%%myorg%%/${{ env.NEW_ORG }}/g" ${{ env.TEMPLATES_PATH }}/README.md
sed -i "s/%%myproject%%/${{ env.NEW_PROJECT }}/g" ${{ env.TEMPLATES_PATH }}/README.md
sed -i "s|%%description%%|${{ fromJson(steps.get_repo_meta.outputs.data).description }}|g" ${{ env.TEMPLATES_PATH }}/README.md
mv include/myproject include/${{ env.NEW_SAFE_PROJECT }}
cp ${{ env.TEMPLATES_PATH }}/README.md README.md
- name: Print diff after replacement
Expand Down Expand Up @@ -115,7 +113,6 @@ jobs:

cmake: true
ninja: false
conan: true
vcpkg: false
ccache: false
clangtidy: false
Expand All @@ -125,9 +122,6 @@ jobs:
gcovr: false
opencppcoverage: false

- name: Cleanup Conan system packages (they are not properly cached)
run: |
conan remove -f '*/system'

- name: Test simple configuration to make sure nothing broke
run: |
Expand Down Expand Up @@ -217,7 +211,6 @@ jobs:

cmake: true
ninja: false
conan: true
vcpkg: false
ccache: false
clangtidy: false
Expand All @@ -227,9 +220,6 @@ jobs:
gcovr: false
opencppcoverage: false

- name: Cleanup Conan system packages (they are not properly cached)
run: |
conan remove -f '*/system'

- name: Test simple configuration to make sure nothing broke (default compiler,cmake,developer_mode OFF)
run: |
Expand Down
Loading

0 comments on commit 5bf4f34

Please sign in to comment.