« Create a google site with the Google sites Java API | Main | Mate Framework - an example »

Alchemy: compiling C/C++ code into SWF or SWC

Alchemy is a research project that is intended to allow user to compile C and C++ code that is targeted to run on the open source ActionScript Virtual Machine (AVM2).

With Alchemy, Web application developers can now reuse existing open source C and C++ client or server-side code on the Flash Platform. Alchemy aims to bring the power of high performance C and C++ libraries to Web applications with minimal degradation on AVM2. The C/C++ code is compiled to ActionScript 3.0 as a SWF or SWC that runs on Adobe Flash Player 10 or Adobe AIR 1.5. Alchemy is primarily intended to be used with C/C++ libraries that have few OS dependencies. The generated content runs within the security constraints of the AVM2, and cannot bypass Flash Player security protections.

In this post I'm going to explain how to setup up Alchemy on Windows and then I'm showing how to walk through the steps to get a simple C code file compiled into a SWC, and then how to use that SWC within a simple Flex example.

Requirements
On windows we will use Cygwin.


  • Alchemy Toolkit Package for your Windows OS

  • Cygwin with the following packages installed


    • Perl

    • zip

    • gcc / g++


  • Java

  • Flex 3.2 SDK

  • Flex SDK setup to target compilation for Flash Player 10


Download and install Cygwin with the packages suggested before.Download and install Java. Make sure to restart the Cygwin terminal after installing Java. Download and install the Flex SDK, and add the $FLEX_HOME/bin directory to your Cygwin environment's path (within ~/.bashrc).Download the Alchemy Package, unzip it and then copy the alchemy folder to your system. We will refer to this path as $ALCHEMY_HOME. Open a Cygwin terminal and change to the $ALCHEMY_HOME/ directory. Run the $ALCHEMY_HOME/config script using the following command:

./config

Open alchemy_setup for editing and add the path to the ADL executable (included in the Flex SDK):

export ADL=/cygdrive/c/PATH_TO_FLEX_SDK_HOME/bin/adl.exe

Make sure to uncomment this line, and that it contains the path to ADL.exe on your system (Substitute PATH_TO_FLEX_SDK_HOME with the path of the Flex Sdk installed on your system). Open your bash setup script to edit. This can usually be found in the ~/.bashrc file. Edit the the .bashrc script so that alchemy-setup is run when the script is run:

source /cygdrive/c/alchemy/alchemy-setup

This should be added before your PATH is modified. Add $ALCHEMY_HOME/achacks to your path.

PATH=$ALCHEMY_HOME/achacks:/cygdrive/c/PATH_TO_FLEX_SDK_HOME/bin:$PATH

Your .bashrc file should look similar to:

source /cygdrive/c/alchemy/alchemy-setup
PATH=$ALCHEMY_HOME/achacks:/cygdrive/c/PATH_TO_FLEX_SDK_HOME/bin:$PATH
export PATH

Obviously the file may contain other commands specific to your system. Save the file, and restart your cygwin terminal. Change to the $ALCHEMY_HOME/bin directory, and run the following command:

ln -s llvm-stub llvm-stub.exe

Note that this step wont be necessary in future builds. Now your environment is ready to compile C/C++ code into a SWF or a SWC.

I've devoleped a simple C function to sum two doubles. Here is the code:

//File doublesum.c
#include <stdlib.h>
#include <stdio.h>
 
//Header file for AS3 interop APIs
//this is linked in by the compiler (when using flaccon)
#include "AS3.h"
 
//Method exposed to ActionScript
//Takes two doubles and return the sum
static AS3_Val sum(void* self, AS3_Val args)
{
	//initialize double parameters to 0.0
	double operand1 = 0.0;
	double operand2 = 0.0;
	
	//parse the arguments. Expect 2.
	//pass in operand1 to hold the first argument, which
	//should be a double. Does the same with the second argument
	AS3_ArrayValue( args, "DoubleType, DoubleType", &operand1, &operand2 );
	
	//calculate the sum and return it
	double sum = operand1+operand2;
	return AS3_Number(sum);
}
 
//entry point for code
int main()
{
	//define the methods exposed to ActionScript
	//typed as an ActionScript Function instance
	AS3_Val sumMethod = AS3_Function( NULL, sum );
 
	// construct an object that holds references to the functions
	AS3_Val result = AS3_Object( "sum: AS3ValType", sumMethod );
 
	// Release
	AS3_Release( sumMethod );
 
	// notify that we initialized -- THIS DOES NOT RETURN!
	AS3_LibInit( result );
 
	// should never get here!
	return 0;
}

To compile the file doublesum.c , into the cygwin shell change to the directory where you stored the file (or your C file) and type the following command :

alc-on; which gcc

It should print out the path that points to the gcc contained with the $ALCHEMY_HOME/achacks/ directory. Then enter the following command:

gcc doublesum.c -O3 -Wall -swc -o doublesum.swc

You should see output similar to:

WARNING: While resolving call to function 'main' arguments were dropped!

4232.achacks.swf, 261018 bytes written
frame rate: 60
frame count: 1
69 : 4
72 : 260949
76 : 32
1 : 0
0 : 0
frame rate: 24
frame count: 1
69 : 4
77 : 506
64 : 31
63 : 16
65 : 4
9 : 3
41 : 26
82 : 471
1 : 0
0 : 0

adding: catalog.xml (deflated 75%)
adding: library.swf (deflated 70%)

Now in the same directory you will find the file doublesum.swc. To test it I have developed an mxml file named DoubleSumTest.mxml. This is the code:

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="vertical">
	<mx:Script>
		<![CDATA[
			import cmodule.doublesum.CLibInit;
			
			private function c_doubleSumFunction(op1:Number, op2:Number):Number
			{
				var loader:CLibInit = new CLibInit();
				var lib:Object = loader.init();
				return lib.sum(op1,op2);
				
			}
			
			private function buttonHandler():void
			{
				var operand1:Number = Number(operand1Field.text);
				var operand2:Number = Number(operand2Field.text);
				var sum:Number = this.c_doubleSumFunction(operand1, operand2);
				resultField.text = String(sum);
			}
		]]>
	</mx:Script>
 
	<mx:TextInput id="operand1Field" />
	<mx:Label text="+" />
	<mx:TextInput id="operand2Field"/>
	<mx:Label text="=" />
	<mx:TextInput id="resultField" />
	<mx:Button id="sumButton" label="sum" click="buttonHandler()"/>
	
</mx:Application> 
Download this file,named DoubleSumTest.mxml (or write your own test file) and save it in the same directory of the doublesum.swc. Into the cygwin shell, change to the directory where you stored the file and use the following command to compile the ActionScript/Flex code using MXMLC:

mxmlc.exe -library-path+=./doublesum.swc -target-player=10.0.0 DoubleSumTest.mxml

This will generate a Flash Player 10 SWF. Executing this file in a browser will show you 3 TextInput followed by a Button labeled sum.
Introducing 2 double numbers in the first and second TextInput and clicking on the button will show their sum in the third TextInput calculated by the C function in doublesum.c .

For more information about Alchemy visit: http://labs.adobe.com/technologies/alchemy/ .

TrackBack

TrackBack URL for this entry:
http://blog.comtaste.com/mt-tb.cgi/115

Comments (2)

Thanks for posting this XML source code, very helpful!

Good workt! Your C function development looks superb........ Thanks for the code reference......

Post a comment

(If you haven't left a comment here before, you may need to be approved by the site owner before your comment will appear. Until then, it won't appear on the entry. Thanks for waiting.)

About

This page contains a single entry from the blog posted on April 30, 2010 10:29 AM.

The previous post in this blog was Create a google site with the Google sites Java API.

The next post in this blog is Mate Framework - an example.

Many more can be found on the main index page or by looking through the archives.

Powered by
Movable Type 3.33