[JS IR] save the desired JS output file name in the klib.

This is only usedful for code splitting. The output .js file name
is provided by the build system during the module compilation.
It is desirable to keep the .js output file names same as in old BE,
but calculating those name during the klib -> js phase is tricky.

Thus the desired names are saved in the klib, and used later on.
This commit is contained in:
Anton Bannykh
2020-09-30 13:35:36 +03:00
committed by TeamCityServer
parent 6633a9edc0
commit 7bd9462ffb
7 changed files with 51 additions and 20 deletions
@@ -205,8 +205,8 @@ class K2JsIrCompiler : CLICompiler<K2JSCompilerArguments>() {
friendDependencies = friendDependencies, friendDependencies = friendDependencies,
irFactory = PersistentIrFactory(), // TODO IrFactoryImpl? irFactory = PersistentIrFactory(), // TODO IrFactoryImpl?
outputKlibPath = outputFile.path, outputKlibPath = outputFile.path,
nopack = arguments.irProduceKlibDir nopack = arguments.irProduceKlibDir,
) jsOutputName = FileUtil.getNameWithoutExtension(outputFile),)
} }
if (arguments.irProduceJs) { if (arguments.irProduceJs) {
@@ -54,7 +54,7 @@ fun compile(
propertyLazyInitialization: Boolean, propertyLazyInitialization: Boolean,
legacyPropertyAccess: Boolean = false, legacyPropertyAccess: Boolean = false,
): CompilerResult { ): CompilerResult {
val (moduleFragment: IrModuleFragment, dependencyModules, irBuiltIns, symbolTable, deserializer) = val (moduleFragment: IrModuleFragment, dependencyModules, irBuiltIns, symbolTable, deserializer, moduleToName) =
loadIr(project, mainModule, analyzer, configuration, allDependencies, friendDependencies, irFactory) loadIr(project, mainModule, analyzer, configuration, allDependencies, friendDependencies, irFactory)
val moduleDescriptor = moduleFragment.descriptor val moduleDescriptor = moduleFragment.descriptor
@@ -109,7 +109,8 @@ fun compile(
fullJs = true, fullJs = true,
dceJs = false, dceJs = false,
multiModule = multiModule, multiModule = multiModule,
relativeRequirePath = relativeRequirePath relativeRequirePath = relativeRequirePath,
moduleToName = moduleToName,
) )
return transformer.generateModule(allModules) return transformer.generateModule(allModules)
} else { } else {
@@ -120,7 +121,8 @@ fun compile(
fullJs = generateFullJs, fullJs = generateFullJs,
dceJs = generateDceJs, dceJs = generateDceJs,
multiModule = multiModule, multiModule = multiModule,
relativeRequirePath = relativeRequirePath relativeRequirePath = relativeRequirePath,
moduleToName = moduleToName,
) )
return transformer.generateModule(allModules) return transformer.generateModule(allModules)
} }
@@ -31,7 +31,8 @@ class IrModuleToJsTransformer(
private val fullJs: Boolean = true, private val fullJs: Boolean = true,
private val dceJs: Boolean = false, private val dceJs: Boolean = false,
private val multiModule: Boolean = false, private val multiModule: Boolean = false,
private val relativeRequirePath: Boolean = false private val relativeRequirePath: Boolean = false,
private val moduleToName: Map<IrModuleFragment, String> = emptyMap()
) { ) {
private val generateRegionComments = backendContext.configuration.getBoolean(JSConfigurationKeys.GENERATE_REGION_COMMENTS) private val generateRegionComments = backendContext.configuration.getBoolean(JSConfigurationKeys.GENERATE_REGION_COMMENTS)
@@ -91,7 +92,7 @@ class IrModuleToJsTransformer(
) )
val dependencies = others.mapIndexed { index, module -> val dependencies = others.mapIndexed { index, module ->
val moduleName = sanitizeName(module.safeName) val moduleName = moduleToName[module] ?: sanitizeName(module.safeName)
val exportedDeclarations = ExportModelGenerator(backendContext).let { module.files.flatMap { file -> it.generateExport(file) } } val exportedDeclarations = ExportModelGenerator(backendContext).let { module.files.flatMap { file -> it.generateExport(file) } }
@@ -206,7 +207,7 @@ class IrModuleToJsTransformer(
} }
val moduleName = declareFreshGlobal(module.safeName) val moduleName = declareFreshGlobal(module.safeName)
modules += JsImportedModule(moduleName.ident, moduleName, moduleName.makeRef(), relativeRequirePath) modules += JsImportedModule(moduleToName[module] ?: moduleName.ident, moduleName, moduleName.makeRef(), relativeRequirePath)
names.forEach { names.forEach {
imports += JsVars(JsVars.JsVar(JsName(it), JsNameRef(it, JsNameRef("\$crossModule\$", moduleName.makeRef())))) imports += JsVars(JsVars.JsVar(JsName(it), JsNameRef(it, JsNameRef("\$crossModule\$", moduleName.makeRef()))))
@@ -70,6 +70,7 @@ import org.jetbrains.kotlin.storage.LockBasedStorageManager
import org.jetbrains.kotlin.storage.StorageManager import org.jetbrains.kotlin.storage.StorageManager
import org.jetbrains.kotlin.utils.DFS import org.jetbrains.kotlin.utils.DFS
import java.io.File import java.io.File
import java.util.*
import org.jetbrains.kotlin.konan.file.File as KFile import org.jetbrains.kotlin.konan.file.File as KFile
val KotlinLibrary.moduleName: String val KotlinLibrary.moduleName: String
@@ -104,7 +105,8 @@ fun generateKLib(
friendDependencies: List<KotlinLibrary>, friendDependencies: List<KotlinLibrary>,
irFactory: IrFactory, irFactory: IrFactory,
outputKlibPath: String, outputKlibPath: String,
nopack: Boolean nopack: Boolean,
jsOutputName: String?,
) { ) {
val incrementalDataProvider = configuration.get(JSConfigurationKeys.INCREMENTAL_DATA_PROVIDER) val incrementalDataProvider = configuration.get(JSConfigurationKeys.INCREMENTAL_DATA_PROVIDER)
val errorPolicy = configuration.get(JSConfigurationKeys.ERROR_TOLERANCE_POLICY) ?: ErrorTolerancePolicy.DEFAULT val errorPolicy = configuration.get(JSConfigurationKeys.ERROR_TOLERANCE_POLICY) ?: ErrorTolerancePolicy.DEFAULT
@@ -194,7 +196,8 @@ fun generateKLib(
icData, icData,
nopack, nopack,
perFile = false, perFile = false,
hasErrors hasErrors,
jsOutputName
) )
} }
@@ -203,7 +206,8 @@ data class IrModuleInfo(
val allDependencies: List<IrModuleFragment>, val allDependencies: List<IrModuleFragment>,
val bultins: IrBuiltIns, val bultins: IrBuiltIns,
val symbolTable: SymbolTable, val symbolTable: SymbolTable,
val deserializer: JsIrLinker val deserializer: JsIrLinker,
val moduleFragmentToUniqueName: Map<IrModuleFragment, String>,
) )
private fun sortDependencies(dependencies: List<KotlinLibrary>, mapping: Map<KotlinLibrary, ModuleDescriptor>): Collection<KotlinLibrary> { private fun sortDependencies(dependencies: List<KotlinLibrary>, mapping: Map<KotlinLibrary, ModuleDescriptor>): Collection<KotlinLibrary> {
@@ -238,9 +242,14 @@ fun loadIr(
val feContext = psi2IrContext.run { val feContext = psi2IrContext.run {
JsIrLinker.JsFePluginContext(moduleDescriptor, symbolTable, typeTranslator, irBuiltIns) JsIrLinker.JsFePluginContext(moduleDescriptor, symbolTable, typeTranslator, irBuiltIns)
} }
val moduleFragmentToUniqueName = mutableMapOf<IrModuleFragment, String>()
val irLinker = JsIrLinker(psi2IrContext.moduleDescriptor, messageLogger, irBuiltIns, symbolTable, functionFactory, feContext, null) val irLinker = JsIrLinker(psi2IrContext.moduleDescriptor, messageLogger, irBuiltIns, symbolTable, functionFactory, feContext, null)
val deserializedModuleFragments = sortDependencies(allDependencies.getFullList(), depsDescriptors.descriptors).map { val deserializedModuleFragments = sortDependencies(allDependencies.getFullList(), depsDescriptors.descriptors).map { klib ->
irLinker.deserializeIrModuleHeader(depsDescriptors.getModuleDescriptor(it), it) irLinker.deserializeIrModuleHeader(depsDescriptors.getModuleDescriptor(klib), klib).also { moduleFragment ->
klib.manifestProperties.getProperty(KLIB_PROPERTY_JS_OUTPUT_NAME)?.let {
moduleFragmentToUniqueName[moduleFragment] = it
}
}
} }
val moduleFragment = psi2IrContext.generateModuleFragmentWithPlugins(project, mainModule.files, irLinker, messageLogger) val moduleFragment = psi2IrContext.generateModuleFragmentWithPlugins(project, mainModule.files, irLinker, messageLogger)
@@ -257,7 +266,7 @@ fun loadIr(
irBuiltIns.knownBuiltins.forEach { it.acceptVoid(mangleChecker) } irBuiltIns.knownBuiltins.forEach { it.acceptVoid(mangleChecker) }
return IrModuleInfo(moduleFragment, deserializedModuleFragments, irBuiltIns, symbolTable, irLinker) return IrModuleInfo(moduleFragment, deserializedModuleFragments, irBuiltIns, symbolTable, irLinker, moduleFragmentToUniqueName)
} }
is MainModule.Klib -> { is MainModule.Klib -> {
val moduleDescriptor = depsDescriptors.getModuleDescriptor(mainModule.lib) val moduleDescriptor = depsDescriptors.getModuleDescriptor(mainModule.lib)
@@ -271,14 +280,20 @@ fun loadIr(
val irLinker = val irLinker =
JsIrLinker(null, messageLogger, irBuiltIns, symbolTable, functionFactory, null, null) JsIrLinker(null, messageLogger, irBuiltIns, symbolTable, functionFactory, null, null)
val deserializedModuleFragments = sortDependencies(allDependencies.getFullList(), depsDescriptors.descriptors).map { val moduleFragmentToUniqueName = mutableMapOf<IrModuleFragment, String>()
val deserializedModuleFragments = sortDependencies(allDependencies.getFullList(), depsDescriptors.descriptors).map { klib ->
val strategy = val strategy =
if (it == mainModule.lib) if (it == mainModule.lib)
DeserializationStrategy.ALL DeserializationStrategy.ALL
else else
DeserializationStrategy.EXPLICITLY_EXPORTED DeserializationStrategy.EXPLICITLY_EXPORTED
irLinker.deserializeIrModuleHeader(depsDescriptors.getModuleDescriptor(it), it, strategy) irLinker.deserializeIrModuleHeader(depsDescriptors.getModuleDescriptor(klib), klib, strategy).also { moduleFragment ->
klib.manifestProperties.getProperty(KLIB_PROPERTY_JS_OUTPUT_NAME)?.let {
moduleFragmentToUniqueName[moduleFragment] = it
}
}
} }
irBuiltIns.functionFactory = functionFactory irBuiltIns.functionFactory = functionFactory
@@ -288,7 +303,7 @@ fun loadIr(
ExternalDependenciesGenerator(symbolTable, listOf(irLinker)).generateUnboundSymbolsAsDependencies() ExternalDependenciesGenerator(symbolTable, listOf(irLinker)).generateUnboundSymbolsAsDependencies()
irLinker.postProcess() irLinker.postProcess()
return IrModuleInfo(moduleFragment, deserializedModuleFragments, irBuiltIns, symbolTable, irLinker) return IrModuleInfo(moduleFragment, deserializedModuleFragments, irBuiltIns, symbolTable, irLinker, moduleFragmentToUniqueName)
} }
} }
} }
@@ -475,7 +490,8 @@ fun serializeModuleIntoKlib(
cleanFiles: List<KotlinFileSerializedData>, cleanFiles: List<KotlinFileSerializedData>,
nopack: Boolean, nopack: Boolean,
perFile: Boolean, perFile: Boolean,
containsErrorCode: Boolean = false containsErrorCode: Boolean = false,
jsOutputName: String?,
) { ) {
assert(files.size == moduleFragment.files.size) assert(files.size == moduleFragment.files.size)
@@ -551,6 +567,14 @@ fun serializeModuleIntoKlib(
} }
} else null } else null
val properties = Properties().also { p ->
if (jsOutputName != null) {
p.setProperty(KLIB_PROPERTY_JS_OUTPUT_NAME, jsOutputName)
}
if (containsErrorCode) {
p.setProperty(KLIB_PROPERTY_CONTAINS_ERROR_CODE, "true")
}
buildKotlinLibrary( buildKotlinLibrary(
linkDependencies = dependencies, linkDependencies = dependencies,
ir = fullSerializedIr, ir = fullSerializedIr,
@@ -566,6 +590,8 @@ fun serializeModuleIntoKlib(
) )
} }
const val KLIB_PROPERTY_JS_OUTPUT_NAME = "jsOutputName"
private fun KlibMetadataIncrementalSerializer.serializeScope( private fun KlibMetadataIncrementalSerializer.serializeScope(
ktFile: KtFile, ktFile: KtFile,
bindingContext: BindingContext, bindingContext: BindingContext,
@@ -491,7 +491,8 @@ class GenerateIrRuntime {
mutableMapOf(), mutableMapOf(),
emptyList(), emptyList(),
true, true,
perFile perFile,
null
) )
return tmpKlibDir return tmpKlibDir
@@ -188,7 +188,8 @@ abstract class BasicIrBoxTest(
friendDependencies = emptyList(), friendDependencies = emptyList(),
irFactory = IrFactoryImpl, irFactory = IrFactoryImpl,
outputKlibPath = actualOutputFile, outputKlibPath = actualOutputFile,
nopack = true nopack = true,
null
) )
logger.logFile("Output klib", File(actualOutputFile)) logger.logFile("Output klib", File(actualOutputFile))