blob: d0bb011cca4fc9a6fd65b043207a938e1339eb59 [file] [log] [blame]
Stephan Herrmann7b7062f2010-04-01 19:56:59 +00001/*******************************************************************************
2 * Copyright (c) 2005, 2006 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 *******************************************************************************/
11import java.io.BufferedWriter;
12import java.io.File;
13import java.io.FileFilter;
14import java.io.FileWriter;
15import java.io.IOException;
16import java.io.Writer;
17import java.text.MessageFormat;
18import java.util.ArrayList;
19
20public class GenerateBuildScript {
21
22 private static final String LINE_SEPARATOR = System.getProperty("line.separator"); //$NON-NLS-1$
23 private static final String HEADER=
24 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + LINE_SEPARATOR + //$NON-NLS-1$
25 "<project name=\"export-executable\" default=\"build_executable\">" +LINE_SEPARATOR + //$NON-NLS-1$
26 " <target name=\"build_executable\">" + LINE_SEPARATOR; //$NON-NLS-1$
27
28 private static final String SOURCE_FILES =
29 " <echo message=\"compiling sources -> .o\"/>" + LINE_SEPARATOR + //$NON-NLS-1$
30 " <apply failonerror=\"true\" executable=\"$'{'gcc-path'}'/bin/{0}\" dest=\"{1}\" parallel=\"false\">" + LINE_SEPARATOR + //$NON-NLS-1$
31 " <arg value=\"--verbose\"/>" + LINE_SEPARATOR + //$NON-NLS-1$
32 " <arg value=\"--classpath={1}\"/>" + LINE_SEPARATOR + //$NON-NLS-1$
33 " <arg value=\"-O2\"/>" + LINE_SEPARATOR + //$NON-NLS-1$
34 " <arg value=\"-c\"/>" + LINE_SEPARATOR + //$NON-NLS-1$
35 " <arg value=\"-fassume-compiled\"/>" + LINE_SEPARATOR + //$NON-NLS-1$
36 " <arg value=\"-march=pentium4\"/>" + LINE_SEPARATOR + //$NON-NLS-1$
37 " <arg value=\"-mfpmath=sse\"/>" + LINE_SEPARATOR + //$NON-NLS-1$
38 " <srcfile/>" + LINE_SEPARATOR + //$NON-NLS-1$
39 " <arg value=\"-o\"/>" + LINE_SEPARATOR + //$NON-NLS-1$
40 " <targetfile/>" + LINE_SEPARATOR + //$NON-NLS-1$
41 " <fileset dir=\"{1}\" includes=\"**/*.java\"/>" + LINE_SEPARATOR + //$NON-NLS-1$
42 " <mapper type=\"glob\" from=\"*.java\" to=\"*.o\"/>" + LINE_SEPARATOR + //$NON-NLS-1$
43 " </apply>" + LINE_SEPARATOR + LINE_SEPARATOR; //$NON-NLS-1$
44 private static final String FOOTER =
45 " <echo message=\"linking .o -> $'{'binaryname'}'\"/>" + LINE_SEPARATOR + //$NON-NLS-1$
46 " <apply failonerror=\"true\" executable=\"$'{'gcc-path'}'/bin/{0}\" parallel=\"true\">" + LINE_SEPARATOR + //$NON-NLS-1$
47 " <arg value=\"--verbose\"/>" + LINE_SEPARATOR + //$NON-NLS-1$
48 " <arg line =\"-o $'{'dest'}'/$'{'binaryname'}'\"/>" + LINE_SEPARATOR + //$NON-NLS-1$
49 " <arg value=\"-fassume-compiled\"/>" + LINE_SEPARATOR + //$NON-NLS-1$
50 " <arg value=\"-march=pentium4\"/>" + LINE_SEPARATOR + //$NON-NLS-1$
51 " <arg value=\"-mfpmath=sse\"/>" + LINE_SEPARATOR + //$NON-NLS-1$
52 " <arg line=\"--main=org.eclipse.jdt.internal.compiler.batch.Main\"/>" + LINE_SEPARATOR + //$NON-NLS-1$
53 " <fileset dir=\"{1}\" includes=\"**/*.o\"/>" + LINE_SEPARATOR + //$NON-NLS-1$
54 " </apply>" + LINE_SEPARATOR + //$NON-NLS-1$
55 " </target>" + LINE_SEPARATOR + //$NON-NLS-1$
56 "</project>" + LINE_SEPARATOR; //$NON-NLS-1$
57
58 private static void collectAllFiles(File root, ArrayList collector, FileFilter fileFilter) {
59 File[] files = root.listFiles(fileFilter);
60 for (int i = 0; i < files.length; i++) {
61 if (files[i].isDirectory()) {
62 collectAllFiles(files[i], collector, fileFilter);
63 } else {
64 String newElement = files[i].getAbsolutePath();
65 newElement = newElement.replace('\\', '/');
66 collector.add(newElement);
67 }
68 }
69 }
70
71 private static void dumpAllProperties(Writer writer, File sourceDir, ArrayList collector, String gcj_exe, String dest_dir) throws IOException {
72 writer.write(" <echo message=\"compiling resources -> .o\"/>" + LINE_SEPARATOR); //$NON-NLS-1$
73 for (int i = 0, max = collector.size(); i < max; i++) {
74 String absolutePath = (String) collector.get(i);
75 String fileName = absolutePath.substring(sourceDir.getAbsolutePath().length() + 1);
76 writer.write(MessageFormat.format(" <exec dir=\"{1}\" executable=\"$'{'gcc-path'}'/bin/{0}\">" + LINE_SEPARATOR, new Object[] { gcj_exe, dest_dir})); //$NON-NLS-1$
77 writer.write(" <arg line=\"--resource "); //$NON-NLS-1$
78 writer.write(fileName + " " + fileName + " -c -o " + getObjectName(fileName) + "\"/>" + LINE_SEPARATOR); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
79 writer.write(" </exec>" + LINE_SEPARATOR); //$NON-NLS-1$
80 }
81 }
82
83 private static void dumpAllClassFiles(Writer writer, File sourceDir, ArrayList collector, String gcj_exe, String dest_dir) throws IOException {
84 writer.write(" <echo message=\"compiling class files -> .o\"/>" + LINE_SEPARATOR); //$NON-NLS-1$
85 writer.write(
86 MessageFormat.format(
87 " <apply failonerror=\"true\" executable=\"$'{'gcc-path'}'/bin/{0}\" dest=\"{1}\" parallel=\"false\">" + LINE_SEPARATOR + //$NON-NLS-1$
88 " <arg value=\"--verbose\"/>" + LINE_SEPARATOR + //$NON-NLS-1$
89 " <arg value=\"--classpath={1}\"/>" + LINE_SEPARATOR + //$NON-NLS-1$
90 " <arg value=\"-O2\"/>" + LINE_SEPARATOR + //$NON-NLS-1$
91 " <arg value=\"-c\"/>" + LINE_SEPARATOR + //$NON-NLS-1$
92 " <arg value=\"-fassume-compiled\"/>" + LINE_SEPARATOR + //$NON-NLS-1$
93 " <arg value=\"-march=pentium4\"/>" + LINE_SEPARATOR + //$NON-NLS-1$
94 " <arg value=\"-mfpmath=sse\"/>" + LINE_SEPARATOR + //$NON-NLS-1$
95 " <srcfile/>" + LINE_SEPARATOR + //$NON-NLS-1$
96 " <arg value=\"-o\"/>" + LINE_SEPARATOR + //$NON-NLS-1$
97 " <targetfile/>" + LINE_SEPARATOR + //$NON-NLS-1$
98 " <fileset dir=\"{1}\" includes=\"**/*.class\"/>" + LINE_SEPARATOR + //$NON-NLS-1$
99 " <mapper type=\"glob\" from=\"*.class\" to=\"*.o\"/>" + LINE_SEPARATOR + //$NON-NLS-1$
100 " </apply>" + LINE_SEPARATOR + LINE_SEPARATOR,//$NON-NLS-1$
101 new Object[] {
102 gcj_exe,
103 dest_dir
104 }));
105 }
106
107 private static String getObjectName(String fileName) {
108 return fileName.substring(0, fileName.lastIndexOf('.')) + ".o"; //$NON-NLS-1$
109 }
110
111 public static void main(String[] args) {
112 if (args.length != 5) {
113 System.out.println("Usage: script_name directory gcj_exe_name dest_dir source/bin"); //$NON-NLS-1$
114 return;
115 }
116 try {
117 BufferedWriter writer = new BufferedWriter(new FileWriter(new File(args[0])));
118 writer.write(HEADER);
119 File sourceDir = new File(args[1]);
120 if (sourceDir.exists()) {
121 ArrayList collector = new ArrayList();
122 collectAllFiles(sourceDir, collector, new FileFilter() {
123 public boolean accept(File pathname) {
124 String fileName = pathname.getAbsolutePath();
125 return pathname.isDirectory() || fileName.endsWith(".rsc") || fileName.endsWith(".properties"); //$NON-NLS-1$ //$NON-NLS-2$
126 }
127 });
128 dumpAllProperties(writer, sourceDir, collector, args[2], args[3]);
129 if ("source".equals(args[4])) { //$NON-NLS-1$
130 writer.write(MessageFormat.format(SOURCE_FILES, new Object[] {args[2], args[3]}));
131 } else {
132 collector = new ArrayList();
133 collectAllFiles(sourceDir, collector, new FileFilter() {
134 public boolean accept(File pathname) {
135 String fileName = pathname.getAbsolutePath();
136 return pathname.isDirectory() || fileName.endsWith(".class"); //$NON-NLS-1$
137 }
138 });
139 dumpAllClassFiles(writer, sourceDir, collector, args[2], args[3]);
140 }
141 }
142 writer.write(MessageFormat.format(FOOTER, new Object[] {args[2], args[3]}));
143 writer.flush();
144 writer.close();
145 } catch (IOException e) {
146 e.printStackTrace();
147 }
148 }
149}