Skip to content
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

Generate target platform assembly attributes #11499

Merged
5 commits merged into from
Jul 10, 2020
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
2 changes: 1 addition & 1 deletion global.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"tools": {
"dotnet": "5.0.100-preview.8.20351.5",
"dotnet": "5.0.100-preview.8.20359.7",
"runtimes": {
"dotnet": [
"$(MicrosoftNETCoreAppRuntimePackageVersion)"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,15 @@ Copyright (c) .NET Foundation. All rights reserved.
<_Parameter2>%(AssemblyMetadata.Value)</_Parameter2>
</AssemblyAttribute>
</ItemGroup>

<ItemGroup Condition="'$(TargetPlatformIdentifier)' != ''
and '$(MinimumOSPlatform)' != ''
and '$(TargetFrameworkIdentifier)' == '.NETCoreApp'
and $([MSBuild]::VersionGreaterThanOrEquals($(_TargetFrameworkVersionWithoutV), '5.0'))">
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@dsplaisted @mhutch I added the condition on > net5.0 . We won't have the type when it is lower

<AssemblyAttribute Include="System.Runtime.Versioning.MinimumOSPlatformAttribute">
<_Parameter1>$(TargetPlatformIdentifier)$(MinimumOSPlatform)</_Parameter1>
</AssemblyAttribute>
</ItemGroup>
</Target>

<!--
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,11 @@ Copyright (c) .NET Foundation. All rights reserved.
<_UnsupportedTargetFrameworkError>true</_UnsupportedTargetFrameworkError>
</PropertyGroup>

<!--Default MinimumOSPlatform to TargetPlatformVersion-->
<PropertyGroup Condition="'$(MinimumOSPlatform)' == '' and '$(TargetPlatformVersion)' != ''">
joeloff marked this conversation as resolved.
Show resolved Hide resolved
<MinimumOSPlatform>$(TargetPlatformVersion)</MinimumOSPlatform>
</PropertyGroup>

<!--
NOTE: We must not validate the TFM before restore target runs as it prevents adding additional TFM
support from being provided by a nuget package such as MSBuild.Sdk.Extras.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
// Copyright (c) .NET Foundation and contributors. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using System.IO;
using Microsoft.NET.TestFramework;
using Microsoft.NET.TestFramework.Assertions;
using Microsoft.NET.TestFramework.Commands;
using Xunit;
using FluentAssertions;
using Xunit.Abstractions;
using Microsoft.NET.TestFramework.ProjectConstruction;

namespace Microsoft.NET.Build.Tests
{
public class GivenThatWeWantToBuildALibraryWithOSMinimumVersion : SdkTest
{
public GivenThatWeWantToBuildALibraryWithOSMinimumVersion(ITestOutputHelper log) : base(log)
{
}

[Fact]
public void WhenPropertiesAreNotSetItShouldNotGenerateMinimumOSPlatformAttribute()
{
TestProject testProject = SetUpProject();
var testAsset = _testAssetsManager.CreateTestProject(testProject);

var runCommand = new DotnetCommand(Log, "run");
runCommand.WorkingDirectory = Path.Combine(testAsset.TestRoot, testProject.Name);
runCommand.Execute()
.Should()
.Pass().And.HaveStdOutContaining("NO ATTRIBUTE");
}

[Fact]
public void WhenPropertiesAreSetItCanGenerateMinimumOSPlatformAttribute()
{
TestProject testProject = SetUpProject();

var targetPlatformIdentifier = "iOS";
testProject.AdditionalProperties["TargetPlatformIdentifier"] = targetPlatformIdentifier;
testProject.AdditionalProperties["MinimumOSPlatform"] = "13.2";
wli3 marked this conversation as resolved.
Show resolved Hide resolved
testProject.AdditionalProperties["TargetPlatformVersion"] = "14.0";

var testAsset = _testAssetsManager.CreateTestProject(testProject);

var runCommand = new DotnetCommand(Log, "run");
runCommand.WorkingDirectory = Path.Combine(testAsset.TestRoot, testProject.Name);
runCommand.Execute()
.Should()
.Pass().And.HaveStdOutContaining("PlatformName:iOS13.2");
}

[Fact]
public void WhenMinimumOSPlatformISNotSetTargetPlatformVersionIsSetItCanGenerateMinimumOSPlatformAttribute()
{
TestProject testProject = SetUpProject();

var targetPlatformIdentifier = "iOS";
testProject.AdditionalProperties["TargetPlatformIdentifier"] = targetPlatformIdentifier;
testProject.AdditionalProperties["TargetPlatformVersion"] = "13.2";

var testAsset = _testAssetsManager.CreateTestProject(testProject);

var runCommand = new DotnetCommand(Log, "run");
runCommand.WorkingDirectory = Path.Combine(testAsset.TestRoot, testProject.Name);
runCommand.Execute()
.Should()
.Pass().And.HaveStdOutContaining("PlatformName:iOS13.2");
}

private static TestProject SetUpProject()
{
TestProject testProject = new TestProject()
{
Name = "Project",
IsSdkProject = true,
IsExe = true,
TargetFrameworks = "net5.0",
};

testProject.SourceFiles["PrintAttribute.cs"] = _printAttribute;
return testProject;
}

private static readonly string _printAttribute = @"
using System;
using System.Runtime.Versioning;

namespace CustomAttributesTestApp
{
internal static class CustomAttributesTestApp
{
public static void Main()
{
var assembly = typeof(CustomAttributesTestApp).Assembly;
object[] attributes = assembly.GetCustomAttributes(typeof(System.Runtime.Versioning.MinimumOSPlatformAttribute), false);
if (attributes.Length > 0)
{
var attribute = attributes[0] as System.Runtime.Versioning.MinimumOSPlatformAttribute;
Console.WriteLine($""PlatformName:{attribute.PlatformName}"");
}
else
{
Console.WriteLine(""NO ATTRIBUTE"");
}
}
}
}
";

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ public void It_generates_a_single_file_for_self_contained_apps()
.Should()
.Pass();

string[] expectedFiles = { SingleFile, PdbFile, SmallNameDirWord, LargeNameDirWord, GetNativeDll("coreclr"), GetNativeDll("clrjit") };
string[] expectedFiles = { SingleFile, PdbFile, SmallNameDirWord, LargeNameDirWord };
string[] unexpectedFiles = { GetNativeDll("hostfxr"), GetNativeDll("hostpolicy") };

GetPublishDirectory(publishCommand)
Expand Down