Skip to content

Add support for additional platform targets (uap and xamarin.ios) as … - #401

Closed
gentledepp wants to merge 3 commits into
MessagePack-CSharp:v1.8from
gentledepp:feature/no_dynamic_code
Closed

gentledepp wants to merge 3 commits into
MessagePack-CSharp:v1.8from
gentledepp:feature/no_dynamic_code

Conversation

@gentledepp

Copy link
Copy Markdown

…these forbid any IL code to be created or even referenced.

Otherwise e.g. when referencing MessagePack in an UWP solution and you run the publish wizard to publish it in Microsoft Store, you will get a bunch of compiler errors. It boils down to the one error regarding MessagePack:

Type 'System.Reflection.Emit.ModuleBuilder' from assembly 'System.Reflection.Emit' was not included in compilation, but was referenced in type 'MessagePack.Internal.DynamicAssembly'. There may have been a missing assembly, or a dependency on a more recent Windows SDK release. .UWP

So this PR adds two additional platform targetx: ios and uwp
In these, all IL creating code is not included

…these forbid any IL code to be created or even referenced.
@felixse

felixse commented Mar 18, 2019

Copy link
Copy Markdown

Any ETA when this can get merged?

@AliGuemues

Copy link
Copy Markdown

Hello, is there any plan when this commit is available in the MessagePack-CSharp project? Or maybe a equilvalent solution to use MessagePack in a UWP project?

@offbyoneBB

Copy link
Copy Markdown

Would also like to see this merged.

@gentledepp

Copy link
Copy Markdown
Author

This also fixed #440

@gentledepp

Copy link
Copy Markdown
Author

I am willing to merge any changes from the master branch, but only after some contributor can guarantee me, that these changes will then be merged right after that! Cheers!

@AArnott
AArnott changed the base branch from master to v1.8 May 13, 2019 13:00
@AArnott

AArnott commented May 13, 2019

Copy link
Copy Markdown
Collaborator

For now, I've just edited your PR to target v1.8, which skirts the merge conflict issue (for now). As for whether to accept it, I haven't reviewed your PR yet so I can't say.

@gentledepp

Copy link
Copy Markdown
Author

Ok great - thank you for the reply!
As there was no response for weeks, I thought this project was more or less abandoned.
I would very much prefer to not maintain a separate MessagePack package.

We are actually using the iOS and UWP builds in production. The OutOfMemoryException issue on UWP took me more than one week to track down (#440 ) so I really hope to save some developers out there the pain :-|

@AArnott

AArnott commented May 13, 2019

Copy link
Copy Markdown
Collaborator

OK, what you're trying to solve looks very worth fixing. Your approach of removing all the IL generating code looks pretty good. I would request these changes:

  1. change the NO_IL_CODE precompiler symbol to something I find less confusing (since of course the whole assembly is IL code 😄). Perhaps REF_EMIT (as a positive instead of a negative symbol would make it less confusing as well)
  2. Add target frameworks to the test project as well. Maybe some tests will have to be excluded from these targets.
  3. Adding a UWP project to the solution that references messagepack, compiles with optimization and .NET Native. The goal being to add the project to the solution that would fail without the rest of your proposed changes, and builds successfully with them.

With these changes, I think I'd be willing to merge into v1.8, with one extra step: that you have a published branch that merges our master branch with these changes as well, since it's obviously a non-trivial merge. Honestly I wonder if the easiest way to do it is just start from scratch on master. But master might not release to nuget.org for months yet, so v1.8 may be worthwhile targeting in the meantime so you can see the benefit of your work.

@gentledepp

Copy link
Copy Markdown
Author

Hello @AArnott
As it seems you have connections to the .net native guys, may you also tell them about #440 where an UWP build with .net native toolchain enabled fails with an OutOfMemoryException just because the MessagePackSerializer uses the attribute
[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
That'd be very, very nice ;-)

@AliGuemues

Copy link
Copy Markdown

Any Updates on this?

@gentledepp

Copy link
Copy Markdown
Author

Sorry - not yet. We spent almost a week fixing this and the only way to find out if it works was to run the iOS build and the UWP build in "publish" mode which is quite a tedious task.

We may fix this in the future, but as of now this has no priority to us.

@AArnott

AArnott commented Oct 22, 2019

Copy link
Copy Markdown
Collaborator

Closing till the PR author is ready to push an update.

@AArnott AArnott closed this Oct 22, 2019
@gentledepp

gentledepp commented Nov 12, 2019 •

Copy link
Copy Markdown
Author

@bruzkovsky

@bruzkovsky

Copy link
Copy Markdown

Hi @AArnott,

I am trying to get this implemented on the current master as you suggested, but the code changed a lot, which makes it even harder to find the conflicting references now. As you already thought, I needed to start from scratch, because most of the files that were changed by this PR are deleted on master.

I added a UWP test project that is set to 'Compile with native toolchain' and 'optimize code' always. I traced back the changes from commit eed8d6c, first starting with adding a uap10.0 target to the MessagePack project. Then, I removed the references to System.Reflection.Emit and System.Reflection.Emit.Lightweight and proceeded to make it build again by using #if REF_EMIT to exclude the code referencing those assemblies. The MessagePack project builds and produces a uap10.0 artifact just as it should, but the UWP build fails with the following message:
[...]nutc_driver.exe @"C:\Repos\MessagePack-CSharp_gd\obj\TestUWPNETNative\x86\Debug\ilc\intermediate\MDIL\TestUWPNETNative.rsp"' returned exit code -2147483645.

After some more digging, I found a new reference to System.Runtime.CompilerServices.Unsafe, which seems a bit... unsafe ;) So I tried to remove the reference too, but in the process discovered that it is deeply rooted in the serialization, so I'm wondering if it is essential for the serialization to work and must not be removed. Would you agree?

Other than that, I am running out of ideas for this problem...

Now, it gets really funny:

After adding the UWP project to the 1.8 branch, so that I can check that the changes really fix the problem, it compiles with .NETNative toolchain from the start, even without the changes of this PR:
[...]nutc_driver.exe @"C:\Repos\MessagePack-CSharp_gd\obj\TestUWPNETNative\x86\Debug\ilc\intermediate\MDIL\TestUWPNETNative.rsp"' completed. (TaskId:365)

This was strange at first. but then I realized, I already implemented the fix in the UWP.csproj, after reading about the nutc_driver.exe failing here and here. So I added <Use64BitCompiler>true</Use64BitCompiler> and <ShortcutGenericAnalysis>true</ShortcutGenericAnalysis> to my original (production) UWP.csproj and referenced MessagePack 1.8.74, and now it builds with .NETNative enabled 🎉

So, TLDR;: The MessagePack 1.8.74 package can be used in our project by adding the properties

<Use64BitCompiler>true</Use64BitCompiler>
<ShortcutGenericAnalysis>true</ShortcutGenericAnalysis>

to the first PropertyGroup in the csproj. This PR is not needed. But the current master will not compile, even with that fix, which is definitely blocking for us to move to MessagePack 2.x when it comes out.

I will create another PR for adding the UWP project.

Cheers

@AArnott

AArnott commented Nov 14, 2019

Copy link
Copy Markdown
Collaborator

Thanks, @bruzkovsky. I'm a bit confused though. You sent a PR to our master branch (where 2.0 is) saying that you see a build break, yet the PR build succeeded.
If you have changes that add a test project to confirm UWP works, which works properly in the v1.8 branch, by all means send a PR and we can add it so that 1.8 doesn't regress in our UWP support. Does this UWP test simply compile messagepack.dll or does it actually run the tests in the UWP environment? I wouldn't claim that we support messagepack in UWP unless the tests actually run there.

As for why it fails in v2.0, I have no idea what that exit code means. The .Unsafe dependency is something that several corefx assemblies bring in, and yes we may use it directly as well. I don't think it does anything super-magical that we couldn't do ourselves -- the implementation of the DLL looks simple enough. But I'd be surprised if that's actually what's causing the .NET Native toolchain problem.

@bruzkovsky

Copy link
Copy Markdown

@AArnott, the test project simply compiles MessagePack and fails when running nutc_driver. Now I am confused, too! Building it on my machine fails. But going through the logs of the CI build, I cannot see any mention of 'TestUAP_NETNative', so I'm not sure if it was even built in CI.

@AArnott

AArnott commented Nov 14, 2019

Copy link
Copy Markdown
Collaborator

Ah, I know why your project wasn't built -- it only builds when the solution platform built is set to a non "Any CPU" selection (since UWP is always architecture-specific). Our CI hasn't previously needed to build anything but "Any CPU".

@bruzkovsky
bruzkovsky deleted the feature/no_dynamic_code branch November 15, 2019 14:43
@bruzkovsky

Copy link
Copy Markdown

After testing MessagePack 1.8.74 with the aforementioned fix, our production app will build and start, but it cannot use MessagePack code it seems. When we deserialize data, it just hangs indefinitely. Seeing this, I am concluding that these changes are still required for supporting UWP with .NETNative toolchain. More precisely, commits eed8d6c and 28a3b2b are required, but 1aa39c1 only fixed the OutOfMemoryException that is also fixed by <Use64BitCompiler>true</Use64BitCompiler>.

@AArnott, for that reason, I renamed !NO_IL_CODE to REF_EMIT as you suggested and hope that you will merge it and publish a service release for 1.8.

The UWP project is in my other PR and you already did the test project - please correct me if I'm wrong - so, I guess that makes the list complete.

@bruzkovsky

Copy link
Copy Markdown

So, deleting the branch and pushing it again maybe wasn't the best idea... Can the PR be updated manually somehow?

@AArnott

AArnott commented Nov 15, 2019

Copy link
Copy Markdown
Collaborator

GitHub won't let me re-open the PR. Is the reopen button disabled for you too?

@bruzkovsky

Copy link
Copy Markdown

I can't even see it.

@AArnott

AArnott commented Nov 15, 2019

Copy link
Copy Markdown
Collaborator

Well, I guess you'll have to create a new PR then.

@bruzkovsky

Copy link
Copy Markdown

New PR is here: #641, but the build fails, I think because msbuild 16 doesn't like MsBuild.SDK.Extras < 2, at least I get the same error when building with VS2019 (VS2017 builds successfully)

@AArnott AArnott mentioned this pull request May 4, 2020
3 tasks done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants