KT-63264: Add a compiler flag that disables C interface generation

We can reuse existing static/dynamic library output kinds for
the Swift Export. The only change we have to make is to make generation
of C interface opt-out by moving it under the binary option.
As a side effect, it allows evolving C Export in the future by
introducing new option variants.
This commit is contained in:
Sergey Bogolepov
2023-11-25 10:18:26 +01:00
committed by Space Team
parent c499095382
commit 8be547b834
7 changed files with 49 additions and 4 deletions
@@ -72,6 +72,8 @@ object BinaryOptions : BinaryOptionRegistry() {
val enableSafepointSignposts by booleanOption()
val packFields by booleanOption()
val cInterfaceMode by option<CInterfaceGenerationMode>()
}
open class BinaryOption<T : Any>(
@@ -0,0 +1,23 @@
/*
* Copyright 2010-2023 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
*/
package org.jetbrains.kotlin.backend.konan
/**
* Configure the way C export works for dynamic/static libraries.
*/
enum class CInterfaceGenerationMode {
/**
* Do not generate any C interface.
*/
NONE,
/**
* Generate C header file the way it is described in
* https://kotlinlang.org/docs/native-dynamic-libraries.html.
*
*/
V1
}
@@ -30,6 +30,12 @@ val KonanConfig.isFinalBinary: Boolean get() = when (this.produce) {
val CompilerOutputKind.isNativeLibrary: Boolean
get() = this == CompilerOutputKind.DYNAMIC || this == CompilerOutputKind.STATIC
/**
* Return true if compiler has to generate a C API for dynamic/static library.
*/
val KonanConfig.produceCInterface: Boolean
get() = this.produce.isNativeLibrary && this.cInterfaceGenerationMode != CInterfaceGenerationMode.NONE
val CompilerOutputKind.involvesBitcodeGeneration: Boolean
get() = this != CompilerOutputKind.LIBRARY
@@ -608,6 +608,15 @@ class KonanConfig(val project: Project, val configuration: CompilerConfiguration
java.io.File(path)
}
}
internal val cInterfaceGenerationMode: CInterfaceGenerationMode by lazy {
val explicitMode = configuration.get(BinaryOptions.cInterfaceMode)
when {
explicitMode != null -> explicitMode
produce == CompilerOutputKind.DYNAMIC || produce == CompilerOutputKind.STATIC -> CInterfaceGenerationMode.V1
else -> CInterfaceGenerationMode.NONE
}
}
}
fun CompilerConfiguration.report(priority: CompilerMessageSeverity, message: String)
@@ -78,8 +78,13 @@ internal class DynamicCompilerDriver : CompilerDriver() {
private fun produceCLibrary(engine: PhaseEngine<PhaseContext>, config: KonanConfig, environment: KotlinCoreEnvironment) {
val frontendOutput = engine.runFrontend(config, environment) ?: return
val (psiToIrOutput, cAdapterElements) = engine.runPsiToIr(frontendOutput, isProducingLibrary = false) {
it.runPhase(BuildCExports, frontendOutput)
if (config.cInterfaceGenerationMode == CInterfaceGenerationMode.V1) {
it.runPhase(BuildCExports, frontendOutput)
} else {
null
}
}
require(psiToIrOutput is PsiToIrOutput.ForBackend)
val backendContext = createBackendContext(config, frontendOutput, psiToIrOutput) {
@@ -93,7 +93,7 @@ internal fun <C : PhaseContext> PhaseEngine<C>.runBackend(backendContext: Contex
try {
backendEngine.useContext(generationState) { generationStateEngine ->
val bitcodeFile = tempFiles.create(generationState.llvmModuleName, ".bc").javaFile()
val cExportFiles = if (config.produce.isNativeLibrary) {
val cExportFiles = if (config.produceCInterface) {
CExportFiles(
cppAdapter = tempFiles.create("api", ".cpp").javaFile(),
bitcodeAdapter = tempFiles.create("api", ".bc").javaFile(),
@@ -317,7 +317,7 @@ internal fun PhaseEngine<NativeGenerationState>.lowerModuleWithDependencies(modu
internal fun PhaseEngine<NativeGenerationState>.runBackendCodegen(module: IrModuleFragment, cExportFiles: CExportFiles?) {
runCodegen(module)
val generatedBitcodeFiles = if (context.config.produce.isNativeLibrary) {
val generatedBitcodeFiles = if (context.config.produceCInterface) {
require(cExportFiles != null)
val input = CExportGenerateApiInput(
context.context.cAdapterExportedElements!!,
@@ -483,7 +483,7 @@ internal class CodeGeneratorVisitor(
overrideRuntimeGlobals()
appendLlvmUsed("llvm.used", llvm.usedFunctions.map { it.toConstPointer().llvm } + llvm.usedGlobals)
appendLlvmUsed("llvm.compiler.used", llvm.compilerUsedGlobals)
if (context.config.produce.isNativeLibrary) {
if (context.config.produceCInterface) {
context.cAdapterExportedElements?.let { appendCAdapters(it) }
}
}