Skip to content

Commit

Permalink
Optionally build C++20 modules
Browse files Browse the repository at this point in the history
  • Loading branch information
bgs99 committed May 21, 2024
1 parent 9f69cb5 commit 1f58339
Show file tree
Hide file tree
Showing 7 changed files with 68 additions and 2 deletions.
17 changes: 17 additions & 0 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,20 @@ jobs:
run: cmake --build ${{ vars.BUILD_DIR }}
- name: Run tests
run: ctest --output-on-failure --test-dir ${{ vars.BUILD_DIR }}

test-modules:
runs-on: ${{ matrix.os }}
strategy:
fail-fast: false
matrix:
os:
- windows-2022
steps:
- name: Checkout repository
uses: actions/checkout@v3
- name: Configure CMake
run: cmake -B ${{ vars.BUILD_DIR }} -DTMP_MODULES:BOOL=ON

Check failure on line 42 in .github/workflows/tests.yml

View workflow job for this annotation

GitHub Actions / yamllint

42:14 [colons] too many spaces after colon
- name: Build tests
run: cmake --build ${{ vars.BUILD_DIR }}

Check failure on line 44 in .github/workflows/tests.yml

View workflow job for this annotation

GitHub Actions / yamllint

44:14 [colons] too many spaces after colon
- name: Run tests
run: ctest --output-on-failure --test-dir ${{ vars.BUILD_DIR }}

Check failure on line 46 in .github/workflows/tests.yml

View workflow job for this annotation

GitHub Actions / yamllint

46:14 [colons] too many spaces after colon
21 changes: 19 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
cmake_minimum_required(VERSION 3.12)
option(TMP_MODULES "Build C++20 modules" OFF)
if(TMP_MODULES)
cmake_minimum_required(VERSION 3.28)
else()
cmake_minimum_required(VERSION 3.12)
endif()

project(tmp LANGUAGES CXX)

list(APPEND CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/cmake/)
Expand All @@ -7,7 +13,18 @@ include(GNUInstallDirs)
find_package(Filesystem REQUIRED)

add_library(${PROJECT_NAME} STATIC src/tmp.cpp)
target_compile_features(${PROJECT_NAME} PUBLIC cxx_std_17)

if(TMP_MODULES)
target_compile_features(${PROJECT_NAME} PUBLIC cxx_std_20)
target_sources(${PROJECT_NAME}
PUBLIC
FILE_SET CXX_MODULES FILES
src/tmp.cppm
)
else()
target_compile_features(${PROJECT_NAME} PUBLIC cxx_std_17)
endif()

target_link_libraries(${PROJECT_NAME} PUBLIC std::filesystem)
set_target_properties(${PROJECT_NAME} PROPERTIES EXPORT_NAME tmp)
target_include_directories(
Expand Down
17 changes: 17 additions & 0 deletions src/tmp.cppm
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
module;

#include <tmp/directory>
#include <tmp/file>
#include <tmp/filesystem>
#include <tmp/path>

export module tmp;

export {
namespace tmp {
using tmp::directory;
using tmp::file;
using tmp::filesystem;
using tmp::path;
} // namespace tmp
}
3 changes: 3 additions & 0 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ add_executable(${PROJECT_NAME} directory.cpp file.cpp filesystem.cpp)
target_link_libraries(${PROJECT_NAME} tmp::tmp GTest::gtest_main)
target_compile_definitions(
${PROJECT_NAME} PRIVATE PREFIX="com.github.bugdea1er.tmp")
if (TMP_MODULES)
target_compile_definitions(${PROJECT_NAME} PRIVATE TMP_USE_MODULES)
endif()

include(GoogleTest)
gtest_discover_tests(${PROJECT_NAME} EXTRA_ARGS --gtest_color=yes)
4 changes: 4 additions & 0 deletions tests/directory.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
#ifdef TMP_USE_MODULES
import tmp;
#else
#include <tmp/directory>
#include <tmp/file>
#endif

#include <gtest/gtest.h>

Expand Down
4 changes: 4 additions & 0 deletions tests/file.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
#ifdef TMP_USE_MODULES
import tmp;
#else
#include <tmp/directory>
#include <tmp/file>
#endif

#include <gtest/gtest.h>

Expand Down
4 changes: 4 additions & 0 deletions tests/filesystem.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
#ifdef TMP_USE_MODULES
import tmp;
#else
#include <tmp/file>
#include <tmp/filesystem>
#endif

#include <gtest/gtest.h>

Expand Down

0 comments on commit 1f58339

Please sign in to comment.