Skip to content

Commit

Permalink
structured_sources: Allow build_tgt to be added as source
Browse files Browse the repository at this point in the history
In some cases, there is a need to use a built library as a "source" for
another target. Specifically Java JAR might want to contain their own
native JNI libraries in order to provide a single file "executable".
  • Loading branch information
sp1ritCS committed Jul 11, 2024
1 parent 8d92487 commit 2e87b14
Show file tree
Hide file tree
Showing 8 changed files with 38 additions and 15 deletions.
2 changes: 1 addition & 1 deletion mesonbuild/interpreter/interpreter.py
Original file line number Diff line number Diff line change
Expand Up @@ -2367,7 +2367,7 @@ def func_structured_sources(
self, node: mparser.BaseNode,
args: T.Tuple[object, T.Optional[T.Dict[str, object]]],
kwargs: 'TYPE_kwargs') -> build.StructuredSources:
valid_types = (str, mesonlib.File, build.GeneratedList, build.CustomTarget, build.CustomTargetIndex, build.GeneratedList)
valid_types = (str, mesonlib.File, build.GeneratedList, build.CustomTarget, build.CustomTargetIndex, build.GeneratedList, build.BuildTarget)
sources: T.Dict[str, T.List[T.Union[mesonlib.File, 'build.GeneratedTypes']]] = collections.defaultdict(list)

for arg in mesonlib.listify(args[0]):
Expand Down
4 changes: 0 additions & 4 deletions test cases/java/9 jni/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,14 @@ cc.has_header('jni.h', dependencies: [jni_dep], required: true)
# Assert that the platform-specific include directory is included in the compiler arguments.
cc.has_header('jni_md.h', dependencies: [jni_dep], required: true)

# generate native headers
subdir('src')
subdir('lib')

test(
'jnitest',
java,
args: [
'-Djava.library.path=@0@'.format(fs.parent(jnijava.full_path())),
'-jar',
jnijar,
],
protocol : 'exitcode',
depends : [jnijava],
)
36 changes: 32 additions & 4 deletions test cases/java/9 jni/src/com/mesonbuild/JniTest.java
Original file line number Diff line number Diff line change
@@ -1,15 +1,43 @@
package com.mesonbuild;

import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.IOException;

public final class JniTest {
private static String libname = "jnijava";
static {
String fullname = System.mapLibraryName(libname);
// this is somewhat hacky, but attempt to split off the extension
int ext = fullname.indexOf(".");
if (ext < 0)
ext = fullname.length();
try {
File fslib = File.createTempFile(fullname.substring(0, ext), fullname.substring(ext));
fslib.setReadable(true);
fslib.setWritable(true, true);
fslib.setExecutable(true);

InputStream istream = JniTest.class.getResourceAsStream("/" + fullname);
FileOutputStream ostream = new FileOutputStream(fslib);
istream.transferTo(ostream);
istream.close();
ostream.close();

System.load(fslib.getAbsolutePath());

fslib.deleteOnExit();
} catch (IOException e) {
throw new RuntimeException(e);
}
}

private static native int jni_test();

public static void main(String[] args) {
if (jni_test() != Configured.FINGERPRINT) {
throw new RuntimeException("jdk_test() did not return 0");
}
}

static {
System.loadLibrary("jnijava");
}
}
2 changes: 2 additions & 0 deletions test cases/java/9 jni/src/com/mesonbuild/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,5 @@ sources += configured
native_headers = javamod.native_headers(
sources, package: 'com.mesonbuild', classes: ['JniTest'])
native_header_includes = include_directories('.')

subdir('native')
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
sources = [
native_sources = [
files(
'native.c',
'com_mesonbuild_JniTest.c',
Expand All @@ -8,11 +8,7 @@ sources = [

jnijava = shared_module(
'jnijava',
sources,
native_sources,
dependencies : [jni_dep],
include_directories : [native_header_includes]
)

jnijava_dep = declare_dependency(
link_with : jnijava
)
File renamed without changes.
1 change: 1 addition & 0 deletions test cases/java/9 jni/src/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ jnijar = jar(
'jnijar',
sources,
main_class : 'com.mesonbuild.JniTest',
java_resources : structured_sources(jnijava)
)

0 comments on commit 2e87b14

Please sign in to comment.