Towards convenient cross compiling with CMake

Hello,

Today some new features were merged to CMake master to interface with some
Clang features.

http://www.cmake.org/cmake/help/git-master/variable/CMAKE_LANG_COMPILER_TARGET.html

CMAKE_<LANG>_COMPILER_EXTERNAL_TOOLCHAIN — CMake 3.24.20220824-g95dd428 Documentation

CMAKE_SYSROOT — CMake 3.24.20220824-g95dd428 Documentation

http://cmake.org/gitweb?p=cmake.git;a=commitdiff;h=76552d59

http://cmake.org/gitweb?p=cmake.git;a=commitdiff;h=5096967e

http://cmake.org/gitweb?p=cmake.git;a=commitdiff;h=7cd65c97

This simplifies the toolchain to something like this:

  set(CMAKE_SYSTEM_NAME Linux)

  set(CMAKE_SYSROOT /home/stephen/rpi/rasp-pi-rootfs)
  set(CMAKE_FIND_ROOT_PATH /home/stephen/dev/src/qtbase-rpi/extprefix)
  set(gcc_toolchain /home/stephen/rpi/gcc-4.7-linaro-rpi-gnueabihf)

  set(triple arm-linux-gnueabihf)

  set(CMAKE_C_COMPILER /usr/bin/clang)
  set(CMAKE_C_COMPILER_TARGET ${triple})
  set(CMAKE_C_COMPILER_EXTERNAL_TOOLCHAIN ${gcc_toolchain})
  set(CMAKE_CXX_COMPILER /usr/bin/clang++)
  set(CMAKE_CXX_COMPILER_TARGET ${triple})
  set(CMAKE_CXX_COMPILER_EXTERNAL_TOOLCHAIN ${gcc_toolchain})

  # Debian bug 708744
  include_directories(SYSTEM "${CMAKE_SYSROOT}/usr/include/${triple}")

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

As an alternative to specifying the -target, CMake master also knows how to
use symlinked ${triple}-clang executables.

Find appropriate binutils when cross-compiling with clang (b84f5c2e) · Commits · CMake / CMake · GitLab

The commit at

https://gitorious.org/cmake/steveires-cmake/commit/40f5fd5ac

will be merged when Clang 3.4 is released.

I have not been able to make cross-compiling work with my ubuntu supplied
clang, nor with the version at http://llvm.org/apt/ (various issues related
to binutils which I've posted to the list about before). It works with my
self-build of trunk though. The Debian clang maintainer told me on IRC that
cross-compiling is not a target use-case for his packages. If the
llvm.org/apt packages also use his patches, that would explain it.

I would appreciate if you try it out with your cross-compiling setups and
give feedback about how well it works. I am also interested in what your
toolchain file looks like generally, and what else could/should be
abstracted by CMake generally, or with user configuration in the toolchain
file.

Thanks,

Steve.