Skip to content

Commit 8178762

Browse files
authored
Make build scripts more generic (#30)
* Attempt to make scripts more generic * Attempt to unify project options and make them consistent
1 parent 2139ecd commit 8178762

File tree

6 files changed

+53
-22
lines changed

6 files changed

+53
-22
lines changed

.github/workflows/ci.yml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ on:
1212
env:
1313
CLANG_TIDY_VERSION: "15.0.2"
1414
VERBOSE: 1
15+
PROJECT_NAME: ${{ env.PROJECT_NAME }}
16+
1517

1618
jobs:
1719
Test:
@@ -154,7 +156,7 @@ jobs:
154156

155157
- name: Configure CMake
156158
run: |
157-
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 }}
159+
cmake -S . -B ./build -G "${{matrix.generator}}" -D${{ env.PROJECT_NAME }}_ENABLE_IPO=${{matrix.enable_ipo }} -DCMAKE_BUILD_TYPE:STRING=${{matrix.build_type}} -D${{ env.PROJECT_NAME }}_PACKAGING_MAINTAINER_MODE:BOOL=${{matrix.package_maintainer_mode}} -D${{ env.PROJECT_NAME }}_ENABLE_COVERAGE:BOOL=${{ matrix.build_type == 'Debug' }} -DGIT_SHA:STRING=${{ github.sha }}
158160
159161
- name: Build
160162
# Execute the build. You can specify a specific target with "--target <NAME>"

.github/workflows/codeql-analysis.yml

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ on:
2020
schedule:
2121
- cron: '38 0 * * 5'
2222

23+
env:
24+
PROJECT_NAME: myproject
2325

2426
jobs:
2527
analyze:
@@ -80,11 +82,11 @@ jobs:
8082
# has meaningful results
8183
- name: Configure CMake
8284
run: |
83-
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' }}
85+
cmake -S . -B ./build -G "${{matrix.generator}}" -DCMAKE_BUILD_TYPE:STRING=${{matrix.build_type}} -D${{ env.PROJECT_NAME }}_PACKAGING_MAINTAINER_MODE:BOOL=${{matrix.packaging_maintainer_mode}} -D${{ env.PROJECT_NAME }}_ENABLE_COVERAGE:BOOL=${{ matrix.build_type == 'Debug' }}
8486
8587
# Initializes the CodeQL tools for scanning.
8688
- name: Initialize CodeQL
87-
uses: github/codeql-action/init@v1
89+
uses: github/codeql-action/init@v2
8890
with:
8991
languages: ${{ matrix.language }}
9092
# If you wish to specify custom queries, you can do so here or in a config file.
@@ -99,4 +101,4 @@ jobs:
99101
cmake --build ./build --config ${{matrix.build_type}}
100102
101103
- name: Perform CodeQL Analysis
102-
uses: github/codeql-action/analyze@v1
104+
uses: github/codeql-action/analyze@v2

CMakeLists.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,11 @@ endif()
7676

7777
if(myproject_BUILD_FUZZ_TESTS)
7878
message(AUTHOR_WARNING "Building Fuzz Tests, using fuzzing sanitizer https://www.llvm.org/docs/LibFuzzer.html")
79+
if (NOT myproject_ENABLE_ADDRESS_SANITIZER AND NOT myproject_ENABLE_THREAD_SANITIZER)
80+
message(WARNING "You need asan or tsan enabled for meaningful fuzz testing")
81+
endif()
7982
add_subdirectory(fuzz_test)
83+
8084
endif()
8185

8286
# If MSVC is being used, and ASAN is enabled, we need to set the debugger environment

ProjectOptions.cmake

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ include(cmake/LibFuzzer.cmake)
33
include(CMakeDependentOption)
44
include(CheckCXXCompilerFlag)
55

6-
76
macro(myproject_setup_options)
87
option(myproject_ENABLE_HARDENING "Enable hardening" ON)
98
option(myproject_ENABLE_COVERAGE "Enable coverage reporting" OFF)
@@ -14,7 +13,6 @@ macro(myproject_setup_options)
1413
myproject_ENABLE_HARDENING
1514
OFF)
1615

17-
1816
if((CMAKE_CXX_COMPILER_ID MATCHES ".*Clang.*" OR CMAKE_CXX_COMPILER_ID MATCHES ".*GNU.*") AND NOT WIN32)
1917
set(SUPPORTS_UBSAN ON)
2018
else()
@@ -27,10 +25,6 @@ macro(myproject_setup_options)
2725
set(SUPPORTS_ASAN ON)
2826
endif()
2927

30-
myproject_check_libfuzzer_support(LIBFUZZER_SUPPORTED)
31-
option(myproject_BUILD_FUZZ_TESTS "Enable fuzz testing executable" ${LIBFUZZER_SUPPORTED})
32-
33-
3428
if(NOT PROJECT_IS_TOP_LEVEL OR myproject_PACKAGING_MAINTAINER_MODE)
3529
option(myproject_ENABLE_IPO "Enable IPO/LTO" OFF)
3630
option(myproject_WARNINGS_AS_ERRORS "Treat Warnings As Errors" OFF)
@@ -78,6 +72,16 @@ macro(myproject_setup_options)
7872
myproject_ENABLE_PCH
7973
myproject_ENABLE_CACHE)
8074
endif()
75+
76+
myproject_check_libfuzzer_support(LIBFUZZER_SUPPORTED)
77+
if(LIBFUZZER_SUPPORTED AND (myproject_ENABLE_SANITIZER_ADDRESS OR myproject_ENABLE_SANITIZER_THREAD OR myproject_ENABLE_SANITIZER_UNDEFINED))
78+
set(DEFAULT_FUZZER ON)
79+
else()
80+
set(DEFAULT_FUZZER OFF)
81+
endif()
82+
83+
option(myproject_BUILD_FUZZ_TESTS "Enable fuzz testing executable" ${DEFAULT_FUZZER})
84+
8185
endmacro()
8286

8387
macro(myproject_global_options)
@@ -88,13 +92,21 @@ macro(myproject_global_options)
8892

8993
if(myproject_ENABLE_HARDENING AND myproject_ENABLE_GLOBAL_HARDENING)
9094
include(cmake/Hardening.cmake)
91-
set(ENABLE_UBSAN_MINIMAL_RUNTIME NOT myproject_ENABLE_SANITIZER_UNDEFINED)
95+
if(myproject_ENABLE_SANITIZER_UNDEFINED
96+
OR myproject_ENABLE_SANITIZER_ADDRESS
97+
OR myproject_ENABLE_SANITIZER_THREAD
98+
OR myproject_ENABLE_SANITIZER_LEAK)
99+
set(ENABLE_UBSAN_MINIMAL_RUNTIME FALSE)
100+
else()
101+
set(ENABLE_UBSAN_MINIMAL_RUNTIME TRUE)
102+
endif()
103+
message("${myproject_ENABLE_HARDENING} ${ENABLE_UBSAN_MINIMAL_RUNTIME} ${myproject_ENABLE_SANITIZER_UNDEFINED}")
92104
myproject_enable_hardening(myproject_options ON ${ENABLE_UBSAN_MINIMAL_RUNTIME})
93105
endif()
94106
endmacro()
95107

96108
macro(myproject_local_options)
97-
if (PROJECT_IS_TOP_LEVEL)
109+
if(PROJECT_IS_TOP_LEVEL)
98110
include(cmake/StandardProjectSettings.cmake)
99111
endif()
100112

@@ -165,7 +177,11 @@ macro(myproject_local_options)
165177

166178
if(myproject_ENABLE_HARDENING AND NOT myproject_ENABLE_GLOBAL_HARDENING)
167179
include(cmake/Hardening.cmake)
168-
set(ENABLE_UBSAN_MINIMAL_RUNTIME NOT myproject_ENABLE_SANITIZER_UNDEFINED)
180+
if(myproject_ENABLE_SANITIZER_UNDEFINED)
181+
set(ENABLE_UBSAN_MINIMAL_RUNTIME FALSE)
182+
else()
183+
set(ENABLE_UBSAN_MINIMAL_RUNTIME TRUE)
184+
endif()
169185
myproject_enable_hardening(myproject_options OFF ${ENABLE_UBSAN_MINIMAL_RUNTIME})
170186
endif()
171187

cmake/Hardening.cmake

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,15 @@ macro(
99
message(STATUS "** Enabling Hardening (Target ${target}) **")
1010

1111
if(MSVC)
12-
set(NEW_COMPILE_OPTIONS
13-
"${NEW_COMPILE_OPTIONS} /sdl /DYNAMICBASE /guard:cf /NXCOMPAT")
14-
message(STATUS "*** MSVC flags: /sdl /DYNAIMCBASE /guard:cf /NXCOMPAT")
12+
set(NEW_COMPILE_OPTIONS "${NEW_COMPILE_OPTIONS} /sdl /DYNAMICBASE /guard:cf")
13+
message(STATUS "*** MSVC flags: /sdl /DYNAMICBASE /guard:cf /NXCOMPAT /CETCOMPAT")
14+
set(NEW_LINK_OPTIONS "${NEW_LINK_OPTIONS} /NXCOMPAT /CETCOMPAT")
1515

1616
elseif(CMAKE_CXX_COMPILER_ID MATCHES ".*Clang|GNU")
1717
set(NEW_CXX_DEFINITIONS "${NEW_CXX_DEFINITIONS} -D_GLIBCXX_ASSERTIONS")
1818
message(STATUS "*** GLIBC++ Assertions (vector[], string[], ...) enabled")
1919

20-
set(NEW_COMPILE_OPTIONS
21-
"${NEW_COMPILE_OPTIONS} -U_FORTIFY_SOURCE -D_FORTIFY_SOURCE=3")
20+
set(NEW_COMPILE_OPTIONS "${NEW_COMPILE_OPTIONS} -U_FORTIFY_SOURCE -D_FORTIFY_SOURCE=3")
2221
message(STATUS "*** g++/clang _FORTIFY_SOURCE=3 enabled")
2322

2423
# check_cxx_compiler_flag(-fpie PIE)
@@ -64,8 +63,16 @@ macro(
6463
check_cxx_compiler_flag("-fsanitize=undefined -fno-sanitize-recover=undefined -fsanitize-minimal-runtime"
6564
MINIMAL_RUNTIME)
6665
if(MINIMAL_RUNTIME)
67-
set(NEW_COMPILE_OPTIONS
68-
"${NEW_COMPILE_OPTIONS} -fsanitize=undefined -fno-sanitize-recover=undefined -fsanitize-minimal-runtime")
66+
set(NEW_COMPILE_OPTIONS "${NEW_COMPILE_OPTIONS} -fsanitize=undefined -fsanitize-minimal-runtime")
67+
set(NEW_LINK_OPTIONS "${NEW_LINK_OPTIONS} -fsanitize=undefined -fsanitize-minimal-runtime")
68+
69+
if(NOT ${global})
70+
set(NEW_COMPILE_OPTIONS "${NEW_COMPILE_OPTIONS} -fno-sanitize-recover=undefined")
71+
set(NEW_LINK_OPTIONS "${NEW_LINK_OPTIONS} -fno-sanitize-recover=undefined")
72+
else()
73+
message(STATUS "** not enabling -fno-sanitize-recover=undefined for global consumption")
74+
endif()
75+
6976
message(STATUS "*** ubsan minimal runtime enabled")
7077
else()
7178
message(STATUS "*** ubsan minimal runtime NOT enabled (not supported)")

fuzz_test/CMakeLists.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ target_link_libraries(
1010
myproject_warnings
1111
fmt::fmt
1212
-coverage
13-
-fsanitize=fuzzer,undefined,address)
14-
target_compile_options(fuzz_tester PRIVATE -fsanitize=fuzzer,undefined,address)
13+
-fsanitize=fuzzer)
14+
target_compile_options(fuzz_tester PRIVATE -fsanitize=fuzzer)
1515

1616
# Allow short runs during automated testing to see if something new breaks
1717
set(FUZZ_RUNTIME

0 commit comments

Comments
 (0)