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.name == nativeUtils.wpi.platforms.roborio) { 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_simulation_')) { 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 ' out.println ''' static JavaVM* jvm = nullptr; namespace hal { namespace sim { jint SimOnLoad(JavaVM* vm, void* reserved) { jvm = vm; JNIEnv *env; if (vm->GetEnv(reinterpret_cast(&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(&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 '}' } } }