Linker versus Customization

MonoTouch 5.3.3 (alpha) has been released with lots of new goodies (and I can predict a few blog posts about them). Let’s start slowly since it’s already the end of the week…

There’s a lot of ways to customize how the linker works on your applications. They covers the most common scenarios but it can sometime feels like it’s missing a final else { } condition. Let’s review the existing conditions:

First the linker’s scope. You can use it on everything (--linkall), on the SDK assemblies shipped with MonoTouch (default) or not at all (--nolink). You can further suppress the linker from executing on one or several assemblies using --linkskip=MyAssembly.

You can also control how the linker code works on your own assemblies. Using the [Preserve] attribute you can ensure that specific parts of your code won’t be linked away (e.g. for reflection/serialization usage). With few (or even no) adjustments most applications can use --linkall and reduce their final size.

Controlling the code linked inside SDK assemblies is less common and a bit harder. The most common way is to write code that use the type, method or fields that you do not want removed. That works well unless you need to preserve some private/internal code. In such case you’re a bit out of luck as using reflection won’t be seen by the linker. Using --linkskip is often your best solution – but you lose the linker benefits over the entire assembly.

Sad ? No more. Starting with 5.3.3 you’ll be able to add extraneous declarations using the new --xml option. Writing XML is never a first choice (at least for me) but it will allow you complete control on your own code (e.g. you do not want or cannot use [Preserve]) and on the SDK assemblies shipped with MonoTouch.

Not surprised ? You should not be ;-) as this is not something really new. The original monolinker has always supported this. It is even used, internally, by mtouch, for the XML definitions used for SDK assemblies (you can find a few similar examples here). However this option was not exposed to MonoTouch developers before 5.3.3.

About these ads

About spouliot

Xamarin Hacker, Mono Contributor, Open Source Enthusiast with strong interests in Code Analysis, Cryptography and Security. Presently working on MonoTouch, previous projects were Moonlight and libgdiplus and a lot of the cryptographic work done on Mono.
This entry was posted in monotouch, mono, xamarin, linker. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s