Skip to content

CMake Scoped Compiler Flags #24

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 10 commits into from
Jun 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ jobs:
run: |
cmake ${{ matrix.package }} \
-B ${{ matrix.package }}/build \
-D CMAKE_CXX_FLAGS=-Werror \
-D BUILD_TESTING=ON

- name: Check code formatting
Expand Down Expand Up @@ -58,7 +57,6 @@ jobs:
cmake ${{ matrix.package }} `
-B ${{ matrix.package }}/build `
-D CMAKE_CXX_COMPILER=cl `
-D CMAKE_CXX_FLAGS=/WX `
-D BUILD_TESTING=ON

- name: Build project
Expand Down
39 changes: 27 additions & 12 deletions error/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,36 +2,51 @@ cmake_minimum_required(VERSION 3.0)

project(error)

if(MSVC)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /permissive- /W4 /w14640 /EHsc")
else()
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wextra -Wnon-virtual-dtor -Wpedantic")
endif()

set(CMAKE_CXX_STANDARD 11)

# Import dependencies
include(cmake/CPM.cmake)
cpmaddpackage("gh:fmtlib/fmt#10.0.0")

# Build the main library
add_library(error src/error.cpp)
target_include_directories(error PUBLIC include)
target_link_libraries(error PUBLIC fmt)

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

if(BUILD_TESTING)
enable_testing()

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

if(NOT MSVC)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fprofile-arcs -ftest-coverage -fPIC -O0")
endif()

# Build tests for the main library
add_executable(error_test test/error_test.cpp)
target_link_libraries(error_test PRIVATE error Catch2::Catch2WithMain)
catch_discover_tests(error_test)
endif()

# Get all targets in this directory
get_property(
TARGETS
DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
PROPERTY BUILDSYSTEM_TARGETS)

foreach(TARGET IN LISTS TARGETS)
# Statically analyze code by checking for warnings
if(MSVC)
target_compile_options(${TARGET} PRIVATE /WX /permissive- /W4 /w14640 /EHsc)
else()
target_compile_options(${TARGET} PRIVATE -Werror -Wall -Wextra -Wnon-virtual-dtor -Wpedantic)
endif()

# Enable support to check for test coverage
if(BUILD_TESTING AND NOT MSVC)
target_compile_options(${TARGET} PRIVATE --coverage -O0)
target_link_options(${TARGET} PRIVATE --coverage)
endif()
endforeach()
endif()