-
Notifications
You must be signed in to change notification settings - Fork 4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: deffile options #4405
feat: deffile options #4405
Conversation
|
||
1. Create a `.def` file describing what to include into bindings. For Objective-C, add the following property to the top: | ||
|
||
```none |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe it should be a part of properties table?
(the `.osx` suffix) and Linux (the `.linux` suffix). Parameters without a suffix are also possible | ||
(for example, `linkerOpts=`) and applied to all platforms. | ||
|
||
5. Run the `cinterop` tool. You can specify properties as options to the `cinterop` call as well, for example: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It is not needed in case of Gradle / IDE. Manual invocation of cinterop
tool probably should be covered separately at the end of the article.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done, moved all mentions of manual cinterop calls to a new section at the bottom
language = Objective-С | ||
``` | ||
|
||
2. Use the `cinterop` tool to produce Kotlin bindings. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's a bit different in gradle: write a bunch of lines in build.gradle.kts
, press sync button, voila.
``` | ||
|
||
2. Use the `cinterop` tool to produce Kotlin bindings. | ||
3. Run the Kotlin/Native compiler on an application to produce the final executable. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe
3. Use generated bindings in your Kotlin code.
4. Run the Kotlin/Native compiler to produce the final binary.
package = png | ||
|
||
compilerOpts.linux = -I/usr/include -I/usr/include/x86_64-linux-gnu | ||
linkerOpts.osx = -L/opt/local/lib -L/usr/local/opt/png/lib -lcurl |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There are no compilerOpts
for osx which means that no declarations are going to be generated for macOS target.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also, -lcurl
should be replaced with -lpng
Note that the generated bindings are generally platform-specific, so if you are developing for | ||
multiple targets, the bindings need to be regenerated. | ||
|
||
After the generation of bindings, they can be used by the IDE as a proxy view of the |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Only if we run it "Gradle way", not manually via cinterop
.
| [`compilerOpts`](#compiler-and-linker-options) | Compiler options that the cinterop tool passes to the compiler. | | ||
| `excludeSystemLibs` | | | ||
| [`excludeDependentModules`](#filter-headers-by-module-maps) | Exclude headers from `module.modulemap` or `module.map` files. When used with `headerFilter`, they are applied as an intersection. | | ||
| `entryPoints` | | |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Guess could be dropped?
| `allowedOverloadsForCFunctions` | | | ||
| [`disableDesignatedInitializerChecks`](#allow-calling-a-non-designated-initializer) | Disable the compiler check that doesn't allow calling a non-designated initializer as a `super()` constructor. | | ||
| [`foreignExceptionMode`](#wrap-exceptions) | Wraps exceptions from Objective-C code into Kotlin exceptions with the `ForeignException` type. | | ||
| `pluginName` | | |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Guess could be dropped?
| [`strictEnums`](#configure-enums-generation) | A space-separated list of enums that should be generated as a [Kotlin enum](enum-classes.md). | | ||
| [`nonStrictEnums`](#configure-enums-generation) | A space-separated list of enums that should be generated as integral values. | | ||
| [`noStringConversion`](#set-up-string-conversion) | A space-separated lists of functions whose `const char*` parameters should not be auto-converted to Kotlin `String`s. | | ||
| `depends` | | |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Guess could be dropped? Used only for internal purposes, IIRC.
| [`nonStrictEnums`](#configure-enums-generation) | A space-separated list of enums that should be generated as integral values. | | ||
| [`noStringConversion`](#set-up-string-conversion) | A space-separated lists of functions whose `const char*` parameters should not be auto-converted to Kotlin `String`s. | | ||
| `depends` | | | ||
| `exportForwardDeclarations` | | |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
OMG, I don't remember the intended usage :(
| [`noStringConversion`](#set-up-string-conversion) | A space-separated lists of functions whose `const char*` parameters should not be auto-converted to Kotlin `String`s. | | ||
| `depends` | | | ||
| `exportForwardDeclarations` | | | ||
| `allowedOverloadsForCFunctions` | | |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
By default, C functions are considered unique by name, and between multiple with the same name only one is picked. However, user can disable this behavior by specifying these functions in allowedOverloadsForCFunctions
7e0170c
to
20b6a24
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks nice! I've got a few suggestions and I made some writerside corrections :)
Co-authored-by: Sarah Haggarty <[email protected]>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks great! Just a couple things I noticed for you to consider. Otherwise, LGTM!
Let's create a definition file and generate bindings for a C library: | ||
|
||
1. In your IDE, select the `src` folder and create a new directory with **File | New | Directory**. | ||
2. Name the new directory **nativeInterop/cinterop**. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Don't we usually write directory names with inline code formatting?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No, you're right
#### Exclude headers | ||
|
||
To exclude specific headers, use the `excludeFilter` property. It can be helpful to remove redundant or problematic | ||
headers and optimize compilation, as declarations from the specified headers are not included into the bindings: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
headers and optimize compilation, as declarations from the specified headers are not included into the bindings: | |
headers and optimize compilation, as declarations from the specified headers are not included in the bindings: |
No description provided.