Skip to content

Commit

Permalink
Fix a crash caused by SKIE generating invalid modulemap if no Swift c…
Browse files Browse the repository at this point in the history
…ode generated was generated.
  • Loading branch information
FilipDolnik committed Apr 30, 2024
1 parent a601413 commit 8584e39
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ sealed class GenerateModulemapFilePhase(private val generateSwiftModule: Boolean

appendLine("}")

if (generateSwiftModule) {
if (generateSwiftModule && framework.swiftHeader.exists()) {
appendLine()
appendLine("module ${framework.moduleName}.Swift {")
appendLine(" header \"${framework.swiftHeader.name}\"")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,15 @@ class CompileSwiftPhase(
private val outputFileMap = context.skieBuildDirectory.swiftCompiler.config.outputFileMap
private val objectFiles = skieBuildDirectory.swiftCompiler.objectFiles
private val moduleDirectory = skieBuildDirectory.swiftCompiler.module
private val targetTriple = swiftCompilerConfiguration.targetTriple

private val isLibraryEvolutionEnabled = SkieConfigurationFlag.Build_SwiftLibraryEvolution in rootConfiguration.enabledFlags
private val isParallelSwiftCompilationEnabled = SkieConfigurationFlag.Build_ParallelSwiftCompilation in rootConfiguration.enabledFlags
private val isConcurrentSkieCompilationEnabled = SkieConfigurationFlag.Build_ConcurrentSkieCompilation in rootConfiguration.enabledFlags

context(SirPhase.Context)
override suspend fun execute() {
val sourceFiles = sirProvider.skieModuleFiles.filterIsInstance<SirCompilableFile>().map { it.absolutePath.toFile() }
val sourceFiles = sirProvider.compilableFiles.map { it.absolutePath.toFile() }

if (sourceFiles.isEmpty()) {
return
}
Expand All @@ -42,10 +42,6 @@ class CompileSwiftPhase(
callSwiftCompiler()

deleteOldObjectFiles(sourceFiles)

copySwiftModuleFiles()

copySwiftLibraryEvolutionFiles()
}

private fun createSwiftFileList(sourceFiles: List<File>) {
Expand Down Expand Up @@ -169,31 +165,6 @@ class CompileSwiftPhase(
SwiftCompilerConfiguration.BitcodeEmbeddingMode.Full -> "-embed-bitcode"
}

private fun copySwiftModuleFiles() {
val copyFiles = mapOf(
swiftFrameworkHeader.swiftModule to framework.swiftModule(targetTriple),
swiftFrameworkHeader.swiftDoc to framework.swiftDoc(targetTriple),
swiftFrameworkHeader.abiJson to framework.abiJson(targetTriple),
swiftFrameworkHeader.swiftSourceInfo to framework.swiftSourceInfo(targetTriple),
swiftFrameworkHeader.swiftHeader to framework.swiftHeader,
)

copyFiles.forEach { (source, target) ->
source.copyTo(target, overwrite = true)
}
}

private fun copySwiftLibraryEvolutionFiles() {
if (isLibraryEvolutionEnabled) {
swiftFrameworkHeader.swiftInterface.copyTo(framework.swiftInterface(targetTriple), overwrite = true)
swiftFrameworkHeader.privateSwiftInterface.copyTo(framework.privateSwiftInterface(targetTriple), overwrite = true)
} else {
// WIP Check what happens with swiftFrameworkHeader
framework.swiftInterface(targetTriple).delete()
framework.privateSwiftInterface(targetTriple).delete()
}
}

private val parallelizationArgument: String
get() {
val numberOfAvailableProcessors = if (isParallelSwiftCompilationEnabled) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package co.touchlab.skie.phases.swift

import co.touchlab.skie.configuration.SkieConfigurationFlag
import co.touchlab.skie.phases.SirPhase

class CopySwiftOutputFilesToFrameworkPhase(
context: SirPhase.Context,
) : SirPhase {

private val framework = context.framework
private val swiftFrameworkHeader = context.skieBuildDirectory.swiftCompiler.moduleHeader(framework.moduleName)
private val targetTriple = context.swiftCompilerConfiguration.targetTriple

context(SirPhase.Context)
override suspend fun execute() {
if (sirProvider.compilableFiles.isNotEmpty()) {
copySwiftModuleFiles()
copySwiftLibraryEvolutionFiles()
} else {
deleteSwiftModuleFiles()
deleteSwiftLibraryEvolutionFiles()
}
}

private fun copySwiftModuleFiles() {
val copyFiles = mapOf(
swiftFrameworkHeader.swiftModule to framework.swiftModule(targetTriple),
swiftFrameworkHeader.swiftDoc to framework.swiftDoc(targetTriple),
swiftFrameworkHeader.abiJson to framework.abiJson(targetTriple),
swiftFrameworkHeader.swiftSourceInfo to framework.swiftSourceInfo(targetTriple),
swiftFrameworkHeader.swiftHeader to framework.swiftHeader,
)

copyFiles.forEach { (source, target) ->
source.copyTo(target, overwrite = true)
}
}

private fun deleteSwiftModuleFiles() {
framework.swiftModuleParent.deleteRecursively()
framework.swiftHeader.delete()
}

context(SirPhase.Context)
private fun copySwiftLibraryEvolutionFiles() {
if (SkieConfigurationFlag.Build_SwiftLibraryEvolution.isEnabled) {
swiftFrameworkHeader.swiftInterface.copyTo(framework.swiftInterface(targetTriple), overwrite = true)
swiftFrameworkHeader.privateSwiftInterface.copyTo(framework.privateSwiftInterface(targetTriple), overwrite = true)
} else {
deleteSwiftLibraryEvolutionFiles()
}
}

private fun deleteSwiftLibraryEvolutionFiles() {
framework.swiftInterface(targetTriple).delete()
framework.privateSwiftInterface(targetTriple).delete()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import co.touchlab.skie.phases.SirPhase
import co.touchlab.skie.sir.builtin.SirBuiltins
import co.touchlab.skie.sir.element.SirCallableDeclaration
import co.touchlab.skie.sir.element.SirClass
import co.touchlab.skie.sir.element.SirCompilableFile
import co.touchlab.skie.sir.element.SirDeclaration
import co.touchlab.skie.sir.element.SirDeclarationParent
import co.touchlab.skie.sir.element.SirExtension
Expand Down Expand Up @@ -48,6 +49,9 @@ class SirProvider(
val skieModuleFiles: Collection<SirFile>
get() = skieModule.files

val compilableFiles: List<SirCompilableFile>
get() = skieModuleFiles.filterIsInstance<SirCompilableFile>()

val allLocalDeclarations: List<SirDeclaration>
get() = listOf(skieModule, kotlinModule).flatMap { it.getAllDeclarationsRecursively() }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ import co.touchlab.skie.phases.sir.type.InitializeSirSuperTypesPhase
import co.touchlab.skie.phases.swift.CompileSwiftPhase
import co.touchlab.skie.phases.swift.ConvertSirIrFilesToSourceFilesPhase
import co.touchlab.skie.phases.swift.ConvertSirSourceFilesToCompilableFilesPhase
import co.touchlab.skie.phases.swift.CopySwiftOutputFilesToFrameworkPhase
import co.touchlab.skie.phases.swift.SwiftKotlinFrameworkCacheSetupPhase
import co.touchlab.skie.phases.typeconflicts.RenameTypesConflictingWithKeywordsPhase
import co.touchlab.skie.phases.typeconflicts.RenameTypesConflictingWithKotlinModulePhase
Expand Down Expand Up @@ -241,6 +242,7 @@ class LinkerPhaseScheduler : SkiePhaseScheduler {
SwiftKotlinFrameworkCacheSetupPhase,
ImportFakeObjCDependenciesPhase.RevertPhase,
CompileSwiftPhase(context),
CopySwiftOutputFilesToFrameworkPhase(context),
TemporarilyRenameTypesConflictingWithExternalModulesPhase.RevertPhase,
GenerateModulemapFilePhase.ForFramework,
ApiNotesGenerationPhase.ForFramework,
Expand Down

0 comments on commit 8584e39

Please sign in to comment.