Skip to content

Commit

Permalink
initial
Browse files Browse the repository at this point in the history
  • Loading branch information
scivision committed Apr 25, 2024
0 parents commit 067279b
Show file tree
Hide file tree
Showing 10 changed files with 325 additions and 0 deletions.
25 changes: 25 additions & 0 deletions .github/workflows/cmake.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
name: cmake

on:
push:
paths:
- "**.cpp"
- "**.ixx"
- "**.cmake"
- "**/CMakeLists.txt"
- ".github/workflows/cmake.yml"

env:
CTEST_NO_TESTS_ACTION: error
CTEST_PARALLEL_LEVEL: 4


jobs:

windows-msvc:
runs-on: windows-latest

steps:
- uses: actions/checkout@v4

- run: cmake --workflow --preset debug
18 changes: 18 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
cmake_minimum_required(VERSION 3.28...3.30)

project(CppModules LANGUAGES CXX)

enable_testing()

include(CheckCXXSymbolExists)


if(NOT CMAKE_CXX_STANDARD)
set(CMAKE_CXX_STANDARD 23)
endif()

include(cmake/compilers.cmake)

add_subdirectory(src)

file(GENERATE OUTPUT .gitignore CONTENT "*")
147 changes: 147 additions & 0 deletions CMakePresets.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
{
"version": 6,

"configurePresets": [
{
"name": "default",
"binaryDir": "${sourceDir}/build",
"cacheVariables": {
"CMAKE_COMPILE_WARNING_AS_ERROR": true
}
},
{
"name": "multi", "inherits": "default",
"displayName": "Ninja Multi-Config",
"generator": "Ninja Multi-Config"
},
{
"name": "msvc", "inherits": "default",
"generator": "Visual Studio 17 2022",
"cacheVariables": {
"CMAKE_COMPILE_WARNING_AS_ERROR": false
}
}
],
"buildPresets": [
{
"name": "default",
"configurePreset": "default"
},
{
"name": "release",
"configurePreset": "multi",
"configuration": "Release"
},
{
"name": "debug",
"configurePreset": "multi",
"configuration": "Debug"
},
{
"name": "msvc-debug",
"configurePreset": "msvc",
"configuration": "Debug"
}
],
"testPresets": [
{
"name": "default",
"configurePreset": "default",
"output": {
"outputOnFailure": true,
"verbosity": "verbose"
},
"execution": {
"noTestsAction": "error",
"scheduleRandom": true,
"stopOnFailure": false,
"timeout": 60
}
},
{
"name": "release", "inherits": "default",
"configurePreset": "multi",
"configuration": "Release"
},
{
"name": "debug", "inherits": "default",
"configurePreset": "multi",
"configuration": "Debug"
},
{
"name": "msvc-debug", "inherits": "default",
"configurePreset": "msvc",
"configuration": "Debug"
}
],
"workflowPresets": [
{
"name": "default",
"steps": [
{
"type": "configure",
"name": "default"
},
{
"type": "build",
"name": "default"
},
{
"type": "test",
"name": "default"
}
]
},
{
"name": "debug",
"steps": [
{
"type": "configure",
"name": "multi"
},
{
"type": "build",
"name": "debug"
},
{
"type": "test",
"name": "debug"
}
]
},
{
"name": "release",
"steps": [
{
"type": "configure",
"name": "multi"
},
{
"type": "build",
"name": "release"
},
{
"type": "test",
"name": "release"
}
]
},
{
"name": "msvc",
"steps": [
{
"type": "configure",
"name": "msvc"
},
{
"type": "build",
"name": "msvc-debug"
},
{
"type": "test",
"name": "msvc-debug"
}
]
}
]
}
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2024 scivision

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
21 changes: 21 additions & 0 deletions Readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# C++ modules with CMake

[![cmake](https://github.com/scivision/CppModules/actions/workflows/cmake.yml/badge.svg)](https://github.com/scivision/CppModules/actions/workflows/cmake.yml)

C++ modules require recent C++ compilers such as:

* Visual Studio ≥ [16.8](https://devblogs.microsoft.com/cppblog/standard-c20-modules-support-with-msvc-in-visual-studio-2019-version-16-8/)
* GCC ≥ 14
* Clang ≥ 16

The Ninja generator is required for C++ modules with CMake.

## Reference

A fully working
[ball pit simulation](https://github.com/cdacamar/ball_pit)
shows more advanced use of C++ modules.

https://www.kitware.com/import-cmake-c20-modules/

https://cmake.org/cmake/help/latest/manual/cmake-cxxmodules.7.html
17 changes: 17 additions & 0 deletions cmake/compilers.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
if(CMAKE_CXX_COMPILER_ID STREQUAL "MSVC")
# /std:c++20 or newer implicitly enables modules
# https://learn.microsoft.com/en-us/cpp/build/reference/experimental-module
elseif(CMAKE_CXX_COMPILER_ID STREQUAL "GNU" AND CMAKE_CXX_COMPILER_VERSION VERSION_GREATER_EQUAL 14.1)
add_compile_options(-fmodules-ts)
elseif(CMAKE_CXX_COMPILER_ID STREQUAL "Clang" AND CMAKE_CXX_COMPILER_VERSION VERSION_GREATER_EQUAL 17)
# -std=c++20 or higher implicitly enables modules
# https://releases.llvm.org/17.0.1/tools/clang/docs/StandardCPlusPlusModules.html#how-to-enable-standard-c-modules
endif()

check_cxx_symbol_exists(__cpp_modules "" HAVE_CXX_MODULES)
# Clang 19 didn't have __cpp_modules
if(NOT CMAKE_CXX_COMPILER_ID STREQUAL "Clang" AND NOT HAVE_CXX_MODULES)
message(FATAL_ERROR "${CMAKE_CXX_COMPILER_ID} ${CMAKE_CXX_COMPILER_VERSION} does not support C++20 modules")
endif()

check_cxx_symbol_exists(__cpp_lib_modules "version" HAVE_STD_MODULES)
38 changes: 38 additions & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
if(MSVC AND HAVE_STD_MODULES)
find_file(msvc_std_src NAMES std.ixx
PATHS $ENV{VCToolsInstallDir}/modules
NO_DEFAULT_PATH)

if(msvc_std_src)

set(msvc_std_ifc ${CMAKE_CURRENT_BINARY_DIR}/std.ifc)

add_custom_target(msvc_std
COMMAND ${CMAKE_CXX_COMPILER} /std:c++latest /EHsc /nologo /c ${msvc_std_src}
VERBATIM
BYPRODUCTS ${msvc_std_ifc}
)

cmake_path(CONVERT ${msvc_std_ifc} TO_NATIVE_PATH_LIST msvc_std_ifc)


add_executable(msvc_core msvc_core.cpp)
target_compile_options(msvc_core PRIVATE /reference ${msvc_std_ifc})
add_dependencies(msvc_core msvc_std)

add_test(NAME msvc_core COMMAND msvc_core)

else()
message(WARNING "Cannot find MSVC std.ixx, skipping import std example")
endif()
endif()

add_executable(math_mod math.cpp)
# https://cmake.org/cmake/help/latest/command/target_sources.html#file-sets
target_sources(math_mod PUBLIC
FILE_SET my_math
TYPE CXX_MODULES
FILES math.ixx
)

add_test(NAME MathModule COMMAND math_mod)
19 changes: 19 additions & 0 deletions src/math.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#include <iostream>
#include <cassert>

import math;

int main(){
int a=1;
int b=2;

int absum = add(a, b);
int abdif = subtract(a, b);

assert(a + b == absum);
assert(a - b == abdif);

std::cout << "OK: Modules demo" << std::endl;

return EXIT_SUCCESS;
}
9 changes: 9 additions & 0 deletions src/math.ixx
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
export module math;

export int add(int a, int b){
return a + b;
}

export int subtract(int a, int b){
return a - b;
}
10 changes: 10 additions & 0 deletions src/msvc_core.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// https://docs.microsoft.com/en-us/cpp/cpp/modules-cpp?view=msvc-170#consume-the-c-standard-library-as-modules

import std;

int main(){

std::cout << "hello" << std::endl;
return 0;

}

0 comments on commit 067279b

Please sign in to comment.