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

Refactor native source sets to enable fork/exec targets #33

Merged
merged 1 commit into from
Feb 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions library/process/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,20 @@ kmpConfiguration {
findByName("jvmTest")?.apply { dependsOn(nonJsTest) }
findByName("nativeTest")?.apply { dependsOn(nonJsTest) }
}

val linuxMain = findByName("linuxMain")
val macosMain = findByName("macosMain")
if (linuxMain != null || macosMain != null) {
val forkExecMain = maybeCreate("forkExecMain")
forkExecMain.dependsOn(getByName("nativeMain"))
linuxMain?.apply { dependsOn(forkExecMain) }
macosMain?.apply { dependsOn(forkExecMain) }

val forkExecTest = maybeCreate("forkExecTest")
forkExecTest.dependsOn(getByName("nativeTest"))
findByName("linuxTest")?.apply { dependsOn(forkExecTest) }
findByName("macosTest")?.apply { dependsOn(forkExecTest) }
}
}
targets.filterIsInstance<KotlinNativeTarget>().spawnCInterop()
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
* Copyright (c) 2024 Matthew Nelson
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
**/
package io.matthewnelson.kmp.process.internal

import io.matthewnelson.kmp.file.IOException
import io.matthewnelson.kmp.process.Signal
import io.matthewnelson.kmp.process.Stdio
import kotlinx.cinterop.ExperimentalForeignApi
import kotlinx.cinterop.MemScope

@OptIn(ExperimentalForeignApi::class)
@Throws(IOException::class, UnsupportedOperationException::class)
internal actual fun MemScope.forkExec(
command: String,
args: List<String>,
env: Map<String, String>,
stdio: Stdio.Config,
destroy: Signal,
): NativeProcess {
throw IOException("Not yet implemented")
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
* Copyright (c) 2024 Matthew Nelson
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
**/
package io.matthewnelson.kmp.process.internal

import io.matthewnelson.kmp.file.IOException
import io.matthewnelson.kmp.process.Signal
import io.matthewnelson.kmp.process.Stdio
import kotlinx.cinterop.ExperimentalForeignApi
import kotlinx.cinterop.MemScope

@OptIn(ExperimentalForeignApi::class)
@Throws(IOException::class, UnsupportedOperationException::class)
internal actual fun MemScope.forkExec(
command: String,
args: List<String>,
env: Map<String, String>,
stdio: Stdio.Config,
destroy: Signal,
): NativeProcess {
throw UnsupportedOperationException("Fork & Exec is not supported on iOS")
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,36 @@ import io.matthewnelson.kmp.file.DelicateFileApi
import io.matthewnelson.kmp.file.IOException
import io.matthewnelson.kmp.file.InterruptedException
import io.matthewnelson.kmp.file.errnoToIOException
import io.matthewnelson.kmp.process.Signal
import io.matthewnelson.kmp.process.Stdio
import kotlinx.cinterop.ExperimentalForeignApi
import kotlinx.cinterop.MemScope
import platform.posix.errno
import platform.posix.usleep
import kotlin.contracts.ExperimentalContracts
import kotlin.contracts.InvocationKind
import kotlin.contracts.contract
import kotlin.time.Duration

@Suppress("NOTHING_TO_INLINE")
internal expect inline fun PlatformBuilder.parentEnvironment(): MutableMap<String, String>
@OptIn(ExperimentalForeignApi::class)
@Throws(IOException::class, UnsupportedOperationException::class)
internal expect fun MemScope.posixSpawn(
command: String,
args: List<String>,
env: Map<String, String>,
stdio: Stdio.Config,
destroy: Signal,
): NativeProcess

@OptIn(ExperimentalForeignApi::class)
@Throws(IOException::class, UnsupportedOperationException::class)
internal expect fun MemScope.forkExec(
command: String,
args: List<String>,
env: Map<String, String>,
stdio: Stdio.Config,
destroy: Signal,
): NativeProcess

@Suppress("NOTHING_TO_INLINE")
@Throws(InterruptedException::class)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/*
* Copyright (c) 2024 Matthew Nelson
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
**/
@file:Suppress("EXPECT_ACTUAL_CLASSIFIERS_ARE_IN_BETA_WARNING", "KotlinRedundantDiagnosticSuppress")

package io.matthewnelson.kmp.process.internal

import io.matthewnelson.kmp.file.IOException
import io.matthewnelson.kmp.file.wrapIOException
import io.matthewnelson.kmp.process.Output
import io.matthewnelson.kmp.process.Process
import io.matthewnelson.kmp.process.Signal
import io.matthewnelson.kmp.process.Stdio
import kotlinx.cinterop.ExperimentalForeignApi
import kotlinx.cinterop.memScoped

@Suppress("NOTHING_TO_INLINE")
internal expect inline fun PlatformBuilder.parentEnvironment(): MutableMap<String, String>

// nativeMain
internal actual class PlatformBuilder actual constructor() {

internal actual val env: MutableMap<String, String> by lazy {
parentEnvironment()
}

@Throws(IOException::class)
internal actual fun output(
command: String,
args: List<String>,
env: Map<String, String>,
stdio: Stdio.Config,
options: Output.Options,
destroy: Signal,
): Output = blockingOutput(command, args, env, stdio, options, destroy)

@Throws(IOException::class)
@OptIn(ExperimentalForeignApi::class)
internal actual fun spawn(
command: String,
args: List<String>,
env: Map<String, String>,
stdio: Stdio.Config,
destroy: Signal,
): Process {
try {
val p: NativeProcess = memScoped {
posixSpawn(command, args, env, stdio, destroy)
}
return p
} catch (_: UnsupportedOperationException) {
/* ignore and try fork/exec */
}

try {
val p: NativeProcess = memScoped {
forkExec(command, args, env, stdio, destroy)
}

return p
} catch (e: UnsupportedOperationException) {
throw e.wrapIOException { "Neither posix_spawn or fork/exec were supported" }
}
}
}

This file was deleted.

Loading
Loading