Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Vcxproj] Improve NuGet handling in packages.config mode & add necessary logic for package reference mode #399

Open
wants to merge 12 commits into
base: main
Choose a base branch
from
15 changes: 15 additions & 0 deletions SamplesDef.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,21 @@
"./{testFolder}/projects/output/vs2019/v4_7_2/{configuration}/TestCSharpConsole.exe"
]
},
{
"Name": "CPPForcePackageReference",
"CIs": [ "github", "gitlab" ],
"OSs": [ "windows-2022" ],
"Frameworks": [ "net6.0" ],
"Configurations": [ "debug", "release" ],
"TestFolder": "samples/CPPForcePackageReference",
"Commands":
[
"./RunSharpmake.ps1 -workingDirectory {testFolder} -sharpmakeFile \"CPPForcePackageReference.sharpmake.cs\" -framework {framework}",
"./RunProcess.ps1 -exeToRun msbuild -workingDirectory \"{testFolder}/projects\" -arguments \"-t:restore CPPForcePackageReference_{VsVersionSuffix}_win64.sln\"",
"./Compile.ps1 -slnOrPrjFile \"CPPForcePackageReference_{VsVersionSuffix}_win64.sln\" -configuration {configuration} -platform \"x64\" -WorkingDirectory \"{testFolder}/projects\" -VsVersion {os} -compiler MsBuild",
"&'./{testFolder}/projects/output/win64/{configuration}/cppforcepackagereference.exe'"
]
},
{
"Name": "CSharpHelloWorld_old_Frameworks",
"CIs": [ "github", "gitlab" ],
Expand Down
13 changes: 7 additions & 6 deletions Sharpmake.Generators/VisualStudio/Vcxproj.Template.cs
Original file line number Diff line number Diff line change
Expand Up @@ -160,21 +160,22 @@ public static class Project
";

// Support both regular and native package types, whichever happens to exist
public static string ProjectTargetsNugetReferenceImport =
@" <Import Project=""$(SolutionDir)\packages\[packageName].[packageVersion]\build\[packageName].targets"" Condition=""Exists('$(SolutionDir)\packages\[packageName].[packageVersion]\build\[packageName].targets')"" />
<Import Project=""$(SolutionDir)\packages\[packageName].[packageVersion]\build\native\[packageName].targets"" Condition=""Exists('$(SolutionDir)\packages\[packageName].[packageVersion]\build\native\[packageName].targets')"" />
// possible file extension: .targets and .props
public static string ProjectNugetReferenceImport =
@" <Import Project=""$(SolutionDir)\packages\[packageName].[packageVersion]\build\native\[packageName].[fileExtension]"" Condition=""Exists('$(SolutionDir)\packages\[packageName].[packageVersion]\build\native\[packageName].[fileExtension]')"" />
<Import Project=""$(SolutionDir)\packages\[packageName].[packageVersion]\build\[packageName].[fileExtension]"" Condition=""!Exists('$(SolutionDir)\packages\[packageName].[packageVersion]\build\native\[packageName].[fileExtension]') and Exists('$(SolutionDir)\packages\[packageName].[packageVersion]\build\[packageName].[fileExtension]')"" />
";

public static string ProjectTargetsNugetReferenceError =
@" <Error Condition=""!Exists('$(SolutionDir)\packages\[packageName].[packageVersion]\build\[packageName].targets') and !Exists('$(SolutionDir)\packages\[packageName].[packageVersion]\build\native\[packageName].targets')"" Text=""$([[System.String]]::Format('$(ErrorText)', '$(SolutionDir)\packages\[packageName].[packageVersion]\build\native\[packageName].targets'))"" />
public static string ProjectNugetReferenceError =
@" <Error Condition=""!Exists('$(SolutionDir)\packages\[packageName].[packageVersion]\build\[packageName].[fileExtension]') and !Exists('$(SolutionDir)\packages\[packageName].[packageVersion]\build\native\[packageName].[fileExtension]')"" Text=""$([[System.String]]::Format('$(ErrorText)', '$(SolutionDir)\packages\[packageName].[packageVersion]\build\native\[packageName].[fileExtension]'))"" />
";

public static string ProjectTargetsEnd =
@" </ImportGroup>
";

public static string ProjectCustomTargetsBegin =
@" <Target Name=""name"" BeforeTargets=""PrepareForBuild"">
@" <Target Name=""[targetName]"" BeforeTargets=""[beforeTargets]"">
";

public static string ProjectCustomTargetsEnd =
Expand Down
254 changes: 187 additions & 67 deletions Sharpmake.Generators/VisualStudio/Vcxproj.cs

Large diffs are not rendered by default.

11 changes: 8 additions & 3 deletions Sharpmake/Project.cs
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,14 @@ public string RootPath
set { SetProperty(ref _rootPath, value); }
}


private NuGetPackageMode _nuGetReferenceType = NuGetPackageMode.VersionDefault; // Determines the type of NuGet references generated for this project
public NuGetPackageMode NuGetReferenceType
{
get { return _nuGetReferenceType; }
set { SetProperty(ref _nuGetReferenceType, value); }
}

private DependenciesCopyLocalTypes _dependenciesCopyLocal = DependenciesCopyLocalTypes.Default; //used primarily for the .Net Framework
public DependenciesCopyLocalTypes DependenciesCopyLocal
{
Expand Down Expand Up @@ -2395,9 +2403,6 @@ public class CSharpProject : Project

public GeneratedAssemblyConfig GeneratedAssemblyConfig = new GeneratedAssemblyConfig();

// Determines the type of NuGet references generated for this project
public NuGetPackageMode NuGetReferenceType = NuGetPackageMode.VersionDefault;

public Options.CSharp.RunPostBuildEvent RunPostBuildEvent = Options.CSharp.RunPostBuildEvent.OnBuildSuccess;

public string CodeAnalysisRuleSetFileName;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
using System.IO;
using Sharpmake;

namespace CPPForcePackageReference
{
[Generate]
public class CPPForcePackageReference : Project
{
public CPPForcePackageReference()
{
Name = "CPPForcePackageReference";

AddTargets(new Target(
Platform.win64,
DevEnv.vs2022,
Optimization.Debug | Optimization.Release
));

SourceRootPath = @"[project.SharpmakeCsPath]\codebase";
NuGetReferenceType = NuGetPackageMode.PackageReference; // explicitly specify PackageReference for this cpp project
}

[Configure]
public void ConfigureAll(Configuration conf, Target target)
{
conf.ProjectFileName = "[project.Name]_[target.DevEnv]_[target.Platform]";
conf.ProjectPath = @"[project.SharpmakeCsPath]\projects";
conf.Options.Add(Options.Vc.Compiler.Exceptions.Enable);

// copy Directory.Build.props that forces the Nuget PackageReference feature for cpp project to be enabled
string directoryBuildPropsName = "Directory.Build.props";
DirectoryInfo srcPath = new DirectoryInfo(@$"{SharpmakeCsPath}\codebase\"); // SourceRootPath + ..
DirectoryInfo destPath = new DirectoryInfo(@$"{SharpmakeCsPath}\projects\"); // conf.ProjectPath + ..
if (!destPath.Exists)
{
destPath.Create();
}
Util.ForceCopy(Path.Combine(srcPath.FullName, directoryBuildPropsName), Path.Combine(destPath.FullName, directoryBuildPropsName));

// cpp source code uses nlohmann.json, we specify another nuget package that depends on nlohmann.json
// to test if nuget could correctly restore the dependency
conf.ReferencesByNuGetPackage.Add("SiddiqSoft.sip2json", "1.17.3");
}
}

[Generate]
public class CPPForcePackageReferenceSolution : Sharpmake.Solution
{
public CPPForcePackageReferenceSolution()
{
Name = "CPPForcePackageReference";

AddTargets(new Target(
Platform.win64,
DevEnv.vs2022,
Optimization.Debug | Optimization.Release
));
}

[Configure()]
public void ConfigureAll(Configuration conf, Target target)
{
conf.SolutionFileName = "[solution.Name]_[target.DevEnv]_[target.Platform]";
conf.SolutionPath = @"[solution.SharpmakeCsPath]\projects";
conf.AddProject<CPPForcePackageReference>(target);

}
}

public static class Main
{
[Sharpmake.Main]
public static void SharpmakeMain(Sharpmake.Arguments arguments)
{
KitsRootPaths.SetUseKitsRootForDevEnv(DevEnv.vs2022, KitsRootEnum.KitsRoot10, Options.Vc.General.WindowsTargetPlatformVersion.v10_0_19041_0);
arguments.Generate<CPPForcePackageReferenceSolution>();
}
}
}
18 changes: 18 additions & 0 deletions samples/CPPForcePackageReference/codebase/Directory.Build.props
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<Project DefaultTargets="Build" ToolsVersion="17.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">

<!-- Enable PackageReference support in C++ projects, see https://github.com/NuGet/NuGet.Client/pull/3145#issuecomment-888769783 -->
<PropertyGroup Condition="'$(MSBuildProjectExtension)' == '.vcxproj'">
<NuGetTargetMoniker Condition="'$(NuGetTargetMoniker)' == ''">native,Version=v0.0</NuGetTargetMoniker>
</PropertyGroup>

<ItemGroup Condition="'$(MSBuildProjectExtension)' == '.vcxproj'">
<ProjectCapability Include="PackageReferences" />
</ItemGroup>

<PropertyGroup>

<MSBuildProjectExtensionsPath>obj\$(MSBuildProjectName)\</MSBuildProjectExtensionsPath>

</PropertyGroup>

</Project>
37 changes: 37 additions & 0 deletions samples/CPPForcePackageReference/codebase/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#include <iostream>
#include <nlohmann/json.hpp>
using json = nlohmann::json;


int main(int, char**)
{
std::cout << "I was built in "

#if _DEBUG
"Debug"
#endif

#if NDEBUG
"Release"
#endif

#if _WIN64
" x64"
#else
" x86"
#endif

<< std::endl;


json j = {
{"happy", true},
{"pi", 3.141},
};

for (auto& element : j) {
std::cout << element << '\n';
}

return 0;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "CPPForcePackageReference", "cppforcepackagereference_vs2022_win64.vcxproj", "{9E635693-40E9-C1A2-3B1D-447B25D6293E}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|x64 = Debug|x64
Release|x64 = Release|x64
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{9E635693-40E9-C1A2-3B1D-447B25D6293E}.Debug|x64.ActiveCfg = Debug|x64
{9E635693-40E9-C1A2-3B1D-447B25D6293E}.Debug|x64.Build.0 = Debug|x64
{9E635693-40E9-C1A2-3B1D-447B25D6293E}.Release|x64.ActiveCfg = Release|x64
{9E635693-40E9-C1A2-3B1D-447B25D6293E}.Release|x64.Build.0 = Release|x64
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
EndGlobal
Loading