Friday, June 15, 2012

Eclipse Plugins with Native Code for Multiple Platforms

Eclipse plugins can contain native code. Since the native code is specific to a certain architecture, this should probably be put into a fragment that is then added to the 'main' plugin depending on the architecture.

Such fragments can support multiple platforms, for example Linux on both 32 and 64 bit CPUs, by adding something like this to the MANIFEST.MF:

Eclipse-PlatformFilter: (& (osgi.os=linux) (| (osgi.arch=x86)(osgi.arch=x86_64)))
Bundle-NativeCode
 lib/linux/x86/mylib.so;    osname=linux; processor=x86,
 lib/linux/x86_64/mylib.so; osname=linux; processor=x86_64

On Linux, the correct library for either CPU type will be now available.Excellent!

However, when combining such plugins into a feature-based product, a problem arises. Assume you have a fragment for windows, one for Linux, one for OS X. When you add the fragments to the product's feature, you must again specify their architecture:

   plugin
         id="org.mylib.linux"
         os="linux"
         arch="x86"
         ...
If you don't specify the os and arch in the feature, the build will fail: Eclipse will try to include your plugin on both Windows and Linux, but then complain that the "linux" plugin doesn't work on Windows and vice versa.

If you do specify the os and arch, however, you can only list one arch, even though our linux fragment would work on both x86 and x86_64.

The solution is to list the same fragment again in the fragment.xml with the other os and arch settings:

   plugin
         id="org.mylib.linux"
         os="linux"
         arch="x86_64"
         ...

Note that you may have to do this directly in the feature.xml. The feature editor's Plug-Ins tab doesn't support adding an already included plugin multiple times, but once you did add it in the feature.xml, you can view and edit it in there.