Skip to content

Commit

Permalink
Initial work to spport building it as shared lib :)
Browse files Browse the repository at this point in the history
Signed-off-by: Christian Parpart <[email protected]>
  • Loading branch information
christianparpart committed Oct 25, 2024
1 parent 0dc3977 commit c0d82f5
Show file tree
Hide file tree
Showing 60 changed files with 519 additions and 3,601 deletions.
15 changes: 10 additions & 5 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -163,13 +163,17 @@ jobs:
-DCMAKE_CXX_COMPILER="${CC_EXE}-${CC_VER}" \
-DCMAKE_CXX_FLAGS="${CMAKE_CXX_FLAGS}" \
-DCMAKE_EXE_LINKER_FLAGS="${CMAKE_EXE_LINKER_FLAGS}" \
-DCMAKE_INSTALL_PREFIX="/usr" \
-DCMAKE_INSTALL_PREFIX="/usr/local" \
-DPEDANTIC_COMPILER_WERROR=OFF \
--preset linux-gcc-release
- name: "build"
run: cmake --build --preset linux-gcc-release -- -j3
- name: "tests"
run: ctest --preset linux-gcc-release
- name: "package tar.gz"
run: |
cpack --preset linux-gcc-release
cp out/package/linux-gcc-debug/Lightweight-*-Linux.tar.gz ./Lightweight-Linux-CI.tar.gz
- name: "Run tests through valgrind (without model tests)"
run: |
valgrind \
Expand All @@ -186,7 +190,7 @@ jobs:
with:
name: ubuntu2404-tests
path: |
LightweightTest
Lightweight-Linux-CI.tar.gz
retention-days: 1

dbms_test_matrix:
Expand All @@ -213,15 +217,16 @@ jobs:
uses: actions/download-artifact@v4
with:
name: ubuntu2404-tests
- name: "Mark unit tests as executable"
run: chmod 0755 LightweightTest
- name: "Extract package"
run: |
sudo tar -xvf Lightweight-Linux-CI.tar.gz --strip-components=1 -C /usr/local
- name: "Setup ${{ matrix.database }}"
id: setup
run: bash ./.github/prepare-test-run.sh "${{ matrix.database }}"
- name: "Dump SQL connection string"
run: echo "ODBC_CONNECTION_STRING=${{ steps.setup.outputs.ODBC_CONNECTION_STRING }}"
- name: "Run SQL Core tests"
run: ./LightweightTest --trace-sql --trace-model -s # --odbc-connection-string="${{ steps.setup.outputs.ODBC_CONNECTION_STRING }}"
run: LightweightTest --trace-sql --trace-model -s # --odbc-connection-string="${{ steps.setup.outputs.ODBC_CONNECTION_STRING }}"
env:
ODBC_CONNECTION_STRING: "${{ steps.setup.outputs.ODBC_CONNECTION_STRING }}"

Expand Down
1 change: 0 additions & 1 deletion .vimspector.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
"request": "launch",
"program": "${workspaceRoot}/out/build/linux-gcc-debug/src/tests/LightweightTest",
"args": [
"[Unicode]"
],
"cwd": "${workspaceRoot}",
"externalConsole": true,
Expand Down
12 changes: 12 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,18 @@ if(DEFINED MSVC)
add_compile_options(/nologo)
endif()

if(NOT(CPACK_GENERATOR))
set(CPACK_GENERATOR TGZ)
endif()
set(CPACK_PACKAGE_NAME "Lightweight")
set(CPACK_PACKAGE_VENDOR "https://github.com/christianparpart/Lightwweight/")
set(CPACK_PACKAGE_DESCRIPTION_SUMMARY "Lightweight SQL C++ library on top of ODBC")
set(CPACK_PACKAGE_CONTACT "Christian Parpart <[email protected]>")
set(CPACK_RESOURCE_FILE_README "${CMAKE_SOURCE_DIR}/README.md")
#set(CPACK_PACKAGE_EXECUTABLES LightweightTest "Lightweight Test Suite")
#set(CPACK_RESOURCE_FILE_LICENSE "${CMAKE_SOURCE_DIR}/LICENSE.txt")
#set(CPACK_PACKAGE_INSTALL_DIRECTORY "Lightweight SQL ${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR}")

add_subdirectory(src/Lightweight)
add_subdirectory(src/tools)

Expand Down
16 changes: 16 additions & 0 deletions CMakePresets.json
Original file line number Diff line number Diff line change
Expand Up @@ -84,5 +84,21 @@
{ "name": "linux-gcc-release", "configurePreset": "linux-gcc-release", "output": {"outputOnFailure": true}, "execution": { "noTestsAction": "error", "stopOnFailure": true } },
{ "name": "linux-clang-debug", "configurePreset": "linux-clang-debug", "output": {"outputOnFailure": true}, "execution": { "noTestsAction": "error", "stopOnFailure": true } },
{ "name": "linux-clang-release", "configurePreset": "linux-clang-release", "output": {"outputOnFailure": true}, "execution": { "noTestsAction": "error", "stopOnFailure": true } }
],
"packagePresets": [
{
"name": "linux-gcc-debug",
"packageName": "Lightweight",
"packageDirectory": "${sourceDir}/out/package/linux-gcc-debug",
"configurePreset": "linux-gcc-debug",
"generators": [ "TGZ" ]
},
{
"name": "linux-gcc-release",
"packageName": "Lightweight",
"packageDirectory": "${sourceDir}/out/package/linux-gcc-debug",
"configurePreset": "linux-gcc-release",
"generators": [ "TGZ" ]
}
]
}
25 changes: 25 additions & 0 deletions src/Lightweight/Api.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// SPDX-License-Identifier: Apache-2.0

#pragma once

#if defined(__GNUC__)
#define LIGHTWEIGHT_NO_EXPORT __attribute__((visibility("hidden")))
#define LIGHTWEIGHT_EXPORT __attribute__((visibility("default")))
#define LIGHTWEIGHT_IMPORT /*!*/
#define LIGHTWEIGHT_FORCE_INLINE __attribute__((always_inline))
#elif defined(_MSC_VER)
#define LIGHTWEIGHT_NO_EXPORT /*!*/
#define LIGHTWEIGHT_EXPORT __declspec(dllexport)
#define LIGHTWEIGHT_IMPORT __declspec(dllimport)
#define LIGHTWEIGHT_FORCE_INLINE __forceinline
#endif

#if defined(LIGHTWEIGHT_SHARED)
#if defined(BUILD_LIGHTWEIGHT)
#define LIGHTWEIGHT_API LIGHTWEIGHT_EXPORT
#else
#define LIGHTWEIGHT_API LIGHTWEIGHT_IMPORT
#endif
#else
#define LIGHTWEIGHT_API /*!*/
#endif
78 changes: 64 additions & 14 deletions src/Lightweight/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,30 @@ if(NOT WIN32)
endif()

set(HEADER_FILES
DataBinder/BasicStringBinder.hpp
DataBinder/Core.hpp
DataBinder/MFCStringLike.hpp
DataBinder/Primitives.hpp
DataBinder/SqlDate.hpp
DataBinder/SqlDateTime.hpp
DataBinder/SqlFixedString.hpp
DataBinder/SqlNullValue.hpp
DataBinder/SqlText.hpp
DataBinder/SqlTime.hpp
DataBinder/SqlTrimmedString.hpp
DataBinder/SqlVariant.hpp
DataBinder/StdOptional.hpp
DataBinder/StdString.hpp
DataBinder/StdStringView.hpp
DataBinder/StringLiteral.hpp
DataBinder/UnicodeConverter.hpp
Model/Associations/BelongsTo.hpp
Model/Associations/HasMany.hpp
Model/Associations/HasOne.hpp
Model/ColumnType.hpp
Model/Detail.hpp
Model/Logger.hpp
Model/Record.hpp
Model/Relation.hpp
Model/StringLiteral.hpp
Model/Utils.hpp

SqlQuery/Core.hpp
SqlQuery/Delete.hpp
SqlQuery/Insert.hpp
SqlQuery/Select.hpp
SqlQuery/Update.hpp

SqlConnectInfo.hpp
SqlConnection.hpp
SqlError.hpp
Expand All @@ -47,8 +60,6 @@ set(HEADER_FILES

set(SOURCE_FILES
DataBinder/UnicodeConverter.cpp
Model/AbstractRecord.cpp
Model/Logger.cpp
SqlConnectInfo.cpp
SqlConnection.cpp
SqlError.cpp
Expand All @@ -61,11 +72,31 @@ set(SOURCE_FILES
SqlTransaction.cpp
)

add_library(Lightweight STATIC)
set(LIGHTWEIGHT_BUILD_SHARED_DEFAULT OFF)
if(WIN32)
set(LIGHTWEIGHT_BUILD_SHARED_DEFAULT ON)
endif()

option(LIGHTWEIGHT_BUILD_SHARED "Build Lightweight as a shared library" ${LIGHTWEIGHT_BUILD_SHARED_DEFAULT})

if(LIGHTWEIGHT_BUILD_SHARED)
set(LIGHTWEIGHT_LIBRARY_TYPE SHARED)
else()
set(LIGHTWEIGHT_LIBRARY_TYPE STATIC)
endif()

add_library(Lightweight ${LIGHTWEIGHT_LIBRARY_TYPE})

add_library(Lightweight::Lightweight ALIAS Lightweight)
target_compile_features(Lightweight PUBLIC cxx_std_23)
target_sources(Lightweight PRIVATE ${SOURCE_FILES})
#target_sources(Lightweight PUBLIC ${HEADER_FILES})
target_sources(Lightweight PUBLIC ${HEADER_FILES})

if(LIGHTWEIGHT_BUILD_SHARED)
target_compile_definitions(Lightweight PRIVATE BUILD_LIGHTWEIGHT=1)
target_compile_definitions(Lightweight PUBLIC LIGHTWEIGHT_SHARED=1)
set_target_properties(Lightweight PROPERTIES CXX_VISIBILITY_PRESET hidden)
endif()

if(CLANG_TIDY_EXE)
set_target_properties(Lightweight PROPERTIES CXX_CLANG_TIDY "${CLANG_TIDY_EXE}")
Expand All @@ -90,3 +121,22 @@ else()
target_link_libraries(Lightweight PUBLIC stdc++exp) # GCC >= 14
endif()
endif()

# ==================================================================================================


if(NOT WIN32)
include(GNUInstallDirs)
endif()

#install(TARGETS LightweightTest DESTINATION bin)
install(TARGETS Lightweight DESTINATION lib)

# install header files recursively
install(
DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/
DESTINATION include/Lightweight
FILES_MATCHING PATTERN "*.hpp"
)

include(CPack)
4 changes: 2 additions & 2 deletions src/Lightweight/DataBinder/BasicStringBinder.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
#include <utility>

template <SqlCommonStringBinderConcept StringType>
struct SqlDataBinder<StringType>
struct LIGHTWEIGHT_API SqlDataBinder<StringType>
{
using ValueType = StringType;
using StringTraits = SqlCommonStringBinder<ValueType>;
Expand Down Expand Up @@ -121,7 +121,7 @@ struct SqlDataBinder<StringType>
};

template <SqlCommonWideStringBinderConcept StringType>
struct SqlDataBinder<StringType>
struct LIGHTWEIGHT_API SqlDataBinder<StringType>
{
using ValueType = StringType;
using StringTraits = SqlCommonStringBinder<ValueType>;
Expand Down
3 changes: 2 additions & 1 deletion src/Lightweight/DataBinder/Core.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include <Windows.h>
#endif

#include "../Api.hpp"
#include "../SqlTraits.hpp"

#include <concepts>
Expand All @@ -20,7 +21,7 @@
// This is needed because the SQLBindCol() function does not allow to specify a callback function to be called
// after the data has been fetched from the database. This is needed to trim strings to the correct size, for
// example.
class SqlDataBinderCallback
class LIGHTWEIGHT_API SqlDataBinderCallback
{
public:
virtual ~SqlDataBinderCallback() = default;
Expand Down
2 changes: 1 addition & 1 deletion src/Lightweight/DataBinder/MFCStringLike.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ concept MFCStringLike = requires(T const& t) {

template <typename T>
requires(MFCStringLike<T>)
struct SqlDataBinder<T>
struct LIGHTWEIGHT_API SqlDataBinder<T>
{
static SQLRETURN InputParameter(SQLHSTMT stmt, SQLUSMALLINT column, T const& value) noexcept
{
Expand Down
2 changes: 1 addition & 1 deletion src/Lightweight/DataBinder/Primitives.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

// clang-format off
template <typename T, SQLSMALLINT TheCType, SQLINTEGER TheSqlType, SqlColumnType TheColumnType>
struct SqlSimpleDataBinder
struct LIGHTWEIGHT_API SqlSimpleDataBinder
{
static constexpr inline SqlColumnType ColumnType = TheColumnType;

Expand Down
4 changes: 2 additions & 2 deletions src/Lightweight/DataBinder/SqlDate.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
#include <chrono>

// Helper struct to store a date (without time of the day) to write to or read from a database.
struct SqlDate
struct LIGHTWEIGHT_API SqlDate
{
SQL_DATE_STRUCT sqlValue {};

Expand Down Expand Up @@ -69,7 +69,7 @@ struct SqlDate
};

template <>
struct SqlDataBinder<SqlDate>
struct LIGHTWEIGHT_API SqlDataBinder<SqlDate>
{
static SQLRETURN InputParameter(SQLHSTMT stmt, SQLUSMALLINT column, SqlDate const& value) noexcept
{
Expand Down
6 changes: 3 additions & 3 deletions src/Lightweight/DataBinder/SqlDateTime.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

#include <chrono>

struct SqlDateTime
struct LIGHTWEIGHT_API SqlDateTime
{
using native_type = std::chrono::time_point<std::chrono::system_clock, std::chrono::nanoseconds>;

Expand Down Expand Up @@ -110,7 +110,7 @@ struct SqlDateTime
};

template <>
struct SqlDataBinder<SqlDateTime::native_type>
struct LIGHTWEIGHT_API SqlDataBinder<SqlDateTime::native_type>
{
static SQLRETURN GetColumn(SQLHSTMT stmt,
SQLUSMALLINT column,
Expand All @@ -126,7 +126,7 @@ struct SqlDataBinder<SqlDateTime::native_type>
};

template <>
struct SqlDataBinder<SqlDateTime>
struct LIGHTWEIGHT_API SqlDataBinder<SqlDateTime>
{
static SQLRETURN InputParameter(SQLHSTMT stmt, SQLUSMALLINT column, SqlDateTime const& value) noexcept
{
Expand Down
8 changes: 4 additions & 4 deletions src/Lightweight/DataBinder/SqlFixedString.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ enum class SqlStringPostRetrieveOperation : uint8_t
template <std::size_t N,
typename T = char,
SqlStringPostRetrieveOperation PostOp = SqlStringPostRetrieveOperation::NOTHING>
class SqlFixedString
class LIGHTWEIGHT_API SqlFixedString
{
private:
T _data[N + 1] {};
Expand Down Expand Up @@ -192,7 +192,7 @@ template <std::size_t N, typename T = char>
using SqlTrimmedFixedString = SqlFixedString<N, T, SqlStringPostRetrieveOperation::TRIM_RIGHT>;

template <std::size_t N, typename T, SqlStringPostRetrieveOperation PostOp>
struct SqlDataBinder<SqlFixedString<N, T, PostOp>>
struct LIGHTWEIGHT_API SqlDataBinder<SqlFixedString<N, T, PostOp>>
{
using ValueType = SqlFixedString<N, T, PostOp>;
using StringTraits = SqlCommonStringBinder<ValueType>;
Expand Down Expand Up @@ -269,7 +269,7 @@ struct SqlDataBinder<SqlFixedString<N, T, PostOp>>
};

template <std::size_t N, typename T, SqlStringPostRetrieveOperation PostOp>
struct SqlCommonStringBinder<SqlFixedString<N, T, PostOp>>
struct LIGHTWEIGHT_API SqlCommonStringBinder<SqlFixedString<N, T, PostOp>>
{
using ValueType = SqlFixedString<N, T, PostOp>;
// clang-format off
Expand All @@ -283,7 +283,7 @@ struct SqlCommonStringBinder<SqlFixedString<N, T, PostOp>>
};

template <std::size_t N, SqlStringPostRetrieveOperation PostOp>
struct SqlDataTraits<SqlFixedString<N, char, PostOp>>
struct LIGHTWEIGHT_API SqlDataTraits<SqlFixedString<N, char, PostOp>>
{
static constexpr inline unsigned Size = N;
static constexpr inline SqlColumnType Type = SqlColumnType::CHAR;
Expand Down
Loading

0 comments on commit c0d82f5

Please sign in to comment.