Skip to content

Commit

Permalink
Use analyzers from targeting pack for .NET Current
Browse files Browse the repository at this point in the history
Add analyzers to the frameworklist that OOB projects in src/libraries
use, and only auto ProjectReference the analyzers in generators.targets
when not using the analyzers from the targeting pack.
  • Loading branch information
ViktorHofer committed Sep 5, 2022
1 parent b90a734 commit c6d7dcb
Show file tree
Hide file tree
Showing 7 changed files with 78 additions and 75 deletions.
38 changes: 38 additions & 0 deletions eng/generatorProjects.targets
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<Project>

<PropertyGroup>
<GeneratorProjectBaseTargetPath>analyzers/dotnet</GeneratorProjectBaseTargetPath>
<GeneratorProjectBaseTargetPath Condition="'$(AnalyzerLanguage)' != ''">$(GeneratorProjectBaseTargetPath)/$(AnalyzerLanguage)</GeneratorProjectBaseTargetPath>
</PropertyGroup>

<ItemGroup>
<BinPlaceDir Include="$(MicrosoftNetCoreAppRefPackDir)$(GeneratorProjectBaseTargetPath)"
Condition="'$(IsNETCoreAppAnalyzer)' == 'true'" />
</ItemGroup>

<ItemDefinitionGroup>
<TargetPathWithTargetPlatformMoniker>
<GeneratorProjectBaseTargetPath>$(GeneratorProjectBaseTargetPath)</GeneratorProjectBaseTargetPath>
</TargetPathWithTargetPlatformMoniker>
</ItemDefinitionGroup>

<Target Name="GetAnalyzerPackFiles"
DependsOnTargets="$(GenerateNuspecDependsOn)"
Returns="@(_AnalyzerPackFile)">
<PropertyGroup>
<_analyzerPath>analyzers/dotnet</_analyzerPath>
<_analyzerPath Condition="'$(_analyzerPath)' != ''">$(_analyzerPath)/roslyn$(AnalyzerRoslynVersion)</_analyzerPath>
<_analyzerPath Condition="'$(_analyzerPath)' != ''">$(_analyzerPath)/$(AnalyzerLanguage)</_analyzerPath>
</PropertyGroup>

<ItemGroup>
<_AnalyzerPackFile Include="@(_BuildOutputInPackage)" IsSymbol="false" />
<_AnalyzerPackFile Include="@(_TargetPathsToSymbols)" IsSymbol="true" />
<_AnalyzerPackFile PackagePath="$(_analyzerPath)/%(TargetPath)" />
</ItemGroup>

<Error Condition="'%(_AnalyzerPackFile.TargetFramework)' != 'netstandard2.0'"
Text="Analyzers must only target netstandard2.0 since they run in the compiler which targets netstandard2.0. The following files were found to target '%(_AnalyzerPackFile.TargetFramework)': @(_AnalyzerPackFile)" />
</Target>

</Project>
59 changes: 31 additions & 28 deletions eng/generators.targets
Original file line number Diff line number Diff line change
Expand Up @@ -9,23 +9,27 @@
<!-- If the current project is not System.Private.CoreLib, we enable the LibraryImportGenerator source generator
when the project is a C# source project that:
- doesn't target the latest TFM or
- references everything from the targeting pack (OOB) or
- references System.Private.CoreLib, or
- references System.Runtime.InteropServices -->
- doesn't reference the targeting pack (i.e. when inbox) and
- references System.Private.CoreLib, or
- references System.Runtime.InteropServices -->
<EnabledGenerators Include="LibraryImportGenerator"
Condition="'$(EnableLibraryImportGenerator)' == '' and
'$(IsSourceProject)' == 'true' and
'$(MSBuildProjectExtension)' == '.csproj' and
(
'$(TargetFrameworkMoniker)' != '$(NetCoreAppCurrentTargetFrameworkMoniker)' or
'$(DisableImplicitFrameworkReferences)' != 'true' or
(
'@(Reference)' != '' and
@(Reference->AnyHaveMetadataValue('Identity', 'System.Runtime.InteropServices'))
) or
(
'@(ProjectReference)' != '' and
@(ProjectReference->AnyHaveMetadataValue('Identity', '$(CoreLibProject)'))
'$(DisableImplicitFrameworkReferences)' == 'true' and
(
(
'@(Reference)' != '' and
@(Reference->AnyHaveMetadataValue('Identity', 'System.Runtime.InteropServices'))
) or
(
'@(ProjectReference)' != '' and
@(ProjectReference->AnyHaveMetadataValue('Identity', '$(CoreLibProject)'))
)
)
)
)" />
</ItemGroup>
Expand Down Expand Up @@ -55,22 +59,21 @@
OutputItemType="" />
</ItemGroup>

<Target Name="ConfigureGenerators"
DependsOnTargets="ConfigureLibraryImportGenerator"
BeforeTargets="CoreCompile" />

<!-- Microsoft.Interop.LibraryImportGenerator -->
<Target Name="ConfigureLibraryImportGenerator"
Condition="'@(EnabledGenerators)' != '' and @(EnabledGenerators->AnyHaveMetadataValue('Identity', 'LibraryImportGenerator'))"
DependsOnTargets="ResolveProjectReferences"
BeforeTargets="GenerateMSBuildEditorConfigFileShouldRun">
<PropertyGroup>
<LibraryImportGenerator_UseMarshalType>true</LibraryImportGenerator_UseMarshalType>
</PropertyGroup>

<ItemGroup Condition="'$(NetCoreAppCurrentTargetFrameworkMoniker)' == '$(TargetFrameworkMoniker)' and '$(IncludeLibraryImportGeneratorSources)' != 'false'">
</ItemGroup>
</Target>

<Import Project="$(LibrariesProjectRoot)System.Runtime.InteropServices\gen\LibraryImportGenerator\Microsoft.Interop.LibraryImportGenerator.props" />
<!--
Define all of the configuration options supported for the LibraryImportGenerator.
To use, set an MSBuild property with the name of the option to `true`.
See OptionsHelper.cs for more information on usage.
-->
<ItemGroup>
<!--
Use the System.Runtime.InteropServices.Marshal type instead of
the System.Runtime.InteropServices.MarshalEx type when emitting code.
-->
<CompilerVisibleProperty Include="LibraryImportGenerator_UseMarshalType" />
<!--
Generate a stub that forwards to a runtime implemented P/Invoke stub instead
of generating a stub that handles all of the marshalling.
-->
<CompilerVisibleProperty Include="LibraryImportGenerator_GenerateForwarders" />
</ItemGroup>
</Project>
27 changes: 2 additions & 25 deletions src/libraries/Directory.Build.targets
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@
</ItemGroup>
</Target>

<ItemGroup Condition="'@(BinPlaceTargetFrameworks)' == ''">
<ItemGroup>
<!-- Used by the runtime tests to prepare the CORE_ROOT layout. Don't use in libraries. -->
<BinPlaceTargetFrameworks Include="$(NetCoreAppCurrent)-$(TargetOS)"
Condition="'$(BinPlaceForTargetVertical)' == 'true'">
Expand All @@ -95,9 +95,6 @@
<RuntimePath>$(LibrariesAllBinArtifactsPath)</RuntimePath>
</BinPlaceTargetFrameworks>

<BinPlaceDir Include="$(MicrosoftNetCoreAppRefPackDir)analyzers\dotnet\$(AnalyzerLanguage)"
Condition="'$(IsNETCoreAppAnalyzer)' == 'true'" />

<!-- Setup the shared framework directory for testing -->
<BinPlaceTargetFrameworks Include="$(NetCoreAppCurrent)-$(TargetOS)">
<NativePath>$(NetCoreAppCurrentTestHostSharedFrameworkPath)</NativePath>
Expand All @@ -110,8 +107,6 @@
<RefPath Condition="'$(IsNETCoreAppRef)' == 'true'">$(MicrosoftNetCoreAppRefPackRefDir)</RefPath>
<RuntimePath Condition="'$(IsNETCoreAppSrc)' == 'true'">$(MicrosoftNetCoreAppRuntimePackRidLibTfmDir)</RuntimePath>
</BinPlaceTargetFrameworks>

<BinPlaceTargetFrameworks Include="@(AdditionalBinPlaceTargetFrameworks)" />
</ItemGroup>

<Import Project="$(RepositoryEngineeringDir)targetingpacks.targets" />
Expand All @@ -125,6 +120,7 @@

<Import Project="$(RepositoryEngineeringDir)codeOptimization.targets" />
<Import Project="$(RepositoryEngineeringDir)references.targets" />
<Import Project="$(RepositoryEngineeringDir)generatorProjects.targets" Condition="'$(IsGeneratorProject)' == 'true'" />
<Import Project="$(RepositoryEngineeringDir)resolveContract.targets" />
<Import Project="$(RepositoryEngineeringDir)testing\tests.targets" Condition="'$(EnableTestSupport)' == 'true'" />
<Import Project="$(RepositoryEngineeringDir)testing\linker\trimmingTests.targets" Condition="'$(IsTrimmingTestProject)' == 'true'" />
Expand Down Expand Up @@ -216,23 +212,4 @@
</ItemGroup>
</When>
</Choose>

<Target Name="GetAnalyzerPackFiles"
DependsOnTargets="$(GenerateNuspecDependsOn)"
Returns="@(_AnalyzerPackFile)">
<PropertyGroup>
<_analyzerPath>analyzers/dotnet</_analyzerPath>
<_analyzerPath Condition="'$(AnalyzerRoslynVersion)' != ''">$(_analyzerPath)/roslyn$(AnalyzerRoslynVersion)</_analyzerPath>
<_analyzerPath Condition="'$(AnalyzerLanguage)' != ''">$(_analyzerPath)/$(AnalyzerLanguage)</_analyzerPath>
</PropertyGroup>

<ItemGroup>
<_AnalyzerPackFile Include="@(_BuildOutputInPackage)" IsSymbol="false" />
<_AnalyzerPackFile Include="@(_TargetPathsToSymbols)" IsSymbol="true" />
<_AnalyzerPackFile PackagePath="$(_analyzerPath)/%(TargetPath)" />
</ItemGroup>

<Error Condition="'%(_AnalyzerPackFile.TargetFramework)' != 'netstandard2.0'"
Text="Analyzers must only target netstandard2.0 since they run in the compiler which targets netstandard2.0. The following files were found to target '%(_AnalyzerPackFile.TargetFramework)': @(_AnalyzerPackFile)" />
</Target>
</Project>

This file was deleted.

6 changes: 4 additions & 2 deletions src/libraries/frameworklist.targets
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,16 @@
@(SharedFrameworkAssemblyWithoutReferenceAssembly)"
TargetPath="ref/$(NetCoreAppCurrent)"
Condition="!$(NetCoreAppLibraryNoReference.Contains('%(Filename);'))" />
<FrameworkListInputFile Include="@(SharedFrameworkAnalyzerAssembly)"
TargetPath="%(SharedFrameworkAnalyzerAssembly.GeneratorProjectBaseTargetPath)" />
</ItemGroup>
</Target>

<UsingTask TaskName="CreateFrameworkListFile" AssemblyFile="$(DotNetSharedFrameworkTaskFile)"/>
<Target Name="GenerateFrameworkListFile"
DependsOnTargets="GetGenerateFrameworkListFileInputs"
AfterTargets="Build"
Condition="'@(SharedFrameworkAssembly)' != ''"
Condition="'@(SharedFrameworkAssembly)' != '' and '@(SharedFrameworkAnalyzerAssembly)' != ''"
Inputs="@(FrameworkListInputFile)"
Outputs="$(FrameworkListOutputPath)">
<ItemGroup>
Expand All @@ -34,7 +36,7 @@

<CreateFrameworkListFile Files="@(FrameworkListInputFile)"
TargetFile="$(FrameworkListOutputPath)"
TargetFilePrefixes="ref/;runtimes/"
TargetFilePrefixes="ref/;runtimes/;analyzers/"
RootAttributes="@(FrameworkListRootAttribute)" />
</Target>

Expand Down
2 changes: 2 additions & 0 deletions src/libraries/oob.proj
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
</PropertyGroup>

<ItemGroup Condition="'$(BuildTargetFramework)' == '$(NetCoreAppCurrent)' or '$(BuildTargetFramework)' == ''">
<ProjectReference Include="sfx-gen.proj" OutputItemType="SharedFrameworkAnalyzerAssembly" />
<ProjectReference Include="sfx-src.proj" OutputItemType="SharedFrameworkAssembly" />
<ProjectReference Include="oob-src.proj" OutputItemType="OOBAssembly" />
<ProjectReference Include="shims.proj" OutputItemType="SharedFrameworkAssembly" />
Expand All @@ -22,6 +23,7 @@
<!-- Support building reference projects only. -->
<ItemGroup Condition="'$(RefOnly)' == 'true'">
<ProjectReference Remove="@(ProjectReference)" />
<ProjectReference Include="sfx-gen.proj" OutputItemType="SharedFrameworkAnalyzerAssembly" />
<ProjectReference Include="sfx-ref.proj" OutputItemType="SharedFrameworkAssembly" />
<ProjectReference Include="oob-ref.proj" />
<ProjectReference Include="shims.proj" OutputItemType="SharedFrameworkAssembly" />
Expand Down
2 changes: 1 addition & 1 deletion src/libraries/sfx.proj
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@
<ProjectReference Include="sfx-ref.proj">
<OutputItemType Condition="'$(RefOnly)' == 'true'">SharedFrameworkAssembly</OutputItemType>
</ProjectReference>
<ProjectReference Include="sfx-gen.proj" OutputItemType="SharedFrameworkAnalyzerAssembly" />
</ItemGroup>

<!-- Support building only the reference assemblies. -->
<ItemGroup Condition="'$(RefOnly)' != 'true'">
<ProjectReference Include="sfx-src.proj" OutputItemType="SharedFrameworkAssembly" />
<ProjectReference Include="sfx-gen.proj" />
</ItemGroup>

<!-- Generate the targeting pack's framework list so that out-of-band projects can leverage it. -->
Expand Down

0 comments on commit c6d7dcb

Please sign in to comment.