You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

Bootstrap.java 11 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. // -------------------------------------------------------------------------------
  2. // Copyright (c)2000 Apache Software Foundation
  3. // -------------------------------------------------------------------------------
  4. import java.io.*;
  5. import java.util.*;
  6. import java.util.zip.*;
  7. /**
  8. * Quick and dirty single class bootstrap utility for getting Ant off
  9. * the ground when in need. To use, compile this file in the directory
  10. * where the source code is in the repository, then execute it. That's
  11. * it.<p>
  12. *
  13. * No pretense is made that this is an elegant peice of code. This code
  14. * only exists to do a ground zero build of Ant. Any other building of
  15. * Ant should be done with itself whenever possible.
  16. *
  17. * @author James Duncan Davidson (duncan@apache.org)
  18. */
  19. public class Bootstrap {
  20. private static String base = "../";
  21. private static String crimsonSources = "../../../xml-crimson/src"; // relative to base
  22. private static String[] modules = new String[]{"copy", "echo", "jar", "javac"};
  23. /**
  24. * Command line entry point.
  25. */
  26. public static void main(String[] args) {
  27. // check for secret sugar left by MRJ startup script...
  28. if (args.length > 0) {
  29. if (args[0].equals("osx")) {
  30. base = "";
  31. }
  32. }
  33. long startTime = System.currentTimeMillis();
  34. System.out.println("Starting Bootstrap....");
  35. // ------------------------------------------------------------
  36. // first create dirs that we need for strapping
  37. // ------------------------------------------------------------
  38. mkdir(base + "bootstrap/temp");
  39. mkdir(base + "bootstrap/temp/crimson");
  40. mkdir(base + "bootstrap/temp/main");
  41. mkdir(base + "bootstrap/temp/tasks");
  42. mkdir(base + "bootstrap/temp/taskjars");
  43. for (int i = 0; i < modules.length; i++) {
  44. mkdir(base + "bootstrap/temp/tasks/" + modules[i]);
  45. }
  46. // ------------------------------------------------------------
  47. // build crimson
  48. // ------------------------------------------------------------
  49. System.out.println("CRIMSON: " + base + crimsonSources);
  50. Vector v1 = getSources(base + crimsonSources);
  51. doCompile(base + "bootstrap/temp/crimson", v1);
  52. // ------------------------------------------------------------
  53. // build the main thing
  54. // ------------------------------------------------------------
  55. Vector v2 = getSources(base + "source/main");
  56. doCompile(base + "bootstrap/temp/main", v2);
  57. // ------------------------------------------------------------
  58. // now build each of the needed peices into their
  59. // areas within the strapping area
  60. // ------------------------------------------------------------
  61. for (int i = 0; i < modules.length; i++) {
  62. buildModule(modules[i]);
  63. }
  64. // ------------------------------------------------------------
  65. // now, set classpaths and launch an Ant build to
  66. // have Ant build itself nicely
  67. // ------------------------------------------------------------
  68. System.out.println();
  69. System.out.println("-------------------------------------------");
  70. System.out.println("STARTING REAL BUILD");
  71. System.out.println("-------------------------------------------");
  72. System.out.println();
  73. String[] cmdarray = new String[9];
  74. cmdarray[0] = "java";
  75. cmdarray[1] = "-cp";
  76. cmdarray[2] = base + "bootstrap/temp/main:" +
  77. base + "bootstrap/temp/crimson";
  78. cmdarray[3] = "org.apache.ant.cli.Main";
  79. cmdarray[4] = "-taskpath";
  80. cmdarray[5] = base + "bootstrap/temp/taskjars";
  81. cmdarray[6] = "-buildfile";
  82. cmdarray[7] = base + "source/main.ant";
  83. cmdarray[8] = "default";
  84. try {
  85. Runtime runtime = Runtime.getRuntime();
  86. Process process = runtime.exec(cmdarray);
  87. // echo output from process
  88. InputStream in = process.getInputStream();
  89. byte[] buf = new byte[80];
  90. int count = 0;
  91. count = in.read(buf, 0, buf.length);
  92. while (count != -1) {
  93. System.out.write(buf, 0, count);
  94. count = in.read(buf, 0, buf.length);
  95. }
  96. in = process.getErrorStream();
  97. count = in.read(buf, 0, buf.length);
  98. if (count > 0) {
  99. System.out.println();
  100. System.out.println("Error Stream Output:");
  101. while (count != -1) {
  102. System.out.write(buf, 0, count);
  103. count = in.read(buf, 0, buf.length);
  104. }
  105. }
  106. } catch (Exception e) {
  107. System.out.println("OUCHY: " + e);
  108. return;
  109. }
  110. System.out.println();
  111. System.out.println("-------------------------------------------");
  112. System.out.println("FINISHED WITH REAL BUILD");
  113. System.out.println("-------------------------------------------");
  114. System.out.println();
  115. // ------------------------------------------------------------
  116. // Remove Temporary classes
  117. // ------------------------------------------------------------
  118. // delete(tempDirName);
  119. // ------------------------------------------------------------
  120. // Print Closer
  121. // ------------------------------------------------------------
  122. long endTime = System.currentTimeMillis();
  123. long elapsd = endTime - startTime;
  124. System.out.println("Bootstrap Time: " + (elapsd/1000) + "." + (elapsd%1000) +
  125. " seconds");
  126. }
  127. private static void mkdir(String arg) {
  128. File dir = new File(arg);
  129. if (dir.exists() && !dir.isDirectory()) {
  130. System.out.println("Oh, horrors! Dir " + arg + " " +
  131. "doesn't seem to be a dir... Stop!");
  132. System.exit(1);
  133. }
  134. if (!dir.exists()) {
  135. System.out.println("Making dir: " + arg);
  136. dir.mkdir();
  137. }
  138. }
  139. private static void buildModule(String arg) {
  140. System.out.println("Building " + arg);
  141. // get all sources and hand them off to the compiler to
  142. // build over into destination
  143. Vector v = getSources(base + "source/coretasks/" + arg);
  144. if (v.size() > 0) {
  145. doCompile(base + "bootstrap/temp/tasks/" + arg, v);
  146. }
  147. // move taskdef.properties for the module
  148. copyfile(base + "source/coretasks/" + arg + "/taskdef.properties",
  149. base + "bootstrap/temp/tasks/" + arg + "/taskdef.properties");
  150. // jar up tasks
  151. try {
  152. jarDir(new File(base + "bootstrap/temp/tasks/" + arg),
  153. new File(base + "bootstrap/temp/taskjars/" + arg + ".jar"));
  154. } catch(IOException ioe) {
  155. System.out.println("problem jar'ing: " + arg);
  156. }
  157. }
  158. private static Vector getSources(String arg) {
  159. File sourceDir = new File(arg);
  160. Vector v = new Vector();
  161. scanDir(sourceDir, v, ".java");
  162. return v;
  163. }
  164. private static void jarDir(File dir, File jarfile) throws IOException {
  165. String[] files = dir.list();
  166. if (files.length > 0) {
  167. System.out.println("Jaring: " + jarfile);
  168. FileOutputStream fos = new FileOutputStream(jarfile);
  169. ZipOutputStream zos = new ZipOutputStream(fos);
  170. jarDir(dir, "", zos);
  171. zos.close();
  172. }
  173. }
  174. private static void jarDir(File dir, String prefix, ZipOutputStream zos) throws
  175. IOException
  176. {
  177. String[] files = dir.list();
  178. for (int i = 0; i < files.length; i++) {
  179. File f = new File(dir, files[i]);
  180. if (f.isDirectory()) {
  181. jarDir(f, prefix + "/" + files[i], zos);
  182. } else {
  183. ZipEntry ze = new ZipEntry(prefix + "/" + files[i]);
  184. zos.putNextEntry(ze);
  185. FileInputStream fis = new FileInputStream(f);
  186. int count = 0;
  187. byte[] buf = new byte[8 * 1024];
  188. count = fis.read(buf, 0, buf.length);
  189. while (count != -1) {
  190. zos.write(buf, 0, count);
  191. count = fis.read(buf, 0, buf.length);
  192. }
  193. fis.close();
  194. }
  195. }
  196. }
  197. private static void scanDir(File dir, Vector v, String endsWith) {
  198. // System.out.println("user.dir=" + System.getProperty("user.dir"));
  199. // System.out.println("Scanning: " + dir);
  200. String[] files = dir.list();
  201. // System.out.println("Files: " + files);
  202. for (int i = 0; i < files.length; i++) {
  203. File f = new File(dir, files[i]);
  204. if (f.isDirectory()) {
  205. scanDir(f, v, endsWith);
  206. } else {
  207. if (files[i].endsWith(endsWith)) {
  208. v.addElement(f);
  209. }
  210. }
  211. }
  212. }
  213. private static void doCompile(String dest, Vector sources) {
  214. System.out.println(" Compiling " + sources.size() + " files to " + dest);
  215. // XXX This should be more forgiving about compiling wherever
  216. // under whatever compiler, but this works so...
  217. sun.tools.javac.Main compiler = new sun.tools.javac.Main(System.out,
  218. "javac");
  219. String[] args = new String[sources.size() + 4];
  220. args[0] = "-classpath";
  221. args[1] = base + "bootstrap/temp/main:" +
  222. base + "bootstrap/temp/crimson";
  223. args[2] = "-d";
  224. args[3] = dest;
  225. for (int i = 0; i < sources.size(); i++) {
  226. args[4+i] = ((File)sources.elementAt(i)).toString();
  227. }
  228. // System.out.print("javac ");
  229. // for (int i = 0; i < args.length; i++) {
  230. // System.out.print(args[i] + " ");
  231. // }
  232. // System.out.println();
  233. compiler.compile(args);
  234. }
  235. private static void copyfile(String from, String dest) {
  236. File fromF = new File(from);
  237. File destF = new File(dest);
  238. if (fromF.exists()) {
  239. System.out.println(" Copying " + from);
  240. try {
  241. FileInputStream in = new FileInputStream(fromF);
  242. FileOutputStream out = new FileOutputStream(destF);
  243. byte[] buf = new byte[1024 * 16];
  244. int count = 0;
  245. count = in.read(buf, 0, buf.length);
  246. if (count != -1) {
  247. out.write(buf, 0, count);
  248. count = in.read(buf, 0, buf.length);
  249. }
  250. in.close();
  251. out.close();
  252. } catch (IOException ioe) {
  253. System.out.println("OUCH: " + from);
  254. System.out.println(ioe);
  255. }
  256. }
  257. }
  258. }