|
| 1 | +/* |
| 2 | + * SPDX-License-Identifier: Apache-2.0 |
| 3 | + * |
| 4 | + * The OpenSearch Contributors require contributions made to |
| 5 | + * this file be licensed under the Apache-2.0 license or a |
| 6 | + * compatible open source license. |
| 7 | + */ |
| 8 | + |
| 9 | +apply plugin: 'java' |
| 10 | +apply plugin: 'idea' |
| 11 | +apply plugin: 'opensearch.internal-cluster-test' |
| 12 | +apply plugin: 'opensearch.yaml-rest-test' |
| 13 | +apply plugin: 'opensearch.pluginzip' |
| 14 | + |
| 15 | +def pluginName = 'engine-datafusion' |
| 16 | +def pluginDescription = 'OpenSearch plugin providing access to DataFusion via JNI' |
| 17 | +def projectPath = 'org.opensearch' |
| 18 | +def pathToPlugin = 'datafusion.DataFusionPlugin' |
| 19 | +def pluginClassName = 'DataFusionPlugin' |
| 20 | + |
| 21 | +opensearchplugin { |
| 22 | + name = pluginName |
| 23 | + description = pluginDescription |
| 24 | + classname = "${projectPath}.${pathToPlugin}" |
| 25 | + licenseFile = rootProject.file('LICENSE.txt') |
| 26 | + noticeFile = rootProject.file('NOTICE.txt') |
| 27 | +} |
| 28 | + |
| 29 | +dependencies { |
| 30 | + implementation "org.apache.logging.log4j:log4j-api:${versions.log4j}" |
| 31 | + implementation "org.apache.logging.log4j:log4j-core:${versions.log4j}" |
| 32 | + testImplementation "junit:junit:${versions.junit}" |
| 33 | + testImplementation "org.hamcrest:hamcrest:${versions.hamcrest}" |
| 34 | + testImplementation "org.mockito:mockito-core:${versions.mockito}" |
| 35 | +} |
| 36 | + |
| 37 | +// Task to build the Rust JNI library |
| 38 | +task buildRustLibrary(type: Exec) { |
| 39 | + description = 'Build the Rust JNI library using Cargo' |
| 40 | + group = 'build' |
| 41 | + |
| 42 | + workingDir file('jni') |
| 43 | + |
| 44 | + // Determine the target directory and library name based on OS |
| 45 | + def osName = System.getProperty('os.name').toLowerCase() |
| 46 | + def libPrefix = osName.contains('windows') ? '' : 'lib' |
| 47 | + def libExtension = osName.contains('windows') ? '.dll' : (osName.contains('mac') ? '.dylib' : '.so') |
| 48 | + |
| 49 | + // Use debug build for development, release for production |
| 50 | + def buildType = project.hasProperty('rustRelease') ? 'release' : 'debug' |
| 51 | + def targetDir = "target/${buildType}" |
| 52 | + |
| 53 | + def cargoArgs = ['cargo', 'build'] |
| 54 | + if (buildType == 'release') { |
| 55 | + cargoArgs.add('--release') |
| 56 | + } |
| 57 | + |
| 58 | + if (osName.contains('windows')) { |
| 59 | + commandLine cargoArgs |
| 60 | + } else { |
| 61 | + commandLine cargoArgs |
| 62 | + } |
| 63 | + |
| 64 | + // Set environment variables for cross-compilation if needed |
| 65 | + environment 'CARGO_TARGET_DIR', file('jni/target').absolutePath |
| 66 | + |
| 67 | + inputs.files fileTree('jni/src') |
| 68 | + inputs.file 'jni/Cargo.toml' |
| 69 | + outputs.files file("jni/${targetDir}/${libPrefix}opensearch_datafusion_jni${libExtension}") |
| 70 | + System.out.println("Building Rust library in ${buildType} mode"); |
| 71 | +} |
| 72 | + |
| 73 | +// Task to copy the native library to resources |
| 74 | +task copyNativeLibrary(type: Copy, dependsOn: buildRustLibrary) { |
| 75 | + description = 'Copy the native library to Java resources' |
| 76 | + group = 'build' |
| 77 | + |
| 78 | + def osName = System.getProperty('os.name').toLowerCase() |
| 79 | + def libPrefix = osName.contains('windows') ? '' : 'lib' |
| 80 | + def libExtension = osName.contains('windows') ? '.dll' : (osName.contains('mac') ? '.dylib' : '.so') |
| 81 | + def buildType = project.hasProperty('rustRelease') ? 'release' : 'debug' |
| 82 | + |
| 83 | + from file("jni/target/${buildType}/${libPrefix}opensearch_datafusion_jni${libExtension}") |
| 84 | + into file('src/main/resources/native') |
| 85 | + |
| 86 | + // Rename to a standard name for Java to load |
| 87 | + rename { filename -> |
| 88 | + "libopensearch_datafusion_jni${libExtension}" |
| 89 | + } |
| 90 | +} |
| 91 | + |
| 92 | +// Ensure native library is built before Java compilation |
| 93 | +compileJava.dependsOn copyNativeLibrary |
| 94 | + |
| 95 | +// Ensure processResources depends on copyNativeLibrary |
| 96 | +processResources.dependsOn copyNativeLibrary |
| 97 | + |
| 98 | +// Clean task should also clean Rust artifacts |
| 99 | +clean { |
| 100 | + delete file('jni/target') |
| 101 | + delete file('src/main/resources/native') |
| 102 | +} |
| 103 | + |
| 104 | +test { |
| 105 | + // Set system property to help tests find the native library |
| 106 | + systemProperty 'java.library.path', file('src/main/resources/native').absolutePath |
| 107 | +} |
| 108 | + |
| 109 | +yamlRestTest { |
| 110 | + systemProperty 'tests.security.manager', 'false' |
| 111 | +} |
0 commit comments