diff --git a/CMakeLists.txt b/CMakeLists.txt index b7bdf86a..fea20eb3 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -4,6 +4,12 @@ project(tinyxml2 VERSION 11.0.0) include(CTest) option(tinyxml2_BUILD_TESTING "Build tests for tinyxml2" "${BUILD_TESTING}") +if (CMAKE_VERSION VERSION_GREATER_EQUAL 3.28) + option(tinyxml2_BUILD_MODULE "Build C++ module for tinyxml2" OFF) +else () + set(tinyxml2_BUILD_MODULE OFF) +endif () + ## ## Honor tinyxml2_SHARED_LIBS to match install interface ## @@ -55,6 +61,30 @@ if (tinyxml2_BUILD_TESTING) set_tests_properties(xmltest PROPERTIES PASS_REGULAR_EXPRESSION ", Fail 0") endif () +## +## C++ Module build +## + +if (tinyxml2_BUILD_MODULE AND CMAKE_VERSION VERSION_GREATER_EQUAL 3.28) + + add_library(tinyxml2_module) + target_sources(tinyxml2_module + PUBLIC + FILE_SET CXX_MODULES FILES + tinyxml2.cppm + ) + target_link_libraries(tinyxml2_module PUBLIC tinyxml2::tinyxml2) + target_compile_features(tinyxml2_module PUBLIC cxx_std_20) + add_library(tinyxml2::tinyxml2_module ALIAS tinyxml2_module) + + set_target_properties( + tinyxml2_module + PROPERTIES + VERSION "${tinyxml2_VERSION}" + SOVERSION "${tinyxml2_VERSION_MAJOR}" + ) +endif () + ## ## Installation ## @@ -83,6 +113,17 @@ install( INCLUDES DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}" ) +if (tinyxml2_BUILD_MODULE) + install( + TARGETS tinyxml2_module EXPORT tinyxml2-targets + RUNTIME COMPONENT tinyxml2_runtime + LIBRARY COMPONENT tinyxml2_runtime + NAMELINK_COMPONENT tinyxml2_development + ARCHIVE COMPONENT tinyxml2_development + FILE_SET CXX_MODULES DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}" + ) +endif () + # Type-specific targets if (BUILD_SHARED_LIBS) diff --git a/readme.md b/readme.md index eb8bf167..c1b57765 100644 --- a/readme.md +++ b/readme.md @@ -282,6 +282,9 @@ There are 2 files in TinyXML-2: And additionally a test file: * `xmltest.cpp` +And additionally a module: +* `tinyxml2.cppm` + Generally speaking, the intent is that you simply include the `tinyxml2.cpp` and `tinyxml2.h` files in your project and build with your other source code. @@ -290,6 +293,9 @@ There is also a CMake build included. CMake is the general build for TinyXML-2. (Additional build systems are costly to maintain, and tend to become outdated. They are being removed over time.) +If using C++20 or later and CMake 3.28+, the option `tinyxml2_BUILD_MODULE` may be enabled, +which enables building the `tinyxml2` module. + ### Building TinyXML-2 - Using vcpkg You can download and install TinyXML-2 using the [vcpkg](https://github.com/Microsoft/vcpkg) dependency manager: diff --git a/tinyxml2.cppm b/tinyxml2.cppm new file mode 100644 index 00000000..938c477f --- /dev/null +++ b/tinyxml2.cppm @@ -0,0 +1,53 @@ +/* +Original code by Lee Thomason (www.grinninglizard.com) + +This software is provided 'as-is', without any express or implied +warranty. In no event will the authors be held liable for any +damages arising from the use of this software. + +Permission is granted to anyone to use this software for any +purpose, including commercial applications, and to alter it and +redistribute it freely, subject to the following restrictions: + +1. The origin of this software must not be misrepresented; you must +not claim that you wrote the original software. If you use this +software in a product, an acknowledgment in the product documentation +would be appreciated but is not required. + +2. Altered source versions must be plainly marked as such, and +must not be misrepresented as being the original software. + +3. This notice may not be removed or altered from any source +distribution. +*/ + +module; + +#include "tinyxml2.h" + +export module tinyxml2; + +export namespace tinyxml2 { + inline constexpr int MAX_ELEMENT_DEPTH = TINYXML2_MAX_ELEMENT_DEPTH; + + using tinyxml2::XMLAttribute; + using tinyxml2::XMLComment; + using tinyxml2::XMLConstHandle; + using tinyxml2::XMLDeclaration; + using tinyxml2::XMLDocument; + using tinyxml2::XMLElement; + using tinyxml2::XMLError; + using tinyxml2::XMLHandle; + using tinyxml2::XMLNode; + using tinyxml2::XMLPrinter; + using tinyxml2::XMLText; + using tinyxml2::XMLUnknown; + using tinyxml2::XMLUtil; + using tinyxml2::XMLVisitor; + + using tinyxml2::DynArray; + using tinyxml2::MemPool; + using tinyxml2::MemPoolT; + using tinyxml2::StrPair; + using tinyxml2::Whitespace; +} // namespace tinyxml2