Skip to content

Commit bdf2b7c

Browse files
authored
[jnigen] Add basic YAML config support (#32)
1 parent aceff05 commit bdf2b7c

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

56 files changed

+1241
-545
lines changed

pkgs/jni/android/src/main/java/dev/dart/jni/JniPlugin.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
// Copyright (c) 2022, the Dart project authors. Please see the AUTHORS file
2+
// for details. All rights reserved. Use of this source code is governed by a
3+
// BSD-style license that can be found in the LICENSE file.
4+
15
package dev.dart.jni;
26

37
import android.app.Activity;

pkgs/jni/bin/setup.dart

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
// Copyright (c) 2022, the Dart project authors. Please see the AUTHORS file
2+
// for details. All rights reserved. Use of this source code is governed by a
3+
// BSD-style license that can be found in the LICENSE file.
4+
15
import 'dart:io';
26

37
import 'package:args/args.dart';

pkgs/jni/example/lib/main.dart

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
// Copyright (c) 2022, the Dart project authors. Please see the AUTHORS file
2+
// for details. All rights reserved. Use of this source code is governed by a
3+
// BSD-style license that can be found in the LICENSE file.
4+
15
// ignore_for_file: library_private_types_in_public_api
26

37
import 'package:flutter/material.dart';

pkgs/jni/example/test/widget_test.dart

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
// Copyright (c) 2022, the Dart project authors. Please see the AUTHORS file
2+
// for details. All rights reserved. Use of this source code is governed by a
3+
// BSD-style license that can be found in the LICENSE file.
4+
15
import 'dart:io';
26

37
import 'package:flutter/material.dart';

pkgs/jni/lib/jni_object.dart

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
// Copyright (c) 2022, the Dart project authors. Please see the AUTHORS file
2+
// for details. All rights reserved. Use of this source code is governed by a
3+
// BSD-style license that can be found in the LICENSE file.
4+
15
/// jni_object library provides an easier interface to JNI's object references,
26
/// providing various helper methods for one-off uses.
37
///

pkgs/jni/lib/src/direct_methods_generated.dart

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
// Copyright (c) 2022, the Dart project authors. Please see the AUTHORS file
2+
// for details. All rights reserved. Use of this source code is governed by a
3+
// BSD-style license that can be found in the LICENSE file.
4+
15
// Autogenerated; DO NOT EDIT
26
// Generated by running the script in tool/gen_aux_methods.dart
37
// coverage:ignore-file

pkgs/jni/lib/src/extensions.dart

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
// Copyright (c) 2022, the Dart project authors. Please see the AUTHORS file
2+
// for details. All rights reserved. Use of this source code is governed by a
3+
// BSD-style license that can be found in the LICENSE file.
4+
15
import 'dart:ffi';
26

37
import 'package:ffi/ffi.dart';

pkgs/jni/lib/src/jl_object.dart

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
// Copyright (c) 2022, the Dart project authors. Please see the AUTHORS file
2+
// for details. All rights reserved. Use of this source code is governed by a
3+
// BSD-style license that can be found in the LICENSE file.
4+
15
import 'dart:ffi';
26

37
import 'package:ffi/ffi.dart';

pkgs/jni/lib/src/jni.dart

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
// Copyright (c) 2022, the Dart project authors. Please see the AUTHORS file
2+
// for details. All rights reserved. Use of this source code is governed by a
3+
// BSD-style license that can be found in the LICENSE file.
4+
15
import 'dart:ffi';
26
import 'dart:io';
37

pkgs/jni/lib/src/jni_class.dart

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
// Copyright (c) 2022, the Dart project authors. Please see the AUTHORS file
2+
// for details. All rights reserved. Use of this source code is governed by a
3+
// BSD-style license that can be found in the LICENSE file.
4+
15
import 'dart:ffi';
26

37
import 'package:ffi/ffi.dart';

pkgs/jni/lib/src/jni_class_methods_generated.dart

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
// Copyright (c) 2022, the Dart project authors. Please see the AUTHORS file
2+
// for details. All rights reserved. Use of this source code is governed by a
3+
// BSD-style license that can be found in the LICENSE file.
4+
15
// Autogenerated; DO NOT EDIT
26
// Generated by running the script in tool/gen_aux_methods.dart
37
// coverage:ignore-file

pkgs/jni/lib/src/jni_exceptions.dart

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
// Copyright (c) 2022, the Dart project authors. Please see the AUTHORS file
2+
// for details. All rights reserved. Use of this source code is governed by a
3+
// BSD-style license that can be found in the LICENSE file.
4+
15
import 'dart:ffi';
26

37
import 'third_party/jni_bindings_generated.dart';

pkgs/jni/lib/src/jni_object.dart

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
// Copyright (c) 2022, the Dart project authors. Please see the AUTHORS file
2+
// for details. All rights reserved. Use of this source code is governed by a
3+
// BSD-style license that can be found in the LICENSE file.
4+
15
import 'dart:ffi';
26

37
import 'package:ffi/ffi.dart';

pkgs/jni/lib/src/jni_object_methods_generated.dart

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
// Copyright (c) 2022, the Dart project authors. Please see the AUTHORS file
2+
// for details. All rights reserved. Use of this source code is governed by a
3+
// BSD-style license that can be found in the LICENSE file.
4+
15
// Autogenerated; DO NOT EDIT
26
// Generated by running the script in tool/gen_aux_methods.dart
37
// coverage:ignore-file

pkgs/jni/lib/src/jvalues.dart

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
// Copyright (c) 2022, the Dart project authors. Please see the AUTHORS file
2+
// for details. All rights reserved. Use of this source code is governed by a
3+
// BSD-style license that can be found in the LICENSE file.
4+
15
import 'dart:ffi';
26
import 'package:ffi/ffi.dart';
37

pkgs/jni/src/dartjni.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
// Copyright (c) 2022, the Dart project authors. Please see the AUTHORS file
2+
// for details. All rights reserved. Use of this source code is governed by a
3+
// BSD-style license that can be found in the LICENSE file.
4+
15
#include <jni.h>
26
#include <stdint.h>
37

pkgs/jni/src/dartjni.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
// Copyright (c) 2022, the Dart project authors. Please see the AUTHORS file
2+
// for details. All rights reserved. Use of this source code is governed by a
3+
// BSD-style license that can be found in the LICENSE file.
4+
15
#include <jni.h>
26
#include <stdint.h>
37
#include <stdio.h>

pkgs/jni/test/exception_test.dart

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
// Copyright (c) 2022, the Dart project authors. Please see the AUTHORS file
2+
// for details. All rights reserved. Use of this source code is governed by a
3+
// BSD-style license that can be found in the LICENSE file.
4+
15
import 'dart:io';
26

37
import 'package:test/test.dart';

pkgs/jni/test/jni_object_test.dart

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
// Copyright (c) 2022, the Dart project authors. Please see the AUTHORS file
2+
// for details. All rights reserved. Use of this source code is governed by a
3+
// BSD-style license that can be found in the LICENSE file.
4+
15
import 'dart:io';
26
import 'dart:ffi';
37
import 'dart:isolate';

pkgs/jni/test/jni_test.dart

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
// Copyright (c) 2022, the Dart project authors. Please see the AUTHORS file
2+
// for details. All rights reserved. Use of this source code is governed by a
3+
// BSD-style license that can be found in the LICENSE file.
4+
15
import 'dart:io';
26
import 'dart:ffi';
37

pkgs/jni/tool/gen_aux_methods.dart

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,16 @@
1-
/// Run from templates directory
1+
// Copyright (c) 2022, the Dart project authors. Please see the AUTHORS file
2+
// for details. All rights reserved. Use of this source code is governed by a
3+
// BSD-style license that can be found in the LICENSE file.
24

35
import 'dart:io' as io;
46
import 'package:path/path.dart';
57

8+
const _license = '''
9+
// Copyright (c) 2022, the Dart project authors. Please see the AUTHORS file
10+
// for details. All rights reserved. Use of this source code is governed by a
11+
// BSD-style license that can be found in the LICENSE file.
12+
13+
''';
614
final targetTypes = {
715
"String": "String",
816
"Object": "JniObject",
@@ -53,6 +61,7 @@ void main(List<String> args) {
5361
sInvoke: join(outputDir, "direct_methods_generated.dart")
5462
};
5563
for (final s in [sInst, sStatic, sInvoke]) {
64+
s.write(_license);
5665
s.write("// Autogenerated; DO NOT EDIT\n"
5766
"// Generated by running the script in tool/gen_aux_methods.dart\n");
5867
s.write("// coverage:ignore-file\n");

pkgs/jni_gen/README.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,27 @@ This enables calling Java code from Dart.
55

66
This is a GSoC 2022 project.
77

8+
Currently this package is highly experimental and proof-of-concept. See `test/jackson_core_test` for an example of generating bindings for a library. It is possible to specify some dependencies to be downloaded automatically through `maven`.
9+
10+
## Basics
11+
### Running `jni_gen`
12+
There are 2 ways to use `jni_gen`:
13+
14+
* Import `package:jni_gen/jni_gen.dart` from a script in `tool/` directory of your project.
15+
* Run as command line tool with a YAML config.
16+
17+
Both approaches are almost identical. If using YAML, it's possible to selectively override configuration properties with command line, using `-Dproperty.name=value` syntax.
18+
19+
### Generated bindings
20+
Generated bindings will consist of 2 parts - C bindings which call JNI, and Dart bindings which call C bindings. The generated bindings will depend on `package:jni` for instantiating / obtaining a JVM instance.
21+
22+
The following properties must be specified in yaml.
23+
24+
* `c_root`: root folder to write generated C bindings.
25+
* `dart_root`: root folder to write generated Dart bindings (see below).
26+
* `library_name`: specifies name of the generated library in CMakeFiles.txt.
27+
28+
The generated C file has to be linked to JNI libraries. Therefore a CMake configuration is always generated which builds the generated code as shared library. The `init.dart` in generated dart code loads the library on first time a method is accessed. On dart standalone, it will be loaded from the same directory specified in `Jni.spawn` call.
29+
30+
## Examples
31+
See [jackson_core_test](test/jackson_core_test) folder for an example how bindings are generated. Runnable examples will be added soon.

pkgs/jni_gen/bin/jni_gen.dart

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// Copyright (c) 2022, the Dart project authors. Please see the AUTHORS file
2+
// for details. All rights reserved. Use of this source code is governed by a
3+
// BSD-style license that can be found in the LICENSE file.
4+
5+
import 'package:jni_gen/jni_gen.dart';
6+
7+
void main(List<String> args) async {
8+
final config = Config.parseArgs(args);
9+
await generateJniBindings(config);
10+
}

pkgs/jni_gen/bin/setup.dart

Lines changed: 4 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -2,67 +2,20 @@
22
// for details. All rights reserved. Use of this source code is governed by a
33
// BSD-style license that can be found in the LICENSE file.
44

5-
// This script gets the java sources using the copy of this package, and builds
6-
// ApiSummarizer jar using Maven.
75
import 'dart:io';
86

9-
import 'package:path/path.dart';
10-
11-
import 'package:jni_gen/src/util/find_package.dart';
12-
13-
final toolPath = join('.', '.dart_tool', 'jni_gen');
14-
final mvnTargetDir = join(toolPath, 'target');
15-
final jarFile = join(toolPath, 'ApiSummarizer.jar');
16-
final targetJarFile = join(mvnTargetDir, 'ApiSummarizer.jar');
17-
18-
Future<void> buildApiSummarizer() async {
19-
final pkg = await findPackageRoot('jni_gen');
20-
if (pkg == null) {
21-
stderr.writeln('package jni_gen not found!');
22-
exitCode = 2;
23-
return;
24-
}
25-
final pom = pkg.resolve('java/pom.xml');
26-
await Directory(toolPath).create(recursive: true);
27-
final mvnProc = await Process.start(
28-
'mvn',
29-
[
30-
'--batch-mode',
31-
'--update-snapshots',
32-
'-f',
33-
pom.toFilePath(),
34-
'assembly:assembly'
35-
],
36-
workingDirectory: toolPath,
37-
mode: ProcessStartMode.inheritStdio);
38-
await mvnProc.exitCode;
39-
// move ApiSummarizer.jar from target to current directory
40-
File(targetJarFile).renameSync(jarFile);
41-
Directory(mvnTargetDir).deleteSync(recursive: true);
42-
}
7+
import 'package:jni_gen/src/tools/tools.dart';
438

449
void main(List<String> args) async {
4510
bool force = false;
4611
if (args.isNotEmpty) {
4712
if (args.length != 1 || args[0] != '-f') {
4813
stderr.writeln('usage: dart run jni_gen:setup [-f]');
49-
stderr.writeln('use -f option to rebuild ApiSummarizer jar '
50-
'even if it already exists.');
14+
stderr.writeln('* -f\trebuild ApiSummarizer jar even if it already '
15+
'exists.');
5116
} else {
5217
force = true;
5318
}
5419
}
55-
final jarExists = await File(jarFile).exists();
56-
final isJarStale = jarExists &&
57-
await isPackageModifiedAfter(
58-
'jni_gen', await File(jarFile).lastModified(), 'java/');
59-
if (isJarStale) {
60-
stderr.writeln('Rebuilding ApiSummarizer component since sources '
61-
'have changed. This might take some time.');
62-
}
63-
if (!jarExists || isJarStale || force) {
64-
await buildApiSummarizer();
65-
} else {
66-
stderr.writeln('ApiSummarizer.jar exists. Skipping build..');
67-
}
20+
buildSummarizerIfNotExists(force: force);
6821
}

pkgs/jni_gen/lib/jni_gen.dart

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,5 @@ library jni_gen;
99

1010
export 'src/elements/elements.dart';
1111
export 'src/config/config.dart';
12-
export 'src/writers/writers.dart';
12+
export 'src/config/filters.dart';
13+
export 'src/generate_bindings.dart';

pkgs/jni_gen/lib/src/bindings/c_bindings.dart

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
// BSD-style license that can be found in the LICENSE file.
44

55
import 'package:jni_gen/src/elements/elements.dart';
6-
import 'package:jni_gen/src/config/wrapper_options.dart';
6+
import 'package:jni_gen/src/config/config.dart';
77

88
import 'common.dart';
99

@@ -28,8 +28,8 @@ class CBindingGenerator {
2828
String _cParamRename(String paramName) =>
2929
_cTypeKeywords.contains(paramName) ? '${paramName}0' : paramName;
3030

31-
CBindingGenerator(this.options);
32-
WrapperOptions options;
31+
CBindingGenerator(this.config);
32+
Config config;
3333

3434
String generateBinding(ClassDecl c) {
3535
return _class(c);

pkgs/jni_gen/lib/src/bindings/dart_bindings.dart

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import 'dart:io';
66

77
import 'package:jni_gen/src/elements/elements.dart';
8-
import 'package:jni_gen/src/config/wrapper_options.dart';
8+
import 'package:jni_gen/src/config/config.dart';
99
import 'package:jni_gen/src/util/rename_conflict.dart';
1010

1111
import 'symbol_resolver.dart';
@@ -29,8 +29,8 @@ class DartBindingsGenerator {
2929

3030
static const String _jlObject = '${jni}JlObject';
3131

32-
DartBindingsGenerator(this.options, this.resolver);
33-
WrapperOptions options;
32+
DartBindingsGenerator(this.config, this.resolver);
33+
Config config;
3434
SymbolResolver resolver;
3535

3636
String generateBinding(ClassDecl decl) {
@@ -176,7 +176,7 @@ class DartBindingsGenerator {
176176

177177
void writeAccessor({bool isSetter = false}) {
178178
final symPrefix = isSetter ? 'set' : 'get';
179-
final sym = '_$symPrefix$name';
179+
final sym = '_${symPrefix}_$name';
180180
final ffiSig = dartSigForField(f, isSetter: isSetter, isFfiSig: true);
181181
final dartSig = dartSigForField(f, isSetter: isSetter, isFfiSig: false);
182182
s.write('${_indent}static final $sym = $_jlookup'
@@ -300,6 +300,7 @@ class DartBindingsGenerator {
300300

301301
String _literal(dynamic value) {
302302
if (value is String) {
303+
// TODO(#31): escape string literal.
303304
return '"$value"';
304305
}
305306
if (value is int || value is double || value is bool) {

0 commit comments

Comments
 (0)