Skip to content

Commit 5bca8ad

Browse files
authored
[build] Automatically add NullableAttributes.cs for netstandard2.0 (#1188)
`netstandard2.0` doesn't have "built-in" compiler support for Nullable Reference Type attributes. Thus when we enable nullable reference types on `netstandard2.0` projects we are responsible for providing the attributes ourselves. Rather than manually repeat this info in each `.csproj` targeting `netstandard2.0`, we can use `Directory.Build.targets` to automatically add `NullableAttributes` in projects that need it. Additionally, we can mark it as `Visible="false"` so the file doesn't show up in Visual Studio Solution Explorer, just like the "real" "built-in" attributes.
1 parent 45437e2 commit 5bca8ad

File tree

7 files changed

+10
-25
lines changed

7 files changed

+10
-25
lines changed

Directory.Build.targets

+9
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,15 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<Project>
33

4+
<!-- Automatically add NRT attribute support for netstandard2.0 projects using NRT -->
5+
<ItemGroup Condition=" '$(TargetFramework)' == 'netstandard2.0' AND '$(Nullable)' == 'enable' ">
6+
<Compile Include="$(MSBuildThisFileDirectory)src\utils\NullableAttributes.cs" Visible="false" />
7+
</ItemGroup>
8+
9+
<PropertyGroup Condition=" '$(TargetFramework)' == 'netstandard2.0' AND '$(Nullable)' == 'enable' ">
10+
<DefineConstants>$(DefineConstants);INTERNAL_NULLABLE_ATTRIBUTES</DefineConstants>
11+
</PropertyGroup>
12+
413
<!-- Add Roslyn analyzers NuGet to all projects -->
514
<ItemGroup Condition=" '$(DisableRoslynAnalyzers)' != 'True' ">
615
<PackageReference Include="Microsoft.CodeAnalysis.FxCopAnalyzers" Version="3.3.0">

src/Java.Interop.Localization/Java.Interop.Localization.csproj

-5
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
<TargetFramework>netstandard2.0</TargetFramework>
55
<LangVersion>8.0</LangVersion>
66
<Nullable>enable</Nullable>
7-
<DefineConstants>INTERNAL_NULLABLE_ATTRIBUTES</DefineConstants>
87
<NeutralLanguage>en</NeutralLanguage>
98
<SignAssembly>true</SignAssembly>
109
<AssemblyOriginatorKeyFile>..\..\product.snk</AssemblyOriginatorKeyFile>
@@ -20,10 +19,6 @@
2019
</Compile>
2120
</ItemGroup>
2221

23-
<ItemGroup>
24-
<Compile Include="..\utils\NullableAttributes.cs" />
25-
</ItemGroup>
26-
2722
<ItemGroup>
2823
<EmbeddedResource Update="Resources.resx">
2924
<Generator>PublicResXFileCodeGenerator</Generator>

src/Java.Interop.Tools.Cecil/Java.Interop.Tools.Cecil.csproj

-5
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
<TargetFramework>netstandard2.0</TargetFramework>
55
<LangVersion>8.0</LangVersion>
66
<Nullable>enable</Nullable>
7-
<DefineConstants>INTERNAL_NULLABLE_ATTRIBUTES</DefineConstants>
87
<SignAssembly>true</SignAssembly>
98
<AssemblyOriginatorKeyFile>..\..\product.snk</AssemblyOriginatorKeyFile>
109
</PropertyGroup>
@@ -17,10 +16,6 @@
1716

1817
<Import Project="..\..\build-tools\scripts\cecil.projitems" />
1918

20-
<ItemGroup>
21-
<Compile Include="..\utils\NullableAttributes.cs" />
22-
</ItemGroup>
23-
2419
<ItemGroup>
2520
<ProjectReference Include="..\Java.Interop.Localization\Java.Interop.Localization.csproj" />
2621
<ProjectReference Include="..\Java.Interop.Tools.Diagnostics\Java.Interop.Tools.Diagnostics.csproj" />

src/Java.Interop.Tools.Diagnostics/Java.Interop.Tools.Diagnostics.csproj

-5
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
<TargetFramework>netstandard2.0</TargetFramework>
55
<LangVersion>8.0</LangVersion>
66
<Nullable>enable</Nullable>
7-
<DefineConstants>INTERNAL_NULLABLE_ATTRIBUTES</DefineConstants>
87
<SignAssembly>true</SignAssembly>
98
<AssemblyOriginatorKeyFile>..\..\product.snk</AssemblyOriginatorKeyFile>
109
</PropertyGroup>
@@ -17,10 +16,6 @@
1716

1817
<Import Project="..\..\build-tools\scripts\cecil.projitems" />
1918

20-
<ItemGroup>
21-
<Compile Include="..\utils\NullableAttributes.cs" />
22-
</ItemGroup>
23-
2419
<ItemGroup>
2520
<PackageReference Include="Microsoft.SourceLink.GitHub" PrivateAssets="All" />
2621
</ItemGroup>

src/Java.Interop.Tools.JavaCallableWrappers/Java.Interop.Tools.JavaCallableWrappers.csproj

-3
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,6 @@
2323
<Compile Include="..\Java.Interop.Tools.TypeNameMappings\Java.Interop.Tools.TypeNameMappings\JavaNativeTypeManager.cs">
2424
<Link>JavaNativeTypeManager.cs</Link>
2525
</Compile>
26-
<Compile Include="..\utils\NullableAttributes.cs">
27-
<Link>NullableAttributes.cs</Link>
28-
</Compile>
2926
</ItemGroup>
3027

3128
<ItemGroup>

src/Java.Interop.Tools.Maven/Java.Interop.Tools.Maven.csproj

-5
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
<TargetFramework>netstandard2.0</TargetFramework>
55
<LangVersion>11.0</LangVersion>
66
<Nullable>enable</Nullable>
7-
<DefineConstants>INTERNAL_NULLABLE_ATTRIBUTES</DefineConstants>
87
<Nullable>enable</Nullable>
98
<SignAssembly>true</SignAssembly>
109
<AssemblyOriginatorKeyFile>..\..\product.snk</AssemblyOriginatorKeyFile>
@@ -16,10 +15,6 @@
1615
<OutputPath>$(UtilityOutputFullPath)</OutputPath>
1716
</PropertyGroup>
1817

19-
<ItemGroup>
20-
<Compile Include="..\utils\NullableAttributes.cs" />
21-
</ItemGroup>
22-
2318
<ItemGroup>
2419
<PackageReference Include="Microsoft.SourceLink.GitHub" PrivateAssets="All" />
2520
</ItemGroup>

src/Java.Interop/Java.Interop.csproj

+1-2
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
</PropertyGroup>
2525
<Import Project="..\..\TargetFrameworkDependentValues.props" />
2626
<PropertyGroup>
27-
<DefineConstants>INTEROP;FEATURE_JNIOBJECTREFERENCE_INTPTRS;INTERNAL_NULLABLE_ATTRIBUTES;$(JavaInteropDefineConstants)</DefineConstants>
27+
<DefineConstants>INTEROP;FEATURE_JNIOBJECTREFERENCE_INTPTRS;$(JavaInteropDefineConstants)</DefineConstants>
2828
<IntermediateOutputPath>$(BaseIntermediateOutputPath)$(Configuration)\$(TargetFramework.ToLowerInvariant())\</IntermediateOutputPath>
2929
<OutputPath>$(ToolOutputFullPath)</OutputPath>
3030
<DocumentationFile>$(ToolOutputFullPath)Java.Interop.xml</DocumentationFile>
@@ -42,7 +42,6 @@
4242
<DefineConstants>FEATURE_JNIENVIRONMENT_JI_PINVOKES;$(DefineConstants)</DefineConstants>
4343
</PropertyGroup>
4444
<ItemGroup>
45-
<Compile Condition=" '$(TargetFramework)' == 'netstandard2.0' " Include="..\utils\NullableAttributes.cs" />
4645
<Compile Remove="Java.Interop\JniLocationException.cs" />
4746
</ItemGroup>
4847
<PropertyGroup>

0 commit comments

Comments
 (0)