Browse Source

Merge topic 'try_compile-linker-language'

0f37000304 try_{compile,run}: add LINKER_LANGUAGE option
dc0dbffb0f Tests: Remove redundant policy setting from RunCMake.try_{compile,run} cases

Acked-by: Kitware Robot <[email protected]>
Tested-by: buildbot <[email protected]>
Merge-request: !8871
Brad King 2 years ago
parent
commit
c672e51bd3

+ 9 - 0
Help/command/try_compile.rst

@@ -77,6 +77,7 @@ Try Compiling Source Files
               [COMPILE_DEFINITIONS <defs>...]
               [LINK_OPTIONS <options>...]
               [LINK_LIBRARIES <libs>...]
+              [LINKER_LANGUAGE <lang>]
               [OUTPUT_VARIABLE <var>]
               [COPY_FILE <fileName> [COPY_FILE_ERROR <var>]]
               [<LANG>_STANDARD <std>]
@@ -184,6 +185,14 @@ The options for the above signatures are:
   set the :prop_tgt:`STATIC_LIBRARY_OPTIONS` target property in the generated
   project, depending on the :variable:`CMAKE_TRY_COMPILE_TARGET_TYPE` variable.
 
+``LINKER_LANGUAGE <lang>```
+  .. versionadded:: 3.29
+
+  Specify the :prop_tgt:`LINKER_LANGUAGE` target property of the generated
+  project.  When using multiple source files with different languages, set
+  this to the language of the source file containing the program entry point,
+  e.g., ``main``.
+
 ``LOG_DESCRIPTION <text>``
   .. versionadded:: 3.26
 

+ 1 - 0
Help/command/try_run.rst

@@ -67,6 +67,7 @@ The signature above is recommended for clarity.
           [COMPILE_DEFINITIONS <defs>...]
           [LINK_OPTIONS <options>...]
           [LINK_LIBRARIES <libs>...]
+          [LINKER_LANGUAGE <lang>]
           [COMPILE_OUTPUT_VARIABLE <var>]
           [COPY_FILE <fileName> [COPY_FILE_ERROR <var>]]
           [<LANG>_STANDARD <std>]

+ 6 - 0
Help/dev/try_compile-linker-language.rst

@@ -0,0 +1,6 @@
+try_compile-linker-language
+---------------------------
+
+* The :command:`try_compile` and :command:`try_run` commands gained a
+  ``LINKER_LANGUAGE`` option to specify the :prop_tgt:`LINKER_LANGUAGE`
+  target property in the generated test project.

+ 14 - 0
Source/cmCoreTryCompile.cxx

@@ -178,6 +178,7 @@ auto const TryCompileBaseSourcesArgParser =
           ArgumentParser::ExpectAtLeast{ 0 })
     .Bind("LINK_LIBRARIES"_s, &Arguments::LinkLibraries)
     .Bind("LINK_OPTIONS"_s, &Arguments::LinkOptions)
+    .Bind("LINKER_LANGUAGE"_s, &Arguments::LinkerLanguage)
     .Bind("COPY_FILE"_s, &Arguments::CopyFileTo)
     .Bind("COPY_FILE_ERROR"_s, &Arguments::CopyFileError)
     .BIND_LANG_PROPS(C)
@@ -1045,6 +1046,19 @@ cm::optional<cmTryCompileResult> cmCoreTryCompile::TryCompileCode(
       }
     }
 
+    if (arguments.LinkerLanguage) {
+      std::string LinkerLanguage = *arguments.LinkerLanguage;
+      if (testLangs.find(LinkerLanguage) == testLangs.end()) {
+        this->Makefile->IssueMessage(
+          MessageType::FATAL_ERROR,
+          "Linker language '" + LinkerLanguage +
+            "' must be enabled in project(LANGUAGES).");
+      }
+
+      fprintf(fout, "set_property(TARGET %s PROPERTY LINKER_LANGUAGE %s)\n",
+              targetName.c_str(), LinkerLanguage.c_str());
+    }
+
     if (arguments.LinkLibraries) {
       std::string libsToLink = " ";
       for (std::string const& i : *arguments.LinkLibraries) {

+ 1 - 0
Source/cmCoreTryCompile.h

@@ -91,6 +91,7 @@ public:
     cm::optional<ArgumentParser::MaybeEmpty<std::vector<std::string>>>
       LinkLibraries;
     ArgumentParser::MaybeEmpty<std::vector<std::string>> LinkOptions;
+    cm::optional<std::string> LinkerLanguage;
     std::map<std::string, std::string> LangProps;
     std::string CMakeInternal;
     cm::optional<std::string> OutputVariable;

+ 3 - 1
Tests/RunCMake/CMakeLists.txt

@@ -583,7 +583,9 @@ endfunction()
 add_RunCMake_test_try_compile()
 
 add_RunCMake_test(try_run -DCMAKE_SYSTEM_NAME=${CMAKE_SYSTEM_NAME}
-                          -DCMAKE_C_COMPILER_ID=${CMAKE_C_COMPILER_ID})
+                          -DCMAKE_C_COMPILER_ID=${CMAKE_C_COMPILER_ID}
+                          -DCMAKE_CXX_COMPILER_ID=${CMAKE_CXX_COMPILER_ID}
+                          -DCMAKE_Fortran_COMPILER_ID=${CMAKE_Fortran_COMPILER_ID})
 add_RunCMake_test(set)
 add_RunCMake_test(variable_watch)
 add_RunCMake_test(while)

+ 0 - 3
Tests/RunCMake/try_compile/LinkOptions.cmake

@@ -1,8 +1,5 @@
-
 enable_language(C)
 
-cmake_policy(SET CMP0054 NEW)
-
 set (lib_name "${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_STATIC_LIBRARY_PREFIX}lib${CMAKE_STATIC_LIBRARY_SUFFIX}")
 if (CMAKE_SYSTEM_NAME STREQUAL "Windows")
   if (RunCMake_C_COMPILER_ID STREQUAL "MSVC"

+ 0 - 3
Tests/RunCMake/try_run/LinkOptions.cmake

@@ -1,8 +1,5 @@
-
 enable_language(C)
 
-cmake_policy(SET CMP0054 NEW)
-
 set (lib_name "${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_STATIC_LIBRARY_PREFIX}lib${CMAKE_STATIC_LIBRARY_SUFFIX}")
 if (CMAKE_SYSTEM_NAME STREQUAL "Windows")
   if (RunCMake_C_COMPILER_ID STREQUAL "MSVC"

+ 29 - 0
Tests/RunCMake/try_run/LinkerLanguage.cmake

@@ -0,0 +1,29 @@
+enable_language(CXX)
+enable_language(Fortran)
+
+set (lib_name "${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_STATIC_LIBRARY_PREFIX}lib${CMAKE_STATIC_LIBRARY_SUFFIX}")
+if (CMAKE_SYSTEM_NAME STREQUAL "Windows")
+  if (CMAKE_SIZEOF_VOID_P EQUAL 4)
+    set (undef_flag -u _func)
+  else()
+    set (undef_flag -u func)
+  endif()
+elseif (CMAKE_SYSTEM_NAME STREQUAL "Darwin")
+  set (undef_flag -u _func)
+else()
+  set (undef_flag -u func)
+endif()
+
+set(CMAKE_TRY_COMPILE_TARGET_TYPE EXECUTABLE)
+try_run(run_result compile_result
+  SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/lib.cxx ${CMAKE_CURRENT_SOURCE_DIR}/main.f90
+  COMPILE_OUTPUT_VARIABLE compile_out
+  RUN_OUTPUT_VARIABLE run_out
+  LINKER_LANGUAGE Fortran)
+
+if(NOT compile_result)
+  message(FATAL_ERROR "try_run(... LINKER_LANGUAGE Fortran) compilation failed:\n${compile_out}")
+endif()
+if(run_result STREQUAL "FAILED_TO_RUN")
+  message(FATAL_ERROR "try_run(... LINKER_LANGUAGE Fortran) execution failed:\n${run_out}")
+endif()

+ 10 - 0
Tests/RunCMake/try_run/RunCMakeTest.cmake

@@ -19,3 +19,13 @@ if (CMAKE_SYSTEM_NAME MATCHES "^(Linux|Darwin|Windows)$" AND
   run_cmake(LinkOptions)
   unset (RunCMake_TEST_OPTIONS)
 endif()
+
+if (CMAKE_SYSTEM_NAME MATCHES "^(Linux|Darwin|Windows)$" AND
+    CMAKE_CXX_COMPILER_ID MATCHES "^(GNU|Clang|AppleClang)$" AND
+    CMAKE_Fortran_COMPILER_ID MATCHES "^(GNU)$")
+  set (RunCMake_TEST_OPTIONS
+    -DRunCMake_CXX_COMPILER_ID=${CMAKE_CXX_COMPILER_ID}
+    -DRunCMake_Fortran_COMPILER_ID=${CMAKE_Fortran_COMPILER_ID})
+  run_cmake(LinkerLanguage)
+  unset (RunCMake_TEST_OPTIONS)
+endif()

+ 4 - 0
Tests/RunCMake/try_run/lib.cxx

@@ -0,0 +1,4 @@
+
+extern "C" void func()
+{
+}

+ 12 - 0
Tests/RunCMake/try_run/main.f90

@@ -0,0 +1,12 @@
+program main
+
+implicit none
+
+interface
+subroutine func() bind(C)
+end subroutine
+end interface
+
+call func()
+
+end program