diff mbox series

[3/4] package/llvm: add option to build the BPF backend in host-llvm

Message ID 20260828-xdp-tools-tleb-v1-3-463c25b5a99b@bootlin.com
State New
Headers show
Series Add xdp-tools package | expand

Commit Message

Théo Lebrun Aug. 28, 2026, 4:46 p.m. UTC
A package that cross-compiles BPF programs at build-time needs a
host-clang with BPF backend. Today, it is only possible through a
behavior that looks like a bug: if BR2_PACKAGE_LLVM_BPF=y then
`LLVM_TARGETS_TO_BUILD += BPF` and HOST_LLVM_CONF_OPTS inherits this
implicitly because of the deferred expansion of LLVM_TARGETS_TO_BUILD.

Changes:
(1) BR2_PACKAGE_LLVM_BPF=y does not imply BPF backend in host-llvm.
(2) Create a new BR2_PACKAGE_HOST_LLVM_BPF config entry.
(3) Support building host-llvm with BPF as sole backend.

About (3): make sure NOT to pass an empty -DLLVM_TARGET_ARCH= else llvm
refuses compilation. The flag must not be present for LLVM to accept
building with only the BPF backend.

Signed-off-by: Théo Lebrun <theo.lebrun@bootlin.com>
---
 package/Config.in.host                   |  1 +
 package/llvm-project/llvm/Config.in.host | 10 ++++++++++
 package/llvm-project/llvm/llvm.mk        | 12 +++++++++++-
 3 files changed, 22 insertions(+), 1 deletion(-)
diff mbox series

Patch

diff --git a/package/Config.in.host b/package/Config.in.host
index 595caf9e7f..424d3e0ac2 100644
--- a/package/Config.in.host
+++ b/package/Config.in.host
@@ -63,6 +63,7 @@  menu "Host utilities"
 	source "package/kmod/Config.in.host"
 	source "package/libp11/Config.in.host"
 	source "package/llvm-project/lld/Config.in.host"
+	source "package/llvm-project/llvm/Config.in.host"
 	source "package/lpc3250loader/Config.in.host"
 	source "package/lttng-babeltrace/Config.in.host"
 	source "package/lzma-alone/Config.in.host"
diff --git a/package/llvm-project/llvm/Config.in.host b/package/llvm-project/llvm/Config.in.host
new file mode 100644
index 0000000000..7e7294b2ad
--- /dev/null
+++ b/package/llvm-project/llvm/Config.in.host
@@ -0,0 +1,10 @@ 
+config BR2_PACKAGE_HOST_LLVM_BPF
+	bool "host llvm BPF backend"
+	help
+	  Build the BPF backend in host-llvm so that host-clang can
+	  compile eBPF objects. This is supported even with BPF as
+	  sole backend.
+
+	  Enabling this option does not build host LLVM by itself: a
+	  package depending on host-clang triggers the host LLVM/clang
+	  build.
diff --git a/package/llvm-project/llvm/llvm.mk b/package/llvm-project/llvm/llvm.mk
index fb59aa64e8..e18159166b 100644
--- a/package/llvm-project/llvm/llvm.mk
+++ b/package/llvm-project/llvm/llvm.mk
@@ -53,14 +53,16 @@  LLVM_TARGETS_TO_BUILD = RISCV
 else
 LLVM_TARGETS_TO_BUILD = $(LLVM_TARGET_ARCH)
 endif
-HOST_LLVM_CONF_OPTS += -DLLVM_TARGETS_TO_BUILD="$(subst $(space),;,$(LLVM_TARGETS_TO_BUILD))"
 LLVM_CONF_OPTS += -DLLVM_TARGETS_TO_BUILD="$(subst $(space),;,$(LLVM_TARGETS_TO_BUILD))"
 
 # LLVM target to use for native code generation. This is required for JIT generation.
 # It must be set to LLVM_TARGET_ARCH for host and target, otherwise we get
 # "No available targets are compatible for this triple" with llvmpipe when host
 # and target architectures are different.
+# host-llvm with LLVM_TARGET_ARCH empty is supported to get only the BPF backend.
+ifneq ($(LLVM_TARGET_ARCH),)
 HOST_LLVM_CONF_OPTS += -DLLVM_TARGET_ARCH=$(LLVM_TARGET_ARCH)
+endif
 LLVM_CONF_OPTS += -DLLVM_TARGET_ARCH=$(LLVM_TARGET_ARCH)
 
 # Build AMDGPU backend
@@ -72,6 +74,14 @@  ifeq ($(BR2_PACKAGE_LLVM_AMDGPU),y)
 LLVM_TARGETS_TO_BUILD += AMDGPU
 endif
 
+# Host LLVM targets. Snapshot the target list here and ignore
+# what's coming after (BPF if BR2_PACKAGE_LLVM_BPF).
+HOST_LLVM_TARGETS_TO_BUILD := $(LLVM_TARGETS_TO_BUILD)
+ifeq ($(BR2_PACKAGE_HOST_LLVM_BPF),y)
+HOST_LLVM_TARGETS_TO_BUILD += BPF
+endif
+HOST_LLVM_CONF_OPTS += -DLLVM_TARGETS_TO_BUILD="$(subst $(space),;,$(HOST_LLVM_TARGETS_TO_BUILD))"
+
 # Build BPF backend
 ifeq ($(BR2_PACKAGE_LLVM_BPF),y)
 LLVM_TARGETS_TO_BUILD += BPF