From 75a3f2a6399fcba3337226e4a495732e82a5d813 Mon Sep 17 00:00:00 2001 From: Vadym Starodubov Date: Wed, 27 Sep 2023 15:27:30 -0400 Subject: [PATCH] Add custom properties for configuration level --- .../VisualStudio/Csproj.Template.cs | 19 +++--------- Sharpmake.Generators/VisualStudio/Csproj.cs | 29 +++++++++++++----- Sharpmake.Generators/VisualStudio/Vcxproj.cs | 2 ++ .../VisualStudio/VsProjCommon.Template.cs | 4 +++ .../VisualStudio/VsProjCommon.cs | 30 +++++++++++++------ Sharpmake/Project.Configuration.cs | 4 +++ .../CSharpHelloWorld/HelloWorld.sharpmake.cs | 2 ++ .../HelloWorld.vs2017.v4_6_1.csproj | 2 ++ .../HelloWorld.vs2022.net6_0.csproj | 2 ++ samples/HelloWorld/HelloWorld.sharpmake.cs | 2 ++ .../projects/helloworld_vs2019_win32.vcxproj | 6 ++++ .../projects/helloworld_vs2019_win64.vcxproj | 6 ++++ 12 files changed, 77 insertions(+), 31 deletions(-) diff --git a/Sharpmake.Generators/VisualStudio/Csproj.Template.cs b/Sharpmake.Generators/VisualStudio/Csproj.Template.cs index 10e6a2fdc..b5a93aef5 100644 --- a/Sharpmake.Generators/VisualStudio/Csproj.Template.cs +++ b/Sharpmake.Generators/VisualStudio/Csproj.Template.cs @@ -119,8 +119,7 @@ public static class Project public const string MultiFrameworkProjectConfigurationCondition = "'$(Configuration)|$(Platform)|$(TargetFramework)'=='[conf.Name]|[platformName]|[targetFramework]'"; public static string ProjectConfigurationsGeneral = -@" - [platformName] +@" [platformName] [options.DebugSymbols] [options.DebugType] [options.Optimize] @@ -147,7 +146,6 @@ public static class Project [options.CopyVsixExtensionFiles] [options.CopyVsixExtensionLocation] [options.ProduceReferenceAssembly] - "; public static string ImportProjectItemSimple = @@ -305,7 +303,9 @@ public static class Project @" [cachedSettingsPropName] "; - + public static string PropertyGroupWithConditionStart = +@" +"; public static class ItemGroups { @@ -689,17 +689,6 @@ public static class TargetElement "; - public const string CustomPropertiesStart = -@" -"; - - public const string CustomProperty = - @" <[custompropertyname]>[custompropertyvalue] -"; - - public const string CustomPropertiesEnd = - @" -"; public static class UserFile { public static readonly string StartWithProject = diff --git a/Sharpmake.Generators/VisualStudio/Csproj.cs b/Sharpmake.Generators/VisualStudio/Csproj.cs index 29447bf64..ef735315d 100644 --- a/Sharpmake.Generators/VisualStudio/Csproj.cs +++ b/Sharpmake.Generators/VisualStudio/Csproj.cs @@ -1432,7 +1432,10 @@ List skipFiles using (resolver.NewScopedParameter("target", conf.Target)) using (resolver.NewScopedParameter("options", options[conf])) { + Write(Template.PropertyGroupWithConditionStart, writer, resolver); Write(Template.Project.ProjectConfigurationsGeneral, writer, resolver); + WriteProperties(conf.CustomProperties, writer, resolver); + Write(VsProjCommon.Template.PropertyGroupEnd, writer, resolver); } foreach (var dependencies in new[] { conf.DotNetPublicDependencies, conf.DotNetPrivateDependencies }) @@ -1499,6 +1502,7 @@ List skipFiles Project = projectGuid, }); } + } if (project.RunPostBuildEvent != Options.CSharp.RunPostBuildEvent.OnBuildSuccess) @@ -1694,19 +1698,30 @@ private static void WriteImportProjects(IEnumerable importProject } } - // TODO: remove this and use Sharpmake.Generators.VisualStudio.VsProjCommon.WriteCustomProperties instead + /// TODO: remove this and use instead. Note should be migrated to private static void WriteCustomProperties(Dictionary customProperties, Project project, StreamWriter writer, Resolver resolver) { if (customProperties.Any()) { - Write(Template.CustomPropertiesStart, writer, resolver); - foreach (var kvp in customProperties) + Write(VsProjCommon.Template.PropertyGroupStart, writer, resolver); + WriteProperties(customProperties, writer, resolver); + Write(VsProjCommon.Template.PropertyGroupEnd, writer, resolver); + } + } + + private static void WriteProperties( + Dictionary props, + StreamWriter writer, + Resolver resolver + ) + { + foreach (KeyValuePair kvp in props) + { + using (resolver.NewScopedParameter("custompropertyname", kvp.Key)) + using (resolver.NewScopedParameter("custompropertyvalue", kvp.Value)) { - resolver.SetParameter("custompropertyname", kvp.Key); - resolver.SetParameter("custompropertyvalue", kvp.Value); - Write(Template.CustomProperty, writer, resolver); + Write(VsProjCommon.Template.CustomProperty, writer, resolver); } - Write(Template.CustomPropertiesEnd, writer, resolver); } } diff --git a/Sharpmake.Generators/VisualStudio/Vcxproj.cs b/Sharpmake.Generators/VisualStudio/Vcxproj.cs index 6e5eaad29..f34722454 100644 --- a/Sharpmake.Generators/VisualStudio/Vcxproj.cs +++ b/Sharpmake.Generators/VisualStudio/Vcxproj.cs @@ -584,6 +584,8 @@ private void GenerateImpl(GenerationContext context, IList generatedFile { platformVcxproj.GenerateProjectConfigurationGeneral2(context, fileGenerator); } + + VsProjCommon.WriteConfigurationsCustomProperties(conf, fileGenerator); } } } diff --git a/Sharpmake.Generators/VisualStudio/VsProjCommon.Template.cs b/Sharpmake.Generators/VisualStudio/VsProjCommon.Template.cs index 416bcd6d3..ed15de5de 100644 --- a/Sharpmake.Generators/VisualStudio/VsProjCommon.Template.cs +++ b/Sharpmake.Generators/VisualStudio/VsProjCommon.Template.cs @@ -17,6 +17,10 @@ public static class Template @" "; + public static string PropertyGroupWithConditionStart= + @" +"; + public static string PropertyGroupEnd = @" "; diff --git a/Sharpmake.Generators/VisualStudio/VsProjCommon.cs b/Sharpmake.Generators/VisualStudio/VsProjCommon.cs index 5f046c8c4..168a90b3d 100644 --- a/Sharpmake.Generators/VisualStudio/VsProjCommon.cs +++ b/Sharpmake.Generators/VisualStudio/VsProjCommon.cs @@ -13,18 +13,30 @@ namespace Sharpmake.Generators.VisualStudio internal static partial class VsProjCommon { public static void WriteCustomProperties(Dictionary customProperties, IFileGenerator fileGenerator) - { - if (customProperties.Count == 0) - return; + => WritePropertyGroup(customProperties, Template.PropertyGroupStart, fileGenerator); - fileGenerator.Write(Template.PropertyGroupStart); - foreach (var kvp in customProperties) + public static void WriteConfigurationsCustomProperties(Project.Configuration conf, IFileGenerator fileGenerator) + => WritePropertyGroup(conf.CustomProperties, Template.PropertyGroupWithConditionStart, fileGenerator); + + private static void WritePropertyGroup( + Dictionary props, + string headerTemplate, + IFileGenerator fileGenerator + ) + { + if (props.Any()) { - using (fileGenerator.Declare("custompropertyname", kvp.Key)) - using (fileGenerator.Declare("custompropertyvalue", kvp.Value)) - fileGenerator.Write(Template.CustomProperty); + fileGenerator.Write(headerTemplate); + foreach (KeyValuePair kvp in props) + { + using (fileGenerator.Declare("custompropertyname", kvp.Key)) + using (fileGenerator.Declare("custompropertyvalue", kvp.Value)) + { + fileGenerator.Write(VsProjCommon.Template.CustomProperty); + } + } + fileGenerator.Write(Template.PropertyGroupEnd); } - fileGenerator.Write(Template.PropertyGroupEnd); } public static void WriteProjectConfigurationsDescription(IEnumerable configurations, IFileGenerator fileGenerator) diff --git a/Sharpmake/Project.Configuration.cs b/Sharpmake/Project.Configuration.cs index ebf4f4c8d..49481db24 100644 --- a/Sharpmake/Project.Configuration.cs +++ b/Sharpmake/Project.Configuration.cs @@ -3112,6 +3112,10 @@ internal void Resolve(string sourceRootPath, Resolver resolver) public Strings ForceUsingFiles = new Strings(); public Strings CustomPropsFiles = new Strings(); // vs2010+ .props files + /// + /// CustomProperties for configuration level. Supported only in msbuild based targets(C++/C#) + /// + public Dictionary CustomProperties = new Dictionary(); public Strings CustomTargetsFiles = new Strings(); // vs2010+ .targets files // NuGet packages (C# and visual studio c++ for now) diff --git a/samples/CSharpHelloWorld/HelloWorld.sharpmake.cs b/samples/CSharpHelloWorld/HelloWorld.sharpmake.cs index 1af14da2f..8c211a415 100644 --- a/samples/CSharpHelloWorld/HelloWorld.sharpmake.cs +++ b/samples/CSharpHelloWorld/HelloWorld.sharpmake.cs @@ -50,6 +50,8 @@ public virtual void ConfigureAll(Configuration conf, Target target) conf.ProjectFileName = "[project.Name].[target.DevEnv].[target.Framework]"; conf.ProjectPath = @"[project.RootPath]"; + conf.CustomProperties.Add("CustomOptimizationProperty", $"Custom-{target.Optimization}"); + conf.Options.Add(Sharpmake.Options.CSharp.TreatWarningsAsErrors.Enabled); } } diff --git a/samples/CSharpHelloWorld/reference/projects/helloworld/HelloWorld.vs2017.v4_6_1.csproj b/samples/CSharpHelloWorld/reference/projects/helloworld/HelloWorld.vs2017.v4_6_1.csproj index 614508238..b485c074b 100644 --- a/samples/CSharpHelloWorld/reference/projects/helloworld/HelloWorld.vs2017.v4_6_1.csproj +++ b/samples/CSharpHelloWorld/reference/projects/helloworld/HelloWorld.vs2017.v4_6_1.csproj @@ -23,6 +23,7 @@ 4 true false + Custom-Debug AnyCPU @@ -35,6 +36,7 @@ 4 true false + Custom-Release diff --git a/samples/CSharpHelloWorld/reference/projects/helloworld/HelloWorld.vs2022.net6_0.csproj b/samples/CSharpHelloWorld/reference/projects/helloworld/HelloWorld.vs2022.net6_0.csproj index e931b3090..7976a1796 100644 --- a/samples/CSharpHelloWorld/reference/projects/helloworld/HelloWorld.vs2022.net6_0.csproj +++ b/samples/CSharpHelloWorld/reference/projects/helloworld/HelloWorld.vs2022.net6_0.csproj @@ -23,6 +23,7 @@ 4 true false + Custom-Debug AnyCPU @@ -35,6 +36,7 @@ 4 true false + Custom-Release diff --git a/samples/HelloWorld/HelloWorld.sharpmake.cs b/samples/HelloWorld/HelloWorld.sharpmake.cs index a58a611f4..5df422a8e 100644 --- a/samples/HelloWorld/HelloWorld.sharpmake.cs +++ b/samples/HelloWorld/HelloWorld.sharpmake.cs @@ -36,6 +36,8 @@ public void ConfigureAll(Configuration conf, Target target) // if not set, no precompile option will be used. conf.PrecompHeader = "stdafx.h"; conf.PrecompSource = "stdafx.cpp"; + + conf.CustomProperties.Add("CustomOptimizationProperty", $"Custom-{target.Optimization}"); } } diff --git a/samples/HelloWorld/reference/projects/helloworld_vs2019_win32.vcxproj b/samples/HelloWorld/reference/projects/helloworld_vs2019_win32.vcxproj index c19495eda..be77b3d27 100644 --- a/samples/HelloWorld/reference/projects/helloworld_vs2019_win32.vcxproj +++ b/samples/HelloWorld/reference/projects/helloworld_vs2019_win32.vcxproj @@ -51,6 +51,9 @@ output\win32\debug\helloworld.exe false + + Custom-Debug + helloworld output\win32\release\ @@ -61,6 +64,9 @@ output\win32\release\helloworld.exe false + + Custom-Release + Use diff --git a/samples/HelloWorld/reference/projects/helloworld_vs2019_win64.vcxproj b/samples/HelloWorld/reference/projects/helloworld_vs2019_win64.vcxproj index 84fd6c965..129ba6867 100644 --- a/samples/HelloWorld/reference/projects/helloworld_vs2019_win64.vcxproj +++ b/samples/HelloWorld/reference/projects/helloworld_vs2019_win64.vcxproj @@ -51,6 +51,9 @@ output\win64\debug\helloworld.exe false + + Custom-Debug + helloworld output\win64\release\ @@ -61,6 +64,9 @@ output\win64\release\helloworld.exe false + + Custom-Release + Use