| def athenaSimJniOutputDir = file("$buildDir/generated/cpp") |
| def athenaSimJniOutputFile = file("$athenaSimJniOutputDir/simjni.cpp") |
| |
| task generateAthenaSimFiles() { |
| Set dirs = []; |
| |
| def createdTask = it |
| outputs.file athenaSimJniOutputFile |
| |
| model { |
| components { |
| it.all { component -> |
| if (component in getJniSpecClass()) { |
| component.binaries.all { binary -> |
| if (binary.targetPlatform.architecture.name == 'athena') { |
| binary.tasks.withType(CppCompile) { |
| it.dependsOn createdTask |
| } |
| component.jniHeaderLocations.each { |
| dependsOn it.key |
| createdTask.inputs.dir it.value |
| dirs << it.value |
| } |
| } |
| } |
| } |
| } |
| } |
| } |
| |
| doLast { |
| def symbolList = [] |
| dirs.each { |
| def tree = fileTree(dir: it) |
| tree.each { file -> |
| if (!file.name.contains('edu_wpi_first_hal_sim_mockdata_')) { |
| return |
| } |
| boolean reading = false |
| String currentLine = '' |
| file.eachLine { line -> |
| if (line.trim()) { |
| if (line.contains(';') && reading) { |
| currentLine += line.trim() |
| reading = false |
| symbolList << currentLine |
| currentLine = '' |
| } |
| if (line.startsWith("JNIEXPORT ") && line.contains('JNICALL')) { |
| if (line.contains(';')) { |
| symbolList << line |
| currentLine = '' |
| reading = false |
| } else { |
| reading = true |
| currentLine += line.trim() |
| } |
| |
| } |
| } |
| } |
| } |
| } |
| athenaSimJniOutputDir.mkdirs() |
| athenaSimJniOutputFile.withWriter { out -> |
| out.println '#include <jni.h>' |
| out.println ''' |
| static JavaVM* jvm = nullptr; |
| |
| namespace sim { |
| jint SimOnLoad(JavaVM* vm, void* reserved) { |
| jvm = vm; |
| |
| JNIEnv *env; |
| if (vm->GetEnv(reinterpret_cast<void **>(&env), JNI_VERSION_1_6) != JNI_OK) |
| return JNI_ERR; |
| |
| return JNI_VERSION_1_6; |
| } |
| |
| void SimOnUnload(JavaVM * vm, void* reserved) { |
| JNIEnv *env; |
| if (vm->GetEnv(reinterpret_cast<void **>(&env), JNI_VERSION_1_6) != JNI_OK) |
| return; |
| jvm = nullptr; |
| } |
| } |
| |
| static void ThrowSimException(JNIEnv* env) { |
| |
| } |
| ''' |
| out.println 'extern "C" {' |
| symbolList.each { |
| def symbol = it.replace('JNIEnv *', 'JNIEnv * env') |
| if (symbol.contains('JNIEXPORT void')) { |
| symbol = symbol.replace(';', ''' { |
| ThrowSimException(env); |
| }''') |
| } else { |
| symbol = symbol.replace(';', ''' { |
| ThrowSimException(env); |
| return 0; |
| }''') |
| } |
| out.println symbol |
| } |
| out.println '}' |
| } |
| } |
| } |