001: /*
002: *
003: *
004: * Copyright 1990-2007 Sun Microsystems, Inc. All Rights Reserved.
005: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License version
009: * 2 only, as published by the Free Software Foundation.
010: *
011: * This program is distributed in the hope that it will be useful, but
012: * WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * General Public License version 2 for more details (a copy is
015: * included at /legal/license.txt).
016: *
017: * You should have received a copy of the GNU General Public License
018: * version 2 along with this work; if not, write to the Free Software
019: * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA
021: *
022: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
023: * Clara, CA 95054 or visit www.sun.com if you need additional
024: * information or have any questions.
025: */
026:
027: package mjpp;
028:
029: import java.io.*;
030: import java.util.*;
031: import util.*;
032:
033: /**
034: * Java preprocessor --
035: * Preprocess java source files to reduce footprint.
036: *
037: * See $(JVMWorkSpace)/internal_doc/BuildSystem.html for more
038: * information.
039: */
040: public class Main {
041: static void usage() {
042: System.out.println("Usage: java -jar buildtool.jar "
043: + "mjpp -srcroot <dir> -outdir <dir> files ...");
044: }
045:
046: static boolean do_strip = true;
047:
048: public static void main(String args[]) throws Throwable {
049: Vector roots = new Vector();
050: Vector files = new Vector();
051: Vector output = new Vector();
052: int i;
053:
054: String outdir = null;
055: for (i = 0; i < args.length;) {
056: if (args[i].equals("-srcroot")) {
057: roots.addElement(args[i + 1]);
058: i += 2;
059: } else if (args[i].equals("-outdir")) {
060: outdir = args[i + 1];
061: i += 2;
062: } else if (args[i].equals("-n")) {
063: do_strip = false;
064: i++;
065: } else {
066: files.addElement(args[i]);
067: i++;
068: }
069: }
070:
071: if (outdir == null) {
072: System.err.println("-outdir not specified");
073: System.exit(-1);
074: }
075: if (roots.size() == 0) {
076: System.err.println("-srcroot not specified");
077: System.exit(-1);
078: }
079:
080: process(outdir, roots, files, output);
081: for (i = 0; i < output.size(); i++) {
082: System.out.print(output.elementAt(i));
083: System.out.print(" ");
084: }
085: }
086:
087: public static void process(String outdir, Vector roots,
088: Vector files, Vector output) throws Throwable {
089: File file = new File(outdir);
090: if (!file.exists()) {
091: file.mkdirs();
092: }
093: for (int i = 0; i < files.size(); i++) {
094: String f = process(outdir, roots, (String) files
095: .elementAt(i));
096: output.addElement(f);
097: }
098: }
099:
100: static String process(String outdir, Vector roots, String file)
101: throws IOException {
102: String subdir = findSubDir(roots, file);
103: File dir = new File(outdir);
104: dir = new File(dir, subdir);
105: if (!dir.exists()) {
106: dir.mkdirs();
107: }
108:
109: File inf = new File(file);
110: File outf = new File(dir, inf.getName());
111: process(inf, outf);
112:
113: return outf.toString().replace('\\', '/');
114: }
115:
116: /*
117: * IMPL_NOTE: currently the preprocessor doesn't look at the
118: * parameter of the #{if,ifdef,ifndef} directive. It just
119: * blindly removes everything between #if ... #endif
120: */
121: static void process(File inf, File outf) throws IOException {
122: FileInputStream in = new FileInputStream(inf);
123: FileOutputStream out = new FileOutputStream(outf);
124: BufferedReader reader = new BufferedReader(
125: new InputStreamReader(in));
126: PrintWriter writer = new PrintWriter(
127: new OutputStreamWriter(out));
128: String s;
129:
130: boolean skipping = false;
131: String startPrefix = "/* #if";
132: String endPrefix = "/* #endif */";
133: int lineno = 0;
134: while ((s = reader.readLine()) != null) {
135: lineno++;
136: if (do_strip && s.startsWith(startPrefix)) {
137: if (skipping) {
138: System.err.println("Error in " + inf + ": "
139: + lineno);
140: System.err
141: .println("/* #ifxxx cannot be nested (yet)! */");
142: writer.close();
143: System.exit(1);
144: } else {
145: skipping = true;
146: }
147: writer.println(s);
148: } else if (do_strip && s.startsWith(endPrefix)) {
149: if (!skipping) {
150: System.err.println("Error in " + inf + ": "
151: + lineno);
152: System.err.println("unexpected /* #endif */");
153: writer.close();
154: System.exit(1);
155: } else {
156: skipping = false;
157: }
158: writer.println(s);
159: } else {
160: if (skipping) {
161: writer.print("/// skipped ");
162: }
163: writer.println(s);
164: }
165: }
166: if (skipping) {
167: System.err.println("Error in " + inf + ": " + lineno);
168: System.err.println("missing /* #endif */");
169: writer.close();
170: System.exit(1);
171: }
172:
173: writer.close();
174: reader.close();
175: }
176:
177: static String findSubDir(Vector roots, String file) {
178: for (int i = 0; i < roots.size(); i++) {
179: String root = (String) roots.elementAt(i);
180: if (file.startsWith(root)) {
181: file = file.substring(root.length());
182: while (file.startsWith(File.separator)) {
183: file = file.substring(1);
184: }
185: File f = new File(file);
186: return f.getParent();
187: }
188: }
189: throw new RuntimeException("root not known: " + file);
190: }
191: }
|