From 6dd950cd5ade615c387d8b494b7b2997798fb79c Mon Sep 17 00:00:00 2001 From: Alexey Sedunov Date: Wed, 26 Oct 2016 12:31:24 +0300 Subject: [PATCH] Kotlin Facet: Use facet configuration in JPS build --- .../kotlin/jps/JpsKotlinCompilerSettings.kt | 32 +++++++++++-- .../kotlin/jps/build/KotlinBuilder.kt | 17 +++---- .../JpsKotlinFacetConfigurationSerializer.kt | 47 +++++++++++++++++++ .../model/JpsKotlinFacetModuleExtension.kt | 43 +++++++++++++++++ .../model/KotlinModelSerializerService.java | 8 ++++ 5 files changed, 135 insertions(+), 12 deletions(-) create mode 100644 jps-plugin/src/org/jetbrains/kotlin/jps/model/JpsKotlinFacetConfigurationSerializer.kt create mode 100644 jps-plugin/src/org/jetbrains/kotlin/jps/model/JpsKotlinFacetModuleExtension.kt diff --git a/jps-plugin/src/org/jetbrains/kotlin/jps/JpsKotlinCompilerSettings.kt b/jps-plugin/src/org/jetbrains/kotlin/jps/JpsKotlinCompilerSettings.kt index e3dc4de3717..0a50eb22998 100644 --- a/jps-plugin/src/org/jetbrains/kotlin/jps/JpsKotlinCompilerSettings.kt +++ b/jps-plugin/src/org/jetbrains/kotlin/jps/JpsKotlinCompilerSettings.kt @@ -19,10 +19,14 @@ package org.jetbrains.kotlin.jps import org.jetbrains.jps.model.JpsProject import org.jetbrains.jps.model.ex.JpsElementBase import org.jetbrains.jps.model.ex.JpsElementChildRoleBase +import org.jetbrains.jps.model.module.JpsModule import org.jetbrains.kotlin.cli.common.arguments.CommonCompilerArguments import org.jetbrains.kotlin.cli.common.arguments.K2JSCompilerArguments import org.jetbrains.kotlin.cli.common.arguments.K2JVMCompilerArguments +import org.jetbrains.kotlin.cli.common.arguments.copyBean import org.jetbrains.kotlin.config.CompilerSettings +import org.jetbrains.kotlin.config.JVMPlatform +import org.jetbrains.kotlin.jps.model.kotlinFacetExtension class JpsKotlinCompilerSettings : JpsElementBase() { private var commonCompilerArguments: CommonCompilerArguments = CommonCompilerArguments.DummyImpl() @@ -57,25 +61,45 @@ class JpsKotlinCompilerSettings : JpsElementBase() { return settings } - fun getCommonCompilerArguments(project: JpsProject) = getSettings(project).commonCompilerArguments + fun getCommonCompilerArguments(module: JpsModule): CommonCompilerArguments { + val defaultArguments = getSettings(module.project).commonCompilerArguments + val facetSettings = module.kotlinFacetExtension?.settings ?: return defaultArguments + val (languageLevel, apiLevel) = facetSettings.versionInfo + return facetSettings.compilerInfo.commonCompilerArguments?.apply { + languageVersion = languageLevel?.description + apiVersion = apiLevel?.description + } ?: defaultArguments + } fun setCommonCompilerArguments(project: JpsProject, commonCompilerSettings: CommonCompilerArguments) { getOrCreateSettings(project).commonCompilerArguments = commonCompilerSettings } - fun getK2JvmCompilerArguments(project: JpsProject) = getSettings(project).k2JvmCompilerArguments + fun getK2JvmCompilerArguments(module: JpsModule): K2JVMCompilerArguments { + val defaultArguments = getSettings(module.project).k2JvmCompilerArguments + val facetSettings = module.kotlinFacetExtension?.settings ?: return defaultArguments + val targetPlatform = facetSettings.versionInfo.targetPlatformKindKind as? JVMPlatform ?: return defaultArguments + return copyBean(defaultArguments).apply { + jvmTarget = targetPlatform.version.description + } + } fun setK2JvmCompilerArguments(project: JpsProject, k2JvmCompilerArguments: K2JVMCompilerArguments) { getOrCreateSettings(project).k2JvmCompilerArguments = k2JvmCompilerArguments } - fun getK2JsCompilerArguments(project: JpsProject) = getSettings(project).k2JsCompilerArguments + fun getK2JsCompilerArguments(module: JpsModule): K2JSCompilerArguments { + return module.kotlinFacetExtension?.settings?.compilerInfo?.k2jsCompilerArguments + ?: getSettings(module.project).k2JsCompilerArguments + } fun setK2JsCompilerArguments(project: JpsProject, k2JsCompilerArguments: K2JSCompilerArguments) { getOrCreateSettings(project).k2JsCompilerArguments = k2JsCompilerArguments } - fun getCompilerSettings(project: JpsProject) = getSettings(project).compilerSettings + fun getCompilerSettings(module: JpsModule): CompilerSettings { + return module.kotlinFacetExtension?.settings?.compilerInfo?.compilerSettings ?: getSettings(module.project).compilerSettings + } fun setCompilerSettings(project: JpsProject, compilerSettings: CompilerSettings) { getOrCreateSettings(project).compilerSettings = compilerSettings diff --git a/jps-plugin/src/org/jetbrains/kotlin/jps/build/KotlinBuilder.kt b/jps-plugin/src/org/jetbrains/kotlin/jps/build/KotlinBuilder.kt index 45d378514d6..e57be963c48 100644 --- a/jps-plugin/src/org/jetbrains/kotlin/jps/build/KotlinBuilder.kt +++ b/jps-plugin/src/org/jetbrains/kotlin/jps/build/KotlinBuilder.kt @@ -214,7 +214,7 @@ class KotlinBuilder : ModuleLevelBuilder(BuilderCategory.SOURCE_PROCESSOR) { return ABORT } - val commonArguments = JpsKotlinCompilerSettings.getCommonCompilerArguments(project) + val commonArguments = JpsKotlinCompilerSettings.getCommonCompilerArguments(chunk.representativeTarget().module) commonArguments.verbose = true // Make compiler report source to output files mapping val allCompiledFiles = getAllCompiledFilesContainer(context) @@ -618,11 +618,12 @@ class KotlinBuilder : ModuleLevelBuilder(BuilderCategory.SOURCE_PROCESSOR) { val outputDir = KotlinBuilderModuleScriptGenerator.getOutputDirSafe(representativeTarget) - val moduleName = representativeTarget.module.name + val representativeModule = representativeTarget.module + val moduleName = representativeModule.name val outputFile = JpsJsModuleUtils.getOutputFile(outputDir, moduleName) val libraryFiles = JpsJsModuleUtils.getLibraryFilesAndDependencies(representativeTarget) - val compilerSettings = JpsKotlinCompilerSettings.getCompilerSettings(project) - val k2JsArguments = JpsKotlinCompilerSettings.getK2JsCompilerArguments(project) + val compilerSettings = JpsKotlinCompilerSettings.getCompilerSettings(representativeModule) + val k2JsArguments = JpsKotlinCompilerSettings.getK2JsCompilerArguments(representativeModule) KotlinCompilerRunner.runK2JsCompiler(commonArguments, k2JsArguments, compilerSettings, messageCollector, environment, outputItemCollector, sourceFiles, libraryFiles, outputFile) return outputItemCollector @@ -631,7 +632,7 @@ class KotlinBuilder : ModuleLevelBuilder(BuilderCategory.SOURCE_PROCESSOR) { private fun copyJsLibraryFilesIfNeeded(chunk: ModuleChunk, project: JpsProject) { val representativeTarget = chunk.representativeTarget() val outputDir = KotlinBuilderModuleScriptGenerator.getOutputDirSafe(representativeTarget) - val compilerSettings = JpsKotlinCompilerSettings.getCompilerSettings(project) + val compilerSettings = JpsKotlinCompilerSettings.getCompilerSettings(representativeTarget.module) if (compilerSettings.copyJsLibraryFiles) { val outputLibraryRuntimeDirectory = File(outputDir, compilerSettings.outputDirectoryForJsLibraryFiles).absolutePath val libraryFilesToCopy = arrayListOf() @@ -682,9 +683,9 @@ class KotlinBuilder : ModuleLevelBuilder(BuilderCategory.SOURCE_PROCESSOR) { return null } - val project = context.projectDescriptor.project - val k2JvmArguments = JpsKotlinCompilerSettings.getK2JvmCompilerArguments(project) - val compilerSettings = JpsKotlinCompilerSettings.getCompilerSettings(project) + val module = chunk.representativeTarget().module + val k2JvmArguments = JpsKotlinCompilerSettings.getK2JvmCompilerArguments(module) + val compilerSettings = JpsKotlinCompilerSettings.getCompilerSettings(module) KotlinBuilder.LOG.debug("Compiling to JVM ${filesToCompile.values().size} files" + (if (totalRemovedFiles == 0) "" else " ($totalRemovedFiles removed files)") diff --git a/jps-plugin/src/org/jetbrains/kotlin/jps/model/JpsKotlinFacetConfigurationSerializer.kt b/jps-plugin/src/org/jetbrains/kotlin/jps/model/JpsKotlinFacetConfigurationSerializer.kt new file mode 100644 index 00000000000..9bf0e88d700 --- /dev/null +++ b/jps-plugin/src/org/jetbrains/kotlin/jps/model/JpsKotlinFacetConfigurationSerializer.kt @@ -0,0 +1,47 @@ +/* + * Copyright 2010-2016 JetBrains s.r.o. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.jetbrains.kotlin.jps.model + +import com.intellij.util.xmlb.XmlSerializer +import org.jdom.Element +import org.jetbrains.jps.model.JpsElement +import org.jetbrains.jps.model.module.JpsModule +import org.jetbrains.jps.model.serialization.facet.JpsFacetConfigurationSerializer +import org.jetbrains.kotlin.config.KotlinFacetSettings + +object JpsKotlinFacetConfigurationSerializer : JpsFacetConfigurationSerializer( + JpsKotlinFacetModuleExtension.KIND, + JpsKotlinFacetModuleExtension.FACET_TYPE_ID, + JpsKotlinFacetModuleExtension.FACET_NAME +) { + override fun loadExtension( + facetConfigurationElement: Element, + name: String, + parent: JpsElement?, + module: JpsModule + ): JpsKotlinFacetModuleExtension { + return JpsKotlinFacetModuleExtension(XmlSerializer.deserialize(facetConfigurationElement, KotlinFacetSettings::class.java)!!) + } + + override fun saveExtension( + extension: JpsKotlinFacetModuleExtension?, + facetConfigurationTag: Element, + module: JpsModule + ) { + XmlSerializer.serializeInto((extension as JpsKotlinFacetModuleExtension).settings, facetConfigurationTag) + } +} \ No newline at end of file diff --git a/jps-plugin/src/org/jetbrains/kotlin/jps/model/JpsKotlinFacetModuleExtension.kt b/jps-plugin/src/org/jetbrains/kotlin/jps/model/JpsKotlinFacetModuleExtension.kt new file mode 100644 index 00000000000..0add54661f4 --- /dev/null +++ b/jps-plugin/src/org/jetbrains/kotlin/jps/model/JpsKotlinFacetModuleExtension.kt @@ -0,0 +1,43 @@ +/* + * Copyright 2010-2016 JetBrains s.r.o. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.jetbrains.kotlin.jps.model + +import org.jetbrains.jps.model.ex.JpsElementBase +import org.jetbrains.jps.model.ex.JpsElementChildRoleBase +import org.jetbrains.jps.model.module.JpsModule +import org.jetbrains.kotlin.config.KotlinFacetSettings + +class JpsKotlinFacetModuleExtension(settings: KotlinFacetSettings) : JpsElementBase() { + var settings = settings + private set + + companion object { + val KIND = JpsElementChildRoleBase.create("kotlin facet extension") + // These must be changed in sync with KotlinFacetType.TYPE_ID and KotlinFacetType.NAME + val FACET_TYPE_ID = "kotlin-language" + val FACET_NAME = "Kotlin" + } + + override fun createCopy() = JpsKotlinFacetModuleExtension(settings) + + override fun applyChanges(modified: JpsKotlinFacetModuleExtension) { + this.settings = modified.settings + } +} + +val JpsModule.kotlinFacetExtension: JpsKotlinFacetModuleExtension? + get() = container.getChild(JpsKotlinFacetModuleExtension.KIND) \ No newline at end of file diff --git a/jps-plugin/src/org/jetbrains/kotlin/jps/model/KotlinModelSerializerService.java b/jps-plugin/src/org/jetbrains/kotlin/jps/model/KotlinModelSerializerService.java index 8274f453dde..1e12e3e6db9 100644 --- a/jps-plugin/src/org/jetbrains/kotlin/jps/model/KotlinModelSerializerService.java +++ b/jps-plugin/src/org/jetbrains/kotlin/jps/model/KotlinModelSerializerService.java @@ -19,8 +19,10 @@ package org.jetbrains.kotlin.jps.model; import org.jetbrains.annotations.NotNull; import org.jetbrains.jps.model.serialization.JpsModelSerializerExtension; import org.jetbrains.jps.model.serialization.JpsProjectExtensionSerializer; +import org.jetbrains.jps.model.serialization.facet.JpsFacetConfigurationSerializer; import java.util.Arrays; +import java.util.Collections; import java.util.List; public class KotlinModelSerializerService extends JpsModelSerializerExtension { @@ -32,4 +34,10 @@ public class KotlinModelSerializerService extends JpsModelSerializerExtension { new Kotlin2JsCompilerArgumentsSerializer(), new KotlinCompilerSettingsSerializer()); } + + @NotNull + @Override + public List> getFacetConfigurationSerializers() { + return Collections.singletonList(JpsKotlinFacetConfigurationSerializer.INSTANCE); + } }