Skip to content

Commit d620195

Browse files
committed
2 parents 3059e4c + dbc9151 commit d620195

File tree

4 files changed

+95
-37
lines changed

4 files changed

+95
-37
lines changed

CMakeLists.txt

Lines changed: 74 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,31 @@
11
cmake_minimum_required(VERSION 3.0)
2-
project(qsqlcipher)
2+
project(libqt5sql5-sqlcipher)
33

44
set(CMAKE_MODULE_PATH "${CMAKE_MODULE_PATH};${PROJECT_SOURCE_DIR}/cmake/")
5-
SET(CMAKE_PREFIX_PATH "D:/Qt/5.9.2/msvc2017_64/lib/cmake/")
5+
6+
if(CMAKE_SIZEOF_VOID_P EQUAL 8)
7+
SET(CMAKE_PREFIX_PATH "C:/Qt/5.9.2/msvc2017_64/lib/cmake/")
8+
else()
9+
SET(CMAKE_PREFIX_PATH "C:/Qtx86/5.9.2/msvc2015/lib/cmake/")
10+
endif()
611

712
find_package(Qt5Sql REQUIRED)
8-
find_package(PkgConfig REQUIRED)
913

1014
if (NOT WIN32)
15+
find_package(PkgConfig REQUIRED)
1116
pkg_check_modules(SQLCIPHER REQUIRED sqlcipher)
1217
else()
18+
if(CMAKE_SIZEOF_VOID_P EQUAL 8)
19+
set(_lib_suffix_win "x64")
20+
else()
21+
set(_lib_suffix_win "x86")
22+
endif()
23+
24+
# Change this by hand if needed
25+
set(Libsqlcipher_INCLUDE_DIRS "C:/CppProjects/sqlcipher/include")
26+
# Change this by hand if needed
27+
set(Libsqlcipher_LIBRARIES "optimized;C:/CppProjects/sqlcipher/compile/${_lib_suffix_win}/Release/sqlite3.lib;debug;C:/CppProjects/sqlcipher/compile/${_lib_suffix_win}/Debug/sqlite3.lib")
28+
1329
find_package(Libsqlcipher REQUIRED)
1430
endif()
1531

@@ -19,9 +35,16 @@ set(CMAKE_AUTOMOC ON)
1935
# Arrange output paths so that the plugin is found in the default search path
2036
# relative to the test binary.
2137
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR})
22-
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/sqldrivers)
38+
if (NOT WIN32)
39+
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/sqldrivers)
40+
else()
41+
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY_DEBUG ${CMAKE_BINARY_DIR}/Debug/sqldrivers)
42+
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY_RELEASE ${CMAKE_BINARY_DIR}/Release/sqldrivers)
43+
endif()
2344

2445
option(QSQLCIPHER_STATIC "Build plugin as a static library" OFF)
46+
option(QSQLCIPHER_BUILD_TESTS "Build the test binary" ON)
47+
set(QSQLCIPHER_COPYTO_DIR "" CACHE PATH "If set, the build artifact of the library will be copied there")
2548

2649
set(CMAKE_CXX_STANDARD 14)
2750
set(CMAKE_CXX_STANDARD_REQUIRED ON)
@@ -83,27 +106,62 @@ add_library(qsqlcipher ${LIBTYPE}
83106
"${_PRIVATE_SOURCES_DIRECTORY}/qsql_sqlite.cpp"
84107
)
85108

86-
if (WIN32)
87-
# Change this by hand if needed
88-
set(SQLCIPHER_INCLUDE_DIRS "D:/CppProjects/sqlcipher-original")
89-
endif()
90-
91109
target_include_directories(qsqlcipher PRIVATE
92110
${Qt5Sql_PRIVATE_INCLUDE_DIRS}
93111
${SQLCIPHER_INCLUDE_DIRS} ${Libsqlcipher_INCLUDE_DIRS}
94112
)
95113

96-
if (WIN32)
97-
# Change this by hand if needed
98-
set(SQLCIPHER_LIBRARIES "optimized;D:/CppProjects/sqlcipher-original-build/Retail/x64/sqlite3.lib;debug;D:/CppProjects/sqlcipher-original-build/Debug/x64/sqlite3.lib")
99-
endif()
100-
101114
target_link_libraries(qsqlcipher
102115
Qt5::Sql
103116
${SQLCIPHER_LIBRARIES} ${Libsqlcipher_LIBRARIES}
104117
)
105118

106-
include(CTest)
107-
if(BUILD_TESTING)
119+
if (NOT ("${QSQLCIPHER_COPYTO_DIR}" STREQUAL ""))
120+
add_custom_command(TARGET qsqlcipher POST_BUILD COMMAND ${CMAKE_COMMAND} -E copy $<TARGET_FILE:qsqlcipher> ${QSQLCIPHER_COPYTO_DIR}/$<TARGET_FILE_NAME:qsqlcipher>)
121+
endif()
122+
123+
find_program(DPKG_PROGRAM dpkg DOC "dpkg program of Debian-based systems")
124+
if (NOT WIN32)
125+
if (DPKG_PROGRAM)
126+
if(CMAKE_SIZEOF_VOID_P EQUAL 8)
127+
INSTALL(TARGETS qsqlcipher DESTINATION lib/x86_64-linux-gnu/qt5/plugins/sqldrivers)
128+
else()
129+
INSTALL(TARGETS qsqlcipher DESTINATION lib/i386-linux-gnu/qt5/plugins/sqldrivers)
130+
endif()
131+
else()
132+
message(WARNING "Packaging is not set up for this platform, either submit a ticket or change/add pathes yourself, if packaging is required.")
133+
INSTALL(TARGETS qsqlcipher DESTINATION sqldrivers)
134+
endif()
135+
else()
136+
INSTALL(TARGETS qsqlcipher DESTINATION sqldrivers)
137+
endif()
138+
139+
#include(CTest)
140+
if(QSQLCIPHER_BUILD_TESTS)
108141
add_subdirectory(${TEST_DIR})
109142
endif()
143+
144+
# Packaging
145+
SET(CPACK_PACKAGE_NAME "libqt5sql5-sqlcipher")
146+
SET(CPACK_PACKAGE_CONTACT "[email protected]")
147+
148+
SET(CPACK_PACKAGE_VERSION_MAJOR "0")
149+
SET(CPACK_PACKAGE_VERSION_MINOR "1")
150+
SET(CPACK_PACKAGE_VERSION_PATCH "1")
151+
SET(CPACK_PACKAGE_VERSION_REVISION "1")
152+
SET(CPACK_PACKAGE_VERSION "${CPACK_PACKAGE_VERSION_MAJOR}.${CPACK_PACKAGE_VERSION_MINOR}.${CPACK_PACKAGE_VERSION_PATCH}")
153+
154+
if (DPKG_PROGRAM)
155+
execute_process(COMMAND ${DPKG_PROGRAM} --print-architecture OUTPUT_VARIABLE CPACK_DEBIAN_PACKAGE_ARCHITECTURE OUTPUT_STRIP_TRAILING_WHITESPACE)
156+
SET(CPACK_PACKAGE_FILE_NAME "${CPACK_PACKAGE_NAME}_${CPACK_PACKAGE_VERSION}-${CPACK_PACKAGE_VERSION_REVISION}_${CPACK_DEBIAN_PACKAGE_ARCHITECTURE}")
157+
else (DPKG_PROGRAM)
158+
SET(CPACK_PACKAGE_FILE_NAME "${CPACK_PACKAGE_NAME}_${CPACK_PACKAGE_VERSION}-${CPACK_PACKAGE_VERSION_REVISION}_${CMAKE_SYSTEM_NAME}")
159+
SET(CPACK_DEBIAN_PACKAGE_ARCHITECTURE "amd64")
160+
endif(DPKG_PROGRAM)
161+
162+
SET(CPACK_DEBIAN_PACKAGE_DEPENDS "libqt5sql5, libsqlcipher0 (>= 3.4.1-1)")
163+
SET(DEBIAN_PACKAGE_BUILDS_DEPENDS "debhelper (>= 9), cmake (>= 3), qt5-qmake, qtbase5-dev, qtbase5-private-dev, qtbase5-dev-tools, qtmultimedia5-dev, gcc, g++")
164+
SET(CPACK_PACKAGE_DESCRIPTION_SUMMARY "A Qt5 wrapper for using SqlCipher as a QSqlDriver.")
165+
SET(CPACK_DEBIAN_PACKAGE_SECTION "libs")
166+
167+
INCLUDE(CPack)

cmake/FindLibsqlcipher.cmake

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
# This module finds the libsqlcipher header and library files. It
55
# sets the following variables.
66
#
7-
# Libsqlcipher_INCLUDE_DIR - Include directory for Libsqlcipher header files.
7+
# Libsqlcipher_INCLUDE_DIRS - Include directories for Libsqlcipher header files.
88
# Libsqlcipher_LIBRARIES - The name of the library to link against.
99
#
1010
# Libsqlcipher_FOUND - Indicates whether Libsqlcipher has been found
@@ -15,7 +15,7 @@ if(PKG_CONFIG_FOUND)
1515
pkg_check_modules(Libsqlcipher QUIET SQLCipher)
1616
endif()
1717

18-
find_path(Libsqlcipher_INCLUDE_DIR sqlcipher/sqlite3.h
18+
find_path(Libsqlcipher_INCLUDE_DIRS sqlcipher/sqlite3.h
1919
HINTS
2020
$ENV{LIBSQLCIPHER_DIR}
2121
PATH_SUFFIXES include/sqlcipher include
@@ -61,7 +61,6 @@ FIND_LIBRARY(Libsqlcipher_LIBRARIES
6161
/usr
6262
)
6363

64-
set(Libsqlcipher_INCLUDE_DIRS ${Libsqlcipher_INCLUDE_DIR})
6564
find_package_handle_standard_args(Libsqlcipher FOUND_VAR Libsqlcipher_FOUND REQUIRED_VARS Libsqlcipher_LIBRARIES Libsqlcipher_INCLUDE_DIRS)
66-
mark_as_advanced(Libsqlcipher_INCLUDE_DIR)
65+
mark_as_advanced(Libsqlcipher_INCLUDE_DIRS)
6766
mark_as_advanced(Libsqlcipher_LIBRARIES)

test-shared/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,4 @@ set(CMAKE_CXX_FLAGS "-std=c++14")
44
add_executable(qsqlcipher-test main.cpp)
55
target_link_libraries(qsqlcipher-test Qt5::Sql)
66

7-
add_test(NAME qsqlcipher-test COMMAND qsqlcipher-test)
7+
#add_test(NAME qsqlcipher-test COMMAND qsqlcipher-test)

test-shared/main.cpp

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,31 @@
11
#include <QCoreApplication>
2-
#include <QDebug>
32
#include <QtSql>
43
#include <QTemporaryDir>
54
#include <iostream>
5+
#include <cstdlib>
66

7-
#ifndef QT_DEBUG
8-
#error Must be built in debug mode!
9-
#endif
7+
#define QSQLCIPHER_TEST_ASSERT(cond, message) if(!(cond)) { std::cerr << message << std::endl; exit(-1); }
108

119
int main(int argc, char *argv[])
1210
{
1311
QCoreApplication app(argc, argv);
1412

1513
for (const auto& driver : QSqlDatabase::drivers()) {
16-
qDebug() << "Available QSqlDatabase driver: " << driver;
14+
std::cout << "Available QSqlDatabase driver: " << driver.toStdString() << std::endl;
1715
}
1816

19-
Q_ASSERT(QSqlDatabase::isDriverAvailable("QSQLITE")); // from Qt
20-
Q_ASSERT(QSqlDatabase::isDriverAvailable("QSQLCIPHER")); // from our plugin
17+
QSQLCIPHER_TEST_ASSERT(QSqlDatabase::isDriverAvailable("QSQLITE"), "QSQLITE driver is not available!"); // from Qt
18+
QSQLCIPHER_TEST_ASSERT(QSqlDatabase::isDriverAvailable("QSQLCIPHER"), "QSQLCIPHER driver is not available!"); // from our plugin
2119

2220
QTemporaryDir tmp;
23-
Q_ASSERT(tmp.isValid());
21+
QSQLCIPHER_TEST_ASSERT(tmp.isValid(), "Could not get temp directory.");
2422

2523
auto withDB = [&](const char *driver, auto fn) {
2624
QString path = QDir(tmp.path()).absoluteFilePath(QString(driver) + ".db");
2725
{
2826
QSqlDatabase db = QSqlDatabase::addDatabase(driver, "db");
2927
db.setDatabaseName(path);
30-
Q_ASSERT(db.open());
28+
QSQLCIPHER_TEST_ASSERT(db.open(), "Could not open database!");
3129
fn(db);
3230
}
3331
QSqlDatabase::removeDatabase("db");
@@ -46,27 +44,30 @@ int main(int argc, char *argv[])
4644
// Check that we can read from the SQLite db
4745
withDB("QSQLITE", [](auto db){
4846
QSqlQuery q = db.exec("select bar from foo");
49-
Q_ASSERT(q.next());
50-
Q_ASSERT(q.value(0).toInt() == 42);
47+
QSQLCIPHER_TEST_ASSERT(q.next(), "Expected a fetchable entry in Query object.");
48+
QSQLCIPHER_TEST_ASSERT(q.value(0).toInt() == 42, "Database returned invalid value for row!");
5149
});
5250

5351
std::cout << "Running Task 3..." << std::endl;
5452
// Check that SQLite is not SQLCipher
5553
withDB("QSQLITE", [](auto db){
5654
QSqlQuery q = db.exec("select sqlcipher_export()");
5755
QString errmsg = q.lastError().databaseText();
58-
Q_ASSERT(errmsg.startsWith("no such function"));
56+
QSQLCIPHER_TEST_ASSERT(errmsg.startsWith("no such function"), "Database did not respond with the expected error message.");
5957
});
6058
}
6159

6260
// QSQLCIPHER
6361
{
62+
#if defined(_WIN32) || defined(_WIN64) || defined(_MSC_VER )
63+
std::cout << "Hint: If you get an error in the following task like \"QSQLCIPHER driver not loaded\" then check if library dependencies (e.g. libeay32.dll) are in the same folder as the test executable." << std::endl;
64+
#endif
6465
std::cout << "Running Task 4..." << std::endl;
6566
// Check that SQLCipher is not SQLite
6667
withDB("QSQLCIPHER", [](auto db){
6768
QSqlQuery q = db.exec("select sqlcipher_export()");
6869
QString errmsg = q.lastError().databaseText();
69-
Q_ASSERT(errmsg.startsWith("wrong number of arguments"));
70+
QSQLCIPHER_TEST_ASSERT(errmsg.startsWith("wrong number of arguments"), "Database did not respond with the expected error message.");
7071
});
7172

7273
std::cout << "Running Task 5..." << std::endl;
@@ -84,16 +85,16 @@ int main(int argc, char *argv[])
8485
// Check that we can't read from the SQLCipher db without the passphrase
8586
withDB("QSQLCIPHER", [](auto db){
8687
QSqlQuery q = db.exec("select bar from foo");
87-
Q_ASSERT(!q.next());
88+
QSQLCIPHER_TEST_ASSERT(!q.next(), "Query returned a fetchable row, this should not be the case.");
8889
});
8990

9091
std::cout << "Running Task 7..." << std::endl;
9192
// Check that we can read from the SQLCipher db with the passphrase
9293
withDB("QSQLCIPHER", [](auto db){
9394
db.exec("PRAGMA key = 'foobar';");
9495
QSqlQuery q = db.exec("select bar from foo");
95-
Q_ASSERT(q.next());
96-
Q_ASSERT(q.value(0).toInt() == 42);
96+
QSQLCIPHER_TEST_ASSERT(q.next(), "Expected a fetchable entry in Query object.");
97+
QSQLCIPHER_TEST_ASSERT(q.value(0).toInt() == 42, "Database returned invalid value for row!");
9798
});
9899
std::cout << "Success! All tests completed." << std::endl;
99100
}

0 commit comments

Comments
 (0)