LLVM cross-compilation cmake issues

Shoaib Meenai via llvm-commits llvm-commits at lists.llvm.org
Tue Oct 17 17:08:52 PDT 2017


Hi all (CC beanz for cmake advice),

I'm running into a cmake problem when I try to cross-compile a Windows-hosted
toolchain (building on Linux). I'm using clang-cl as my cross-compiler, and I
set up the compiler paths in a cmake toolchain file. Unfortunately, as part of
setting up the compiler, cmake sets the environment variables ASM, CC, CXX,
etc. to the paths it found. It's easy enough to reproduce this; just configure
the following CMakeLists.txt:

message(STATUS "CC is $ENV{CC}")
project(null C)
message(STATUS "CC is $ENV{CC}")

The first printout should be empty (assuming you don't have CC set in your
environment already, of course), and the second value should be the full path
the compiler found by cmake. Note that this will only occur on the initial
configure; subsequent configures should have the compiler setup cached, and
will therefore not set the environment variable. I've confirmed this behavior
on cmake 3.5, 3.6, and 3.9, so I'm fairly sure it's universal.

This causes problems for me because when I'm running the initial cmake
configure for my cross-compile, ASM, CC, and CXX get set to the values for my
cross-compiler (clang-cl). LLVM's build then launches the configure for the
native portions of the build (TableGen, etc.), but this configure is launched
with the aforementioned environment variables, and cmake's compiler detection
picks up on those environment variables, so it then tries to compile the
native portions with my cross-compiler, which goes about as well as you'd
expect.

My current workaround is to simply unset these environment variables before
running the native configure step:

--- a/cmake/modules/CrossCompile.cmake
+++ b/cmake/modules/CrossCompile.cmake
@@ -45,6 +45,9 @@ function(llvm_create_cross_target_internal target_name toolchain buildtype)
          # Propagate LLVM_EXTERNAL_CLANG_SOURCE_DIR so that clang-tblgen can be built
          set(external_clang_dir "-DLLVM_EXTERNAL_CLANG_SOURCE_DIR=${LLVM_EXTERNAL_CLANG_SOURCE_DIR}")
        endif()
+    unset(ENV{ASM})
+    unset(ENV{CC})
+    unset(ENV{CXX})
     execute_process(COMMAND ${CMAKE_COMMAND} ${build_type_flags}
         -G "${CMAKE_GENERATOR}" -DLLVM_TARGETS_TO_BUILD=${LLVM_TARGETS_TO_BUILD}
         ${CROSS_TOOLCHAIN_FLAGS_${target_name}} ${CMAKE_SOURCE_DIR}

It would be more proper to save the values of these environment variables at
the start of the configure (before any project command is run) and restore
those values before starting the native configure, of course. This still feels
pretty ugly though, and I'm sure I'm not the first person to run into a
similar issue. What would be the best way to fix this?

Thanks,
Shoaib



More information about the llvm-commits mailing list