diff --git a/CMakeLists.txt b/CMakeLists.txt index ab2034db..2f444359 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -153,6 +153,20 @@ set(SQLITECPP_INC ) source_group(include FILES ${SQLITECPP_INC}) +# modules +option(SQLITECPP_BUILD_MODULES "Build C++ modules support" OFF) + +if(SQLITECPP_BUILD_MODULES) + if(CMAKE_VERSION VERSION_GREATER_EQUAL 3.28) + message(STATUS "Building SQLiteCpp C++ modules (CMake ${CMAKE_VERSION} supports modules)") + add_subdirectory(src/modules) + else() + message(WARNING "Skipping SQLiteCpp C++ modules (requires CMake 3.28+, found ${CMAKE_VERSION})") + endif() +else() + message(STATUS "SQLiteCpp C++ modules support is disabled. Enable with -DSQLITECPP_BUILD_MODULES=ON") +endif() + # list of test files of the library set(SQLITECPP_TESTS tests/Column_test.cpp diff --git a/README.md b/README.md index b66fc912..c0224774 100644 --- a/README.md +++ b/README.md @@ -192,6 +192,8 @@ cmake --build . ctest --output-on-failure ``` +To enable building with C++20 modules (and importing the library with `import sqlite;`), pass `SQLITECPP_BUILD_MODULES` to the build system. + #### Building with meson You can build SQLiteCpp with [meson](https://mesonbuild.com/) using the provided meson project. diff --git a/docs/README.md b/docs/README.md index 9bf9f0b3..ee6de593 100644 --- a/docs/README.md +++ b/docs/README.md @@ -193,6 +193,8 @@ cmake --build . ctest --output-on-failure ``` +To enable building with C++20 modules (and importing the library with `import sqlite;`), pass `SQLITECPP_BUILD_MODULES` to the build system. + #### CMake options * For more options on customizing the build, see the [CMakeLists.txt](https://github.com/SRombauts/SQLiteCpp/blob/master/CMakeLists.txt) file. diff --git a/src/modules/CMakeLists.txt b/src/modules/CMakeLists.txt new file mode 100644 index 00000000..b87e9089 --- /dev/null +++ b/src/modules/CMakeLists.txt @@ -0,0 +1,30 @@ +file(GLOB_RECURSE SQLITECPP_MODULES *.cppm) + +add_library(sqlitecpp_modules) + +cmake_minimum_required(VERSION 3.28) + +if(NOT COMMAND configure_cpp_module_target) + function(configure_cpp_module_target target) + if(CMAKE_VERSION VERSION_GREATER_EQUAL 3.28) + target_sources(${target} PUBLIC FILE_SET CXX_MODULES FILES ${SQLITECPP_MODULES}) + else() + message(WARNING "C++ modules require CMake 3.28+. Using standard compilation.") + target_sources(${target} PRIVATE ${SQLITECPP_MODULES}) + endif() + endfunction() +endif() + +configure_cpp_module_target(sqlitecpp_modules) + +target_link_libraries(sqlitecpp_modules + PUBLIC + sqlitecpp +) + +target_include_directories(sqlitecpp_modules + PRIVATE + ${PROJECT_SOURCE_DIR}/include +) + +target_compile_features(sqlitecpp_modules PUBLIC cxx_std_20) \ No newline at end of file diff --git a/src/modules/Column.cppm b/src/modules/Column.cppm new file mode 100644 index 00000000..05edfb2e --- /dev/null +++ b/src/modules/Column.cppm @@ -0,0 +1,30 @@ +/** + * @file Column.cppm + * @brief Module file for SQLiteCpp Column class. + */ + +module; + +#include + +export module sqlite.column; + +import sqlite.sqlite3forward; + +/** + * @namespace SQLite + * @brief The SQLite SQLite:: namespace + */ +export namespace SQLite { + using SQLite::INTEGER; + using SQLite::FLOAT; + using SQLite::TEXT; + using SQLite::BLOB; + using SQLite::Null; + + using SQLite::Column; + + using SQLite::operator<<; +} + +export namespace sqlite = SQLite; diff --git a/src/modules/Database.cppm b/src/modules/Database.cppm new file mode 100644 index 00000000..366fb5cb --- /dev/null +++ b/src/modules/Database.cppm @@ -0,0 +1,40 @@ +/** + * @file Exception.cppm + * @brief Module file for SQLiteCpp Exception class. + */ + +module; + +#include + +export module sqlite.database; + +import sqlite.sqlite3forward; + +/** + * @namespace SQLite + * @brief The SQLite SQLite:: namespace + */ +export namespace SQLite { + using SQLite::OPEN_READONLY; + using SQLite::OPEN_READWRITE; + using SQLite::OPEN_CREATE; + using SQLite::OPEN_URI; + using SQLite::OPEN_MEMORY; + using SQLite::OPEN_NOMUTEX; + using SQLite::OPEN_FULLMUTEX; + using SQLite::OPEN_SHAREDCACHE; + using SQLite::OPEN_PRIVATECACHE; + using SQLite::OPEN_NOFOLLOW; + using SQLite::OK; + using SQLite::VERSION; + using SQLite::VERSION_NUMBER; + + using SQLite::getLibVersion; + using SQLite::getLibVersionNumber; + + using SQLite::Header; + using SQLite::Database; +} + +export namespace sqlite = SQLite; diff --git a/src/modules/Exception.cppm b/src/modules/Exception.cppm new file mode 100644 index 00000000..d6dfa557 --- /dev/null +++ b/src/modules/Exception.cppm @@ -0,0 +1,22 @@ +/** + * @file Exception.cppm + * @brief Module file for SQLiteCpp Exception class. + */ + +module; + +#include + +export module sqlite.exception; + +import sqlite.sqlite3forward; + +/** + * @namespace SQLite + * @brief The SQLite SQLite:: namespace + */ +export namespace SQLite { + using SQLite::Exception; +} + +export namespace sqlite = SQLite; diff --git a/src/modules/SQLiteCpp.cppm b/src/modules/SQLiteCpp.cppm new file mode 100644 index 00000000..ce0e42cb --- /dev/null +++ b/src/modules/SQLiteCpp.cppm @@ -0,0 +1,12 @@ +/** + * @file SQLiteCpp.cppm + * @brief File containing the module declaration for SQLiteC++. + */ + +export module sqlite; + +export import sqlite.column; +export import sqlite.database; +export import sqlite.exception; +export import sqlite.statement; +export import sqlite.transaction; diff --git a/src/modules/Statement.cppm b/src/modules/Statement.cppm new file mode 100644 index 00000000..c8d3393b --- /dev/null +++ b/src/modules/Statement.cppm @@ -0,0 +1,25 @@ +/** + * @file Statement.cppm + * @brief Module file for SQLiteCpp Statement class. + */ + +module; + +#include + +export module sqlite.statement; + +export import sqlite.column; +export import sqlite.database; + +import sqlite.sqlite3forward; + +/** + * @namespace SQLite + * @brief The SQLite SQLite:: namespace + */ +export namespace SQLite { + using SQLite::Statement; +} + +export namespace sqlite = SQLite; diff --git a/src/modules/Transaction.cppm b/src/modules/Transaction.cppm new file mode 100644 index 00000000..950497de --- /dev/null +++ b/src/modules/Transaction.cppm @@ -0,0 +1,25 @@ +/** + * @file Transaction.cppm + * @brief Module file for SQLiteCpp Transaction class. + */ + +module; + +#include + +export module sqlite.transaction; + +export import sqlite.database; + +import sqlite.sqlite3forward; + +/** + * @namespace SQLite + * @brief The SQLite SQLite:: namespace + */ +export namespace SQLite { + using SQLite::TransactionBehavior; + using SQLite::Transaction; +} + +export namespace sqlite = SQLite; diff --git a/src/modules/sqlite3forward.cppm b/src/modules/sqlite3forward.cppm new file mode 100644 index 00000000..6095cd26 --- /dev/null +++ b/src/modules/sqlite3forward.cppm @@ -0,0 +1,35 @@ +/** + * @file sqlite3forward.cppm + * @brief Module file containing forward declarations of sqlite3 structs. + */ + +module; + +#include + +export module sqlite.sqlite3forward; + +/** + * @namespace SQLite + * @brief The SQLite SQLite:: namespace + */ +export namespace SQLite { + using ::sqlite3; + using ::sqlite3_context; + using ::sqlite3_stmt; + using ::sqlite3_value; + + using ::sqlite3_open; + using ::sqlite3_close; + using ::sqlite3_prepare_v2; + using ::sqlite3_step; + using ::sqlite3_finalize; + using ::sqlite3_bind_text; + using ::sqlite3_bind_int; + using ::sqlite3_bind_double; + using ::sqlite3_column_text; + using ::sqlite3_column_int; + using ::sqlite3_column_double; + using ::sqlite3_errmsg; + using ::sqlite3_errcode; +}