Skip to content

Commit 06e0d92

Browse files
authored
[release/5.0] Support Source Generators in WPF projects (#3846)
[release/5.0] Support source generators in WPF projects (#3846) [5.0.2 servicing]
1 parent 6b56589 commit 06e0d92

File tree

2 files changed

+326
-9
lines changed

2 files changed

+326
-9
lines changed

src/Microsoft.DotNet.Wpf/src/PresentationBuildTasks/Microsoft.WinFX.targets

Lines changed: 72 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -383,12 +383,16 @@
383383
CleanupTemporaryTargetAssembly
384384
</MarkupCompilePass2ForMainAssemblyDependsOn>
385385

386-
<_CompileTargetNameForLocalType Condition="'$(_CompileTargetNameForLocalType)' == ''">_CompileTemporaryAssembly</_CompileTargetNameForLocalType>
386+
<_CompileTargetNameForLocalType Condition="'$(_CompileTargetNameForLocalType)' == ''">_CompileTemporaryAssembly</_CompileTargetNameForLocalType>
387387

388+
<!-- The updated .NET 5.0.2 GenerateTemporaryTargetAssembly behavior that supports source generators is off by default. -->
389+
<IncludePackageReferencesDuringMarkupCompilation Condition="'$(IncludePackageReferencesDuringMarkupCompilation)' == ''">false</IncludePackageReferencesDuringMarkupCompilation>
390+
<_ResolveProjectReferencesTargetName Condition="'$(IncludePackageReferencesDuringMarkupCompilation)' != 'false'">ResolveProjectReferences</_ResolveProjectReferencesTargetName>
391+
<_CompileTemporaryAssemblyDependsOn>BuildOnlySettings;ResolveKeySource;$(_ResolveProjectReferencesTargetName);CoreCompile</_CompileTemporaryAssemblyDependsOn>
388392

389393
</PropertyGroup>
390394

391-
<Target Name="_CompileTemporaryAssembly" DependsOnTargets="BuildOnlySettings;ResolveKeySource;CoreCompile" />
395+
<Target Name="_CompileTemporaryAssembly" DependsOnTargets="$(_CompileTemporaryAssemblyDependsOn)" />
392396

393397

394398
<Target Name="MarkupCompilePass2ForMainAssembly"
@@ -397,7 +401,6 @@
397401

398402
</Target>
399403

400-
401404
<!--
402405
403406
==========================================
@@ -407,23 +410,78 @@
407410
Name : GenerateTemporaryTargetAssembly
408411
409412
-->
413+
<Target Name="GenerateTemporaryTargetAssembly" Condition="'$(_RequireMCPass2ForMainAssembly)' == 'true'">
414+
415+
<Message Text="MSBuildProjectFile is $(MSBuildProjectFile)" Condition="'$(MSBuildTargetsVerbose)' == 'true'" />
416+
417+
<!-- Create a temporary target assembly project name with a random component. -->
418+
<PropertyGroup Condition="'$(IncludePackageReferencesDuringMarkupCompilation)' != 'false'">
419+
<_ParentProjectName>$([System.IO.Path]::GetFileNameWithoutExtension('$(MSBuildProjectFullPath)'))</_ParentProjectName>
420+
<_ParentProjectExtension>$([System.IO.Path]::GetExtension($(MSBuildProjectFullPath)))</_ParentProjectExtension>
421+
<_TemporaryTargetAssemblyProjectNameNoExtension>$([System.String]::Join("_", "$(_ParentProjectName)", "$([System.IO.Path]::GetFileNameWithoutExtension($([System.IO.Path]::GetRandomFileName())))", "wpftmp"))</_TemporaryTargetAssemblyProjectNameNoExtension>
422+
<_TemporaryTargetAssemblyProjectName>$(_TemporaryTargetAssemblyProjectNameNoExtension)$(_ParentProjectExtension)</_TemporaryTargetAssemblyProjectName>
423+
</PropertyGroup>
424+
425+
<!-- Collect the generated NuGet files from the parent project required to support PackageReferences. -->
426+
<ItemGroup Condition="'$(IncludePackageReferencesDuringMarkupCompilation)' != 'false'">
427+
<_SourceGeneratedNuGetPropsAndTargets Include="$(MSBuildProjectExtensionsPath)$(_ParentProjectName)$(_ParentProjectExtension).nuget.g.props"/>
428+
<_SourceGeneratedNuGetPropsAndTargets Include="$(MSBuildProjectExtensionsPath)$(_ParentProjectName)$(_ParentProjectExtension).nuget.g.targets"/>
429+
<_SourceGeneratedNuGetPropsAndTargets Include="$(MSBuildProjectExtensionsPath)$(_ParentProjectName)$(_ParentProjectExtension).nuget.dgspec.json"/>
430+
431+
<_DestGeneratedNuGetPropsAndTargets Include="$(MSBuildProjectExtensionsPath)$(_TemporaryTargetAssemblyProjectName).nuget.g.props"/>
432+
<_DestGeneratedNuGetPropsAndTargets Include="$(MSBuildProjectExtensionsPath)$(_TemporaryTargetAssemblyProjectName).nuget.g.targets"/>
433+
<_DestGeneratedNuGetPropsAndTargets Include="$(MSBuildProjectExtensionsPath)$(_TemporaryTargetAssemblyProjectName).nuget.dgspec.json"/>
434+
435+
</ItemGroup>
436+
437+
<!-- Copy the renamed outer project NuGet props/targets files to the MSBuildProjectExtensionsPath used by NuGet. -->
438+
<Copy Condition="'$(IncludePackageReferencesDuringMarkupCompilation)' != 'false'"
439+
SourceFiles="@(_SourceGeneratedNuGetPropsAndTargets)"
440+
DestinationFiles="@(_DestGeneratedNuGetPropsAndTargets)"
441+
/>
410442

411-
<Target Name="GenerateTemporaryTargetAssembly"
412-
Condition="'$(_RequireMCPass2ForMainAssembly)' == 'true' " >
443+
<!--
444+
Undo TargetFramework and RID append set in Microsoft.NET.RuntimeIdentifierInference.targets to prevent
445+
a duplicate target framework and runtime identifier in the IntermediateOutputPath when the new BuildEngine
446+
instance runs.
413447
414-
<Message Text="MSBuildProjectFile is $(MSBuildProjectFile)" Condition="'$(MSBuildTargetsVerbose)' == 'true'" />
448+
'Append $(RuntimeIdentifier) to directory to output and intermediate paths to prevent bin clashes between
449+
targets. But do not append the implicit default runtime identifier for .NET Framework apps as that would
450+
append a RID the user never mentioned in the path and do so even in the AnyCPU case.'
451+
-->
415452

453+
<PropertyGroup>
454+
<_IntermediateOutputPathNoTargetFrameworkOrRID>$(IntermediateOutputPath)</_IntermediateOutputPathNoTargetFrameworkOrRID>
455+
</PropertyGroup>
456+
457+
<PropertyGroup Condition="'$(IncludePackageReferencesDuringMarkupCompilation)' != 'false' and
458+
'$(AppendRuntimeIdentifierToOutputPath)' == 'true' and '$(RuntimeIdentifier)' != '' and
459+
'$(_UsingDefaultRuntimeIdentifier)' != 'true'">
460+
<_IntermediateOutputPathNoTargetFrameworkOrRID>$([System.Text.RegularExpressions.Regex]::Replace($(_IntermediateOutputPathNoTargetFrameworkOrRID), "$(RuntimeIdentifier)\\$",, System.Text.RegularExpressions.RegexOptions.IgnoreCase))</_IntermediateOutputPathNoTargetFrameworkOrRID>
461+
</PropertyGroup>
462+
463+
<PropertyGroup Condition="'$(IncludePackageReferencesDuringMarkupCompilation)' != 'false' and
464+
'$(AppendTargetFrameworkToOutputPath)' == 'true' and '$(TargetFramework)' != '' and
465+
'$(_UnsupportedTargetFrameworkError)' != 'true'">
466+
<_IntermediateOutputPathNoTargetFrameworkOrRID>$([System.Text.RegularExpressions.Regex]::Replace($(_IntermediateOutputPathNoTargetFrameworkOrRID), "$(TargetFramework)\\$",, System.Text.RegularExpressions.RegexOptions.IgnoreCase))</_IntermediateOutputPathNoTargetFrameworkOrRID>
467+
</PropertyGroup>
468+
469+
<!-- Use the legacy .NET Framework/.NET Core 3.0 GenerateTemporaryTargetAssembly path if 'IncludePackageReferencesDuringMarkupCompilation' is 'false',. -->
416470
<GenerateTemporaryTargetAssembly
417471
CurrentProject="$(MSBuildProjectFullPath)"
418472
MSBuildBinPath="$(MSBuildBinPath)"
419473
ReferencePathTypeName="ReferencePath"
420474
CompileTypeName="Compile"
421475
GeneratedCodeFiles="@(_GeneratedCodeFiles)"
422476
ReferencePath="@(ReferencePath)"
423-
IntermediateOutputPath="$(IntermediateOutputPath)"
477+
BaseIntermediateOutputPath="$(BaseIntermediateOutputPath)"
478+
IntermediateOutputPath="$(_IntermediateOutputPathNoTargetFrameworkOrRID)"
424479
AssemblyName="$(AssemblyName)"
425480
CompileTargetName="$(_CompileTargetNameForLocalType)"
426481
GenerateTemporaryTargetAssemblyDebuggingInformation="$(GenerateTemporaryTargetAssemblyDebuggingInformation)"
482+
IncludePackageReferencesDuringMarkupCompilation="$(IncludePackageReferencesDuringMarkupCompilation)"
483+
Analyzers="$(Analyzers)"
484+
TemporaryTargetAssemblyProjectName="$(_TemporaryTargetAssemblyProjectName)"
427485
>
428486

429487
</GenerateTemporaryTargetAssembly>
@@ -432,8 +490,13 @@
432490
<Output TaskParameter="Include" ItemName="AssemblyForLocalTypeReference" />
433491
</CreateItem>
434492

435-
</Target>
436-
493+
<!-- Remove generated NuGet props/targets files. -->
494+
<Delete
495+
Condition="'$(IncludePackageReferencesDuringMarkupCompilation)' != 'false'
496+
and '$(GenerateTemporaryTargetAssemblyDebuggingInformation)' != 'true'"
497+
Files="@(_DestGeneratedNuGetPropsAndTargets)" />
498+
499+
</Target>
437500

438501
<!--
439502

0 commit comments

Comments
 (0)