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 KiB

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