Skip to content

Commit

Permalink
Use import std; guarded by macro (#1932)
Browse files Browse the repository at this point in the history
* Initial build and tests setup

* Enable the `CppStdModule` test

* Snippets and generator changes

* Generated files changes

* Enable std module with macro

* Unconditionally use `import std` with C++23

* Add support for external `import std` control with `VULKAN_HPP_ENABLE_STD_MODULE`

- Rewrite `includes.hpp` and `macros.hpp`
- Unconditionally set `VULKAN_HPP_ENABLE_STD_MODULE` for `vulkan.cppm` to bug-fix
- Generated necessary files again

* Top-level CMake changes

* Reorganise logic, and propagate external macro correctly

* RAII: vulkan_hpp before ifdef

* Define `VK_USE_64_BIT_PTR_DEFINES` in `vulkan_hpp_macros.hpp`

* Generate the `VK_USE_64_BIT_PTR_DEFINES` clause from the XML instead of hardcoding it

* Refactored handling for `VK_USE_64_BIT_PTR_DEFINES`

- Updated condition to positive test for `#ifndef VK_USE_64_BIT_PTR_DEFINES` in `completeMacro`

* Added `#include <string.h>` to resolve `strnlen` missing in the module

* Use only `std.compat`

* FIx after rebase
  • Loading branch information
sharadhr authored Sep 5, 2024
1 parent 7d423e6 commit 6abd3f4
Show file tree
Hide file tree
Showing 22 changed files with 286 additions and 76 deletions.
8 changes: 7 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ option( VULKAN_HPP_SAMPLES_BUILD_ONLY_DYNAMIC "Build only dynamic. Required in c
option( VULKAN_HPP_TESTS_BUILD_ONLY_DYNAMIC "Build only dynamic" OFF )
option( VULKAN_HPP_BUILD_WITH_LOCAL_VULKAN_HPP "Build with local Vulkan headers" ON )
option( VULKAN_HPP_ENABLE_CPP20_MODULES "Build Vulkan-Hpp as C++20 module; requires minimum CMake version 3.28" OFF )
option( VULKAN_HPP_ENABLE_STD_MODULE "Build Vulkan-Hpp with import std; requires minimum CMake version 3.30" OFF )
option( VULKAN_HPP_CPP20_MODULE_DYNAMIC_DISPATCHER "Build C++20 module with dynamic Dispatcher" ON )

if ( VULKAN_HPP_ENABLE_CPP20_MODULES )
Expand Down Expand Up @@ -346,7 +347,12 @@ if( VULKAN_HPP_ENABLE_CPP20_MODULES )
else()
target_compile_definitions( VulkanHppModule PUBLIC VULKAN_HPP_DISPATCH_LOADER_DYNAMIC=0 )
endif()
target_compile_features( VulkanHppModule PUBLIC cxx_std_20 )
if( VULKAN_HPP_ENABLE_STD_MODULE )
target_compile_features( VulkanHppModule PUBLIC cxx_std_23 )
set_target_properties( VulkanHppModule PROPERTIES CXX_MODULE_STD ON )
else()
target_compile_features( VulkanHppModule PUBLIC cxx_std_20 )
endif()
target_sources( VulkanHppModule
PUBLIC
FILE_SET vulkan_module_file BASE_DIRS ${CMAKE_CURRENT_SOURCE_DIR} TYPE CXX_MODULES FILES vulkan/vulkan.cppm )
Expand Down
52 changes: 40 additions & 12 deletions VulkanHppGenerator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -130,11 +130,15 @@ void VulkanHppGenerator::generateExtensionInspectionFile() const
#ifndef VULKAN_EXTENSION_INSPECTION_HPP
# define VULKAN_EXTENSION_INSPECTION_HPP

#include <map>
#include <set>
#include <string>
#include <vector>
#include <vulkan/${api}.hpp>
#if defined( VULKAN_HPP_ENABLE_STD_MODULE ) && defined( VULKAN_HPP_STD_MODULE )
import VULKAN_HPP_STD_MODULE;
#else
# include <map>
# include <set>
# include <string>
# include <vector>
# include <vulkan/${api}.hpp>
#endif

namespace VULKAN_HPP_NAMESPACE
{
Expand Down Expand Up @@ -578,7 +582,11 @@ void VulkanHppGenerator::generateMacrosFile() const

std::string str = replaceWithMap(
macrosTemplate,
{ { "licenseHeader", m_vulkanLicenseHeader }, { "macros", replaceWithMap( readSnippet( "macros.hpp" ), { { "vulkan_hpp", m_api + ".hpp" } } ) } } );
{ { "licenseHeader", m_vulkanLicenseHeader },
{ "macros",
replaceWithMap( readSnippet( "macros.hpp" ),
{ { "vulkan_hpp", m_api + ".hpp" },
{ "vulkan_64_bit_ptr_defines", m_defines.at( "VK_USE_64_BIT_PTR_DEFINES" ).possibleDefinition } } ) } } );

writeToFile( str, macros_hpp );
}
Expand All @@ -592,9 +600,11 @@ void VulkanHppGenerator::generateRAIIHppFile() const
#ifndef VULKAN_RAII_HPP
#define VULKAN_RAII_HPP

#include <memory> // std::unique_ptr
#include <utility> // std::forward
#include <vulkan/${api}.hpp>
#if !( defined( VULKAN_HPP_ENABLE_STD_MODULE ) && defined( VULKAN_HPP_STD_MODULE ) )
# include <memory> // std::unique_ptr
# include <utility> // std::forward
#endif

#if !defined( VULKAN_HPP_DISABLE_ENHANCED_MODE )
namespace VULKAN_HPP_NAMESPACE
Expand Down Expand Up @@ -668,8 +678,10 @@ void VulkanHppGenerator::generateSharedHppFile() const
#define VULKAN_SHARED_HPP

#include <vulkan/${api}.hpp>
#include <atomic> // std::atomic_size_t

#if !( defined( VULKAN_HPP_ENABLE_STD_MODULE ) && defined( VULKAN_HPP_STD_MODULE ) )
#include <atomic> // std::atomic_size_t
#endif

namespace VULKAN_HPP_NAMESPACE
{
Expand Down Expand Up @@ -769,10 +781,14 @@ void VulkanHppGenerator::generateToStringHppFile() const
# pragma warning( disable : 4996 )
#endif

#if __cpp_lib_format
# include <format> // std::format
#if defined( VULKAN_HPP_ENABLE_STD_MODULE ) && defined( VULKAN_HPP_STD_MODULE )
import VULKAN_HPP_STD_MODULE;
#else
# include <sstream> // std::stringstream
# if __cpp_lib_format
# include <format> // std::format
# else
# include <sstream> // std::stringstream
# endif
#endif

namespace VULKAN_HPP_NAMESPACE
Expand Down Expand Up @@ -811,6 +827,12 @@ void VulkanHppGenerator::generateCppModuleFile() const

module;

#include <vulkan/vulkan_hpp_macros.hpp>

#if defined( __cpp_lib_modules )
#define VULKAN_HPP_ENABLE_STD_MODULE
#endif

#include <vulkan/${api}.hpp>
#include <vulkan/${api}_extension_inspection.hpp>
#include <vulkan/${api}_format_traits.hpp>
Expand Down Expand Up @@ -16417,6 +16439,12 @@ namespace
std::erase( rawComment, '/' );
auto const strippedComment = trim( stripPostfix( stripPrefix( rawComment, " DEPRECATED:" ), "#define " ) );

// special case for VK_USE_64_BIT_PTR_DEFINES
if ( completeMacro.size() == 1 && completeMacro.front().find( "#ifndef VK_USE_64_BIT_PTR_DEFINES" ) != std::string::npos )
{
return { {}, {}, {}, strippedComment };
}

// macro with parameters and implementation
if ( completeMacro.size() == 3 )
{
Expand Down
28 changes: 17 additions & 11 deletions snippets/includes.hpp
Original file line number Diff line number Diff line change
@@ -1,21 +1,27 @@
#include <algorithm>
#include <array> // ArrayWrapperND
#include <string.h> // strnlen
#include <string> // std::string
#include <utility> // std::exchange
#include <vulkan/${vulkan_h}>
#include <vulkan/vulkan_hpp_macros.hpp>

#if 17 <= VULKAN_HPP_CPP_VERSION
#if defined( VULKAN_HPP_ENABLE_STD_MODULE ) && defined( VULKAN_HPP_STD_MODULE )
#include <string.h>
import VULKAN_HPP_STD_MODULE;
#else
# include <algorithm>
# include <array> // ArrayWrapperND
# include <string.h> // strnlen
# include <string> // std::string
# include <utility> // std::exchange
#endif
#include <vulkan/${vulkan_h}>

#if 17 <= VULKAN_HPP_CPP_VERSION && !( defined( VULKAN_HPP_ENABLE_STD_MODULE ) && defined( VULKAN_HPP_STD_MODULE ) )
# include <string_view>
#endif

#if !defined( VULKAN_HPP_DISABLE_ENHANCED_MODE )
#if !defined( VULKAN_HPP_DISABLE_ENHANCED_MODE ) && !( defined( VULKAN_HPP_ENABLE_STD_MODULE ) && defined( VULKAN_HPP_STD_MODULE ) )
# include <tuple> // std::tie
# include <vector> // std::vector
#endif

#if !defined( VULKAN_HPP_NO_EXCEPTIONS )
#if !defined( VULKAN_HPP_NO_EXCEPTIONS ) && !( defined( VULKAN_HPP_ENABLE_STD_MODULE ) && defined( VULKAN_HPP_STD_MODULE ) )
# include <system_error> // std::is_error_code_enum
#endif

Expand All @@ -39,10 +45,10 @@ extern "C" __declspec( dllimport ) FARPROC __stdcall GetProcAddress( HINSTANCE h
# endif
#endif

#if defined( VULKAN_HPP_HAS_SPACESHIP_OPERATOR )
#if defined( VULKAN_HPP_HAS_SPACESHIP_OPERATOR ) && !( defined( VULKAN_HPP_ENABLE_STD_MODULE ) && defined( VULKAN_HPP_STD_MODULE ) )
# include <compare>
#endif

#if defined( VULKAN_HPP_SUPPORT_SPAN )
#if defined( VULKAN_HPP_SUPPORT_SPAN ) && !( defined( VULKAN_HPP_ENABLE_STD_MODULE ) && defined( VULKAN_HPP_STD_MODULE ) )
# include <span>
#endif
11 changes: 9 additions & 2 deletions snippets/macros.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,12 @@
# define VULKAN_HPP_SUPPORT_SPAN
#endif

#if defined( __cpp_lib_modules ) && !defined( VULKAN_HPP_STD_MODULE ) && defined( VULKAN_HPP_ENABLE_STD_MODULE )
# define VULKAN_HPP_STD_MODULE std.compat
#endif

${vulkan_64_bit_ptr_defines}

// 32-bit vulkan is not typesafe for non-dispatchable handles, so don't allow copy constructors on this platform by default.
// To enable this feature on 32-bit platforms please #define VULKAN_HPP_TYPESAFE_CONVERSION 1
// To disable this feature on 64-bit platforms please #define VULKAN_HPP_TYPESAFE_CONVERSION 0
Expand All @@ -86,7 +92,6 @@
# endif
#endif


#if defined( __GNUC__ )
# define GCC_VERSION ( __GNUC__ * 10000 + __GNUC_MINOR__ * 100 + __GNUC_PATCHLEVEL__ )
#endif
Expand Down Expand Up @@ -276,7 +281,9 @@ namespace VULKAN_HPP_NAMESPACE
#endif

#if !defined( VULKAN_HPP_EXPECTED ) && ( 23 <= VULKAN_HPP_CPP_VERSION ) && defined( __cpp_lib_expected )
# include <expected>
# if !( defined( VULKAN_HPP_ENABLE_STD_MODULE ) && defined( VULKAN_HPP_STD_MODULE ) )
# include <expected>
# endif
# define VULKAN_HPP_EXPECTED std::expected
# define VULKAN_HPP_UNEXPECTED std::unexpected
#endif
Expand Down
3 changes: 3 additions & 0 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@
add_subdirectory( ArrayProxy )
add_subdirectory( ArrayProxyNoTemporaries )
add_subdirectory( ArrayWrapper )
if( VULKAN_HPP_ENABLE_STD_MODULE )
add_subdirectory( CppStdModule )
endif()
if( VULKAN_HPP_ENABLE_CPP20_MODULES )
add_subdirectory( Cpp20Modules )
endif()
Expand Down
5 changes: 2 additions & 3 deletions tests/Cpp20Modules/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
cmake_minimum_required( VERSION 3.25 )
cmake_minimum_required( VERSION 3.28 )

vulkan_hpp__setup_test( NAME Cpp20Modules CXX_STANDARD 20 LIBRARIES VulkanHppModule NO_UTILS )

Expand All @@ -9,6 +9,5 @@ if( NOT VULKAN_HPP_SAMPLES_BUILD_ONLY_DYNAMIC )
target_compile_definitions( Cpp20Modules PUBLIC VULKAN_HPP_DISPATCH_LOADER_DYNAMIC=0 )
endif()
target_link_libraries( Cpp20Modules PRIVATE Vulkan::Vulkan )
set_target_properties( Cpp20Modules PROPERTIES CXX_EXTENSIONS OFF
)
set_target_properties( Cpp20Modules PROPERTIES CXX_EXTENSIONS OFF )
endif()
18 changes: 18 additions & 0 deletions tests/CppStdModule/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
cmake_minimum_required( VERSION 3.30 )

target_compile_features( VulkanHppModule PUBLIC cxx_std_23 )
target_compile_definitions( VulkanHppModule PUBLIC VULKAN_HPP_ENABLE_STD_MODULE )

vulkan_hpp__setup_test( NAME CppStdModule CXX_STANDARD 23 LIBRARIES VulkanHppModule NO_UTILS )

target_compile_features( CppStdModule PUBLIC cxx_std_23 )

if( NOT VULKAN_HPP_SAMPLES_BUILD_ONLY_DYNAMIC )
if ( VULKAN_HPP_CPP20_MODULE_DYNAMIC_DISPATCHER )
target_compile_definitions( CppStdModule PUBLIC VULKAN_HPP_DISPATCH_LOADER_DYNAMIC=1 )
else()
target_compile_definitions( CppStdModule PUBLIC VULKAN_HPP_DISPATCH_LOADER_DYNAMIC=0 )
endif()
target_link_libraries( CppStdModule PRIVATE Vulkan::Vulkan )
set_target_properties( CppStdModule PROPERTIES CXX_EXTENSIONS OFF )
endif()
59 changes: 59 additions & 0 deletions tests/CppStdModule/CppStdModule.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import std;
import vulkan_hpp;

#include <vulkan/vulkan_hpp_macros.hpp>

static std::string AppName = "CppStdModule";
static std::string EngineName = "Vulkan.cppm";

#if VULKAN_HPP_DISPATCH_LOADER_DYNAMIC == 1
VULKAN_HPP_DEFAULT_DISPATCH_LOADER_DYNAMIC_STORAGE
#endif

int main(int /*argc*/, char** /*argv*/)
{
/* VULKAN_HPP_KEY_START */
try
{
#if ( VULKAN_HPP_DISPATCH_LOADER_DYNAMIC == 1 )
// initialize minimal set of function pointers
VULKAN_HPP_DEFAULT_DISPATCHER.init();
#endif

// initialize the vk::ApplicationInfo structure
vk::ApplicationInfo applicationInfo(AppName.c_str(), 1, EngineName.c_str(), 1, vk::makeApiVersion(1, 0, 0, 0));

// initialize the vk::InstanceCreateInfo
vk::InstanceCreateInfo instanceCreateInfo({}, &applicationInfo);

// create an Instance
vk::Instance instance = vk::createInstance(instanceCreateInfo);

#if ( VULKAN_HPP_DISPATCH_LOADER_DYNAMIC == 1 )
// initialize function pointers for instance
VULKAN_HPP_DEFAULT_DISPATCHER.init(instance);
#endif

// destroy it again
instance.destroy();
}
catch (vk::SystemError& err)
{
std::cout << "vk::SystemError: " << err.what() << std::endl;
exit(-1);
}
catch (std::exception& err)
{
std::cout << "std::exception: " << err.what() << std::endl;
exit(-1);
}
catch (...)
{
std::cout << "unknown error\n";
exit(-1);
}

/* VULKAN_HPP_KEY_END */

return 0;
}
7 changes: 7 additions & 0 deletions vulkan/vulkan.cppm
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@

module;

#include <vulkan/vulkan_hpp_macros.hpp>

#if defined( __cpp_lib_modules )
# define VULKAN_HPP_ENABLE_STD_MODULE
#endif

#include <vulkan/vulkan.hpp>
#include <vulkan/vulkan_extension_inspection.hpp>
#include <vulkan/vulkan_format_traits.hpp>
Expand Down Expand Up @@ -2580,6 +2586,7 @@ export namespace VULKAN_HPP_NAMESPACE
//=== CONSTEXPR VALUEs ===
//========================
using VULKAN_HPP_NAMESPACE::HeaderVersion;
using VULKAN_HPP_NAMESPACE::Use64BitPtrDefines;

//=========================
//=== CONSTEXPR CALLEEs ===
Expand Down
31 changes: 19 additions & 12 deletions vulkan/vulkan.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,24 +8,30 @@
#ifndef VULKAN_HPP
#define VULKAN_HPP

#include <algorithm>
#include <array> // ArrayWrapperND
#include <string.h> // strnlen
#include <string> // std::string
#include <utility> // std::exchange
#include <vulkan/vulkan.h>
#include <vulkan/vulkan_hpp_macros.hpp>

#if 17 <= VULKAN_HPP_CPP_VERSION
#if defined( VULKAN_HPP_ENABLE_STD_MODULE ) && defined( VULKAN_HPP_STD_MODULE )
# include <string.h>
import VULKAN_HPP_STD_MODULE;
#else
# include <algorithm>
# include <array> // ArrayWrapperND
# include <string.h> // strnlen
# include <string> // std::string
# include <utility> // std::exchange
#endif
#include <vulkan/vulkan.h>

#if 17 <= VULKAN_HPP_CPP_VERSION && !( defined( VULKAN_HPP_ENABLE_STD_MODULE ) && defined( VULKAN_HPP_STD_MODULE ) )
# include <string_view>
#endif

#if !defined( VULKAN_HPP_DISABLE_ENHANCED_MODE )
#if !defined( VULKAN_HPP_DISABLE_ENHANCED_MODE ) && !( defined( VULKAN_HPP_ENABLE_STD_MODULE ) && defined( VULKAN_HPP_STD_MODULE ) )
# include <tuple> // std::tie
# include <vector> // std::vector
#endif

#if !defined( VULKAN_HPP_NO_EXCEPTIONS )
#if !defined( VULKAN_HPP_NO_EXCEPTIONS ) && !( defined( VULKAN_HPP_ENABLE_STD_MODULE ) && defined( VULKAN_HPP_STD_MODULE ) )
# include <system_error> // std::is_error_code_enum
#endif

Expand All @@ -49,11 +55,11 @@ extern "C" __declspec( dllimport ) FARPROC __stdcall GetProcAddress( HINSTANCE h
# endif
#endif

#if defined( VULKAN_HPP_HAS_SPACESHIP_OPERATOR )
#if defined( VULKAN_HPP_HAS_SPACESHIP_OPERATOR ) && !( defined( VULKAN_HPP_ENABLE_STD_MODULE ) && defined( VULKAN_HPP_STD_MODULE ) )
# include <compare>
#endif

#if defined( VULKAN_HPP_SUPPORT_SPAN )
#if defined( VULKAN_HPP_SUPPORT_SPAN ) && !( defined( VULKAN_HPP_ENABLE_STD_MODULE ) && defined( VULKAN_HPP_STD_MODULE ) )
# include <span>
#endif

Expand Down Expand Up @@ -6875,7 +6881,8 @@ namespace VULKAN_HPP_NAMESPACE
//========================
//=== CONSTEXPR VALUEs ===
//========================
VULKAN_HPP_CONSTEXPR_INLINE uint32_t HeaderVersion = VK_HEADER_VERSION;
VULKAN_HPP_CONSTEXPR_INLINE uint32_t HeaderVersion = VK_HEADER_VERSION;
VULKAN_HPP_CONSTEXPR_INLINE uint32_t Use64BitPtrDefines = VK_USE_64_BIT_PTR_DEFINES;

//=========================
//=== CONSTEXPR CALLEEs ===
Expand Down
Loading

0 comments on commit 6abd3f4

Please sign in to comment.