-
Notifications
You must be signed in to change notification settings - Fork 51
C interop build integration support #29
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
Open
leodouglas
wants to merge
1
commit into
JetBrains:main
Choose a base branch
from
leodouglas:feature/cinterop-support
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| product: | ||
| type: linux/app | ||
| platforms: | ||
| - linuxX64 | ||
|
|
||
| settings: | ||
| kotlin: | ||
| serialization: json | ||
| native: | ||
| entryPoint: org.jetbrains.amper.ktor.main | ||
|
|
||
| dependencies: | ||
| - io.ktor:ktor-server-core:3.2.3 | ||
| - io.ktor:ktor-server-cio:3.2.3 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| /* | ||
| * Copyright 2000-2025 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license. | ||
| */ | ||
|
|
||
| package org.jetbrains.amper.ktor | ||
|
|
||
| import io.ktor.server.application.* | ||
| import io.ktor.server.engine.* | ||
| import io.ktor.server.cio.* | ||
| import io.ktor.server.response.* | ||
| import io.ktor.server.routing.* | ||
|
|
||
| fun main() { | ||
| embeddedServer(CIO, port = 8080, host = "0.0.0.0", module = Application::module) | ||
| .start(wait = true) | ||
| } | ||
|
|
||
| fun Application.module() { | ||
| configureRouting() | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| /* | ||
| * Copyright 2000-2025 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license. | ||
| */ | ||
|
|
||
| package org.jetbrains.amper.ktor | ||
|
|
||
| import io.ktor.server.application.* | ||
| import io.ktor.server.response.* | ||
| import io.ktor.server.routing.* | ||
|
|
||
| fun Application.configureRouting() { | ||
| routing { | ||
| get("/") { | ||
| call.respondText("Hello World!") | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| product: | ||
| type: linux/app | ||
| platforms: [ linuxX64 ] | ||
|
|
||
| settings: | ||
| native: | ||
| entryPoint: 'org.jetbrains.amper.samples.cinterop.main' |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| headers = src/c/hello.c |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| #include <stdio.h> | ||
|
|
||
| void sayHello(const char* name) { | ||
| printf("Hello. %s from C!\n", name); | ||
| } |
10 changes: 10 additions & 0 deletions
10
examples/native-cinterop/src/kotlin/org/jetbrains/amper/samples/cinterop/main.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| package org.jetbrains.amper.samples.cinterop | ||
|
|
||
| import kotlinx.cinterop.* | ||
| import hello.* | ||
|
|
||
| @OptIn(kotlinx.cinterop.ExperimentalForeignApi::class) | ||
| fun main() = memScoped { | ||
| println("Hello from Kotlin!") | ||
| sayHello("John Doe") | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
90 changes: 90 additions & 0 deletions
90
sources/amper-cli/src/org/jetbrains/amper/tasks/native/CinteropTask.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,90 @@ | ||
| /* | ||
| * Copyright 2000-2025 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license. | ||
| */ | ||
|
|
||
| package org.jetbrains.amper.tasks.native | ||
|
|
||
| import org.jetbrains.amper.cli.AmperProjectTempRoot | ||
| import org.jetbrains.amper.compilation.KotlinArtifactsDownloader | ||
| import org.jetbrains.amper.compilation.downloadNativeCompiler | ||
| import org.jetbrains.amper.compilation.serializableKotlinSettings | ||
| import org.jetbrains.amper.core.AmperUserCacheRoot | ||
| import org.jetbrains.amper.core.extract.cleanDirectory | ||
| import org.jetbrains.amper.engine.BuildTask | ||
| import org.jetbrains.amper.engine.TaskGraphExecutionContext | ||
| import org.jetbrains.amper.frontend.AmperModule | ||
| import org.jetbrains.amper.frontend.Platform | ||
| import org.jetbrains.amper.frontend.TaskName | ||
| import org.jetbrains.amper.frontend.isDescendantOf | ||
| import org.jetbrains.amper.incrementalcache.IncrementalCache | ||
| import org.jetbrains.amper.tasks.TaskOutputRoot | ||
| import org.jetbrains.amper.tasks.TaskResult | ||
| import org.jetbrains.amper.util.BuildType | ||
| import org.slf4j.LoggerFactory | ||
| import java.nio.file.Path | ||
|
|
||
| /** | ||
| * A task that runs the Kotlin/Native cinterop tool. | ||
| */ | ||
| internal class CinteropTask( | ||
| override val module: AmperModule, | ||
| override val platform: Platform, | ||
| private val userCacheRoot: AmperUserCacheRoot, | ||
| private val taskOutputRoot: TaskOutputRoot, | ||
| private val incrementalCache: IncrementalCache, | ||
| override val taskName: TaskName, | ||
| private val tempRoot: AmperProjectTempRoot, | ||
| override val isTest: Boolean, | ||
| override val buildType: BuildType, | ||
| private val defFile: Path, | ||
| private val kotlinArtifactsDownloader: KotlinArtifactsDownloader = | ||
| KotlinArtifactsDownloader(userCacheRoot, incrementalCache), | ||
| ) : BuildTask { | ||
| init { | ||
| require(platform.isLeaf) | ||
| require(platform.isDescendantOf(Platform.NATIVE)) | ||
| } | ||
|
|
||
| override suspend fun run(dependenciesResult: List<TaskResult>, executionContext: TaskGraphExecutionContext): TaskResult { | ||
| // For now, we assume a single fragment. This might need to be adjusted. | ||
| val fragment = module.fragments.first { it.platforms.contains(platform) && it.isTest == isTest } | ||
| val kotlinUserSettings = fragment.serializableKotlinSettings() | ||
|
|
||
| val configuration = mapOf( | ||
| "kotlin.version" to kotlinUserSettings.compilerVersion, | ||
| "def.file" to defFile.toString(), | ||
| ) | ||
| val inputs = listOf(defFile) | ||
|
|
||
| val artifact = incrementalCache.execute(taskName.name, configuration, inputs) { | ||
| cleanDirectory(taskOutputRoot.path) | ||
|
|
||
| val outputKLib = taskOutputRoot.path.resolve(defFile.toFile().nameWithoutExtension + ".klib") | ||
|
|
||
| val nativeCompiler = downloadNativeCompiler(kotlinUserSettings.compilerVersion, userCacheRoot) | ||
| val args = listOf( | ||
| "-def", defFile.toString(), | ||
| "-o", outputKLib.toString(), | ||
| "-compiler-option", "-I.", | ||
| "-target", platform.nameForCompiler, | ||
| ) | ||
|
|
||
| logger.info("Running cinterop for '${defFile.fileName}'...") | ||
| nativeCompiler.cinterop(args, module) | ||
|
|
||
| return@execute IncrementalCache.ExecutionResult(listOf(outputKLib)) | ||
| }.outputs.singleOrNull() | ||
|
|
||
| return Result( | ||
| compiledKlib = artifact, | ||
| taskName = taskName, | ||
| ) | ||
| } | ||
|
|
||
| class Result( | ||
| val compiledKlib: Path?, | ||
| val taskName: TaskName, | ||
| ) : TaskResult | ||
|
|
||
| private val logger = LoggerFactory.getLogger(javaClass) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
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.
I think we need some design here. Could you please describe the use cases for this approach? Why do we need to support explicit and implicit def files, respectively?
I'm not very fond of browsing the fs tree during model parsing in general (for perf reasons, because the auto-sync in the IDE should be super fast), so it's important to understand why we're doing this (cc @Jeffset, please feel free to jump in).
Also, from what I see in the interop docs, there seems to be more parameters that users should be able to customize when registering definition files. In Gradle it can look like this: