|
1 /* |
|
2 * Copyright (c) 2015, 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. Oracle designates this |
|
8 * particular file as subject to the "Classpath" exception as provided |
|
9 * by Oracle in the LICENSE file that accompanied this code. |
|
10 * |
|
11 * This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 * version 2 for more details (a copy is included in the LICENSE file that |
|
15 * accompanied this code). |
|
16 * |
|
17 * You should have received a copy of the GNU General Public License version |
|
18 * 2 along with this work; if not, write to the Free Software Foundation, |
|
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 * |
|
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
|
22 * or visit www.oracle.com if you need additional information or have any |
|
23 * questions. |
|
24 */ |
|
25 |
|
26 package com.sun.tools.sjavac.comp; |
|
27 |
|
28 import java.io.File; |
|
29 import java.io.IOException; |
|
30 import java.io.PrintWriter; |
|
31 import java.io.StringWriter; |
|
32 import java.net.URI; |
|
33 import java.util.Arrays; |
|
34 import java.util.Iterator; |
|
35 import java.util.List; |
|
36 import java.util.Set; |
|
37 |
|
38 import javax.tools.JavaFileObject; |
|
39 import javax.tools.StandardJavaFileManager; |
|
40 import javax.tools.StandardLocation; |
|
41 import javax.tools.ToolProvider; |
|
42 |
|
43 import com.sun.tools.javac.api.JavacTaskImpl; |
|
44 import com.sun.tools.javac.api.JavacTool; |
|
45 import com.sun.tools.javac.util.Context; |
|
46 import com.sun.tools.javac.util.Dependencies; |
|
47 import com.sun.tools.javac.util.ListBuffer; |
|
48 import com.sun.tools.sjavac.Log; |
|
49 import com.sun.tools.sjavac.Util; |
|
50 import com.sun.tools.sjavac.comp.dependencies.NewDependencyCollector; |
|
51 import com.sun.tools.sjavac.comp.dependencies.PublicApiCollector; |
|
52 import com.sun.tools.sjavac.server.CompilationSubResult; |
|
53 import com.sun.tools.sjavac.server.SysInfo; |
|
54 |
|
55 /** |
|
56 * <p><b>This is NOT part of any supported API. |
|
57 * If you write code that depends on this, you do so at your own risk. |
|
58 * This code and its internal interfaces are subject to change or |
|
59 * deletion without notice.</b> |
|
60 */ |
|
61 public class CompilationService { |
|
62 |
|
63 public SysInfo getSysInfo() { |
|
64 return new SysInfo(Runtime.getRuntime().availableProcessors(), |
|
65 Runtime.getRuntime().maxMemory()); |
|
66 } |
|
67 |
|
68 public CompilationSubResult compile(String protocolId, |
|
69 String invocationId, |
|
70 String[] args, |
|
71 List<File> explicitSources, |
|
72 Set<URI> sourcesToCompile, |
|
73 Set<URI> visibleSources) { |
|
74 |
|
75 JavacTool compiler = (JavacTool) ToolProvider.getSystemJavaCompiler(); |
|
76 try (StandardJavaFileManager fm = compiler.getStandardFileManager(null, null, null)) { |
|
77 SmartFileManager sfm = new SmartFileManager(fm); |
|
78 Context context = new Context(); |
|
79 |
|
80 Dependencies.GraphDependencies.preRegister(context); |
|
81 |
|
82 // Now setup the actual compilation |
|
83 CompilationSubResult compilationResult = new CompilationSubResult(0); |
|
84 |
|
85 // First deal with explicit source files on cmdline and in at file |
|
86 ListBuffer<JavaFileObject> explicitJFOs = new ListBuffer<>(); |
|
87 for (JavaFileObject jfo : fm.getJavaFileObjectsFromFiles(explicitSources)) { |
|
88 explicitJFOs.append(SmartFileManager.locWrap(jfo, StandardLocation.SOURCE_PATH)); |
|
89 } |
|
90 // Now deal with sources supplied as source_to_compile |
|
91 ListBuffer<File> sourcesToCompileFiles = new ListBuffer<>(); |
|
92 for (URI u : sourcesToCompile) |
|
93 sourcesToCompileFiles.append(new File(u)); |
|
94 |
|
95 for (JavaFileObject jfo : fm.getJavaFileObjectsFromFiles(sourcesToCompileFiles)) |
|
96 explicitJFOs.append(SmartFileManager.locWrap(jfo, StandardLocation.SOURCE_PATH)); |
|
97 |
|
98 // Create a new logger |
|
99 StringWriter stdoutLog = new StringWriter(); |
|
100 StringWriter stderrLog = new StringWriter(); |
|
101 PrintWriter stdout = new PrintWriter(stdoutLog); |
|
102 PrintWriter stderr = new PrintWriter(stderrLog); |
|
103 com.sun.tools.javac.main.Main.Result rc = com.sun.tools.javac.main.Main.Result.OK; |
|
104 PublicApiCollector pubApiCollector = new PublicApiCollector(context, explicitJFOs); |
|
105 PathAndPackageVerifier papVerifier = new PathAndPackageVerifier(); |
|
106 NewDependencyCollector depsCollector = new NewDependencyCollector(context, explicitJFOs); |
|
107 try { |
|
108 if (explicitJFOs.size() > 0) { |
|
109 sfm.setVisibleSources(visibleSources); |
|
110 sfm.cleanArtifacts(); |
|
111 sfm.setLog(stdout); |
|
112 |
|
113 // Do the compilation! |
|
114 JavacTaskImpl task = |
|
115 (JavacTaskImpl) compiler.getTask(stderr, |
|
116 sfm, |
|
117 null, |
|
118 Arrays.asList(args), |
|
119 null, |
|
120 explicitJFOs, |
|
121 context); |
|
122 sfm.setSymbolFileEnabled(!com.sun.tools.javac.util.Options.instance(context).isSet("ignore.symbol.file")); |
|
123 task.addTaskListener(depsCollector); |
|
124 task.addTaskListener(pubApiCollector); |
|
125 task.addTaskListener(papVerifier); |
|
126 logJavacInvocation(args); |
|
127 rc = task.doCall(); |
|
128 Log.debug("javac returned with code " + rc); |
|
129 sfm.flush(); |
|
130 } |
|
131 } catch (Exception e) { |
|
132 Log.error(Util.getStackTrace(e)); |
|
133 stderrLog.append(Util.getStackTrace(e)); |
|
134 rc = com.sun.tools.javac.main.Main.Result.ERROR; |
|
135 } |
|
136 |
|
137 compilationResult.packageArtifacts = sfm.getPackageArtifacts(); |
|
138 |
|
139 if (papVerifier.errorsDiscovered()) |
|
140 rc = com.sun.tools.javac.main.Main.Result.ERROR; |
|
141 |
|
142 compilationResult.packageDependencies = depsCollector.getDependencies(false); |
|
143 compilationResult.packageCpDependencies = depsCollector.getDependencies(true); |
|
144 |
|
145 compilationResult.packagePubapis = pubApiCollector.getPubApis(true); |
|
146 compilationResult.dependencyPubapis = pubApiCollector.getPubApis(false); |
|
147 compilationResult.stdout = stdoutLog.toString(); |
|
148 compilationResult.stderr = stderrLog.toString(); |
|
149 compilationResult.returnCode = rc.exitCode; |
|
150 |
|
151 return compilationResult; |
|
152 } catch (IOException e) { |
|
153 throw new Error(e); |
|
154 } |
|
155 } |
|
156 |
|
157 private void logJavacInvocation(String[] args) { |
|
158 Log.debug("Invoking javac with args"); |
|
159 Iterator<String> argIter = Arrays.asList(args).iterator(); |
|
160 while (argIter.hasNext()) { |
|
161 String arg = argIter.next(); |
|
162 String line = " " + arg; |
|
163 if (arg.matches("\\-(d|cp|classpath|sourcepath|source|target)") |
|
164 && argIter.hasNext()) { |
|
165 line += " " + argIter.next(); |
|
166 } |
|
167 Log.debug(line); |
|
168 } |
|
169 } |
|
170 |
|
171 } |