Files
shaka-packager/packager/fully-static.cmake
Joey Parrish 9be7c2b1ac feat: Portable, fully-static release executables on Linux (#1351)
This adds the option FULLY_STATIC to create fully-static executables.

To create portable, fully-static release executables on Linux, we need
to use musl instead of glibc. Static executables from glibc are not
portable.

The popular musl-gcc wrapper does not support C++, so instead we use a
full musl cross-compiler toolchain in the build workflow.

To build FULLY_STATIC, the user must point to the appropriate
cross-compiler, as we do in the workflow. On systems where musl is the
native libc (such as Alpine Linux), this is not necessary.

I have also read that musl's allocator is not very fast in
multi-threaded applications. So when FULLY_STATIC is enabled, we will
also enable mimalloc, a replacement allocator that is very fast.

I tested a very basic packaging command to compare speeds of dynamic
glibc, static musl, and static musl+mimalloc:

dynamic glibc:
runs: 2.527, 2.798, 2.703, 2.756, 2.959
avg = 2.749, std dev = 0.156s

static musl:
runs: 2.813, 2.920, 3.129, 3.003, 2.738
avg = 2.921s, std dev = 0.154s

static musl+mimalloc:
runs: 2.291, 2.034, 2.415, 2.303, 2.265
avg = 2.262s, std dev = 0.140s

The mimalloc build is 82% faster than musl default allocator, 77% faster
than glibc, and has more consistent runtime characteristics (lower
standard deviation).
2024-02-27 10:47:04 -08:00

28 lines
1.2 KiB
CMake

# Copyright 2024 Google LLC. All rights reserved.
#
# Use of this source code is governed by a BSD-style
# license that can be found in the LICENSE file or at
# https://developers.google.com/open-source/licenses/bsd
# Fully-static build settings.
if(FULLY_STATIC)
# This is the "object" version of mimalloc, as opposed to the library
# version. This is important for a static override of malloc and friends.
set(EXTRA_EXE_LIBRARIES $<TARGET_OBJECTS:mimalloc-obj>)
# Keep the linker from searching for dynamic libraries.
set(CMAKE_LINK_SEARCH_START_STATIC OFF)
set(CMAKE_LINK_SEARCH_END_STATIC OFF)
# Tell CMake not to plan to relink the executables, which wouldn't make sense
# in this context and causes CMake to fail at configure time when using a
# musl toolchain for static builds.
set(CMAKE_SKIP_BUILD_RPATH ON)
# Set extra linker options necessary for fully static linking. These apply
# to all executables, which is critical when using a musl toolchain. Without
# applying these to all executables, we could create dynamic musl executables
# as intermediate outputs, which then could not run on a glibc host system.
add_link_options(-static-libgcc -static-libstdc++ -static)
endif()