001: package antlr.build;
002:
003: import java.io.*;
004:
005: /** Simple class that uses build.Tool to compile ANTLR's Java stuff */
006: public class ANTLR {
007: public static String compiler = "javac";
008: public static String jarName = "antlr.jar";
009: public static String root = ".";
010:
011: public static String[] srcdir = { "antlr", "antlr/actions/cpp",
012: "antlr/actions/java", "antlr/actions/csharp",
013: "antlr/collections", "antlr/collections/impl",
014: "antlr/debug", "antlr/ASdebug", "antlr/debug/misc",
015: "antlr/preprocessor" };
016:
017: public ANTLR() {
018: compiler = System.getProperty("antlr.build.compiler", compiler);
019: root = System.getProperty("antlr.build.root", root);
020: }
021:
022: public String getName() {
023: return "ANTLR";
024: }
025:
026: /** Build ANTLR. action on cmd-line matches method name */
027: public void build(Tool tool) {
028: if (!rootIsValidANTLRDir(tool)) {
029: return;
030: }
031: // run ANTLR on its own .g files
032: tool.antlr(root + "/antlr/antlr.g");
033: tool.antlr(root + "/antlr/tokdef.g");
034: tool.antlr(root + "/antlr/preprocessor/preproc.g");
035: tool.antlr(root + "/antlr/actions/java/action.g");
036: tool.antlr(root + "/antlr/actions/cpp/action.g");
037: tool.antlr(root + "/antlr/actions/csharp/action.g");
038: for (int i = 0; i < srcdir.length; i++) {
039: String cmd = compiler + " -d " + root + " " + root + "/"
040: + srcdir[i] + "/*.java";
041: tool.system(cmd);
042: }
043: }
044:
045: /** Jar up all the .class files */
046: public void jar(Tool tool) {
047: if (!rootIsValidANTLRDir(tool)) {
048: return;
049: }
050: StringBuffer cmd = new StringBuffer(2000);
051: cmd.append("jar cvf " + root + "/" + jarName);
052: for (int i = 0; i < srcdir.length; i++) {
053: cmd.append(" " + root + "/" + srcdir[i] + "/*.class");
054: }
055: tool.system(cmd.toString());
056: }
057:
058: /** ANTLR root dir must contain an "antlr" dir and must have java
059: * files underneath etc...
060: */
061: protected boolean rootIsValidANTLRDir(Tool tool) {
062: if (root == null) {
063: return false;
064: }
065: File antlrRootDir = new File(root);
066: if (!antlrRootDir.exists()) {
067: tool.error("Property antlr.build.root==" + root
068: + " does not exist");
069: return false;
070: }
071: if (!antlrRootDir.isDirectory()) {
072: tool.error("Property antlr.build.root==" + root
073: + " is not a directory");
074: return false;
075: }
076: String[] antlrDir = antlrRootDir.list(new FilenameFilter() {
077: public boolean accept(File dir, String name) {
078: return dir.isDirectory() && name.equals("antlr");
079: }
080: });
081: if (antlrDir == null || antlrDir.length == 0) {
082: tool
083: .error("Property antlr.build.root=="
084: + root
085: + " does not appear to be a valid ANTLR project root (no antlr subdir)");
086: return false;
087: }
088: File antlrPackageDir = new File(root + "/antlr");
089: String[] antlrPackageJavaFiles = antlrPackageDir.list();
090: if (antlrPackageJavaFiles == null
091: || antlrPackageJavaFiles.length == 0) {
092: tool
093: .error("Property antlr.build.root=="
094: + root
095: + " does not appear to be a valid ANTLR project root (no .java files in antlr subdir");
096: return false;
097: }
098: return true;
099: }
100: }
|