From 548f8ab1d525dd2b19bb6d91c3143d4f1d6c94cb Mon Sep 17 00:00:00 2001 From: Jeff Date: Wed, 9 Mar 2016 07:44:44 -0700 Subject: [PATCH 1/2] Adding simple cffi example. Known issues: - hardcoded Linux64 in hx/build.hxml (copying the ndll to the output directory, not sure how to do this cross-platform in an hxml?) - directory naming conventions, .gitignore conventions --- .../cffi/examples/simple/cpp/.gitignore | 2 + .../cffi/examples/simple/cpp/Build.xml | 63 +++++++++++++++++++ .../cffi/examples/simple/cpp/Foreign.cpp | 17 +++++ .../cffi/examples/simple/cpp/README.md | 9 +++ .../cffi/examples/simple/hx/.gitignore | 1 + .../cffi/examples/simple/hx/Main.hx | 21 +++++++ .../cffi/examples/simple/hx/build.hxml | 5 ++ 7 files changed, 118 insertions(+) create mode 100644 work-in-progress/cffi/examples/simple/cpp/.gitignore create mode 100644 work-in-progress/cffi/examples/simple/cpp/Build.xml create mode 100644 work-in-progress/cffi/examples/simple/cpp/Foreign.cpp create mode 100644 work-in-progress/cffi/examples/simple/cpp/README.md create mode 100644 work-in-progress/cffi/examples/simple/hx/.gitignore create mode 100644 work-in-progress/cffi/examples/simple/hx/Main.hx create mode 100644 work-in-progress/cffi/examples/simple/hx/build.hxml diff --git a/work-in-progress/cffi/examples/simple/cpp/.gitignore b/work-in-progress/cffi/examples/simple/cpp/.gitignore new file mode 100644 index 0000000..1f45a09 --- /dev/null +++ b/work-in-progress/cffi/examples/simple/cpp/.gitignore @@ -0,0 +1,2 @@ +ndll +obj diff --git a/work-in-progress/cffi/examples/simple/cpp/Build.xml b/work-in-progress/cffi/examples/simple/cpp/Build.xml new file mode 100644 index 0000000..d3db52e --- /dev/null +++ b/work-in-progress/cffi/examples/simple/cpp/Build.xml @@ -0,0 +1,63 @@ + + + + + + + + + + + + + + + +
+ +
+ +
+ + +
+ +
+ +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
diff --git a/work-in-progress/cffi/examples/simple/cpp/Foreign.cpp b/work-in-progress/cffi/examples/simple/cpp/Foreign.cpp new file mode 100644 index 0000000..2b68dfe --- /dev/null +++ b/work-in-progress/cffi/examples/simple/cpp/Foreign.cpp @@ -0,0 +1,17 @@ +#define IMPLEMENT_API +#include + +extern "C" +{ + + value CPP_ForeignFunction(value haxeVal) + { + int intVal = val_int(haxeVal); + printf("CPP: intVal = %d\n",intVal); + intVal++; + value returnVal = alloc_int(intVal); + return returnVal; + } + DEFINE_PRIM(CPP_ForeignFunction, 1); + +} diff --git a/work-in-progress/cffi/examples/simple/cpp/README.md b/work-in-progress/cffi/examples/simple/cpp/README.md new file mode 100644 index 0000000..fa82abe --- /dev/null +++ b/work-in-progress/cffi/examples/simple/cpp/README.md @@ -0,0 +1,9 @@ +Use hxcpp (invoked with haxelib) and a `Build.xml` file to define a +cross-platform build. Run the build with: + +``` +haxelib run hxcpp Build.xml +``` + +This is a cross-platform way to invoke the C++ compiler with all the options +necessary to build the `libforeign.ndll` library. diff --git a/work-in-progress/cffi/examples/simple/hx/.gitignore b/work-in-progress/cffi/examples/simple/hx/.gitignore new file mode 100644 index 0000000..1fcb152 --- /dev/null +++ b/work-in-progress/cffi/examples/simple/hx/.gitignore @@ -0,0 +1 @@ +out diff --git a/work-in-progress/cffi/examples/simple/hx/Main.hx b/work-in-progress/cffi/examples/simple/hx/Main.hx new file mode 100644 index 0000000..bd14601 --- /dev/null +++ b/work-in-progress/cffi/examples/simple/hx/Main.hx @@ -0,0 +1,21 @@ +import cpp.Lib; + +class Main +{ + public var ForeignFunction:Dynamic; + public function new() + { + trace("Hello from Main, about to load libforeign library"); + try { + ForeignFunction = cpp.Lib.load("libforeign", "CPP_ForeignFunction",1); + } catch (e:Dynamic) { + trace("Failed to load libforeign.ndll!"); + return; + } + + var a:Int = ForeignFunction(2); + trace("Main called ForeignFunction(2), returned "+a); + } + + public static function main() { new Main(); } +} diff --git a/work-in-progress/cffi/examples/simple/hx/build.hxml b/work-in-progress/cffi/examples/simple/hx/build.hxml new file mode 100644 index 0000000..19426e3 --- /dev/null +++ b/work-in-progress/cffi/examples/simple/hx/build.hxml @@ -0,0 +1,5 @@ +-main Main.hx +-cpp out + +# Runtime needs the ndll +-cmd cp ../cpp/ndll/Linux64/libforeign.ndll out From 21c3d54464b6cc8dc82958c7c7d025795a307fd4 Mon Sep 17 00:00:00 2001 From: Jeff Date: Thu, 10 Mar 2016 07:40:14 -0700 Subject: [PATCH 2/2] Removed $ORIGIN (steamwrap relic) --- work-in-progress/cffi/examples/simple/cpp/Build.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/work-in-progress/cffi/examples/simple/cpp/Build.xml b/work-in-progress/cffi/examples/simple/cpp/Build.xml index d3db52e..ac31e60 100644 --- a/work-in-progress/cffi/examples/simple/cpp/Build.xml +++ b/work-in-progress/cffi/examples/simple/cpp/Build.xml @@ -53,7 +53,7 @@ - +