Skip to content

Commit d888b4f

Browse files
committed
add options for static linking and LTO to CMake builds
1 parent 8cba430 commit d888b4f

File tree

4 files changed

+63
-1
lines changed

4 files changed

+63
-1
lines changed

.github/workflows/clang.yml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,17 @@ jobs:
2727
cd build
2828
cmake ../
2929
make -j2
30+
- name: Build statically linked with Clang ${{ matrix.version }}
31+
run: |
32+
export CXX=clang++-${{ matrix.version }}
33+
export CC=clang-${{ matrix.version }}
34+
cd $GITHUB_WORKSPACE
35+
mkdir build-static
36+
cd build-static
37+
cmake -DENABLE_LTO=ON -DENABLE_STATIC_LINKING=ON ../
38+
make -j2
39+
# Only run static build on latest version in the matrix.
40+
if: matrix.version == 14
3041
- name: Run tests
3142
run: |
3243
cd "$GITHUB_WORKSPACE/build"

.github/workflows/gcc.yml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,17 @@ jobs:
2727
cd build
2828
cmake ../
2929
make -j2
30+
- name: Build statically linked with GNU GCC ${{ matrix.version }}
31+
run: |
32+
export CXX=g++-${{ matrix.version }}
33+
export CC=gcc-${{ matrix.version }}
34+
cd $GITHUB_WORKSPACE
35+
mkdir build-static
36+
cd build-static
37+
cmake -DENABLE_LTO=ON -DENABLE_STATIC_LINKING=ON ../
38+
make -j2
39+
# Only run static build on latest version in the matrix.
40+
if: matrix.version == 11
3041
- name: Run tests
3142
run: |
3243
cd "$GITHUB_WORKSPACE/build"

.github/workflows/msys2.yml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,3 +37,13 @@ jobs:
3737
export MSYSTEM=MINGW64
3838
cd "$GITHUB_WORKSPACE/build"
3939
ctest -V
40+
- name: Build statically linked
41+
run: |
42+
export MSYSTEM=MINGW64
43+
export CXX=g++
44+
export CC=gcc
45+
cd $GITHUB_WORKSPACE
46+
mkdir build-static
47+
cd build-static
48+
cmake -DENABLE_LTO=ON -DENABLE_STATIC_LINKING=ON ../
49+
cmake --build . -j2

CMakeLists.txt

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,38 @@
1-
# We might support earlier versions, too, but try to use a recent one.
21
cmake_minimum_required(VERSION 3.8)
32

43
project(sha256)
54

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

0 commit comments

Comments
 (0)