From a2516548e4f7bf7514c6e26c0ca3eb60faa05ef1 Mon Sep 17 00:00:00 2001 From: Johan Bay Date: Mon, 8 Jan 2024 16:03:10 +0100 Subject: [PATCH] [K/N] avoid overwriting header when output paths are equal ^KT-65442 --- .../jetbrains/kotlin/backend/konan/KonanConfig.kt | 2 +- .../backend/konan/driver/DynamicCompilerDriver.kt | 12 ++++++++++-- .../kotlin/backend/konan/driver/phases/WriteKlib.kt | 2 +- 3 files changed, 12 insertions(+), 4 deletions(-) diff --git a/kotlin-native/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/KonanConfig.kt b/kotlin-native/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/KonanConfig.kt index 572850069d4..bbaaf26aec3 100644 --- a/kotlin-native/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/KonanConfig.kt +++ b/kotlin-native/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/KonanConfig.kt @@ -288,7 +288,7 @@ class KonanConfig(val project: Project, val configuration: CompilerConfiguration internal val metadataKlib get() = configuration.getBoolean(CommonConfigurationKeys.METADATA_KLIB) - internal val headerKlibPath get() = configuration.get(KonanConfigKeys.HEADER_KLIB) + internal val headerKlibPath get() = configuration.get(KonanConfigKeys.HEADER_KLIB)?.removeSuffixIfPresent(".klib") internal val produceStaticFramework get() = configuration.getBoolean(KonanConfigKeys.STATIC_FRAMEWORK) diff --git a/kotlin-native/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/driver/DynamicCompilerDriver.kt b/kotlin-native/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/driver/DynamicCompilerDriver.kt index d0ff59502a5..0713c7c6727 100644 --- a/kotlin-native/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/driver/DynamicCompilerDriver.kt +++ b/kotlin-native/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/driver/DynamicCompilerDriver.kt @@ -119,6 +119,9 @@ internal class DynamicCompilerDriver : CompilerDriver() { if (!headerKlibPath.isNullOrEmpty()) { val headerKlib = engine.runFir2IrSerializer(FirSerializerInput(fir2IrOutput, produceHeaderKlib = true)) engine.writeKlib(headerKlib, headerKlibPath) + // Don't overwrite the header klib with the full klib and stop compilation here. + // By providing the same path for both regular output and header klib we can skip emitting the full klib. + if (File(config.outputPath).canonicalPath == File(headerKlibPath).canonicalPath) return null } engine.runK2SpecialBackendChecks(fir2IrOutput) @@ -137,9 +140,14 @@ internal class DynamicCompilerDriver : CompilerDriver() { } else { engine.runPsiToIr(frontendOutput, isProducingLibrary = true) as PsiToIrOutput.ForKlib } - if (!config.headerKlibPath.isNullOrEmpty()) { + + val headerKlibPath = config.headerKlibPath + if (!headerKlibPath.isNullOrEmpty()) { val headerKlib = engine.runSerializer(frontendOutput.moduleDescriptor, psiToIrOutput, produceHeaderKlib = true) - engine.writeKlib(headerKlib, config.headerKlibPath) + engine.writeKlib(headerKlib, headerKlibPath) + // Don't overwrite the header klib with the full klib and stop compilation here. + // By providing the same path for both regular output and header klib we can skip emitting the full klib. + if (File(config.outputPath).canonicalPath == File(headerKlibPath).canonicalPath) return null } return engine.runSerializer(frontendOutput.moduleDescriptor, psiToIrOutput) } diff --git a/kotlin-native/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/driver/phases/WriteKlib.kt b/kotlin-native/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/driver/phases/WriteKlib.kt index 9ed196963bd..f62f45a090e 100644 --- a/kotlin-native/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/driver/phases/WriteKlib.kt +++ b/kotlin-native/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/driver/phases/WriteKlib.kt @@ -26,7 +26,7 @@ internal val WriteKlibPhase = createSimpleNamedCompilerPhase val config = context.config val configuration = config.configuration - val outputFiles = OutputFiles(input.customOutputPath?.removeSuffixIfPresent(".klib") + val outputFiles = OutputFiles(input.customOutputPath ?: config.outputPath, config.target, config.produce) val nopack = configuration.getBoolean(KonanConfigKeys.NOPACK) val output = outputFiles.klibOutputFileName(!nopack)