Thu, 07 Jun 2012 20:30:57 -0700
7170079: Adjustments to build-infra makefiles
Reviewed-by: ohair, ohrstrom, ihse, jonas
Contributed-by: jonas <jonas.oreland@oracle.com>, erikj <erik.joelsson@oracle.com>, ihse <magnus.ihse.bursie@oracle.com>, tgranat <torbjorn.granat@oracle.com>, ykantser <yekaterina.kantserova@oracle.com>
1 /*
2 * Copyright (c) 2006, 2007, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
24 /*
25 * @test
26 * @bug 6400872
27 * @summary REGRESSION: Java Compiler cannot find jar files referenced by other
28 * @run main T6400872
29 */
31 // ${TESTJAVA}/bin/javac -d ${TESTCLASSES} ${TESTSRC}/A.java ${TESTSRC}/B.java
32 // ${TESTJAVA}/bin/jar -cfm A.jar ${TESTSRC}/A/META-INF/MANIFEST.MF -C ${TESTCLASSES} A.class
33 // ${TESTJAVA}/bin/jar -cfm B.jar ${TESTSRC}/B/META-INF/MANIFEST.MF -C ${TESTCLASSES} B.class
34 // ${TESTJAVA}/bin/javac -cp A.jar ${TESTSRC}/C.java
36 import java.io.*;
37 import java.nio.*;
38 import java.util.*;
39 import java.util.jar.*;
40 import javax.tools.*;
41 import javax.tools.StandardJavaFileManager.*;
43 public class T6400872 {
44 static File testSrc = new File(System.getProperty("test.src", "."));
45 static File testClasses = new File(System.getProperty("test.classes", "."));
47 public static void main(String... args) throws Exception {
48 // compile A.java and B.java
49 compile(testClasses, null, new File(testSrc, "A.java"), new File(testSrc, "B.java"));
50 // put them in mutually referential class files
51 jar(new File("A.jar"), iterable(new File(".", "B.jar")), testClasses, new File("A.class"));
52 jar(new File("B.jar"), iterable(new File(".", "A.jar")), testClasses, new File("B.class"));
53 // verify we can successfully use the class path entries in the jar files
54 compile(new File("."), iterable(new File("A.jar")), new File(testSrc, "C.java"));
55 }
57 static void compile(File classOutDir, Iterable<File> classPath, File... files)
58 throws IOException {
59 System.err.println("compile...");
60 JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
61 StandardJavaFileManager fm = compiler.getStandardFileManager(null, null, null);
62 try {
63 Iterable<? extends JavaFileObject> fileObjects =
64 fm.getJavaFileObjectsFromFiles(Arrays.asList(files));
66 List<String> options = new ArrayList<String>();
67 if (classOutDir != null) {
68 options.add("-d");
69 options.add(classOutDir.getPath());
70 }
71 if (classPath != null) {
72 options.add("-classpath");
73 options.add(join(classPath, File.pathSeparator));
74 }
75 options.add("-verbose");
77 JavaCompiler.CompilationTask task =
78 compiler.getTask(null, fm, null, options, null, fileObjects);
79 if (!task.call())
80 throw new AssertionError("compilation failed");
81 } finally {
82 fm.close();
83 }
84 }
86 static void jar(File jar, Iterable<File> classPath, File base, File... files)
87 throws IOException {
88 System.err.println("jar...");
89 Manifest m = new Manifest();
90 if (classPath != null) {
91 Attributes mainAttrs = m.getMainAttributes();
92 mainAttrs.put(Attributes.Name.MANIFEST_VERSION, "1.0");
93 mainAttrs.put(Attributes.Name.CLASS_PATH, join(classPath, " "));
94 }
95 OutputStream out = new BufferedOutputStream(new FileOutputStream(jar));
96 JarOutputStream j = new JarOutputStream(out, m);
97 add(j, base, files);
98 j.close();
99 }
101 static void add(JarOutputStream j, File base, File... files) throws IOException {
102 if (files == null)
103 return;
105 for (File f: files)
106 add(j, base, f);
107 }
109 static void add(JarOutputStream j, File base, File file) throws IOException {
110 File f = new File(base, file.getPath());
111 if (f.isDirectory()) {
112 String[] children = f.list();
113 if (children != null)
114 for (String c: children)
115 add(j, base, new File(file, c));
116 } else {
117 JarEntry e = new JarEntry(file.getPath());
118 e.setSize(f.length());
119 j.putNextEntry(e);
120 j.write(read(f));
121 j.closeEntry();
122 }
124 }
126 static byte[] read(File f) throws IOException {
127 byte[] buf = new byte[(int) f.length()];
128 BufferedInputStream in = new BufferedInputStream(new FileInputStream(f));
129 int offset = 0;
130 while (offset < buf.length) {
131 int n = in.read(buf, offset, buf.length - offset);
132 if (n < 0)
133 throw new EOFException();
134 offset += n;
135 }
136 return buf;
137 }
139 static <T> Iterable<T> iterable(T single) {
140 return Collections.singleton(single);
141 }
143 static <T> String join(Iterable<T> iter, String sep) {
144 StringBuilder p = new StringBuilder();
145 for (T t: iter) {
146 if (p.length() > 0)
147 p.append(' ');
148 p.append(t);
149 }
150 return p.toString();
151 }
152 }