From 83d102d98ab477fc87846e86ba0b7cc2d78c1f8a Mon Sep 17 00:00:00 2001
From: Gilles Khouzam <gillesk@microsoft.com>
Date: Fri, 17 Jun 2016 14:18:39 -0700
Subject: [PATCH] Adding VS_SDK_REFERENCES to CMake

Allows to specify external SDK references such as: "Microsoft.AdMediatorWindows81, Version=1.0"
---
 Help/manual/cmake-properties.7.rst         |  1 +
 Help/prop_tgt/VS_SDK_REFERENCES.rst        |  8 ++++++++
 Source/cmVisualStudio10TargetGenerator.cxx | 22 +++++++++++++++++++++-
 Tests/VSWinStorePhone/CMakeLists.txt       |  3 +++
 4 files changed, 33 insertions(+), 1 deletion(-)
 create mode 100644 Help/prop_tgt/VS_SDK_REFERENCES.rst

diff --git a/Help/manual/cmake-properties.7.rst b/Help/manual/cmake-properties.7.rst
index 3574b7f..f96d0e2 100644
--- a/Help/manual/cmake-properties.7.rst
+++ b/Help/manual/cmake-properties.7.rst
@@ -275,6 +275,7 @@ Properties on Targets
    /prop_tgt/VS_SCC_LOCALPATH
    /prop_tgt/VS_SCC_PROJECTNAME
    /prop_tgt/VS_SCC_PROVIDER
+   /prop_tgt/VS_SDK_REFERENCES
    /prop_tgt/VS_WINDOWS_TARGET_PLATFORM_MIN_VERSION
    /prop_tgt/VS_WINRT_COMPONENT
    /prop_tgt/VS_WINRT_EXTENSIONS
diff --git a/Help/prop_tgt/VS_SDK_REFERENCES.rst b/Help/prop_tgt/VS_SDK_REFERENCES.rst
new file mode 100644
index 0000000..5d810a9
--- /dev/null
+++ b/Help/prop_tgt/VS_SDK_REFERENCES.rst
@@ -0,0 +1,8 @@
+VS_SDK_REFERENCES
+-------------------
+
+Visual Studio project SDK references
+
+Adds one or more semicolon-delimited SDK references to a generated
+Visual Studio project.  For example,
+"Microsoft.AdMediatorWindows81, Version=1.0".
diff --git a/Source/cmVisualStudio10TargetGenerator.cxx b/Source/cmVisualStudio10TargetGenerator.cxx
index 3b1cddb..60a8ba1 100644
--- a/Source/cmVisualStudio10TargetGenerator.cxx
+++ b/Source/cmVisualStudio10TargetGenerator.cxx
@@ -2643,6 +2643,20 @@ void cmVisualStudio10TargetGenerator::WriteSinglePlatformExtension(
 
 void cmVisualStudio10TargetGenerator::WriteSDKReferences()
 {
+  std::vector<std::string> sdkReferences;
+  bool hasWrittenItemGroup = false;
+  if (const char *vsSDKReferences =
+      this->GeneratorTarget->GetProperty("VS_SDK_REFERENCES")) {
+    cmSystemTools::ExpandListArgument(vsSDKReferences, sdkReferences);
+    this->WriteString("<ItemGroup>\n", 1);
+    hasWrittenItemGroup = true;
+    for(std::vector<std::string>::iterator ri = sdkReferences.begin();
+        ri != sdkReferences.end(); ++ri) {
+      this->WriteString("<SDKReference Include=\"", 2);
+      (*this->BuildFileStream) << cmVS10EscapeXML(*ri) << "\"/>\n";
+    }
+  }
+
   // This only applies to Windows 10 apps
   if (this->GlobalGenerator->TargetsWindowsStore() &&
       cmHasLiteralPrefix(this->GlobalGenerator->GetSystemVersion(), "10.0")) {
@@ -2655,7 +2669,10 @@ void cmVisualStudio10TargetGenerator::WriteSDKReferences()
 
     if (desktopExtensionsVersion || mobileExtensionsVersion ||
         iotExtensionsVersion) {
-      this->WriteString("<ItemGroup>\n", 1);
+      if(!hasWrittenItemGroup) {
+        this->WriteString("<ItemGroup>\n", 1);
+        hasWrittenItemGroup = true;
+      }
       if (desktopExtensionsVersion) {
         this->WriteSingleSDKReference("WindowsDesktop",
                                       desktopExtensionsVersion);
@@ -2667,6 +2684,9 @@ void cmVisualStudio10TargetGenerator::WriteSDKReferences()
       if (iotExtensionsVersion) {
         this->WriteSingleSDKReference("WindowsIoT", iotExtensionsVersion);
       }
+    }
+
+    if(hasWrittenItemGroup) {
       this->WriteString("</ItemGroup>\n", 1);
     }
   }
diff --git a/Tests/VSWinStorePhone/CMakeLists.txt b/Tests/VSWinStorePhone/CMakeLists.txt
index ae82755..5429673 100644
--- a/Tests/VSWinStorePhone/CMakeLists.txt
+++ b/Tests/VSWinStorePhone/CMakeLists.txt
@@ -134,6 +134,9 @@ if("${SHORT_VERSION}" STREQUAL "10.0")
   set_property(TARGET ${EXE_NAME} PROPERTY VS_DESKTOP_EXTENSIONS_VERSION "${CMAKE_VS_WINDOWS_TARGET_PLATFORM_VERSION}")
   set_property(TARGET ${EXE_NAME} PROPERTY VS_MOBILE_EXTENSIONS_VERSION "${CMAKE_VS_WINDOWS_TARGET_PLATFORM_VERSION}")
   set_property(TARGET ${EXE_NAME} PROPERTY VS_IOT_EXTENSIONS_VERSION "${CMAKE_VS_WINDOWS_TARGET_PLATFORM_VERSION}")
+
+  # Add a reference to an SDK
+  set_property(TARGET ${EXE_NAME} PROPERTY VS_SDK_REFERENCES "Microsoft.NET.Native.Runtime.1.1, Version=1.1")
 endif()
 
 
-- 
2.8.2.windows.1

