[cmake-developers] Cross-compiling with clang

Stephen Kelly steveire at gmail.com
Tue Jun 11 07:35:34 EDT 2013


Hi there,

Here is a toolchain file that I can use (after a few pending merges to 
CMake) to cross compile things for the RaspberryPi with clang:

 set(CMAKE_SYSTEM_NAME Linux)

 set(CMAKE_SYSROOT "/home/stephen/rpi/rasp-pi-rootfs/")

 set(triple arm-linux-gnueabihf)
 set(CMAKE_LIBRARY_ARCHITECTURE ${triple})
 set(CMAKE_C_COMPILER_TARGET ${triple})
 set(CMAKE_CXX_COMPILER_TARGET ${triple})

 set(CMAKE_C_COMPILER "/usr/bin/clang")
 set(CMAKE_CXX_COMPILER "/usr/bin/clang++")

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

 # Clang workarounds
 set(toolchain_lib_dir_1 "/usr/lib/gcc-cross/${triple}/4.7")
 set(toolchain_lib_dir_2 "/usr/${triple}/lib/")
 set(CMAKE_TOOLCHAIN_LINK_FLAGS "-B${toolchain_lib_dir_1} -
B${toolchain_lib_dir_2} -L${toolchain_lib_dir_1} -L${toolchain_lib_dir_2}")

 # CMake workarounds
 set(CMAKE_EXE_LINKER_FLAGS ${CMAKE_TOOLCHAIN_LINK_FLAGS} CACHE INTERNAL 
"exe link flags")
 set(CMAKE_MODULE_LINKER_FLAGS ${CMAKE_TOOLCHAIN_LINK_FLAGS} CACHE INTERNAL 
"module link flags")
 set(CMAKE_SHARED_LINKER_FLAGS ${CMAKE_TOOLCHAIN_LINK_FLAGS} CACHE INTERNAL 
"shared link flags")

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



The clang workarounds are needed because otherwise it does not know where to 
find the C runtime files:

 http://thread.gmane.org/gmane.comp.compilers.clang.devel/28960/focus=29445

I still think there may be sense in patching clang, but I have not yet had 
success with that. One patch which I need to get back to at some point is

 http://thread.gmane.org/gmane.comp.compilers.clang.scm/73551/focus=73568

I don't understand why debian is putting cross-compile directories in the 
gcc-cross/ dir instead of gcc/. I'll try to find that out first.

The CMake workarounds are needed because I don't think there's a convenient 
alternative way to specify the -L and -B flags that clang needs. If it makes 
sense, I can see about adding a patch for specific 
CMAKE_TOOLCHAIN_LINK_FLAGS support to CMake. 

Any comments? Is there an easier way to cross compile with clang and cmake 
which I am just missing?

Thanks,

Steve.





More information about the cmake-developers mailing list