KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > GenerateBuildScript


1 /*******************************************************************************
2  * Copyright (c) 2005 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 import java.io.BufferedWriter JavaDoc;
12 import java.io.File JavaDoc;
13 import java.io.FileWriter JavaDoc;
14 import java.io.IOException JavaDoc;
15 import java.io.Writer JavaDoc;
16 import java.util.ArrayList JavaDoc;
17
18 public class GenerateBuildScript {
19
20     private static final String JavaDoc LINE_SEPARATOR = System.getProperty("line.separator");
21     private static final String JavaDoc HEADER=
22         "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + LINE_SEPARATOR +
23         "<project name=\"export-executable\" default=\"build_executable\">" +LINE_SEPARATOR +
24         " <target name=\"build_executable\">" + LINE_SEPARATOR +
25         " <echo message=\"compiling resources -> .o\"/>" + LINE_SEPARATOR;
26         
27     private static final String JavaDoc FOOTER =
28         " <echo message=\"compiling sources -> .o\"/>" + LINE_SEPARATOR +
29         " <apply failonerror=\"true\" executable=\"${gcc-path}/bin/gcj.exe\" dest=\"${work}\" parallel=\"false\">" + LINE_SEPARATOR +
30         " <arg value=\"--verbose\"/>" + LINE_SEPARATOR +
31         " <arg value=\"--classpath=${work}\"/>" + LINE_SEPARATOR +
32         " <arg value=\"-O2\"/>" + LINE_SEPARATOR +
33         " <arg value=\"-c\"/>" + LINE_SEPARATOR +
34         " <arg value=\"-fassume-compiled\"/>" + LINE_SEPARATOR +
35         " <arg value=\"-march=pentium4\"/>" + LINE_SEPARATOR +
36         " <arg value=\"-mfpmath=sse\"/>" + LINE_SEPARATOR +
37         " <srcfile/>" + LINE_SEPARATOR +
38         " <arg value=\"-o\"/>" + LINE_SEPARATOR +
39         " <targetfile/>" + LINE_SEPARATOR +
40         " <fileset dir=\"${work}\" includes=\"**/*.java\"/>" + LINE_SEPARATOR +
41         " <mapper type=\"glob\" from=\"*.java\" to=\"*.o\"/>" + LINE_SEPARATOR +
42         " </apply>" + LINE_SEPARATOR + LINE_SEPARATOR +
43         " <echo message=\"linking .o -> ${binaryname}\"/>" + LINE_SEPARATOR +
44         " <apply failonerror=\"true\" executable=\"${gcc-path}/bin/gcj.exe\" parallel=\"true\">" + LINE_SEPARATOR +
45         " <arg value=\"--verbose\"/>" + LINE_SEPARATOR +
46         " <arg line =\"-o ${dest}${binaryname}.exe\"/>" + LINE_SEPARATOR +
47         " <arg value=\"-fassume-compiled\"/>" + LINE_SEPARATOR +
48         " <arg value=\"-march=pentium4\"/>" + LINE_SEPARATOR +
49         " <arg value=\"-mfpmath=sse\"/>" + LINE_SEPARATOR +
50         " <arg line=\"--main=org.eclipse.jdt.internal.compiler.batch.Main\"/>" + LINE_SEPARATOR +
51         " <fileset dir=\"${work}\" includes=\"**/*.o\"/>" + LINE_SEPARATOR +
52         " </apply>" + LINE_SEPARATOR +
53         " </target>" + LINE_SEPARATOR +
54         "</project>" + LINE_SEPARATOR;
55
56     private static void collectAllPropertiesFiles(File JavaDoc root, ArrayList JavaDoc collector) {
57         File JavaDoc[] files = root.listFiles();
58         for (int i = 0; i < files.length; i++) {
59             if (files[i].isDirectory()) {
60                 collectAllPropertiesFiles(files[i], collector);
61             } else if (files[i].getName().endsWith(".rsc") || files[i].getName().endsWith(".properties")) { //$NON-NLS-1$
62
String JavaDoc newElement = files[i].getAbsolutePath();
63                 newElement = newElement.replace('\\', '/');
64                 collector.add(newElement);
65             }
66         }
67     }
68
69     private static void dumpAllProperties(Writer JavaDoc writer, File JavaDoc sourceDir, ArrayList JavaDoc collector) throws IOException JavaDoc {
70         for (int i = 0, max = collector.size(); i < max; i++) {
71             String JavaDoc absolutePath = (String JavaDoc) collector.get(i);
72             String JavaDoc fileName = absolutePath.substring(sourceDir.getAbsolutePath().length() + 1);
73             writer.write(" <exec dir=\"${work}\" executable=\"${gcc-path}/bin/gcj.exe\">" + LINE_SEPARATOR);
74             writer.write(" <arg line=\"--resource ");
75             writer.write(fileName + " " + fileName + " -c -o " + getObjectName(fileName) + "\"/>" + LINE_SEPARATOR);
76             writer.write(" </exec>" + LINE_SEPARATOR);
77         }
78     }
79
80     private static String JavaDoc getObjectName(String JavaDoc fileName) {
81         return fileName.substring(0, fileName.lastIndexOf('.')) + ".o";
82     }
83             
84     public static void main(String JavaDoc[] args) {
85         if (args.length != 2) {
86             return;
87         }
88         try {
89             BufferedWriter JavaDoc writer = new BufferedWriter JavaDoc(new FileWriter JavaDoc(new File JavaDoc(args[0])));
90             writer.write(HEADER);
91             File JavaDoc sourceDir = new File JavaDoc(args[1]);
92             if (sourceDir.exists()) {
93                 ArrayList JavaDoc collector = new ArrayList JavaDoc();
94                 collectAllPropertiesFiles(sourceDir, collector);
95                 dumpAllProperties(writer, sourceDir, collector);
96             }
97             writer.write(FOOTER);
98             writer.flush();
99             writer.close();
100         } catch (IOException JavaDoc e) {
101             e.printStackTrace();
102         }
103     }
104 }
105
Popular Tags