Kotlin Facet: Use facet configuration in JPS build

This commit is contained in:
Alexey Sedunov
2016-10-26 12:31:24 +03:00
parent d0de9dd43c
commit 6dd950cd5a
5 changed files with 135 additions and 12 deletions
@@ -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<JpsKotlinCompilerSettings>() {
private var commonCompilerArguments: CommonCompilerArguments = CommonCompilerArguments.DummyImpl()
@@ -57,25 +61,45 @@ class JpsKotlinCompilerSettings : JpsElementBase<JpsKotlinCompilerSettings>() {
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
@@ -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<String>()
@@ -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)")
@@ -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>(
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)
}
}
@@ -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<JpsKotlinFacetModuleExtension>() {
var settings = settings
private set
companion object {
val KIND = JpsElementChildRoleBase.create<JpsKotlinFacetModuleExtension>("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)
@@ -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<? extends JpsFacetConfigurationSerializer<?>> getFacetConfigurationSerializers() {
return Collections.singletonList(JpsKotlinFacetConfigurationSerializer.INSTANCE);
}
}