Skip to content

Fix cross compilation#1

Open
Leo-Yan wants to merge 6 commits intoacmel:mainfrom
Leo-Yan:fix-riscv64-cross-build
Open

Fix cross compilation#1
Leo-Yan wants to merge 6 commits intoacmel:mainfrom
Leo-Yan:fix-riscv64-cross-build

Conversation

@Leo-Yan
Copy link

@Leo-Yan Leo-Yan commented Oct 3, 2025

When cross building riscv64, we need to specify ARCH=riscv so that this can set to $(SRCARCH) and can find the asm headers correctly.

Also install Clang and related libs.

When cross building riscv64, we need to specify ARCH=riscv so that this
can set to $(SRCARCH) and can find the asm headers correctly.

Also install Clang and related libs.

Signed-off-by: Leo Yan <leo.yan@arm.com>
Install Clang/LLVM and host packages. Remove the prefix
${CROSS_COMPILE} from clang.

Signed-off-by: Leo Yan <leo.yan@arm.com>
Install Clang/LLVM and host packages. Remove the prefix
${CROSS_COMPILE} from clang.

Signed-off-by: Leo Yan <leo.yan@arm.com>
Install Clang/LLVM and host packages. Remove the prefix
${CROSS_COMPILE} from clang.

Signed-off-by: Leo Yan <leo.yan@arm.com>
Install Clang/LLVM and host packages. Remove the prefix
${CROSS_COMPILE} from clang.

Signed-off-by: Leo Yan <leo.yan@arm.com>
@Leo-Yan Leo-Yan force-pushed the fix-riscv64-cross-build branch from 5088142 to b837a98 Compare October 6, 2025 11:02
Don't unset TARGET variable as it is used later.

Signed-off-by: Leo Yan <leo.yan@arm.com>
@Leo-Yan Leo-Yan force-pushed the fix-riscv64-cross-build branch from b837a98 to 84e39fa Compare October 6, 2025 16:08
@acmel
Copy link
Owner

acmel commented Oct 6, 2025

Hey, have you looked at the produced binaries? I'm not understanding how installing the host compilers and the host devel packages will end up producing a binary for the target arch, can you please elaborate?

@Leo-Yan
Copy link
Author

Leo-Yan commented Oct 6, 2025

Hi @acmel ,

As claimed in [1]: "... Clang/LLVM is natively a cross-compiler, meaning that one set of programs can compile to all targets by setting the -target option."

So we don't need to install extra arch specific Clang/LLVM packages, as the official Clang/LLVM can support cross compilation.

I can confirm after using Clang for cross compilation arm64, I can get the binary info:

/tmp/build/perf/perf: ELF 64-bit LSB pie executable, ARM aarch64, version 1 (SYSV), dynamically linked, interpreter /lib/ld-linux-aarch64.so.1, BuildID[sha1]=9655da4ec0f34ba91b400     b4b23aa347c99ecc6b8, for GNU/Linux 3.7.0, with debug_info, not stripped

[1] https://clang.llvm.org/docs/CrossCompilation.html

@acmel
Copy link
Owner

acmel commented Oct 6, 2025

Ok, that covers the clang, so since rx_and_build.sh already passes ARCH, no need to have the CROSS_COMPILE prefix for the clang binary, excellent.

But is ubuntu multi-lib in the sense that the devel packages have the .so files for all target architectures?

@acmel
Copy link
Owner

acmel commented Oct 7, 2025

Reading that clang.llvm.org doc on x-compilation, this part:

Another problem is that compilers come with standard libraries only
(like compiler-rt, libcxx, libgcc, libm, etc), so you’ll have to find and make
available to the build system, every other library required to build your
software, that is specific to your target. It’s not enough to have your
host’s libraries installed.

and:

Target Specific Libraries

@Leo-Yan
Copy link
Author

Leo-Yan commented Oct 7, 2025

But is ubuntu multi-lib in the sense that the devel packages have the .so files for all target architectures?

Ubuntu's multi-arch libraries can be used by both GCC and Clang. For example, if installed libtraceevent:arm64 on Ubuntu, then whether we build perf with aarch64-linux-gnu-gcc or with clang, the build will find the Arm64 version's libtraceevent.

The $(CROSS_COMPILE) variable plays an important role: it extends $PKG_CONFIG_LIBDIR (e.g., to /usr/lib/aarch64-linux-gnu/pkgconfig), which guides the Makefile to find correct headers and libraries by using pkg-config.

@Leo-Yan
Copy link
Author

Leo-Yan commented Oct 7, 2025

Just note for Android build:

So far we can use Clang provided by Android NDK to build perf binary. If build with static linkage, we should have no concern for running perf on Android.

On the other hand, except the Clang/LLVM is included in Android NDK package, it also contains sysroot and libs:

$ ls /data_nvme1n1/niayan01/android-ndk-r27d/toolchains/llvm/prebuilt/linux-x86_64/lib/
aarch64-unknown-linux-musl    i686-w64-windows-gnu  libc++.so     liblldbIntelFeatures.so  libscanbuild  python3
arm-unknown-linux-musleabihf  libbolt_rt_instr.a    libc++.so.1   liblldb.so               libunwind.so  python3.11
clang                         libc++.a              libear        libncurses.so.6          libxml2.so    x86_64-unknown-linux-gnu
i386-unknown-linux-gnu        libc++abi.a           libedit.so.0  libpanel.so.6            libxml2.so.2  x86_64-unknown-linux-musl
i686-unknown-linux-musl       libc++abi.so.1        libform.so.6  libRemarks.so            LLVMPolly.so  x86_64-w64-windows-gnu

Because Android NDK does not provide pkgconfig, so we cannot smoothly link these libs. Later we might consider to improve this a bit if have requirement.

@Leo-Yan Leo-Yan changed the title ubuntu:22.04:x-riscv64: Fix for riscv64 cross compilation Fix cross compilation Jan 22, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants