瀏覽代碼

Warn if CMAKE_CROSSCOMPILING is true without CMAKE_SYSTEM_NAME set

Also clarify the `CMAKE_CROSSCOMPILING` documentation to state that
CMake sets the variable automatically.
Martin Storsjö 2 年之前
父節點
當前提交
4d23636694

+ 5 - 5
Help/variable/CMAKE_CROSSCOMPILING.rst

@@ -1,15 +1,15 @@
 CMAKE_CROSSCOMPILING
 --------------------
 
-Intended to indicate whether CMake is cross compiling, but note limitations
-discussed below.
+This variable is set by CMake to indicate whether it is cross compiling,
+but note limitations discussed below.
 
 This variable will be set to true by CMake if the :variable:`CMAKE_SYSTEM_NAME`
 variable has been set manually (i.e. in a toolchain file or as a cache entry
 from the :manual:`cmake <cmake(1)>` command line). In most cases, manually
-setting :variable:`CMAKE_SYSTEM_NAME` will only be done when cross compiling,
-since it will otherwise be given the same value as
-:variable:`CMAKE_HOST_SYSTEM_NAME` if not manually set, which is correct for
+setting :variable:`CMAKE_SYSTEM_NAME` will only be done when cross compiling
+since, if not manually set, it will be given the same value as
+:variable:`CMAKE_HOST_SYSTEM_NAME`, which is correct for
 the non-cross-compiling case. In the event that :variable:`CMAKE_SYSTEM_NAME`
 is manually set to the same value as :variable:`CMAKE_HOST_SYSTEM_NAME`, then
 ``CMAKE_CROSSCOMPILING`` will still be set to true.

+ 7 - 0
Modules/CMakeDetermineSystem.cmake

@@ -176,6 +176,13 @@ else()
     set(CMAKE_SYSTEM_VERSION "${CMAKE_HOST_SYSTEM_VERSION}")
   endif()
   set(CMAKE_SYSTEM_PROCESSOR "${CMAKE_HOST_SYSTEM_PROCESSOR}")
+  if(CMAKE_CROSSCOMPILING)
+    message(AUTHOR_WARNING
+      "CMAKE_CROSSCOMPILING has been set by the project, toolchain file, or user.  "
+      "CMake is resetting it to false because CMAKE_SYSTEM_NAME was not set.  "
+      "To indicate cross compilation, only CMAKE_SYSTEM_NAME needs to be set."
+      )
+  endif()
   set(CMAKE_CROSSCOMPILING FALSE)
   set(PRESET_CMAKE_SYSTEM_NAME FALSE)
 endif()

+ 1 - 0
Tests/RunCMake/ToolchainFile/RunCMakeTest.cmake

@@ -14,6 +14,7 @@ run_cmake_toolchain(LinkFlagsInit)
 run_cmake_toolchain(CMP0126-NEW)
 run_cmake_toolchain(CMP0126-OLD)
 run_cmake_toolchain(CMP0126-WARN)
+run_cmake_toolchain(SetCrossCompiling)
 
 function(run_IncludeDirectories)
   run_cmake_toolchain(IncludeDirectories)

+ 8 - 0
Tests/RunCMake/ToolchainFile/SetCrossCompiling-stderr.txt

@@ -0,0 +1,8 @@
+^CMake Warning \(dev\) at [^
+]*/Modules/CMakeDetermineSystem.cmake:[0-9]+ \(message\):
+  CMAKE_CROSSCOMPILING has been set by the project, toolchain file, or user\.
+  CMake is resetting it to false because CMAKE_SYSTEM_NAME was not set\.  To
+  indicate cross compilation, only CMAKE_SYSTEM_NAME needs to be set\.
+Call Stack \(most recent call first\):
+  CMakeLists\.txt:[0-9]+ \(project\)
+This warning is for project developers\.  Use -Wno-dev to suppress it\.$

+ 1 - 0
Tests/RunCMake/ToolchainFile/SetCrossCompiling-toolchain.cmake

@@ -0,0 +1 @@
+set(CMAKE_CROSSCOMPILING TRUE)

+ 0 - 0
Tests/RunCMake/ToolchainFile/SetCrossCompiling.cmake