Skip to content

Add support for C++ modules #513

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
2 changes: 2 additions & 0 deletions docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
30 changes: 30 additions & 0 deletions src/modules/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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)
30 changes: 30 additions & 0 deletions src/modules/Column.cppm
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/**
* @file Column.cppm
* @brief Module file for SQLiteCpp Column class.
*/

module;

#include <SQLiteCpp/Column.h>

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;
40 changes: 40 additions & 0 deletions src/modules/Database.cppm
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/**
* @file Exception.cppm
* @brief Module file for SQLiteCpp Exception class.
*/

module;

#include <SQLiteCpp/Database.h>

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;
22 changes: 22 additions & 0 deletions src/modules/Exception.cppm
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/**
* @file Exception.cppm
* @brief Module file for SQLiteCpp Exception class.
*/

module;

#include <SQLiteCpp/Exception.h>

export module sqlite.exception;

import sqlite.sqlite3forward;

/**
* @namespace SQLite
* @brief The SQLite SQLite:: namespace
*/
export namespace SQLite {
using SQLite::Exception;
}

export namespace sqlite = SQLite;
12 changes: 12 additions & 0 deletions src/modules/SQLiteCpp.cppm
Original file line number Diff line number Diff line change
@@ -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;
25 changes: 25 additions & 0 deletions src/modules/Statement.cppm
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/**
* @file Statement.cppm
* @brief Module file for SQLiteCpp Statement class.
*/

module;

#include <SQLiteCpp/Statement.h>

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;
25 changes: 25 additions & 0 deletions src/modules/Transaction.cppm
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/**
* @file Transaction.cppm
* @brief Module file for SQLiteCpp Transaction class.
*/

module;

#include <SQLiteCpp/Transaction.h>

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;
35 changes: 35 additions & 0 deletions src/modules/sqlite3forward.cppm
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/**
* @file sqlite3forward.cppm
* @brief Module file containing forward declarations of sqlite3 structs.
*/

module;

#include <sqlite3.h>

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;
}