Skip to contentSkip to conversation

clang: globally disable build-time RPATHs for reproducibility

PATCH New Ross Burton 1 participant 4 messages Oct 7, 2025

lore
Share & tools
lore
#0 lore raw

PATCH 1/4 clang: globally disable build-time RPATHs for reproducibility

Various bits of the LLVM project set the build RPATH in a way that means we have non-deterministic binaries even though we should always be using relative paths.

This clearly is not working as some of the binaries get rewritten on install and have large string paddings that correlate with erased build paths.

So that we don't have to disable RPATHs in every recipe, just do it once in the common include file and remove the existing recipe-specific assignments that are now not needed.

---
 meta/recipes-devtools/clang/common-clang.inc | 4 ++++
 meta/recipes-devtools/clang/libcxx_git.bb    | 1 -
 meta/recipes-devtools/clang/lld_git.bb       | 6 +-----
 3 files changed, 5 insertions(+), 6 deletions(-)
meta/recipes-devtools/clang/common-clang.inc +4 -0
index ca3a3a08b48..f7b7a1cf9a0 100644
@@ -44,4 +44,8 @@ def get_clang_host_arch(bb, d):
 def get_clang_target_arch(bb, d):
     return get_clang_arch(bb, d, 'TARGET_ARCH')
 
+# Don't put build-time RPATHs in the binaries, whilst they'll be stripped on
+# install they cause non-deterministic binaries.
+EXTRA_OECMAKE += "-DCMAKE_BUILD_WITH_INSTALL_RPATH=ON"
+
 require common.inc
meta/recipes-devtools/clang/libcxx_git.bb +0 -1
index b0ee3925f43..c630bd973ea 100644
@@ -77,7 +77,6 @@ EXTRA_OECMAKE += "\
                   -DLLVM_RUNTIME_TARGETS=${HOST_ARCH} \
                   -DLLVM_LIBDIR_SUFFIX=${LLVM_LIBDIR_SUFFIX} \
                   -DLLVM_APPEND_VC_REV=OFF \
-                  -DCMAKE_BUILD_WITH_INSTALL_RPATH=ON \
 "
 
 EXTRA_OECMAKE:append:class-target = " \
meta/recipes-devtools/clang/lld_git.bb +1 -5
index b0261ad2cb6..ba369427909 100644
@@ -14,13 +14,9 @@ DEPENDS = "llvm-tblgen-native llvm"
 
 OECMAKE_SOURCEPATH = "${S}/lld"
 
-# Explicitly disable RPATHs as otherwise they're stipped out of the binaries,
-# and are then non-reproducible.
-#
 # Explicitly enable symlinks as the lld build doesn't call into the llvm setup
 # and turn that on.
-EXTRA_OECMAKE += "-DCMAKE_SKIP_BUILD_RPATH=ON \
-                  -DLLVM_INCLUDE_TESTS=OFF -DLLVM_USE_SYMLINKS=ON \
+EXTRA_OECMAKE += "-DLLVM_INCLUDE_TESTS=OFF -DLLVM_USE_SYMLINKS=ON \
                   -DLLVM_TABLEGEN_EXE=${STAGING_BINDIR_NATIVE}/llvm-tblgen"
 
 BBCLASSEXTEND = "native nativesdk"
Signature · 1 line
2.43.0
#1 lore raw

PATCH 2/4 llvm: add recipe for just the LLVM libraries

Whilst it's convenient to build all of the LLVM project in one big recipe, that's not ideal when we may just need LLVM on target and not the rest.

Bring back a LLVM recipe that can be used by both clang (shortly) and Rust (in the future)

Set the build type to MinSizeRel and DEBUG_LEVELFLAG to -g1 (instead of the default, -g): the LLVM debug symbols are very large (several gigabytes) and this reduces them to hundreds of megabytes.

---
 meta/conf/distro/include/maintainers.inc |   1 +
 meta/recipes-devtools/clang/llvm_git.bb  | 129 +++++++++++++++++++++++
 2 files changed, 130 insertions(+)
 create mode 100644 meta/recipes-devtools/clang/llvm_git.bb
meta/conf/distro/include/maintainers.inc +1 -0
index 0e5150895f2..d2c613b8e9d 100644
@@ -483,6 +483,7 @@ RECIPE_MAINTAINER:pn-lld = "Khem Raj <[email protected]>"
 RECIPE_MAINTAINER:pn-lldb = "Khem Raj <[email protected]>"
 RECIPE_MAINTAINER:pn-llvm-project-source-21.1.2 = "Khem Raj <[email protected]>"
 RECIPE_MAINTAINER:pn-llvm-tblgen-native = "Khem Raj <[email protected]>"
+RECIPE_MAINTAINER:pn-llvm = "Khem Raj <[email protected]>"
 RECIPE_MAINTAINER:pn-logrotate = "Yi Zhao <[email protected]>"
 RECIPE_MAINTAINER:pn-log4cplus = "Unassigned <[email protected]>"
 RECIPE_MAINTAINER:pn-lrzsz = "Unassigned <[email protected]>"
meta/recipes-devtools/clang/llvm_git.bb new file +129 -0

129 lines not on this page · raw message

Signature · 1 line
2.43.0
#2 lore raw

PATCH 3/4 clang: use llvm recipe

In reply to Ross Burton

Change this recipe to build just clang and clang-tools-extra, using the LLVM provided by the llvm recipe.

This adds an 'extra-tools' PACKAGECONFIG (enabled by default) that controls whether to build the clang-tools-extra project. This includes clang-tidy and clangd, but the compile time and size for these components is not insignificant.

Add a patch from upstream to support using native prebuilt tools (such as clang-tblgen) when building standalone.

Add a patch that is being worked on with upstream to not rebuild clang- tblgen if it has already been provided. This saves a little build time, but more importantly for us resolves a static linking/uninative problem.

Remove the dependency on clang-cross in non-native builds by just depending on llvm-native and using the un-prefixed tools directly.

---
 ...ilding-native-tools-when-cross-compi.patch |  63 ++++++
 .../clang/clang/clang-no-tblgen.patch         |  24 +++
 meta/recipes-devtools/clang/clang_git.bb      | 190 +++---------------
 meta/recipes-devtools/clang/common.inc        |   2 +
 4 files changed, 117 insertions(+), 162 deletions(-)
 create mode 100644 meta/recipes-devtools/clang/clang/0001-clang-Support-building-native-tools-when-cross-compi.patch
 create mode 100644 meta/recipes-devtools/clang/clang/clang-no-tblgen.patch
meta/recipes-devtools/clang/clang/0001-clang-Support-building-native-tools-when-cross-compi.patch new file +63 -0
new file mode 100644
index 00000000000..c4198394acf
@@ -0,0 +1,63 @@
+From 2ff1134d1d2b9f3c207e3ebd98de922f9ef6c15a Mon Sep 17 00:00:00 2001
+From: Ross Burton <[email protected]>
+Date: Wed, 24 Sep 2025 17:02:57 +0100
+Subject: [PATCH] [clang] Support building native tools when cross-compiling
+ standalone clang
+
+When cross-compiling the LLVM project as a whole (from llvm/), if it
+cannot find presupplied tools it will create a native build environment
+to build the tools it needs.
+
+However, when doing a standalone build of clang (that is, from clang/
+and linking against an existing libLLVM) this doesn't work. Instead
+a _target_ binary is built which predictably then fails.
+
+The conventional workaround for this is to build the native tools in a
+separate native compile phase and pass the paths to the cross build, for
+example see OpenEmbedded[1] or Nix[2]. But we can do better!
+
+The first problem is that LLVM_USE_HOST_TOOLS is only set in the llvm/
+CMakeLists.txt, so setup_host_tool() will never consider building a
+native binary.  This can be solved by setting LLVM_USE_HOST_TOOLS based
+on CMAKE_CROSSCOMPILING in clang/CMakeLists.txt in the standalone case.
+
+Now setup_host_tool() will try to build a native tool, but it needs
+build_native_tool() from CrossCompile.cmake, so that also needs to be
+included.
+
+Finally, the native binary then fails because there's no provider for
+the dependency "CONFIGURE_Clang_NATIVE", so use llvm_create_cross_target
+to create the native environment.
+
+These few lines mirror what the lldb CMakeLists.txt does in the
+standalone case, so there is prior art for this.
+
+[1] https://git.openembedded.org/openembedded-core/tree/meta/recipes-devtools/clang/clang_git.bb?id=e18d697e92b55e57124e80234369d46575226386#n212
+[2] https://github.com/NixOS/nixpkgs/blob/3354d448f2a26117a74638957b0131ce3da9c8c4/pkgs/development/compilers/llvm/common/tblgen.nix#L54
+
+Upstream-Status: Submitted [https://github.com/llvm/llvm-project/pull/160605]
+Signed-off-by: Ross Burton <[email protected]>
+---
+ clang/CMakeLists.txt | 6 ++++++
+ 1 file changed, 6 insertions(+)
+
+diff --git a/clang/CMakeLists.txt b/clang/CMakeLists.txt
+index 1bb73599970c..4eaa71289985 100644
+--- a/clang/CMakeLists.txt
++++ b/clang/CMakeLists.txt
+@@ -80,6 +80,12 @@ if(CLANG_BUILT_STANDALONE)
+   include(GetErrcMessages)
+   include(LLVMDistributionSupport)
+ 
++  if(CMAKE_CROSSCOMPILING)
++    set(LLVM_USE_HOST_TOOLS ON)
++    include(CrossCompile)
++    llvm_create_cross_target(Clang NATIVE "" Release)
++  endif()
++
+   set(PACKAGE_VERSION "${LLVM_PACKAGE_VERSION}")
+   set(BUG_REPORT_URL "${LLVM_PACKAGE_BUGREPORT}" CACHE STRING
+     "Default URL where bug reports are to be submitted.")
+-- 
+2.43.0
+
meta/recipes-devtools/clang/clang/clang-no-tblgen.patch new file +24 -0
new file mode 100644
index 00000000000..e89cad8dc83
@@ -0,0 +1,24 @@
+[clang] Only build clang-tblgen if it is actually needed
+
+It's possible to build clang with an existing clang-tblgen (common when
+cross-compiling, for instance) by setting CLANG_TABLEGEN_EXE.  If this
+is the case there's no need to build it, as it won't be used.
+
+Upstream-Status: Submitted [https://github.com/llvm/llvm-project/pull/161952]
+Signed-off-by: Ross Burton <[email protected]>
+
+diff --git a/clang/CMakeLists.txt b/clang/CMakeLists.txt
+index e4cb1a359620..b650b3b986f4 100644
+--- a/clang/CMakeLists.txt
++++ b/clang/CMakeLists.txt
+@@ -479,7 +479,9 @@ option(CLANG_ENABLE_HLSL "Include HLSL build products" Off)
+ # While HLSL support is experimental this should stay hidden.
+ mark_as_advanced(CLANG_ENABLE_HLSL)
+ 
+-add_subdirectory(utils/TableGen)
++if (NOT DEFINED CLANG_TABLEGEN_EXE OR CLANG_INCLUDE_TESTS)
++  add_subdirectory(utils/TableGen)
++endif()
+ 
+ # Export CLANG_TABLEGEN_EXE for use by flang docs.
+ set(CLANG_TABLEGEN_EXE "${CLANG_TABLEGEN_EXE}" CACHE INTERNAL "")
meta/recipes-devtools/clang/clang_git.bb +28 -162

293 lines not on this page · raw message

meta/recipes-devtools/clang/common.inc +2 -0
index d7f069d2533..a89831ce8f5 100644
@@ -62,6 +62,8 @@ SRC_URI = "\
     file://0039-Only-build-c-index-test-when-clang-tests-are-include.patch \
     file://0001-Install-lldb-tblgen.patch \
     file://0001-dont-expose-LLVM_HAVE_OPT_VIEWER_MODULES.patch \
+    file://0001-clang-Support-building-native-tools-when-cross-compi.patch \
+    file://clang-no-tblgen.patch \
 "
 # Fallback to no-PIE if not set
 GCCPIE ??= ""
Signature · 1 line
2.43.0
#3 lore raw

PATCH 4/4 classes/toolchain/clang: depend on llvm-native for tools, not clang

In reply to Ross Burton

The relevant tools (objdump, strip, etc) are part of llvm-native now, so trim dependencies and depend on that directly instead of clang-cross.

---
 meta/classes/toolchain/clang.bbclass | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
meta/classes/toolchain/clang.bbclass +2 -2
index f422a01988f..b4a1c180f6a 100644
@@ -33,7 +33,7 @@ TUNE_CCARGS += "${@bb.utils.contains("DISTRO_FEATURES", "usrmerge", " --dyld-pre
 LDFLAGS:append:class-nativesdk:x86-64 = " -Wl,-dynamic-linker,${base_libdir}/ld-linux-x86-64.so.2"
 LDFLAGS:append:class-nativesdk:aarch64 = " -Wl,-dynamic-linker,${base_libdir}/ld-linux-aarch64.so.1"
 
-# do_populate_sysroot needs STRIP
-POPULATESYSROOTDEPS:append:class-target = " ${MLPREFIX}clang-cross-${TARGET_ARCH}:do_populate_sysroot"
+# do_populate_sysroot needs STRIP, do_package_qa needs OBJDUMP
+POPULATESYSROOTDEPS:append:class-target = " llvm-native:do_populate_sysroot"
 
 TCOVERRIDE = "toolchain-clang"
Signature · 1 line
2.43.0