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

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