diff --git a/.github/workflows/clang.yml b/.github/workflows/clang.yml index 30d9654..232ef98 100644 --- a/.github/workflows/clang.yml +++ b/.github/workflows/clang.yml @@ -27,6 +27,17 @@ jobs: cd build cmake ../ make -j2 + - name: Build statically linked with Clang ${{ matrix.version }} + run: | + export CXX=clang++-${{ matrix.version }} + export CC=clang-${{ matrix.version }} + cd $GITHUB_WORKSPACE + mkdir build-static + cd build-static + cmake -DENABLE_LTO=ON -DENABLE_STATIC_LINKING=ON ../ + make -j2 + # Only run static build on latest version in the matrix. + if: matrix.version == 14 - name: Run tests run: | cd "$GITHUB_WORKSPACE/build" diff --git a/.github/workflows/gcc.yml b/.github/workflows/gcc.yml index b32ff73..19a9862 100644 --- a/.github/workflows/gcc.yml +++ b/.github/workflows/gcc.yml @@ -27,6 +27,17 @@ jobs: cd build cmake ../ make -j2 + - name: Build statically linked with GNU GCC ${{ matrix.version }} + run: | + export CXX=g++-${{ matrix.version }} + export CC=gcc-${{ matrix.version }} + cd $GITHUB_WORKSPACE + mkdir build-static + cd build-static + cmake -DENABLE_LTO=ON -DENABLE_STATIC_LINKING=ON ../ + make -j2 + # Only run static build on latest version in the matrix. + if: matrix.version == 11 - name: Run tests run: | cd "$GITHUB_WORKSPACE/build" diff --git a/.github/workflows/msys2.yml b/.github/workflows/msys2.yml index 6df5536..28207a3 100644 --- a/.github/workflows/msys2.yml +++ b/.github/workflows/msys2.yml @@ -37,3 +37,13 @@ jobs: export MSYSTEM=MINGW64 cd "$GITHUB_WORKSPACE/build" ctest -V + - name: Build statically linked + run: | + export MSYSTEM=MINGW64 + export CXX=g++ + export CC=gcc + cd $GITHUB_WORKSPACE + mkdir build-static + cd build-static + cmake -DENABLE_LTO=ON -DENABLE_STATIC_LINKING=ON ../ + cmake --build . -j2 diff --git a/CMakeLists.txt b/CMakeLists.txt index 7942f2f..f9377e1 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,8 +1,38 @@ -# We might support earlier versions, too, but try to use a recent one. cmake_minimum_required(VERSION 3.8) project(sha256) +# If the option ENABLE_LTO is enabled (e. g. via `cmake -DENABLE_LTO=ON`) +# during the build, then all binaries will use link-time optimization (LTO). +option(ENABLE_LTO "Enable link-time optimization" OFF) +# Not all compilers support LTO / IPO, so it has to be checked. +if (ENABLE_LTO) + cmake_policy(SET CMP0069 NEW) + include(CheckIPOSupported) + check_ipo_supported(RESULT HAS_LTO_SUPPORT OUTPUT LTO_FAIL_REASON + LANGUAGES C CXX) + if (NOT HAS_LTO_SUPPORT) + message(FATAL "IPO / LTO is not supported: ${LTO_FAIL_REASON}") + else() + message(STATUS "IPO / LTO is supported. Using it.") + endif() +endif (ENABLE_LTO) + +# If ENABLE_STATIC_LINKING is on (e. g. via `cmake -DENABLE_STATIC_LINKING=ON`), +# then all libraries are linked statically. The option is off by default. +# +# Static linking increases the size of the binaries, but those binaries do not +# need the libraries to be present on the system. +# +# WARNING: This option is still experimental and may not work completely. +option(ENABLE_STATIC_LINKING "Links all libraries statically" OFF) +if (ENABLE_STATIC_LINKING) + set(CMAKE_LINK_SEARCH_START_STATIC 1) + set(CMAKE_LINK_SEARCH_END_STATIC 1) + set(CMAKE_FIND_LIBRARY_SUFFIXES ".a") + set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -static-libgcc -static-libstdc++ -static") +endif () + # Recurse into subdirectory for sha256 executable. add_subdirectory (sha256)