001 /*
002 * Copyright 2010-2013 JetBrains s.r.o.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017 package org.jetbrains.jet.cli.jvm;
018
019 import com.google.common.base.Splitter;
020 import com.google.common.collect.Lists;
021 import com.intellij.openapi.Disposable;
022 import com.intellij.openapi.util.text.StringUtil;
023 import org.jetbrains.annotations.NotNull;
024 import org.jetbrains.jet.cli.common.CLICompiler;
025 import org.jetbrains.jet.cli.common.CLIConfigurationKeys;
026 import org.jetbrains.jet.cli.common.ExitCode;
027 import org.jetbrains.jet.cli.common.arguments.K2JVMCompilerArguments;
028 import org.jetbrains.jet.cli.common.messages.*;
029 import org.jetbrains.jet.cli.jvm.compiler.*;
030 import org.jetbrains.jet.cli.jvm.repl.ReplFromTerminal;
031 import org.jetbrains.jet.codegen.CompilationException;
032 import org.jetbrains.jet.config.CommonConfigurationKeys;
033 import org.jetbrains.jet.config.CompilerConfiguration;
034 import org.jetbrains.jet.lang.resolve.AnalyzerScriptParameter;
035 import org.jetbrains.jet.utils.KotlinPaths;
036 import org.jetbrains.jet.utils.KotlinPathsFromHomeDir;
037 import org.jetbrains.jet.utils.PathUtil;
038
039 import java.io.File;
040 import java.util.Arrays;
041 import java.util.Collections;
042 import java.util.List;
043
044 import static com.google.common.base.Predicates.in;
045 import static org.jetbrains.jet.cli.common.ExitCode.INTERNAL_ERROR;
046 import static org.jetbrains.jet.cli.common.ExitCode.OK;
047
048 @SuppressWarnings("UseOfSystemOutOrSystemErr")
049 public class K2JVMCompiler extends CLICompiler<K2JVMCompilerArguments> {
050
051 public static void main(String... args) {
052 doMain(new K2JVMCompiler(), args);
053 }
054
055 @Override
056 @NotNull
057 protected ExitCode doExecute(
058 @NotNull K2JVMCompilerArguments arguments,
059 @NotNull MessageCollector messageCollector,
060 @NotNull Disposable rootDisposable
061 ) {
062 KotlinPaths paths = arguments.kotlinHome != null
063 ? new KotlinPathsFromHomeDir(new File(arguments.kotlinHome))
064 : PathUtil.getKotlinPathsForCompiler();
065
066 messageCollector.report(CompilerMessageSeverity.LOGGING,
067 "Using Kotlin home directory " + paths.getHomePath(), CompilerMessageLocation.NO_LOCATION);
068
069 CompilerConfiguration configuration = new CompilerConfiguration();
070
071 try {
072 configuration.addAll(JVMConfigurationKeys.CLASSPATH_KEY, getClasspath(paths, arguments));
073 configuration.addAll(JVMConfigurationKeys.ANNOTATIONS_PATH_KEY, getAnnotationsPath(paths, arguments));
074 }
075 catch (Throwable t) {
076 MessageCollectorUtil.reportException(messageCollector, t);
077 return INTERNAL_ERROR;
078 }
079
080 if (!arguments.script &&
081 arguments.module == null &&
082 arguments.src == null &&
083 arguments.freeArgs.isEmpty()
084 ) {
085 ReplFromTerminal.run(rootDisposable, configuration);
086 return ExitCode.OK;
087 }
088 else if (arguments.module != null) {
089 }
090 else if (arguments.script) {
091 configuration.add(CommonConfigurationKeys.SOURCE_ROOTS_KEY, arguments.freeArgs.get(0));
092 }
093 else {
094 if (arguments.src != null) {
095 List<String> sourcePathsSplitByPathSeparator
096 = Arrays.asList(arguments.src.split(StringUtil.escapeToRegexp(File.pathSeparator)));
097 configuration.addAll(CommonConfigurationKeys.SOURCE_ROOTS_KEY, sourcePathsSplitByPathSeparator);
098 }
099 for (String freeArg : arguments.freeArgs) {
100 configuration.add(CommonConfigurationKeys.SOURCE_ROOTS_KEY, freeArg);
101 }
102 }
103
104 configuration.put(JVMConfigurationKeys.SCRIPT_PARAMETERS, arguments.script
105 ? CommandLineScriptUtils.scriptParameters()
106 : Collections.<AnalyzerScriptParameter>emptyList());
107
108 configuration.put(JVMConfigurationKeys.GENERATE_NOT_NULL_ASSERTIONS, arguments.notNullAssertions);
109 configuration.put(JVMConfigurationKeys.GENERATE_NOT_NULL_PARAMETER_ASSERTIONS, arguments.notNullParamAssertions);
110
111 configuration.put(CLIConfigurationKeys.MESSAGE_COLLECTOR_KEY, messageCollector);
112
113 messageCollector.report(CompilerMessageSeverity.LOGGING, "Configuring the compilation environment",
114 CompilerMessageLocation.NO_LOCATION);
115 try {
116 configureEnvironment(configuration, arguments);
117
118 File jar = arguments.jar != null ? new File(arguments.jar) : null;
119 File outputDir = arguments.outputDir != null ? new File(arguments.outputDir) : null;
120
121 if (arguments.module != null) {
122 MessageCollector sanitizedCollector = new FilteringMessageCollector(messageCollector, in(CompilerMessageSeverity.VERBOSE));
123 ModuleChunk modules = CompileEnvironmentUtil.loadModuleDescriptions(paths, arguments.module, sanitizedCollector);
124
125 if (outputDir != null) {
126 messageCollector.report(CompilerMessageSeverity.WARNING, "The '-output' option is ignored because '-module' is specified",
127 CompilerMessageLocation.NO_LOCATION);
128 }
129
130 File directory = new File(arguments.module).getAbsoluteFile().getParentFile();
131 KotlinToJVMBytecodeCompiler.compileModules(configuration, modules,
132 directory, jar,
133 arguments.includeRuntime);
134 }
135 else if (arguments.script) {
136 List<String> scriptArgs = arguments.freeArgs.subList(1, arguments.freeArgs.size());
137 JetCoreEnvironment environment = JetCoreEnvironment.createForProduction(rootDisposable, configuration);
138 KotlinToJVMBytecodeCompiler.compileAndExecuteScript(paths, environment, scriptArgs);
139 }
140 else {
141 JetCoreEnvironment environment = JetCoreEnvironment.createForProduction(rootDisposable, configuration);
142 KotlinToJVMBytecodeCompiler.compileBunchOfSources(environment, jar, outputDir, arguments.includeRuntime);
143 }
144 return OK;
145 }
146 catch (CompilationException e) {
147 messageCollector.report(CompilerMessageSeverity.EXCEPTION, MessageRenderer.PLAIN.renderException(e),
148 MessageUtil.psiElementToMessageLocation(e.getElement()));
149 return INTERNAL_ERROR;
150 }
151 }
152
153
154 /**
155 * Allow derived classes to add additional command line arguments
156 */
157 @NotNull
158 @Override
159 protected K2JVMCompilerArguments createArguments() {
160 return new K2JVMCompilerArguments();
161 }
162
163 @NotNull
164 private static List<File> getClasspath(@NotNull KotlinPaths paths, @NotNull K2JVMCompilerArguments arguments) {
165 List<File> classpath = Lists.newArrayList();
166 if (!arguments.noJdk) {
167 classpath.add(PathUtil.findRtJar());
168 }
169 if (!arguments.noStdlib) {
170 classpath.add(paths.getRuntimePath());
171 }
172 if (arguments.classpath != null) {
173 for (String element : Splitter.on(File.pathSeparatorChar).split(arguments.classpath)) {
174 classpath.add(new File(element));
175 }
176 }
177 return classpath;
178 }
179
180 @NotNull
181 private static List<File> getAnnotationsPath(@NotNull KotlinPaths paths, @NotNull K2JVMCompilerArguments arguments) {
182 List<File> annotationsPath = Lists.newArrayList();
183 if (!arguments.noJdkAnnotations) {
184 annotationsPath.add(paths.getJdkAnnotationsPath());
185 }
186 if (arguments.annotations != null) {
187 for (String element : Splitter.on(File.pathSeparatorChar).split(arguments.annotations)) {
188 annotationsPath.add(new File(element));
189 }
190 }
191 return annotationsPath;
192 }
193 }