Skip to content

Commit 699723c

Browse files
committed
add experimental support for statically linked builds
1 parent c16dfc5 commit 699723c

File tree

9 files changed

+183
-36
lines changed

9 files changed

+183
-36
lines changed

.github/workflows/alpine.yml

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
name: Alpine
2+
3+
on: push
4+
5+
jobs:
6+
alpine:
7+
runs-on: ubuntu-24.04
8+
container: 'alpine:3.22'
9+
10+
steps:
11+
- name: Install and prepare Git
12+
run: |
13+
apk update && apk upgrade
14+
apk add git
15+
git config --global --add safe.directory "$GITHUB_WORKSPACE"
16+
# Checks-out the repository under $GITHUB_WORKSPACE.
17+
- uses: actions/checkout@v4
18+
with:
19+
fetch-depth: 0
20+
submodules: 'recursive'
21+
- name: Install packages
22+
run: |
23+
apk update
24+
apk add catch2 cmake g++ libxml2-dev make pkgconf zlib-dev
25+
- name: Build
26+
run: |
27+
cd "$GITHUB_WORKSPACE"
28+
mkdir build
29+
cd build
30+
cmake ../
31+
cmake --build . -j4
32+
- name: Run tests
33+
run: |
34+
cd "$GITHUB_WORKSPACE/build"
35+
ctest -V
36+
- name: Install statically linked libraries
37+
run: |
38+
apk add libxml2-static xz-static zlib-static
39+
- name: Build statically linked executables
40+
run: |
41+
cd "$GITHUB_WORKSPACE"
42+
mkdir build-static
43+
cd build-static
44+
cmake ../ -DENABLE_LTO=ON -DENABLE_STATIC_LINKING=ON
45+
cmake --build . -j4
46+
- name: Run tests for statically linked build
47+
run: |
48+
cd "$GITHUB_WORKSPACE/build-static"
49+
ctest -V
50+
- name: Gather build artifacts
51+
run: |
52+
cd "$GITHUB_WORKSPACE"
53+
mkdir artifacts
54+
# binary files
55+
cp build-static/code/pmdb artifacts/
56+
# license
57+
cp LICENSE artifacts/
58+
# determine version
59+
VERSION=$(git describe --always)
60+
echo Version is $VERSION.
61+
mv artifacts pmdb-$VERSION
62+
tar czf pmdb_${VERSION}_linux-amd64-generic.tar.gz pmdb-$VERSION
63+
- name: Archive build artifacts
64+
uses: actions/upload-artifact@v4
65+
with:
66+
name: pmdb_linux-amd64-generic
67+
path: pmdb_*_linux-amd64-generic.tar.gz
68+
if-no-files-found: error

CMakeLists.txt

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,37 @@ project(pmdb)
55
# allow tests
66
enable_testing()
77

8+
# If the option ENABLE_LTO is enabled (e. g. via `cmake -DENABLE_LTO=ON`)
9+
# during the build, then all binaries will use link-time optimization (LTO).
10+
option(ENABLE_LTO "Enable link-time optimization" OFF)
11+
# Not all compilers support LTO / IPO, so it has to be checked.
12+
if (ENABLE_LTO)
13+
cmake_policy(SET CMP0069 NEW)
14+
include(CheckIPOSupported)
15+
check_ipo_supported(RESULT HAS_LTO_SUPPORT OUTPUT LTO_FAIL_REASON
16+
LANGUAGES C CXX)
17+
if (NOT HAS_LTO_SUPPORT)
18+
message(FATAL "IPO / LTO is not supported: ${LTO_FAIL_REASON}")
19+
else()
20+
message(STATUS "IPO / LTO is supported. Using it.")
21+
endif()
22+
endif (ENABLE_LTO)
23+
24+
# If ENABLE_STATIC_LINKING is on (e. g. via `cmake -DENABLE_STATIC_LINKING=ON`),
25+
# then all libraries are linked statically. The option is off by default.
26+
#
27+
# Static linking increases the size of the binaries, but those binaries do not
28+
# need the libraries to be present on the system.
29+
#
30+
# WARNING: This option is still experimental and may not work completely.
31+
option(ENABLE_STATIC_LINKING "Links all libraries statically" OFF)
32+
if (ENABLE_STATIC_LINKING)
33+
set(CMAKE_LINK_SEARCH_START_STATIC 1)
34+
set(CMAKE_LINK_SEARCH_END_STATIC 1)
35+
set(CMAKE_FIND_LIBRARY_SUFFIXES ".a")
36+
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -static-libgcc -static-libstdc++ -static")
37+
endif ()
38+
839
#actual project
940
add_subdirectory (code)
1041

code/CMakeLists.txt

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -49,20 +49,30 @@ set(CMAKE_CXX_STANDARD_REQUIRED ON)
4949
add_executable(pmdb ${pmdb_sources})
5050

5151
# find libxml2
52-
set(LIBXML2_DIR "../cmake/" )
53-
find_package (LIBXML2)
52+
find_package (PkgConfig)
53+
pkg_search_module (LIBXML2 REQUIRED libxml-2.0)
5454
if (LIBXML2_FOUND)
55-
include_directories(${LIBXML2_INCLUDE_DIRS})
56-
target_link_libraries (pmdb ${LIBXML2_LIBRARIES})
55+
if (ENABLE_STATIC_LINKING)
56+
include_directories(${LIBXML2_STATIC_INCLUDE_DIRS})
57+
target_link_libraries (pmdb ${LIBXML2_STATIC_LIBRARIES})
58+
else ()
59+
include_directories(${LIBXML2_INCLUDE_DIRS})
60+
target_link_libraries (pmdb ${LIBXML2_LIBRARIES})
61+
endif ()
5762
else ()
5863
message ( FATAL_ERROR "libxml2 was not found!" )
5964
endif (LIBXML2_FOUND)
6065

6166
#find zlib
62-
find_package (ZLIB)
67+
pkg_search_module (ZLIB REQUIRED zlib)
6368
if (ZLIB_FOUND)
64-
include_directories(${ZLIB_INCLUDE_DIRS})
65-
target_link_libraries (pmdb ${ZLIB_LIBRARIES})
69+
if (ENABLE_STATIC_LINKING)
70+
include_directories(${ZLIB_STATIC_INCLUDE_DIRS})
71+
target_link_libraries (pmdb ${ZLIB_STATIC_LIBRARIES})
72+
else ()
73+
include_directories(${ZLIB_INCLUDE_DIRS})
74+
target_link_libraries (pmdb ${ZLIB_LIBRARIES})
75+
endif ()
6676
else ()
6777
message ( FATAL_ERROR "zlib was not found!" )
6878
endif (ZLIB_FOUND)

program-no-compression/CMakeLists.txt

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,11 +51,16 @@ endif ()
5151
add_executable(pmdb_no_comp ${pmdb_no_comp_sources})
5252

5353
# find libxml2
54-
set(LIBXML2_DIR "../cmake/" )
55-
find_package (LIBXML2)
54+
find_package (PkgConfig)
55+
pkg_search_module (LIBXML2 REQUIRED libxml-2.0)
5656
if (LIBXML2_FOUND)
57-
include_directories(${LIBXML2_INCLUDE_DIRS})
58-
target_link_libraries (pmdb_no_comp ${LIBXML2_LIBRARIES})
57+
if (ENABLE_STATIC_LINKING)
58+
include_directories(${LIBXML2_STATIC_INCLUDE_DIRS})
59+
target_link_libraries (pmdb_no_comp ${LIBXML2_STATIC_LIBRARIES})
60+
else ()
61+
include_directories(${LIBXML2_INCLUDE_DIRS})
62+
target_link_libraries (pmdb_no_comp ${LIBXML2_LIBRARIES})
63+
endif ()
5964
else ()
6065
message ( FATAL_ERROR "libxml2 was not found!" )
6166
endif (LIBXML2_FOUND)

tests/messagedatabase/addMessage/CMakeLists.txt

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,16 @@ set(CMAKE_CXX_STANDARD_REQUIRED ON)
3333
add_executable(addMessage_test ${addMessage_test_src})
3434

3535
# find libxml2
36-
set(LIBXML2_DIR "../../../cmake/" )
37-
find_package (LIBXML2)
36+
find_package (PkgConfig)
37+
pkg_search_module (LIBXML2 REQUIRED libxml-2.0)
3838
if (LIBXML2_FOUND)
39-
include_directories(${LIBXML2_INCLUDE_DIRS})
40-
target_link_libraries (addMessage_test ${LIBXML2_LIBRARIES})
39+
if (ENABLE_STATIC_LINKING)
40+
include_directories(${LIBXML2_STATIC_INCLUDE_DIRS})
41+
target_link_libraries (addMessage_test ${LIBXML2_STATIC_LIBRARIES})
42+
else ()
43+
include_directories(${LIBXML2_INCLUDE_DIRS})
44+
target_link_libraries (addMessage_test ${LIBXML2_LIBRARIES})
45+
endif ()
4146
else ()
4247
message ( FATAL_ERROR "libxml2 was not found!" )
4348
endif (LIBXML2_FOUND)

tests/messagedatabase/importFromFile/CMakeLists.txt

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,20 +35,27 @@ endif ()
3535
add_executable(importFromFile_test ${importFromFile_test_src})
3636

3737
# find libxml2
38-
set(LIBXML2_DIR "../../../cmake/" )
39-
find_package (LIBXML2)
38+
find_package (PkgConfig)
39+
pkg_search_module (LIBXML2 REQUIRED libxml-2.0)
4040
if (LIBXML2_FOUND)
41-
include_directories(${LIBXML2_INCLUDE_DIRS})
42-
target_link_libraries (importFromFile_test ${LIBXML2_LIBRARIES})
41+
if (ENABLE_STATIC_LINKING)
42+
include_directories(${LIBXML2_STATIC_INCLUDE_DIRS})
43+
target_link_libraries (importFromFile_test ${LIBXML2_STATIC_LIBRARIES})
44+
else ()
45+
include_directories(${LIBXML2_INCLUDE_DIRS})
46+
target_link_libraries (importFromFile_test ${LIBXML2_LIBRARIES})
47+
endif ()
4348
else ()
4449
message ( FATAL_ERROR "libxml2 was not found!" )
4550
endif (LIBXML2_FOUND)
4651

4752
# find iconv
4853
find_package(Iconv)
4954
if (Iconv_FOUND)
50-
include_directories(${Iconv_INCLUDE_DIRS})
51-
target_link_libraries (importFromFile_test ${Iconv_LIBRARIES})
55+
if (NOT Iconv_IS_BUILT_IN)
56+
include_directories(${Iconv_INCLUDE_DIRS})
57+
target_link_libraries (importFromFile_test ${Iconv_LIBRARIES})
58+
endif ()
5259
else ()
5360
message ( FATAL_ERROR "libiconv was not found!" )
5461
endif (Iconv_FOUND)

tests/messagedatabase/save-load-compressed/CMakeLists.txt

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -34,20 +34,30 @@ set(CMAKE_CXX_STANDARD_REQUIRED ON)
3434
add_executable(MessageDatabase_saveload_compressed_test ${MessageDatabase_saveload_compressed_test_src})
3535

3636
# find libxml2
37-
set(LIBXML2_DIR "../../../cmake/" )
38-
find_package (LIBXML2)
37+
find_package (PkgConfig)
38+
pkg_search_module (LIBXML2 REQUIRED libxml-2.0)
3939
if (LIBXML2_FOUND)
40-
include_directories(${LIBXML2_INCLUDE_DIRS})
41-
target_link_libraries (MessageDatabase_saveload_compressed_test ${LIBXML2_LIBRARIES})
40+
if (ENABLE_STATIC_LINKING)
41+
include_directories(${LIBXML2_STATIC_INCLUDE_DIRS})
42+
target_link_libraries (MessageDatabase_saveload_compressed_test ${LIBXML2_STATIC_LIBRARIES})
43+
else ()
44+
include_directories(${LIBXML2_INCLUDE_DIRS})
45+
target_link_libraries (MessageDatabase_saveload_compressed_test ${LIBXML2_LIBRARIES})
46+
endif ()
4247
else ()
4348
message ( FATAL_ERROR "libxml2 was not found!" )
4449
endif (LIBXML2_FOUND)
4550

4651
#find zlib
47-
find_package (ZLIB)
52+
pkg_search_module (ZLIB REQUIRED zlib)
4853
if (ZLIB_FOUND)
49-
include_directories(${ZLIB_INCLUDE_DIRS})
50-
target_link_libraries (MessageDatabase_saveload_compressed_test ${ZLIB_LIBRARIES})
54+
if (ENABLE_STATIC_LINKING)
55+
include_directories(${ZLIB_STATIC_INCLUDE_DIRS})
56+
target_link_libraries (MessageDatabase_saveload_compressed_test ${ZLIB_STATIC_LIBRARIES})
57+
else ()
58+
include_directories(${ZLIB_INCLUDE_DIRS})
59+
target_link_libraries (MessageDatabase_saveload_compressed_test ${ZLIB_LIBRARIES})
60+
endif ()
5161
else ()
5262
message ( FATAL_ERROR "zlib was not found!" )
5363
endif (ZLIB_FOUND)

tests/messagedatabase/save-load/CMakeLists.txt

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,16 @@ set(CMAKE_CXX_STANDARD_REQUIRED ON)
3333
add_executable(MessageDatabase_saveload_test ${MessageDatabase_saveload_test_src})
3434

3535
# find libxml2
36-
set(LIBXML2_DIR "../../../cmake/" )
37-
find_package (LIBXML2)
36+
find_package (PkgConfig)
37+
pkg_search_module (LIBXML2 REQUIRED libxml-2.0)
3838
if (LIBXML2_FOUND)
39-
include_directories(${LIBXML2_INCLUDE_DIRS})
40-
target_link_libraries (MessageDatabase_saveload_test ${LIBXML2_LIBRARIES})
39+
if (ENABLE_STATIC_LINKING)
40+
include_directories(${LIBXML2_STATIC_INCLUDE_DIRS})
41+
target_link_libraries (MessageDatabase_saveload_test ${LIBXML2_STATIC_LIBRARIES})
42+
else ()
43+
include_directories(${LIBXML2_INCLUDE_DIRS})
44+
target_link_libraries (MessageDatabase_saveload_test ${LIBXML2_LIBRARIES})
45+
endif ()
4146
else ()
4247
message ( FATAL_ERROR "libxml2 was not found!" )
4348
endif (LIBXML2_FOUND)

tests/privatemessage/save-load-compressed/CMakeLists.txt

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,17 @@ set(CMAKE_CXX_STANDARD_REQUIRED ON)
2424

2525
add_executable(PM_save_load_compressed_test ${PM_save_load_compressed_test_src})
2626

27-
#find zlib
28-
find_package (ZLIB)
27+
# find zlib
28+
find_package (PkgConfig)
29+
pkg_search_module (ZLIB REQUIRED zlib)
2930
if (ZLIB_FOUND)
30-
include_directories(${ZLIB_INCLUDE_DIRS})
31-
target_link_libraries (PM_save_load_compressed_test ${ZLIB_LIBRARIES})
31+
if (ENABLE_STATIC_LINKING)
32+
include_directories(${ZLIB_STATIC_INCLUDE_DIRS})
33+
target_link_libraries (PM_save_load_compressed_test ${ZLIB_STATIC_LIBRARIES})
34+
else ()
35+
include_directories(${ZLIB_INCLUDE_DIRS})
36+
target_link_libraries (PM_save_load_compressed_test ${ZLIB_LIBRARIES})
37+
endif ()
3238
else ()
3339
message ( FATAL_ERROR "zlib was not found!" )
3440
endif (ZLIB_FOUND)

0 commit comments

Comments
 (0)