Introduce K2MetadataCompiler, extract MetadataSerializer out of BuiltInsSerializer
K2MetadataCompiler is a compiler facade similar to K2JVMCompiler and
K2JSCompiler and it produces .kotlin_metadata files. Each .kotlin_metadata file
contains the binary data which is a serialized BuiltIns protobuf message.
There's no 'kotlinc-***' script yet though, so to run this compiler currently
invoke this:
KOTLIN_COMPILER=org.jetbrains.kotlin.cli.metadata.K2MetadataCompiler kotlinc
This commit is contained in:
+29
@@ -0,0 +1,29 @@
|
||||
/*
|
||||
* 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.cli.common.arguments;
|
||||
|
||||
import com.sampullara.cli.Argument;
|
||||
|
||||
public class K2MetadataCompilerArguments extends CommonCompilerArguments {
|
||||
@Argument(value = "d", description = "Destination for generated .kotlin_metadata files")
|
||||
@ValueDescription("<directory|jar>")
|
||||
public String destination;
|
||||
|
||||
@Argument(value = "classpath", alias = "cp", description = "Paths where to find library .kotlin_metadata files")
|
||||
@ValueDescription("<path>")
|
||||
public String classpath;
|
||||
}
|
||||
@@ -21,5 +21,6 @@
|
||||
<orderEntry type="module" module-name="js.serializer" />
|
||||
<orderEntry type="module" module-name="util" />
|
||||
<orderEntry type="module" module-name="annotation-collector" />
|
||||
<orderEntry type="module" module-name="builtins-serializer" />
|
||||
</component>
|
||||
</module>
|
||||
@@ -20,6 +20,8 @@ import org.jetbrains.kotlin.cli.common.messages.MessageCollector;
|
||||
import org.jetbrains.kotlin.cli.jvm.compiler.CompilerJarLocator;
|
||||
import org.jetbrains.kotlin.config.CompilerConfigurationKey;
|
||||
|
||||
import java.io.File;
|
||||
|
||||
public class CLIConfigurationKeys {
|
||||
public static final CompilerConfigurationKey<MessageCollector> MESSAGE_COLLECTOR_KEY =
|
||||
CompilerConfigurationKey.create("message collector");
|
||||
@@ -32,6 +34,11 @@ public class CLIConfigurationKeys {
|
||||
public static final CompilerConfigurationKey<CompilerJarLocator> COMPILER_JAR_LOCATOR =
|
||||
CompilerConfigurationKey.create("compiler jar locator");
|
||||
|
||||
// See K2MetadataCompilerArguments
|
||||
|
||||
public static final CompilerConfigurationKey<File> METADATA_DESTINATION_DIRECTORY =
|
||||
CompilerConfigurationKey.create("metadata destination directory");
|
||||
|
||||
private CLIConfigurationKeys() {
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,107 @@
|
||||
/*
|
||||
* 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.cli.metadata
|
||||
|
||||
import com.intellij.openapi.Disposable
|
||||
import org.jetbrains.kotlin.cli.common.CLICompiler
|
||||
import org.jetbrains.kotlin.cli.common.CLIConfigurationKeys
|
||||
import org.jetbrains.kotlin.cli.common.ExitCode
|
||||
import org.jetbrains.kotlin.cli.common.arguments.K2MetadataCompilerArguments
|
||||
import org.jetbrains.kotlin.cli.common.messages.CompilerMessageLocation
|
||||
import org.jetbrains.kotlin.cli.common.messages.CompilerMessageSeverity
|
||||
import org.jetbrains.kotlin.cli.common.messages.MessageUtil
|
||||
import org.jetbrains.kotlin.cli.common.messages.OutputMessageUtil
|
||||
import org.jetbrains.kotlin.cli.jvm.compiler.EnvironmentConfigFiles
|
||||
import org.jetbrains.kotlin.cli.jvm.compiler.KotlinCoreEnvironment
|
||||
import org.jetbrains.kotlin.cli.jvm.config.addJvmClasspathRoots
|
||||
import org.jetbrains.kotlin.codegen.CompilationException
|
||||
import org.jetbrains.kotlin.config.CommonConfigurationKeys
|
||||
import org.jetbrains.kotlin.config.CompilerConfiguration
|
||||
import org.jetbrains.kotlin.config.Services
|
||||
import org.jetbrains.kotlin.config.addKotlinSourceRoot
|
||||
import org.jetbrains.kotlin.load.java.JvmAbi
|
||||
import org.jetbrains.kotlin.serialization.MetadataSerializer
|
||||
import java.io.File
|
||||
|
||||
class K2MetadataCompiler : CLICompiler<K2MetadataCompilerArguments>() {
|
||||
override fun createArguments() = K2MetadataCompilerArguments()
|
||||
|
||||
override fun setupPlatformSpecificArgumentsAndServices(
|
||||
configuration: CompilerConfiguration, arguments: K2MetadataCompilerArguments, services: Services
|
||||
) {
|
||||
// No specific arguments yet
|
||||
}
|
||||
|
||||
override fun doExecute(
|
||||
arguments: K2MetadataCompilerArguments, configuration: CompilerConfiguration, rootDisposable: Disposable
|
||||
): ExitCode {
|
||||
val collector = configuration.getNotNull(CLIConfigurationKeys.MESSAGE_COLLECTOR_KEY)
|
||||
|
||||
for (arg in arguments.freeArgs) {
|
||||
configuration.addKotlinSourceRoot(arg)
|
||||
}
|
||||
if (arguments.classpath != null) {
|
||||
configuration.addJvmClasspathRoots(arguments.classpath.split(File.pathSeparatorChar).map(::File))
|
||||
}
|
||||
|
||||
configuration.put(CommonConfigurationKeys.MODULE_NAME, JvmAbi.DEFAULT_MODULE_NAME)
|
||||
|
||||
val destination = arguments.destination
|
||||
if (destination != null) {
|
||||
if (destination.endsWith(".jar")) {
|
||||
// TODO: support .jar destination
|
||||
collector.report(
|
||||
CompilerMessageSeverity.WARNING,
|
||||
".jar destination is not yet supported, results will be written to the directory with the given name",
|
||||
CompilerMessageLocation.NO_LOCATION
|
||||
)
|
||||
}
|
||||
configuration.put(CLIConfigurationKeys.METADATA_DESTINATION_DIRECTORY, File(destination))
|
||||
}
|
||||
|
||||
val environment = KotlinCoreEnvironment.createForProduction(rootDisposable, configuration, EnvironmentConfigFiles.JVM_CONFIG_FILES)
|
||||
|
||||
if (environment.getSourceFiles().isEmpty()) {
|
||||
if (arguments.version) {
|
||||
return ExitCode.OK
|
||||
}
|
||||
collector.report(CompilerMessageSeverity.ERROR, "No source files", CompilerMessageLocation.NO_LOCATION)
|
||||
return ExitCode.COMPILATION_ERROR
|
||||
}
|
||||
|
||||
try {
|
||||
MetadataSerializer(true).serialize(environment)
|
||||
}
|
||||
catch (e: CompilationException) {
|
||||
collector.report(
|
||||
CompilerMessageSeverity.EXCEPTION,
|
||||
OutputMessageUtil.renderException(e),
|
||||
MessageUtil.psiElementToMessageLocation(e.element)
|
||||
)
|
||||
return ExitCode.INTERNAL_ERROR
|
||||
}
|
||||
|
||||
return ExitCode.OK
|
||||
}
|
||||
|
||||
companion object {
|
||||
@JvmStatic
|
||||
fun main(args: Array<String>) {
|
||||
doMain(K2MetadataCompiler(), args)
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user