conan-io / conan

Conan - The open-source C and C++ package manager
https://conan.io
MIT License
8.11k stars 960 forks source link

[question] Cross compiling with Clang #15034

Open johningve opened 10 months ago

johningve commented 10 months ago

What is your question?

Hi! I want to cross compile for an armv8 Linux host using clang on Linux.

I have tried to follow the documentation, but I'm having the problem that several recipes are built for x86_64 instead. I suspect this is because my clang compiler supports both x86_64 (the default) and armv8, and the target needs to be specified using the --target flag. However, if I add this flag to my build profile via CXXFLAGS it breaks other recipes that need to build native tools.

I noticed that the documentation seems to be assuming "gcc-style" cross compilation where you have a separate compiler named aarch64-linux-gnu-gcc or something. Does conan actually support this "clang-style" cross compiling?

I have set up the profile like this:

File (linux-base)

C_COMPILER_NAME=clang
CXX_COMPILER_NAME=clang++
COMPILER_VERSION=16
[settings]
os=Linux
compiler=$C_COMPILER_NAME
compiler.version=$COMPILER_VERSION
compiler.libcxx=libstdc++11
[options]
[build_requires]
[env]
CC=$C_COMPILER_NAME-$COMPILER_VERSION
CXX=$CXX_COMPILER_NAME-$COMPILER_VERSION

[conf]
tools.build:jobs=18

File (linux-base-armv8)

include(./linux-base)

target_host=aarch64-linux-gnu
toolchain=/usr/aarch64-linux-gnu

[settings]
arch=armv8

[env]
CONAN_CMAKE_FIND_ROOT_PATH=$toolchain  # Optional, for CMake to find things in that folder
CONAN_CMAKE_SYSROOT=$toolchain  # Optional, if we want to define sysroot

I am using conan 1.61 for now, but may consider updating to 2.0 if that supports my use case.

Have you read the CONTRIBUTING guide?

jwillikers commented 10 months ago

This should be possible. I'd recommend using the conf variables instead though to set the cross compiler, CMake toolchain, and compiler / linker flags as needed. See here.

y9luiz commented 2 months ago

Hi, any update on this? I've a similar issue, I'm trying to cross compiling a project from a mac system to a raspberry. The project uses conan as package manager and I would like to build it using clang

franramirez688 commented 2 months ago

Hi @johningve @y9luiz

Sorry for the long delay 😞

Cross-compiling with Clang should not be an issue at all, but it could be challenging if you are still using the legacy toolchains and build-helpers. We recommend migrating to the modern tools (see more info here), and, if possible, to Conan v2 for sure.

For instance:

host_profile

[settings]
arch=armv8
os=Linux
compiler=clang
compiler.version=16
compiler.libcxx=libstdc++11

[buildenv]
# extra env flags: AR, AS, LD, ...

[conf]
tools.build:cxxflags=["--flag1", "--flag2"]
tools.build:cflags=["--flag3", "--flag4"]
tools.build:compiler_executables={"c": "clang-16", "cpp": "clang++-16"}
tools.build:sysroot=/usr/aarch64-linux-gnu

I also recommend reading the cross-building section with Conan v2.