Writing portable code

From GNUstepWiki
Jump to navigation Jump to search

GNUstep opens up some fairly interesting opportunities to target a number of platforms with the same source code, for example porting Mac OS X Cocoa applications to Linux, the BSDs, other Unices and (ultimately) Windows without significant re-engineering effort or degraded end-user functionality.

Some portability gotchas are listed below (please update when you run into a new one!):

Porting Applications

You can generally convert NIB bundles from NeXTSTEP and OPENSTEP to GNUstep using the nib2gmodel tool.

  • nib2gmodel Something.nib Something.gmodel
  • Open the gmodel file in Gorm on GNUstep and save it as gorm

Porting from NeXTSTEP (NX*) to GNUstep

For this you should have access to an old OPENSTEP installation, or find a friend who has one, otherwise you're simply better off just rewriting the application from scratch.

There are some scripts which can be used to convert an application from NeXTSTEP to OPENSTEP located in:

  /NextDeveloper/OpenStepConversion/ConversionScripts

They use a scripting language called "tops". I haven't been able, as yet, to locate a version of tops for Linux or any other operating system other than Mac OS X or OPENSTEP/Mach or NeXTSTEP.

There are a few conversion applications which basically just use the tops scripts to do the conversion. They are actually two versions of the same program, but the second one doesn't run on NeXT, so here's both. These are:

[1] [2]

You should be able to point it at the project directory you wish to convert and start the conversion. The GUI is very straightforward.

You can also run the tops scripts on a Mac simply by saying:

  tops -verbose -scriptfile {scriptfilename} {filestoconvert}

All of the files are converted in place. You must run all of the tops scripts in the ConversionScripts directory against the application code. It is considerably simply to use the existing applications. Unfortunately these scripts do not exist on Mac OS X, otherwise porting the Conversion application might be useful.

When you start the scripts, if you're running them on black hardware you should go get a cup of coffe and watch a movie with your wife or go code something else for a while. The conversion will take a good long while on an older machine. I haven't timed it on the Mac, though. Once this is done, there is still a long way to go. Read on.

OPENSTEP4.2/Mach had a class called NSCStringText. This class was primarily for apps which were converted from NeXTSTEP to use as a stop-gap measure in OpenStep. This is the main reason it isn't in openstep. You will need to make sure that any objects modified by the scripts to use NSCStringText will use NSText and it's related classes as appropriate.

OPENSTEP4.2/Mac also had some other extensions on the OpenStep standard such as NXStreams. You will also need to convert any and all instances of NXStream to use NSData instead in order to make your app work with GNUstep.

If your app is fairly small, you shouldn't be in for a great shock, but if your app is heavily reliant on the old Text object from NeXTSTEP, you have a lot of work ahead. Other gaps which the scripts might leave are problems with NSUserDefaults which it can't fix on it's own.

Porting from OPENSTEP (NS*) to GNUstep

Porting from GNUstep to Cocoa

tbd.


Porting from Cocoa to GNUstep

Porting from Cocoa to GNUstep has different aspects.

First there is the compatibility of the basic Foundation Kit and Application Kit. We support a lot of classes, but not all of them and the same is true for specific methods, Specifically newly introduced classes and methods may still be missing as GNUstep started out aiming at OpenStep compatibility.

Second you have all the additional frameworks and libraries programmers on Mac OS X take for granted, for some of them free replacements exists, for most they are still missing.

Mac OS X developers should try and avoid CoreFoundation as this will complicate your dependency situation on non-Mac hosts, even if that part of CoreFoundation has actually been ported.

GNUstep Base and the FoundationKit offer many parts of the CoreFoundation functionality in a natural Objective-C manner.

The porting itself

short overview:

  • write a makefile
  • convert your NIB files to gmodel
  • provide an Info.plist.

detailed instructions:

  1. you need to write a new GNUstep make file for the project (very easy, still there is currently no automatic way to do so)
  2. you need to convert the NIB files to GNUstep format. At some point in time GORM will be able to read NIB files, but currently you need to use the gmodel intermediate format for this and for this the above mentioned tools are used.

Don'ts

Don't use anything we do not provide:

  • don't use CoreFoundation
  • don't use KeyChain
  • don't use Carbon
  • don't use Quartz
  • don't use QuickTime

gcc mainline currently doesn't support ObjC++ although it is already in the works somewhat (expected for gcc 4.1). that means:

  • don't use WebKit (currently)

gcc mainline also does not support altivec, this is an apple specific extentsion, that means:

  • don't use altivec features or the vecLib.framework (a collection of functions for vector manipulation).

Stumbling Blocks

  • Apple's Cocoa.h

it is there to enable pre-compiled headers and speed up compile time thusly. Anyway, it contains just two lines:

#import <Foundation/Foundation.h>
#import <AppKit/AppKit.h>

So you can easily work around this by providing your own or replace your include of <Cocoa/Cocoa.h> with just the above, which will also work on OS-X.

  • When you need to distinguish between Cocoa and GNUstep, you may use the following
#ifdef GNUSTEP
    /* GNUstep code goes here ... */
#else
    /* OS-X Cocoa native code goes here ... */
#endif


Porting the .nib files

First get nib2gmodel, follow the included instructions to build and install it. Once that's done you're ready to begin:

  • Run the nib2gmodel program on your nib like so:
  nib2gmodel mygui.nib mygui.gmodel

This should create the .gmodel as a plist. I recommend that you convert the gmodel to a "normal" plist instead of an XML plist. You can do that by doing this on OS X:

  pl < mygui.gmodel -output mygui.ser
  pl > mygui.gmodel -input mygui.ser
  rm mygui.ser
  • Now you can either use the gmodel as it is, or you can convert it to a .gorm file.

Gmodel to .gorm conversion

NOTE: Conversion from gmodel to .gorm is experimental and will probably remain so for a while. It currently works in the majority of cases, but there are still improvements which need to be made.

Conversion to a .gorm file is relatively straightforward:

  • Start Gorm.app.
  • Choose Gorm->Document->Open and find the gmodel you wish to load.
  • Gorm will prompt you to either define the superclass for classes it doesn't know about or to parse the headers.

After you've defined all of the unknown classes, Gorm should complete the conversion. Once this is done you should save the result to make sure you don't loose your work.

FIXME: add following:

Notes

Do not rely on tools available in development environment

All the world is not Linux or FreeBSD or Windows. System constants may be different on different platforms. Paths to command-line tools, the parameters they accept and locations of temporary directories may be different to what you are used to on your platform.

  • GNUstep Base and the FoundationKit offer some of this functionality "for free".
  • Try not to hard code paths and so on into your code, use macros (not recommended), or even better an abstraction mechanism.
  • Avoid hard-coding system constants, or platform-specific ones where Posix or commonly-accepted ones exist (and we're not talking about glibc here :-)