@@ -13,9 +13,9 @@ endif()
13
13
14
14
set (JAVA_SRC_ROOT ${CMAKE_CURRENT_SOURCE_DIR} )
15
15
# <build output dir>/src/java (path used with add_subdirectory in root CMakeLists.txt)
16
- set (JAVA_OUTPUT_DIR ${CMAKE_CURRENT_BINARY_DIR} )
16
+ set (JAVA_OUTPUT_DIR ${CMAKE_CURRENT_BINARY_DIR} )
17
17
18
- # Should we use onnxruntime-genai or onnxruntime-genai-static? Using onnxruntime-genai for now.
18
+ # Should we use onnxruntime-genai or onnxruntime-genai-static? Using onnxruntime-genai for now.
19
19
# Add dependency on native target
20
20
set (JAVA_DEPENDS onnxruntime-genai)
21
21
@@ -34,20 +34,20 @@ elseif (ANDROID)
34
34
endif ()
35
35
36
36
# this jar is solely used to signaling mechanism for dependency management in CMake
37
- # if any of the Java sources change, the jar (and generated headers) will be regenerated
37
+ # if any of the Java sources change, the jar (and generated headers) will be regenerated
38
38
# and the onnxruntime-genai-jni target will be rebuilt
39
39
set (JAVA_OUTPUT_JAR ${JAVA_OUTPUT_DIR} /build /libs/onnxruntime-genai.jar)
40
40
set (GRADLE_ARGS clean jar -x test )
41
41
42
42
# this jar is solely used to signaling mechanism for dependency management in CMake
43
- # if any of the Java sources change, the jar (and generated headers) will be regenerated
43
+ # if any of the Java sources change, the jar (and generated headers) will be regenerated
44
44
# and the onnxruntime-genai-jni target will be rebuilt
45
45
set (JAVA_OUTPUT_JAR ${JAVA_SRC_ROOT} /build /libs/onnxruntime-genai.jar)
46
46
set (GRADLE_ARGS clean jar -x test )
47
47
48
- add_custom_command (OUTPUT ${JAVA_OUTPUT_JAR}
49
- COMMAND ${GRADLE_EXECUTABLE} ${GRADLE_OPTIONS} ${GRADLE_ARGS}
50
- WORKING_DIRECTORY ${JAVA_SRC_ROOT}
48
+ add_custom_command (OUTPUT ${JAVA_OUTPUT_JAR}
49
+ COMMAND ${GRADLE_EXECUTABLE} ${GRADLE_OPTIONS} ${GRADLE_ARGS}
50
+ WORKING_DIRECTORY ${JAVA_SRC_ROOT}
51
51
DEPENDS ${genai4j_gradle_files} ${genai4j_srcs} )
52
52
add_custom_target (onnxruntime-genai4j DEPENDS ${JAVA_OUTPUT_JAR} )
53
53
@@ -60,13 +60,13 @@ file(GLOB genai4j_native_src
60
60
"${JAVA_SRC_ROOT} /src/main/native/*.h"
61
61
"${SRC_ROOT} /ort_genai_c.h"
62
62
)
63
-
63
+
64
64
add_library (onnxruntime-genai-jni SHARED ${genai4j_native_src} )
65
65
set_property (TARGET onnxruntime-genai-jni PROPERTY CXX_STANDARD 17)
66
66
add_dependencies (onnxruntime-genai-jni onnxruntime-genai4j)
67
67
# the JNI headers are generated in the genai4j target
68
68
target_include_directories (onnxruntime-genai-jni PRIVATE ${SRC_ROOT}
69
- ${JAVA_SRC_ROOT} /build /headers
69
+ ${JAVA_SRC_ROOT} /build /headers
70
70
${JNI_INCLUDE_DIRS} )
71
71
target_link_libraries (onnxruntime-genai-jni PUBLIC onnxruntime-genai)
72
72
@@ -104,13 +104,13 @@ file(MAKE_DIRECTORY ${JAVA_PACKAGE_LIB_DIR})
104
104
105
105
# Add the native genai library to the native-lib dir
106
106
add_custom_command (TARGET onnxruntime-genai-jni POST_BUILD
107
- COMMAND ${CMAKE_COMMAND} -E copy_if_different
108
- $<TARGET_FILE:onnxruntime-genai>
107
+ COMMAND ${CMAKE_COMMAND} -E copy_if_different
108
+ $<TARGET_FILE:onnxruntime-genai>
109
109
${JAVA_PACKAGE_LIB_DIR} /$<TARGET_FILE_NAME:onnxruntime-genai>)
110
110
111
111
# Add the JNI bindings to the native-jni dir
112
112
add_custom_command (TARGET onnxruntime-genai-jni POST_BUILD
113
- COMMAND ${CMAKE_COMMAND} -E copy_if_different
113
+ COMMAND ${CMAKE_COMMAND} -E copy_if_different
114
114
$<TARGET_FILE:onnxruntime-genai-jni>
115
115
${JAVA_PACKAGE_LIB_DIR} /$<TARGET_FILE_NAME:onnxruntime-genai-jni>)
116
116
@@ -140,21 +140,21 @@ if (ANDROID)
140
140
file (MAKE_DIRECTORY ${ANDROID_PACKAGE_HEADERS_DIR} )
141
141
file (MAKE_DIRECTORY ${ANDROID_PACKAGE_ABI_DIR} )
142
142
143
- # copy C/C++ API headers to be packed into Android AAR package
144
- add_custom_command (TARGET onnxruntime-genai-jni POST_BUILD
143
+ # copy C/C++ API headers to be packed into Android AAR package
144
+ add_custom_command (TARGET onnxruntime-genai-jni POST_BUILD
145
145
COMMAND ${CMAKE_COMMAND} -E copy_if_different ${SRC_ROOT} /ort_genai.h ${ANDROID_PACKAGE_HEADERS_DIR} )
146
- add_custom_command (TARGET onnxruntime-genai-jni POST_BUILD
146
+ add_custom_command (TARGET onnxruntime-genai-jni POST_BUILD
147
147
COMMAND ${CMAKE_COMMAND} -E copy_if_different ${SRC_ROOT} /ort_genai_c.h ${ANDROID_PACKAGE_HEADERS_DIR} )
148
148
149
149
# Copy onnxruntime-genai.so and onnxruntime-genai-jni.so for building Android AAR package
150
150
add_custom_command (TARGET onnxruntime-genai-jni POST_BUILD
151
- COMMAND ${CMAKE_COMMAND} -E copy_if_different
151
+ COMMAND ${CMAKE_COMMAND} -E copy_if_different
152
152
$<TARGET_FILE:onnxruntime-genai>
153
153
${ANDROID_PACKAGE_ABI_DIR} /$<TARGET_LINKER_FILE_NAME:onnxruntime-genai>)
154
154
155
155
add_custom_command (TARGET onnxruntime-genai-jni POST_BUILD
156
- COMMAND ${CMAKE_COMMAND} -E copy_if_different
157
- $<TARGET_FILE:onnxruntime-genai-jni>
156
+ COMMAND ${CMAKE_COMMAND} -E copy_if_different
157
+ $<TARGET_FILE:onnxruntime-genai-jni>
158
158
${ANDROID_PACKAGE_ABI_DIR} /$<TARGET_LINKER_FILE_NAME:onnxruntime-genai-jni>)
159
159
160
160
# Generate the Android AAR package
@@ -176,20 +176,20 @@ if (ANDROID)
176
176
file (GLOB android_test_files "${ANDROID_TEST_SRC_ROOT} /*" )
177
177
file (COPY ${android_test_files} DESTINATION ${ANDROID_TEST_PACKAGE_DIR} )
178
178
179
- set (ANDROID_TEST_PACKAGE_LIB_DIR ${ANDROID_TEST_PACKAGE_DIR} /app/libs)
179
+ set (ANDROID_TEST_PACKAGE_LIB_DIR ${ANDROID_TEST_PACKAGE_DIR} /app/libs)
180
180
set (ANDROID_TEST_PACKAGE_APP_ASSETS_DIR ${ANDROID_TEST_PACKAGE_DIR} /app/src/main/assets)
181
181
file (MAKE_DIRECTORY ${ANDROID_TEST_PACKAGE_LIB_DIR} )
182
182
file (MAKE_DIRECTORY ${ANDROID_TEST_PACKAGE_APP_ASSETS_DIR} )
183
183
184
184
# Copy the test model to the assets folder in the test app
185
185
add_custom_command (TARGET onnxruntime-genai-jni POST_BUILD
186
- COMMAND ${CMAKE_COMMAND} -E copy_directory_if_different
186
+ COMMAND ${CMAKE_COMMAND} -E copy_directory_if_different
187
187
${REPO_ROOT} /test /test_models/hf-internal -testing/tiny-random-gpt2-fp32
188
188
${ANDROID_TEST_PACKAGE_APP_ASSETS_DIR} /model)
189
189
190
190
# Copy the Android AAR package we built to the libs folder of our test app
191
191
add_custom_command (TARGET onnxruntime-genai-jni POST_BUILD
192
- COMMAND ${CMAKE_COMMAND} -E copy_if_different
192
+ COMMAND ${CMAKE_COMMAND} -E copy_if_different
193
193
${ANDROID_PACKAGE_OUTPUT_DIR} /outputs/aar/onnxruntime-genai-debug.aar
194
194
${ANDROID_TEST_PACKAGE_LIB_DIR} /onnxruntime-genai.aar)
195
195
@@ -207,19 +207,38 @@ if (ENABLE_TESTS)
207
207
# On windows ctest requires a test to be an .exe(.com) file
208
208
# With gradle wrapper we get gradlew.bat. We delegate execution to a separate .cmake file
209
209
# That can handle both .exe and .bat
210
- add_test (NAME onnxruntime-genai4j_test
211
- COMMAND ${CMAKE_COMMAND}
212
- -DGRADLE_EXECUTABLE=${GRADLE_EXECUTABLE}
213
- -DBIN_DIR=${JAVA_OUTPUT_DIR}
210
+ add_test (NAME onnxruntime-genai4j_test
211
+ COMMAND ${CMAKE_COMMAND}
212
+ -DGRADLE_EXECUTABLE=${GRADLE_EXECUTABLE}
213
+ -DBIN_DIR=${JAVA_OUTPUT_DIR}
214
214
-DJAVA_SRC_ROOT=${JAVA_SRC_ROOT}
215
215
-DJAVA_PACKAGE_LIB_DIR=${JAVA_PACKAGE_LIB_DIR}
216
216
-P ${JAVA_SRC_ROOT} /windows-unittests.cmake)
217
217
else ()
218
- add_test (NAME onnxruntime-genai4j_test
219
- COMMAND ${GRADLE_EXECUTABLE} cmakeCheck
218
+ add_test (NAME onnxruntime-genai4j_test
219
+ COMMAND ${GRADLE_EXECUTABLE} cmakeCheck
220
220
-DcmakeBuildDir=${JAVA_OUTPUT_DIR} -DnativeLibDir=${JAVA_PACKAGE_LIB_DIR}
221
221
WORKING_DIRECTORY ${JAVA_SRC_ROOT} )
222
222
endif ()
223
223
224
+ if (WIN32 )
225
+ set (ONNXRUNTIME_GENAI_DEPENDENCY "*.dll" )
226
+ elseif (APPLE )
227
+ set (ONNXRUNTIME_GENAI_DEPENDENCY "*.dylib" )
228
+ else ()
229
+ set (ONNXRUNTIME_GENAI_DEPENDENCY "*.so" )
230
+ endif ()
231
+
232
+ file (GLOB ort_native_libs "${ORT_LIB_DIR} /${ONNXRUNTIME_GENAI_DEPENDENCY} " )
233
+
234
+ # Copy ORT native libs for Java tests
235
+ foreach (LIB_FILE ${ort_native_libs} )
236
+ add_custom_command (
237
+ TARGET onnxruntime-genai-jni POST_BUILD
238
+ COMMAND ${CMAKE_COMMAND} -E copy
239
+ ${LIB_FILE}
240
+ ${JAVA_PACKAGE_LIB_DIR} /)
241
+ endforeach ()
242
+
224
243
set_property (TEST onnxruntime-genai4j_test APPEND PROPERTY DEPENDS onnxruntime-genai-jni)
225
244
endif ()
0 commit comments