Skip to content

Commit

Permalink
Merge branch 'add-custom-properties-for-comfiguration-II' into 'main'
Browse files Browse the repository at this point in the history
Add custom properties for configuration level

See merge request Sharpmake/sharpmake!462
  • Loading branch information
jspelletier committed Sep 27, 2023
2 parents 0403bca + 75a3f2a commit 909c4a0
Show file tree
Hide file tree
Showing 12 changed files with 77 additions and 31 deletions.
19 changes: 4 additions & 15 deletions Sharpmake.Generators/VisualStudio/Csproj.Template.cs
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,7 @@ public static class Project
public const string MultiFrameworkProjectConfigurationCondition = "'$(Configuration)|$(Platform)|$(TargetFramework)'=='[conf.Name]|[platformName]|[targetFramework]'";

public static string ProjectConfigurationsGeneral =
@" <PropertyGroup Condition=""[projectConfigurationCondition]"">
<PlatformTarget>[platformName]</PlatformTarget>
@" <PlatformTarget>[platformName]</PlatformTarget>
<DebugSymbols>[options.DebugSymbols]</DebugSymbols>
<DebugType>[options.DebugType]</DebugType>
<Optimize>[options.Optimize]</Optimize>
Expand All @@ -147,7 +146,6 @@ public static class Project
<CopyVsixExtensionFiles>[options.CopyVsixExtensionFiles]</CopyVsixExtensionFiles>
<CopyVsixExtensionLocation>[options.CopyVsixExtensionLocation]</CopyVsixExtensionLocation>
<ProduceReferenceAssembly>[options.ProduceReferenceAssembly]</ProduceReferenceAssembly>
</PropertyGroup>
";

public static string ImportProjectItemSimple =
Expand Down Expand Up @@ -305,7 +303,9 @@ public static class Project
@" <CachedSettingsPropName>[cachedSettingsPropName]</CachedSettingsPropName>
";


public static string PropertyGroupWithConditionStart =
@" <PropertyGroup Condition=""[projectConfigurationCondition]"">
";

public static class ItemGroups
{
Expand Down Expand Up @@ -689,17 +689,6 @@ public static class TargetElement
</ProjectExtensions>
";

public const string CustomPropertiesStart =
@" <PropertyGroup>
";

public const string CustomProperty =
@" <[custompropertyname]>[custompropertyvalue]</[custompropertyname]>
";

public const string CustomPropertiesEnd =
@" </PropertyGroup>
";
public static class UserFile
{
public static readonly string StartWithProject =
Expand Down
29 changes: 22 additions & 7 deletions Sharpmake.Generators/VisualStudio/Csproj.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1432,7 +1432,10 @@ List<string> 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 })
Expand Down Expand Up @@ -1499,6 +1502,7 @@ List<string> skipFiles
Project = projectGuid,
});
}

}

if (project.RunPostBuildEvent != Options.CSharp.RunPostBuildEvent.OnBuildSuccess)
Expand Down Expand Up @@ -1694,19 +1698,30 @@ private static void WriteImportProjects(IEnumerable<ImportProject> importProject
}
}

// TODO: remove this and use Sharpmake.Generators.VisualStudio.VsProjCommon.WriteCustomProperties instead
/// TODO: remove this and use <see cref="VsProjCommon.WriteCustomProperties"/> instead. Note <see cref="CSproj"/> should be migrated to <see cref="IFileGenerator"/>
private static void WriteCustomProperties(Dictionary<string, string> 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<string, string> props,
StreamWriter writer,
Resolver resolver
)
{
foreach (KeyValuePair<string, string> 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);
}
}

Expand Down
2 changes: 2 additions & 0 deletions Sharpmake.Generators/VisualStudio/Vcxproj.cs
Original file line number Diff line number Diff line change
Expand Up @@ -584,6 +584,8 @@ private void GenerateImpl(GenerationContext context, IList<string> generatedFile
{
platformVcxproj.GenerateProjectConfigurationGeneral2(context, fileGenerator);
}

VsProjCommon.WriteConfigurationsCustomProperties(conf, fileGenerator);
}
}
}
Expand Down
4 changes: 4 additions & 0 deletions Sharpmake.Generators/VisualStudio/VsProjCommon.Template.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ public static class Template
@" <PropertyGroup>
";

public static string PropertyGroupWithConditionStart=
@" <PropertyGroup Condition=""'$(Configuration)|$(Platform)'=='[conf.Name]|[platformName]'"">
";

public static string PropertyGroupEnd =
@" </PropertyGroup>
";
Expand Down
30 changes: 21 additions & 9 deletions Sharpmake.Generators/VisualStudio/VsProjCommon.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,30 @@ namespace Sharpmake.Generators.VisualStudio
internal static partial class VsProjCommon
{
public static void WriteCustomProperties(Dictionary<string, string> 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<string, string> 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<string, string> 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<Project.Configuration> configurations, IFileGenerator fileGenerator)
Expand Down
4 changes: 4 additions & 0 deletions Sharpmake/Project.Configuration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3112,6 +3112,10 @@ internal void Resolve(string sourceRootPath, Resolver resolver)
public Strings ForceUsingFiles = new Strings();

public Strings CustomPropsFiles = new Strings(); // vs2010+ .props files
/// <summary>
/// CustomProperties for configuration level. Supported only in msbuild based targets(C++/C#)
/// </summary>
public Dictionary<string, string> CustomProperties = new Dictionary<string, string>();
public Strings CustomTargetsFiles = new Strings(); // vs2010+ .targets files

// NuGet packages (C# and visual studio c++ for now)
Expand Down
2 changes: 2 additions & 0 deletions samples/CSharpHelloWorld/HelloWorld.sharpmake.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
<WarningLevel>4</WarningLevel>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
<Prefer32Bit>false</Prefer32Bit>
<CustomOptimizationProperty>Custom-Debug</CustomOptimizationProperty>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'">
<PlatformTarget>AnyCPU</PlatformTarget>
Expand All @@ -35,6 +36,7 @@
<WarningLevel>4</WarningLevel>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
<Prefer32Bit>false</Prefer32Bit>
<CustomOptimizationProperty>Custom-Release</CustomOptimizationProperty>
</PropertyGroup>
<ItemGroup>
<Compile Include="..\..\codebase\HelloWorld\Program.cs">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
<WarningLevel>4</WarningLevel>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
<Prefer32Bit>false</Prefer32Bit>
<CustomOptimizationProperty>Custom-Debug</CustomOptimizationProperty>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'">
<PlatformTarget>AnyCPU</PlatformTarget>
Expand All @@ -35,6 +36,7 @@
<WarningLevel>4</WarningLevel>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
<Prefer32Bit>false</Prefer32Bit>
<CustomOptimizationProperty>Custom-Release</CustomOptimizationProperty>
</PropertyGroup>
<Import Project="Sdk.props" Sdk="Microsoft.NET.Sdk" />
<ItemGroup>
Expand Down
2 changes: 2 additions & 0 deletions samples/HelloWorld/HelloWorld.sharpmake.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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}");
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@
<OutputFile>output\win32\debug\helloworld.exe</OutputFile>
<IgnoreImportLibrary>false</IgnoreImportLibrary>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<CustomOptimizationProperty>Custom-Debug</CustomOptimizationProperty>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<TargetName>helloworld</TargetName>
<OutDir>output\win32\release\</OutDir>
Expand All @@ -61,6 +64,9 @@
<OutputFile>output\win32\release\helloworld.exe</OutputFile>
<IgnoreImportLibrary>false</IgnoreImportLibrary>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<CustomOptimizationProperty>Custom-Release</CustomOptimizationProperty>
</PropertyGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<ClCompile>
<PrecompiledHeader>Use</PrecompiledHeader>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@
<OutputFile>output\win64\debug\helloworld.exe</OutputFile>
<IgnoreImportLibrary>false</IgnoreImportLibrary>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<CustomOptimizationProperty>Custom-Debug</CustomOptimizationProperty>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<TargetName>helloworld</TargetName>
<OutDir>output\win64\release\</OutDir>
Expand All @@ -61,6 +64,9 @@
<OutputFile>output\win64\release\helloworld.exe</OutputFile>
<IgnoreImportLibrary>false</IgnoreImportLibrary>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<CustomOptimizationProperty>Custom-Release</CustomOptimizationProperty>
</PropertyGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<ClCompile>
<PrecompiledHeader>Use</PrecompiledHeader>
Expand Down

0 comments on commit 909c4a0

Please sign in to comment.