support multiple source roots

This commit is contained in:
develar
2012-06-22 13:21:49 +04:00
parent 63e41b771f
commit 9443007dc3
4 changed files with 33 additions and 38 deletions
@@ -65,21 +65,15 @@ public class K2JSCompiler extends CLICompiler<K2JSCompilerArguments, K2JSCompile
@NotNull @NotNull
@Override @Override
protected ExitCode doExecute(K2JSCompilerArguments arguments, PrintingMessageCollector messageCollector, Disposable rootDisposable) { protected ExitCode doExecute(K2JSCompilerArguments arguments, PrintingMessageCollector messageCollector, Disposable rootDisposable) {
if (arguments.sourceFiles == null) {
if (arguments.srcdir == null && arguments.sourceFiles == null) { messageCollector.report(CompilerMessageSeverity.ERROR, "Specify sources location via -sourceFiles", NO_LOCATION);
messageCollector.report(CompilerMessageSeverity.ERROR, "Specify sources location via -srcdir", NO_LOCATION);
return ExitCode.INTERNAL_ERROR; return ExitCode.INTERNAL_ERROR;
} }
JetCoreEnvironment environmentForJS = JetCoreEnvironment.getCoreEnvironmentForJS(rootDisposable); JetCoreEnvironment environmentForJS = JetCoreEnvironment.getCoreEnvironmentForJS(rootDisposable);
if (arguments.srcdir != null) { for (String sourceFile : arguments.sourceFiles) {
environmentForJS.addSources(arguments.srcdir); environmentForJS.addSources(sourceFile);
}
if (arguments.sourceFiles != null) {
for (String sourceFile : arguments.sourceFiles) {
environmentForJS.addSources(sourceFile);
}
} }
Project project = environmentForJS.getProject(); Project project = environmentForJS.getProject();
@@ -21,7 +21,6 @@ import org.jetbrains.jet.cli.common.CompilerArguments;
import org.jetbrains.k2js.facade.MainCallParameters; import org.jetbrains.k2js.facade.MainCallParameters;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List;
/** /**
* @author Pavel Talanov * @author Pavel Talanov
@@ -29,7 +28,7 @@ import java.util.List;
/** /**
* NOTE: for now K2JSCompiler supports only minimal amount of parameters required to launch it from the plugin. * NOTE: for now K2JSCompiler supports only minimal amount of parameters required to launch it from the plugin.
* You can specify only one source folder, path to the file where generated file will be stored, path to zipped library sources. * You can specify path to the file where generated file will be stored, path to zipped library sources.
*/ */
public class K2JSCompilerArguments extends CompilerArguments { public class K2JSCompilerArguments extends CompilerArguments {
@Argument(value = "output", description = "Output file path") @Argument(value = "output", description = "Output file path")
@@ -39,8 +38,8 @@ public class K2JSCompilerArguments extends CompilerArguments {
@Argument(value = "libraryFiles", description = "Path to zipped lib sources or kotlin files") @Argument(value = "libraryFiles", description = "Path to zipped lib sources or kotlin files")
public String[] libraryFiles; public String[] libraryFiles;
@Argument(value = "srcdir", description = "Sources directory") @Argument(value = "sourceFiles", description = "Source files (dir or file)")
public String srcdir; public String[] sourceFiles;
@Argument(value = "target", description = "Generate js files for specific ECMA version (3 or 5, default ECMA 3)") @Argument(value = "target", description = "Generate js files for specific ECMA version (3 or 5, default ECMA 3)")
public String target; public String target;
@@ -60,8 +59,6 @@ public class K2JSCompilerArguments extends CompilerArguments {
@Argument(value = "help", alias = "h", description = "Show help") @Argument(value = "help", alias = "h", description = "Show help")
public boolean help; public boolean help;
public List<String> sourceFiles;
@Override @Override
public boolean isHelp() { public boolean isHelp() {
return help; return help;
@@ -84,10 +81,7 @@ public class K2JSCompilerArguments extends CompilerArguments {
@Override @Override
public String getSrc() { public String getSrc() {
if (sourceFiles != null) { throw new IllegalStateException();
return sourceFiles.toString();
}
return srcdir;
} }
public MainCallParameters createMainCallParameters() { public MainCallParameters createMainCallParameters() {
@@ -117,16 +117,9 @@ public final class K2JSCompiler implements TranslatingCompiler {
@NotNull @NotNull
private static Integer doExec(@NotNull CompileContext context, @NotNull CompilerEnvironment environment, @NotNull PrintStream out, private static Integer doExec(@NotNull CompileContext context, @NotNull CompilerEnvironment environment, @NotNull PrintStream out,
@NotNull Module module) throws Exception { @NotNull Module module) throws Exception {
VirtualFile[] roots = ModuleRootManager.getInstance(module).getSourceRoots();
if (roots.length != 1) {
context.addMessage(CompilerMessageCategory.ERROR, "K2JSCompiler does not support multiple module source roots.", null, -1, -1);
return -1;
}
VirtualFile outDir = context.getModuleOutputDirectory(module); VirtualFile outDir = context.getModuleOutputDirectory(module);
String outFile = outDir == null ? null : outDir.getPath() + "/" + module.getName() + ".js"; String outFile = outDir == null ? null : outDir.getPath() + "/" + module.getName() + ".js";
String[] commandLineArgs = constructArguments(module, outFile);
String[] commandLineArgs = constructArguments(module, outFile, roots[0]);
Object rc = invokeExecMethod(environment, out, context, commandLineArgs, "org.jetbrains.jet.cli.js.K2JSCompiler"); Object rc = invokeExecMethod(environment, out, context, commandLineArgs, "org.jetbrains.jet.cli.js.K2JSCompiler");
if (outDir != null && !ApplicationManager.getApplication().isUnitTestMode()) { if (outDir != null && !ApplicationManager.getApplication().isUnitTestMode()) {
@@ -136,15 +129,16 @@ public final class K2JSCompiler implements TranslatingCompiler {
} }
@NotNull @NotNull
private static String[] constructArguments(@NotNull Module module, @Nullable String outFile, @NotNull VirtualFile srcDir) { private static String[] constructArguments(@NotNull Module module, @Nullable String outFile) {
ArrayList<String> args = Lists.newArrayList("-tags", "-verbose", "-version", "-mainCall", "mainWithArgs"); ArrayList<String> args = Lists.newArrayList("-tags", "-verbose", "-version", "-mainCall", "mainWithArgs");
addPathToSourcesDir(args, srcDir); addPathToSourcesDir(module, args);
addOutputPath(outFile, args); addOutputPath(outFile, args);
addLibLocationAndTarget(module, args); addLibLocationAndTarget(module, args);
return ArrayUtil.toStringArray(args); return ArrayUtil.toStringArray(args);
} }
// we cannot use OrderEnumerator because it has critical bug — try https://gist.github.com/2953261, processor will never be called for module dependency // we cannot use OrderEnumerator because it has critical bug — try https://gist.github.com/2953261, processor will never be called for module dependency
// we don't use context.getCompileScope().getAffectedModules() because we want to know about linkage type (well, we ignore scope right now, but in future...)
private static void collectModuleDependencies(Module dependentModule, Set<Module> modules) { private static void collectModuleDependencies(Module dependentModule, Set<Module> modules) {
for (OrderEntry entry : ModuleRootManager.getInstance(dependentModule).getOrderEntries()) { for (OrderEntry entry : ModuleRootManager.getInstance(dependentModule).getOrderEntries()) {
if (entry instanceof ModuleOrderEntry) { if (entry instanceof ModuleOrderEntry) {
@@ -165,6 +159,11 @@ public final class K2JSCompiler implements TranslatingCompiler {
} }
} }
private static VirtualFile[] getSourceFiles(@NotNull Module module) {
return CompilerManager.getInstance(module.getProject()).createModuleCompileScope(module, false)
.getFiles(JetFileType.INSTANCE, true);
}
private static void addLibLocationAndTarget(@NotNull Module module, @NotNull ArrayList<String> args) { private static void addLibLocationAndTarget(@NotNull Module module, @NotNull ArrayList<String> args) {
Pair<String[], String> libLocationAndTarget = JsModuleDetector.getLibLocationAndTargetForProject(module); Pair<String[], String> libLocationAndTarget = JsModuleDetector.getLibLocationAndTargetForProject(module);
@@ -176,9 +175,8 @@ public final class K2JSCompiler implements TranslatingCompiler {
if (!modules.isEmpty()) { if (!modules.isEmpty()) {
for (Module dependency : modules) { for (Module dependency : modules) {
sb.append('@').append(dependency.getName()).append(','); sb.append('@').append(dependency.getName()).append(',');
VirtualFile[] files = CompilerManager.getInstance(module.getProject()).createModuleCompileScope(dependency, false)
.getFiles(JetFileType.INSTANCE, true); for (VirtualFile file : getSourceFiles(dependency)) {
for (VirtualFile file : files) {
sb.append(file.getPath()).append(','); sb.append(file.getPath()).append(',');
} }
} }
@@ -206,10 +204,19 @@ public final class K2JSCompiler implements TranslatingCompiler {
} }
} }
private static void addPathToSourcesDir(@NotNull ArrayList<String> args, @NotNull VirtualFile srcDir) { private static void addPathToSourcesDir(@NotNull Module module, @NotNull ArrayList<String> args) {
String srcPath = srcDir.getPath(); args.add("-sourceFiles");
args.add("-srcdir");
args.add(srcPath); StringBuilder sb = StringBuilderSpinAllocator.alloc();
try {
for (VirtualFile file : getSourceFiles(module)) {
sb.append(file.getPath()).append(',');
}
args.add(sb.substring(0, sb.length() - 1));
}
finally {
StringBuilderSpinAllocator.dispose(sb);
}
} }
private static void addOutputPath(@Nullable String outFile, @NotNull ArrayList<String> args) { private static void addOutputPath(@Nullable String outFile, @NotNull ArrayList<String> args) {
@@ -79,7 +79,7 @@ abstract class StdLibTestSupport extends SingleFileTranslationTest {
K2JSCompiler compiler = new K2JSCompiler(); K2JSCompiler compiler = new K2JSCompiler();
K2JSCompilerArguments arguments = new K2JSCompilerArguments(); K2JSCompilerArguments arguments = new K2JSCompilerArguments();
arguments.outputFile = getOutputFilePath(getTestName(false) + ".compiler.kt", version); arguments.outputFile = getOutputFilePath(getTestName(false) + ".compiler.kt", version);
arguments.sourceFiles = files; arguments.sourceFiles = files.toArray(new String[files.size()]);
arguments.verbose = true; arguments.verbose = true;
System.out.println("Compiling with version: " + version + " to: " + arguments.outputFile); System.out.println("Compiling with version: " + version + " to: " + arguments.outputFile);
ExitCode answer = compiler.exec(System.out, arguments); ExitCode answer = compiler.exec(System.out, arguments);