From 3d31ddc0a450cd8b19aac241b42184ad031f6853 Mon Sep 17 00:00:00 2001 From: Andy Gocke Date: Sat, 22 Jan 2022 17:59:02 -0800 Subject: [PATCH 1/4] Add analyzer bootstrap The existing integration test build will now also use the live analyzer built first. --- build.cmd | 2 +- build.sh | 20 ++++++++++++++++++++ eng/Analyzers.props | 11 +++++++++++ eng/bootstrap.proj | 10 ++++++++++ eng/build.ps1 | 19 +++++++++++++++++++ eng/common/build.ps1 | 8 ++++---- 6 files changed, 65 insertions(+), 5 deletions(-) create mode 100644 eng/bootstrap.proj create mode 100644 eng/build.ps1 diff --git a/build.cmd b/build.cmd index 675fdf83f6a0..5f7196bcdf14 100644 --- a/build.cmd +++ b/build.cmd @@ -1,2 +1,2 @@ @echo off -powershell -ExecutionPolicy ByPass -NoProfile -command "& """%~dp0eng\common\Build.ps1""" -restore -build %*" +powershell -ExecutionPolicy ByPass -NoProfile -command "& """%~dp0eng\Build.ps1""" -restore -build %*" diff --git a/build.sh b/build.sh index 8477d5af8817..e2aa1080eeef 100755 --- a/build.sh +++ b/build.sh @@ -1,7 +1,27 @@ #!/usr/bin/env bash +# Stop script if unbound variable found (use ${var:-} if intentional) +set -u + +# Stop script if subcommand fails +set -e + source="${BASH_SOURCE[0]}" +bootstrap=false + +args="" +while [[ $# > 0 ]]; do + opt="$(echo "${1/#--/-}" | tr "[:upper:]" "[:lower:]")" + case "$opt" in + -integrationtest) + bootstrap=true + ;; + esac + args="$args $1" + shift +done + # resolve $SOURCE until the file is no longer a symlink while [[ -h $source ]]; do scriptroot="$( cd -P "$( dirname "$source" )" && pwd )" diff --git a/eng/Analyzers.props b/eng/Analyzers.props index 18e658e00583..275038c62615 100644 --- a/eng/Analyzers.props +++ b/eng/Analyzers.props @@ -4,4 +4,15 @@ + + + + + true + + + + + + diff --git a/eng/bootstrap.proj b/eng/bootstrap.proj new file mode 100644 index 000000000000..29b6451bd3f4 --- /dev/null +++ b/eng/bootstrap.proj @@ -0,0 +1,10 @@ + + + + + + + \ No newline at end of file diff --git a/eng/build.ps1 b/eng/build.ps1 new file mode 100644 index 000000000000..c83579597dae --- /dev/null +++ b/eng/build.ps1 @@ -0,0 +1,19 @@ +[CmdletBinding(PositionalBinding=$false)] +Param( + [switch] $integrationTest, + [Parameter(ValueFromRemainingArguments=$true)][String[]]$remaining +) + +. (Join-Path $PSScriptRoot "common/tools.ps1") + +echo $ArtifactsDir + +$args = $remaining.Clone() + +if ($integrationTest) { + dotnet build (Join-Path $PSScriptRoot bootstrap.proj) + $args += "-integrationTest" + $args += "/p:BootstrapBuildPath=$ArtifactsDir/bootstrap" +} + +powershell -ExecutionPolicy ByPass -NoProfile (Join-Path $PSScriptRoot "common/build.ps1") @args \ No newline at end of file diff --git a/eng/common/build.ps1 b/eng/common/build.ps1 index 8943da242f6e..cefc8cc891e7 100644 --- a/eng/common/build.ps1 +++ b/eng/common/build.ps1 @@ -33,7 +33,7 @@ Param( # Unset 'Platform' environment variable to avoid unwanted collision in InstallDotNetCore.targets file # some computer has this env var defined (e.g. Some HP) if($env:Platform) { - $env:Platform="" + $env:Platform="" } function Print-Usage() { Write-Host "Common settings:" @@ -98,10 +98,10 @@ function Build { # Re-assign properties to a new variable because PowerShell doesn't let us append properties directly for unclear reasons. # Explicitly set the type as string[] because otherwise PowerShell would make this char[] if $properties is empty. [string[]] $msbuildArgs = $properties - - # Resolve relative project paths into full paths + + # Resolve relative project paths into full paths $projects = ($projects.Split(';').ForEach({Resolve-Path $_}) -join ';') - + $msbuildArgs += "/p:Projects=$projects" $properties = $msbuildArgs } From 42c199a39a24c26070549aa4f4532bccace66c12 Mon Sep 17 00:00:00 2001 From: Andy Gocke Date: Sat, 22 Jan 2022 23:02:58 -0800 Subject: [PATCH 2/4] Fix problems found by bootstrap and silence trim warnings --- eng/Analyzers.props | 3 ++- src/ILLink.RoslynAnalyzer/RequiresAnalyzerBase.cs | 4 ++-- src/linker/Linker.Steps/OutputStep.cs | 3 +++ src/linker/Linker/Driver.cs | 10 ++++++++++ .../RequiresCapability/BasicRequires.cs | 6 ++++++ 5 files changed, 23 insertions(+), 3 deletions(-) diff --git a/eng/Analyzers.props b/eng/Analyzers.props index 275038c62615..37816d62cac7 100644 --- a/eng/Analyzers.props +++ b/eng/Analyzers.props @@ -7,7 +7,8 @@ - + + true diff --git a/src/ILLink.RoslynAnalyzer/RequiresAnalyzerBase.cs b/src/ILLink.RoslynAnalyzer/RequiresAnalyzerBase.cs index d52bc04bd7b0..aff22c907948 100644 --- a/src/ILLink.RoslynAnalyzer/RequiresAnalyzerBase.cs +++ b/src/ILLink.RoslynAnalyzer/RequiresAnalyzerBase.cs @@ -172,10 +172,10 @@ public override void Initialize (AnalysisContext context) for (int i = 0; i < typeParams.Length; i++) { var typeParam = typeParams[i]; var typeArg = typeArgs[i]; - if (!typeParam.HasConstructorConstraint) + if (!typeParam.HasConstructorConstraint || + typeArg is not INamedTypeSymbol { InstanceConstructors: { } typeArgCtors }) continue; - var typeArgCtors = ((INamedTypeSymbol) typeArg).InstanceConstructors; foreach (var instanceCtor in typeArgCtors) { if (instanceCtor.Arity > 0) continue; diff --git a/src/linker/Linker.Steps/OutputStep.cs b/src/linker/Linker.Steps/OutputStep.cs index da4d0e58b4df..eeea5b861e33 100644 --- a/src/linker/Linker.Steps/OutputStep.cs +++ b/src/linker/Linker.Steps/OutputStep.cs @@ -174,8 +174,11 @@ private void OutputPInvokes () using (var fs = File.Open (Path.Combine (Context.OutputDirectory, Context.PInvokesListFile), FileMode.Create)) { var values = Context.PInvokes.Distinct ().OrderBy (l => l); + // Ignore warning, since we're just enabling analyzer for dogfooding +#pragma warning disable IL2026 var jsonSerializer = new DataContractJsonSerializer (typeof (List)); jsonSerializer.WriteObject (fs, values); +#pragma warning restore IL2026 } } diff --git a/src/linker/Linker/Driver.cs b/src/linker/Linker/Driver.cs index 6ba1445a17c8..50d62c383984 100644 --- a/src/linker/Linker/Driver.cs +++ b/src/linker/Linker/Driver.cs @@ -832,7 +832,11 @@ string Unquote (string arg) if (File.Exists (assemblyPath)) { // The CLR will return the already-loaded assembly if the same path is requested multiple times // (or even if a different path specifies the "same" assembly, based on the MVID). + + // Ignore warning, since we're just enabling analyzer for dogfooding +#pragma warning disable IL2026 return AssemblyLoadContext.Default.LoadFromAssemblyPath (assemblyPath); +#pragma warning restore IL2026 } Context.LogError (null, DiagnosticId.AssemblyInCustomStepOptionCouldNotBeFound, arg); } else @@ -992,7 +996,10 @@ protected bool AddCustomStep (Pipeline pipeline, string arg) Type? ResolveStepType (string type, Assembly assembly) { + // Ignore warning, since we're just enabling analyzer for dogfooding +#pragma warning disable IL2026 Type? step = assembly != null ? assembly.GetType (type) : Type.GetType (type, false); +#pragma warning restore IL2026 if (step == null) { Context.LogError (null, DiagnosticId.CustomStepTypeCouldNotBeFound, type); @@ -1004,7 +1011,10 @@ protected bool AddCustomStep (Pipeline pipeline, string arg) TStep? ResolveStep (string type, Assembly assembly) where TStep : class { + // Ignore warning, since we're just enabling analyzer for dogfooding +#pragma warning disable IL2026 Type? step = assembly != null ? assembly.GetType (type) : Type.GetType (type, false); +#pragma warning restore IL2026 if (step == null) { Context.LogError (null, DiagnosticId.CustomStepTypeCouldNotBeFound, type); diff --git a/test/Mono.Linker.Tests.Cases/RequiresCapability/BasicRequires.cs b/test/Mono.Linker.Tests.Cases/RequiresCapability/BasicRequires.cs index 5e890cb0fc68..972e28c4962e 100644 --- a/test/Mono.Linker.Tests.Cases/RequiresCapability/BasicRequires.cs +++ b/test/Mono.Linker.Tests.Cases/RequiresCapability/BasicRequires.cs @@ -5,6 +5,8 @@ using System.Collections.Generic; using System.Diagnostics; using System.Diagnostics.CodeAnalysis; +using System.IO; +using System.Linq; using System.Linq.Expressions; using System.Reflection; using System.Runtime.InteropServices; @@ -190,9 +192,13 @@ public static void GenericTypeWithStaticMethodViaLdftn () class TestType { } + static T MakeNew () where T : new() => new T (); + static T MakeNew2 () where T : new() => MakeNew (); + public static void Test () { GenericTypeWithStaticMethodViaLdftn (); + MakeNew2 (); } } } From 5282fc63880cde7c119aa224b2f02a9186ddf414 Mon Sep 17 00:00:00 2001 From: Andy Gocke Date: Mon, 24 Jan 2022 21:12:31 -0800 Subject: [PATCH 3/4] Revert unnecessary changes --- build.sh | 20 -------------------- eng/common/build.ps1 | 8 ++++---- 2 files changed, 4 insertions(+), 24 deletions(-) diff --git a/build.sh b/build.sh index e2aa1080eeef..8477d5af8817 100755 --- a/build.sh +++ b/build.sh @@ -1,27 +1,7 @@ #!/usr/bin/env bash -# Stop script if unbound variable found (use ${var:-} if intentional) -set -u - -# Stop script if subcommand fails -set -e - source="${BASH_SOURCE[0]}" -bootstrap=false - -args="" -while [[ $# > 0 ]]; do - opt="$(echo "${1/#--/-}" | tr "[:upper:]" "[:lower:]")" - case "$opt" in - -integrationtest) - bootstrap=true - ;; - esac - args="$args $1" - shift -done - # resolve $SOURCE until the file is no longer a symlink while [[ -h $source ]]; do scriptroot="$( cd -P "$( dirname "$source" )" && pwd )" diff --git a/eng/common/build.ps1 b/eng/common/build.ps1 index cefc8cc891e7..8943da242f6e 100644 --- a/eng/common/build.ps1 +++ b/eng/common/build.ps1 @@ -33,7 +33,7 @@ Param( # Unset 'Platform' environment variable to avoid unwanted collision in InstallDotNetCore.targets file # some computer has this env var defined (e.g. Some HP) if($env:Platform) { - $env:Platform="" + $env:Platform="" } function Print-Usage() { Write-Host "Common settings:" @@ -98,10 +98,10 @@ function Build { # Re-assign properties to a new variable because PowerShell doesn't let us append properties directly for unclear reasons. # Explicitly set the type as string[] because otherwise PowerShell would make this char[] if $properties is empty. [string[]] $msbuildArgs = $properties - - # Resolve relative project paths into full paths + + # Resolve relative project paths into full paths $projects = ($projects.Split(';').ForEach({Resolve-Path $_}) -join ';') - + $msbuildArgs += "/p:Projects=$projects" $properties = $msbuildArgs } From d1a9e876f2ae5a446a61c27d10c9c70a097b4563 Mon Sep 17 00:00:00 2001 From: Andy Gocke Date: Mon, 24 Jan 2022 21:14:03 -0800 Subject: [PATCH 4/4] Remove debugging printf --- eng/build.ps1 | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/eng/build.ps1 b/eng/build.ps1 index c83579597dae..5305b8fdc1e7 100644 --- a/eng/build.ps1 +++ b/eng/build.ps1 @@ -6,8 +6,6 @@ Param( . (Join-Path $PSScriptRoot "common/tools.ps1") -echo $ArtifactsDir - $args = $remaining.Clone() if ($integrationTest) { @@ -16,4 +14,4 @@ if ($integrationTest) { $args += "/p:BootstrapBuildPath=$ArtifactsDir/bootstrap" } -powershell -ExecutionPolicy ByPass -NoProfile (Join-Path $PSScriptRoot "common/build.ps1") @args \ No newline at end of file +powershell -ExecutionPolicy ByPass -NoProfile (Join-Path $PSScriptRoot "common/build.ps1") @args