diff --git a/build.gradle b/build.gradle index 3c00130fd5..f4c8281fd4 100644 --- a/build.gradle +++ b/build.gradle @@ -1,6 +1,7 @@ plugins { id "com.diffplug.spotless" version "6.22.0" id "edu.wpi.first.NativeUtils" version "2024.6.1" apply false + id 'edu.wpi.first.GradleJni' version '1.1.0' id "edu.wpi.first.wpilib.repositories.WPILibRepositoriesPlugin" version "2020.2" id "edu.wpi.first.GradleRIO" version "2024.1.1-beta-4" id 'edu.wpi.first.WpilibTools' version '1.3.0' @@ -90,3 +91,8 @@ spotless { wrapper { gradleVersion '8.4' } + +import edu.wpi.first.toolchain.NativePlatforms +ext.getCurrentArch = { + return NativePlatforms.desktop +} diff --git a/photon-core/build.gradle b/photon-core/build.gradle index d5de014915..48bc192dc7 100644 --- a/photon-core/build.gradle +++ b/photon-core/build.gradle @@ -1,7 +1,31 @@ +plugins { + id 'edu.wpi.first.WpilibTools' version '1.3.0' +} + import java.nio.file.Path apply from: "${rootDir}/shared/common.gradle" +ext { + nativeName = "photoncore" + + main_native_libs = [ + "opencv_shared" + ] + + test_native_libs = [ + "opencv_static" + ] + + dev_native_libs = [ + "opencv_static", + ] +} + +def sharedCvConfigs = [photoncore: []] +def staticCvConfigs = [:] + + dependencies { // JOGL stuff (currently we only distribute for aarch64, which is Pi 4) implementation "org.jogamp.gluegen:gluegen-rt:$joglVersion" @@ -22,3 +46,5 @@ task writeCurrentVersion { } build.dependsOn writeCurrentVersion + +apply from: "${rootDir}/shared/javacpp/setupBuild.gradle" diff --git a/photon-core/src/dev/native/cpp/devmain.cpp b/photon-core/src/dev/native/cpp/devmain.cpp new file mode 100644 index 0000000000..69f8136930 --- /dev/null +++ b/photon-core/src/dev/native/cpp/devmain.cpp @@ -0,0 +1,16 @@ +#include "test.h" + +#include +#include +#include + +#include + +int main() { + + cv::Mat mat = cv::imread("/home/matt/Documents/GitHub/photonvision/test-resources/testimages/2022/WPI/FarLaunchpad13ft10in.png"); + + printf("DevMain! mat size %i %i\n", mat.rows, mat.cols); + + return 1; +} \ No newline at end of file diff --git a/photon-core/src/main/java/org/photonvision/raspi/LibCameraJNI.java b/photon-core/src/main/java/org/photonvision/raspi/LibCameraJNI.java index 8a12aeabfa..a243a47967 100644 --- a/photon-core/src/main/java/org/photonvision/raspi/LibCameraJNI.java +++ b/photon-core/src/main/java/org/photonvision/raspi/LibCameraJNI.java @@ -116,7 +116,7 @@ public static boolean isSupported() { * * @param width Camera video mode width in pixels * @param height Camera video mode height in pixels - * @param fps Camera video mode FPS + * @param rotation Asdf * @return success of creating a camera object */ public static native boolean createCamera(int width, int height, int rotation); diff --git a/photon-core/src/main/native/cpp/jni/TestJni.cpp b/photon-core/src/main/native/cpp/jni/TestJni.cpp new file mode 100644 index 0000000000..6e8f36bd5a --- /dev/null +++ b/photon-core/src/main/native/cpp/jni/TestJni.cpp @@ -0,0 +1,8 @@ +#include + +extern "C" { + JNIEXPORT jint JNICALL + some_native_function() { + return 0; + } +} \ No newline at end of file diff --git a/photon-core/src/main/native/cpp/test.cpp b/photon-core/src/main/native/cpp/test.cpp new file mode 100644 index 0000000000..203a2f8f8e --- /dev/null +++ b/photon-core/src/main/native/cpp/test.cpp @@ -0,0 +1,16 @@ +#include "test.h" + +#include +#include +#include + +#include + +int some_test() { + + cv::Mat mat = cv::imread("/home/matt/Documents/GitHub/photonvision/test-resources/testimages/2022/WPI/FarLaunchpad13ft10in.png"); + + printf("mat size %i %i\n", mat.rows, mat.cols); + + return 1; +} \ No newline at end of file diff --git a/photon-core/src/main/native/include/test.h b/photon-core/src/main/native/include/test.h new file mode 100644 index 0000000000..4e3098346f --- /dev/null +++ b/photon-core/src/main/native/include/test.h @@ -0,0 +1,2 @@ +#pragma once +int some_test(); diff --git a/photon-core/src/test/native/cpp/devmain.cpp b/photon-core/src/test/native/cpp/devmain.cpp new file mode 100644 index 0000000000..b003512ea7 --- /dev/null +++ b/photon-core/src/test/native/cpp/devmain.cpp @@ -0,0 +1,7 @@ +#include "test.h" +#include + +int main(int argc, char** argv) { + printf("hello!\n"); + return some_test(); +} \ No newline at end of file diff --git a/photon-lib/build.gradle b/photon-lib/build.gradle index 099308f470..6b6cec8dec 100644 --- a/photon-lib/build.gradle +++ b/photon-lib/build.gradle @@ -11,6 +11,21 @@ ext { includePhotonTargeting = true // Include the generated Version file generatedHeaders = "src/generate/native/include" + + test_native_libs = [ + "cscore_shared", + "cameraserver_shared", + "wpilib_executable_shared", + "googletest_static", + "apriltag_shared", + "opencv_shared", + ] + + main_native_libs = [ + "wpilib_shared", "apriltag_shared", "opencv_shared" + ] + + dev_native_libs = [] } apply from: "${rootDir}/shared/javacpp/setupBuild.gradle" diff --git a/photon-targeting/build.gradle b/photon-targeting/build.gradle index 4238e951e6..5e8af89ee4 100644 --- a/photon-targeting/build.gradle +++ b/photon-targeting/build.gradle @@ -4,6 +4,21 @@ plugins { ext { nativeName = "photontargeting" + + main_native_libs = [ + "wpilib_shared", "apriltag_shared", "opencv_shared" + ] + + test_native_libs = [ + "cscore_shared", + "cameraserver_shared", + "wpilib_executable_shared", + "googletest_static", + "apriltag_shared", + "opencv_shared", + ] + + dev_native_libs = [] } apply from: "${rootDir}/shared/javacpp/setupBuild.gradle" diff --git a/shared/javacpp/setupBuild.gradle b/shared/javacpp/setupBuild.gradle index dd8a5bec32..6ebc1dfcbb 100644 --- a/shared/javacpp/setupBuild.gradle +++ b/shared/javacpp/setupBuild.gradle @@ -1,6 +1,7 @@ apply plugin: 'cpp' apply plugin: 'google-test-test-suite' apply plugin: 'edu.wpi.first.NativeUtils' +apply plugin: 'edu.wpi.first.GradleJni' apply from: "${rootDir}/shared/config.gradle" apply from: "${rootDir}/shared/javacommon.gradle" @@ -60,6 +61,86 @@ model { nativeUtils.useRequiredLibrary(it, "wpilib_shared") nativeUtils.useRequiredLibrary(it, "apriltag_shared") nativeUtils.useRequiredLibrary(it, "opencv_shared") + main_native_libs.each { name -> + nativeUtils.useRequiredLibrary(it, name) + } + } + // By default, a development executable will be generated. This is to help the case of + // testing specific functionality of the library. + "${nativeName}Dev"(NativeExecutableSpec) { + targetBuildTypes 'debug' + sources { + cpp { + source { + srcDirs 'src/dev/native/cpp' + include '**/*.cpp' + } + exportedHeaders { + srcDir 'src/main/native/include' + if (project.hasProperty('generatedHeaders')) { + srcDir generatedHeaders + } + } + } + } + binaries.all { + lib library: nativeName, linkage: 'shared' + it.tasks.withType(CppCompile) { + // it.dependsOn generateProto + } + if (project.hasProperty('exeSplitSetup')) { + exeSplitSetup(it) + } + } + dev_native_libs.each { name -> + nativeUtils.useRequiredLibrary(it, name) + } + } + "${nativeName}JNI"(JniNativeLibrarySpec) { + baseName = nativeName + 'jni' + + enableCheckTask !project.hasProperty('skipJniCheck') + javaCompileTasks << compileJava + jniCrossCompileOptions << JniCrossCompileOptions(nativeUtils.wpi.platforms.roborio) + jniCrossCompileOptions << JniCrossCompileOptions(nativeUtils.wpi.platforms.linuxarm32) + jniCrossCompileOptions << JniCrossCompileOptions(nativeUtils.wpi.platforms.linuxarm64) + + sources { + cpp { + source { + srcDirs 'src/main/native/cpp' + if (project.hasProperty('generatedSources')) { + srcDir generatedSources + } + include '**/jni/**/*.cpp' + } + exportedHeaders { + srcDir 'src/main/native/include' + if (project.hasProperty('generatedHeaders')) { + srcDir generatedHeaders + } + include '**/*.h' + } + } + } + + binaries.all { + if (it instanceof StaticLibraryBinarySpec) { + it.buildable = false + return + } + lib library: "${nativeName}", linkage: 'shared' + if (!project.hasProperty('noWpiutil')) { + lib project: ':wpiutil', library: 'wpiutil', linkage: 'shared' + } + if (project.hasProperty('jniSplitSetup')) { + jniSplitSetup(it) + } + } + + if(project.hasProperty("jni_native_libs")) jni_native_libs.each { name -> + nativeUtils.useRequiredLibrary(it, name) + } } } testSuites { @@ -88,12 +169,9 @@ model { } } - nativeUtils.useRequiredLibrary(it, "cscore_shared") - nativeUtils.useRequiredLibrary(it, "cameraserver_shared") - nativeUtils.useRequiredLibrary(it, "wpilib_executable_shared") - nativeUtils.useRequiredLibrary(it, "googletest_static") - nativeUtils.useRequiredLibrary(it, "apriltag_shared") - nativeUtils.useRequiredLibrary(it, "opencv_shared") + test_native_libs.each { name -> + nativeUtils.useRequiredLibrary(it, name) + } } } }