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.

Bootstrap2.java 9.7 kB

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