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

feat: Modernize cmake #17

Merged
merged 10 commits into from
May 2, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
4 changes: 2 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -121,9 +121,9 @@ jobs:
- name: Run tests
id: test
shell: msys2 {0}
working-directory: build/tests
working-directory: build
run: |
./test_libdisplaydevice.exe --gtest_color=yes
ctest

- name: Generate gcov report
# any except canceled or skipped
Expand Down
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,6 @@
# build directories
build/
cmake-*/

# CTest
Testing/
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,6 @@ add_subdirectory(src)
#
# Testing only available if this is the main project
#
if(CMAKE_PROJECT_NAME STREQUAL PROJECT_NAME AND BUILD_TESTING)
if(CMAKE_PROJECT_NAME STREQUAL PROJECT_NAME AND BUILD_TESTS)
add_subdirectory(tests)
endif()
2 changes: 1 addition & 1 deletion src/displaydevice/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,4 @@ add_library(${MODULE} ${HEADER_LIST} libddplaceholder.cpp)
target_include_directories(${MODULE} PUBLIC include)

# Required libraries
target_link_libraries(${MODULE} PRIVATE libplatf libcommon)
target_link_libraries(${MODULE} PUBLIC libcommon PRIVATE libplatf)
102 changes: 38 additions & 64 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,75 +1,49 @@
cmake_minimum_required(VERSION 3.13)
# https://github.com/google/oss-policies-info/blob/main/foundational-cxx-support-matrix.md#foundational-c-support

project(test_libdisplaydevice)

include_directories("${CMAKE_SOURCE_DIR}")

#
# Setup google test
#
enable_testing()

# Add GoogleTest directory to the project
set(GTEST_SOURCE_DIR "${CMAKE_SOURCE_DIR}/third-party/googletest")
set(INSTALL_GTEST OFF)
set(INSTALL_GMOCK OFF)
add_subdirectory("${GTEST_SOURCE_DIR}" "${CMAKE_CURRENT_BINARY_DIR}/googletest")
include_directories("${GTEST_SOURCE_DIR}/googletest/include" "${GTEST_SOURCE_DIR}")

# coverage
include(GoogleTest)
add_subdirectory("${PROJECT_SOURCE_DIR}/third-party/googletest" "third-party/googletest")

# A helper function to setup the test executables
function(add_dd_tests)
set(options "")
set(oneValueArgs "")
set(multiValueArgs TESTS LIBRARIES)
cmake_parse_arguments(FN_VARS "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN})

foreach(test ${FN_VARS_TESTS})
add_executable(${test} ${test}.cpp)
target_link_libraries(${test}
gtest
gtest_main # if we use this we don't need our own main function
libdisplaydevice # we are always testing at least the public API so it's safe to always link this
libfixtures # these are our fixtures/helpers for the tests
${FN_VARS_LIBRARIES} # additional libraries if needed
)

# Adds the test to CTest
gtest_discover_tests(${test})
endforeach()
endfunction()

#
# Add subdirectories
#
add_subdirectory(fixtures)
add_subdirectory(unit)

#
# Additional setup for coverage
# https://gcovr.com/en/stable/guide/compiling.html#compiler-options
#
set(CMAKE_CXX_FLAGS "-fprofile-arcs -ftest-coverage -O1")
set(CMAKE_C_FLAGS "-fprofile-arcs -ftest-coverage -O1")

# if windows
# TODO: why is this needed?
FrogTheFrog marked this conversation as resolved.
Show resolved Hide resolved
if (WIN32)
# For Windows: Prevent overriding the parent project's compiler/linker settings
set(gtest_force_shared_crt ON CACHE BOOL "" FORCE) # cmake-lint: disable=C0103
endif ()

set(TEST_PATTERNS test_* mock_*)
file(GLOB_RECURSE TEST_SOURCES
"${CMAKE_SOURCE_DIR}/tests/conftest.cpp"
"${CMAKE_SOURCE_DIR}/tests/utils.cpp")
set(TEST_PLATFORM_SOURCES)

foreach(pattern ${TEST_PATTERNS})
file(GLOB_RECURSE CURRENT_TEST_SOURCES
"${CMAKE_SOURCE_DIR}/tests/${pattern}.cpp")
list(APPEND TEST_SOURCES ${CURRENT_TEST_SOURCES})

if(WIN32)
file(GLOB_RECURSE CURRENT_TEST_PLATFORM_SOURCES
"${CMAKE_SOURCE_DIR}/tests/*/platf/windows/${pattern}.cpp")
elseif(__APPLE__)
file(GLOB_RECURSE CURRENT_TEST_PLATFORM_SOURCES
"${CMAKE_SOURCE_DIR}/tests/*/platf/macos/${pattern}.cpp")
elseif(UNIX)
file(GLOB_RECURSE CURRENT_TEST_PLATFORM_SOURCES
"${CMAKE_SOURCE_DIR}/tests/*/platf/linux/${pattern}.cpp")
else()
message(FATAL_ERROR "Unsupported platform")
endif()

list(APPEND TEST_PLATFORM_SOURCES ${CURRENT_TEST_PLATFORM_SOURCES})
endforeach()

list(FILTER TEST_SOURCES EXCLUDE REGEX ".*/platf/.*")
message(STATUS "Common Test source files: ${TEST_SOURCES}")
message(STATUS "Platform Test source files: ${TEST_PLATFORM_SOURCES}")

set(DD_SOURCES
${DD_TARGET_FILES})

add_executable(${PROJECT_NAME}
${TEST_SOURCES}
${TEST_PLATFORM_SOURCES}
${DD_SOURCES})
set_target_properties(${PROJECT_NAME} PROPERTIES CXX_STANDARD 20)
target_link_libraries(${PROJECT_NAME}
${DD_EXTERNAL_LIBRARIES}
gtest
gtest_main) # if we use this we don't need our own main function
target_compile_definitions(${PROJECT_NAME} PUBLIC ${DD_DEFINITIONS} ${TEST_DEFINITIONS})
target_compile_options(${PROJECT_NAME} PRIVATE $<$<COMPILE_LANGUAGE:CXX>:${DD_COMPILE_OPTIONS}> -std=c++20)
target_link_options(${PROJECT_NAME} PRIVATE)

add_test(NAME ${PROJECT_NAME} COMMAND libdisplaydevice_test)
177 changes: 0 additions & 177 deletions tests/conftest.cpp

This file was deleted.

14 changes: 14 additions & 0 deletions tests/fixtures/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# A global identifier for the library
set(MODULE libfixtures)

# Globing headers here so that they appear in some IDEs
file(GLOB HEADER_LIST CONFIGURE_DEPENDS "*.h")

# Automatic library - will be static or dynamic based on user setting
add_library(${MODULE} ${HEADER_LIST} utils.cpp fixtures.cpp)

# Provide the includes together with this library
target_include_directories(${MODULE} PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})

# Link the additional libraries
target_link_libraries(${MODULE} PRIVATE gtest libcommon)
Loading