Skip to content

Commit

Permalink
Toolchain: Add support for building the userland with the mold linker
Browse files Browse the repository at this point in the history
This commit adds support for building the SerenityOS userland with the
new [mold linker].

This is not enabled by default yet; to link using mold, run the
`Toolchain/BuildMold.sh` script to build the latest release of mold, and
set the `ENABLE_MOLD_LINKER` CMake variable to ON. This option relies on
toolchain support that has been added just recently, so you might need
to rebuild your toolchain for mold to work.

[mold linker]: https://github.com/rui314/mold
  • Loading branch information
BertalanD authored and bgianfo committed Jan 24, 2022
1 parent ad649c4 commit 4055c39
Show file tree
Hide file tree
Showing 7 changed files with 60 additions and 1 deletion.
3 changes: 3 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,9 @@ endif()
add_subdirectory(AK)
add_subdirectory(Kernel)
if(NOT "${SERENITY_ARCH}" STREQUAL "aarch64")
if (ENABLE_MOLD_LINKER)
add_link_options(-fuse-ld=mold)
endif()
add_subdirectory(Userland)
add_subdirectory(Tests)
endif()
Expand Down
1 change: 1 addition & 0 deletions Documentation/AdvancedBuildInstructions.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ There are some optional features that can be enabled during compilation that are
- `ENABLE_PCI_IDS_DOWNLOAD`: downloads the [`pci.ids` database](https://pci-ids.ucw.cz/) that contains information about PCI devices at build time, if not already present. Enabled by default.
- `BUILD_LAGOM`: builds [Lagom](../Meta/Lagom/ReadMe.md), which makes various SerenityOS libraries and programs available on the host system.
- `ENABLE_KERNEL_LTO`: builds the kernel with link-time optimization.
- `ENABLE_MOLD_LINKER`: builds the userland with the [`mold` linker](https://github.com/rui314/mold). `mold` can be built by running `Toolchain/BuildMold.sh`.
- `INCLUDE_WASM_SPEC_TESTS`: downloads and includes the WebAssembly spec testsuite tests. In order to use this option, you will need to install `prettier` and `wabt`. wabt version 1.0.23 or higher is required to pre-process the WebAssembly spec testsuite.
- `SERENITY_TOOLCHAIN`: Specifies whether to use the established GNU toolchain, or the experimental Clang-based toolchain for building SerenityOS. See the [Clang-based toolchain](#clang-based-toolchain) section below.
- `SERENITY_ARCH`: Specifies which architecture to build for. Currently supported options are `i686` and `x86_64`. `x86_64` requires a separate toolchain build from `i686`.
Expand Down
1 change: 1 addition & 0 deletions Meta/CMake/serenity_options.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@ serenity_option(ENABLE_KERNEL_ADDRESS_SANITIZER OFF CACHE BOOL "Enable kernel ad
serenity_option(ENABLE_KERNEL_COVERAGE_COLLECTION OFF CACHE BOOL "Enable KCOV and kernel coverage instrumentation in gcc/clang")
serenity_option(ENABLE_KERNEL_LTO OFF CACHE BOOL "Build the kernel with link-time optimization")
serenity_option(ENABLE_EXTRA_KERNEL_DEBUG_SYMBOLS OFF CACHE BOOL "Enable -Og and -ggdb3 options for Kernel code for easier debugging")
serenity_option(ENABLE_MOLD_LINKER OFF CACHE BOOL "Link the SerenityOS userland with the mold linker")
4 changes: 3 additions & 1 deletion Meta/CMake/utils.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,9 @@ function(serenity_libc target_name fs_name)
target_link_libraries(${target_name} clang_rt.builtins)
# FIXME: Implement -static-libstdc++ in the next toolchain update.
target_link_options(${target_name} PRIVATE -nostdlib++ -Wl,-Bstatic -lc++ -Wl,-Bdynamic)
target_link_options(${target_name} PRIVATE -Wl,--no-dependent-libraries)
if (NOT ENABLE_MOLD_LINKER)
target_link_options(${target_name} PRIVATE -Wl,--no-dependent-libraries)
endif()
endif()
target_link_directories(LibC PUBLIC ${CMAKE_CURRENT_BINARY_DIR})
serenity_generated_sources(${target_name})
Expand Down
4 changes: 4 additions & 0 deletions Toolchain/BuildClang.sh
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,10 @@ pushd "$DIR/Build/clang"
done
popd

pushd "$DIR/Local/clang/bin/"
buildstep "mold_symlink" ln -s ../../mold/bin/mold ld.mold
popd

# === SAVE TO CACHE ===

pushd "$DIR"
Expand Down
4 changes: 4 additions & 0 deletions Toolchain/BuildIt.sh
Original file line number Diff line number Diff line change
Expand Up @@ -399,6 +399,10 @@ pushd "$DIR/Build/$ARCH"

popd

pushd "$DIR/Local/$ARCH/$ARCH-pc-serenity/bin"
buildstep "mold_symlink" ln -s ../../../mold/bin/mold ld.mold
popd


# == SAVE TO CACHE ==

Expand Down
44 changes: 44 additions & 0 deletions Toolchain/BuildMold.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#!/usr/bin/env bash

# This script builds the mold linker that can optionally be used for linking
# the SerenityOS userland.
set -e

DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"

NPROC="nproc"
SYSTEM_NAME="$(uname -s)"

if [ "$SYSTEM_NAME" = "OpenBSD" ]; then
NPROC="sysctl -n hw.ncpuonline"
elif [ "$SYSTEM_NAME" = "FreeBSD" ]; then
NPROC="sysctl -n hw.ncpu"
elif [ "$SYSTEM_NAME" = "Darwin" ]; then
NPROC="sysctl -n hw.ncpu"
fi

[ -z "$MAKEJOBS" ] && MAKEJOBS=$($NPROC)

mkdir -p "$DIR"/Tarballs
pushd "$DIR"/Tarballs

if [ "$1" = "--git" ]; then
[ ! -d mold ] && git clone https://github.com/rui314/mold.git

cd mold

git pull
else
VERSION=1.0.2
[ ! -e mold-$VERSION.tar.gz ] && curl -L "https://github.com/rui314/mold/archive/refs/tags/v$VERSION.tar.gz" -o mold-$VERSION.tar.gz
[ ! -e mold-$VERSION ] && tar -xzf mold-$VERSION.tar.gz
cd mold-$VERSION
fi

make clean
export DESTDIR="$DIR"/Local/mold
export PREFIX=
make -j "$MAKEJOBS"
make install

popd

0 comments on commit 4055c39

Please sign in to comment.