Skip to content

Commit

Permalink
[mtouch] Show warnings when we can't find referenced assemblies.
Browse files Browse the repository at this point in the history
This would have helped track down dotnet#4235.
  • Loading branch information
rolfbjarne committed Jul 26, 2018
1 parent 9e31d07 commit 02f4395
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 10 deletions.
2 changes: 2 additions & 0 deletions docs/website/mmp-errors.md
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,8 @@ Alternatively, enable the managed [linker](https://docs.microsoft.com/xamarin/ma

As a last-straw solution, use an older version of Xamarin.Mac that does not require these new SDKs to be present during the build process.

<!-- 0136 and 0137 used by mtouch -->

# MM1xxx: file copy / symlinks (project related)

### <a name="MM1034">MM1034: Could not create symlink '{file}' -> '{target}': error {number}
Expand Down
16 changes: 16 additions & 0 deletions docs/website/mtouch-errors.md
Original file line number Diff line number Diff line change
Expand Up @@ -683,6 +683,22 @@ Alternatively, enable the managed [linker](https://docs.microsoft.com/en-us/xama

As a last-straw solution, use an older version of Xamarin.iOS that does not require these new SDKs to be present during the build process.

### <a name="MT0136"/>MT0136: Cannot find {assembly} referenced from {assembly}.

This warning occurs when an assembly passed to mtouch contains a reference to
another assembly that can't be found.

mtouch may in certain cases still find references at a later point, which
means that if the build otherwise succeeds, this warning can be ignored.

### <a name="MT0137"/>MT0137: Cannot find {assembly}, referenced by an attribute in {assembly}.

This warning occurs when an attribute contains a reference to another assembly
that can't be found.

mtouch may in certain cases still find references at a later point, which
means that if the build otherwise succeeds, this warning can be ignored.

# MT1xxx: Project related error messages

### MT10xx: Installer / mtouch
Expand Down
56 changes: 56 additions & 0 deletions tests/mtouch/MTouch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1716,6 +1716,62 @@ public void MT0132 ()
}
}

[Test]
public void MT0136 ()
{
using (var mtouch = new MTouchTool ()) {
var tmpdir = mtouch.CreateTemporaryDirectory ();
mtouch.CreateTemporaryCacheDirectory ();

var codeDll = @"public class A {}";
var codeExe = @"public class B : A {}";

var dllPath = CompileTestAppLibrary (tmpdir, codeDll, profile: Profile.iOS, appName: "A");

mtouch.CreateTemporaryApp (extraCode: codeExe, extraArg: $"-r:{StringUtils.Quote (dllPath)}");
mtouch.Debug = false;
mtouch.Linker = MTouchLinker.DontLink;
File.Delete (dllPath);
mtouch.AssertExecuteFailure (MTouchAction.BuildSim, "build");
mtouch.AssertWarningPattern (136, "Cannot find 'A, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null' referenced from '.*/testApp.exe'.");
mtouch.AssertError (2002, "Failed to resolve assembly: 'A, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null'");
mtouch.AssertErrorCount (1);
mtouch.AssertWarningCount (1);
}
}

[Test]
public void MT0137 ()
{
using (var mtouch = new MTouchTool ()) {
var tmpdir = mtouch.CreateTemporaryDirectory ();
mtouch.CreateTemporaryCacheDirectory ();

var codeDll = @"public class A {}";
var codeExe = @"
public class TAttribute : System.Attribute
{
public TAttribute (System.Type type) {}
}
[T (typeof (A))]
public class B
{
}
";

var dllPath = CompileTestAppLibrary (tmpdir, codeDll, profile: Profile.iOS, appName: "A");

mtouch.CreateTemporaryApp (extraCode: codeExe, extraArg: $"-r:{StringUtils.Quote (dllPath)}");
mtouch.Debug = false;
mtouch.Linker = MTouchLinker.DontLink;
File.Delete (dllPath);
mtouch.AssertExecute (MTouchAction.BuildSim, "build");
mtouch.AssertWarningPattern (137, "Cannot find 'A, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null', referenced by an attribute in 'testApp.exe'.");
mtouch.AssertWarningCount (1);
}
}

[Test]
[TestCase ("all")]
[TestCase ("-all")]
Expand Down
28 changes: 18 additions & 10 deletions tools/mtouch/Target.cs
Original file line number Diff line number Diff line change
Expand Up @@ -393,6 +393,10 @@ void ComputeListOfAssemblies (HashSet<string> assemblies, AssemblyDefinition ass
}

var reference_assembly = ManifestResolver.Resolve (reference);
if (reference_assembly == null) {
ErrorHelper.Warning (136, "Cannot find '{0}' referenced from '{1}'.", reference.FullName, main.FileName);
continue;
}
ComputeListOfAssemblies (assemblies, reference_assembly, exceptions);
}

Expand All @@ -402,46 +406,50 @@ void ComputeListOfAssemblies (HashSet<string> assemblies, AssemblyDefinition ass
// Custom Attribute metadata can include references to other assemblies, e.g. [X (typeof (Y)],
// but it is not reflected in AssemblyReferences :-( ref: #37611
// so we must scan every custom attribute to look for System.Type
GetCustomAttributeReferences (assembly, assemblies, exceptions);
GetCustomAttributeReferences (main, assemblies, exceptions);
GetCustomAttributeReferences (main, assembly, assemblies, exceptions);
GetCustomAttributeReferences (main, main, assemblies, exceptions);
if (main.HasTypes) {
foreach (var ca in main.GetCustomAttributes ())
GetCustomAttributeReferences (ca, assemblies, exceptions);
GetCustomAttributeReferences (main, ca, assemblies, exceptions);
}
}

void GetCustomAttributeReferences (ICustomAttributeProvider cap, HashSet<string> assemblies, List<Exception> exceptions)
void GetCustomAttributeReferences (ModuleDefinition main, ICustomAttributeProvider cap, HashSet<string> assemblies, List<Exception> exceptions)
{
if (!cap.HasCustomAttributes)
return;
foreach (var ca in cap.CustomAttributes)
GetCustomAttributeReferences (ca, assemblies, exceptions);
GetCustomAttributeReferences (main, ca, assemblies, exceptions);
}

void GetCustomAttributeReferences (CustomAttribute ca, HashSet<string> assemblies, List<Exception> exceptions)
void GetCustomAttributeReferences (ModuleDefinition main, CustomAttribute ca, HashSet<string> assemblies, List<Exception> exceptions)
{
if (ca.HasConstructorArguments) {
foreach (var arg in ca.ConstructorArguments)
GetCustomAttributeArgumentReference (arg, assemblies, exceptions);
GetCustomAttributeArgumentReference (main, arg, assemblies, exceptions);
}
if (ca.HasFields) {
foreach (var arg in ca.Fields)
GetCustomAttributeArgumentReference (arg.Argument, assemblies, exceptions);
GetCustomAttributeArgumentReference (main, arg.Argument, assemblies, exceptions);
}
if (ca.HasProperties) {
foreach (var arg in ca.Properties)
GetCustomAttributeArgumentReference (arg.Argument, assemblies, exceptions);
GetCustomAttributeArgumentReference (main, arg.Argument, assemblies, exceptions);
}
}

void GetCustomAttributeArgumentReference (CustomAttributeArgument arg, HashSet<string> assemblies, List<Exception> exceptions)
void GetCustomAttributeArgumentReference (ModuleDefinition main, CustomAttributeArgument arg, HashSet<string> assemblies, List<Exception> exceptions)
{
if (!arg.Type.Is ("System", "Type"))
return;
var ar = (arg.Value as TypeReference)?.Scope as AssemblyNameReference;
if (ar == null)
return;
var reference_assembly = ManifestResolver.Resolve (ar);
if (reference_assembly == null) {
ErrorHelper.Warning (137, "Cannot find '{0}', referenced by an attribute in '{1}'.", ar.FullName, main.Name);
return;
}
ComputeListOfAssemblies (assemblies, reference_assembly, exceptions);
}

Expand Down

0 comments on commit 02f4395

Please sign in to comment.