From 5b222354de7060af409d0d80aa06180ed1b3ac64 Mon Sep 17 00:00:00 2001
From: Stephen Kelly <steveire@gmail.com>
Date: Mon, 3 Jun 2013 23:53:29 +0200
Subject: Genex: Fix the HEAD target used for evaluated expressions

If the expression $<TARGET_PROPERTY:prop> appears in the content
of a target property, the target that prop is read from is
the 'head target' of the expression. In contexts such as evaluating
the content of a target property during generation, such
as INCLUDE_DIRECTORIES, the 'head target' is the one on which the
initial request was made.

If evaluating a generator expression which is not a target property
content, the target must be explicitly specified. Such contexts
include add_custom_command and file(GENERATE). The content might
then look like

 $<TARGET_PROPERTY:tgt,prop>

However, as there is no HeadTarget set, any generator expressions
evaluated as part of reading prop from tgt which do not specify
the tgt directly report an error.

Modify the logic of the TARGET_PROPERTY generator expression so
that in such contexts, the 'head target' is set to the appropriate
target which was first encountered.
---
 Source/cmGeneratorExpressionEvaluator.cxx                   | 13 +++++++++----
 .../TargetIncludeDirectories/CMakeLists.txt                 |  5 +++++
 Tests/IncludeDirectories/TargetIncludeDirectories/other.cpp |  6 ++++++
 3 files changed, 20 insertions(+), 4 deletions(-)
 create mode 100644 Tests/IncludeDirectories/TargetIncludeDirectories/other.cpp

diff --git a/Source/cmGeneratorExpressionEvaluator.cxx b/Source/cmGeneratorExpressionEvaluator.cxx
index de6371a..a01a0f8 100644
--- a/Source/cmGeneratorExpressionEvaluator.cxx
+++ b/Source/cmGeneratorExpressionEvaluator.cxx
@@ -315,6 +315,7 @@ static const char* targetPropertyTransitiveWhitelist[] = {
 
 std::string getLinkedTargetsContent(const std::vector<std::string> &libraries,
                                   cmTarget *target,
+                                  cmTarget *headTarget,
                                   cmGeneratorExpressionContext *context,
                                   cmGeneratorExpressionDAGChecker *dagChecker,
                                   const std::string &interfacePropertyName)
@@ -345,7 +346,7 @@ std::string getLinkedTargetsContent(const std::vector<std::string> &libraries,
   std::string linkedTargetsContent = cge->Evaluate(context->Makefile,
                       context->Config,
                       context->Quiet,
-                      context->HeadTarget,
+                      headTarget,
                       target,
                       dagChecker);
   if (cge->GetHadContextSensitiveCondition())
@@ -538,6 +539,8 @@ static const struct TargetPropertyNode : public cmGeneratorExpressionNode
       interfacePropertyName = "INTERFACE_COMPILE_DEFINITIONS";
       }
 
+    cmTarget *headTarget = context->HeadTarget ? context->HeadTarget : target;
+
     const char **transBegin = targetPropertyTransitiveWhitelist;
     const char **transEnd = targetPropertyTransitiveWhitelist
               + (sizeof(targetPropertyTransitiveWhitelist) /
@@ -547,11 +550,12 @@ static const struct TargetPropertyNode : public cmGeneratorExpressionNode
       {
       const cmTarget::LinkInterface *iface = target->GetLinkInterface(
                                                     context->Config,
-                                                    context->HeadTarget);
+                                                    headTarget);
       if(iface)
         {
         linkedTargetsContent =
                   getLinkedTargetsContent(iface->Libraries, target,
+                                          headTarget,
                                           context, &dagChecker,
                                           interfacePropertyName);
         }
@@ -561,11 +565,12 @@ static const struct TargetPropertyNode : public cmGeneratorExpressionNode
       {
       const cmTarget::LinkImplementation *impl = target->GetLinkImplementation(
                                                     context->Config,
-                                                    context->HeadTarget);
+                                                    headTarget);
       if(impl)
         {
         linkedTargetsContent =
                   getLinkedTargetsContent(impl->Libraries, target,
+                                          headTarget,
                                           context, &dagChecker,
                                           interfacePropertyName);
         }
@@ -614,7 +619,7 @@ static const struct TargetPropertyNode : public cmGeneratorExpressionNode
         std::string result = cge->Evaluate(context->Makefile,
                             context->Config,
                             context->Quiet,
-                            context->HeadTarget,
+                            headTarget,
                             target,
                             &dagChecker);
 
diff --git a/Tests/IncludeDirectories/TargetIncludeDirectories/CMakeLists.txt b/Tests/IncludeDirectories/TargetIncludeDirectories/CMakeLists.txt
index ad6671f..bcfbe3c 100644
--- a/Tests/IncludeDirectories/TargetIncludeDirectories/CMakeLists.txt
+++ b/Tests/IncludeDirectories/TargetIncludeDirectories/CMakeLists.txt
@@ -109,6 +109,11 @@ set_property(TARGET libbad APPEND PROPERTY
   INTERFACE_INCLUDE_DIRECTORIES "${CMAKE_CURRENT_BINARY_DIR}/bad"
 )
 
+add_library(lib6 SHARED other.cpp)
+add_library(lib7 SHARED other.cpp)
+target_link_libraries(lib7 $<$<STREQUAL:$<TARGET_PROPERTY:TYPE>,EXECUTABLE>:lib6>)
+target_include_directories(lib7 PUBLIC $<$<STREQUAL:$<TARGET_PROPERTY:TYPE>,EXECUTABLE>:/lib7/include>)
+add_custom_target(head_target_test "some_bogus_custom_tool \"$<TARGET_PROPERTY:lib7,INCLUDE_DIRECTORIES>$<TARGET_PROPERTY:lib7,INTERFACE_INCLUDE_DIRECTORIES>\"")
 
 file(WRITE "${CMAKE_CURRENT_BINARY_DIR}/lib5.cpp" "#include \"common.h\"\n")
 add_library(lib5 "${CMAKE_CURRENT_BINARY_DIR}/lib5.cpp")
diff --git a/Tests/IncludeDirectories/TargetIncludeDirectories/other.cpp b/Tests/IncludeDirectories/TargetIncludeDirectories/other.cpp
new file mode 100644
index 0000000..6673471
--- /dev/null
+++ b/Tests/IncludeDirectories/TargetIncludeDirectories/other.cpp
@@ -0,0 +1,6 @@
+#ifdef _WIN32
+__declspec(dllexport)
+#endif
+int other() {
+  return 0;
+}
-- 
cgit v0.12