Jelajahi Sumber

try_compile: Optionally forward custom platform variables to test project

Add a `CMAKE_TRY_COMPILE_PLATFORM_VARIABLES` variable to specify a list
of custom variables to be forwarded to a `try_compile` test project.
This will be useful for platform information modules or toolchain files
to forward some platform-specific set of variables from the host project
(perhaps set in its cache) to the test project so that it can build the
same way.
Brad King 9 tahun lalu
induk
melakukan
d256ba078a

+ 4 - 0
Help/command/try_compile.rst

@@ -115,3 +115,7 @@ a build configuration.
 
 Set the :variable:`CMAKE_TRY_COMPILE_TARGET_TYPE` variable to specify
 the type of target used for the source file signature.
+
+Set the :variable:`CMAKE_TRY_COMPILE_PLATFORM_VARIABLES` variable to specify
+variables that must be propagated into the test project.  This variable is
+meant for use only in toolchain files.

+ 1 - 0
Help/manual/cmake-variables.7.rst

@@ -298,6 +298,7 @@ Variables that Control the Build
    /variable/CMAKE_STATIC_LINKER_FLAGS_CONFIG
    /variable/CMAKE_STATIC_LINKER_FLAGS
    /variable/CMAKE_TRY_COMPILE_CONFIGURATION
+   /variable/CMAKE_TRY_COMPILE_PLATFORM_VARIABLES
    /variable/CMAKE_TRY_COMPILE_TARGET_TYPE
    /variable/CMAKE_USE_RELATIVE_PATHS
    /variable/CMAKE_VISIBILITY_INLINES_HIDDEN

+ 7 - 0
Help/release/dev/try_compile-custom-variables.rst

@@ -0,0 +1,7 @@
+try_compile-custom-variables
+----------------------------
+
+* A :variable:`CMAKE_TRY_COMPILE_PLATFORM_VARIABLES` variable was
+  added for use by toolchain files to specify platform-specific
+  variables that must be propagated by the :command:`try_compile`
+  command into test projects.

+ 10 - 0
Help/variable/CMAKE_TRY_COMPILE_PLATFORM_VARIABLES.rst

@@ -0,0 +1,10 @@
+CMAKE_TRY_COMPILE_PLATFORM_VARIABLES
+------------------------------------
+
+List of variables that the :command:`try_compile` command source file signature
+must propagate into the test project in order to target the same platform as
+the host project.
+
+This variable should not be set by project code.  It is meant to be set by
+CMake's platform information modules for the current toolchain, or by a
+toolchain file when used with :variable:`CMAKE_TOOLCHAIN_FILE`.

+ 9 - 0
Source/cmCoreTryCompile.cxx

@@ -41,6 +41,8 @@ static std::string const kCMAKE_POSITION_INDEPENDENT_CODE =
 static std::string const kCMAKE_SYSROOT = "CMAKE_SYSROOT";
 static std::string const kCMAKE_TRY_COMPILE_OSX_ARCHITECTURES =
   "CMAKE_TRY_COMPILE_OSX_ARCHITECTURES";
+static std::string const kCMAKE_TRY_COMPILE_PLATFORM_VARIABLES =
+  "CMAKE_TRY_COMPILE_PLATFORM_VARIABLES";
 
 int cmCoreTryCompile::TryCompileCode(std::vector<std::string> const& argv,
                                      bool isTryRun)
@@ -421,6 +423,13 @@ int cmCoreTryCompile::TryCompileCode(std::vector<std::string> const& argv,
       vars.insert(kCMAKE_POSITION_INDEPENDENT_CODE);
       vars.insert(kCMAKE_SYSROOT);
 
+      if (const char* varListStr = this->Makefile->GetDefinition(
+            kCMAKE_TRY_COMPILE_PLATFORM_VARIABLES)) {
+        std::vector<std::string> varList;
+        cmSystemTools::ExpandListArgument(varListStr, varList);
+        vars.insert(varList.begin(), varList.end());
+      }
+
       /* for the TRY_COMPILEs we want to be able to specify the architecture.
          So the user can set CMAKE_OSX_ARCHITECTURES to i386;ppc and then set
          CMAKE_TRY_COMPILE_OSX_ARCHITECTURES first to i386 and then to ppc to

+ 23 - 0
Tests/RunCMake/try_compile/PlatformVariables.cmake

@@ -0,0 +1,23 @@
+enable_language(C)
+
+# Normally this variable should be set by a platform information module or
+# a toolchain file, but for purposes of this test we simply set it here.
+set(CMAKE_TRY_COMPILE_PLATFORM_VARIABLES MY_CUSTOM_VARIABLE)
+
+set(MY_CUSTOM_VARIABLE SOME_VALUE)
+
+try_compile(result ${CMAKE_CURRENT_BINARY_DIR}
+  SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/src.c
+  OUTPUT_VARIABLE out
+  )
+if(NOT result)
+  message(FATAL_ERROR "try_compile failed:\n${out}")
+endif()
+
+# Check that the cache was populated with our custom variable.
+file(STRINGS ${CMAKE_BINARY_DIR}/CMakeFiles/CMakeTmp/CMakeCache.txt entries
+  REGEX MY_CUSTOM_VARIABLE:UNINITIALIZED=${MY_CUSTOM_VARIABLE}
+  )
+if(NOT entries)
+  message(FATAL_ERROR "try_compile did not populate cache as expected")
+endif()

+ 4 - 0
Tests/RunCMake/try_compile/RunCMakeTest.cmake

@@ -16,6 +16,10 @@ run_cmake(BadSources2)
 run_cmake(NonSourceCopyFile)
 run_cmake(NonSourceCompileDefinitions)
 
+set(RunCMake_TEST_OPTIONS --debug-trycompile)
+run_cmake(PlatformVariables)
+unset(RunCMake_TEST_OPTIONS)
+
 run_cmake(TargetTypeExe)
 run_cmake(TargetTypeInvalid)
 run_cmake(TargetTypeStatic)