Add nested tag "externalannotations" to tags "withKotlin" and "kotlinc"

This commit is contained in:
Erokhin Stanislav
2013-10-25 19:10:07 +04:00
parent 35bc3cd587
commit 694eed595b
3 changed files with 54 additions and 12 deletions
@@ -43,6 +43,7 @@ public class BytecodeCompilerTask extends Task {
private File jar; private File jar;
private File stdlib; private File stdlib;
private Path src; private Path src;
private Path externalAnnotations;
private File module; private File module;
private Path compileClasspath; private Path compileClasspath;
private boolean includeRuntime = true; private boolean includeRuntime = true;
@@ -70,6 +71,17 @@ public class BytecodeCompilerTask extends Task {
return src.createPath(); return src.createPath();
} }
public void setExternalAnnotations(Path externalAnnotations) {
this.externalAnnotations = externalAnnotations;
}
public Path createExternalAnnotations() {
if (externalAnnotations == null) {
externalAnnotations = new Path(getProject());
}
return externalAnnotations.createPath();
}
public void setModule(File module) { public void setModule(File module) {
this.module = module; this.module = module;
} }
@@ -123,6 +135,7 @@ public class BytecodeCompilerTask extends Task {
BytecodeCompiler compiler = new BytecodeCompiler(); BytecodeCompiler compiler = new BytecodeCompiler();
String stdlibPath = (this.stdlib != null ? getPath(this.stdlib) : null); String stdlibPath = (this.stdlib != null ? getPath(this.stdlib) : null);
String[] classpath = (this.compileClasspath != null ? this.compileClasspath.list() : null); String[] classpath = (this.compileClasspath != null ? this.compileClasspath.list() : null);
String[] externalAnnotationsPath = (this.externalAnnotations != null) ? this.externalAnnotations.list() : null;
if (this.src != null) { if (this.src != null) {
@@ -136,10 +149,10 @@ public class BytecodeCompilerTask extends Task {
log(String.format("Compiling [%s] => [%s]", Arrays.toString(source), destination)); log(String.format("Compiling [%s] => [%s]", Arrays.toString(source), destination));
if (this.output != null) { if (this.output != null) {
compiler.sourcesToDir(source, destination, stdlibPath, classpath); compiler.sourcesToDir(source, destination, stdlibPath, classpath, externalAnnotationsPath);
} }
else { else {
compiler.sourcesToJar(source, destination, this.includeRuntime, stdlibPath, classpath); compiler.sourcesToJar(source, destination, this.includeRuntime, stdlibPath, classpath, externalAnnotationsPath);
} }
} }
else if (this.module != null) { else if (this.module != null) {
@@ -154,7 +167,7 @@ public class BytecodeCompilerTask extends Task {
log(jarPath != null ? String.format("Compiling [%s] => [%s]", modulePath, jarPath) : log(jarPath != null ? String.format("Compiling [%s] => [%s]", modulePath, jarPath) :
String.format("Compiling [%s]", modulePath)); String.format("Compiling [%s]", modulePath));
compiler.moduleToJar(modulePath, jarPath, this.includeRuntime, stdlibPath, classpath); compiler.moduleToJar(modulePath, jarPath, this.includeRuntime, stdlibPath, classpath, externalAnnotationsPath);
} }
else { else {
throw new CompileEnvironmentException("\"src\" or \"module\" should be specified"); throw new CompileEnvironmentException("\"src\" or \"module\" should be specified");
@@ -20,8 +20,22 @@ import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.taskdefs.Javac; import org.apache.tools.ant.taskdefs.Javac;
import org.apache.tools.ant.taskdefs.compilers.DefaultCompilerAdapter; import org.apache.tools.ant.taskdefs.compilers.DefaultCompilerAdapter;
import org.apache.tools.ant.taskdefs.compilers.Javac13; import org.apache.tools.ant.taskdefs.compilers.Javac13;
import org.apache.tools.ant.types.Path;
public class KotlinCompilerAdapter extends DefaultCompilerAdapter { public class KotlinCompilerAdapter extends DefaultCompilerAdapter {
private Path externalAnnotations;
public void setExternalAnnotations(Path externalAnnotations) {
this.externalAnnotations = externalAnnotations;
}
public Path createExternalAnnotations() {
if (externalAnnotations == null) {
externalAnnotations = new Path(getProject());
}
return externalAnnotations.createPath();
}
@Override @Override
public boolean execute() throws BuildException { public boolean execute() throws BuildException {
Javac javac = getJavac(); Javac javac = getJavac();
@@ -30,6 +44,7 @@ public class KotlinCompilerAdapter extends DefaultCompilerAdapter {
kotlinTask.setOutput(javac.getDestdir()); kotlinTask.setOutput(javac.getDestdir());
kotlinTask.setClasspath(javac.getClasspath()); kotlinTask.setClasspath(javac.getClasspath());
kotlinTask.setSrc(javac.getSrcdir()); kotlinTask.setSrc(javac.getSrcdir());
kotlinTask.setExternalAnnotations(externalAnnotations);
kotlinTask.execute(); kotlinTask.execute();
@@ -63,13 +63,16 @@ public class BytecodeCompiler {
* @param sourceRoots * @param sourceRoots
* @return compile environment instance * @return compile environment instance
*/ */
private JetCoreEnvironment env(String stdlib, String[] classpath, String[] sourceRoots) { private JetCoreEnvironment env(String stdlib, String[] classpath, String[] externalAnnotationsPath, String[] sourceRoots) {
CompilerConfiguration configuration = createConfiguration(stdlib, classpath, sourceRoots); CompilerConfiguration configuration = createConfiguration(stdlib, classpath, externalAnnotationsPath, sourceRoots);
return JetCoreEnvironment.createForProduction(Disposer.newDisposable(), configuration); return JetCoreEnvironment.createForProduction(Disposer.newDisposable(), configuration);
} }
private CompilerConfiguration createConfiguration(String stdlib, String[] classpath, String[] sourceRoots) { private CompilerConfiguration createConfiguration(@Nullable String stdlib,
@Nullable String[] classpath,
@Nullable String[] externalAnnotationsPath,
@NotNull String[] sourceRoots) {
KotlinPaths paths = getKotlinPathsForAntTask(); KotlinPaths paths = getKotlinPathsForAntTask();
CompilerConfiguration configuration = new CompilerConfiguration(); CompilerConfiguration configuration = new CompilerConfiguration();
configuration.add(CLASSPATH_KEY, PathUtil.findRtJar()); configuration.add(CLASSPATH_KEY, PathUtil.findRtJar());
@@ -87,6 +90,11 @@ public class BytecodeCompiler {
configuration.add(CLASSPATH_KEY, new File(path)); configuration.add(CLASSPATH_KEY, new File(path));
} }
} }
if ((externalAnnotationsPath != null) && (externalAnnotationsPath.length > 0)) {
for (String path : externalAnnotationsPath) {
configuration.add(ANNOTATIONS_PATH_KEY, new File(path));
}
}
File jdkAnnotationsPath = paths.getJdkAnnotationsPath(); File jdkAnnotationsPath = paths.getJdkAnnotationsPath();
if (jdkAnnotationsPath.exists()) { if (jdkAnnotationsPath.exists()) {
configuration.add(ANNOTATIONS_PATH_KEY, jdkAnnotationsPath); configuration.add(ANNOTATIONS_PATH_KEY, jdkAnnotationsPath);
@@ -141,9 +149,13 @@ public class BytecodeCompiler {
* @param stdlib "kotlin-runtime.jar" path * @param stdlib "kotlin-runtime.jar" path
* @param classpath compilation classpath, can be <code>null</code> or empty * @param classpath compilation classpath, can be <code>null</code> or empty
*/ */
public void sourcesToDir(@NotNull String[] src, @NotNull String output, @Nullable String stdlib, @Nullable String[] classpath) { public void sourcesToDir(@NotNull String[] src,
@NotNull String output,
@Nullable String stdlib,
@Nullable String[] classpath,
@Nullable String[] externalAnnotationsPath) {
try { try {
JetCoreEnvironment environment = env(stdlib, classpath, src); JetCoreEnvironment environment = env(stdlib, classpath, externalAnnotationsPath, src);
boolean success = KotlinToJVMBytecodeCompiler.compileBunchOfSources(environment, null, new File(output), true); boolean success = KotlinToJVMBytecodeCompiler.compileBunchOfSources(environment, null, new File(output), true);
if (!success) { if (!success) {
@@ -169,9 +181,10 @@ public class BytecodeCompiler {
@NotNull String jar, @NotNull String jar,
boolean includeRuntime, boolean includeRuntime,
@Nullable String stdlib, @Nullable String stdlib,
@Nullable String[] classpath) { @Nullable String[] classpath,
@Nullable String[] externalAnnotationsPath) {
try { try {
JetCoreEnvironment environment = env(stdlib, classpath, src); JetCoreEnvironment environment = env(stdlib, classpath, externalAnnotationsPath, src);
boolean success = KotlinToJVMBytecodeCompiler.compileBunchOfSources(environment, new File(jar), null, includeRuntime); boolean success = KotlinToJVMBytecodeCompiler.compileBunchOfSources(environment, new File(jar), null, includeRuntime);
if (!success) { if (!success) {
@@ -197,7 +210,8 @@ public class BytecodeCompiler {
@NotNull String jar, @NotNull String jar,
boolean includeRuntime, boolean includeRuntime,
@Nullable String stdlib, @Nullable String stdlib,
@Nullable String[] classpath) { @Nullable String[] classpath,
@Nullable String[] externalAnnotationsPath) {
try { try {
ModuleChunk modules = CompileEnvironmentUtil.loadModuleDescriptions(getKotlinPathsForAntTask(), module, ModuleChunk modules = CompileEnvironmentUtil.loadModuleDescriptions(getKotlinPathsForAntTask(), module,
MessageCollectorPlainTextToStream.PLAIN_TEXT_TO_SYSTEM_ERR); MessageCollectorPlainTextToStream.PLAIN_TEXT_TO_SYSTEM_ERR);
@@ -205,7 +219,7 @@ public class BytecodeCompiler {
for (Module m : modules.getModules()) { for (Module m : modules.getModules()) {
sourcesRoots.addAll(m.getSourceFiles()); sourcesRoots.addAll(m.getSourceFiles());
} }
CompilerConfiguration configuration = createConfiguration(stdlib, classpath, sourcesRoots.toArray(new String[0])); CompilerConfiguration configuration = createConfiguration(stdlib, classpath, externalAnnotationsPath, sourcesRoots.toArray(new String[0]));
File directory = new File(module).getParentFile(); File directory = new File(module).getParentFile();
boolean success = KotlinToJVMBytecodeCompiler.compileModules(configuration, modules, directory, new File(jar), includeRuntime); boolean success = KotlinToJVMBytecodeCompiler.compileModules(configuration, modules, directory, new File(jar), includeRuntime);
if (!success) { if (!success) {