From 8a64bcbe1c246a6901c72e86f2e5e791a1cf6df4 Mon Sep 17 00:00:00 2001
From: David Cole <DLRdave@aol.com>
Date: Fri, 17 Jul 2015 18:01:14 -0400
Subject: [PATCH] CMake: Add TARGETS to get_cmake_property

---
 Source/cmGetCMakePropertyCommand.cxx | 14 ++++++++++++++
 Source/cmGlobalGenerator.cxx         | 27 +++++++++++++++++++++++++++
 Source/cmGlobalGenerator.h           |  3 +++
 Tests/AliasTarget/CMakeLists.txt     | 36 ++++++++++++++++++++++++++++++++++++
 4 files changed, 80 insertions(+)

diff --git a/Source/cmGetCMakePropertyCommand.cxx b/Source/cmGetCMakePropertyCommand.cxx
index a460ca6..e713571 100644
--- a/Source/cmGetCMakePropertyCommand.cxx
+++ b/Source/cmGetCMakePropertyCommand.cxx
@@ -49,6 +49,20 @@ bool cmGetCMakePropertyCommand
       = this->Makefile->GetGlobalGenerator()->GetInstallComponents();
     output = cmJoin(*components, ";");
     }
+  else if ( args[1] == "TARGETS" )
+    {
+    // First attempt yielded ONLY targets directly defined in this->Makefile:
+    //std::vector<std::string> targetNames;
+    //const cmTargets targets = this->Makefile->GetTargets();
+    //for (cmTargets::const_iterator l = targets.begin();
+    //     l != targets.end(); l++)
+    //  {
+    //  targetNames.push_back(l->first);
+    //  }
+    std::vector<std::string> targetNames
+      = this->Makefile->GetGlobalGenerator()->GetAllTargetNames();
+    output = cmJoin(targetNames, ";");
+    }
   else
     {
     const char *prop = 0;
diff --git a/Source/cmGlobalGenerator.cxx b/Source/cmGlobalGenerator.cxx
index 88ac0bc..5d9026c 100644
--- a/Source/cmGlobalGenerator.cxx
+++ b/Source/cmGlobalGenerator.cxx
@@ -2119,6 +2119,33 @@ cmGlobalGenerator::FindTarget(const std::string& name,
 }
 
 //----------------------------------------------------------------------------
+std::vector<std::string>
+cmGlobalGenerator::GetAllTargetNames() const
+{
+  std::vector<std::string> targetNames;
+
+  TargetMap::const_iterator i = this->AliasTargets.begin();
+  for ( ; i != this->AliasTargets.end() ; ++i)
+    {
+    targetNames.push_back(i->first);
+    }
+
+  i = this->TotalTargets.begin();
+  for ( ; i != this->TotalTargets.end() ; ++i)
+    {
+    targetNames.push_back(i->first);
+    }
+
+  i = this->ImportedTargets.begin();
+  for ( ; i != this->ImportedTargets.end() ; ++i)
+    {
+    targetNames.push_back(i->first);
+    }
+
+  return targetNames;
+}
+
+//----------------------------------------------------------------------------
 bool
 cmGlobalGenerator::NameResolvesToFramework(const std::string& libname) const
 {
diff --git a/Source/cmGlobalGenerator.h b/Source/cmGlobalGenerator.h
index 398335b..11b63b2 100644
--- a/Source/cmGlobalGenerator.h
+++ b/Source/cmGlobalGenerator.h
@@ -238,6 +238,9 @@ public:
   cmTarget* FindTarget(const std::string& name,
                        bool excludeAliases = false) const;
 
+   ///! Retrieve all target names everywhere.
+  std::vector<std::string> GetAllTargetNames() const;
+
   void AddAlias(const std::string& name, cmTarget *tgt);
   bool IsAlias(const std::string& name) const;
 
diff --git a/Tests/AliasTarget/CMakeLists.txt b/Tests/AliasTarget/CMakeLists.txt
index c50b4e6..60067ed 100644
--- a/Tests/AliasTarget/CMakeLists.txt
+++ b/Tests/AliasTarget/CMakeLists.txt
@@ -1,6 +1,36 @@
 cmake_minimum_required(VERSION 2.8.11)
 project(AliasTarget)
 
+function(printTargetsAsJson)
+  get_cmake_property(targets TARGETS)
+
+  list(LENGTH targets n)
+  set(i 1)
+
+  message("")
+  message("{")
+  message("  \"targets\" : [")
+
+  foreach(t ${targets})
+    if(i LESS ${n})
+      set(c ",")
+    else()
+      set(c "")
+    endif()
+
+    get_property(tt TARGET ${t} PROPERTY TYPE)
+    message("    { \"name\" : \"${t}\", \"type\" : \"${tt}\" }${c}")
+
+    math(EXPR i "${i}+1")
+  endforeach()
+
+  message("  ]")
+  message("}")
+  message("")
+endfunction()
+
+printTargetsAsJson()
+
 set(CMAKE_CXX_STANDARD 98)
 
 # Those versions of the HP compiler that need a flag to get proper C++98
@@ -11,12 +41,16 @@ if (CMAKE_CXX_COMPILER_ID STREQUAL HP AND
 endif ()
 
 add_library(foo SHARED empty.cpp)
+printTargetsAsJson()
+
 add_library(PREFIX::Foo ALIAS foo)
 add_library(Another::Alias ALIAS foo)
 
 add_library(objects OBJECT object.cpp)
 add_library(Alias::Objects ALIAS objects)
 
+printTargetsAsJson()
+
 target_compile_definitions(foo PUBLIC FOO_DEFINE)
 
 add_library(bar SHARED empty.cpp)
@@ -78,3 +112,5 @@ endif()
 if (NOT _notAlias2 STREQUAL _notAlias2-NOTFOUND)
   message(SEND_ERROR "_notAlias2 not defined to a -NOTFOUND variant")
 endif()
+
+printTargetsAsJson()
-- 
1.8.3.msysgit.0

