Skip to content

Commit 1768059

Browse files
committed
Merge branch 'improve-manifest-file-generation-dpiawareness-fix' into 'main'
Improve manifest file generation + dpiAwareness fix See merge request Sharpmake/sharpmake!632
2 parents 604a66f + 94fb129 commit 1768059

File tree

2 files changed

+71
-11
lines changed

2 files changed

+71
-11
lines changed
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// Copyright (c) Ubisoft. All Rights Reserved.
2+
// Licensed under the Apache 2.0 License. See LICENSE.md in the project root for license information.
3+
4+
namespace Sharpmake.Generators.VisualStudio
5+
{
6+
public partial class ProjectOptionsGenerator
7+
{
8+
public static class Template
9+
{
10+
public static class Manifest
11+
{
12+
// Manifest file contents copied from example on msdn page here:
13+
// https://learn.microsoft.com/en-us/windows/win32/hidpi/setting-the-default-dpi-awareness-for-a-process#setting-default-awareness-with-the-application-manifest
14+
public const string FileBegin = @"<?xml version=""1.0"" encoding=""utf-8"" standalone=""yes""?>
15+
<assembly xmlns=""urn:schemas-microsoft-com:asm.v1"" manifestVersion=""1.0"">
16+
<application xmlns=""urn:schemas-microsoft-com:asm.v3"">
17+
<windowsSettings>";
18+
19+
// Include both dpiAware and dpiAwareness values for "backwards compatibility", though it should be good enough to just specify dpiAwareness = PerMonitorV2
20+
// See msdn link above for more info on the dpiAwareness configuration values.
21+
public const string DPIAwarenessSettings = @"
22+
<dpiAware xmlns=""http://schemas.microsoft.com/SMI/2005/WindowsSettings"">true/PM</dpiAware>
23+
<dpiAwareness xmlns=""http://schemas.microsoft.com/SMI/2016/WindowsSettings"">PerMonitorV2,PerMonitor</dpiAwareness>
24+
";
25+
26+
public const string FileEnd =
27+
@" </windowsSettings>
28+
</application>
29+
</assembly>";
30+
}
31+
}
32+
}
33+
}

Sharpmake.Generators/VisualStudio/ProjectOptionsGenerator.cs

Lines changed: 38 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ internal enum ProjectOptionGenerationLevel
2222
All,
2323
}
2424

25-
public class ProjectOptionsGenerator
25+
public partial class ProjectOptionsGenerator
2626
{
2727
// Only MacOS have a subfolder, refer to https://developer.apple.com/library/archive/documentation/CoreFoundation/Conceptual/CFBundles/BundleTypes/BundleTypes.html
2828
private static readonly string AppleAppBinaryRootFolderForMac = "Contents" + XCodeProj.FolderSeparator + "MacOS" + XCodeProj.FolderSeparator;
@@ -1948,37 +1948,64 @@ private void GenerateManifestToolOptions(IGenerationContext context, ProjectOpti
19481948

19491949
Strings manifestInputs = new Strings();
19501950

1951-
string vsManifestFilesPath = Util.SimplifyPath(Path.Combine(context.DevelopmentEnvironment.GetVisualStudioVCRootPath(), "Include", "Manifest"));
1951+
bool generateManifestFile = false;
19521952

19531953
//EnableDpiAwareness
1954+
// Yes and PerMonitor both map to the same value, since dpi scaling really doesn't work well in windows 10+ without PerMonitorV2 specified.
1955+
// This should be safe for older versions too, according to msdn documentation (see link below): "On older versions of Windows, the newer <dpiAwareness> tag will be ignored."
1956+
// https://learn.microsoft.com/en-us/windows/win32/hidpi/setting-the-default-dpi-awareness-for-a-process#setting-default-awareness-with-the-application-manifest
19541957
context.SelectOption
19551958
(
19561959
Options.Option(Options.Vc.ManifestTool.EnableDpiAwareness.Default, () => { context.Options["EnableDpiAwareness"] = FileGeneratorUtilities.RemoveLineTag; }),
1957-
Options.Option(Options.Vc.ManifestTool.EnableDpiAwareness.Yes, () => { context.Options["EnableDpiAwareness"] = "true"; manifestInputs.Add(Path.Combine(vsManifestFilesPath, "dpiaware.manifest")); }),
1958-
Options.Option(Options.Vc.ManifestTool.EnableDpiAwareness.PerMonitor, () => { context.Options["EnableDpiAwareness"] = "PerMonitorHighDPIAware"; manifestInputs.Add(Path.Combine(vsManifestFilesPath, "PerMonitorHighDPIAware.manifest")); }),
1960+
Options.Option(Options.Vc.ManifestTool.EnableDpiAwareness.Yes, () => { context.Options["EnableDpiAwareness"] = "PerMonitorHighDPIAware"; generateManifestFile = true; }),
1961+
Options.Option(Options.Vc.ManifestTool.EnableDpiAwareness.PerMonitor, () => { context.Options["EnableDpiAwareness"] = "PerMonitorHighDPIAware"; generateManifestFile = true; }),
19591962
Options.Option(Options.Vc.ManifestTool.EnableDpiAwareness.No, () => { context.Options["EnableDpiAwareness"] = "false"; })
19601963
);
19611964

1962-
if (context.Configuration.AdditionalManifestFiles.Count > 0)
1965+
if (generateManifestFile)
19631966
{
1964-
context.Options["AdditionalManifestFiles"] = string.Join(";", Util.PathGetRelative(context.ProjectDirectory, context.Configuration.AdditionalManifestFiles));
1965-
manifestInputs.AddRange(context.Configuration.AdditionalManifestFiles);
1967+
var fileGenerator = new XmlFileGenerator();
1968+
fileGenerator.Write(Template.Manifest.FileBegin);
1969+
fileGenerator.Write(Template.Manifest.DPIAwarenessSettings);
1970+
fileGenerator.Write(Template.Manifest.FileEnd);
1971+
1972+
string projFilePath = context.Configuration.ProjectFullFileName;
1973+
projFilePath = projFilePath.Replace('.', '_');
1974+
1975+
FileInfo projectFileInfo = new FileInfo(projFilePath + ".manifest");
1976+
context.Builder.Context.WriteGeneratedFile(context.Project.GetType(), projectFileInfo, fileGenerator);
1977+
1978+
manifestInputs.Add(projectFileInfo.FullName);
19661979
}
1967-
else
1968-
context.Options["AdditionalManifestFiles"] = FileGeneratorUtilities.RemoveLineTag;
1980+
1981+
if (context.Configuration.AdditionalManifestFiles.Count > 0)
1982+
manifestInputs.AddRange(context.Configuration.AdditionalManifestFiles);
19691983

19701984
if (manifestInputs.Count > 0)
19711985
{
19721986
Options.Vc.Linker.EmbedManifest embedManifest = Options.GetObject<Options.Vc.Linker.EmbedManifest>(context.Configuration);
19731987
if (embedManifest == Options.Vc.Linker.EmbedManifest.No)
19741988
throw new NotImplementedException("Sharpmake does not support manifestinputs without embedding the manifest!");
19751989

1976-
var cmdManifests = manifestInputs.Select(p => Bff.CmdLineConvertIncludePathsFunc(context, optionsContext.Resolver, p, "/manifestinput:"));
1990+
if (context.Configuration.IsFastBuild)
1991+
{
1992+
var cmdManifests = manifestInputs.Select(p => Bff.CmdLineConvertIncludePathsFunc(context, optionsContext.Resolver, p, "/manifestinput:"));
19771993

1978-
context.CommandLineOptions["ManifestInputs"] = string.Join($"'{Environment.NewLine} + ' ", cmdManifests);
1994+
// With fastbuild, manifest references are added to the .bff file via command line
1995+
context.Options["AdditionalManifestFiles"] = FileGeneratorUtilities.RemoveLineTag;
1996+
context.CommandLineOptions["ManifestInputs"] = string.Join($"'{Environment.NewLine} + ' ", cmdManifests);
1997+
}
1998+
else
1999+
{
2000+
// With msbuild, manifest references are added to the .vcxproj file via the AdditionalManifestFiles field
2001+
// See VcxProj.cs -> Template.Project.ProjectConfigurationsManifestTool use
2002+
context.Options["AdditionalManifestFiles"] = string.Join(";", Util.PathGetRelative(context.ProjectDirectory, manifestInputs));
2003+
context.CommandLineOptions["ManifestInputs"] = FileGeneratorUtilities.RemoveLineTag;
2004+
}
19792005
}
19802006
else
19812007
{
2008+
context.Options["AdditionalManifestFiles"] = FileGeneratorUtilities.RemoveLineTag;
19822009
context.CommandLineOptions["ManifestInputs"] = FileGeneratorUtilities.RemoveLineTag;
19832010
}
19842011
}

0 commit comments

Comments
 (0)