summaryrefslogtreecommitdiff
path: root/support/ant/src.java/scala/tools/scalac4ant/AntAdaptor.java
blob: bb682cf81591db2ac32f59174520814734684648 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
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);
        }
    }
    */
}