Skip to content
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

Merged
merged 8 commits into from
Oct 8, 2024
Merged

feat: deffile options #4405

merged 8 commits into from
Oct 8, 2024

Conversation

danil-pavlov
Copy link
Contributor

No description provided.

@danil-pavlov danil-pavlov requested a review from a team as a code owner August 28, 2024 15:34

1. Create a `.def` file describing what to include into bindings. For Objective-C, add the following property to the top:

```none
Copy link
Contributor

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:
Copy link
Contributor

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.

Copy link
Contributor Author

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.
Copy link
Contributor

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.
Copy link
Contributor

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
Copy link
Contributor

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.

Copy link
Contributor

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
Copy link
Contributor

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` | |
Copy link
Contributor

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` | |
Copy link
Contributor

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` | |
Copy link
Contributor

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` | |
Copy link
Contributor

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` | |
Copy link
Contributor

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

Copy link
Collaborator

@sarahhaggarty sarahhaggarty left a 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 :)

Copy link
Collaborator

@sarahhaggarty sarahhaggarty left a 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**.
Copy link
Collaborator

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?

Copy link
Contributor Author

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:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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:

@danil-pavlov danil-pavlov merged commit f8361d5 into master Oct 8, 2024
4 checks passed
@danil-pavlov danil-pavlov deleted the deffile-page branch October 8, 2024 14:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants