[K/N] Move c++ adapter compilation to a separate phase
Besides purely architectural reasons or, rather, as a direct effect of those, it allows moving tempFiles usages to the driver.
This commit is contained in:
committed by
Space Team
parent
bb1feabb79
commit
1182903b20
+4
-12
@@ -5,7 +5,6 @@
|
||||
package org.jetbrains.kotlin.backend.konan
|
||||
|
||||
import llvm.*
|
||||
import org.jetbrains.kotlin.backend.konan.cexport.produceCAdapterBitcode
|
||||
import org.jetbrains.kotlin.backend.konan.driver.PhaseContext
|
||||
import org.jetbrains.kotlin.backend.konan.llvm.*
|
||||
import org.jetbrains.kotlin.backend.konan.llvm.objc.patchObjCRuntimeModule
|
||||
@@ -15,6 +14,7 @@ import org.jetbrains.kotlin.konan.target.CompilerOutputKind
|
||||
import org.jetbrains.kotlin.library.BaseKotlinLibrary
|
||||
import org.jetbrains.kotlin.library.uniqueName
|
||||
import org.jetbrains.kotlin.utils.addToStdlib.ifNotEmpty
|
||||
import java.io.File
|
||||
|
||||
/**
|
||||
* Supposed to be true for a single LLVM module within final binary.
|
||||
@@ -142,23 +142,15 @@ internal fun insertAliasToEntryPoint(context: PhaseContext, module: LLVMModuleRe
|
||||
LLVMAddAlias(module, LLVMTypeOf(entryPoint)!!, entryPoint, "main")
|
||||
}
|
||||
|
||||
internal fun linkBitcodeDependencies(generationState: NativeGenerationState) {
|
||||
internal fun linkBitcodeDependencies(generationState: NativeGenerationState,
|
||||
generatedBitcodeFiles: List<File>) {
|
||||
val config = generationState.config
|
||||
val tempFiles = generationState.tempFiles
|
||||
val produce = config.produce
|
||||
|
||||
val generatedBitcodeFiles =
|
||||
if (produce == CompilerOutputKind.DYNAMIC || produce == CompilerOutputKind.STATIC) {
|
||||
produceCAdapterBitcode(
|
||||
config.clang,
|
||||
tempFiles.cAdapterCppName,
|
||||
tempFiles.cAdapterBitcodeName)
|
||||
listOf(tempFiles.cAdapterBitcodeName)
|
||||
} else emptyList()
|
||||
if (produce == CompilerOutputKind.FRAMEWORK && config.produceStaticFramework) {
|
||||
embedAppleLinkerOptionsToBitcode(generationState.llvm, config)
|
||||
}
|
||||
linkAllDependencies(generationState, generatedBitcodeFiles)
|
||||
linkAllDependencies(generationState, generatedBitcodeFiles.map { it.absoluteFile.normalize().path })
|
||||
|
||||
}
|
||||
|
||||
|
||||
+11
-4
@@ -7,10 +7,17 @@ package org.jetbrains.kotlin.backend.konan.cexport
|
||||
|
||||
import org.jetbrains.kotlin.konan.exec.*
|
||||
import org.jetbrains.kotlin.konan.target.*
|
||||
import org.jetbrains.kotlin.konan.file.*
|
||||
import java.io.File
|
||||
|
||||
// Fourth phase of C export: compile runtime bindings to bitcode.
|
||||
fun produceCAdapterBitcode(clang: ClangArgs, cppFileName: String, bitcodeFileName: String) {
|
||||
val clangCommand = clang.clangCXX("-std=c++17", cppFileName, "-emit-llvm", "-c", "-o", bitcodeFileName)
|
||||
/**
|
||||
* Fourth phase of C export: compile runtime bindings to bitcode.
|
||||
*/
|
||||
fun produceCAdapterBitcode(clang: ClangArgs, cppFile: File, bitcodeFile: File) {
|
||||
val clangCommand = clang.clangCXX(
|
||||
"-std=c++17",
|
||||
cppFile.absoluteFile.normalize().path,
|
||||
"-emit-llvm", "-c",
|
||||
"-o", bitcodeFile.absoluteFile.normalize().path
|
||||
)
|
||||
Command(clangCommand).execute()
|
||||
}
|
||||
|
||||
+2
-2
@@ -102,11 +102,11 @@ internal val CStubsPhase = createSimpleNamedCompilerPhase<NativeGenerationState,
|
||||
op = { context, _ -> produceCStubs(context) }
|
||||
)
|
||||
|
||||
internal val LinkBitcodeDependenciesPhase = createSimpleNamedCompilerPhase<NativeGenerationState, Unit>(
|
||||
internal val LinkBitcodeDependenciesPhase = createSimpleNamedCompilerPhase<NativeGenerationState, List<File>>(
|
||||
name = "LinkBitcodeDependencies",
|
||||
description = "Link bitcode dependencies",
|
||||
postactions = getDefaultLlvmModuleActions(),
|
||||
op = { context, _ -> linkBitcodeDependencies(context) }
|
||||
op = { context, input -> linkBitcodeDependencies(context, input) }
|
||||
)
|
||||
|
||||
internal val VerifyBitcodePhase = createSimpleNamedCompilerPhase<PhaseContext, LLVMModuleRef>(
|
||||
|
||||
+13
@@ -5,6 +5,7 @@
|
||||
|
||||
package org.jetbrains.kotlin.backend.konan.driver.phases
|
||||
|
||||
import org.jetbrains.kotlin.backend.konan.cexport.*
|
||||
import org.jetbrains.kotlin.backend.konan.cexport.CAdapterApiExporter
|
||||
import org.jetbrains.kotlin.backend.konan.cexport.CAdapterExportedElements
|
||||
import org.jetbrains.kotlin.backend.konan.cexport.CAdapterGenerator
|
||||
@@ -39,4 +40,16 @@ internal val CExportGenerateApiPhase = createSimpleNamedCompilerPhase<PhaseConte
|
||||
cppAdapterFile = input.cppAdapterFile,
|
||||
target = context.config.target,
|
||||
).makeGlobalStruct()
|
||||
}
|
||||
|
||||
internal class CExportCompileAdapterInput(
|
||||
val cppAdapterFile: File,
|
||||
val bitcodeAdapterFile: File,
|
||||
)
|
||||
|
||||
internal val CExportCompileAdapterPhase = createSimpleNamedCompilerPhase<PhaseContext, CExportCompileAdapterInput>(
|
||||
name = "CExportCompileAdapter",
|
||||
description = "Compile C++ adapter to bitcode"
|
||||
) { context, input ->
|
||||
produceCAdapterBitcode(context.config.clang, input.cppAdapterFile, input.bitcodeAdapterFile)
|
||||
}
|
||||
+9
-3
@@ -174,14 +174,20 @@ internal fun PhaseEngine<NativeGenerationState>.runBackendCodegen(module: IrModu
|
||||
}
|
||||
mergeDependencies(module, dependenciesToCompile)
|
||||
runCodegen(module)
|
||||
if (context.config.produce.isNativeLibrary) {
|
||||
val generatedBitcodeFiles = if (context.config.produce.isNativeLibrary) {
|
||||
val cppAdapterFile = context.tempFiles.cAdapterCpp.javaFile()
|
||||
val bitcodeAdapterFile = context.tempFiles.cAdapterBitcode.javaFile()
|
||||
val input = CExportGenerateApiInput(
|
||||
context.context.cAdapterExportedElements!!,
|
||||
headerFile = context.outputFiles.cAdapterHeader.javaFile(),
|
||||
defFile = if (context.config.target.family == Family.MINGW) context.outputFiles.cAdapterDef.javaFile() else null,
|
||||
cppAdapterFile = context.tempFiles.cAdapterCpp.javaFile()
|
||||
cppAdapterFile = cppAdapterFile
|
||||
)
|
||||
runPhase(CExportGenerateApiPhase, input)
|
||||
runPhase(CExportCompileAdapterPhase, CExportCompileAdapterInput(cppAdapterFile, bitcodeAdapterFile))
|
||||
listOf(bitcodeAdapterFile)
|
||||
} else {
|
||||
emptyList()
|
||||
}
|
||||
runPhase(CStubsPhase)
|
||||
// TODO: Consider extracting llvmModule and friends from nativeGenerationState and pass them explicitly.
|
||||
@@ -194,7 +200,7 @@ internal fun PhaseEngine<NativeGenerationState>.runBackendCodegen(module: IrModu
|
||||
if (context.shouldPrintBitCode()) {
|
||||
runPhase(PrintBitcodePhase, llvmModule)
|
||||
}
|
||||
runPhase(LinkBitcodeDependenciesPhase)
|
||||
runPhase(LinkBitcodeDependenciesPhase, generatedBitcodeFiles)
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user