Skip to content

Commit cebdb73

Browse files
jpbruckeranakryiko
authored andcommitted
tools: Help cross-building with clang
Cross-compilation with clang uses the -target parameter rather than a toolchain prefix. Just like the kernel Makefile, add that parameter to CFLAGS when CROSS_COMPILE is set. Unlike the kernel Makefile, we use the --sysroot and --gcc-toolchain options because unlike the kernel, tools require standard libraries. Commit c91d4e4 ("Makefile: Remove '--gcc-toolchain' flag") provides some background about --gcc-toolchain. Normally clang finds on its own the additional utilities and libraries that it needs (for example GNU ld or glibc). On some systems however, this autodetection doesn't work. There, our only recourse is asking GCC directly, and pass the result to --sysroot and --gcc-toolchain. Of course that only works when a cross GCC is available. Autodetection worked fine on Debian, but to use the aarch64-linux-gnu toolchain from Archlinux I needed both --sysroot (for crt1.o) and --gcc-toolchain (for crtbegin.o, -lgcc). The --prefix parameter wasn't needed there, but it might be useful on other distributions. Use the CLANG_CROSS_FLAGS variable instead of CLANG_FLAGS because it allows tools such as bpftool, that need to build both host and target binaries, to easily filter out the cross-build flags from CFLAGS. Signed-off-by: Jean-Philippe Brucker <[email protected]> Signed-off-by: Andrii Nakryiko <[email protected]> Acked-by: Quentin Monnet <[email protected]> Acked-by: Nick Desaulniers <[email protected]> Link: https://lore.kernel.org/bpf/[email protected]
1 parent f979823 commit cebdb73

File tree

1 file changed

+12
-1
lines changed

1 file changed

+12
-1
lines changed

tools/scripts/Makefile.include

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,18 @@ LLVM_STRIP ?= llvm-strip
8787

8888
ifeq ($(CC_NO_CLANG), 1)
8989
EXTRA_WARNINGS += -Wstrict-aliasing=3
90-
endif
90+
91+
else ifneq ($(CROSS_COMPILE),)
92+
CLANG_CROSS_FLAGS := --target=$(notdir $(CROSS_COMPILE:%-=%))
93+
GCC_TOOLCHAIN_DIR := $(dir $(shell which $(CROSS_COMPILE)gcc))
94+
ifneq ($(GCC_TOOLCHAIN_DIR),)
95+
CLANG_CROSS_FLAGS += --prefix=$(GCC_TOOLCHAIN_DIR)$(notdir $(CROSS_COMPILE))
96+
CLANG_CROSS_FLAGS += --sysroot=$(shell $(CROSS_COMPILE)gcc -print-sysroot)
97+
CLANG_CROSS_FLAGS += --gcc-toolchain=$(realpath $(GCC_TOOLCHAIN_DIR)/..)
98+
endif # GCC_TOOLCHAIN_DIR
99+
CFLAGS += $(CLANG_CROSS_FLAGS)
100+
AFLAGS += $(CLANG_CROSS_FLAGS)
101+
endif # CROSS_COMPILE
91102

92103
# Hack to avoid type-punned warnings on old systems such as RHEL5:
93104
# We should be changing CFLAGS and checking gcc version, but this

0 commit comments

Comments
 (0)