Skip to content
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

CMake and CI refinements #44

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
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
8 changes: 3 additions & 5 deletions .github/workflows/build-linux.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ jobs:
c_compiler: [gcc, clang]
sanitizers: [address, OFF]
steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4
- name: Set reusable strings
id: strings
shell: bash
Expand All @@ -24,18 +24,16 @@ jobs:
- name: Configure CMake
run: >
cmake -B ${{ steps.strings.outputs.build-output-dir }}
-DCMAKE_CXX_COMPILER=${{ matrix.cpp_compiler }}
-DCMAKE_C_COMPILER=${{ matrix.c_compiler }}
-DCMAKE_BUILD_TYPE=${{ matrix.build_type }}
-DENABLE_TESTS=1
-DENABLE_COVERAGE=1
-DENABLE_SANITIZER=${{ matrix.sanitizers }}
-S ${{ github.workspace }}
- name: Build
run: cmake --build ${{ steps.strings.outputs.build-output-dir }} --config ${{ matrix.build_type }}
run: cmake --build ${{ steps.strings.outputs.build-output-dir }}
- name: Test
working-directory: ${{ steps.strings.outputs.build-output-dir }}
run: ctest --build-config ${{ matrix.build_type }}
run: ctest --ttest-dir ${{ steps.strings.outputs.build-output-dir }}
- name: Coverage
if: ${{ matrix.c_compiler == 'gcc' && matrix.sanitizers == 'OFF' }}
working-directory: ${{ steps.strings.outputs.build-output-dir }}
Expand Down
8 changes: 3 additions & 5 deletions .github/workflows/build-macos.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ jobs:
build_type: [Release]
c_compiler: [gcc, clang]
steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4
- name: Set reusable strings
id: strings
shell: bash
Expand All @@ -23,13 +23,11 @@ jobs:
- name: Configure CMake
run: >
cmake -B ${{ steps.strings.outputs.build-output-dir }}
-DCMAKE_CXX_COMPILER=${{ matrix.cpp_compiler }}
-DCMAKE_C_COMPILER=${{ matrix.c_compiler }}
-DCMAKE_BUILD_TYPE=${{ matrix.build_type }}
-DENABLE_TESTS=1
-S ${{ github.workspace }}
- name: Build
run: cmake --build ${{ steps.strings.outputs.build-output-dir }} --config ${{ matrix.build_type }}
run: cmake --build ${{ steps.strings.outputs.build-output-dir }}
- name: Test
working-directory: ${{ steps.strings.outputs.build-output-dir }}
run: ctest --build-config ${{ matrix.build_type }}
run: ctest --test-dir ${{ steps.strings.outputs.build-output-dir }}
3 changes: 1 addition & 2 deletions .github/workflows/build-win.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ jobs:
build_type: [Release]
c_compiler: [cl]
steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4
- name: Set reusable strings
id: strings
shell: bash
Expand All @@ -23,7 +23,6 @@ jobs:
- name: Configure CMake
run: >
cmake -B ${{ steps.strings.outputs.build-output-dir }}
-DCMAKE_CXX_COMPILER=${{ matrix.cpp_compiler }}
-DCMAKE_C_COMPILER=${{ matrix.c_compiler }}
-DCMAKE_BUILD_TYPE=${{ matrix.build_type }}
-DCMAKE_C_FLAGS=/wd5105
Expand Down
21 changes: 13 additions & 8 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
cmake_minimum_required(VERSION 3.9.2)
cmake_minimum_required(VERSION 3.14)

# set project name
project(cwalk
Expand All @@ -7,6 +7,11 @@ project(cwalk
HOMEPAGE_URL "https://likle.github.io/cwalk/"
LANGUAGES C)

option(ENABLE_COVERAGE "Coverage testing")
option(BUILD_SHARED_LIBS "Build shared library")
option(ENABLE_TESTS "Build test executables")
option(IGNORE_WARNINGS "Ignore warnings")

# include utilities
list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake")
include(EnableWarnings)
Expand All @@ -25,16 +30,16 @@ set(TEST_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/test")

# enable coverage if requested
if(ENABLE_COVERAGE)
message("-- Coverage enabled")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fprofile-arcs -ftest-coverage")
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} --coverage")
message(STATUS "Coverage enabled")
add_compile_options("$<$<COMPILE_LANGUAGE:C>:-fprofile-arcs;-ftest-coverage>")
add_link_options("$<$<COMPILE_LANGUAGE:C>:--coverage>")
endif()

# enable sanitizer
if(ENABLE_SANITIZER)
message("-- Sanitizer enabled")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fno-omit-frame-pointer -fsanitize=${ENABLE_SANITIZER}")
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -fno-omit-frame-pointer -fsanitize=${ENABLE_SANITIZER}")
message(STATUS "Sanitizer enabled")
add_compile_options("$<$<COMPILE_LANGUAGE:C>:-fno-omit-frame-pointer;-fsanitize=${ENABLE_SANITIZER}>")
add_link_options("$<$<COMPILE_LANGUAGE:C>:-fno-omit-frame-pointer;-fsanitize=${ENABLE_SANITIZER}>")
endif()

# add the main executable
Expand All @@ -56,7 +61,7 @@ endif()

# enable tests
if(ENABLE_TESTS)
message("-- Tests enabled")
message(STATUS "Tests enabled")
enable_testing()

create_test_list(DEFAULT cwalktest)
Expand Down
7 changes: 4 additions & 3 deletions cmake/CreateTestList.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@ endfunction()

function(write_test_file list_name file)
set("TEST_LIST_FILE_${list_name}" ${file} PARENT_SCOPE)
file(WRITE ${file} "#define UNIT_TESTS(XX) \\\n")
file(APPEND ${file} ${TEST_LIST_CONTENT_${list_name}})
file(APPEND ${file} "\n")
file(WRITE ${file} "#define UNIT_TESTS(XX) \\
${TEST_LIST_CONTENT_${list_name}}
")
endfunction()

function(create_test list_name unit_name test_name)
set(TEST_LIST_CONTENT_${list_name} "${TEST_LIST_CONTENT_${list_name}} XX(${unit_name},${test_name}) \\\n" PARENT_SCOPE)
add_test(NAME "${unit_name}_${test_name}" COMMAND ${TEST_LIST_TARGET_${list_name}} ${unit_name} ${test_name})
set_property(TEST "${unit_name}_${test_name}" PROPERTY LABELS "${unit_name}")
endfunction()
35 changes: 18 additions & 17 deletions cmake/EnableWarnings.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,24 @@ function(enable_warnings target)
if(MSVC)
target_compile_definitions(${target} PRIVATE _CRT_SECURE_NO_WARNINGS)
target_compile_options(${target} PRIVATE /W4)
if (NOT IGNORE_WARNINGS)
target_compile_options(${target} PRIVATE /WX)
endif()
elseif("${CMAKE_C_COMPILER_ID}" STREQUAL "Clang")
target_compile_options(${target} PRIVATE -Wall)
target_compile_options(${target} PRIVATE -Wextra)
target_compile_options(${target} PRIVATE -Wpedantic)
target_compile_options(${target} PRIVATE -Wno-gnu-zero-variadic-macro-arguments)
if (NOT IGNORE_WARNINGS)
target_compile_options(${target} PRIVATE -Werror)
endif()
elseif("${CMAKE_C_COMPILER_ID}" STREQUAL "GNU")
target_compile_options(${target} PRIVATE -Wall)
target_compile_options(${target} PRIVATE -Wextra)
target_compile_options(${target} PRIVATE -Wpedantic)
if (NOT IGNORE_WARNINGS)
target_compile_options(${target} PRIVATE -Werror)
elseif(CMAKE_C_COMPILER_ID STREQUAL "Clang")
target_compile_options(${target} PRIVATE
-Wall
-Wextra
-Wpedantic
-Wno-gnu-zero-variadic-macro-arguments
)
elseif(CMAKE_C_COMPILER_ID STREQUAL "GNU")
target_compile_options(${target} PRIVATE
-Wall
-Wextra
-Wpedantic
)
endif()

if(CMAKE_VERSION VERSION_GREATER_EQUAL "3.24")
if(NOT IGNORE_WARNINGS)
set_property(TARGET ${target} PROPERTY COMPILE_WARNING_AS_ERROR true)
endif()
endif()
endfunction()