Skip to content

[preview5] Remove linker warnings for native host and COM #17699

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
May 19, 2021
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
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -27,41 +27,21 @@ Copyright (c) .NET Foundation. All rights reserved.
</AssemblyAttribute>
</ItemGroup>

<!-- We disable startup hooks for trimmed apps here so that the feature
switch can flow to the runtimeconfig.json. Startup hooks are disabled
by default since they may require assemblies, types or members that
could be removed by the linker, causing a trimmed app to crash. -->
<PropertyGroup Condition="'$(StartupHookSupport)' == '' And
'$(PublishTrimmed)' == 'true' And
$([MSBuild]::VersionGreaterThanOrEquals($(_TargetFrameworkVersionWithoutV), '6.0'))">
<StartupHookSupport>false</StartupHookSupport>
</PropertyGroup>

<!-- We disable custom resource types for trimmed apps here so that the feature
switch can flow to the runtimeconfig.json. Custom resource types are disabled
by default since they may require assemblies, types or members that
could be removed by the linker, causing a trimmed app to crash. -->
<PropertyGroup Condition="'$(CustomResourceTypesSupport)' == '' And
'$(PublishTrimmed)' == 'true' And
$([MSBuild]::VersionGreaterThanOrEquals($(_TargetFrameworkVersionWithoutV), '6.0'))">
<CustomResourceTypesSupport>false</CustomResourceTypesSupport>
</PropertyGroup>
<PropertyGroup Condition="'$(EnableUnsafeBinaryFormatterInDesigntimeLicenseContextSerialization)' == '' And
'$(PublishTrimmed)' == 'true' And
<!-- We disable features for trimmed apps here so that the feature
switches can flow to the runtimeconfig.json. Features are disabled
by default since they may require assemblies, types or members that
could be removed by the linker, causing a trimmed app to crash. -->
<PropertyGroup Condition="'$(PublishTrimmed)' == 'true' And
$([MSBuild]::VersionGreaterThanOrEquals($(_TargetFrameworkVersionWithoutV), '6.0'))">
<EnableUnsafeBinaryFormatterInDesigntimeLicenseContextSerialization>false</EnableUnsafeBinaryFormatterInDesigntimeLicenseContextSerialization>
<StartupHookSupport Condition="'$(StartupHookSupport)' == ''">false</StartupHookSupport>
<CustomResourceTypesSupport Condition="'$(CustomResourceTypesSupport)' == ''">false</CustomResourceTypesSupport>
<EnableUnsafeBinaryFormatterInDesigntimeLicenseContextSerialization Condition="'$(EnableUnsafeBinaryFormatterInDesigntimeLicenseContextSerialization)' == ''">false</EnableUnsafeBinaryFormatterInDesigntimeLicenseContextSerialization>
<BuiltInComInteropSupport Condition="'$(BuiltInComInteropSupport)' == ''">false</BuiltInComInteropSupport>
<EnableCPlusPlusCLIHostActivation Condition="'$(EnableCPlusPlusCLIHostActivation)' == ''">false</EnableCPlusPlusCLIHostActivation>
<_EnableCallingManagedFunctionFromNativeHosting Condition="'$(_EnableCallingManagedFunctionFromNativeHosting)' == ''">false</_EnableCallingManagedFunctionFromNativeHosting>
</PropertyGroup>

<!-- We disable in-built COM support for trimmed apps here so that the feature
switch can flow to the runtimeconfig.json. Built-in COM support is disabled
by default since they may require assemblies, types or members that
could be removed by the linker, causing a trimmed app to crash. -->
<PropertyGroup Condition="'$(BuiltInComSupport)' == '' And
'$(PublishTrimmed)' == 'true' And
$([MSBuild]::VersionGreaterThanOrEquals($(_TargetFrameworkVersionWithoutV), '6.0'))">
<BuiltInComSupport>false</BuiltInComSupport>
</PropertyGroup>

<PropertyGroup Condition="'$(SuppressTrimAnalysisWarnings)' == '' And ('$(EnableTrimAnalyzer)' == 'true' Or '$(PublishTrimmed)' == 'true')">
<!-- Trim analysis warnings are suppressed for .NET < 6. -->
<SuppressTrimAnalysisWarnings Condition="$([MSBuild]::VersionLessThan('$(TargetFrameworkVersion)', '6.0'))">true</SuppressTrimAnalysisWarnings>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -359,9 +359,14 @@ Copyright (c) .NET Foundation. All rights reserved.
-->

<ItemGroup>
<RuntimeHostConfigurationOption Include=" System.Runtime.InteropServices.Marshal.IsBuiltInComSupported"
Condition="'$(BuiltInComSupport)' != ''"
Value="$(BuiltInComSupport)"
<RuntimeHostConfigurationOption Include="Internal.Runtime.InteropServices.ComponentActivator.IsSupported"
Condition="'$(_EnableCallingManagedFunctionFromNativeHosting)' != ''"
Value="$(_EnableCallingManagedFunctionFromNativeHosting)"
Trim="true" />

<RuntimeHostConfigurationOption Include="Internal.Runtime.InteropServices.InMemoryAssemblyLoader.IsSupported"
Condition="'$(EnableCPlusPlusCLIHostActivation)' != ''"
Value="$(EnableCPlusPlusCLIHostActivation)"
Trim="true" />

<RuntimeHostConfigurationOption Include="System.ComponentModel.TypeConverter.EnableUnsafeBinaryFormatterInDesigntimeLicenseContextSerialization"
Expand Down Expand Up @@ -416,6 +421,11 @@ Copyright (c) .NET Foundation. All rights reserved.
Value="$(UseSystemResourceKeys)"
Trim="true" />

<RuntimeHostConfigurationOption Include="System.Runtime.InteropServices.BuiltInComInterop.IsSupported"
Condition="'$(BuiltInComInteropSupport)' != ''"
Value="$(BuiltInComInteropSupport)"
Trim="true" />

<RuntimeHostConfigurationOption Include="System.Runtime.Serialization.EnableUnsafeBinaryFormatterSerialization"
Condition="'$(EnableUnsafeBinaryFormatterSerialization)' != ''"
Value="$(EnableUnsafeBinaryFormatterSerialization)"
Expand Down
38 changes: 19 additions & 19 deletions src/Tests/Microsoft.NET.Publish.Tests/GivenThatWeWantToRunILLink.cs
Original file line number Diff line number Diff line change
Expand Up @@ -387,11 +387,7 @@ public void ILLink_analysis_warnings_are_disabled_by_default(string targetFramew
publishCommand.Execute($"/p:RuntimeIdentifier={rid}")
.Should().Pass()
// trim analysis warnings are disabled
.And.NotHaveStdOutContaining("warning IL2075")
.And.NotHaveStdOutContaining("warning IL2026")
.And.NotHaveStdOutContaining("warning IL2043")
.And.NotHaveStdOutContaining("warning IL2046")
.And.NotHaveStdOutContaining("warning IL2093");
.And.NotHaveStdOutMatching(@"warning IL\d\d\d\d");
}

[RequiresMSBuildVersionTheory("16.8.0")]
Expand Down Expand Up @@ -453,11 +449,7 @@ public void ILLink_accepts_option_to_disable_analysis_warnings(string targetFram
var publishCommand = new PublishCommand(testAsset);
publishCommand.Execute($"/p:RuntimeIdentifier={rid}")
.Should().Pass()
.And.NotHaveStdOutContaining("warning IL2075")
.And.NotHaveStdOutContaining("warning IL2026")
.And.NotHaveStdOutContaining("warning IL2043")
.And.NotHaveStdOutContaining("warning IL2046")
.And.NotHaveStdOutContaining("warning IL2093");
.And.NotHaveStdOutMatching(@"warning IL\d\d\d\d");
}

[RequiresMSBuildVersionTheory("16.8.0")]
Expand Down Expand Up @@ -603,7 +595,7 @@ public void ILLink_verify_analysis_warnings_hello_world_app_trim_mode_copyused(s
var testAsset = _testAssetsManager.CreateTestProject(testProject, identifier: targetFramework);

var publishCommand = new PublishCommand(Log, Path.Combine(testAsset.TestRoot, testProject.Name));
var result = publishCommand.Execute($"/p:RuntimeIdentifier={rid}", "/p:PublishTrimmed=true", "/p:TrimMode=copyused");
var result = publishCommand.Execute($"/p:RuntimeIdentifier={rid}", "/p:PublishTrimmed=true", "/p:TrimMode=copyused", "/p:TrimmerSingleWarn=false");
result.Should().Pass();
ValidateWarningsOnHelloWorldApp(publishCommand, result, expectedOutput, targetFramework, rid);
}
Expand All @@ -619,15 +611,15 @@ public void ILLink_verify_analysis_warnings_hello_world_app_trim_mode_link(strin
var testAsset = _testAssetsManager.CreateTestProject(testProject, identifier: targetFramework);

var publishCommand = new PublishCommand(Log, Path.Combine(testAsset.TestRoot, testProject.Name));
var result = publishCommand.Execute($"/p:RuntimeIdentifier={rid}", "/p:PublishTrimmed=true");
var result = publishCommand.Execute($"/p:RuntimeIdentifier={rid}", "/p:PublishTrimmed=true", "/p:TrimmerSingleWarn=false");
result.Should().Pass();
ValidateWarningsOnHelloWorldApp(publishCommand, result, Array.Empty<string>(), targetFramework, rid);
}

private void ValidateWarningsOnHelloWorldApp (PublishCommand publishCommand, CommandResult result, string[] expectedOutput, string targetFramework, string rid)
{
// This checks that there are no unexpected warnings, but does not cause failures for missing expected warnings.
var warnings = result.StdOut.Split('\n', '\r', ')').Where(line => line.StartsWith("ILLink :"));
var warnings = result.StdOut.Split('\n', '\r', ')').Where(line => line.StartsWith("warning IL"));
var extraWarnings = warnings.Except(expectedOutput);

StringBuilder errorMessage = new StringBuilder();
Expand Down Expand Up @@ -682,24 +674,32 @@ public void TrimmingOptions_are_defaulted_correctly_on_trimmed_apps(string targe
{
JObject runtimeConfig = JObject.Parse(runtimeConfigContents);
runtimeConfig["runtimeOptions"]["configProperties"]
["System.Runtime.InteropServices.Marshal.IsBuiltInComSupported"].Value<bool>()
["Internal.Runtime.InteropServices.ComponentActivator.IsSupported"].Value<bool>()
.Should().BeFalse();
runtimeConfig["runtimeOptions"]["configProperties"]
["System.StartupHookProvider.IsSupported"].Value<bool>()
["Internal.Runtime.InteropServices.InMemoryAssemblyLoader.IsSupported"].Value<bool>()
.Should().BeFalse();
runtimeConfig["runtimeOptions"]["configProperties"]
["System.ComponentModel.TypeConverter.EnableUnsafeBinaryFormatterInDesigntimeLicenseContextSerialization"].Value<bool>()
.Should().BeFalse();
runtimeConfig["runtimeOptions"]["configProperties"]
["System.Resources.ResourceManager.AllowCustomResourceTypes"].Value<bool>()
.Should().BeFalse();
runtimeConfig["runtimeOptions"]["configProperties"]
["System.ComponentModel.TypeConverter.EnableUnsafeBinaryFormatterInDesigntimeLicenseContextSerialization"].Value<bool>()
["System.Runtime.InteropServices.BuiltInComInterop.IsSupported"].Value<bool>()
.Should().BeFalse();
runtimeConfig["runtimeOptions"]["configProperties"]
["System.StartupHookProvider.IsSupported"].Value<bool>()
.Should().BeFalse();
}
else
{
runtimeConfigContents.Should().NotContain("System.Runtime.InteropServices.Marshal.IsBuiltInComSupported");
runtimeConfigContents.Should().NotContain("System.StartupHookProvider.IsSupported");
runtimeConfigContents.Should().NotContain("System.Resources.ResourceManager.AllowCustomResourceTypes");
runtimeConfigContents.Should().NotContain("Internal.Runtime.InteropServices.ComponentActivator.IsSupported");
runtimeConfigContents.Should().NotContain("Internal.Runtime.InteropServices.InMemoryAssemblyLoader.IsSupported");
runtimeConfigContents.Should().NotContain("System.ComponentModel.TypeConverter.EnableUnsafeBinaryFormatterInDesigntimeLicenseContextSerialization");
runtimeConfigContents.Should().NotContain("System.Resources.ResourceManager.AllowCustomResourceTypes");
runtimeConfigContents.Should().NotContain("System.Runtime.InteropServices.BuiltInComInterop.IsSupported");
runtimeConfigContents.Should().NotContain("System.StartupHookProvider.IsSupported");
}
}

Expand Down