Skip to content

Commit

Permalink
Add desktop test
Browse files Browse the repository at this point in the history
  • Loading branch information
xpenatan committed Jul 10, 2023
1 parent 4b666d9 commit e7a97d4
Show file tree
Hide file tree
Showing 19 changed files with 316 additions and 58 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,4 @@ out/
*.ini
**/webapp/**
**/example-core/src/**
**/example-desktop/src/**
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package com.github.xpenatan.jparser.example;

public class NormalClassTest {
public class NormalClass {
/*[-C++;-NATIVE]
#include "NormalClassTest.h"
#include "NormalClass.h"
*/


Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.github.xpenatan.jparser.example;

public class ParentClass {
/*[-C++;-NATIVE]
#include "ParentClass.h"
*/


}
6 changes: 6 additions & 0 deletions example/example-build/jni/cpp/src/NormalClass.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#include "NormalClass.h"

int NormalClass::addIntValue(int a, int b)
{
return a + b;
}
10 changes: 10 additions & 0 deletions example/example-build/jni/cpp/src/NormalClass.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#ifndef NORMALCLASS_H
#define NORMALCLASS_H

class NormalClass
{
public:
int addIntValue(int a, int b);
};

#endif //NORMALCLASS_H
11 changes: 0 additions & 11 deletions example/example-build/jni/cpp/src/NormalClassTest.cpp

This file was deleted.

11 changes: 0 additions & 11 deletions example/example-build/jni/cpp/src/NormalClassTest.h

This file was deleted.

11 changes: 11 additions & 0 deletions example/example-build/jni/cpp/src/ParentClass.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#include "ParentClass.h"

float ParentClass::addFloatValue(float a, float b)
{
return a + b;
}

bool ParentClass::invertBoolean(bool value)
{
return !value;
}
11 changes: 11 additions & 0 deletions example/example-build/jni/cpp/src/ParentClass.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#ifndef PARENTCLASS_H
#define PARENTCLASS_H

class ParentClass
{
public:
float addFloatValue(float a, float b);
bool invertBoolean(bool value);
};

#endif //PARENTCLASS_H
6 changes: 0 additions & 6 deletions example/example-build/jni/cpp/src/ParentClassTest.cpp

This file was deleted.

10 changes: 0 additions & 10 deletions example/example-build/jni/cpp/src/ParentClassTest.h

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -35,18 +35,16 @@ private static void generateCPP() throws Exception {
String idlPath = "src\\main\\resources\\idl\\Test.idl";
String baseJavaDir = new File(".").getAbsolutePath() + "./example-base/src/main/java";
String genDir = "../example-core/src/main/java";
String classPath = CppCodeParser.getClassPath("example-base", "gdx-1", "gdx-jnigen-loader", "jParser-loader");
String libsDir = new File("../example-desktop/src/main/resources/").getCanonicalPath();
String jniBuildPath = new File("./build/c++/").getCanonicalPath();

String cppPath = new File("./jni/").getCanonicalPath();
String jniBuildPath = cppPath + "/build/c++/";
FileCopyHelper.copyDir(cppPath + "/cpp/src/", jniBuildPath + "/src");
FileCopyHelper.copyDir( "./jni/cpp/src/", jniBuildPath + "/src");

IDLReader idlReader = IDLReader.readIDL(idlPath);

CppGenerator cppGenerator = new NativeCPPGeneratorV2(jniBuildPath);
CppCodeParser idlParser = new CppCodeParser(cppGenerator, idlReader, basePackage);
idlParser.generateClass = true;
JParser.generate(idlParser, baseJavaDir, genDir);
// CPPBuildHelper.build(libName, jniBuildPath);
CPPBuildHelper.build(libName, jniBuildPath, libsDir);
}
}
13 changes: 7 additions & 6 deletions example/example-build/src/main/resources/idl/Test.idl
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
interface ParentClassTest {
float getFloatValue();
interface ParentClass {
float addFloatValue(float a, float b);
boolean invertBoolean(boolean value);
};

interface NormalClassTest {
long getIntValue();
boolean getBoolValue(float floatValue);
interface NormalClass {
void NormalClassTest();
long addIntValue(long a, long b);
};
NormalClassTest implements ParentClassTest;
NormalClass implements ParentClass;
36 changes: 36 additions & 0 deletions example/example-desktop/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
apply plugin: "java"

group = project.groupId
version = "1.0.0"

def module_name = "example-core"

dependencies {
testImplementation project(":jParser:loader")
testImplementation project(":example:example-core")
testImplementation "junit:junit:$project.jUnitVersion"
}

clean.doFirst {
def srcPath = projectDir.toString() + "/src/main/"
project.delete(files(srcPath))
}

publishing {
publications {
maven(MavenPublication) {
artifactId = module_name
from components.java
}
}
}

tasks.register("prepareTest", GradleBuild) {
tasks = [
":example:example-build:clean",
":example:example-core:clean",
":example:example-build:generateNativeProject",
"compileTestJava"
]
}
test.dependsOn "prepareTest"
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package com.github.xpenatan.jparser.example;

import com.github.xpenatan.jparser.loader.JParserLibraryLoader;
import org.junit.BeforeClass;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

public class NormalClassTest {

@BeforeClass
public static void setUp() throws Exception {
String libDir = "src/main/resources/example-test-natives.jar";
new JParserLibraryLoader(libDir).load("example-test");
}

@Test
public void test_add_int() {
NormalClass normalClass = new NormalClass();
int ret = normalClass.addIntValue(10, 10);
assertEquals(20, ret);
}

@Test
public void test_add_float() {
NormalClass normalClass = new NormalClass();
float ret = normalClass.addFloatValue(10.3f, 10.3f);
assertEquals(20.6, ret, 1.0f);
}

@Test
public void test_invert_boolean_should_be_false() {
NormalClass normalClass = new NormalClass();
boolean ret = normalClass.invertBoolean(true);
assertFalse(ret);
}

@Test
public void test_invert_boolean_should_be_true() {
NormalClass normalClass = new NormalClass();
boolean ret = normalClass.invertBoolean(false);
assertTrue(ret);
}

}

Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,10 @@ public class CustomAntScriptGenerator {
* @param targets list of {@link BuildTarget} instances */
public void generate (BuildConfig config, BuildTarget... targets) {
// create all the directories for outputing object files, shared libs and natives jar as well as build scripts.
if (!config.libsDir.exists()) {
if (!config.libsDir.mkdirs())
throw new RuntimeException("Couldn't create directory for shared library files in '" + config.libsDir + "'");
}
// if (!config.libsDir.exists()) {
// if (!config.libsDir.mkdirs())
// throw new RuntimeException("Couldn't create directory for shared library files in '" + config.libsDir + "'");
// }
if (!config.jniDir.exists()) {
if (!config.jniDir.mkdirs())
throw new RuntimeException("Couldn't create native code directory '" + config.jniDir + "'");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ public static void build(String libName, String buildPath) {
build(libName, buildPath, null, null, null, false);
}

public static void build(String libName, String buildPath, String libsDir) {
build(libName, buildPath, libsDir, null, null, null, null, false);
}

public static void build(String libName, String buildPath, String [] cppFlags) {
build(libName, buildPath, cppFlags, null, null, false);
}
Expand All @@ -41,7 +45,15 @@ public static void build(String libName, String buildPath, String libsDir, Strin
catch(IOException e) {
throw new RuntimeException(e);
}
String[] headerDir = {"src", sharedSrcPath};
String[] headerDir = null;
if(sharedSrcPath != null) {
String[] headerDirr = {"src", sharedSrcPath};
headerDir = headerDirr;
}
else {
String[] headerDirr = {"src"};
headerDir = headerDirr;
}
String[] includes = {"**/*.cpp"};

BuildConfig buildConfig = new BuildConfig(libName, "target", libsDir, buildPath);
Expand Down
Loading

0 comments on commit e7a97d4

Please sign in to comment.