diff --git a/Source/cmComputeLinkDepends.cxx b/Source/cmComputeLinkDepends.cxx index dffe5e099b..f3057b47b9 100644 --- a/Source/cmComputeLinkDepends.cxx +++ b/Source/cmComputeLinkDepends.cxx @@ -647,13 +647,13 @@ cmComputeLinkDepends::cmComputeLinkDepends(const cmGeneratorTarget* target, if (!feature->empty() && key.length() > lloPrefix.length()) { auto item = key.substr(lloPrefix.length()); cmGeneratorExpressionDAGChecker dagChecker{ - this->Target->GetBacktrace(), this->Target, "LINK_LIBRARY_OVERRIDE", nullptr, nullptr, this->Target->GetLocalGenerator(), config, + this->Target->GetBacktrace(), }; auto overrideFeature = cmGeneratorExpression::Evaluate( *feature, this->Target->GetLocalGenerator(), config, @@ -667,13 +667,13 @@ cmComputeLinkDepends::cmComputeLinkDepends(const cmGeneratorTarget* target, if (cmValue linkLibraryOverride = this->Target->GetProperty("LINK_LIBRARY_OVERRIDE")) { cmGeneratorExpressionDAGChecker dagChecker{ - target->GetBacktrace(), target, "LINK_LIBRARY_OVERRIDE", nullptr, nullptr, target->GetLocalGenerator(), config, + target->GetBacktrace(), }; auto overrideValue = cmGeneratorExpression::Evaluate( *linkLibraryOverride, target->GetLocalGenerator(), config, target, diff --git a/Source/cmGeneratorExpressionDAGChecker.cxx b/Source/cmGeneratorExpressionDAGChecker.cxx index aad25f032d..c275ec689b 100644 --- a/Source/cmGeneratorExpressionDAGChecker.cxx +++ b/Source/cmGeneratorExpressionDAGChecker.cxx @@ -19,34 +19,24 @@ cmGeneratorExpressionDAGChecker::cmGeneratorExpressionDAGChecker( cmGeneratorTarget const* target, std::string property, - const GeneratorExpressionContent* content, + GeneratorExpressionContent const* content, cmGeneratorExpressionDAGChecker* parent, cmLocalGenerator const* contextLG, - std::string const& contextConfig) - : cmGeneratorExpressionDAGChecker(cmListFileBacktrace(), target, - std::move(property), content, parent, - contextLG, contextConfig) -{ -} - -cmGeneratorExpressionDAGChecker::cmGeneratorExpressionDAGChecker( - cmListFileBacktrace backtrace, cmGeneratorTarget const* target, - std::string property, const GeneratorExpressionContent* content, - cmGeneratorExpressionDAGChecker* parent, cmLocalGenerator const* contextLG, - std::string const& contextConfig) + std::string const& contextConfig, cmListFileBacktrace backtrace, + ComputingLinkLibraries computingLinkLibraries) : Parent(parent) , Top(parent ? parent->Top : this) , Target(target) , Property(std::move(property)) , Content(content) , Backtrace(std::move(backtrace)) + , ComputingLinkLibraries_(computingLinkLibraries) { if (parent) { this->TopIsTransitiveProperty = parent->TopIsTransitiveProperty; } else { this->TopIsTransitiveProperty = this->Target - ->IsTransitiveProperty(this->Property, contextLG, contextConfig, - this->EvaluatingLinkLibraries()) + ->IsTransitiveProperty(this->Property, contextLG, contextConfig, this) .has_value(); } @@ -204,6 +194,11 @@ bool cmGeneratorExpressionDAGChecker::EvaluatingLinkerLauncher() const "_LINKER_LAUNCHER"_s; } +bool cmGeneratorExpressionDAGChecker::IsComputingLinkLibraries() const +{ + return this->Top->ComputingLinkLibraries_ == ComputingLinkLibraries::Yes; +} + bool cmGeneratorExpressionDAGChecker::EvaluatingLinkLibraries( cmGeneratorTarget const* tgt, ForGenex genex) const { diff --git a/Source/cmGeneratorExpressionDAGChecker.h b/Source/cmGeneratorExpressionDAGChecker.h index 8b0eea76b4..064804f47a 100644 --- a/Source/cmGeneratorExpressionDAGChecker.h +++ b/Source/cmGeneratorExpressionDAGChecker.h @@ -17,19 +17,19 @@ class cmLocalGenerator; struct cmGeneratorExpressionDAGChecker { - cmGeneratorExpressionDAGChecker(cmListFileBacktrace backtrace, - cmGeneratorTarget const* target, - std::string property, - const GeneratorExpressionContent* content, - cmGeneratorExpressionDAGChecker* parent, - cmLocalGenerator const* contextLG, - std::string const& contextConfig); - cmGeneratorExpressionDAGChecker(cmGeneratorTarget const* target, - std::string property, - const GeneratorExpressionContent* content, - cmGeneratorExpressionDAGChecker* parent, - cmLocalGenerator const* contextLG, - std::string const& contextConfig); + enum class ComputingLinkLibraries + { + No, + Yes, + }; + cmGeneratorExpressionDAGChecker( + cmGeneratorTarget const* target, std::string property, + GeneratorExpressionContent const* content, + cmGeneratorExpressionDAGChecker* parent, cmLocalGenerator const* contextLG, + std::string const& contextConfig, + cmListFileBacktrace backtrace = cmListFileBacktrace(), + ComputingLinkLibraries computingLinkLibraries = + ComputingLinkLibraries::No); enum Result { @@ -52,6 +52,11 @@ struct cmGeneratorExpressionDAGChecker bool EvaluatingLinkOptionsExpression() const; bool EvaluatingLinkerLauncher() const; + /** Returns true only when computing the actual link dependency + graph for cmGeneratorTarget::GetLinkImplementationLibraries + or cmGeneratorTarget::GetLinkInterfaceLibraries. */ + bool IsComputingLinkLibraries() const; + enum class ForGenex { ANY, @@ -85,4 +90,6 @@ private: bool TransitivePropertiesOnly = false; bool CMP0131 = false; bool TopIsTransitiveProperty = false; + ComputingLinkLibraries const ComputingLinkLibraries_ = + ComputingLinkLibraries::No; }; diff --git a/Source/cmGeneratorExpressionNode.cxx b/Source/cmGeneratorExpressionNode.cxx index e355cf676c..f6c2d2212f 100644 --- a/Source/cmGeneratorExpressionNode.cxx +++ b/Source/cmGeneratorExpressionNode.cxx @@ -486,13 +486,13 @@ protected: { if (context->HeadTarget) { cmGeneratorExpressionDAGChecker dagChecker{ - context->Backtrace, context->HeadTarget, genexOperator + ":" + expression, content, dagCheckerParent, context->LG, context->Config, + context->Backtrace, }; switch (dagChecker.Check()) { case cmGeneratorExpressionDAGChecker::SELF_REFERENCE: @@ -2957,8 +2957,7 @@ static const struct TargetPropertyNode : public cmGeneratorExpressionNode if (cm::optional transitiveProp = target->IsTransitiveProperty(propertyName, context->LG, - context->Config, - evaluatingLinkLibraries)) { + context->Config, dagCheckerParent)) { interfacePropertyName = std::string(transitiveProp->InterfaceName); isInterfaceProperty = transitiveProp->InterfaceName == propertyName; usage = transitiveProp->Usage; @@ -2993,8 +2992,13 @@ static const struct TargetPropertyNode : public cmGeneratorExpressionNode } cmGeneratorExpressionDAGChecker dagChecker{ - context->Backtrace, target, propertyName, content, - dagCheckerParent, context->LG, context->Config, + target, + propertyName, + content, + dagCheckerParent, + context->LG, + context->Config, + context->Backtrace, }; switch (dagChecker.Check()) { diff --git a/Source/cmGeneratorTarget.h b/Source/cmGeneratorTarget.h index fe783610b0..758fb8af9e 100644 --- a/Source/cmGeneratorTarget.h +++ b/Source/cmGeneratorTarget.h @@ -1001,7 +1001,8 @@ public: cm::optional IsTransitiveProperty( cm::string_view prop, cmLocalGenerator const* lg, - std::string const& config, bool evaluatingLinkLibraries) const; + std::string const& config, + cmGeneratorExpressionDAGChecker const* dagChecker) const; bool HaveInstallTreeRPATH(const std::string& config) const; diff --git a/Source/cmGeneratorTarget_IncludeDirectories.cxx b/Source/cmGeneratorTarget_IncludeDirectories.cxx index da97a68ff3..d911f10282 100644 --- a/Source/cmGeneratorTarget_IncludeDirectories.cxx +++ b/Source/cmGeneratorTarget_IncludeDirectories.cxx @@ -48,8 +48,13 @@ std::string AddLangSpecificInterfaceIncludeDirectories( cmGeneratorExpressionDAGChecker* context) { cmGeneratorExpressionDAGChecker dagChecker{ - target->GetBacktrace(), target, propertyName, nullptr, context, - target->GetLocalGenerator(), config, + target, + propertyName, + nullptr, + context, + target->GetLocalGenerator(), + config, + target->GetBacktrace(), }; switch (dagChecker.Check()) { case cmGeneratorExpressionDAGChecker::SELF_REFERENCE: @@ -101,8 +106,13 @@ void AddLangSpecificImplicitIncludeDirectories( if (const auto* libraries = target->GetLinkImplementationLibraries(config, UseTo::Compile)) { cmGeneratorExpressionDAGChecker dagChecker{ - target->GetBacktrace(), target, propertyName, nullptr, nullptr, - target->GetLocalGenerator(), config, + target, + propertyName, + nullptr, + nullptr, + target->GetLocalGenerator(), + config, + target->GetBacktrace(), }; for (const cmLinkImplItem& library : libraries->Libraries) { diff --git a/Source/cmGeneratorTarget_Link.cxx b/Source/cmGeneratorTarget_Link.cxx index e1db657ed2..74e77de1ee 100644 --- a/Source/cmGeneratorTarget_Link.cxx +++ b/Source/cmGeneratorTarget_Link.cxx @@ -565,7 +565,14 @@ void cmGeneratorTarget::ExpandLinkItems(std::string const& prop, } // Keep this logic in sync with ComputeLinkImplementationLibraries. cmGeneratorExpressionDAGChecker dagChecker{ - this, prop, nullptr, nullptr, this->LocalGenerator, config, + this, + prop, + nullptr, + nullptr, + this->LocalGenerator, + config, + cmListFileBacktrace(), + cmGeneratorExpressionDAGChecker::ComputingLinkLibraries::Yes, }; // The $ expression may be in a link interface to specify // private link dependencies that are otherwise excluded from usage @@ -1322,7 +1329,14 @@ void cmGeneratorTarget::ComputeLinkImplementationLibraries( for (auto const& entry : entryRange) { // Keep this logic in sync with ExpandLinkItems. cmGeneratorExpressionDAGChecker dagChecker{ - this, "LINK_LIBRARIES", nullptr, nullptr, this->LocalGenerator, config, + this, + "LINK_LIBRARIES", + nullptr, + nullptr, + this->LocalGenerator, + config, + cmListFileBacktrace(), + cmGeneratorExpressionDAGChecker::ComputingLinkLibraries::Yes, }; // The $ expression may be used to specify link dependencies // that are otherwise excluded from usage requirements. diff --git a/Source/cmGeneratorTarget_TransitiveProperty.cxx b/Source/cmGeneratorTarget_TransitiveProperty.cxx index d88a5b55cb..7c197fc74b 100644 --- a/Source/cmGeneratorTarget_TransitiveProperty.cxx +++ b/Source/cmGeneratorTarget_TransitiveProperty.cxx @@ -110,13 +110,13 @@ std::string cmGeneratorTarget::EvaluateInterfaceProperty( // a subset of TargetPropertyNode::Evaluate without stringify/parse steps // but sufficient for transitive interface properties. cmGeneratorExpressionDAGChecker dagChecker{ - context->Backtrace, this, prop, nullptr, dagCheckerParent, - this->LocalGenerator, + context->LG, context->Config, + context->Backtrace, }; switch (dagChecker.Check()) { case cmGeneratorExpressionDAGChecker::SELF_REFERENCE: @@ -183,10 +183,9 @@ std::string cmGeneratorTarget::EvaluateInterfaceProperty( } cm::optional -cmGeneratorTarget::IsTransitiveProperty(cm::string_view prop, - cmLocalGenerator const* lg, - std::string const& config, - bool evaluatingLinkLibraries) const +cmGeneratorTarget::IsTransitiveProperty( + cm::string_view prop, cmLocalGenerator const* lg, std::string const& config, + cmGeneratorExpressionDAGChecker const* dagChecker) const { cm::optional result; static const cm::string_view kINTERFACE_ = "INTERFACE_"_s; @@ -215,7 +214,7 @@ cmGeneratorTarget::IsTransitiveProperty(cm::string_view prop, result = TransitiveProperty{ "INTERFACE_COMPILE_DEFINITIONS"_s, UseTo::Compile }; } - } else if (!evaluatingLinkLibraries) { + } else if (!dagChecker || !dagChecker->IsComputingLinkLibraries()) { // Honor TRANSITIVE_COMPILE_PROPERTIES and TRANSITIVE_LINK_PROPERTIES // from the link closure when we are not evaluating the closure itself. CustomTransitiveProperties const& ctp = diff --git a/Tests/CustomTransitiveProperties/CMakeLists.txt b/Tests/CustomTransitiveProperties/CMakeLists.txt index 83ceff5482..a9ac2b87bd 100644 --- a/Tests/CustomTransitiveProperties/CMakeLists.txt +++ b/Tests/CustomTransitiveProperties/CMakeLists.txt @@ -90,9 +90,22 @@ target_compile_definitions(CustomTransitiveProperties PRIVATE $ ) +# Test TRANSITIVE_LINK_PROPERTIES containing LINK_LIBRARIES itself. +add_library(iface10 INTERFACE) +set_property(TARGET iface10 PROPERTY TRANSITIVE_LINK_PROPERTIES "LINK_LIBRARIES") +add_library(iface11 INTERFACE) +target_link_libraries(iface11 INTERFACE iface10) +add_library(static10 STATIC static10.c) +target_link_libraries(static10 PRIVATE iface11) +add_library(static11 STATIC static11.c) +target_link_libraries(static11 PRIVATE static10 iface11) +add_executable(main10 main10.c) +target_link_libraries(main10 PRIVATE static11 static10) + # Test TRANSITIVE_*_PROPERTY evaluation outside of usage requirements. +add_executable(check-args check-args.c) set(out "${CMAKE_CURRENT_BINARY_DIR}/out-$.txt") -file(GENERATE OUTPUT "${out}" CONTENT "# file(GENERATE) produced: +set(in_CUSTOM [====[ iface1 CUSTOM_A: '$' iface1 INTERFACE_CUSTOM_A: '$' iface2 CUSTOM_A: '$' @@ -125,10 +138,35 @@ main CUSTOM_V: '$' main INTERFACE_CUSTOM_V: '$' main CUSTOM_W: '$' main INTERFACE_CUSTOM_W: '$' +]====]) +set(in_LINK_LIBRARIES [====[ +iface1 LINK_LIBRARIES: '$' +iface1 INTERFACE_LINK_LIBRARIES: '$' +iface2 LINK_LIBRARIES: '$' +iface2 INTERFACE_LINK_LIBRARIES: '$' +static1 LINK_LIBRARIES: '$' +static1 INTERFACE_LINK_LIBRARIES: '$' +main LINK_LIBRARIES: '$' +main INTERFACE_LINK_LIBRARIES: '$' +iface10 LINK_LIBRARIES: '$' +iface10 INTERFACE_LINK_LIBRARIES: '$' +iface11 LINK_LIBRARIES: '$' +iface11 INTERFACE_LINK_LIBRARIES: '$' +static10 LINK_LIBRARIES: '$' +static10 INTERFACE_LINK_LIBRARIES: '$' +static11 LINK_LIBRARIES: '$' +static11 INTERFACE_LINK_LIBRARIES: '$' +main10 LINK_LIBRARIES: '$' +main10 INTERFACE_LINK_LIBRARIES: '$' +]====]) +file(GENERATE OUTPUT "${out}" CONTENT "# file(GENERATE) produced: +${in_CUSTOM} +${in_LINK_LIBRARIES} ") add_custom_target(check ALL VERBATIM COMMAND ${CMAKE_COMMAND} -Dconfig=$ -Dout=${out} -P${CMAKE_CURRENT_SOURCE_DIR}/check.cmake COMMAND CustomTransitiveProperties + COMMAND check-args "$" "CUSTOM_A_STATIC1;CUSTOM_A_IFACE2;CUSTOM_A_TARGET_TYPE_STATIC_LIBRARY;CUSTOM_A_IFACE1;CUSTOM_A_TARGET_NAME_STATIC1" "$" "CUSTOM_B_STATIC1;CUSTOM_B_IFACE1" "$" "CUSTOM_U_STATIC1;CUSTOM_U_IFACE2;CUSTOM_U_TARGET_TYPE_STATIC_LIBRARY;CUSTOM_U_IFACE1;CUSTOM_U_TARGET_NAME_STATIC1" @@ -143,4 +181,33 @@ add_custom_target(check ALL VERBATIM "$" "CUSTOM_U_MAIN;CUSTOM_U_STATIC1_IFACE;CUSTOM_U_IFACE2;CUSTOM_U_TARGET_TYPE_EXECUTABLE;CUSTOM_U_IFACE1;CUSTOM_U_TARGET_NAME_CUSTOMTRANSITIVEPROPERTIES;CUSTOM_U_OBJECT1_IFACE" "$" "CUSTOM_V_MAIN;CUSTOM_V_STATIC1_IFACE;CUSTOM_V_IFACE1" "$" "CUSTOM_W_MAIN;CUSTOM_W_IFACE1;CUSTOM_W_OBJECT1_IFACE" + COMMAND check-args + "$" "" + "$" "" + "$" "" + "$" "iface1" + "$" "iface2" + "$" "$" + "$" "static1;object1" + "$" "" + COMMAND check-args + "$" "" + "$" "" + "$" "" + "$" "iface10" + "$" "iface11;iface10" + # _/ \__ + # / \ + # "static10[iface11];iface11[iface10]" + "$" "iface11;iface10" + "$" "static10;iface11;iface11;iface10" + # __/ __/ \__ \__________ + # / / \ \ + # "static11[static10;iface11];static10[iface11;iface11[iface10]]" + "$" "static10;iface11;iface11;iface10" + "$" "static11;static10;static10;iface11;iface11;iface10" + # _______/ _______/ | | \______ \______________ + # / / | | \ \ + # "main10[static11;static10];static11[static10;iface11;static10[iface11;iface11[iface10]]]" + "$" "" ) diff --git a/Tests/CustomTransitiveProperties/check-args.c b/Tests/CustomTransitiveProperties/check-args.c new file mode 100644 index 0000000000..da97f8a695 --- /dev/null +++ b/Tests/CustomTransitiveProperties/check-args.c @@ -0,0 +1,16 @@ +#include +#include + +int main(int argc, char** argv) +{ + int result = 0; + int i; + for (i = 2; i < argc; i += 2) { + if (strcmp(argv[i - 1], argv[i]) != 0) { + fprintf(stderr, "Argument %d expected '%s' but got '%s'.\n", i, argv[i], + argv[i - 1]); + result = 1; + } + } + return result; +} diff --git a/Tests/CustomTransitiveProperties/check.cmake b/Tests/CustomTransitiveProperties/check.cmake index d7130c84a8..079854292c 100644 --- a/Tests/CustomTransitiveProperties/check.cmake +++ b/Tests/CustomTransitiveProperties/check.cmake @@ -32,6 +32,25 @@ main CUSTOM_V: 'CUSTOM_V_MAIN;CUSTOM_V_STATIC1_IFACE;CUSTOM_V_IFACE1' main INTERFACE_CUSTOM_V: '' main CUSTOM_W: 'CUSTOM_W_MAIN;CUSTOM_W_IFACE1;CUSTOM_W_OBJECT1_IFACE' main INTERFACE_CUSTOM_W: '' + +iface1 LINK_LIBRARIES: '' +iface1 INTERFACE_LINK_LIBRARIES: '' +iface2 LINK_LIBRARIES: '' +iface2 INTERFACE_LINK_LIBRARIES: 'iface1' +static1 LINK_LIBRARIES: 'iface2' +static1 INTERFACE_LINK_LIBRARIES: '\$' +main LINK_LIBRARIES: 'static1;object1' +main INTERFACE_LINK_LIBRARIES: '' +iface10 LINK_LIBRARIES: '' +iface10 INTERFACE_LINK_LIBRARIES: '' +iface11 LINK_LIBRARIES: '' +iface11 INTERFACE_LINK_LIBRARIES: 'iface10' +static10 LINK_LIBRARIES: 'iface11;iface10' +static10 INTERFACE_LINK_LIBRARIES: 'iface11;iface10' +static11 LINK_LIBRARIES: 'static10;iface11;iface11;iface10' +static11 INTERFACE_LINK_LIBRARIES: 'static10;iface11;iface11;iface10' +main10 LINK_LIBRARIES: 'static11;static10;static10;iface11;iface11;iface10' +main10 INTERFACE_LINK_LIBRARIES: '' ]]) string(REGEX REPLACE "\r\n" "\n" expect "${expect}") string(REGEX REPLACE "\n+$" "" expect "${expect}") diff --git a/Tests/CustomTransitiveProperties/main.c b/Tests/CustomTransitiveProperties/main.c index ab70eb0816..03325a0517 100644 --- a/Tests/CustomTransitiveProperties/main.c +++ b/Tests/CustomTransitiveProperties/main.c @@ -1,6 +1,3 @@ -#include -#include - #ifdef CUSTOM_A_IFACE1 # error "CUSTOM_A_IFACE1 incorrectly defined" #endif @@ -117,21 +114,7 @@ extern int static1(void); extern int object1(void); -int check_args(int argc, char** argv) +int main(void) { - int result = 0; - int i; - for (i = 2; i < argc; i += 2) { - if (strcmp(argv[i - 1], argv[i]) != 0) { - fprintf(stderr, "Argument %d expected '%s' but got '%s'.\n", i, argv[i], - argv[i - 1]); - result = 1; - } - } - return result; -} - -int main(int argc, char** argv) -{ - return static1() + object1() + check_args(argc, argv); + return static1() + object1(); } diff --git a/Tests/CustomTransitiveProperties/main10.c b/Tests/CustomTransitiveProperties/main10.c new file mode 100644 index 0000000000..369683c69b --- /dev/null +++ b/Tests/CustomTransitiveProperties/main10.c @@ -0,0 +1,7 @@ +extern int static10(void); +extern int static11(void); + +int main(void) +{ + return static10() + static11(); +} diff --git a/Tests/CustomTransitiveProperties/static10.c b/Tests/CustomTransitiveProperties/static10.c new file mode 100644 index 0000000000..6359f220de --- /dev/null +++ b/Tests/CustomTransitiveProperties/static10.c @@ -0,0 +1,4 @@ +int static10(void) +{ + return 0; +} diff --git a/Tests/CustomTransitiveProperties/static11.c b/Tests/CustomTransitiveProperties/static11.c new file mode 100644 index 0000000000..0ba822df91 --- /dev/null +++ b/Tests/CustomTransitiveProperties/static11.c @@ -0,0 +1,4 @@ +int static11(void) +{ + return 0; +}