diff options
Diffstat (limited to 'sources')
-rw-r--r-- | sources/scala/tools/scala4ant/AntAdaptor.java | 105 | ||||
-rw-r--r-- | sources/scala/tools/scala4ant/AntTask.java | 123 |
2 files changed, 228 insertions, 0 deletions
diff --git a/sources/scala/tools/scala4ant/AntAdaptor.java b/sources/scala/tools/scala4ant/AntAdaptor.java new file mode 100644 index 0000000000..bb682cf815 --- /dev/null +++ b/sources/scala/tools/scala4ant/AntAdaptor.java @@ -0,0 +1,105 @@ +package scala.tools.scalac4ant; + +import scalac.*; +import scalac.util.Reporter; + +import org.apache.tools.ant.BuildException; +import org.apache.tools.ant.Project; +import org.apache.tools.ant.taskdefs.Execute; +import org.apache.tools.ant.taskdefs.Javac; +import org.apache.tools.ant.taskdefs.LogStreamHandler; +import org.apache.tools.ant.taskdefs.compilers.DefaultCompilerAdapter; +import org.apache.tools.ant.types.Commandline; +import org.apache.tools.ant.types.Path; +import java.io.IOException; + + +/** a compiler adaptor for Scalac. + * + * author: Burak Emir + * adapted from package jaco.framework.ant.AntCompilerAdaptor + * (part of Matthias Zenger's jaco framework) + */ + +public class AntAdaptor extends DefaultCompilerAdapter { + + private String source; + private String target; + + public static final String PRODUCT = + System.getProperty("scala.product", "scalac"); + public static final String VERSION = + System.getProperty("scala.version", "unknown version"); + + + public boolean runCompiler(String[] args) { + // dirty work, to get rid of debugging (-g) option, set in setupJavac... + String[] nargs = new String[ args.length - 1 ]; + int j = 0; + for( int i = 0; i<args.length; i++ ) + if( !args[ i ].startsWith("-g") ) { + //System.err.print( args[ i ] +" ") + nargs[ j++ ] = args[ i ]; + } + // compile + Reporter reporter = new Reporter(); + CompilerCommand command = new CompilerCommand( + PRODUCT, VERSION, reporter, new CompilerPhases()); + if (command.parse(nargs) && command.files.list.size() > 0) { + Global global = new Global(command); + try { + global.compile(command.files.toArray(), false); + } catch (Throwable e) { + e.printStackTrace(); + //throw new BuildException(e.message()); + return false; + } + global.stop("total"); + global.reporter.printSummary(); + /* + PizzaSettings js = new PizzaSettings(); + js.parse(args); + return js.JavaContext().JavaCompiler().compile(); + */ + } + return true; + + } + + public String compilerName() { + return "scalac"; + } + + public void setJavac(Javac attributes) { + super.setJavac(attributes); + AntTask myattribs = (AntTask)attributes; + source = myattribs.getSource(); + target = myattribs.getTarget(); + } + + public boolean execute() throws BuildException { + attributes.log("Using " + compilerName() + " as scala compiler", + Project.MSG_VERBOSE); + return runCompiler(setupScalacCommand().getArguments()); + } + + public Commandline setupScalacCommand() { + Commandline cmd = new Commandline(); + setupJavacCommandlineSwitches(cmd); + //setupScalacCommandlineSwitches(cmd); + logAndAddFilesToCompile(cmd); + return cmd; + } + /* + public void setupScalacCommandlineSwitches(Commandline cmd) { + if (source != null) { + cmd.createArgument().setValue("-source"); + cmd.createArgument().setValue(source); + } + if (target != null) { + cmd.createArgument().setValue("-target"); + cmd.createArgument().setValue(target); + } + } + */ +} diff --git a/sources/scala/tools/scala4ant/AntTask.java b/sources/scala/tools/scala4ant/AntTask.java new file mode 100644 index 0000000000..8ec8e363d5 --- /dev/null +++ b/sources/scala/tools/scala4ant/AntTask.java @@ -0,0 +1,123 @@ +package scala.tools.scalac4ant; + +import org.apache.tools.ant.BuildException; +import org.apache.tools.ant.taskdefs.Javac; +import org.apache.tools.ant.util.*; +import java.io.File; +import java.util.*; + + +/** Scala AntTask. + * + * author: Burak Emir + * adapted from package jaco.framework.ant.AntCompilerTask + * (part of Matthias Zenger's jaco framework) + */ + + +public class AntTask extends Javac { + + private String suffixes = "scala"; + private boolean force = false; + private String source = null; + private String target = null; + private FileUtils fileUtils = FileUtils.newFileUtils(); + + + public void execute() { + String old = project.getProperty("build.compiler"); + project.setProperty("build.compiler", "scala.tools.scalac4ant.AntAdaptor"); + super.execute(); + if (old == null) + project.setProperty("build.compiler", "modern"); + else + project.setProperty("build.compiler", old); + } + + public void setForce(boolean fc) { + force = fc; + } + + public boolean getForce() { + return force; + } + + public void setSource(String source) { + this.source = source; + } + + public String getSource() { + return source; + } + + public void setTarget(String target) { + this.target = target; + } + + public String getTarget() { + return target; + } + + public void setSuffixes(String s) { + suffixes = s; + } + + public String getSuffixes() { + return suffixes; + } + + protected String[] parseSuffixes() { + StringTokenizer st = new StringTokenizer(suffixes, " ,"); + ArrayList al = new ArrayList(); + while (st.hasMoreTokens()) + al.add("." + st.nextToken()); + return (String[])al.toArray(new String[al.size()]); + } + + private static boolean hasLegalSuffix(String[] suffixes, String file) { + for (int i = 0; i < suffixes.length; i++) + if (file.endsWith(suffixes[i])) + return true; + return false; + } + + protected void scanDir(File srcDir, File destDir, String files[]) { + String[] sfx = parseSuffixes(); + if (force) { + File[] newCompileList = new File[compileList.length + files.length]; + System.arraycopy(compileList, 0, newCompileList, 0, compileList.length); + int j = 0; + for (int i = 0; i < files.length; i++) + if (hasLegalSuffix(sfx, files[i])) { + if (srcDir == null) + newCompileList[compileList.length + (j++)] = new File(files[i]); + else + newCompileList[compileList.length + (j++)] = + fileUtils.resolveFile(srcDir, files[i]); + } + if (j == files.length) + compileList = newCompileList; + else { + compileList = new File[j]; + System.arraycopy(newCompileList, 0, compileList, 0, j); + } + } else { + GlobPatternMapper m = new GlobPatternMapper(); + for (int i = 0; i < sfx.length; i++) { + m.setFrom("*" + sfx[i]); + m.setTo("*.class"); + SourceFileScanner sfs = new SourceFileScanner(this); + File[] newFiles = sfs.restrictAsFiles(files, srcDir, destDir, m); + if (newFiles.length > 0) { + File[] newCompileList = new File[compileList.length + + newFiles.length]; + System.arraycopy(compileList, 0, newCompileList, 0, + compileList.length); + System.arraycopy(newFiles, 0, newCompileList, + compileList.length, newFiles.length); + compileList = newCompileList; + } + } + } + } +} |