Skip to content

Commit

Permalink
Build shared and static libs separately
Browse files Browse the repository at this point in the history
  • Loading branch information
Krzmbrzl committed Feb 25, 2024
1 parent 7cd867e commit 239b518
Show file tree
Hide file tree
Showing 23 changed files with 313 additions and 504 deletions.
69 changes: 26 additions & 43 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,52 +22,40 @@ on:

jobs:
build:
runs-on: ${{ matrix.runner || 'ubuntu-22.04' }}
container: ${{ matrix.container }}
name: ${{ matrix.name }}
strategy:
fail-fast: false
matrix:
lib_type: [shared, static]
# Can't test DB2 as required db2exc package is no longer available after Ubuntu 14.04
backend: [sqlite3, postgresql, mysql, firebird, oracle, odbc, empty, valgrind]
runner: [ubuntu-22.04]
cxxstd: [14]
test_release_package: [false]
build_examples: [false]
include:
# Note: the jobs are ordered in the order of decreasing running
# time, as this should minimize the total run-time of all jobs.
- backend: postgresql
- backend: empty
runner: macos-11
name: PostgreSQL macOS
- backend: oracle
name: Oracle 11
no_boost: true
- backend: valgrind
name: Valgrind
- backend: odbc
name: ODBC
- backend: firebird
name: Firebird
- backend: postgresql
name: PostgreSQL Linux
- backend: mysql
name: MySQL
- backend: sqlite3
runner: macos-11
name: SQLite3 macOS
- backend: sqlite3
name: SQLite3 C++17
cxxstd: 17
- backend: sqlite3
name: SQLite3
- backend: empty
runner: macos-11
name: Empty macOS
- backend: empty
name: Empty
- backend: oracle
no_boost: true
runner: ubuntu-22.04
- name: SQLite3 Cxx17
backend: sqlite3
cxxstd: 17
runner: ubuntu-22.04
- name: Release package
backend: empty
test_release_package: true
# Unsupported: db2exc package is only available in Ubuntu 14.04 not
# supported by GitHub Actions any longer, we'd need to run it in
# Docker container if we really need it.
# backend: db2
- backend: empty
name: Examples
runner: ubuntu-22.04
- name: Examples
backend: empty
build_examples: true
runner: ubuntu-22.04

runs-on: ${{ matrix.runner }}

env:
SOCI_CI: true
Expand Down Expand Up @@ -102,14 +90,6 @@ jobs:
;;
esac
case "${{matrix.container}}" in
ubuntu:18.04)
# We need to use this option as GitHub certificate is not recognized by
# wget in this old container otherwise.
set_env_var SOCI_WGET_OPTIONS --no-check-certificate
;;
esac
if [ -n "${{matrix.cxxstd}}" ]; then
set_env_var SOCI_CXXSTD ${{matrix.cxxstd}}
fi
Expand All @@ -122,6 +102,9 @@ jobs:
if [ "${{matrix.build_examples}}" = true ]; then
set_env_var BUILD_EXAMPLES YES
fi
if [ "${{matrix.lib_type}}" = "static" ]; then
set_env_var SOCI_BUILD_STATIC YES
fi
- name: Install dependencies under Linux
if: runner.os == 'Linux'
Expand Down
18 changes: 11 additions & 7 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,13 @@ include(CheckCXXCompilerFlag)

check_ipo_supported(RESULT LTO_AVAILABLE)

option(SOCI_SHARED "Enable building SOCI as a shared library" ON)
option(SOCI_STATIC "Enable building SOCI as a static library" ON)
if (SOCI_STATIC)
set(SHARED_DEFAULT OFF)
else()
set(SHARED_DEFAULT ON)
endif()

option(SOCI_SHARED "Enable building SOCI as a shared library" ${SHARED_DEFAULT})
option(SOCI_TESTS "Enable building SOCI test cases" ${PROJECT_IS_TOP_LEVEL})
option(SOCI_ASAN "Enable building SOCI with enabled address sanitizers" OFF)
cmake_dependent_option(SOCI_LTO "Enable link time optimizations in release builds" ON "LTO_AVAILABLE" OFF)
Expand Down Expand Up @@ -61,11 +66,10 @@ set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)

# We need a dummy source file to add as sources for libs that otherwise
# only consist of object libraries (otherwise some toolchains may complain)
set(SOCI_CXX_DUMMY_SOURCE "${CMAKE_CURRENT_BINARY_DIR}/dummy.cpp")
if (NOT EXISTS "${SOCI_CXX_DUMMY_SOURCE}")
file(TOUCH "${SOCI_CXX_DUMMY_SOURCE}")
if (SOCI_SHARED)
set(SOCI_LIB_TYPE "SHARED")
else()
set(SOCI_LIB_TYPE "STATIC")
endif()

add_subdirectory(src)
Expand Down
4 changes: 4 additions & 0 deletions scripts/ci/common.sh
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,10 @@ if [ -n "${WITH_BOOST}" ]; then
SOCI_COMMON_CMAKE_OPTIONS="$SOCI_COMMON_CMAKE_OPTIONS -DWITH_BOOST=${WITH_BOOST}"
fi

if [ -n "${SOCI_BUILD_STATIC}" ]; then
SOCI_COMMON_CMAKE_OPTIONS="${SOCI_COMMON_CMAKE_OPTIONS} -DSOCI_SHARED=OFF"
fi

# These options are defaults and used by most builds, but not Valgrind one.
SOCI_DEFAULT_CMAKE_OPTIONS="${SOCI_COMMON_CMAKE_OPTIONS}
-DSOCI_ASAN=ON
Expand Down
19 changes: 0 additions & 19 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,24 +1,5 @@
add_library(soci_shared_interface INTERFACE)
add_library(SOCI::shared::soci ALIAS soci_shared_interface)

add_library(soci_static_interface INTERFACE)
add_library(SOCI::static::soci ALIAS soci_static_interface)

add_library(soci_interface INTERFACE)
add_library(SOCI::soci ALIAS soci_interface)

if (SOCI_STATIC AND SOCI_SHARED)
if (BUILD_SHARED_LIBS)
target_link_libraries(soci_interface INTERFACE SOCI::shared::soci)
else()
target_link_libraries(soci_interface INTERFACE SOCI::static::soci)
endif()
elseif(SOCI_STATIC)
target_link_libraries(soci_interface INTERFACE SOCI::static::soci)
else()
target_link_libraries(soci_interface INTERFACE SOCI::shared::soci)
endif()


add_subdirectory(core)
add_subdirectory(backends)
41 changes: 0 additions & 41 deletions src/backends/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
include(soci_utils)

set(SOCI_EMPTY ${PROJECT_IS_TOP_LEVEL} CACHE STRING "Include the 'empty' backend. Can be bool-valued or one of 'Enabled', 'Disabled' and 'AsAvailable'")

set(SOCI_DB2 "AsAvailable" CACHE STRING "Include the 'DB2' backend. Can be bool-valued or one of 'Enabled', 'Disabled' and 'AsAvailable'")
Expand All @@ -11,45 +9,6 @@ set(SOCI_POSTGRESQL "AsAvailable" CACHE STRING "Include the 'Postgresql' backend
set(SOCI_SQLITE3 "AsAvailable" CACHE STRING "Include the 'SQLite3' backend. Can be bool-valued or one of 'Enabled', 'Disabled' and 'AsAvailable'")


function(soci_backend_objects_to_lib)
set(FLAGS "")
set(ONE_VAL_OPTIONS "OBJECT_LIB" "SHARED_TARGET_NAME" "STATIC_TARGET_NAME" "ALIAS_NAME")
set(MULTI_VAL_OPTIONS "SHARED_DEPS" "STATIC_DEPS")
cmake_parse_arguments(SOCI_BACKEND_LIB "${FLAGS}" "${ONE_VAL_OPTIONS}" "${MULTI_VAL_OPTIONS}" ${ARGV})
soci_verify_parsed_arguments(
PREFIX "SOCI_BACKEND_LIB"
FUNCTION_NAME "soci_backend_objects_to_lib"
REQUIRED "OBJECT_LIB" "SHARED_TARGET_NAME" "STATIC_TARGET_NAME" "ALIAS_NAME"
)

if (SOCI_SHARED)
add_library(${SOCI_BACKEND_LIB_SHARED_TARGET_NAME} SHARED "${SOCI_CXX_DUMMY_SOURCE}")
target_link_libraries(${SOCI_BACKEND_LIB_SHARED_TARGET_NAME}
PUBLIC
${SOCI_BACKEND_LIB_OBJECT_LIB}
${SOCI_BACKEND_LIB_SHARED_DEPS}
)

add_library(SOCI::shared::${SOCI_BACKEND_LIB_ALIAS_NAME} ALIAS ${SOCI_BACKEND_LIB_SHARED_TARGET_NAME})

target_link_libraries(soci_shared_interface INTERFACE SOCI::shared::${SOCI_BACKEND_LIB_ALIAS_NAME})
endif()

if (SOCI_STATIC)
add_library(${SOCI_BACKEND_LIB_STATIC_TARGET_NAME} STATIC "${SOCI_CXX_DUMMY_SOURCE}")
target_link_libraries(${SOCI_BACKEND_LIB_STATIC_TARGET_NAME}
PUBLIC
${SOCI_BACKEND_LIB_OBJECT_LIB}
${SOCI_BACKEND_LIB_STATIC_DEPS}
)

add_library(SOCI::static::${SOCI_BACKEND_LIB_ALIAS_NAME} ALIAS ${SOCI_BACKEND_LIB_STATIC_TARGET_NAME})

target_link_libraries(soci_static_interface INTERFACE SOCI::static::${SOCI_BACKEND_LIB_ALIAS_NAME})
endif()
endfunction()


foreach(CURRENT IN ITEMS "db2" "empty" "firebird" "mysql" "odbc" "oracle" "postgresql" "sqlite3")
string(TOUPPER "${CURRENT}" CURRENT_UPPER)

Expand Down
43 changes: 20 additions & 23 deletions src/backends/db2/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,34 +12,31 @@ if (NOT DB2_FOUND)
return()
endif()

add_library(soci_db2_interface INTERFACE)
target_link_libraries(soci_db2_interface INTERFACE DB2::DB2 soci_core_interface)

add_library(soci_db2_objects OBJECT
"blob.cpp"
"factory.cpp"
"row-id.cpp"
"session.cpp"
"standard-into-type.cpp"
"standard-use-type.cpp"
"statement.cpp"
"vector-into-type.cpp"
"vector-use-type.cpp"
add_library(soci_db2
${SOCI_LIB_TYPE}
"blob.cpp"
"factory.cpp"
"row-id.cpp"
"session.cpp"
"standard-into-type.cpp"
"standard-use-type.cpp"
"statement.cpp"
"vector-into-type.cpp"
"vector-use-type.cpp"
)

target_link_libraries(soci_db2_objects PUBLIC soci_db2_interface)
target_link_libraries(soci_db2
PUBLIC
DB2::DB2
SOCI::Core
)

target_include_directories(soci_db2_objects
target_include_directories(soci_db2
PRIVATE
"${PROJECT_SOURCE_DIR}/include/private"
"${PROJECT_SOURCE_DIR}/include/soci"
)

soci_backend_objects_to_lib(
OBJECT_LIB soci_db2_objects
SHARED_TARGET_NAME soci_db2
STATIC_TARGET_NAME soci_db2_static
ALIAS_NAME DB2
SHARED_DEPS SOCI::shared::Core
STATIC_DEPS SOCI::static::Core
)
add_library(SOCI::DB2 ALIAS soci_db2)

target_link_libraries(soci_interface INTERFACE SOCI::DB2)
38 changes: 16 additions & 22 deletions src/backends/empty/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,31 +1,25 @@
add_library(soci_empty_interface INTERFACE)
target_link_libraries(soci_empty_interface INTERFACE soci_core_interface)

add_library(soci_empty_objects OBJECT
"blob.cpp"
"factory.cpp"
"row-id.cpp"
"session.cpp"
"standard-into-type.cpp"
"standard-use-type.cpp"
"statement.cpp"
"vector-into-type.cpp"
"vector-use-type.cpp"
add_library(soci_empty
${SOCI_LIB_TYPE}
"blob.cpp"
"factory.cpp"
"row-id.cpp"
"session.cpp"
"standard-into-type.cpp"
"standard-use-type.cpp"
"statement.cpp"
"vector-into-type.cpp"
"vector-use-type.cpp"
)

target_link_libraries(soci_empty_objects PUBLIC soci_empty_interface)
target_link_libraries(soci_empty PUBLIC SOCI::Core)

target_include_directories(soci_empty_objects
target_include_directories(soci_empty
PRIVATE
"${PROJECT_SOURCE_DIR}/include/private"
"${PROJECT_SOURCE_DIR}/include/soci"
)

soci_backend_objects_to_lib(
OBJECT_LIB soci_empty_objects
SHARED_TARGET_NAME soci_empty
STATIC_TARGET_NAME soci_empty_static
ALIAS_NAME Empty
SHARED_DEPS SOCI::shared::Core
STATIC_DEPS SOCI::static::Core
)
add_library(SOCI::Empty ALIAS soci_empty)

target_link_libraries(soci_interface INTERFACE SOCI::Empty)
45 changes: 21 additions & 24 deletions src/backends/firebird/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -18,35 +18,32 @@ if (NOT Firebird_FOUND)
return()
endif()

add_library(soci_firebird_interface INTERFACE)
target_link_libraries(soci_firebird_interface INTERFACE Firebird::Firebird soci_core_interface)

add_library(soci_firebird_objects OBJECT
"blob.cpp"
"common.cpp"
"error-firebird.cpp"
"factory.cpp"
"session.cpp"
"standard-into-type.cpp"
"standard-use-type.cpp"
"statement.cpp"
"vector-into-type.cpp"
"vector-use-type.cpp"
add_library(soci_firebird
${SOCI_LIB_TYPE}
"blob.cpp"
"common.cpp"
"error-firebird.cpp"
"factory.cpp"
"session.cpp"
"standard-into-type.cpp"
"standard-use-type.cpp"
"statement.cpp"
"vector-into-type.cpp"
"vector-use-type.cpp"
)

target_link_libraries(soci_firebird_objects PUBLIC Firebird::Firebird soci_firebird_interface)
target_link_libraries(soci_firebird
PUBLIC
Firebird::Firebird
SOCI::Core
)

target_include_directories(soci_firebird_objects
target_include_directories(soci_firebird
PRIVATE
"${PROJECT_SOURCE_DIR}/include/private"
"${PROJECT_SOURCE_DIR}/include/soci"
)

soci_backend_objects_to_lib(
OBJECT_LIB soci_firebird_objects
SHARED_TARGET_NAME soci_firebird
STATIC_TARGET_NAME soci_firebird_static
ALIAS_NAME Firebird
SHARED_DEPS SOCI::shared::Core
STATIC_DEPS SOCI::static::Core
)
add_library(SOCI::Firebird ALIAS soci_firebird)

target_link_libraries(soci_interface INTERFACE SOCI::Firebird)
Loading

0 comments on commit 239b518

Please sign in to comment.