musllvm

musl and LLVM cross-toolchain build system
git clone https://git.daat.foo/musllvm.git
Log | Files | Refs | README | LICENSE

README.md (8870B)


      1 musllvm
      2 ========
      3 
      4 A simple make build system for a musl Clang/LLVM cross toolchain.
      5 Targets x86_64 and aarch64 Linux.
      6 
      7 The toolchain includes musl, compiler-rt, libc++, libc++abi,
      8 LLVM libunwind and libatomic. Compiler tools run on the build host.
      9 
     10 
     11 downloads
     12 =========
     13 
     14 [Releases](https://github.com/cowmonk/musllvm/releases) provide archives
     15 for x86_64-linux-musl and aarch64-linux-musl. The compiler tools link
     16 musl statically and run on x86_64 Linux, including musl and glibc systems.
     17 Target programs use musl. The wrappers need a shell and the usual POSIX tools.
     18 
     19 Download an archive and SHA256SUMS from the same release, then check
     20 the archive you downloaded:
     21 
     22     sha256sum --ignore-missing -c SHA256SUMS
     23     tar -xJf musllvm-x86_64-linux-musl-host-x86_64-linux.tar.xz
     24     eval "$(musllvm-x86_64-linux-musl/bin/x86_64-linux-musl-env)"
     25 
     26 Each archive includes its source pins, patches, licenses and build information
     27 under share/musllvm/. pkg-config is optional and uses the host installation.
     28 
     29 
     30 dependencies
     31 ============
     32 
     33 Build on a Linux host with:
     34 
     35     clang/llvm    Clang 21 or newer, clang++, LLD, LLVM utilities and
     36                   matching llvm-config and LLVM CMake metadata (system mode
     37                   or a static build using the system LLVM bootstrap)
     38     make          GNU make
     39     cmake         3.20 or newer
     40     ninja
     41     python3
     42     perl
     43     patch         GNU patch
     44     curl
     45     tar           gzip and xz support
     46     sha256sum
     47     rsync
     48 
     49 pkg-config is needed for the target pkg-config wrapper. qemu-aarch64 is
     50 optional for running aarch64 checks on an x86_64 host.
     51 
     52 
     53 building
     54 ========
     55 
     56     make doctor
     57     make
     58 
     59 Defaults to x86_64 with two build jobs. For aarch64:
     60 
     61     make ARCH=aarch64
     62 
     63 LLVM_MODE=system uses the installed host tools and builds the target
     64 libraries from pinned sources. Keep the host LLVM installation available.
     65 
     66 To also build Clang, LLD and the LLVM utilities from source:
     67 
     68     make LLVM_MODE=source ARCH=x86_64 JOBS=2 LINK_JOBS=1 \
     69         PREFIX="$PWD/build/x86_64-source" BUILD_DIR="$PWD/build/work/x86_64-source"
     70 
     71 Source mode installs host tools under PREFIX/host. These tools use the
     72 host libc.
     73 
     74 For compiler tools that link musl, libc++, libc++abi, libunwind and
     75 compiler-rt statically:
     76 
     77     make LLVM_MODE=static ARCH=x86_64 JOBS=2 LINK_JOBS=1 \
     78         PREFIX="$PWD/build/x86_64-static" BUILD_DIR="$PWD/build/work/x86_64-static"
     79 
     80 Static mode first builds musl and the C++ runtimes for the build machine
     81 under BUILD_DIR/bootstrap/. It uses that toolchain to build the final LLVM
     82 tools, then builds the requested target's sysroot. The compiler runs on
     83 the build machine's architecture; ARCH selects the target architecture.
     84 The build checks every installed host executable for a dynamic loader
     85 and shared library dependencies.
     86 
     87 To start with a working GCC C++17 toolchain, including on a musl host:
     88 
     89     make LLVM_MODE=static BOOTSTRAP_LLVM_MODE=source \
     90         HOST_CC=gcc HOST_CXX=g++ ARCH=x86_64 JOBS=2 LINK_JOBS=1 \
     91         PREFIX="$PWD/build/x86_64-static" BUILD_DIR="$PWD/build/work/x86_64-static"
     92 
     93 This also builds the initial LLVM tools from source. It takes longer than
     94 using an installed LLVM. Static host tools cannot load compiler plugins.
     95 The target sysroot still includes static and shared libraries.
     96 
     97 
     98 configuration
     99 =============
    100 
    101 Edit [config.mk](config.mk) or override settings through make.
    102 
    103     ARCH                x86_64                 x86_64 or aarch64
    104     TARGET              $(ARCH)-linux-musl     target triple
    105     PREFIX              build/$(TARGET)        toolchain installation
    106     BUILD_DIR           build/work/$(TARGET)   build files and completed stages
    107     LLVM_MODE           system                 system, source or static
    108     BOOTSTRAP_LLVM_MODE system                 system or source (static mode)
    109     JOBS                2                      compilation jobs
    110     LINK_JOBS           1                      parallel links when building LLVM
    111     TARGET_CFLAGS       -O2                    target library flags
    112     RUNNER                                     emulator executable (optional)
    113 
    114 Each target has its own prefix and work directory. Downloaded and
    115 extracted sources are shared. Build paths must have no whitespace or
    116 shell metacharacters.
    117 
    118 
    119 usage
    120 =====
    121 
    122 After building, load the toolchain into your current shell:
    123 
    124     eval "$(build/x86_64-linux-musl/bin/x86_64-linux-musl-env)"
    125     "$CC" hello.c -o hello
    126     "$CC" -static hello.c -o hello-static
    127     "$CXX" -std=c++20 hello.cpp -o hello-cxx
    128 
    129 The environment command sets CC, CXX, LLVM binutils, LD, PKG_CONFIG,
    130 TARGET, MUSLLVM_SYSROOT and CMAKE_TOOLCHAIN_FILE. Use the environment
    131 command matching your target and installation path.
    132 
    133 The compiler wrappers select the target, sysroot, LLD and target runtimes
    134 through share/musllvm/clang.cfg. They remove host header and library
    135 environment paths and ignore host Clang configuration files.
    136 
    137 The configuration also enables the Chimera Linux hardening defaults:
    138 -fstack-protector-strong, -fno-semantic-interposition, --as-needed and
    139 -z relro -z now. Command-line flags such as -fno-stack-protector,
    140 -Wl,--no-as-needed or -Wl,-z,lazy override them. The pkg-config
    141 wrapper searches the target sysroot.
    142 
    143 libc++ uses Chimera's fast hardening mode and typeinfo comparison setting.
    144 
    145 For CMake:
    146 
    147     cmake -S . -B out -G Ninja \
    148         -DCMAKE_TOOLCHAIN_FILE="$CMAKE_TOOLCHAIN_FILE"
    149     cmake --build out
    150 
    151 The sysroot is under PREFIX/sysroot, with headers in usr/include,
    152 libraries in usr/lib and the musl loader in lib. Compiler-rt is in the
    153 toolchain's Clang resource directory.
    154 
    155 Set MUSLLVM_SYSROOT to use another copy of the sysroot. Set
    156 MUSLLVM_PKG_CONFIG_PATH to add target pkg-config directories.
    157 
    158 
    159 sources and resuming
    160 ====================
    161 
    162 [sources.list](sources.list) pins archive names, URLs and SHA-256 digests.
    163 Downloads enter the cache after checksum verification.
    164 
    165     make pull
    166     make verify-sources
    167     make show-config
    168 
    169 Run the same command after a failed build to resume completed stages.
    170 Changes to the target, source pins, patches, compiler, paths, build recipes or
    171 library flags require make clean with the same ARCH, PREFIX and BUILD_DIR.
    172 Cleaning removes that toolchain and its work directory, but keeps the
    173 downloaded and extracted sources. Changing JOBS does not require cleaning.
    174 
    175 The build applies [patches](patches/README.md) before configuring each source.
    176 If a patch changes for an archive already extracted, remove that source's
    177 directory under build/src/ before rebuilding. Patch failures leave the
    178 downloaded archive available for another attempt.
    179 
    180 A running build holds a lock in BUILD_DIR/lock. After a forced termination,
    181 check that the build process has stopped before removing a leftover lock
    182 with rmdir.
    183 
    184 
    185 checks
    186 ======
    187 
    188     make check
    189 
    190 Runs shell syntax checks and driver tests with mocked tools. This does
    191 no compilation or downloads.
    192 
    193 A completed build also runs make verify. This compiles static, static-pie
    194 and dynamic C and C++ programs, directly and through the CMake toolchain
    195 file, and checks ELF architecture and runtime dependencies. It also
    196 compiles with -Werror. Programs run when the host architecture matches the
    197 target or RUNNER names an emulator.
    198 
    199     make verify ARCH=aarch64 RUNNER=qemu-aarch64
    200 
    201 [CI](.github/workflows/ci.yml) runs offline tests and both target builds.
    202 A manual workflow run can select source or static LLVM builds for both targets.
    203 
    204 
    205 publishing
    206 ==========
    207 
    208 [The release workflow](.github/workflows/release.yml) runs on pushes to
    209 main that change sources.list, config.mk, Makefile, scripts/, files/, patches/
    210 or the compiler smoke tests. This covers source version updates
    211 and new core components added to the build. README edits do not publish
    212 a release. The workflow can also be run manually from main.
    213 
    214 Both targets build with LLVM_MODE=static. Each archive is unpacked into
    215 a new location and tested before upload. CI checks the host ELF files and
    216 starts Clang, LLD and llvm-ar in a chroot containing only the toolchain.
    217 The release is published after both builds pass, with SHA256SUMS and
    218 sources.list attached.
    219 Tags use musllvm-YYYY-MM-DD-COMMIT, with the first 12 commit characters.
    220 Rerunning a published commit keeps its release assets.
    221 
    222 To package a completed static build yourself:
    223 
    224     make package LLVM_MODE=static ARCH=x86_64 \
    225         PREFIX="$PWD/build/x86_64-static" BUILD_DIR="$PWD/build/work/x86_64-static"
    226 
    227 Archives go to build/releases/. Set DIST_DIR to change the output directory.
    228 LLVM_MODE=source also supports packaging, with the build host's libc requirements.
    229 
    230 
    231 credits
    232 ========
    233 
    234 * [Chimera Linux](https://chimera-linux.org) - patches, libatomic and toolchain defaults
    235 * [CMLFS](https://github.com/dslm4515/CMLFS) - build script references
    236 * [Mussel](https://github.com/firasuke/mussel) - musl cross compiler references
    237 * [musl.cc](https://musl.cc) - cross compiler resources