From ae54a52afc3538e72b779c563b7c48ff0be6da25 Mon Sep 17 00:00:00 2001 From: Sarah Oslund Date: Mon, 22 Jun 2020 09:54:11 -0700 Subject: [PATCH 1/4] Adding backwards compatibility for target platform compilation constants --- .../Microsoft.NET.Sdk.BeforeCommon.targets | 15 ++- .../GivenThatWeWantToBuildALibrary.cs | 101 ++++++++++++++++++ 2 files changed, 115 insertions(+), 1 deletion(-) diff --git a/src/Tasks/Microsoft.NET.Build.Tasks/targets/Microsoft.NET.Sdk.BeforeCommon.targets b/src/Tasks/Microsoft.NET.Build.Tasks/targets/Microsoft.NET.Sdk.BeforeCommon.targets index 8b42b2d6e847..1b2a4eeb193d 100644 --- a/src/Tasks/Microsoft.NET.Build.Tasks/targets/Microsoft.NET.Sdk.BeforeCommon.targets +++ b/src/Tasks/Microsoft.NET.Build.Tasks/targets/Microsoft.NET.Sdk.BeforeCommon.targets @@ -206,10 +206,23 @@ Copyright (c) .NET Foundation. All rights reserved. + + + + <_MatchingPlatformVersions Include="@(SupportedTargetPlatform)" Condition="$([System.String]::Copy('%(Identity)').ToUpper().Contains($(TargetPlatformIdentifier.ToUpper())))" /> + <_SupportedPlatformVersions Include="$([System.Text.RegularExpressions.Regex]::Match('%(_MatchingPlatformVersions.Identity)', 'Version=v\d*.\d*'))" /> + <_SupportedPlatformVersionNumbers Include="@(_SupportedPlatformVersions->'%(Identity)'->TrimStart('Version=v'))" /> + <_SupportedPlatformCompatibleVersions Include="@(_SupportedPlatformVersionNumbers)" Condition=" $(TargetPlatformMinVersion) != '' and %(Identity) != '' and $([MSBuild]::VersionGreaterThanOrEquals(%(Identity), $(TargetPlatformMinVersion))) and $([MSBuild]::VersionLessThan(%(Identity), $(TargetPlatformVersion))) " /> + <_SupportedPlatformCompatibleVersions Include="@(_SupportedPlatformVersionNumbers)" Condition=" $(TargetPlatformMinVersion) == '' and %(Identity) != '' and $([MSBuild]::VersionLessThan(%(Identity), $(TargetPlatformVersion))) " /> + <_ImplicitDefineConstant Include="@(_SupportedPlatformCompatibleVersions->'$(TargetPlatformIdentifier.ToUpper())%(Identity)'->Replace('.', '_'))" /> + + + $(DefineConstants);@(_ImplicitDefineConstant) diff --git a/src/Tests/Microsoft.NET.Build.Tests/GivenThatWeWantToBuildALibrary.cs b/src/Tests/Microsoft.NET.Build.Tests/GivenThatWeWantToBuildALibrary.cs index ef98b64c861d..f8941142ec7c 100644 --- a/src/Tests/Microsoft.NET.Build.Tests/GivenThatWeWantToBuildALibrary.cs +++ b/src/Tests/Microsoft.NET.Build.Tests/GivenThatWeWantToBuildALibrary.cs @@ -447,6 +447,42 @@ public void It_implicitly_defines_compilation_constants_for_the_target_platform( AssertDefinedConstantsOutput(testAsset, targetFramework, new[] { "NETCOREAPP", "NET", "NET5_0", "NETCOREAPP3_1" }.Concat(expectedDefines).ToArray()); } + [Theory] + [InlineData(new[] { "iOS,Version=v1.0", "iOS,Version=v1.1" }, "1.0", "ios", "1.1", new[] { "IOS", "IOS1_1", "IOS1_0" })] + [InlineData(new[] { "Andriod,Version=v10.10", "Android,Version=v11.11", "Andriod,Version=v12.12", "Android,Version=v13.13" }, "11.11", "android", "12.12", new[] { "ANDROID", "ANDROID11_11", "ANDROID12_12" })] + [InlineData(new[] { "Windows,Version=v7.0", "Windows,Version=v8.0", "Andriod,Version=v1.0", "iOS,Version=v1.0" }, "1.0", "windows", "8.0", new[] { "WINDOWS", "WINDOWS7_0", "WINDOWS8_0" })] + public void It_implicitly_defines_compilation_constants_for_the_target_platform_with_backwards_compatibility(string[] supportedTargetPlatform, string targetPlatformMinVersion, string targetPlatformIdentifier, string targetPlatformVersion, string[] expectedDefines) + { + var targetFramework = "net5.0"; + var testAsset = _testAssetsManager + .CopyTestAsset("AppWithLibrary", "ImplicitFrameworkConstants", targetFramework) + .WithSource() + .WithTargetFramework(targetFramework) + .WithProjectChanges(project => + { + // Manually set target plaform properties + var ns = project.Root.Name.Namespace; + var propGroup = new XElement(ns + "PropertyGroup"); + project.Root.Add(propGroup); + + var platformIdentifier = new XElement(ns + "TargetPlatformIdentifier", targetPlatformIdentifier); + propGroup.Add(platformIdentifier); + var platformVersion = new XElement(ns + "TargetPlatformVersion", targetPlatformVersion); + propGroup.Add(platformVersion); + var platformMinVersion = new XElement(ns + "TargetPlatformMinVersion", targetPlatformMinVersion); + propGroup.Add(platformMinVersion); + + var itemGroup = new XElement(ns + "ItemGroup"); + project.Root.Add(itemGroup); + foreach (var targetPlatform in supportedTargetPlatform) + { + itemGroup.Add(new XElement(ns + "SupportedTargetPlatform", new XAttribute("Include", targetPlatform))); + } + }); + + AssertDefinedConstantsOutput(testAsset, targetFramework, new[] { "NETCOREAPP", "NET", "NET5_0", "NETCOREAPP3_1" }.Concat(expectedDefines).ToArray()); + } + private void AssertDefinedConstantsOutput(TestAsset testAsset, string targetFramework, string[] expectedDefines) { var libraryProjectDirectory = Path.Combine(testAsset.TestRoot, "TestLibrary"); @@ -468,6 +504,71 @@ private void AssertDefinedConstantsOutput(TestAsset testAsset, string targetFram definedConstants.Should().BeEquivalentTo(new[] { "DEBUG", "TRACE" }.Concat(expectedDefines).ToArray()); } + [WindowsOnlyTheory] + [InlineData("netcoreapp3.1", new[] { "NETCOREAPP", "NETCOREAPP3_1" })] + [InlineData("net5.0", new[] { "NETCOREAPP", "NETCOREAPP3_1", "NET", "NET5_0", "WINDOWS", "WINDOWS7_0" }, "windows", "7.0")] + public void It_can_use_implicitly_defined_compilation_constants(string targetFramework, string[] expectedOutput, string targetPlatformIdentifier = null, string targetPlatformVersion = null) + { + var testProj = new TestProject() + { + Name = "CompilationConstants", + TargetFrameworks = targetFramework, + IsExe = true, + IsSdkProject = true + }; + if (targetPlatformIdentifier != null) + { + testProj.AdditionalProperties["TargetPlatformIdentifier"] = targetPlatformIdentifier; + testProj.AdditionalProperties["TargetPlatformVersion"] = targetPlatformVersion; + } + var testAsset = _testAssetsManager.CreateTestProject(testProj); + File.WriteAllText(Path.Combine(testAsset.Path, testProj.Name, $"{testProj.Name}.cs"), @" +using System; +class Program +{ + static void Main(string[] args) + { + #if NETCOREAPP + Console.WriteLine(""NETCOREAPP""); + #endif + #if NETCOREAPP2_1 + Console.WriteLine(""NETCOREAPP2_1""); + #endif + #if NETCOREAPP3_1 + Console.WriteLine(""NETCOREAPP3_1""); + #endif + #if NET + Console.WriteLine(""NET""); + #endif + #if NET5_0 + Console.WriteLine(""NET5_0""); + #endif + #if WINDOWS + Console.WriteLine(""WINDOWS""); + #endif + #if WINDOWS7_0 + Console.WriteLine(""WINDOWS7_0""); + #endif + #if IOS + Console.WriteLine(""IOS""); + #endif + #if IOS7_0 + Console.WriteLine(""IOS7_0""); + #endif + } +}"); + + var buildCommand = new BuildCommand(Log, Path.Combine(testAsset.Path, testProj.Name)); + buildCommand + .Execute() + .Should() + .Pass(); + + var runCommand = new RunExeCommand(Log, Path.Combine(buildCommand.GetOutputDirectory(targetFramework).FullName, $"{testProj.Name}.exe")); + var stdOut = runCommand.Execute().StdOut.Split(Environment.NewLine.ToCharArray()).Where(line => !string.IsNullOrWhiteSpace(line)); + stdOut.Should().BeEquivalentTo(expectedOutput); + } + [Theory] [InlineData(false)] [InlineData(true)] From 38b03a3193e8664138e4b3eca87a79b8832e5100 Mon Sep 17 00:00:00 2001 From: Sarah Oslund Date: Mon, 29 Jun 2020 14:05:32 -0700 Subject: [PATCH 2/4] Converting SupportedTargetPlatform format --- .../targets/Microsoft.NET.Sdk.BeforeCommon.targets | 7 ++----- .../GivenThatWeWantToBuildALibrary.cs | 6 +++--- 2 files changed, 5 insertions(+), 8 deletions(-) diff --git a/src/Tasks/Microsoft.NET.Build.Tasks/targets/Microsoft.NET.Sdk.BeforeCommon.targets b/src/Tasks/Microsoft.NET.Build.Tasks/targets/Microsoft.NET.Sdk.BeforeCommon.targets index 1b2a4eeb193d..a974c85853ce 100644 --- a/src/Tasks/Microsoft.NET.Build.Tasks/targets/Microsoft.NET.Sdk.BeforeCommon.targets +++ b/src/Tasks/Microsoft.NET.Build.Tasks/targets/Microsoft.NET.Sdk.BeforeCommon.targets @@ -210,11 +210,8 @@ Copyright (c) .NET Foundation. All rights reserved. - <_MatchingPlatformVersions Include="@(SupportedTargetPlatform)" Condition="$([System.String]::Copy('%(Identity)').ToUpper().Contains($(TargetPlatformIdentifier.ToUpper())))" /> - <_SupportedPlatformVersions Include="$([System.Text.RegularExpressions.Regex]::Match('%(_MatchingPlatformVersions.Identity)', 'Version=v\d*.\d*'))" /> - <_SupportedPlatformVersionNumbers Include="@(_SupportedPlatformVersions->'%(Identity)'->TrimStart('Version=v'))" /> - <_SupportedPlatformCompatibleVersions Include="@(_SupportedPlatformVersionNumbers)" Condition=" $(TargetPlatformMinVersion) != '' and %(Identity) != '' and $([MSBuild]::VersionGreaterThanOrEquals(%(Identity), $(TargetPlatformMinVersion))) and $([MSBuild]::VersionLessThan(%(Identity), $(TargetPlatformVersion))) " /> - <_SupportedPlatformCompatibleVersions Include="@(_SupportedPlatformVersionNumbers)" Condition=" $(TargetPlatformMinVersion) == '' and %(Identity) != '' and $([MSBuild]::VersionLessThan(%(Identity), $(TargetPlatformVersion))) " /> + <_SupportedPlatformCompatibleVersions Include="@(SupportedTargetPlatform)" Condition=" $(TargetPlatformMinVersion) != '' and %(Identity) != '' and $([MSBuild]::VersionGreaterThanOrEquals(%(Identity), $(TargetPlatformMinVersion))) and $([MSBuild]::VersionLessThan(%(Identity), $(TargetPlatformVersion))) " /> + <_SupportedPlatformCompatibleVersions Include="@(SupportedTargetPlatform)" Condition=" $(TargetPlatformMinVersion) == '' and %(Identity) != '' and $([MSBuild]::VersionLessThan(%(Identity), $(TargetPlatformVersion))) " /> <_ImplicitDefineConstant Include="@(_SupportedPlatformCompatibleVersions->'$(TargetPlatformIdentifier.ToUpper())%(Identity)'->Replace('.', '_'))" /> diff --git a/src/Tests/Microsoft.NET.Build.Tests/GivenThatWeWantToBuildALibrary.cs b/src/Tests/Microsoft.NET.Build.Tests/GivenThatWeWantToBuildALibrary.cs index f8941142ec7c..dd4d395ac851 100644 --- a/src/Tests/Microsoft.NET.Build.Tests/GivenThatWeWantToBuildALibrary.cs +++ b/src/Tests/Microsoft.NET.Build.Tests/GivenThatWeWantToBuildALibrary.cs @@ -448,9 +448,9 @@ public void It_implicitly_defines_compilation_constants_for_the_target_platform( } [Theory] - [InlineData(new[] { "iOS,Version=v1.0", "iOS,Version=v1.1" }, "1.0", "ios", "1.1", new[] { "IOS", "IOS1_1", "IOS1_0" })] - [InlineData(new[] { "Andriod,Version=v10.10", "Android,Version=v11.11", "Andriod,Version=v12.12", "Android,Version=v13.13" }, "11.11", "android", "12.12", new[] { "ANDROID", "ANDROID11_11", "ANDROID12_12" })] - [InlineData(new[] { "Windows,Version=v7.0", "Windows,Version=v8.0", "Andriod,Version=v1.0", "iOS,Version=v1.0" }, "1.0", "windows", "8.0", new[] { "WINDOWS", "WINDOWS7_0", "WINDOWS8_0" })] + [InlineData(new[] { "1.0", "1.1" }, "1.0", "ios", "1.1", new[] { "IOS", "IOS1_1", "IOS1_0" })] + [InlineData(new[] { "10.10", "11.11", "12.12", "13.13" }, "11.11", "android", "12.12", new[] { "ANDROID", "ANDROID11_11", "ANDROID12_12" })] + [InlineData(new[] { "7.0", "8.0", "10.0.19041", "11.0.0" }, "1.0", "windows", "11.0.0", new[] { "WINDOWS", "WINDOWS7_0", "WINDOWS8_0", "WINDOWS10_0_19041", "WINDOWS11_0_0" })] public void It_implicitly_defines_compilation_constants_for_the_target_platform_with_backwards_compatibility(string[] supportedTargetPlatform, string targetPlatformMinVersion, string targetPlatformIdentifier, string targetPlatformVersion, string[] expectedDefines) { var targetFramework = "net5.0"; From 691b27d6c76444e5370a3014a6e88e43109f39b4 Mon Sep 17 00:00:00 2001 From: Sarah Oslund Date: Wed, 1 Jul 2020 08:46:40 -0700 Subject: [PATCH 3/4] Removing min version for target platform preprocessor symbols --- .../targets/Microsoft.NET.Sdk.BeforeCommon.targets | 3 +-- .../GivenThatWeWantToBuildALibrary.cs | 10 ++++------ 2 files changed, 5 insertions(+), 8 deletions(-) diff --git a/src/Tasks/Microsoft.NET.Build.Tasks/targets/Microsoft.NET.Sdk.BeforeCommon.targets b/src/Tasks/Microsoft.NET.Build.Tasks/targets/Microsoft.NET.Sdk.BeforeCommon.targets index a974c85853ce..c3f08df257a4 100644 --- a/src/Tasks/Microsoft.NET.Build.Tasks/targets/Microsoft.NET.Sdk.BeforeCommon.targets +++ b/src/Tasks/Microsoft.NET.Build.Tasks/targets/Microsoft.NET.Sdk.BeforeCommon.targets @@ -210,8 +210,7 @@ Copyright (c) .NET Foundation. All rights reserved. - <_SupportedPlatformCompatibleVersions Include="@(SupportedTargetPlatform)" Condition=" $(TargetPlatformMinVersion) != '' and %(Identity) != '' and $([MSBuild]::VersionGreaterThanOrEquals(%(Identity), $(TargetPlatformMinVersion))) and $([MSBuild]::VersionLessThan(%(Identity), $(TargetPlatformVersion))) " /> - <_SupportedPlatformCompatibleVersions Include="@(SupportedTargetPlatform)" Condition=" $(TargetPlatformMinVersion) == '' and %(Identity) != '' and $([MSBuild]::VersionLessThan(%(Identity), $(TargetPlatformVersion))) " /> + <_SupportedPlatformCompatibleVersions Include="@(SupportedTargetPlatform)" Condition=" %(Identity) != '' and $([MSBuild]::VersionLessThan(%(Identity), $(TargetPlatformVersion))) " /> <_ImplicitDefineConstant Include="@(_SupportedPlatformCompatibleVersions->'$(TargetPlatformIdentifier.ToUpper())%(Identity)'->Replace('.', '_'))" /> diff --git a/src/Tests/Microsoft.NET.Build.Tests/GivenThatWeWantToBuildALibrary.cs b/src/Tests/Microsoft.NET.Build.Tests/GivenThatWeWantToBuildALibrary.cs index dd4d395ac851..d7d5ca903603 100644 --- a/src/Tests/Microsoft.NET.Build.Tests/GivenThatWeWantToBuildALibrary.cs +++ b/src/Tests/Microsoft.NET.Build.Tests/GivenThatWeWantToBuildALibrary.cs @@ -448,10 +448,10 @@ public void It_implicitly_defines_compilation_constants_for_the_target_platform( } [Theory] - [InlineData(new[] { "1.0", "1.1" }, "1.0", "ios", "1.1", new[] { "IOS", "IOS1_1", "IOS1_0" })] - [InlineData(new[] { "10.10", "11.11", "12.12", "13.13" }, "11.11", "android", "12.12", new[] { "ANDROID", "ANDROID11_11", "ANDROID12_12" })] - [InlineData(new[] { "7.0", "8.0", "10.0.19041", "11.0.0" }, "1.0", "windows", "11.0.0", new[] { "WINDOWS", "WINDOWS7_0", "WINDOWS8_0", "WINDOWS10_0_19041", "WINDOWS11_0_0" })] - public void It_implicitly_defines_compilation_constants_for_the_target_platform_with_backwards_compatibility(string[] supportedTargetPlatform, string targetPlatformMinVersion, string targetPlatformIdentifier, string targetPlatformVersion, string[] expectedDefines) + [InlineData(new[] { "1.0", "1.1" }, "ios", "1.1", new[] { "IOS", "IOS1_1", "IOS1_0" })] + [InlineData(new[] { "11.11", "12.12", "13.13" }, "android", "12.12", new[] { "ANDROID", "ANDROID11_11", "ANDROID12_12" })] + [InlineData(new[] { "7.0", "8.0", "10.0.19041", "11.0.0" }, "windows", "11.0.0", new[] { "WINDOWS", "WINDOWS7_0", "WINDOWS8_0", "WINDOWS10_0_19041", "WINDOWS11_0_0" })] + public void It_implicitly_defines_compilation_constants_for_the_target_platform_with_backwards_compatibility(string[] supportedTargetPlatform, string targetPlatformIdentifier, string targetPlatformVersion, string[] expectedDefines) { var targetFramework = "net5.0"; var testAsset = _testAssetsManager @@ -469,8 +469,6 @@ public void It_implicitly_defines_compilation_constants_for_the_target_platform_ propGroup.Add(platformIdentifier); var platformVersion = new XElement(ns + "TargetPlatformVersion", targetPlatformVersion); propGroup.Add(platformVersion); - var platformMinVersion = new XElement(ns + "TargetPlatformMinVersion", targetPlatformMinVersion); - propGroup.Add(platformMinVersion); var itemGroup = new XElement(ns + "ItemGroup"); project.Root.Add(itemGroup); From ade858d763b4c219619739aa315da6bf1ec76e0c Mon Sep 17 00:00:00 2001 From: Sarah Oslund Date: Wed, 1 Jul 2020 10:55:54 -0700 Subject: [PATCH 4/4] Diabling implicit framework references for preprocessor tests --- .../Microsoft.NET.Build.Tests/GivenThatWeWantToBuildALibrary.cs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/Tests/Microsoft.NET.Build.Tests/GivenThatWeWantToBuildALibrary.cs b/src/Tests/Microsoft.NET.Build.Tests/GivenThatWeWantToBuildALibrary.cs index d7d5ca903603..01a79dc6de4d 100644 --- a/src/Tests/Microsoft.NET.Build.Tests/GivenThatWeWantToBuildALibrary.cs +++ b/src/Tests/Microsoft.NET.Build.Tests/GivenThatWeWantToBuildALibrary.cs @@ -469,6 +469,8 @@ public void It_implicitly_defines_compilation_constants_for_the_target_platform_ propGroup.Add(platformIdentifier); var platformVersion = new XElement(ns + "TargetPlatformVersion", targetPlatformVersion); propGroup.Add(platformVersion); + var disableUnnecessaryImplicitFrameworkReferencesForThisTest = new XElement(ns + "DisableImplicitFrameworkReferences", "true"); + propGroup.Add(disableUnnecessaryImplicitFrameworkReferencesForThisTest); var itemGroup = new XElement(ns + "ItemGroup"); project.Root.Add(itemGroup);