-
Notifications
You must be signed in to change notification settings - Fork 516
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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).
- Loading branch information
1 parent
615720e
commit 9be7c2b
Showing
11 changed files
with
119 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
# 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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
# 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 | ||
|
||
# CMake build file to host mimalloc configuration. | ||
# This is only used to produce static binaries on Linux, as a replacement for | ||
# the default allocator in musl, which is slower. | ||
|
||
# Turn these off to save time. | ||
set(MI_BUILD_SHARED OFF) | ||
set(MI_BUILD_STATIC OFF) | ||
set(MI_BUILD_TESTS OFF) | ||
|
||
# Turn these on. They are already on by default as of the date we wrote this | ||
# file, but in case the defaults ever change, these settings are critical. | ||
set(MI_OVERRIDE ON) | ||
set(MI_BUILD_OBJECT ON) | ||
|
||
# With these set in scope of this folder, load the library's own CMakeLists.txt. | ||
add_subdirectory(source) |