Prechádzať zdrojové kódy

VS: Added support for VS package references for nuget

Kinan Mahdi 6 rokov pred
rodič
commit
42e14d90b1

+ 1 - 0
Help/manual/cmake-properties.7.rst

@@ -342,6 +342,7 @@ Properties on Targets
    /prop_tgt/VS_KEYWORD
    /prop_tgt/VS_MOBILE_EXTENSIONS_VERSION
    /prop_tgt/VS_NO_SOLUTION_DEPLOY
+   /prop_tgt/VS_PACKAGE_REFERENCES
    /prop_tgt/VS_PROJECT_IMPORT
    /prop_tgt/VS_SCC_AUXPATH
    /prop_tgt/VS_SCC_LOCALPATH

+ 13 - 0
Help/prop_tgt/VS_PACKAGE_REFERENCES.rst

@@ -0,0 +1,13 @@
+VS_PACKAGE_REFERENCES
+---------------------
+
+Visual Studio package references for nuget.
+
+Adds one or more semicolon-delimited package references to a generated
+Visual Studio project. The version of the package will be
+underscore delimited. For example, ``boost_1.7.0;nunit_3.12.*``.
+
+.. code-block:: cmake
+
+  set_property(TARGET ${TARGET_NAME} PROPERTY
+    VS_PACKAGE_REFERENCES "boost_1.7.0")

+ 6 - 0
Help/release/dev/vs-add-package-references.rst

@@ -0,0 +1,6 @@
+vs-add-package-references
+-------------------------
+
+* A :prop_tgt:`VS_PACKAGE_REFERENCES` target property was added to
+  tell :ref:`Visual Studio Generators` to add references to nuget
+  packages.

+ 28 - 0
Source/cmVisualStudio10TargetGenerator.cxx

@@ -664,6 +664,7 @@ void cmVisualStudio10TargetGenerator::Generate()
     this->WriteCustomCommands(e0);
     this->WriteAllSources(e0);
     this->WriteDotNetReferences(e0);
+    this->WritePackageReferences(e0);
     this->WriteImports(e0);
     this->WriteEmbeddedResourceGroup(e0);
     this->WriteXamlFilesGroup(e0);
@@ -737,6 +738,33 @@ void cmVisualStudio10TargetGenerator::Generate()
   this->WriteGroups();
 }
 
+void cmVisualStudio10TargetGenerator::WritePackageReferences(Elem& e0)
+{
+  std::vector<std::string> packageReferences;
+  if (const char* vsPackageReferences =
+        this->GeneratorTarget->GetProperty("VS_PACKAGE_REFERENCES")) {
+    cmSystemTools::ExpandListArgument(vsPackageReferences, packageReferences);
+  }
+  if (!packageReferences.empty()) {
+    Elem e1(e0, "ItemGroup");
+    for (std::string const& ri : packageReferences) {
+      size_t versionIndex = ri.find_last_of('_');
+      if (versionIndex != std::string::npos) {
+        WritePackageReference(e1, ri.substr(0, versionIndex),
+                              ri.substr(versionIndex + 1));
+      }
+    }
+  }
+}
+
+void cmVisualStudio10TargetGenerator::WritePackageReference(
+  Elem& e1, std::string const& ref, std::string const& version)
+{
+  Elem e2(e1, "PackageReference");
+  e2.Attribute("Include", ref);
+  e2.Attribute("Version", version);
+}
+
 void cmVisualStudio10TargetGenerator::WriteDotNetReferences(Elem& e0)
 {
   std::vector<std::string> references;

+ 3 - 0
Source/cmVisualStudio10TargetGenerator.h

@@ -72,6 +72,9 @@ private:
   void WriteExcludeFromBuild(Elem& e2,
                              std::vector<size_t> const& exclude_configs);
   void WriteAllSources(Elem& e0);
+  void WritePackageReferences(Elem& e0);
+  void WritePackageReference(Elem& e1, std::string const& ref,
+                             std::string const& version);
   void WriteDotNetReferences(Elem& e0);
   void WriteDotNetReference(Elem& e1, std::string const& ref,
                             std::string const& hint,

+ 1 - 0
Tests/RunCMake/VS10Project/RunCMakeTest.cmake

@@ -21,6 +21,7 @@ run_cmake(VSCSharpDefines)
 run_cmake(VsSdkDirectories)
 run_cmake(VsGlobals)
 run_cmake(VsProjectImport)
+run_cmake(VsPackageReferences)
 
 if(CMAKE_C_COMPILER_ID STREQUAL "MSVC" AND CMAKE_C_COMPILER_VERSION VERSION_GREATER_EQUAL 19.05)
   run_cmake(VsJustMyCode)

+ 39 - 0
Tests/RunCMake/VS10Project/VsPackageReferences-check.cmake

@@ -0,0 +1,39 @@
+set(vcProjectFile "${RunCMake_TEST_BINARY_DIR}/foo.vcxproj")
+if(NOT EXISTS "${vcProjectFile}")
+  set(RunCMake_TEST_FAILED "Project file foo.vcxproj does not exist.")
+  return()
+endif()
+
+
+set(test1Library "boost")
+set(test1Version "1.7.0")
+
+
+set(test2Library "SFML")
+set(test2Version "2.2.0")
+
+set(Library1Found FALSE)
+set(Library2Found FALSE)
+
+file(STRINGS "${vcProjectFile}" lines)
+
+foreach(i 1 2)
+  set(testLibrary "${test${i}Library}")
+  set(testVersion "${test${i}Version}")
+  foreach(line IN LISTS lines)
+    if(line MATCHES "^ *<PackageReference Include=\"${testLibrary}\".*>$")
+      if(line MATCHES "^ *<PackageReference .* Version=\"${testVersion}\".*>$")
+        set(Library${i}Found TRUE)
+        message(STATUS "foo.vcxproj is using package reference ${testLibrary} with version ${testVersion}")
+      elseif()
+        message(STATUS "foo.vcxproj failed to define reference ${testLibrary} with version ${testVersion}")
+        set(Library${i}Found FALSE)
+      endif()
+    endif()
+  endforeach()
+endforeach()
+
+if(NOT Library1Found OR NOT Library2Found)
+  set(RunCMake_TEST_FAILED "Failed to find package references")
+  return()
+endif()

+ 4 - 0
Tests/RunCMake/VS10Project/VsPackageReferences.cmake

@@ -0,0 +1,4 @@
+enable_language(CXX)
+add_library(foo foo.cpp)
+
+set_property(TARGET foo PROPERTY VS_PACKAGE_REFERENCES "boost_1.7.0;SFML_2.2.0")