iPhone
Posted: Sat Jun 30, 2012 11:38 pm
One of our users has taken an interest in writing iPhone apps in Cobra. This is currently possible with C# and .NET through the Xamarin MonoTouch product. Given that Cobra runs on .NET and even generates C# for its back-end, this should be possible.
But this is new territory for Cobra so it's going to start out rough and we'll make it smoother as we go along. This will be our thread for discussion and issues on Cobra+MonoTouch+iPhone.
MonoTouch can work with .NET assemblies, aka DLLs, but only if they are "compiled against the MonoTouch profile". This means referencing the mscorlib.dll and System.dll of MonoTouch rather than the usual ones for .NET/Mono 4.0. Those are the most fundamental libraries in the CLR universe.
My first step in this process was finding how to do this with C# at the command line. I found the info here:
http://stackoverflow.com/questions/9395 ... le-from-th
I then had to enhance Cobra to be able to target a different CLR profile, so I added a new -clr-profile command line option in changeset:2728 which you'll see me use below.
First some basic steps to get the requisite software in place:
-- I'm on Mac OS X 10.6.8
-- Installed latest Mono available today:
MonoFramework-MRE-2.10.9_11.macos10.xamarin.x86.dmg
My "mono --version" went from:
Mono JIT compiler version 2.10.9 (tarball Tue Mar 20 15:31:37 EDT 2012)
to:
Mono JIT compiler version 2.10.9 (tarball Mon May 7 20:25:51 EDT 2012)
Same version, but newer build date. Makes you wonder.
-- Installed latest MonoDevelop which was 3.0.3.2. I did a fresh install rather than using their updater which I had problems with.
-- Installed the current MonoTouch eval obtained from the Xamarin web site (monotouch-eval.dmg, 7.8MB, version 5.2.12).
-- Then a baby test for MonoTouch:
---- MonoDevelop > File > New Solution > C# > MonoTouch > "MonoTouch Library Project"
------ Build succeeded.
------ Added a C# file with some dummy code. Build succeeded.
-- Check that the MonoTouch C# compiler exists from the command line:
-- Tested "regular" compilation of a C# library:
---- Create Foo.cs:
---- At the command line:
-- Test a MonoTouch compilation of that Foo.cs:
-- Now try with Cobra:
-- Create Foo.cobra:
-- If you don't have the latest Cobra, see HowToInstallFromSource. Be sure to install for .NET 4 when prompted.
-- At the command line:
-- And now the moment you have been waiting for:
I would like to put all those -native-compiler-args together, but Mono's own Environment.getCommandLineArgs splits them up even if I quote them.
In any case, once you set up your project, you'll almost certainly put this in a bash script or something similar and not worry about it. In fact, that's what I did.
You'll notice the new -clr-profile option in there.
You can take out the -v:1 if you want less output.
That's as far as I'm taking things for now. I'm on the hook for the core compiler work and other things. Those of you interested in the iPhone can read that stackoverflow article which contains additional info. And/or you can experiment with referencing the Cobra-produced DLL from a MonoTouch project created in MonoDevelop.
Ultimately, I hope others chime in with tips and solutions and carry this further to create a real example. When things are refined and stabilize, an informative wiki page would be nice. I'll tweak and enhance the compiler as needed to eliminate roadblocks.
But this is new territory for Cobra so it's going to start out rough and we'll make it smoother as we go along. This will be our thread for discussion and issues on Cobra+MonoTouch+iPhone.
MonoTouch can work with .NET assemblies, aka DLLs, but only if they are "compiled against the MonoTouch profile". This means referencing the mscorlib.dll and System.dll of MonoTouch rather than the usual ones for .NET/Mono 4.0. Those are the most fundamental libraries in the CLR universe.
My first step in this process was finding how to do this with C# at the command line. I found the info here:
http://stackoverflow.com/questions/9395 ... le-from-th
I then had to enhance Cobra to be able to target a different CLR profile, so I added a new -clr-profile command line option in changeset:2728 which you'll see me use below.
First some basic steps to get the requisite software in place:
-- I'm on Mac OS X 10.6.8
-- Installed latest Mono available today:
MonoFramework-MRE-2.10.9_11.macos10.xamarin.x86.dmg
My "mono --version" went from:
Mono JIT compiler version 2.10.9 (tarball Tue Mar 20 15:31:37 EDT 2012)
to:
Mono JIT compiler version 2.10.9 (tarball Mon May 7 20:25:51 EDT 2012)
Same version, but newer build date. Makes you wonder.
-- Installed latest MonoDevelop which was 3.0.3.2. I did a fresh install rather than using their updater which I had problems with.
-- Installed the current MonoTouch eval obtained from the Xamarin web site (monotouch-eval.dmg, 7.8MB, version 5.2.12).
-- Then a baby test for MonoTouch:
---- MonoDevelop > File > New Solution > C# > MonoTouch > "MonoTouch Library Project"
------ Build succeeded.
------ Added a C# file with some dummy code. Build succeeded.
-- Check that the MonoTouch C# compiler exists from the command line:
- Code: Select all
$ /Developer/MonoTouch/usr/bin/smcs
error CS2008: No files to compile were specified
-- Tested "regular" compilation of a C# library:
---- Create Foo.cs:
- Code: Select all
using System;
namespace Foo {
public class Bar {
public Bar() : base() { }
}
}
---- At the command line:
- Code: Select all
$ dmcs /t:library Foo.cs
$ ls -l Foo.dl
$ rm Foo.dll
-- Test a MonoTouch compilation of that Foo.cs:
- Code: Select all
$ /Developer/MonoTouch/usr/bin/smcs /noconfig "/out:Foo.dll" "/r:/Developer/MonoTouch/usr/lib/mono/2.1/System.dll" "/r:/Developer/MonoTouch/usr/lib/mono/2.1/System.Xml.dll" "/r:/Developer/MonoTouch/usr/lib/mono/2.1/System.Core.dll" "/r:/Developer/MonoTouch/usr/lib/mono/2.1/monotouch.dll" /nologo /warn:4 /debug:+ /debug:full /optimize- /codepage:utf8 "/define:DEBUG" /t:library "Foo.cs"
$ ls -l Foo.dl
$ rm Foo.dll
-- Now try with Cobra:
-- Create Foo.cobra:
namespace Foo
class Bar
cue init
base.init
-- If you don't have the latest Cobra, see HowToInstallFromSource. Be sure to install for .NET 4 when prompted.
-- At the command line:
- Code: Select all
$ cobra -about
The Cobra Programming Language svn:2727 (post 0.8) / 2012-06-30
on Mono 2.10.9 CLR v4.0.30319 on Mac OS X 10.6.8
at /usr/local/cobra/Cobra-svn-2727/bin/cobra.exe
...
-- And now the moment you have been waiting for:
- Code: Select all
cobra -c -t:lib -out:Foo.dll \
-v:1 \
-embed-run-time:yes \
-clr-profile:/Developer/MonoTouch/usr/lib/mono/2.1 \
-native-compiler:/Developer/MonoTouch/usr/bin/smcs \
-native-compiler-args:/noconfig \
-native-compiler-args:/codepage:utf8 \
-native-compiler-args:/warn:4 \
-native-compiler-args:/debug:+ \
-native-compiler-args:/debug:full \
-native-compiler-args:/optimize- \
-native-compiler-args:/codepage:utf8 \
-native-compiler-args:/define:DEBUG \
-ref:/Developer/MonoTouch/usr/lib/mono/2.1/System.dll \
-ref:/Developer/MonoTouch/usr/lib/mono/2.1/System.Xml.dll \
-ref:/Developer/MonoTouch/usr/lib/mono/2.1/System.Core.dll \
-ref:/Developer/MonoTouch/usr/lib/mono/2.1/monotouch.dll \
Foo.cobra
I would like to put all those -native-compiler-args together, but Mono's own Environment.getCommandLineArgs splits them up even if I quote them.
In any case, once you set up your project, you'll almost certainly put this in a bash script or something similar and not worry about it. In fact, that's what I did.
You'll notice the new -clr-profile option in there.
You can take out the -v:1 if you want less output.
That's as far as I'm taking things for now. I'm on the hook for the core compiler work and other things. Those of you interested in the iPhone can read that stackoverflow article which contains additional info. And/or you can experiment with referencing the Cobra-produced DLL from a MonoTouch project created in MonoDevelop.
Ultimately, I hope others chime in with tips and solutions and carry this further to create a real example. When things are refined and stabilize, an informative wiki page would be nice. I'll tweak and enhance the compiler as needed to eliminate roadblocks.