Skip to content

Commit f522d73

Browse files
authored
C++Module: Fixing import std on enum header and using export import std when appropriate (KhronosGroup#2283)
* fix std import on enum header * remove duplicate contents in cppm * moved dynamic enabling of std module into macro header * do not automatically enable import std * automatic opt-out approach for export import std * added opt-out option VULKAN_HPP_NO_STD_MODULE * add VULKAN_HPP_NO_STD_MODULE to CI workflows * added VULKAN_HPP_NO_STD_MODULE flag to the readme * set VULKAN_HPP_NO_STD_MODULE to OFF by default * added import_std exclusion to ubuntu-ext CI
1 parent 4059621 commit f522d73

File tree

13 files changed

+38
-17
lines changed

13 files changed

+38
-17
lines changed

.github/workflows/ci-macos.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@ jobs:
112112
-D VULKAN_HPP_DISPATCH_LOADER_DYNAMIC=ON \
113113
-D VULKAN_HPP_BUILD_WITH_LOCAL_VULKAN_HPP=ON \
114114
-D VULKAN_HPP_PRECOMPILE=OFF \
115+
-D VULKAN_HPP_NO_STD_MODULE=ON \
115116
-D CMAKE_CXX_COMPILER=${{matrix.compiler}} \
116117
-D CMAKE_CXX_STANDARD=$CXX_STANDARD \
117118
-D CMAKE_BUILD_TYPE=$BUILD_TYPE

.github/workflows/ci-ubuntu-22.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,7 @@ jobs:
120120
-D VULKAN_HPP_DISPATCH_LOADER_DYNAMIC=ON \
121121
-D VULKAN_HPP_BUILD_WITH_LOCAL_VULKAN_HPP=ON \
122122
-D VULKAN_HPP_PRECOMPILE=OFF \
123+
-D VULKAN_HPP_NO_STD_MODULE=ON \
123124
-D CMAKE_CXX_COMPILER=${{matrix.compiler}} \
124125
-D CMAKE_CXX_STANDARD=$CXX_STANDARD \
125126
-D CMAKE_BUILD_TYPE=$BUILD_TYPE

.github/workflows/ci-ubuntu-24.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,7 @@ jobs:
125125
-D VULKAN_HPP_DISPATCH_LOADER_DYNAMIC=ON \
126126
-D VULKAN_HPP_BUILD_WITH_LOCAL_VULKAN_HPP=ON \
127127
-D VULKAN_HPP_PRECOMPILE=OFF \
128+
-D VULKAN_HPP_NO_STD_MODULE=ON \
128129
-D CMAKE_CXX_COMPILER=${{matrix.compiler}} \
129130
-D CMAKE_CXX_STANDARD=$CXX_STANDARD \
130131
-D CMAKE_BUILD_TYPE=$BUILD_TYPE

.github/workflows/ci-ubuntu-ext.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,7 @@ jobs:
131131
-D VULKAN_HPP_DISPATCH_LOADER_DYNAMIC=ON \
132132
-D VULKAN_HPP_BUILD_WITH_LOCAL_VULKAN_HPP=ON \
133133
-D VULKAN_HPP_PRECOMPILE=OFF \
134+
-D VULKAN_HPP_NO_STD_MODULE=ON \
134135
-D CMAKE_CXX_COMPILER=clang++-${{matrix.clang_version}} \
135136
-D CMAKE_CXX_STANDARD=$CXX_STANDARD \
136137
-D CMAKE_BUILD_TYPE=$BUILD_TYPE

.github/workflows/ci-windows.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@ jobs:
112112
-D VULKAN_HPP_DISPATCH_LOADER_DYNAMIC=ON \
113113
-D VULKAN_HPP_BUILD_WITH_LOCAL_VULKAN_HPP=ON \
114114
-D VULKAN_HPP_PRECOMPILE=OFF \
115+
-D VULKAN_HPP_NO_STD_MODULE=ON \
115116
-D CMAKE_CXX_STANDARD=$CXX_STANDARD \
116117
-D CMAKE_BUILD_TYPE=$BUILD_TYPE
117118
cmake --build build --parallel --clean-first

CMakeLists.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ option( VULKAN_HPP_NO_NODISCARD_WARNINGS "Remove C++17 [[nodiscard]] from functi
5656
option( VULKAN_HPP_NO_SETTERS "Remove setters for structs and unions" OFF )
5757
option( VULKAN_HPP_NO_SMART_HANDLE "Remove the vk::UniqueHandle helper class" OFF )
5858
option( VULKAN_HPP_NO_SPACESHIP_OPERATOR "Remove the C++20 <=> (spaceship) operator" OFF )
59+
option( VULKAN_HPP_NO_STD_MODULE "Prevent the automatic use of import std in the C++20 module when compiling with C++23" OFF )
5960
option( VULKAN_HPP_NO_TO_STRING "Remove inclusion of vulkan_to_string.hpp from vulkan.hpp" OFF )
6061
option( VULKAN_HPP_NO_WIN32_PROTOTYPES "Remove HINSTANCE, LoadLibraryA and other symbols, which are declared if VULKAN_HPP_ENABLE_DYNAMIC_LOADER_TOOL is enabled on Win32" OFF )
6162
option( VULKAN_HPP_RAII_NO_EXCEPTIONS "Do not throw exceptions when vulkan functions from vk::raii return an error code." OFF )
@@ -278,6 +279,9 @@ endif()
278279
if( VULKAN_HPP_NO_SPACESHIP_OPERATOR )
279280
target_compile_definitions(VulkanHpp INTERFACE "VULKAN_HPP_NO_SPACESHIP_OPERATOR" )
280281
endif()
282+
if( VULKAN_HPP_NO_STD_MODULE )
283+
target_compile_definitions(VulkanHpp INTERFACE "VULKAN_HPP_NO_STD_MODULE" )
284+
endif()
281285

282286
function( vulkan_hpp__setup_vulkan_include )
283287
set( options )

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ The goal of the Vulkan-Hpp is to provide header only C++ bindings for the Vulkan
7474
- [VULKAN_HPP_NO_SETTERS](#no_setters)
7575
- [VULKAN_HPP_NO_SMART_HANDLE](#no_smart_handle)
7676
- [VULKAN_HPP_NO_SPACESHIP_OPERATOR](#no_spaceship_operator)
77+
- [VULKAN_HPP_NO_STD_MODULE](#no_std_module)
7778
- [VULKAN_HPP_NO_TO_STRING](#no_to_string)
7879
- [VULKAN_HPP_NO_WIN32_PROTOTYPES](#no_win32_prototypes)
7980
- [VULKAN_HPP_RAII_NO_EXCEPTIONS](#raii_no_exceptions)
@@ -1053,6 +1054,11 @@ By defining `VULKAN_HPP_NO_SMART_HANDLE` before including `vulkan.hpp`, the help
10531054

10541055
With C++20, the so-called spaceship-operator `<=>` is introduced. If that operator is supported, all the structs and classes in vulkan.hpp use the default implementation of it. As currently some implementations of this operator are very slow, and others seem to be incomplete, by defining `VULKAN_HPP_NO_SPACESHIP_OPERATOR` before including `vulkan.hpp` you can remove that operator from those structs and classes.
10551056

1057+
#### VULKAN_HPP_NO_STD_MODULE <a id='no_std_module'>
1058+
1059+
When compiling the `vulkan.cppm` module with C++23, it will automatically try to use `import std`.
1060+
There might be cases where your toolchain is not set up to deal with `import std` or wrongfully defines `__cpp_lib_modules`, for which you can define `VULKAN_HPP_NO_STD_MODULE` to disable `import std` altogether.
1061+
10561062
#### VULKAN_HPP_NO_TO_STRING <a id='no_to_string'>
10571063

10581064
By default, the file [`vulkan_to_string.hpp`](vulkan/vulkan_to_string.hpp) is included by `vulkan.hpp` and provides functions `vk::to_string` for enums and bitmasks. If you don't need those functions, you can define `VULKAN_HPP_NO_TO_STRING` to prevent that inclusion. If you have certain files where you want to use those functions nevertheless, you can explicitly include `vulkan_to_string.hpp` there.

snippets/CppmTemplate.hpp

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,9 @@
55

66
module;
77

8-
#include <vulkan/vulkan_hpp_macros.hpp>
9-
10-
#if defined( __cpp_lib_modules ) && !defined( VULKAN_HPP_ENABLE_STD_MODULE )
11-
#define VULKAN_HPP_ENABLE_STD_MODULE
8+
#include <version>
9+
#if defined( __cpp_lib_modules ) && !defined( VULKAN_HPP_NO_STD_MODULE )
10+
# define VULKAN_HPP_ENABLE_STD_MODULE
1211
#endif
1312

1413
#include <vulkan/${api}.hpp>
@@ -17,11 +16,11 @@ module;
1716
#include <vulkan/${api}_hash.hpp>
1817
#include <vulkan/${api}_raii.hpp>
1918
#include <vulkan/${api}_shared.hpp>
20-
#ifndef VULKAN_HPP_NO_TO_STRING
21-
#include <vulkan/${api}_to_string.hpp>
22-
#endif
2319

2420
export module ${api}_hpp;
21+
#if defined( VULKAN_HPP_ENABLE_STD_MODULE )
22+
export import VULKAN_HPP_STD_MODULE;
23+
#endif
2524

2625
export namespace VULKAN_HPP_NAMESPACE
2726
{

snippets/EnumsHppTemplate.hpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,11 @@
66
// include-what-you-use: make sure, vulkan.hpp is used by code-completers
77
// IWYU pragma: private, include "vulkan/vulkan.hpp"
88

9-
#include <type_traits> // for std::underlying_type
9+
#if defined( VULKAN_HPP_ENABLE_STD_MODULE ) && defined( VULKAN_HPP_STD_MODULE )
10+
import VULKAN_HPP_STD_MODULE;
11+
#else
12+
# include <type_traits> // for std::underlying_type
13+
#endif
1014

1115
namespace VULKAN_HPP_NAMESPACE
1216
{

snippets/MacrosHppTemplate.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@
8282
# define VULKAN_HPP_SUPPORT_SPAN
8383
#endif
8484

85-
#if defined( __cpp_lib_modules ) && !defined( VULKAN_HPP_STD_MODULE ) && defined( VULKAN_HPP_ENABLE_STD_MODULE )
85+
#if !defined( VULKAN_HPP_STD_MODULE ) && defined( VULKAN_HPP_ENABLE_STD_MODULE )
8686
# define VULKAN_HPP_STD_MODULE std.compat
8787
#endif
8888

0 commit comments

Comments
 (0)