From b722eea9253acd9e33921779175fc615d064577a Mon Sep 17 00:00:00 2001 From: Marc Chevrier Date: Mon, 22 Feb 2021 11:45:11 +0100 Subject: Genex: LINK_LANGUAGE respects LINKER_LANGUAGE property If target property LINKER_LANGUAGE is set, LINK_LANGUAGE generator expression evaluation must be always successful. This fix can be helpful to elaborate a solution for issue #21818. --- Source/cmGeneratorTarget.cxx | 6 +++++- .../RunCMakeTest.cmake | 1 + .../target_link_libraries-LINK_LANGUAGE/func.cxx | 17 ++++++++++++++--- .../target_link_libraries-LINK_LANGUAGE/genex.cmake | 9 +++++++++ .../RunCMake/target_link_libraries-LINK_LANGUAGE/main.c | 12 ++++++++++-- 5 files changed, 39 insertions(+), 6 deletions(-) diff --git a/Source/cmGeneratorTarget.cxx b/Source/cmGeneratorTarget.cxx index 9235faa..d7e9952 100644 --- a/Source/cmGeneratorTarget.cxx +++ b/Source/cmGeneratorTarget.cxx @@ -2648,8 +2648,12 @@ void cmGeneratorTarget::ComputeLinkClosure(const std::string& config, LinkClosure linkClosure; linkClosure.LinkerLanguage = this->LinkerLanguage; + bool hasHardCodedLinkerLanguage = this->Target->GetProperty("HAS_CXX") || + !this->Target->GetSafeProperty("LINKER_LANGUAGE").empty(); + // Get languages built in this target. - secondPass = this->ComputeLinkClosure(config, linkClosure, false); + secondPass = this->ComputeLinkClosure(config, linkClosure, false) && + !hasHardCodedLinkerLanguage; this->LinkerLanguage = linkClosure.LinkerLanguage; if (!secondPass) { lc = std::move(linkClosure); diff --git a/Tests/RunCMake/target_link_libraries-LINK_LANGUAGE/RunCMakeTest.cmake b/Tests/RunCMake/target_link_libraries-LINK_LANGUAGE/RunCMakeTest.cmake index 0f0e5d8..f43a7c6 100644 --- a/Tests/RunCMake/target_link_libraries-LINK_LANGUAGE/RunCMakeTest.cmake +++ b/Tests/RunCMake/target_link_libraries-LINK_LANGUAGE/RunCMakeTest.cmake @@ -34,6 +34,7 @@ if (RunCMake_GENERATOR MATCHES "Makefiles|Ninja|Visual Studio|Xcode|Watcom WMake run_cmake_target(genex CXX_interface2 LinkLibraries_CXX_interface2 --config Release) run_cmake_target(genex C_static LinkLibraries_C_static --config Release) run_cmake_target(genex CXX_static LinkLibraries_CXX_static --config Release) + run_cmake_target(genex C_static_CXX LinkLibraries_C_static_CXX --config Release) unset(RunCMake_TEST_OPTIONS) unset(RunCMake_TEST_OUTPUT_MERGE) diff --git a/Tests/RunCMake/target_link_libraries-LINK_LANGUAGE/func.cxx b/Tests/RunCMake/target_link_libraries-LINK_LANGUAGE/func.cxx index a12caca..ee6811c 100644 --- a/Tests/RunCMake/target_link_libraries-LINK_LANGUAGE/func.cxx +++ b/Tests/RunCMake/target_link_libraries-LINK_LANGUAGE/func.cxx @@ -1,7 +1,18 @@ -#if defined(_WIN32) -__declspec(dllexport) +#if !defined(BUILD_STATIC) && defined(_WIN32) +# define EXPORT_SYMBOL __declspec(dllexport) +#else +# define EXPORT_SYMBOL #endif - void func_cxx() + +EXPORT_SYMBOL +void func_cxx() { } + +extern "C" { +EXPORT_SYMBOL +void func_c_cxx() +{ +} +} diff --git a/Tests/RunCMake/target_link_libraries-LINK_LANGUAGE/genex.cmake b/Tests/RunCMake/target_link_libraries-LINK_LANGUAGE/genex.cmake index f3fe955..22d3df7 100644 --- a/Tests/RunCMake/target_link_libraries-LINK_LANGUAGE/genex.cmake +++ b/Tests/RunCMake/target_link_libraries-LINK_LANGUAGE/genex.cmake @@ -7,6 +7,8 @@ enable_language(CXX) add_library(shared_C SHARED func.c) add_library(shared_CXX SHARED func.cxx) +add_library(static_cxx STATIC func.cxx) +target_compile_definitions(static_cxx PRIVATE BUILD_STATIC) add_library(static1_C STATIC empty.c) target_link_libraries (static1_C INTERFACE $<$:shared_C>) @@ -70,3 +72,10 @@ add_executable(LinkLibraries_C_static main.c) target_link_libraries (LinkLibraries_C_static PRIVATE static3) add_executable(LinkLibraries_CXX_static main.cxx) target_link_libraries (LinkLibraries_CXX_static PRIVATE static3) + +# $ change, by default, link language from C to CXX +# but because LINKER_LANGUAGE property is set, keep C as link language +add_executable(LinkLibraries_C_static_CXX main.c) +target_compile_definitions (LinkLibraries_C_static_CXX PRIVATE C_USE_CXX) +target_link_libraries (LinkLibraries_C_static_CXX PRIVATE $<$:static_cxx>) +set_property(TARGET LinkLibraries_C_static_CXX PROPERTY LINKER_LANGUAGE C) diff --git a/Tests/RunCMake/target_link_libraries-LINK_LANGUAGE/main.c b/Tests/RunCMake/target_link_libraries-LINK_LANGUAGE/main.c index a908dea..689dbd7 100644 --- a/Tests/RunCMake/target_link_libraries-LINK_LANGUAGE/main.c +++ b/Tests/RunCMake/target_link_libraries-LINK_LANGUAGE/main.c @@ -1,12 +1,20 @@ -#if defined(_WIN32) +#if defined(C_USE_CXX) +void func_c_cxx(); +#else +# if defined(_WIN32) __declspec(dllimport) -#endif +# endif void func_c(); +#endif int main() { +#if defined(C_USE_CXX) + func_c_cxx(); +#else func_c(); +#endif return 0; } -- cgit v0.12