onnxruntime/cmake/onnxruntime_java.cmake

170 lines
7.2 KiB
CMake
Raw Normal View History

2019-12-10 16:28:47 +00:00
# Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.
# Licensed under the MIT License.
#set(CMAKE_VERBOSE_MAKEFILE on)
# Setup Java compilation
include(FindJava)
find_package(Java REQUIRED)
include(UseJava)
if (NOT CMAKE_SYSTEM_NAME STREQUAL "Android")
find_package(JNI REQUIRED)
include_directories(${JNI_INCLUDE_DIRS})
endif()
2019-12-10 16:28:47 +00:00
set(JAVA_ROOT ${REPO_ROOT}/java)
2020-02-18 23:41:49 +00:00
set(JAVA_OUTPUT_DIR ${CMAKE_CURRENT_BINARY_DIR}/java)
2019-12-10 16:28:47 +00:00
if (onnxruntime_RUN_ONNX_TESTS)
set(JAVA_DEPENDS onnxruntime ${test_data_target})
else()
set(JAVA_DEPENDS onnxruntime)
endif()
2020-02-18 23:41:49 +00:00
# use the gradle wrapper if it exists
if(EXISTS "${JAVA_ROOT}/gradlew")
set(GRADLE_EXECUTABLE "${JAVA_ROOT}/gradlew")
else()
# fall back to gradle on our PATH
find_program(GRADLE_EXECUTABLE gradle)
if(NOT GRADLE_EXECUTABLE)
message(SEND_ERROR "Gradle installation not found")
endif()
endif()
message(STATUS "Using gradle: ${GRADLE_EXECUTABLE}")
2019-12-10 16:28:47 +00:00
2020-02-18 23:41:49 +00:00
# Specify the Java source files
file(GLOB_RECURSE onnxruntime4j_gradle_files "${JAVA_ROOT}/*.gradle")
file(GLOB_RECURSE onnxruntime4j_src "${JAVA_ROOT}/src/main/java/ai/onnxruntime/*.java")
set(JAVA_OUTPUT_JAR ${JAVA_ROOT}/build/libs/onnxruntime.jar)
# this jar is solely used to signaling mechanism for dependency management in CMake
2020-02-18 23:41:49 +00:00
# if any of the Java sources change, the jar (and generated headers) will be regenerated and the onnxruntime4j_jni target will be rebuilt
set(GRADLE_ARGS clean jar)
if(WIN32)
set(GRADLE_ARGS ${GRADLE_ARGS} -Dorg.gradle.daemon=false)
endif()
Cherry pick PRs to Rel-1.3.1 (#4198) * link to folder instead of READMEs inside folder (#3938) otherwise hard to find the source code * [Node.js binding] fix linux build (#3927) * [Node.js binding] add build flag for node.js binding (#3948) * [Nodejs binding] create a new pipeline to generate signed binaries (#4104) * add yml files * update pipeline * fix yaml syntax * yaml pop BuildCSharp * udpate yaml * do not stage codesign summary * fix build: pipeline Node.js version to 12.16.3 (#4145) * [Node.js binding] upgrade node-addon-api to 3.0 (#4148) * [Node.js binding] add linux and mac package (#4157) * try mac pipeline * fix path separator * copy prebuilds folder * split esrp yaml for win/mac * disable mac signing temporarily * add linux * fix indent * add nodetool in linux * add nodetool in win-ci-2019 * replace linux build by custom docker scripts * use manylinux as node 12.16 not working on centos6 * try ubuntu * loosen timeout for test case - multiple runs calls * add script to support update nodejs binding version (#4164) * [java] Adds a CUDA test (#3956) * [java] - adding a cuda enabled test. * Adding --build_java to the windows gpu ci pipeline. * Removing a stray line from the unit tests that always enabled CUDA for Java. * Update OnnxRuntime.java for OS X environment. (#3985) onnxruntime init failure due to wrong path of reading native libraries. In OS X 64 system, the arch name is detected as x86 which generates invalid path to read native libraries. Exception java.lang.UnsatisfiedLinkError: no onnxruntime in java.library.path at java.lang.ClassLoader.loadLibrary(ClassLoader.java:1867) at java.lang.Runtime.loadLibrary0(Runtime.java:870) at java.lang.System.loadLibrary(System.java:1122) at ai.onnxruntime.OnnxRuntime.load(OnnxRuntime.java:174) at ai.onnxruntime.OnnxRuntime.init(OnnxRuntime.java:81) at ai.onnxruntime.OrtEnvironment.<clinit>(OrtEnvironment.java:24) * Create Java publishing pipeline (#3944) Create CPU and GPu Java publishing pipelines. Final jars are tested on all platforms. However, signing and publishing to maven are manual steps. * Change group id to com.microsoft.onnxruntime per requirements. * Java GPu artifact naming (#4179) Modify gradle build so artifactID has _gpu for GPU builds. Pass USE_CUDA flag on CUDA build Adjust publishing pipelines to extract POM from a correct path. Co-Authored-By: @Craigacp * bump up ORT version to 1.3.1 (#4181) * move back to toolset 14.16 to possibly work around nvcc bug (#4180) * Symbolic shape inference exit on models without onnx opset used (#4090) * Symbolic shape inference exit on models without onnx opset used * Temporary fix for ConvTranspose with symbolic input dims Co-authored-by: Changming Sun <me@sunchangming.com> * Fix Nuphar test failure * Enlarge the read buffer size in C#/Java test code (#4150) 1. Enlarge the read buffer size further, so that our code can run even faster. TODO: need apply the similar changes to python some other language bindings. 2. Add coreml_VGG16_ImageNet to the test exclusion set of x86_32. It is not a new model but previously we didn't run the test against x86_32. * Temporarily disable windows static analysis CI job * skip model coreml_Imputer-LogisticRegression_sklearn_load_breast_cancer * Delete unused variable Co-authored-by: Prasanth Pulavarthi <prasantp@microsoft.com> Co-authored-by: Yulong Wang <yulongw@microsoft.com> Co-authored-by: Adam Pocock <adam.pocock@oracle.com> Co-authored-by: jji2019 <49252772+jji2019@users.noreply.github.com> Co-authored-by: Dmitri Smirnov <yuslepukhin@users.noreply.github.com> Co-authored-by: Dmitri Smirnov <dmitrism@microsoft.com> Co-authored-by: George Wu <jywu@microsoft.com> Co-authored-by: KeDengMS <kedeng@microsoft.com> Co-authored-by: Changming Sun <me@sunchangming.com> Co-authored-by: Changming Sun <chasun@microsoft.com>
2020-06-12 18:27:02 +00:00
if(onnxruntime_USE_CUDA)
set(GRADLE_ARGS ${GRADLE_ARGS} -DUSE_CUDA=1)
endif()
add_custom_command(OUTPUT ${JAVA_OUTPUT_JAR} COMMAND ${GRADLE_EXECUTABLE} ${GRADLE_ARGS} WORKING_DIRECTORY ${JAVA_ROOT} DEPENDS ${onnxruntime4j_gradle_files} ${onnxruntime4j_src})
2020-02-18 23:41:49 +00:00
add_custom_target(onnxruntime4j DEPENDS ${JAVA_OUTPUT_JAR})
set_source_files_properties(${JAVA_OUTPUT_JAR} PROPERTIES GENERATED TRUE)
set_property(TARGET onnxruntime4j APPEND PROPERTY ADDITIONAL_CLEAN_FILES "${JAVA_OUTPUT_DIR}")
2019-12-10 16:28:47 +00:00
2020-02-18 23:41:49 +00:00
# Specify the native sources
2019-12-10 16:28:47 +00:00
file(GLOB onnxruntime4j_native_src
2020-02-18 23:41:49 +00:00
"${JAVA_ROOT}/src/main/native/*.c"
"${JAVA_ROOT}/src/main/native/*.h"
2019-12-10 16:28:47 +00:00
"${REPO_ROOT}/include/onnxruntime/core/session/*.h"
)
# Build the JNI library
2020-02-18 23:41:49 +00:00
add_library(onnxruntime4j_jni SHARED ${onnxruntime4j_native_src})
set_property(TARGET onnxruntime4j_jni PROPERTY CXX_STANDARD 11)
# Tell the JNI code about the requested providers
if (onnxruntime_USE_CUDA)
target_compile_definitions(onnxruntime4j_jni PRIVATE USE_CUDA=1)
endif()
if (onnxruntime_USE_DNNL)
target_compile_definitions(onnxruntime4j_jni PRIVATE USE_DNNL=1)
endif()
if (onnxruntime_USE_NGRAPH)
target_compile_definitions(onnxruntime4j_jni PRIVATE USE_NGRAPH=1)
endif()
if (onnxruntime_USE_OPENVINO)
target_compile_definitions(onnxruntime4j_jni PRIVATE USE_OPENVINO=1)
endif()
if (onnxruntime_USE_TENSORRT)
target_compile_definitions(onnxruntime4j_jni PRIVATE USE_TENSORRT=1)
endif()
if (onnxruntime_USE_NNAPI)
target_compile_definitions(onnxruntime4j_jni PRIVATE USE_NNAPI=1)
endif()
if (onnxruntime_USE_NUPHAR)
target_compile_definitions(onnxruntime4j_jni PRIVATE USE_NUPHAR=1)
endif()
if (onnxruntime_USE_ACL)
target_compile_definitions(onnxruntime4j_jni PRIVATE USE_ACL=1)
endif()
if (onnxruntime_USE_DML)
target_compile_definitions(onnxruntime4j_jni PRIVATE USE_DIRECTML=1)
endif()
2020-02-18 23:41:49 +00:00
# depend on java sources. if they change, the JNI should recompile
add_dependencies(onnxruntime4j_jni onnxruntime4j)
2019-12-10 16:28:47 +00:00
onnxruntime_add_include_to_target(onnxruntime4j_jni onnxruntime_session)
2020-02-18 23:41:49 +00:00
# the JNI headers are generated in the onnxruntime4j target
target_include_directories(onnxruntime4j_jni PRIVATE ${REPO_ROOT}/include ${JAVA_ROOT}/build/headers)
target_link_libraries(onnxruntime4j_jni PUBLIC onnxruntime)
2019-12-10 16:28:47 +00:00
set(JAVA_PACKAGE_OUTPUT_DIR ${JAVA_OUTPUT_DIR}/build)
file(MAKE_DIRECTORY ${JAVA_PACKAGE_OUTPUT_DIR})
if (CMAKE_SYSTEM_NAME STREQUAL "Android")
set(ANDROID_PACKAGE_OUTPUT_DIR ${JAVA_PACKAGE_OUTPUT_DIR}/android)
file(MAKE_DIRECTORY ${ANDROID_PACKAGE_OUTPUT_DIR})
endif()
# Set platform and ach for packaging
if(CMAKE_SIZEOF_VOID_P EQUAL "8")
set(JNI_ARCH x64)
else()
message(FATAL_ERROR "Java is currently not supported for x86 architecture")
endif()
if (WIN32)
set(JAVA_PLAT "win")
elseif (APPLE)
set(JAVA_PLAT "osx")
elseif (${CMAKE_SYSTEM_NAME} MATCHES "Linux")
set(JAVA_PLAT "linux")
else()
# We don't do distribution for Android
# Set for completeness
set(JAVA_PLAT "android")
endif()
# Similar to Nuget schema
set(JAVA_OS_ARCH ${JAVA_PLAT}-${JNI_ARCH})
2020-02-18 23:41:49 +00:00
# expose native libraries to the gradle build process
set(JAVA_PACKAGE_DIR ai/onnxruntime/native/${JAVA_OS_ARCH})
2020-02-18 23:41:49 +00:00
set(JAVA_NATIVE_LIB_DIR ${JAVA_OUTPUT_DIR}/native-lib)
set(JAVA_NATIVE_JNI_DIR ${JAVA_OUTPUT_DIR}/native-jni)
set(JAVA_PACKAGE_LIB_DIR ${JAVA_NATIVE_LIB_DIR}/${JAVA_PACKAGE_DIR})
set(JAVA_PACKAGE_JNI_DIR ${JAVA_NATIVE_JNI_DIR}/${JAVA_PACKAGE_DIR})
file(MAKE_DIRECTORY ${JAVA_PACKAGE_LIB_DIR})
file(MAKE_DIRECTORY ${JAVA_PACKAGE_JNI_DIR})
if (CMAKE_SYSTEM_NAME STREQUAL "Android")
set(ANDROID_PACKAGE_JNILIBS_DIR ${JAVA_OUTPUT_DIR}/android)
set(ANDROID_PACKAGE_ABI_DIR ${ANDROID_PACKAGE_JNILIBS_DIR}/${ANDROID_ABI})
file(MAKE_DIRECTORY ${ANDROID_PACKAGE_JNILIBS_DIR})
file(MAKE_DIRECTORY ${ANDROID_PACKAGE_ABI_DIR})
endif()
# On Windows TARGET_LINKER_FILE_NAME is the .lib, TARGET_FILE_NAME is the .dll
if (WIN32)
add_custom_command(TARGET onnxruntime4j_jni POST_BUILD COMMAND ${CMAKE_COMMAND} -E create_symlink $<TARGET_FILE:onnxruntime> ${JAVA_PACKAGE_LIB_DIR}/$<TARGET_FILE_NAME:onnxruntime>)
add_custom_command(TARGET onnxruntime4j_jni POST_BUILD COMMAND ${CMAKE_COMMAND} -E create_symlink $<TARGET_FILE:onnxruntime4j_jni> ${JAVA_PACKAGE_JNI_DIR}/$<TARGET_FILE_NAME:onnxruntime4j_jni>)
else()
add_custom_command(TARGET onnxruntime4j_jni POST_BUILD COMMAND ${CMAKE_COMMAND} -E create_symlink $<TARGET_FILE:onnxruntime> ${JAVA_PACKAGE_LIB_DIR}/$<TARGET_LINKER_FILE_NAME:onnxruntime>)
add_custom_command(TARGET onnxruntime4j_jni POST_BUILD COMMAND ${CMAKE_COMMAND} -E create_symlink $<TARGET_FILE:onnxruntime4j_jni> ${JAVA_PACKAGE_JNI_DIR}/$<TARGET_LINKER_FILE_NAME:onnxruntime4j_jni>)
endif()
if (CMAKE_SYSTEM_NAME STREQUAL "Android")
add_custom_command(TARGET onnxruntime4j_jni POST_BUILD COMMAND ${CMAKE_COMMAND} -E create_symlink $<TARGET_FILE:onnxruntime> ${ANDROID_PACKAGE_ABI_DIR}/$<TARGET_LINKER_FILE_NAME:onnxruntime>)
add_custom_command(TARGET onnxruntime4j_jni POST_BUILD COMMAND ${CMAKE_COMMAND} -E create_symlink $<TARGET_FILE:onnxruntime4j_jni> ${ANDROID_PACKAGE_ABI_DIR}/$<TARGET_LINKER_FILE_NAME:onnxruntime4j_jni>)
endif()
2020-02-18 23:41:49 +00:00
# run the build process (this copies the results back into CMAKE_CURRENT_BINARY_DIR)
set(GRADLE_ARGS cmakeBuild -DcmakeBuildDir=${CMAKE_CURRENT_BINARY_DIR})
if(WIN32)
set(GRADLE_ARGS ${GRADLE_ARGS} -Dorg.gradle.daemon=false)
endif()
Cherry pick PRs to Rel-1.3.1 (#4198) * link to folder instead of READMEs inside folder (#3938) otherwise hard to find the source code * [Node.js binding] fix linux build (#3927) * [Node.js binding] add build flag for node.js binding (#3948) * [Nodejs binding] create a new pipeline to generate signed binaries (#4104) * add yml files * update pipeline * fix yaml syntax * yaml pop BuildCSharp * udpate yaml * do not stage codesign summary * fix build: pipeline Node.js version to 12.16.3 (#4145) * [Node.js binding] upgrade node-addon-api to 3.0 (#4148) * [Node.js binding] add linux and mac package (#4157) * try mac pipeline * fix path separator * copy prebuilds folder * split esrp yaml for win/mac * disable mac signing temporarily * add linux * fix indent * add nodetool in linux * add nodetool in win-ci-2019 * replace linux build by custom docker scripts * use manylinux as node 12.16 not working on centos6 * try ubuntu * loosen timeout for test case - multiple runs calls * add script to support update nodejs binding version (#4164) * [java] Adds a CUDA test (#3956) * [java] - adding a cuda enabled test. * Adding --build_java to the windows gpu ci pipeline. * Removing a stray line from the unit tests that always enabled CUDA for Java. * Update OnnxRuntime.java for OS X environment. (#3985) onnxruntime init failure due to wrong path of reading native libraries. In OS X 64 system, the arch name is detected as x86 which generates invalid path to read native libraries. Exception java.lang.UnsatisfiedLinkError: no onnxruntime in java.library.path at java.lang.ClassLoader.loadLibrary(ClassLoader.java:1867) at java.lang.Runtime.loadLibrary0(Runtime.java:870) at java.lang.System.loadLibrary(System.java:1122) at ai.onnxruntime.OnnxRuntime.load(OnnxRuntime.java:174) at ai.onnxruntime.OnnxRuntime.init(OnnxRuntime.java:81) at ai.onnxruntime.OrtEnvironment.<clinit>(OrtEnvironment.java:24) * Create Java publishing pipeline (#3944) Create CPU and GPu Java publishing pipelines. Final jars are tested on all platforms. However, signing and publishing to maven are manual steps. * Change group id to com.microsoft.onnxruntime per requirements. * Java GPu artifact naming (#4179) Modify gradle build so artifactID has _gpu for GPU builds. Pass USE_CUDA flag on CUDA build Adjust publishing pipelines to extract POM from a correct path. Co-Authored-By: @Craigacp * bump up ORT version to 1.3.1 (#4181) * move back to toolset 14.16 to possibly work around nvcc bug (#4180) * Symbolic shape inference exit on models without onnx opset used (#4090) * Symbolic shape inference exit on models without onnx opset used * Temporary fix for ConvTranspose with symbolic input dims Co-authored-by: Changming Sun <me@sunchangming.com> * Fix Nuphar test failure * Enlarge the read buffer size in C#/Java test code (#4150) 1. Enlarge the read buffer size further, so that our code can run even faster. TODO: need apply the similar changes to python some other language bindings. 2. Add coreml_VGG16_ImageNet to the test exclusion set of x86_32. It is not a new model but previously we didn't run the test against x86_32. * Temporarily disable windows static analysis CI job * skip model coreml_Imputer-LogisticRegression_sklearn_load_breast_cancer * Delete unused variable Co-authored-by: Prasanth Pulavarthi <prasantp@microsoft.com> Co-authored-by: Yulong Wang <yulongw@microsoft.com> Co-authored-by: Adam Pocock <adam.pocock@oracle.com> Co-authored-by: jji2019 <49252772+jji2019@users.noreply.github.com> Co-authored-by: Dmitri Smirnov <yuslepukhin@users.noreply.github.com> Co-authored-by: Dmitri Smirnov <dmitrism@microsoft.com> Co-authored-by: George Wu <jywu@microsoft.com> Co-authored-by: KeDengMS <kedeng@microsoft.com> Co-authored-by: Changming Sun <me@sunchangming.com> Co-authored-by: Changming Sun <chasun@microsoft.com>
2020-06-12 18:27:02 +00:00
if(onnxruntime_USE_CUDA)
set(GRADLE_ARGS ${GRADLE_ARGS} -DUSE_CUDA=1)
endif()
add_custom_command(TARGET onnxruntime4j_jni POST_BUILD COMMAND ${GRADLE_EXECUTABLE} ${GRADLE_ARGS} WORKING_DIRECTORY ${JAVA_ROOT})
if (CMAKE_SYSTEM_NAME STREQUAL "Android")
add_custom_command(TARGET onnxruntime4j_jni POST_BUILD COMMAND ${GRADLE_EXECUTABLE} -b build-android.gradle -c settings-android.gradle build -DjniLibsDir=${ANDROID_PACKAGE_JNILIBS_DIR} -DbuildDir=${ANDROID_PACKAGE_OUTPUT_DIR} WORKING_DIRECTORY ${JAVA_ROOT})
endif()