Browse Source

Check native compiler toolchain is available before proceeding.
Some cross-compiling build tree configuration needs native compiler toolchain for host tool building via ExternalProject_Add(). This additional check ensures the native compiler toolchain is available up front before the build tree is even generated. Close #1300.

Yao Wei Tjong 姚伟忠 9 years ago
parent
commit
bbbbdb0108

+ 11 - 0
CMake/Modules/CheckCompilerToolchain.cmake

@@ -162,3 +162,14 @@ foreach (VAR NATIVE_64BIT HAVE_MMX HAVE_3DNOW HAVE_SSE HAVE_SSE2 HAVE_ALTIVEC)
         set (${VAR} 0)
     endif ()
 endforeach ()
+
+# When cross-compiling, this macro ensures that a native compiler toolchain also exists for building the host tool targets
+macro (check_native_compiler_exist)
+    file (WRITE ${CMAKE_BINARY_DIR}/generated/CMakeLists.txt "message (\"Probing native compiler toolchain...\")\n")
+    execute_process (COMMAND ${CMAKE_COMMAND} -E env CC=${SAVED_CC} CXX=${SAVED_CXX} ${CMAKE_COMMAND} -G${CMAKE_GENERATOR} .
+        WORKING_DIRECTORY ${CMAKE_BINARY_DIR}/generated RESULT_VARIABLE EXIT_CODE ERROR_VARIABLE ERR_VAR OUTPUT_QUIET)
+    if (NOT EXIT_CODE EQUAL 0)
+        execute_process (COMMAND ${CMAKE_COMMAND} -E remove ${CMAKE_BINARY_DIR}/generated/CMakeCache.txt)
+        message (FATAL_ERROR "Could not find native compiler toolchain. This is usually caused by wrong PATH env-var value.\n${ERR_VAR}")
+    endif ()
+endmacro ()

+ 4 - 0
CMake/Toolchains/android.toolchain.cmake

@@ -222,6 +222,10 @@ if( DEFINED CMAKE_CROSSCOMPILING )
  return()
 endif()
 
+# Urho3D: save the original values of CC and CXX environment variables before they get altered by CMake in the current process (and all the subprocesses later)
+set (SAVED_CC $ENV{CC})
+set (SAVED_CXX $ENV{CXX})
+
 # Urho3D: on Windows Cygwin-based NDK tools may fail in the linking phase with too long command line. Turn on response files to avoid this
 if( CMAKE_HOST_WIN32 )
  set( CMAKE_C_USE_RESPONSE_FILE_FOR_OBJECTS 1 )

+ 4 - 0
CMake/Toolchains/emscripten.toolchain.cmake

@@ -24,6 +24,10 @@ if (DEFINED CMAKE_CROSSCOMPILING)
     return ()
 endif ()
 
+# Save the original values of CC and CXX environment variables before they get altered by CMake in the current process (and all the subprocesses later)
+set (SAVED_CC $ENV{CC})
+set (SAVED_CXX $ENV{CXX})
+
 if (CMAKE_TOOLCHAIN_FILE)
     # Reference toolchain variable to suppress "unused variable" warning
     mark_as_advanced (CMAKE_TOOLCHAIN_FILE)

+ 4 - 0
CMake/Toolchains/mingw.toolchain.cmake

@@ -24,6 +24,10 @@ if (DEFINED CMAKE_CROSSCOMPILING)
     return ()
 endif ()
 
+# Save the original values of CC and CXX environment variables before they get altered by CMake in the current process (and all the subprocesses later)
+set (SAVED_CC $ENV{CC})
+set (SAVED_CXX $ENV{CXX})
+
 if (CMAKE_TOOLCHAIN_FILE)
     # Reference toolchain variable to suppress "unused variable" warning
     mark_as_advanced (CMAKE_TOOLCHAIN_FILE)

+ 4 - 0
CMake/Toolchains/raspberrypi.toolchain.cmake

@@ -24,6 +24,10 @@ if (DEFINED CMAKE_CROSSCOMPILING)
     return ()
 endif ()
 
+# Save the original values of CC and CXX environment variables before they get altered by CMake in the current process (and all the subprocesses later)
+set (SAVED_CC $ENV{CC})
+set (SAVED_CXX $ENV{CXX})
+
 if (CMAKE_TOOLCHAIN_FILE)
     # Reference toolchain variable to suppress "unused variable" warning
     mark_as_advanced (CMAKE_TOOLCHAIN_FILE)

+ 1 - 0
Source/ThirdParty/LuaJIT/CMakeLists.txt

@@ -375,6 +375,7 @@ endif ()
 # Makefile: Make targets
 # The host tool must be built natively
 if (CMAKE_CROSSCOMPILING)
+    check_native_compiler_exist ()
     # Escape the variables
     foreach (ESCAPED_VAR HOST_XCFLAGS TARGET_ARCH DASM_FLAGS DASM_ARCH)
         string (REPLACE -D +D ${ESCAPED_VAR} "${${ESCAPED_VAR}}")

+ 1 - 0
Source/Tools/CMakeLists.txt

@@ -38,6 +38,7 @@ endif ()
 
 # Build PackageTool using host compiler toolchain
 if (CMAKE_CROSSCOMPILING AND URHO3D_PACKAGING)
+    check_native_compiler_exist ()
     # When cross-compiling, build the host tool as external project
     include (ExternalProject)
     if (IOS)

+ 1 - 0
Source/Urho3D/CMakeLists.txt

@@ -167,6 +167,7 @@ endif ()
 if (URHO3D_LUA)
     # The host tool must be built natively
     if (CMAKE_CROSSCOMPILING OR URHO3D_LUAJIT)
+        check_native_compiler_exist ()
         # When cross-compiling or using LuaJIT, build the tolua++ host tool as external project using normal Lua (there is not much point using LuaJIT for the tool building even when technically it can)
         include (ExternalProject)
         if (IOS)