Skip to content

Adding simple cffi example. #5

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions work-in-progress/cffi/examples/simple/cpp/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
ndll
obj
63 changes: 63 additions & 0 deletions work-in-progress/cffi/examples/simple/cpp/Build.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<xml>

<!-- define input cpp and output lib name -->

<files id="files">
<compilerflag value="-Iinclude"/>
<file name="Foreign.cpp" />
</files>

<set name="LIB_NAME" value="libforeign" />

<!-- the below is mostly boilerplate -->

<include name="${HXCPP}/build-tool/BuildCommon.xml"/>

<section if="windows">
<include name="${HXCPP}/toolchain/msvc-toolchain.xml"/>
</section>

<section if="macos">
<set name="HXCPP_CLANG" value="1" />
<include name="${HXCPP}/toolchain/mac-toolchain.xml"/>
</section>

<section if="linux">
<include name="${HXCPP}/toolchain/linux-toolchain.xml"/>
</section>

<compiler id="default">
<objdir value="obj/32" if="HXCPP_M32"/>
<objdir value="obj/64" if="HXCPP_M64"/>
</compiler>

<!-- osx and windows -->
<target id="NDLL" output="${LIB_NAME}" tool="linker" toolid="dll" unless="linux">
<ext value=".ndll" />
<files id="files" />

<outdir name="ndll/Mac64" if="macos HXCPP_M64" />

<outdir name="ndll/Mac" if="macos HXCPP_M32" />

<outdir name="ndll/Windows" if="windows" />
</target>

<!-- linux -->

<target id="NDLL" output="${LIB_NAME}" tool="linker" toolid="dll" if="linux">
<ext value=".ndll" />
<files id="files" />

<outdir name="ndll/Linux64" if="HXCPP_M64" />

<outdir name="ndll/Linux" if="HXCPP_M32" />

<flag value="-Wl,-rpath,'/lib:/usr/lib'" />
</target>

<target id="default">
<target id="NDLL" />
</target>

</xml>
17 changes: 17 additions & 0 deletions work-in-progress/cffi/examples/simple/cpp/Foreign.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#define IMPLEMENT_API
#include <hx/CFFI.h>

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);

}
9 changes: 9 additions & 0 deletions work-in-progress/cffi/examples/simple/cpp/README.md
Original file line number Diff line number Diff line change
@@ -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.
1 change: 1 addition & 0 deletions work-in-progress/cffi/examples/simple/hx/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
out
21 changes: 21 additions & 0 deletions work-in-progress/cffi/examples/simple/hx/Main.hx
Original file line number Diff line number Diff line change
@@ -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(); }
}
5 changes: 5 additions & 0 deletions work-in-progress/cffi/examples/simple/hx/build.hxml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
-main Main.hx
-cpp out

# Runtime needs the ndll
-cmd cp ../cpp/ndll/Linux64/libforeign.ndll out