Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/openjdk-multiarch-jdk8u
Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/lib/testlibrary/CompilerUtils.java
38833 views
1
/*
2
* Copyright (c) 2015, 2017, 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
*/
23
24
import java.io.File;
25
import java.io.IOException;
26
import java.nio.file.Files;
27
import java.nio.file.Path;
28
import java.util.ArrayList;
29
import java.util.Arrays;
30
import java.util.Collections;
31
import java.util.List;
32
import java.util.stream.Collectors;
33
34
import javax.tools.JavaCompiler;
35
import javax.tools.StandardJavaFileManager;
36
import javax.tools.StandardLocation;
37
import javax.tools.ToolProvider;
38
39
import jdk.testlibrary.OutputAnalyzer;
40
import jdk.testlibrary.ProcessTools;
41
42
/**
43
* This class consists exclusively of static utility methods for invoking the
44
* java compiler.
45
*/
46
47
public final class CompilerUtils {
48
private static final String TEST_JAVA_HOME = System.getProperty("test.jdk");
49
private static final String FS = File.separator;
50
private static final String JAVAC = TEST_JAVA_HOME + FS + "bin" + FS + "javac";
51
52
private CompilerUtils() { }
53
54
/**
55
* Compile all the java sources in {@code <source>/**} to
56
* {@code <destination>/**}. The destination directory will be created if
57
* it doesn't exist.
58
*
59
* All warnings/errors emitted by the compiler are output to System.out/err.
60
*
61
* @return true if the compilation is successful
62
*
63
* @throws IOException
64
* if there is an I/O error scanning the source tree or
65
* creating the destination directory
66
* @throws UnsupportedOperationException
67
* if there is no system java compiler
68
*/
69
public static boolean compile(Path source, Path destination, String ... options)
70
throws IOException
71
{
72
List<Path> sources
73
= Files.find(source, Integer.MAX_VALUE,
74
(file, attrs) -> (file.toString().endsWith(".java")))
75
.collect(Collectors.toList());
76
77
Files.createDirectories(destination);
78
List<String> opts = Arrays.asList(options);
79
List<String> compileargs = new ArrayList<String> ();
80
compileargs.add(JAVAC);
81
compileargs.add("-d");
82
compileargs.add(destination.toString());
83
for(String opt: opts) {
84
compileargs.add(opt);
85
}
86
for(Path p: sources) {
87
compileargs.add(p.toString());
88
}
89
90
OutputAnalyzer output = null;
91
try {
92
String[] sarr = compileargs.toArray(new String[0]);
93
output = ProcessTools.executeCommand(sarr);
94
output.shouldHaveExitValue(0);
95
} catch (Throwable t) {
96
throw new RuntimeException("Javac failed", t);
97
}
98
99
return true;
100
}
101
}
102
103