You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The main concept behind the library is to provide easy access to native structures and functions of dynamic libraries written in different languages.
4
+
Java FFM API provides solid core objects and patterns to interact with native code, but is lacking of DevEx when working with real native code.
5
+
That said, the developer needs to write a lot of boilerplate code to make s simple native call and understand the whole native-java engine under the hood.
6
+
7
+
To solve this issue `native-memory-processor` consists of two major parts:
8
+
1)`annotation` project, which declares core annotations and some helpers to work with native code in runtime.
9
+
-`@NativeMemory` - main annotation to interface. Provide header files for parsing or leave empty if you need just native function generation.
10
+
-`@NativeMemoryOptions` - annotation to interface, can define optional advanced options to code generation.
11
+
-`@Structs` / `@Struct` - annotations to interface, defines the intent to parse structures.
12
+
-`@Enums` / `@Enum` - annotations to interface, defines the intent to parse enums.
13
+
-`@Unions` / `@Union` - annotations to interface, defines the intent to parse unions.
14
+
-`@NativeManualFunction` - annotation to method in interface, defines the native function to be generated.
15
+
-`@ByAddress` / `@Returns` - annotation to parameter of method in interface, defines parameter option to send as a pointer / value and native function return object.
16
+
17
+
2)`annotation-processor` project, which provides processing of annotation described above at compile time in few steps:
18
+
- indicate the main `@NativeMemory` annotation on interface
19
+
- get provided header files (if any) and generate structures/enums/unions as defined by corresponding annotations
20
+
- get annotated by `@NativeManualFunction` methods in interface and generate java-to-native code
21
+
22
+
In general, library tries to encapsulate the hardcore part of native interacting by code generation and provide user-specific classes with variety of options.
23
+
24
+
## Difference from jextract
25
+
26
+
The OpenJDK team had already created a similar tool, called `jextract` to keep developer hands clean from native code.
27
+
This is a standalone CLI tool, which works with `libclang` to parse provided header files and generate the Java FFM-ready code.
28
+
While it is great in concept, I found it 'not-so-friendly' for a person, who never worked with native code before.
29
+
Mainly, the interacting and generated code is hard to understand in different ways:
30
+
- structure/function names usually uses `_` and `$` signs or its combinations in generated methods
31
+
- no support of opaque structures
32
+
- it is HUGE! Simple `gpiochip_info` structure from `gpio.h` kernel UAPI of three fields transforms to 284 lines of code (sic!)
33
+
- jextract holds no context of what it is generating. Meaning you can get a header file for defining primitive types by kernel (e.g. `__kernel_sighandler_t` for above structure), which is usually do not needed in your code
34
+
- there is no simple way to understand the connections between generated classes, because of it's size and naming
35
+
- it is standalone binary, which is needed to be connected somehow to your gradle/maven build toolchain
36
+
37
+
I tried to get all advantages of `jextract` and create more user-friendly version of it.
38
+
Still both `native-memory-processor` and `jextract` uses the same parsing library `libclang` and the quality of parsers are the same, but the process is very different:
39
+
- annotation processing by gradle/maven instead of standalone binary
40
+
- very specific control over the objects you are parsing with context based approach
This code snippet will generate all structures and enums within the header file `gpio.h` (located in `resources` folder), as well as `GPIONative` class with call implementation of `ioctl` native function.
58
-
Find more examples in [documentation](USAGE.md) or in my other project https://github.com/digitalsmile/gpio
61
+
62
+
Observe concepts in [doucmentation](CONCEPTS.md) or find more examples in [getting started](USAGE.md). You can also look at [tests](annotation-processor-test/src/test/java/io/github/digitalsmile/gpio) or in my other project https://github.com/digitalsmile/gpio
59
63
60
64
3) Enjoy! :)
61
65
62
66
## Plans
63
67
64
68
- more support of native C/C++ types and patterns
65
-
- validation of structures parameters and custom validation support
66
-
- adding more context on different header files, especially connected with each other
67
-
69
+
- validation of structures parameters and custom validation support
The main concept behind the library is to provide easy access to native structures and functions of dynamic libraries written in different languages.
6
-
Java FFM API provides solid core objects and patterns to interact with native code, but is lacking of DevEx when working with real native code.
7
-
That said, the developer needs to write a lot of boilerplate code to make s simple native call and understand the whole native-java engine under the hood.
8
-
9
-
To solve this issue `native-memory-processor` consists of two major parts:
10
-
1)`annotation` project, which declares core annotations and some helpers to work with native code in runtime.
11
-
-`@NativeMemory` - main annotation to interface. Provide header files for parsing or leave empty if you need just native function generation.
12
-
-`@NativeMemoryOptions` - annotation to interface, can define optional advanced options to code generation.
13
-
-`@Structs` / `@Struct` - annotations to interface, defines the intent to parse structures.
14
-
-`@Enums` / `@Enum` - annotations to interface, defines the intent to parse enums.
15
-
-`@Unions` / `@Union` - annotations to interface, defines the intent to parse unions.
16
-
-`@NativeFunction` - annotation to method in interface, defines the native function to be generated.
17
-
-`@ByAddress` / `@Returns` - annotation to parameter of method in interface, defines parameter option to send as a pointer / value and native function return object.
18
-
19
-
2)`annotation-processor` project, which provides processing of annotation described above at compile time in few steps:
20
-
- indicate the main `@NativeMemory` annotation on interface
21
-
- get provided header files (if any) and generate structures/enums/unions as defined by corresponding annotations
22
-
- get annotated by `@NativeFunction` methods in interface and generate java-to-native code
23
-
24
-
In general, library tries to encapsulate the hardcore part of native interacting by code generation and provide user-specific classes with variety of options.
25
-
26
-
## Difference from jextract
27
-
28
-
The OpenJDK team had already created a similar tool, called `jextract` to keep developer hands clean from native code.
29
-
This is a standalone CLI tool, which works with `libclang` to parse provided header files and generate the Java FFM-ready code.
30
-
While it is great in concept, I found it 'not-so-friendly' for a person, who never worked with native code before.
31
-
Mainly, the interacting and generated code is hard to understand in different ways:
32
-
- structure/function names usually uses `_` and `$` signs or its combinations in generated methods
33
-
- it is HUGE! Simple `gpiochip_info` structure from `gpio.h` kernel UAPI of three fields transforms to 284 lines of code (sic!)
34
-
- jextract holds no context of what it is generating. Meaning you can get a header file for defining primitive types by kernel (e.g. `__kernel_sighandler_t` for above structure), which is usually do not needed in your code
35
-
- there is no simple way to understand the connections between generated classes, because of it's size and naming
36
-
- it is standalone binary, which is needed to be connected somehow to your gradle/maven build toolchain
37
-
38
-
I tried to get all advantages of `jextract` and create more user-friendly version of it.
39
-
Still both `native-memory-processor` and `jextract` uses the same parsing library `libclang` and the quality of parsers are the same, but the process is very different:
40
-
- annotation processing by gradle/maven instead of standalone binary
41
-
- very specific control over the objects you are parsing with context based approach
42
-
- more helpers and code validation
3
+
## Quick Start
4
+
5
+
1) Locate your library header files. Usually, it is a single header file, which interconnects all needed structures/classes. E.g.
0 commit comments