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 7 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
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 --extra-verbose
FrogTheFrog marked this conversation as resolved.
Show resolved Hide resolved

- 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/
69 changes: 18 additions & 51 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,67 +1,34 @@
#
# Project configuration
#
ReenigneArcher marked this conversation as resolved.
Show resolved Hide resolved
cmake_minimum_required(VERSION 3.13) # todo: what is the minimum version required?

project(libdisplaydevice
DESCRIPTION "Library to modify display devices."
HOMEPAGE_URL "https://app.lizardbyte.dev")
HOMEPAGE_URL "https://app.lizardbyte.dev"
LANGUAGES CXX)

set(PROJECT_LICENSE "GPL-3.0")
set(CMAKE_CXX_STANDARD 20)

if(NOT CMAKE_BUILD_TYPE AND NOT CMAKE_CONFIGURATION_TYPES)
message(STATUS "Setting build type to 'Release' as none was specified.")
set(CMAKE_BUILD_TYPE "Release" CACHE STRING "Choose the type of build." FORCE)
endif()

set(CMAKE_CXX_STANDARD 20)

# set the module path, used for includes
set(CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}/cmake")

# options
#
# Project optional configuration
#
option(BUILD_TESTS "Build tests" ON)
option(CODE_QL_ANALYSIS "Build is for codeql analysis" OFF)

# auto-set CODE_QL_ANALYSIS based on env variable
if(DEFINED ENV{GITHUB_CODEQL_BUILD})
set(CODE_QL_ANALYSIS ON)
endif()

# include directories (for including header files)
include_directories("${CMAKE_SOURCE_DIR}")
#
# Library code is located here
#
add_subdirectory(src)

# glob src files, excluding platf directory
file(GLOB_RECURSE DD_COMMON_TARGET_FILES "${CMAKE_SOURCE_DIR}/src/*.cpp")
list(FILTER DD_COMMON_TARGET_FILES EXCLUDE REGEX ".*/platf/.*")

if(WIN32)
file(GLOB_RECURSE DD_PLATFORM_TARGET_FILES "${CMAKE_SOURCE_DIR}/src/platf/windows/*.cpp")
include(${CMAKE_MODULE_PATH}/windows.cmake)
elseif(APPLE)
if(NOT CODE_QL_ANALYSIS)
message(FATAL_ERROR "MacOS is not supported yet.")
endif()
file(GLOB_RECURSE DD_PLATFORM_TARGET_FILES "${CMAKE_SOURCE_DIR}/src/platf/macos/*.cpp")
include(${CMAKE_MODULE_PATH}/macos.cmake)
elseif(UNIX)
if(NOT CODE_QL_ANALYSIS)
message(FATAL_ERROR "Linux is not supported yet.")
endif()
file(GLOB_RECURSE DD_PLATFORM_TARGET_FILES "${CMAKE_SOURCE_DIR}/src/platf/linux/*.cpp")
include(${CMAKE_MODULE_PATH}/linux.cmake)
else()
message(FATAL_ERROR "Unsupported platform")
endif()

message(STATUS "Common source files: ${DD_COMMON_TARGET_FILES}")
message(STATUS "Platform source files: ${DD_PLATFORM_TARGET_FILES}")

# Combine common and platform-specific source files
set(DD_TARGET_FILES ${DD_COMMON_TARGET_FILES} ${DD_PLATFORM_TARGET_FILES})

# tests
if(BUILD_TESTS)
#
# Testing only available if this is the main project
#
if(CMAKE_PROJECT_NAME STREQUAL PROJECT_NAME AND BUILD_TESTS)
enable_testing()
add_subdirectory(tests)
endif()

# lib
add_library(${PROJECT_NAME} ${DD_TARGET_FILES})
target_include_directories(${PROJECT_NAME} PUBLIC include)
3 changes: 0 additions & 3 deletions cmake/linux.cmake

This file was deleted.

3 changes: 0 additions & 3 deletions cmake/macos.cmake

This file was deleted.

3 changes: 0 additions & 3 deletions cmake/windows.cmake

This file was deleted.

19 changes: 19 additions & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# This a shared common library for other libraries
add_subdirectory(common)

# This is a platform-specific library
if(WIN32)
add_subdirectory(windows)
elseif(APPLE)
message(WARNING "MacOS is not supported yet.")
elseif(UNIX)
message(WARNING "Linux is not supported yet.")
else()
message(FATAL_ERROR "Unsupported platform")
endif()

# This is a platform-specific library that loads the correct library for the OS
add_subdirectory(platf)

# This is the main library
add_subdirectory(displaydevice)
12 changes: 12 additions & 0 deletions src/common/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# A global identifier for the library
set(MODULE libcommon)

# Globing headers (so that they appear in some IDEs) and sources
file(GLOB HEADER_LIST CONFIGURE_DEPENDS "include/displaydevice/*.h")
file(GLOB SOURCE_LIST CONFIGURE_DEPENDS "*.cpp")

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

# Provide the includes together with this library
target_include_directories(${MODULE} PUBLIC include)
File renamed without changes.
2 changes: 1 addition & 1 deletion src/logging.cpp → src/common/logging.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// class header include
#include "logging.h"
#include "displaydevice/logging.h"

// system includes
#include <chrono>
Expand Down
15 changes: 15 additions & 0 deletions src/displaydevice/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# A global identifier for the library
set(MODULE libdisplaydevice)

# Globing headers (so that they appear in some IDEs) and sources
file(GLOB HEADER_LIST CONFIGURE_DEPENDS "include/displaydevice/*.h")
file(GLOB SOURCE_LIST CONFIGURE_DEPENDS "*.cpp")

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

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

# Required libraries
target_link_libraries(${MODULE} PUBLIC libcommon PRIVATE libplatf)
Empty file.
6 changes: 6 additions & 0 deletions src/displaydevice/libddplaceholder.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#include "displaydevice/libddplaceholder.h"

int ddplaceholder()
{
return 0;
}
26 changes: 26 additions & 0 deletions src/platf/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# A global identifier for the library
set(MODULE libplatf)

# Globing headers (so that they appear in some IDEs) and sources
file(GLOB HEADER_LIST CONFIGURE_DEPENDS "include/displaydevice/*.h")
file(GLOB SOURCE_LIST CONFIGURE_DEPENDS "*.cpp")

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

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

# Shared libraries between platforms
target_link_libraries(${MODULE} PRIVATE libcommon)

# Link the platform specific library privately
if(WIN32)
target_link_libraries(${MODULE} PRIVATE libwindows)
elseif(APPLE)
message(WARNING "MacOS is not supported yet.")
elseif(UNIX)
message(WARNING "Linux is not supported yet.")
else()
message(FATAL_ERROR "Unsupported platform")
endif()
Empty file.
6 changes: 6 additions & 0 deletions src/platf/libplatfplaceholder.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#include "displaydevice/libplatfplaceholder.h"

int
plaftplaceholder() {
return 0;
}
15 changes: 15 additions & 0 deletions src/windows/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# A global identifier for the library
set(MODULE libwindows)

# Globing headers (so that they appear in some IDEs) and sources
file(GLOB HEADER_LIST CONFIGURE_DEPENDS "include/libdisplaydevice/windows/*.h")
file(GLOB SOURCE_LIST CONFIGURE_DEPENDS "*.cpp")

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

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

# Link the additional libraries
target_link_libraries(${MODULE} PRIVATE libcommon)
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
// class header include
#include "winapilayer.h"
#include "displaydevice/windows/winapilayer.h"

// system includes
#include <iomanip>

// local includes
#include "src/logging.h"
#include "displaydevice/logging.h"

namespace display_device {
namespace {
Expand Down
112 changes: 51 additions & 61 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,75 +1,65 @@
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}")

enable_testing()

# Add GoogleTest directory to the project
set(GTEST_SOURCE_DIR "${CMAKE_SOURCE_DIR}/third-party/googletest")
#
# Setup google test
#
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}")
include(GoogleTest)
add_subdirectory("${PROJECT_SOURCE_DIR}/third-party/googletest" "third-party/googletest")

# 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
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
set(gtest_force_shared_crt ON CACHE BOOL "Always use msvcrt.dll" FORCE) # cmake-lint: disable=C0103
FrogTheFrog marked this conversation as resolved.
Show resolved Hide resolved
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)
# A helper function to setup the dependencies for the test executable
function(add_dd_test_dir)
set(options "")
set(oneValueArgs "")
set(multiValueArgs ADDITIONAL_LIBRARIES)
cmake_parse_arguments(FN_VARS "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN})

# Get the current sources and libraries
get_property(sources GLOBAL PROPERTY DD_TEST_SOURCES)
get_property(libraries GLOBAL PROPERTY DD_TEST_LIBRARIES)

# Gather new data
file(GLOB test_files CONFIGURE_DEPENDS "${CMAKE_CURRENT_SOURCE_DIR}/test_*.cpp")

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

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()
# Update the global variables
set_property(GLOBAL PROPERTY DD_TEST_SOURCES "${sources}")
set_property(GLOBAL PROPERTY DD_TEST_LIBRARIES "${libraries}")
endfunction()

list(APPEND TEST_PLATFORM_SOURCES ${CURRENT_TEST_PLATFORM_SOURCES})
endforeach()
#
# Add subdirectories
#
add_subdirectory(fixtures)
add_subdirectory(unit)

list(FILTER TEST_SOURCES EXCLUDE REGEX ".*/platf/.*")
message(STATUS "Common Test source files: ${TEST_SOURCES}")
message(STATUS "Platform Test source files: ${TEST_PLATFORM_SOURCES}")
#
# Setup the final test binary
#
set(TEST_BINARY test_libdisplaydevice)
get_property(sources GLOBAL PROPERTY DD_TEST_SOURCES)
get_property(libraries GLOBAL PROPERTY DD_TEST_LIBRARIES)

set(DD_SOURCES
${DD_TARGET_FILES})
add_executable(${TEST_BINARY} ${sources})
target_link_libraries(${TEST_BINARY}
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
${libraries} # additional libraries if needed
)

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 the test to CTest
gtest_discover_tests(${TEST_BINARY})

add_test(NAME ${PROJECT_NAME} COMMAND libdisplaydevice_test)
#
# 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")
Loading