diff options
author | Brad King <brad.king@kitware.com> | 2023-10-17 14:22:43 (GMT) |
---|---|---|
committer | Kitware Robot <kwrobot@kitware.com> | 2023-10-17 14:22:54 (GMT) |
commit | dd78592ba1f6b90b5310f18de9cac8efefcebb40 (patch) | |
tree | ee7e63b73d6d0c15da836a3ab45789f35fcb90a2 | |
parent | 3de46de11a2e98184c445f728f438939eb7a8519 (diff) | |
parent | 8f9f371668e5e9dec6c0992d51eb1c4bc793879f (diff) | |
download | CMake-dd78592ba1f6b90b5310f18de9cac8efefcebb40.zip CMake-dd78592ba1f6b90b5310f18de9cac8efefcebb40.tar.gz CMake-dd78592ba1f6b90b5310f18de9cac8efefcebb40.tar.bz2 |
Merge topic 'try_compile-alias-targets'
8f9f371668 try_compile: Add support for using ALIAS targets
9f8aa94192 Add missing `OUTPUT` in ExportImport test
Acked-by: Kitware Robot <kwrobot@kitware.com>
Tested-by: buildbot <buildbot@kitware.com>
Merge-request: !8877
-rw-r--r-- | Help/command/try_compile.rst | 3 | ||||
-rw-r--r-- | Source/cmCoreTryCompile.cxx | 24 | ||||
-rw-r--r-- | Tests/ExportImport/Import/A/CMakeLists.txt | 87 |
3 files changed, 112 insertions, 2 deletions
diff --git a/Help/command/try_compile.rst b/Help/command/try_compile.rst index 24d481b..f44a4e8 100644 --- a/Help/command/try_compile.rst +++ b/Help/command/try_compile.rst @@ -178,6 +178,9 @@ The options for the above signatures are: If this option is specified, any ``-DLINK_LIBRARIES=...`` value given to the ``CMAKE_FLAGS`` option will be ignored. + .. versionadded:: 3.29 + Alias targets to imported libraries are also supported. + ``LINK_OPTIONS <options>...`` .. versionadded:: 3.14 diff --git a/Source/cmCoreTryCompile.cxx b/Source/cmCoreTryCompile.cxx index 3596d47..7f7c747 100644 --- a/Source/cmCoreTryCompile.cxx +++ b/Source/cmCoreTryCompile.cxx @@ -857,8 +857,30 @@ cm::optional<cmTryCompileResult> cmCoreTryCompile::TryCompileCode( fclose(fout); return cm::nullopt; } - fprintf(fout, "\ninclude(\"${CMAKE_CURRENT_LIST_DIR}/%s\")\n\n", + fprintf(fout, "\ninclude(\"${CMAKE_CURRENT_LIST_DIR}/%s\")\n", fname.c_str()); + // Create all relevant alias targets + if (arguments.LinkLibraries) { + const auto& aliasTargets = this->Makefile->GetAliasTargets(); + for (std::string const& i : *arguments.LinkLibraries) { + auto alias = aliasTargets.find(i); + if (alias != aliasTargets.end()) { + const auto& aliasTarget = + this->Makefile->FindTargetToUse(alias->second); + // Create equivalent library/executable alias + if (aliasTarget->GetType() == cmStateEnums::EXECUTABLE) { + fprintf(fout, "add_executable(\"%s\" ALIAS \"%s\")\n", i.c_str(), + alias->second.c_str()); + } else { + // Other cases like UTILITY and GLOBAL_TARGET are excluded when + // arguments.LinkLibraries is initially parsed in this function. + fprintf(fout, "add_library(\"%s\" ALIAS \"%s\")\n", i.c_str(), + alias->second.c_str()); + } + } + } + } + fprintf(fout, "\n"); } /* Set the appropriate policy information for ENABLE_EXPORTS */ diff --git a/Tests/ExportImport/Import/A/CMakeLists.txt b/Tests/ExportImport/Import/A/CMakeLists.txt index 2c5662d..2a57633 100644 --- a/Tests/ExportImport/Import/A/CMakeLists.txt +++ b/Tests/ExportImport/Import/A/CMakeLists.txt @@ -500,7 +500,7 @@ if (((CMAKE_C_COMPILER_ID STREQUAL GNU AND CMAKE_C_COMPILER_VERSION VERSION_GREA OUTPUT_VARIABLE OUTPUT ) if(NOT BLD_ERROR_VARIABLE) - message(SEND_ERROR "BLD_ERROR_VARIABLE try_compile failed, but it was expected to succeed.") + message(SEND_ERROR "BLD_ERROR_VARIABLE try_compile failed, but it was expected to succeed. ${OUTPUT}") endif() if(NOT CMAKE_CROSSCOMPILING) @@ -518,6 +518,91 @@ if (((CMAKE_C_COMPILER_ID STREQUAL GNU AND CMAKE_C_COMPILER_VERSION VERSION_GREA endif() endif() endif() + + # Testing try_compile with ALIAS targets. + # These assume that previous test were successful, or at least the failures will be at the linking stage + # with symbol not found errors + + # First make sure that if the test run without appropriate alias targets, they should error out + try_compile(FAILING_LIBRARY_ALIAS_ERROR_VARIABLE + "${CMAKE_CURRENT_BINARY_DIR}/test_failing_library_alias" + "${CMAKE_CURRENT_SOURCE_DIR}/test_system.cpp" + LINK_LIBRARIES not_existing_library + OUTPUT_VARIABLE OUTPUT + NO_CACHE + ) + if(FAILING_LIBRARY_ALIAS_ERROR_VARIABLE) + message(SEND_ERROR "FAILING_LIBRARY_ALIAS_ERROR_VARIABLE try_compile succeeded, but it was expected to fail ${OUTPUT}.") + endif() + + # FIXME: CMAKE_TRY_COMPILE_TARGET_TYPE=MODULE is needed to properly link and test targets linked to an executable +# set(CMAKE_TRY_COMPILE_TARGET_TYPE STATIC_LIBRARY) +# try_compile(FAILING_EXE_ALIAS_ERROR_VARIABLE +# "${CMAKE_CURRENT_BINARY_DIR}/test_failing_exe_alias" +# "${CMAKE_CURRENT_SOURCE_DIR}/imp_mod1.c" +# LINK_LIBRARIES not_existing_executable +# OUTPUT_VARIABLE OUTPUT +# NO_CACHE +# ) +# unset(CMAKE_TRY_COMPILE_TARGET_TYPE) +# if(FAILING_EXE_ALIAS_ERROR_VARIABLE) +# message(SEND_ERROR "FAILING_EXE_ALIAS_ERROR_VARIABLE try_compile succeeded, but it was expected to fail ${OUTPUT}.") +# endif() + + # Do the actual try_compile tests for ALIAS targets + add_library(exp_systemlib_alias ALIAS exp_systemlib) + try_compile(EXP_LIBRARY_ALIAS_ERROR_VARIABLE + "${CMAKE_CURRENT_BINARY_DIR}/test_library_alias" + "${CMAKE_CURRENT_SOURCE_DIR}/test_system.cpp" + LINK_LIBRARIES exp_systemlib_alias + OUTPUT_VARIABLE OUTPUT + NO_CACHE + ) + if(NOT EXP_LIBRARY_ALIAS_ERROR_VARIABLE) + message(SEND_ERROR "EXP_LIBRARY_ALIAS_ERROR_VARIABLE try_compile failed with library aliased target, but it was expected to succeed ${OUTPUT}.") + endif() + + # FIXME: CMAKE_TRY_COMPILE_TARGET_TYPE=MODULE is needed to properly link and test targets linked to an executable +# set(CMAKE_TRY_COMPILE_TARGET_TYPE MODULE) +# add_executable(exp_exe_alias ALIAS exp_testExe2) +# try_compile(EXP_EXE_ALIAS_ERROR_VARIABLE +# "${CMAKE_CURRENT_BINARY_DIR}/test_exe_alias" +# "${CMAKE_CURRENT_SOURCE_DIR}/imp_mod1.c" +# LINK_LIBRARIES exp_exe_alias +# OUTPUT_VARIABLE OUTPUT +# NO_CACHE +# ) +# unset(CMAKE_TRY_COMPILE_TARGET_TYPE) +# if(NOT EXP_EXE_ALIAS_ERROR_VARIABLE) +# message(SEND_ERROR "EXP_EXE_ALIAS_ERROR_VARIABLE try_compile failed with executable aliased target, but it was expected to succeed ${OUTPUT}.") +# endif() + + add_library(bld_systemlib_alias ALIAS bld_systemlib) + try_compile(BLD_LIBRARY_ALIAS_ERROR_VARIABLE + "${CMAKE_CURRENT_BINARY_DIR}/test_library_alias" + "${CMAKE_CURRENT_SOURCE_DIR}/test_system.cpp" + LINK_LIBRARIES bld_systemlib_alias + OUTPUT_VARIABLE OUTPUT + NO_CACHE + ) + if(NOT BLD_LIBRARY_ALIAS_ERROR_VARIABLE) + message(SEND_ERROR "BLD_LIBRARY_ALIAS_ERROR_VARIABLE try_compile failed with library aliased target, but it was expected to succeed. ${OUTPUT}") + endif() + + # FIXME: CMAKE_TRY_COMPILE_TARGET_TYPE=MODULE is needed to properly link and test targets linked to an executable +# set(CMAKE_TRY_COMPILE_TARGET_TYPE MODULE) +# add_executable(bld_exe_alias ALIAS bld_testExe2) +# try_compile(BLD_EXE_ALIAS_ERROR_VARIABLE +# "${CMAKE_CURRENT_BINARY_DIR}/test_exe_alias" +# "${CMAKE_CURRENT_SOURCE_DIR}/imp_mod1.c" +# LINK_LIBRARIES bld_exe_alias +# OUTPUT_VARIABLE OUTPUT +# NO_CACHE +# ) +# unset(CMAKE_TRY_COMPILE_TARGET_TYPE) +# if(NOT BLD_EXE_ALIAS_ERROR_VARIABLE) +# message(SEND_ERROR "BLD_EXE_ALIAS_ERROR_VARIABLE try_compile failed with executable aliased target, but it was expected to succeed. ${OUTPUT}") +# endif() endif() #--------------------------------------------------------------------------------- |