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 10 kB

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