Skip to content

Commit

Permalink
Add some basic support for iOS and Android.
Browse files Browse the repository at this point in the history
  • Loading branch information
Apprentice-Alchemist committed Apr 23, 2022
1 parent 9452799 commit 2e3e639
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 51 deletions.
104 changes: 71 additions & 33 deletions Build.hx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,23 @@ import haxe.io.Path;

using StringTools;

enum abstract Target(String) to String {
var Windows = "windows";
var Linux = "linux";
var macOS = "macos";
var Android = "android";
var iOS = "ios";

public static function fromString(s:String):Target {
switch (cast s : Target) {
case Windows, Linux, macOS, Android, iOS:
return cast s;
default:
throw "Invalid target";
}
}
}

function applyPatch(dir:String, patch:String) {
Sys.command("git", ["-C", dir, "apply", '../patches/$patch']);
}
Expand All @@ -30,12 +47,17 @@ function main() {
var debug = Sys.getEnv("DEBUG") != null;
var krafix = Sys.getEnv("INCLUDE_KRAFIX") != null;
var g_api = Sys.getEnv("KINCHL_GRAPHICS");
var target = null;
{
final args = Sys.args();
while (true) {
switch args.shift() {
case null:
break;
case "-g":
g_api = args.shift();
case "-t":
target = Target.fromString(args.shift());
case var arg:
if (arg == "-g") {
g_api = args.shift();
Expand Down Expand Up @@ -67,46 +89,62 @@ function main() {
n_args.push("-g");
n_args.push(g_api);
}
if (target != null) {
n_args.push("-t");
n_args.push(target);
}
if (debug)
n_args.push("--debug");
if (Sys.command(sys_name == "windows" ? "Kinc/make.bat" : "Kinc/make", n_args) != 0)
Sys.exit(1);

Sys.setCwd("build");
FileSystem.createDirectory("bin");
if (sys_name == "mac") {
final configuration = debug ? "Debug" : "Release";
if (Sys.command("xcodebuild", [
"-configuration",
configuration,
"-project",
"KincHL.xcodeproj",
"ARCHS=x86_64",
"EXECUTABLE_NAME=kinc.hdll",
]) != 0)
Sys.exit(1);
File.copy('build/$configuration/kinc.hdll', "bin/kinc.hdll");
} else if (sys_name == "windows") {
final configuration = debug ? "Debug" : "Release";
if (Sys.command("MSBuild", [
"KincHL.vcxproj",
"/m" + (num_cpus == null ? "" : ':$num_cpus'),
'/p:Configuration=$configuration,Platform=x64,OutDir=bin/,TargetExt=.hdll,TargetName=kinc'
]) != 0)
Sys.exit(1);
} else if (sys_name == "linux") {
final configuration = debug ? "Debug" : "Release";
File.saveContent('$configuration/makefile', File.getContent('$configuration/makefile').replace('KincHL.so', 'kinc.hdll'));
Sys.setCwd(configuration);
final m_args = [];
if (num_cpus != null) {
Sys.println('Using $num_cpus cores.');
m_args.push("-j" + num_cpus);
}
if (Sys.command("make", m_args) != 0)
Sys.exit(1);
Sys.setCwd("..");
File.copy('$configuration/kinc.hdll', "bin/kinc.hdll");
switch target {
case Windows:
final configuration = debug ? "Debug" : "Release";
if (Sys.command("MSBuild", [
"KincHL.vcxproj",
"/m" + (num_cpus == null ? "" : ':$num_cpus'),
'/p:Configuration=$configuration,Platform=x64,OutDir=bin/,TargetExt=.hdll,TargetName=kinc'
]) != 0)
Sys.exit(1);
case Linux:
final configuration = debug ? "Debug" : "Release";
File.saveContent('$configuration/makefile', File.getContent('$configuration/makefile').replace('KincHL.so', 'kinc.hdll'));
Sys.setCwd(configuration);
final m_args = [];
if (num_cpus != null) {
Sys.println('Using $num_cpus cores.');
m_args.push("-j" + num_cpus);
}
if (Sys.command("make", m_args) != 0)
Sys.exit(1);
Sys.setCwd("..");
File.copy('$configuration/kinc.hdll', "bin/kinc.hdll");
case macOS, iOS:
final configuration = debug ? "Debug" : "Release";
final args = [
"-configuration",
configuration,
"-project",
"KincHL.xcodeproj",
"EXECUTABLE_NAME=kinc.hdll",
];
if (target != iOS)
args.push("ARCHS=x86_64");
if (Sys.command("xcodebuild", args) != 0)
Sys.exit(1);
File.copy('build/$configuration/kinc.hdll', "bin/kinc.hdll");
case Android:
Sys.setCwd("KincHL");
File.saveContent("app/CMakeLists.txt", File.getContent("app/CMakeLists.txt") + '\nset_target_properties(kinc PROPERTIES SUFFIX ".hdll")'); final configuration = debug ? "Debug" : "Release";
switch sys_name {
case "windows":
Sys.command("gradlew.bat", ['assemble$configuration']);
case _:
Sys.command("bash", ["gradlew", 'assemble$configuration']);
}
}

try {
Expand Down
13 changes: 11 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@

Hashlink bindings for [Kinc](https://github.com/Kode/Kinc)

### Building
## Building

On windows the kincfile expects an environment variable called HASHLINK to point to your hashlink installation, which should have the following structure :
### Windows

The kfile expects an environment variable called HASHLINK to point to your hashlink installation, which should have the following structure :
```
%HASHLINK%
- hl.exe
Expand All @@ -28,3 +30,10 @@ $ haxe --run Build
$ copy build/bin/kinc.hdll /path/to/hashlink/ # Windows
$ sudo cp build/bin/kinc.hdll /usr/local/lib/ # Linux/Mac
```

### iOS
The kfile expects the `HASHLINK_BIN` environment variable to point to a directory containing `libhl.dylib`.

### Android

TODO
36 changes: 20 additions & 16 deletions kfile.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
const process = require("process");
const path = require("path");

let project = new Project("KincHL");

await project.addProject(process.env["KINC_PATH"] ?? "Kinc");

project.setDebugDir("");
project.addDefine('KORE_DEBUGDIR=""');
project.addFile("src/kinchl.c");
project.addFile("tests/**"); // shaders
Expand Down Expand Up @@ -32,6 +32,10 @@ switch (platform) {
project.addLib("libhl.dylib");
}
break;
case Platform.iOS:
if (process.env["HASHLINK_BIN"]) project.addIncludeDir(process.env["HASHLINK_BIN"]);
project.addLib(path.join(process.env["HASHLINK_BIN"], "libhl.dylib"));
break;
case Platform.Windows: {
let hl_inc = null;
console.log(process.env);
Expand All @@ -49,22 +53,22 @@ switch (platform) {
break;
}
case Platform.Android:
case Platform.iOS: {
let hl_inc = null;
console.log(process.env);
if (process.env["HASHLINK_SRC"]) {
hl_inc = process.env["HASHLINK_SRC"]
} else if (process.env["HASHLINK"] ?? process.env["HASHLINK_PATH"]) {
hl_inc = (process.env["HASHLINK"] ?? process.env["HASHLINK_PATH"]) + "/include";
}
if (hl_inc == null) throw "could not find hashlink include path";
{
let hl_inc = null;
console.log(process.env);
if (process.env["HASHLINK_SRC"]) {
hl_inc = process.env["HASHLINK_SRC"]
} else if (process.env["HASHLINK"] ?? process.env["HASHLINK_PATH"]) {
hl_inc = (process.env["HASHLINK"] ?? process.env["HASHLINK_PATH"]) + "/include";
}
if (hl_inc == null) throw "could not find hashlink include path";

let hl_bin = process.env["HASHLINK_BIN"] ?? process.env["HASHLINK"] ?? process.env["HASHLINK_PATH"] ?? null;
if (hl_bin == null) throw "could not find hashlink binaries";
project.addLib("hl");
project.addIncludeDir(hl_inc);
break;
}
let hl_bin = process.env["HASHLINK_BIN"] ?? process.env["HASHLINK"] ?? process.env["HASHLINK_PATH"] ?? null;
if (hl_bin == null) throw "could not find hashlink binaries";
project.addLib("hl");
project.addIncludeDir(hl_inc);
break;
}
}

project.flatten();
Expand Down

0 comments on commit 2e3e639

Please sign in to comment.