Skip to content

Commit f50eaf3

Browse files
authored
chore: merge pull request #24 from threeal/cmake-scoped-flags
CMake Scoped Compiler Flags
2 parents 6c2d345 + d3c1463 commit f50eaf3

File tree

2 files changed

+27
-14
lines changed

2 files changed

+27
-14
lines changed

.github/workflows/build.yml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ jobs:
2121
run: |
2222
cmake ${{ matrix.package }} \
2323
-B ${{ matrix.package }}/build \
24-
-D CMAKE_CXX_FLAGS=-Werror \
2524
-D BUILD_TESTING=ON
2625
2726
- name: Check code formatting
@@ -58,7 +57,6 @@ jobs:
5857
cmake ${{ matrix.package }} `
5958
-B ${{ matrix.package }}/build `
6059
-D CMAKE_CXX_COMPILER=cl `
61-
-D CMAKE_CXX_FLAGS=/WX `
6260
-D BUILD_TESTING=ON
6361
6462
- name: Build project

error/CMakeLists.txt

Lines changed: 27 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,36 +2,51 @@ cmake_minimum_required(VERSION 3.0)
22

33
project(error)
44

5-
if(MSVC)
6-
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /permissive- /W4 /w14640 /EHsc")
7-
else()
8-
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wextra -Wnon-virtual-dtor -Wpedantic")
9-
endif()
10-
11-
set(CMAKE_CXX_STANDARD 11)
12-
5+
# Import dependencies
136
include(cmake/CPM.cmake)
147
cpmaddpackage("gh:fmtlib/fmt#10.0.0")
158

9+
# Build the main library
1610
add_library(error src/error.cpp)
1711
target_include_directories(error PUBLIC include)
1812
target_link_libraries(error PUBLIC fmt)
1913

14+
# Check if this project is the main project
2015
if(CMAKE_CURRENT_SOURCE_DIR STREQUAL CMAKE_SOURCE_DIR)
16+
# Import Format.cmake to format source code
2117
cpmaddpackage("gh:TheLartians/Format.cmake@1.7.3")
2218

2319
if(BUILD_TESTING)
2420
enable_testing()
2521

22+
# Import Catch2 as the main testing framework
2623
cpmaddpackage("gh:catchorg/Catch2@3.3.2")
2724
include("${Catch2_SOURCE_DIR}/extras/Catch.cmake")
2825

29-
if(NOT MSVC)
30-
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fprofile-arcs -ftest-coverage -fPIC -O0")
31-
endif()
32-
26+
# Build tests for the main library
3327
add_executable(error_test test/error_test.cpp)
3428
target_link_libraries(error_test PRIVATE error Catch2::Catch2WithMain)
3529
catch_discover_tests(error_test)
3630
endif()
31+
32+
# Get all targets in this directory
33+
get_property(
34+
TARGETS
35+
DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
36+
PROPERTY BUILDSYSTEM_TARGETS)
37+
38+
foreach(TARGET IN LISTS TARGETS)
39+
# Statically analyze code by checking for warnings
40+
if(MSVC)
41+
target_compile_options(${TARGET} PRIVATE /WX /permissive- /W4 /w14640 /EHsc)
42+
else()
43+
target_compile_options(${TARGET} PRIVATE -Werror -Wall -Wextra -Wnon-virtual-dtor -Wpedantic)
44+
endif()
45+
46+
# Enable support to check for test coverage
47+
if(BUILD_TESTING AND NOT MSVC)
48+
target_compile_options(${TARGET} PRIVATE --coverage -O0)
49+
target_link_options(${TARGET} PRIVATE --coverage)
50+
endif()
51+
endforeach()
3752
endif()

0 commit comments

Comments
 (0)