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.CompilerArgumentsUtil;
028 import org.jetbrains.jet.cli.common.arguments.K2JVMCompilerArguments;
029 import org.jetbrains.jet.cli.common.messages.*;
030 import org.jetbrains.jet.cli.jvm.compiler.*;
031 import org.jetbrains.jet.cli.jvm.repl.ReplFromTerminal;
032 import org.jetbrains.jet.codegen.CompilationException;
033 import org.jetbrains.jet.codegen.inline.InlineCodegenUtil;
034 import org.jetbrains.jet.config.CommonConfigurationKeys;
035 import org.jetbrains.jet.config.CompilerConfiguration;
036 import org.jetbrains.jet.lang.resolve.AnalyzerScriptParameter;
037 import org.jetbrains.jet.utils.KotlinPaths;
038 import org.jetbrains.jet.utils.KotlinPathsFromHomeDir;
039 import org.jetbrains.jet.utils.PathUtil;
040
041 import java.io.File;
042 import java.util.Arrays;
043 import java.util.Collections;
044 import java.util.List;
045
046 import static com.google.common.base.Predicates.in;
047 import static org.jetbrains.jet.cli.common.ExitCode.INTERNAL_ERROR;
048 import static org.jetbrains.jet.cli.common.ExitCode.OK;
049
050 @SuppressWarnings("UseOfSystemOutOrSystemErr")
051 public class K2JVMCompiler extends CLICompiler<K2JVMCompilerArguments> {
052
053 public static void main(String... args) {
054 doMain(new K2JVMCompiler(), args);
055 }
056
057 @Override
058 @NotNull
059 protected ExitCode doExecute(
060 @NotNull K2JVMCompilerArguments arguments,
061 @NotNull MessageCollector messageCollector,
062 @NotNull Disposable rootDisposable
063 ) {
064 KotlinPaths paths = arguments.kotlinHome != null
065 ? new KotlinPathsFromHomeDir(new File(arguments.kotlinHome))
066 : PathUtil.getKotlinPathsForCompiler();
067
068 messageCollector.report(CompilerMessageSeverity.LOGGING,
069 "Using Kotlin home directory " + paths.getHomePath(), CompilerMessageLocation.NO_LOCATION);
070
071 CompilerConfiguration configuration = new CompilerConfiguration();
072
073 try {
074 configuration.addAll(JVMConfigurationKeys.CLASSPATH_KEY, getClasspath(paths, arguments));
075 configuration.addAll(JVMConfigurationKeys.ANNOTATIONS_PATH_KEY, getAnnotationsPath(paths, arguments));
076 }
077 catch (Throwable t) {
078 MessageCollectorUtil.reportException(messageCollector, t);
079 return INTERNAL_ERROR;
080 }
081
082 if (!arguments.script &&
083 arguments.module == null &&
084 arguments.src == null &&
085 arguments.freeArgs.isEmpty()
086 ) {
087 ReplFromTerminal.run(rootDisposable, configuration);
088 return ExitCode.OK;
089 }
090 else if (arguments.module != null) {
091 }
092 else if (arguments.script) {
093 configuration.add(CommonConfigurationKeys.SOURCE_ROOTS_KEY, arguments.freeArgs.get(0));
094 }
095 else {
096 if (arguments.src != null) {
097 List<String> sourcePathsSplitByPathSeparator
098 = Arrays.asList(arguments.src.split(StringUtil.escapeToRegexp(File.pathSeparator)));
099 configuration.addAll(CommonConfigurationKeys.SOURCE_ROOTS_KEY, sourcePathsSplitByPathSeparator);
100 }
101 for (String freeArg : arguments.freeArgs) {
102 configuration.add(CommonConfigurationKeys.SOURCE_ROOTS_KEY, freeArg);
103 }
104 }
105
106 configuration.put(JVMConfigurationKeys.SCRIPT_PARAMETERS, arguments.script
107 ? CommandLineScriptUtils.scriptParameters()
108 : Collections.<AnalyzerScriptParameter>emptyList());
109
110 configuration.put(JVMConfigurationKeys.GENERATE_NOT_NULL_ASSERTIONS, arguments.notNullAssertions);
111 configuration.put(JVMConfigurationKeys.GENERATE_NOT_NULL_PARAMETER_ASSERTIONS, arguments.notNullParamAssertions);
112 configuration.put(JVMConfigurationKeys.ENABLE_INLINE,
113 CompilerArgumentsUtil.optionToBooleanFlag(arguments.inline, InlineCodegenUtil.DEFAULT_INLINE_FLAG));
114
115 configuration.put(CLIConfigurationKeys.MESSAGE_COLLECTOR_KEY, messageCollector);
116
117 messageCollector.report(CompilerMessageSeverity.LOGGING, "Configuring the compilation environment",
118 CompilerMessageLocation.NO_LOCATION);
119 try {
120 configureEnvironment(configuration, arguments);
121
122 File jar = arguments.jar != null ? new File(arguments.jar) : null;
123 File outputDir = arguments.outputDir != null ? new File(arguments.outputDir) : null;
124
125 if (arguments.module != null) {
126 MessageCollector sanitizedCollector = new FilteringMessageCollector(messageCollector, in(CompilerMessageSeverity.VERBOSE));
127 ModuleChunk modules = CompileEnvironmentUtil.loadModuleDescriptions(paths, arguments.module, sanitizedCollector);
128
129 if (outputDir != null) {
130 messageCollector.report(CompilerMessageSeverity.WARNING, "The '-output' option is ignored because '-module' is specified",
131 CompilerMessageLocation.NO_LOCATION);
132 }
133
134 File directory = new File(arguments.module).getAbsoluteFile().getParentFile();
135 KotlinToJVMBytecodeCompiler.compileModules(configuration, modules,
136 directory, jar,
137 arguments.includeRuntime);
138 }
139 else if (arguments.script) {
140 List<String> scriptArgs = arguments.freeArgs.subList(1, arguments.freeArgs.size());
141 JetCoreEnvironment environment = JetCoreEnvironment.createForProduction(rootDisposable, configuration);
142 KotlinToJVMBytecodeCompiler.compileAndExecuteScript(paths, environment, scriptArgs);
143 }
144 else {
145 JetCoreEnvironment environment = JetCoreEnvironment.createForProduction(rootDisposable, configuration);
146 KotlinToJVMBytecodeCompiler.compileBunchOfSources(environment, jar, outputDir, arguments.includeRuntime);
147 }
148 return OK;
149 }
150 catch (CompilationException e) {
151 messageCollector.report(CompilerMessageSeverity.EXCEPTION, MessageRenderer.PLAIN.renderException(e),
152 MessageUtil.psiElementToMessageLocation(e.getElement()));
153 return INTERNAL_ERROR;
154 }
155 }
156
157
158 /**
159 * Allow derived classes to add additional command line arguments
160 */
161 @NotNull
162 @Override
163 protected K2JVMCompilerArguments createArguments() {
164 return new K2JVMCompilerArguments();
165 }
166
167 @NotNull
168 private static List<File> getClasspath(@NotNull KotlinPaths paths, @NotNull K2JVMCompilerArguments arguments) {
169 List<File> classpath = Lists.newArrayList();
170 if (!arguments.noJdk) {
171 classpath.addAll(PathUtil.getJdkClassesRoots());
172 }
173 if (!arguments.noStdlib) {
174 classpath.add(paths.getRuntimePath());
175 }
176 if (arguments.classpath != null) {
177 for (String element : Splitter.on(File.pathSeparatorChar).split(arguments.classpath)) {
178 classpath.add(new File(element));
179 }
180 }
181 return classpath;
182 }
183
184 @NotNull
185 private static List<File> getAnnotationsPath(@NotNull KotlinPaths paths, @NotNull K2JVMCompilerArguments arguments) {
186 List<File> annotationsPath = Lists.newArrayList();
187 if (!arguments.noJdkAnnotations) {
188 annotationsPath.add(paths.getJdkAnnotationsPath());
189 }
190 if (arguments.annotations != null) {
191 for (String element : Splitter.on(File.pathSeparatorChar).split(arguments.annotations)) {
192 annotationsPath.add(new File(element));
193 }
194 }
195 return annotationsPath;
196 }
197
198 @Override
199 protected void checkArguments(@NotNull K2JVMCompilerArguments argument) {
200 super.checkArguments(argument);
201
202 if (!CompilerArgumentsUtil.checkOption(argument.inline)) {
203 throw new IllegalArgumentException(CompilerArgumentsUtil.getWrongInlineOptionErrorMessage(argument.inline));
204 }
205 }
206
207 }