Skip to content

Commit ae7e184

Browse files
[XSG] inflator parameter (#31260)
removes newly introduced XamlProcessing attribute in favor of msbuild item metadata. prepare the work to obsolete XamlCompilationAttribute too Co-authored-by: Stephane Delcroix <stephane@delcroix.org>
1 parent 123535b commit ae7e184

File tree

434 files changed

+578
-974
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

434 files changed

+578
-974
lines changed

Directory.Build.props

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,16 @@
22
<Import Project="Sdk.props" Sdk="Microsoft.DotNet.Arcade.Sdk" />
33
<Import Condition="'$(EnvironmentBuildPropsImported)' != 'True'" Project="$(MSBuildThisFileDirectory)eng\Environment.Build.props" />
44

5+
<PropertyGroup>
6+
<!--
7+
Keep backward compatibility with XamlC, XamlCompilationAttribute, and xaml-comp processing instruction
8+
When we're ready to turn this off, we can remove all code depending on this condition, and drop this property group
9+
-->
10+
11+
<_MauiXamlSourceGenBackCompat>true</_MauiXamlSourceGenBackCompat>
12+
<DefineConstants Condition=" '$(_MauiXamlSourceGenBackCompat)' == 'true' ">$(DefineConstants);_MAUIXAML_SOURCEGEN_BACKCOMPAT</DefineConstants>
13+
</PropertyGroup>
14+
515
<PropertyGroup>
616
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
717
<!-- Detailed trimmer warnings, if present -->

src/Controls/samples/Controls.Sample.Embedding/Resources/Styles/Colors.xaml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
<?xml version="1.0" encoding="UTF-8" ?>
2-
<?xaml-comp compile="true" ?>
32
<ResourceDictionary
43
xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
54
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml">

src/Controls/samples/Controls.Sample.Embedding/Resources/Styles/Styles.xaml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
<?xml version="1.0" encoding="UTF-8" ?>
2-
<?xaml-comp compile="true" ?>
32
<ResourceDictionary
43
xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
54
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml">

src/Controls/src/Build.Tasks/XamlCTask.cs

Lines changed: 28 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -203,88 +203,50 @@ public override bool Execute(out IList<Exception> thrownExceptions)
203203

204204
using (var assemblyDefinition = AssemblyDefinition.ReadAssembly(IOPath.GetFullPath(Assembly), readerParameters))
205205
{
206-
CustomAttribute xamlProcessingAttr = null;
207-
if (assemblyDefinition.HasCustomAttributes &&
208-
(xamlProcessingAttr =
209-
assemblyDefinition.CustomAttributes.FirstOrDefault(
210-
ca => ca.AttributeType.FullName == "Microsoft.Maui.Controls.Xaml.XamlProcessingAttribute")) != null)
211-
{
212-
var inflator = (XamlInflator)xamlProcessingAttr.ConstructorArguments[0].Value;
213-
var generateInflatorSwitch = xamlProcessingAttr.ConstructorArguments.Count > 1
214-
&& (bool)xamlProcessingAttr.ConstructorArguments[1].Value;
215-
assemblyInflatorOptions = (generateInflatorSwitch, inflator);
216-
if (!generateInflatorSwitch)
217-
skipassembly = (inflator & XamlInflator.XamlC) != XamlInflator.XamlC && inflator != XamlInflator.Default;
218-
219-
}
220-
221-
#pragma warning disable CS0618 // Type or member is obsolete
206+
#if _MAUIXAML_SOURCEGEN_BACKCOMPAT
222207
CustomAttribute xamlcAttr = null;
223208
if (assemblyDefinition.HasCustomAttributes &&
224209
(xamlcAttr =
225210
assemblyDefinition.CustomAttributes.FirstOrDefault(
226211
ca => ca.AttributeType.FullName == "Microsoft.Maui.Controls.Xaml.XamlCompilationAttribute")) != null)
227212
{
228-
if (xamlProcessingAttr == null)
229-
{
230-
var options = (XamlCompilationOptions)xamlcAttr.ConstructorArguments[0].Value;
231-
if ((options & XamlCompilationOptions.Skip) == XamlCompilationOptions.Skip)
232-
skipassembly = true;
233-
if ((options & XamlCompilationOptions.Compile) == XamlCompilationOptions.Compile)
234-
skipassembly = false;
235-
}
236-
else
237-
LoggingHelper.LogWarning($"{new string(' ', 2)}Assembly has both XamlCompilationAttribute and XamlProcessingAttribute. XamlCompilationAttribute will be ignored.");
213+
var options = (XamlCompilationOptions)xamlcAttr.ConstructorArguments[0].Value;
214+
if ((options & XamlCompilationOptions.Skip) == XamlCompilationOptions.Skip)
215+
skipassembly = true;
216+
if ((options & XamlCompilationOptions.Compile) == XamlCompilationOptions.Compile)
217+
skipassembly = false;
238218
}
239-
#pragma warning restore CS0618 // Type or member is obsolete
240219

241-
xamlcAttr = xamlProcessingAttr = null;
220+
xamlcAttr = null;
221+
#endif
242222

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

248-
if (module.HasCustomAttributes &&
249-
(xamlProcessingAttr =
250-
module.CustomAttributes.FirstOrDefault(
251-
ca => ca.AttributeType.FullName == "Microsoft.Maui.Controls.Xaml.XamlProcessingAttribute")) != null)
252-
{
253-
var inflator = (XamlInflator)xamlProcessingAttr.ConstructorArguments[0].Value;
254-
var generateInflatorSwitch = xamlProcessingAttr.ConstructorArguments.Count > 1
255-
&& (bool)xamlProcessingAttr.ConstructorArguments[1].Value;
256-
moduleInflatorOptions = (generateInflatorSwitch, inflator);
257-
if (!generateInflatorSwitch)
258-
skipmodule = (inflator & XamlInflator.XamlC) != XamlInflator.XamlC && inflator != XamlInflator.Default;
259-
}
260-
261-
#pragma warning disable CS0618 // Type or member is obsolete
228+
#if _MAUIXAML_SOURCEGEN_BACKCOMPAT
262229
if (module.HasCustomAttributes &&
263230
(xamlcAttr =
264231
module.CustomAttributes.FirstOrDefault(
265232
ca => ca.AttributeType.FullName == "Microsoft.Maui.Controls.Xaml.XamlCompilationAttribute")) != null)
266233
{
267-
if (xamlProcessingAttr == null)
268-
{
269-
270-
var options = (XamlCompilationOptions)xamlcAttr.ConstructorArguments[0].Value;
271-
if ((options & XamlCompilationOptions.Skip) == XamlCompilationOptions.Skip)
272-
skipmodule = true;
273-
if ((options & XamlCompilationOptions.Compile) == XamlCompilationOptions.Compile)
274-
skipmodule = false;
275-
}
276-
else
277-
LoggingHelper.LogWarning($"{new string(' ', 2)}Module {module.Name} has both XamlCompilationAttribute and XamlProcessingAttribute. XamlCompilationAttribute will be ignored.");
234+
var options = (XamlCompilationOptions)xamlcAttr.ConstructorArguments[0].Value;
235+
if ((options & XamlCompilationOptions.Skip) == XamlCompilationOptions.Skip)
236+
skipmodule = true;
237+
if ((options & XamlCompilationOptions.Compile) == XamlCompilationOptions.Compile)
238+
skipmodule = false;
278239
}
279-
#pragma warning restore CS0618 // Type or member is obsolete
280-
281-
xamlcAttr = xamlProcessingAttr = null;
240+
xamlcAttr = null;
241+
#endif
282242

283243
LoggingHelper.LogMessage(Low, $"{new string(' ', 2)}Module: {module.Name}");
284244
var resourcesToPrune = new List<EmbeddedResource>();
285245
foreach (var resource in module.Resources.OfType<EmbeddedResource>())
286246
{
287-
var initCompName = "InitializeComponent";
247+
var generateInflatorSwitch = module.Assembly.Name.Name == "Microsoft.Maui.Controls.Xaml.UnitTests";
248+
var initCompName = generateInflatorSwitch ? "InitializeComponentXamlC" : "InitializeComponent";
249+
288250
LoggingHelper.LogMessage(Low, $"{new string(' ', 4)}Resource: {resource.Name}");
289251
string classname;
290252
if (!resource.IsXaml(cache, module, out classname))
@@ -301,53 +263,27 @@ public override bool Execute(out IList<Exception> thrownExceptions)
301263
var skiptype = skipmodule;
302264
(bool, XamlInflator)? typeInflatorOptions = moduleInflatorOptions;
303265

304-
if (typeDef.HasCustomAttributes &&
305-
(xamlProcessingAttr =
306-
typeDef.CustomAttributes.FirstOrDefault(
307-
ca => ca.AttributeType.FullName == "Microsoft.Maui.Controls.Xaml.XamlProcessingAttribute")) != null)
308-
{
309-
var inflator = (XamlInflator)xamlProcessingAttr.ConstructorArguments[0].Value;
310-
var generateInflatorSwitch = xamlProcessingAttr.ConstructorArguments.Count > 1
311-
&& (bool)xamlProcessingAttr.ConstructorArguments[1].Value;
312-
313-
typeInflatorOptions = (generateInflatorSwitch, inflator);
314-
if (generateInflatorSwitch)
315-
{
316-
skiptype = (inflator & XamlInflator.XamlC) != XamlInflator.XamlC && inflator != XamlInflator.Default;
317-
initCompName = "InitializeComponentXamlC";
318-
}
319-
else
320-
skiptype = inflator != XamlInflator.XamlC;
321-
}
322-
#pragma warning disable CS0618 // Type or member is obsolete
266+
#if _MAUIXAML_SOURCEGEN_BACKCOMPAT
323267
if (typeDef.HasCustomAttributes &&
324268
(xamlcAttr =
325269
typeDef.CustomAttributes.FirstOrDefault(
326270
ca => ca.AttributeType.FullName == "Microsoft.Maui.Controls.Xaml.XamlCompilationAttribute")) != null)
327271
{
328-
if (xamlProcessingAttr == null)
329-
{
330-
// XamlCompilationAttribute is obsolete, but we still need to support it
331-
// for backwards compatibility
332-
var options = (XamlCompilationOptions)xamlcAttr.ConstructorArguments[0].Value;
333-
if ((options & XamlCompilationOptions.Skip) == XamlCompilationOptions.Skip)
334-
skiptype = true;
335-
if ((options & XamlCompilationOptions.Compile) == XamlCompilationOptions.Compile)
336-
skiptype = false;
337-
}
338-
else
339-
LoggingHelper.LogWarning($"{new string(' ', 6)}Type {typeDef.Name} has both XamlCompilationAttribute and XamlProcessingAttribute. XamlCompilationAttribute will be ignored.");
272+
var options = (XamlCompilationOptions)xamlcAttr.ConstructorArguments[0].Value;
273+
if ((options & XamlCompilationOptions.Skip) == XamlCompilationOptions.Skip)
274+
skiptype = true;
275+
if ((options & XamlCompilationOptions.Compile) == XamlCompilationOptions.Compile)
276+
skiptype = false;
340277
}
341-
#pragma warning restore CS0618 // Type or member is obsolete
342-
343-
xamlcAttr = xamlProcessingAttr = null;
278+
xamlcAttr = null;
279+
#endif
344280

345281
if (Type != null)
346282
skiptype = !(Type == classname);
347283

348284
if (skiptype && !ForceCompile)
349285
{
350-
LoggingHelper.LogMessage(Low, $"{new string(' ', 6)}has XamlCompilation or XamlProceesing disabling XamlC... skipped.");
286+
LoggingHelper.LogMessage(Low, $"{new string(' ', 6)}has XamlCompilation disabling XamlC... skipped.");
351287
continue;
352288
}
353289

src/Controls/src/Build.Tasks/nuget/buildTransitive/netstandard2.0/Microsoft.Maui.Controls.Common.targets

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,14 @@
2222
<CompilerVisibleItemMetadata Include="AdditionalFiles" MetadataName="ManifestResourceName" />
2323
<CompilerVisibleItemMetadata Include="AdditionalFiles" MetadataName="TargetPath" />
2424
<CompilerVisibleItemMetadata Include="AdditionalFiles" MetadataName="RelativePath" />
25+
<CompilerVisibleItemMetadata Include="AdditionalFiles" MetadataName="Inflator" />
26+
<CompilerVisibleProperty Include="MauiXamlNoWarn" />
27+
<CompilerVisibleItemMetadata Include="AdditionalFiles" MetadataName="NoWarn" />
28+
<CompilerVisibleProperty Include="MauiXamlEnableDiagnostics" />
29+
<CompilerVisibleItemMetadata Include="AdditionalFiles" MetadataName="EnableDiagnostics" />
30+
<CompilerVisibleProperty Include="MauiXamlLineInfo" />
31+
<CompilerVisibleItemMetadata Include="AdditionalFiles" MetadataName="LineInfo" />
32+
<CompilerVisibleProperty Include="MauiXamlNullable" />
33+
<CompilerVisibleItemMetadata Include="AdditionalFiles" MetadataName="Nullable" />
2534
</ItemGroup>
26-
2735
</Project>

src/Controls/src/Build.Tasks/nuget/buildTransitive/netstandard2.0/Microsoft.Maui.Controls.targets

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,11 @@
66
<SynthesizeLinkMetadata>true</SynthesizeLinkMetadata>
77
<EnableDefaultXamlItems Condition="'$(EnableDefaultXamlItems)'==''">True</EnableDefaultXamlItems>
88
<EnableDefaultCssItems Condition="'$(EnableDefaultCssItems)'==''">True</EnableDefaultCssItems>
9+
<!-- default MauiXaml inflators -->
10+
<_MauiXamlInflator Condition="' $(MauiXamlInflator)' != '' ">$(MauiXamlInflator)</_MauiXamlInflator>
11+
<_MauiXamlInflator Condition=" '$(MauiXamlInflator)' == '' And '$(Configuration)' == 'Debug' ">Runtime</_MauiXamlInflator>
12+
<_MauiXamlInflator Condition=" '$(MauiXamlInflator)' == '' And '$(Configuration)' != 'Debug' ">XamlC</_MauiXamlInflator>
13+
914
<!-- 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 -->
1015
<SkipMicrosoftUIXamlCheckTargetPlatformVersion Condition="'$(SkipMicrosoftUIXamlCheckTargetPlatformVersion)'==''">true</SkipMicrosoftUIXamlCheckTargetPlatformVersion>
1116
<SkipMicrosoftUIXamlCheckTargetPlatformVersion Condition="'$(SkipMicrosoftUIXamlCheckTargetPlatformVersion)'=='false'"></SkipMicrosoftUIXamlCheckTargetPlatformVersion>
@@ -73,11 +78,34 @@
7378
Condition="'$(_XFTargetsImported)' == 'true'"/>
7479
</Target>
7580

81+
<Target Name="_MauiXamlComputeInflator">
82+
<ItemGroup>
83+
<!-- Assign the default inflator to MauiXaml that don't have any -->
84+
<!-- there's a roslyn bug that stops parsing value at the first semicolon. replace them all https://github.com/dotnet/roslyn/issues/43970 -->
85+
<MauiXaml Inflator="$([MSBuild]::ValueOrDefault('%(MauiXaml.Inflator)','$(_MauiXamlInflator)').Replace(';', ','))"/>
86+
87+
<_MauiXaml_SG Remove="@(_MauiXaml_SG)" />
88+
<_MauiXaml_RT Remove="@(_MauiXaml_RT)" />
89+
<_MauiXaml_XC Remove="@(_MauiXaml_XC)" />
90+
<_MauiXaml_SG Include="@(MauiXaml)" Condition="$([System.String]::new('%(MauiXaml.Inflator)').Contains('SourceGen', StringComparison.OrdinalIgnoreCase))" />
91+
<_MauiXaml_RT Include="@(MauiXaml)" Condition="$([System.String]::new('%(MauiXaml.Inflator)').Contains('Runtime', StringComparison.OrdinalIgnoreCase))" />
92+
<_MauiXaml_XC Include="@(MauiXaml)" Condition="$([System.String]::new('%(MauiXaml.Inflator)').Contains('XamlC', StringComparison.OrdinalIgnoreCase))" />
93+
<_MauiXaml_AsEmbeddedResource Include="@(_MauiXaml_RT)" />
94+
<_MauiXaml_AsEmbeddedResource Include="@(_MauiXaml_XC)" KeepDuplicates="false" />
95+
</ItemGroup>
96+
</Target>
7697

7798
<!-- Inject MauiXaml and MauiCss as AdditionalFiles for partial type generation-->
99+
<PropertyGroup>
100+
<_MauiInjectXamlCssAdditionalFilesDependsOn>
101+
_MauiXamlComputeInflator;
102+
$(CreateManifestResourceNamesDependsOn);
103+
</_MauiInjectXamlCssAdditionalFilesDependsOn>
104+
</PropertyGroup>
105+
78106
<Target Name="_MauiInjectXamlCssAdditionalFiles"
79107
BeforeTargets="GenerateMSBuildEditorConfigFileShouldRun"
80-
DependsOnTargets="$(CreateManifestResourceNamesDependsOn)">
108+
DependsOnTargets="$(_MauiInjectXamlCssAdditionalFilesDependsOn)">
81109
<ItemGroup>
82110
<_MauiXamlWithResourceNames Remove="@(_MauiXamlWithResourceNames)" />
83111
<_MauiXamlWithTargetPath Remove="@(_MauiXamlWithTargetPath)" />
@@ -113,6 +141,7 @@
113141
TargetPath="%(_MauiXamlWithResourceNames.TargetPath)"
114142
RelativePath="$([MSBuild]::MakeRelative($(MSBuildProjectDirectory), %(_MauiXamlWithResourceNames.TargetPath)))"
115143
ItemSpec="%(_MauiXamlWithResourceNames.OriginalItemSpec)"
144+
Inflator="%(_MauiXamlWithResourceNames.Inflator)"
116145
GenKind="Xaml"/>
117146
<AdditionalFiles Condition="%(_MauiCssWithResourceNames.TargetPath) != ''"
118147
Include="@(_MauiCssWithResourceNames)"
@@ -129,14 +158,19 @@
129158
</Target>
130159

131160

132-
<!-- re-add MauiXaml and MauiCss as EmbeddedResources -->
161+
<!-- re-add MauiXaml and MauiCss as EmbeddedResources for runtime inflation and XamlC -->
133162
<PropertyGroup>
134163
<PrepareResourcesDependsOn>
135164
_MauiAddXamlEmbeddedResources;
136165
$(PrepareResourcesDependsOn);
137166
</PrepareResourcesDependsOn>
167+
<_MauiAddXamlEmbeddedResourcesDependsOn>
168+
_MauiXamlComputeInflator
169+
</_MauiAddXamlEmbeddedResourcesDependsOn>
138170
</PropertyGroup>
139-
<Target Name="_MauiAddXamlEmbeddedResources">
171+
172+
<Target Name="_MauiAddXamlEmbeddedResources"
173+
DependsOnTargets="$(_MauiAddXamlEmbeddedResourcesDependsOn)">
140174
<ItemGroup>
141175
<EmbeddedResource Include="@(MauiXaml)" />
142176
<EmbeddedResource Include="@(MauiCss)" />

0 commit comments

Comments
 (0)