conan-io / conan

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

[question] Migrate cross-compilation cmake setup to Conan 2 #16749

Open Pibben opened 1 month ago

Pibben commented 1 month ago

What is your question?

With Conan 1 i did:

conan install .. -pr:b=default -pr:h=../tools/windows-cross --build=missing cmake .. -DCMAKE_BUILD_TYPE=Release -DCMAKE_TOOLCHAIN_FILE=mingw-w64-x86_64.cmake

Where windows-cross looked like this:

[settings]
os=Windows
os_build=Linux
arch=x86_64
arch_build=x86_64
compiler=gcc
compiler.version=10
compiler.libcxx=libstdc++11
build_type=Release

[env]
CONAN_CMAKE_TOOLCHAIN_FILE=/home/per/stuff/mingw-w64-x86_64.cmake
CC=x86_64-w64-mingw32-gcc
CXX=x86_64-w64-mingw32-g++
AR=x86_64-w64-mingw32-ar
AS=x86_64-w64-mingw32-as
RANLIB=x86_64-w64-mingw32-ranlib
STRIP=x86_64-w64-mingw32-strip
RC=x86_64-w64-mingw32-windres
CONAN_CMAKE_GENERATOR=MinGW Makefiles
WINDRES=x86_64-w64-mingw32-windres

and mingw-w64-x86_64.cmake:

set(CMAKE_SYSTEM_NAME Windows)
set(TOOLCHAIN_PREFIX x86_64-w64-mingw32)

set(CMAKE_C_COMPILER ${TOOLCHAIN_PREFIX}-gcc)
set(CMAKE_CXX_COMPILER ${TOOLCHAIN_PREFIX}-g++)
set(CMAKE_Fortran_COMPILER ${TOOLCHAIN_PREFIX}-gfortran)
set(CMAKE_RC_COMPILER ${TOOLCHAIN_PREFIX}-windres)

set(CMAKE_FIND_ROOT_PATH /usr/${TOOLCHAIN_PREFIX})

set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)
set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)

Now with Conan 2 I do:

conan install .. --build=missing --output-folder conan-gen -s build_type=Release -pr:b default -pr:h ../tools/windows-cross -c tools.cmake.cmaketoolchain:user_toolchain="[\"$PWD/../tools/mingw-w64-x86_64.cmake\"]" cmake .. -DCMAKE_TOOLCHAIN_FILE=conan-gen/conan_toolchain.cmake -DCMAKE_BUILD_TYPE=Release

with windows-cross now looking like this:

[settings]
os=Windows
arch=x86_64
compiler=gcc
compiler.version=10
compiler.libcxx=libstdc++11
build_type=Release

[buildenv]
CC=x86_64-w64-mingw32-gcc
CXX=x86_64-w64-mingw32-g++
AR=x86_64-w64-mingw32-ar
AS=x86_64-w64-mingw32-as
RANLIB=x86_64-w64-mingw32-ranlib
STRIP=x86_64-w64-mingw32-strip
RC=x86_64-w64-mingw32-windres
CONAN_CMAKE_GENERATOR=MinGW Makefiles
WINDRES=x86_64-w64-mingw32-windres

I use --output-folder conan-gen because I want to have the generated files in the build directory so that I can have several cross builds in parallell

Is this the way to do it? Any suggestions welcome.

Have you read the CONTRIBUTING guide?

memsharded commented 1 month ago

Hi @Pibben

Thanks for your question

conan install .. --build=missing --output-folder conan-gen -s build_type=Release -pr:b default -pr:h ../tools/windows-cross -c tools.cmake.cmaketoolchain:user_toolchain="[\"$PWD/../tools/mingw-w64-x86_64.cmake\"]"

A few suggestions:

With the above, the flow can be:

# Note it is not even necessary to change folder, can be done in root
$ conan install . --build=missing -pr:h ../tools/windows-cross
$ cmake --preset conan-default
Pibben commented 1 month ago

Thanks for your comments!

Why is it better to put the -c confs in the profile? Besides less typing on the command line.

memsharded commented 1 month ago

Why is it better to put the -c confs in the profile? Besides less typing on the command line.

Not only less typing, it is also the cognitive overhead, avoiding errors, etc.

The profiles can be shared with conan config install, that means that you might update your mingw-w64-x86_64.cmake file, even change the filename, update its contents, and all developers and CI machines will just do conan config install + conan install . --build=missing -pr:h ../tools/windows-cross. The contents of the mingw-w64-x86_64.cmake can be shared together with the profile too in the same conan config install

Other side note, the CONAN_CMAKE_GENERATOR env-variables are no longer used in Conan2 (or in Conan 1 new integrations), the have been replaced by confs like tools.cmake.cmaketoolchain:generator