Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
10 changes: 10 additions & 0 deletions Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,16 @@
<Import Project="Sdk.props" Sdk="Microsoft.DotNet.Arcade.Sdk" />
<Import Condition="'$(EnvironmentBuildPropsImported)' != 'True'" Project="$(MSBuildThisFileDirectory)eng\Environment.Build.props" />

<PropertyGroup>
<!--
Keep backward compatibility with XamlC, XamlCompilationAttribute, and xaml-comp processing instruction
When we're ready to turn this off, we can remove all code depending on this condition, and drop this property group
-->

<_MauiXamlSourceGenBackCompat>true</_MauiXamlSourceGenBackCompat>
<DefineConstants Condition=" '$(_MauiXamlSourceGenBackCompat)' == 'true' ">$(DefineConstants);_MAUIXAML_SOURCEGEN_BACKCOMPAT</DefineConstants>
</PropertyGroup>

<PropertyGroup>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
<!-- Detailed trimmer warnings, if present -->
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
<?xml version="1.0" encoding="UTF-8" ?>
<?xaml-comp compile="true" ?>
<ResourceDictionary
xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml">
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
<?xml version="1.0" encoding="UTF-8" ?>
<?xaml-comp compile="true" ?>
<ResourceDictionary
xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml">
Expand Down
120 changes: 28 additions & 92 deletions src/Controls/src/Build.Tasks/XamlCTask.cs
Original file line number Diff line number Diff line change
Expand Up @@ -203,88 +203,50 @@ public override bool Execute(out IList<Exception> thrownExceptions)

using (var assemblyDefinition = AssemblyDefinition.ReadAssembly(IOPath.GetFullPath(Assembly), readerParameters))
{
CustomAttribute xamlProcessingAttr = null;
if (assemblyDefinition.HasCustomAttributes &&
(xamlProcessingAttr =
assemblyDefinition.CustomAttributes.FirstOrDefault(
ca => ca.AttributeType.FullName == "Microsoft.Maui.Controls.Xaml.XamlProcessingAttribute")) != null)
{
var inflator = (XamlInflator)xamlProcessingAttr.ConstructorArguments[0].Value;
var generateInflatorSwitch = xamlProcessingAttr.ConstructorArguments.Count > 1
&& (bool)xamlProcessingAttr.ConstructorArguments[1].Value;
assemblyInflatorOptions = (generateInflatorSwitch, inflator);
if (!generateInflatorSwitch)
skipassembly = (inflator & XamlInflator.XamlC) != XamlInflator.XamlC && inflator != XamlInflator.Default;

}

#pragma warning disable CS0618 // Type or member is obsolete
#if _MAUIXAML_SOURCEGEN_BACKCOMPAT
CustomAttribute xamlcAttr = null;
if (assemblyDefinition.HasCustomAttributes &&
(xamlcAttr =
assemblyDefinition.CustomAttributes.FirstOrDefault(
ca => ca.AttributeType.FullName == "Microsoft.Maui.Controls.Xaml.XamlCompilationAttribute")) != null)
{
if (xamlProcessingAttr == null)
{
var options = (XamlCompilationOptions)xamlcAttr.ConstructorArguments[0].Value;
if ((options & XamlCompilationOptions.Skip) == XamlCompilationOptions.Skip)
skipassembly = true;
if ((options & XamlCompilationOptions.Compile) == XamlCompilationOptions.Compile)
skipassembly = false;
}
else
LoggingHelper.LogWarning($"{new string(' ', 2)}Assembly has both XamlCompilationAttribute and XamlProcessingAttribute. XamlCompilationAttribute will be ignored.");
var options = (XamlCompilationOptions)xamlcAttr.ConstructorArguments[0].Value;
if ((options & XamlCompilationOptions.Skip) == XamlCompilationOptions.Skip)
skipassembly = true;
if ((options & XamlCompilationOptions.Compile) == XamlCompilationOptions.Compile)
skipassembly = false;
}
#pragma warning restore CS0618 // Type or member is obsolete

xamlcAttr = xamlProcessingAttr = null;
xamlcAttr = null;
#endif

foreach (var module in assemblyDefinition.Modules)
{
var skipmodule = skipassembly;
(bool, XamlInflator)? moduleInflatorOptions = assemblyInflatorOptions;

if (module.HasCustomAttributes &&
(xamlProcessingAttr =
module.CustomAttributes.FirstOrDefault(
ca => ca.AttributeType.FullName == "Microsoft.Maui.Controls.Xaml.XamlProcessingAttribute")) != null)
{
var inflator = (XamlInflator)xamlProcessingAttr.ConstructorArguments[0].Value;
var generateInflatorSwitch = xamlProcessingAttr.ConstructorArguments.Count > 1
&& (bool)xamlProcessingAttr.ConstructorArguments[1].Value;
moduleInflatorOptions = (generateInflatorSwitch, inflator);
if (!generateInflatorSwitch)
skipmodule = (inflator & XamlInflator.XamlC) != XamlInflator.XamlC && inflator != XamlInflator.Default;
}

#pragma warning disable CS0618 // Type or member is obsolete
#if _MAUIXAML_SOURCEGEN_BACKCOMPAT
if (module.HasCustomAttributes &&
(xamlcAttr =
module.CustomAttributes.FirstOrDefault(
ca => ca.AttributeType.FullName == "Microsoft.Maui.Controls.Xaml.XamlCompilationAttribute")) != null)
{
if (xamlProcessingAttr == null)
{

var options = (XamlCompilationOptions)xamlcAttr.ConstructorArguments[0].Value;
if ((options & XamlCompilationOptions.Skip) == XamlCompilationOptions.Skip)
skipmodule = true;
if ((options & XamlCompilationOptions.Compile) == XamlCompilationOptions.Compile)
skipmodule = false;
}
else
LoggingHelper.LogWarning($"{new string(' ', 2)}Module {module.Name} has both XamlCompilationAttribute and XamlProcessingAttribute. XamlCompilationAttribute will be ignored.");
var options = (XamlCompilationOptions)xamlcAttr.ConstructorArguments[0].Value;
if ((options & XamlCompilationOptions.Skip) == XamlCompilationOptions.Skip)
skipmodule = true;
if ((options & XamlCompilationOptions.Compile) == XamlCompilationOptions.Compile)
skipmodule = false;
}
#pragma warning restore CS0618 // Type or member is obsolete

xamlcAttr = xamlProcessingAttr = null;
xamlcAttr = null;
#endif

LoggingHelper.LogMessage(Low, $"{new string(' ', 2)}Module: {module.Name}");
var resourcesToPrune = new List<EmbeddedResource>();
foreach (var resource in module.Resources.OfType<EmbeddedResource>())
{
var initCompName = "InitializeComponent";
var generateInflatorSwitch = module.Assembly.Name.Name == "Microsoft.Maui.Controls.Xaml.UnitTests";
var initCompName = generateInflatorSwitch ? "InitializeComponentXamlC" : "InitializeComponent";

LoggingHelper.LogMessage(Low, $"{new string(' ', 4)}Resource: {resource.Name}");
string classname;
if (!resource.IsXaml(cache, module, out classname))
Expand All @@ -301,53 +263,27 @@ public override bool Execute(out IList<Exception> thrownExceptions)
var skiptype = skipmodule;
(bool, XamlInflator)? typeInflatorOptions = moduleInflatorOptions;

if (typeDef.HasCustomAttributes &&
(xamlProcessingAttr =
typeDef.CustomAttributes.FirstOrDefault(
ca => ca.AttributeType.FullName == "Microsoft.Maui.Controls.Xaml.XamlProcessingAttribute")) != null)
{
var inflator = (XamlInflator)xamlProcessingAttr.ConstructorArguments[0].Value;
var generateInflatorSwitch = xamlProcessingAttr.ConstructorArguments.Count > 1
&& (bool)xamlProcessingAttr.ConstructorArguments[1].Value;

typeInflatorOptions = (generateInflatorSwitch, inflator);
if (generateInflatorSwitch)
{
skiptype = (inflator & XamlInflator.XamlC) != XamlInflator.XamlC && inflator != XamlInflator.Default;
initCompName = "InitializeComponentXamlC";
}
else
skiptype = inflator != XamlInflator.XamlC;
}
#pragma warning disable CS0618 // Type or member is obsolete
#if _MAUIXAML_SOURCEGEN_BACKCOMPAT
if (typeDef.HasCustomAttributes &&
(xamlcAttr =
typeDef.CustomAttributes.FirstOrDefault(
ca => ca.AttributeType.FullName == "Microsoft.Maui.Controls.Xaml.XamlCompilationAttribute")) != null)
{
if (xamlProcessingAttr == null)
{
// XamlCompilationAttribute is obsolete, but we still need to support it
// for backwards compatibility
var options = (XamlCompilationOptions)xamlcAttr.ConstructorArguments[0].Value;
if ((options & XamlCompilationOptions.Skip) == XamlCompilationOptions.Skip)
skiptype = true;
if ((options & XamlCompilationOptions.Compile) == XamlCompilationOptions.Compile)
skiptype = false;
}
else
LoggingHelper.LogWarning($"{new string(' ', 6)}Type {typeDef.Name} has both XamlCompilationAttribute and XamlProcessingAttribute. XamlCompilationAttribute will be ignored.");
var options = (XamlCompilationOptions)xamlcAttr.ConstructorArguments[0].Value;
if ((options & XamlCompilationOptions.Skip) == XamlCompilationOptions.Skip)
skiptype = true;
if ((options & XamlCompilationOptions.Compile) == XamlCompilationOptions.Compile)
skiptype = false;
}
#pragma warning restore CS0618 // Type or member is obsolete

xamlcAttr = xamlProcessingAttr = null;
xamlcAttr = null;
#endif

if (Type != null)
skiptype = !(Type == classname);

if (skiptype && !ForceCompile)
{
LoggingHelper.LogMessage(Low, $"{new string(' ', 6)}has XamlCompilation or XamlProceesing disabling XamlC... skipped.");
LoggingHelper.LogMessage(Low, $"{new string(' ', 6)}has XamlCompilation disabling XamlC... skipped.");
continue;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,14 @@
<CompilerVisibleItemMetadata Include="AdditionalFiles" MetadataName="ManifestResourceName" />
<CompilerVisibleItemMetadata Include="AdditionalFiles" MetadataName="TargetPath" />
<CompilerVisibleItemMetadata Include="AdditionalFiles" MetadataName="RelativePath" />
<CompilerVisibleItemMetadata Include="AdditionalFiles" MetadataName="Inflator" />
<CompilerVisibleProperty Include="MauiXamlNoWarn" />
<CompilerVisibleItemMetadata Include="AdditionalFiles" MetadataName="NoWarn" />
<CompilerVisibleProperty Include="MauiXamlEnableDiagnostics" />
<CompilerVisibleItemMetadata Include="AdditionalFiles" MetadataName="EnableDiagnostics" />
<CompilerVisibleProperty Include="MauiXamlLineInfo" />
<CompilerVisibleItemMetadata Include="AdditionalFiles" MetadataName="LineInfo" />
<CompilerVisibleProperty Include="MauiXamlNullable" />
<CompilerVisibleItemMetadata Include="AdditionalFiles" MetadataName="Nullable" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@
<SynthesizeLinkMetadata>true</SynthesizeLinkMetadata>
<EnableDefaultXamlItems Condition="'$(EnableDefaultXamlItems)'==''">True</EnableDefaultXamlItems>
<EnableDefaultCssItems Condition="'$(EnableDefaultCssItems)'==''">True</EnableDefaultCssItems>
<!-- default MauiXaml inflators -->
<_MauiXamlInflator Condition="' $(MauiXamlInflator)' != '' ">$(MauiXamlInflator)</_MauiXamlInflator>
<_MauiXamlInflator Condition=" '$(MauiXamlInflator)' == '' And '$(Configuration)' == 'Debug' ">Runtime</_MauiXamlInflator>
<_MauiXamlInflator Condition=" '$(MauiXamlInflator)' == '' And '$(Configuration)' != 'Debug' ">XamlC</_MauiXamlInflator>

<!-- The WINUI check for this only runs when there is an empty string so I just convert false to an empty string to fall in line with our other properties -->
<SkipMicrosoftUIXamlCheckTargetPlatformVersion Condition="'$(SkipMicrosoftUIXamlCheckTargetPlatformVersion)'==''">true</SkipMicrosoftUIXamlCheckTargetPlatformVersion>
<SkipMicrosoftUIXamlCheckTargetPlatformVersion Condition="'$(SkipMicrosoftUIXamlCheckTargetPlatformVersion)'=='false'"></SkipMicrosoftUIXamlCheckTargetPlatformVersion>
Expand Down Expand Up @@ -73,11 +78,34 @@
Condition="'$(_XFTargetsImported)' == 'true'"/>
</Target>

<Target Name="_MauiXamlComputeInflator">
<ItemGroup>
<!-- Assign the default inflator to MauiXaml that don't have any -->
<!-- there's a roslyn bug that stops parsing value at the first semicolon. replace them all https://github.com/dotnet/roslyn/issues/43970 -->
<MauiXaml Inflator="$([MSBuild]::ValueOrDefault('%(MauiXaml.Inflator)','$(_MauiXamlInflator)').Replace(';', ','))"/>

<_MauiXaml_SG Remove="@(_MauiXaml_SG)" />
<_MauiXaml_RT Remove="@(_MauiXaml_RT)" />
<_MauiXaml_XC Remove="@(_MauiXaml_XC)" />
<_MauiXaml_SG Include="@(MauiXaml)" Condition="$([System.String]::new('%(MauiXaml.Inflator)').Contains('SourceGen', StringComparison.OrdinalIgnoreCase))" />
<_MauiXaml_RT Include="@(MauiXaml)" Condition="$([System.String]::new('%(MauiXaml.Inflator)').Contains('Runtime', StringComparison.OrdinalIgnoreCase))" />
<_MauiXaml_XC Include="@(MauiXaml)" Condition="$([System.String]::new('%(MauiXaml.Inflator)').Contains('XamlC', StringComparison.OrdinalIgnoreCase))" />
<_MauiXaml_AsEmbeddedResource Include="@(_MauiXaml_RT)" />
<_MauiXaml_AsEmbeddedResource Include="@(_MauiXaml_XC)" KeepDuplicates="false" />
</ItemGroup>
</Target>

<!-- Inject MauiXaml and MauiCss as AdditionalFiles for partial type generation-->
<PropertyGroup>
<_MauiInjectXamlCssAdditionalFilesDependsOn>
_MauiXamlComputeInflator;
$(CreateManifestResourceNamesDependsOn);
</_MauiInjectXamlCssAdditionalFilesDependsOn>
</PropertyGroup>

<Target Name="_MauiInjectXamlCssAdditionalFiles"
BeforeTargets="GenerateMSBuildEditorConfigFileShouldRun"
DependsOnTargets="$(CreateManifestResourceNamesDependsOn)">
DependsOnTargets="$(_MauiInjectXamlCssAdditionalFilesDependsOn)">
<ItemGroup>
<_MauiXamlWithResourceNames Remove="@(_MauiXamlWithResourceNames)" />
<_MauiXamlWithTargetPath Remove="@(_MauiXamlWithTargetPath)" />
Expand Down Expand Up @@ -113,6 +141,7 @@
TargetPath="%(_MauiXamlWithResourceNames.TargetPath)"
RelativePath="$([MSBuild]::MakeRelative($(MSBuildProjectDirectory), %(_MauiXamlWithResourceNames.TargetPath)))"
ItemSpec="%(_MauiXamlWithResourceNames.OriginalItemSpec)"
Inflator="%(_MauiXamlWithResourceNames.Inflator)"
GenKind="Xaml"/>
<AdditionalFiles Condition="%(_MauiCssWithResourceNames.TargetPath) != ''"
Include="@(_MauiCssWithResourceNames)"
Expand All @@ -129,14 +158,19 @@
</Target>


<!-- re-add MauiXaml and MauiCss as EmbeddedResources -->
<!-- re-add MauiXaml and MauiCss as EmbeddedResources for runtime inflation and XamlC -->
<PropertyGroup>
<PrepareResourcesDependsOn>
_MauiAddXamlEmbeddedResources;
$(PrepareResourcesDependsOn);
</PrepareResourcesDependsOn>
<_MauiAddXamlEmbeddedResourcesDependsOn>
_MauiXamlComputeInflator
</_MauiAddXamlEmbeddedResourcesDependsOn>
</PropertyGroup>
<Target Name="_MauiAddXamlEmbeddedResources">

<Target Name="_MauiAddXamlEmbeddedResources"
DependsOnTargets="$(_MauiAddXamlEmbeddedResourcesDependsOn)">
<ItemGroup>
<EmbeddedResource Include="@(MauiXaml)" />
<EmbeddedResource Include="@(MauiCss)" />
Expand Down
Loading