1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 *
17 */
18
19 package org.apache.tools.ant.taskdefs.compilers;
20
21 import java.io.IOException;
22 import java.io.File;
23
24 import org.apache.tools.ant.BuildException;
25 import org.apache.tools.ant.Project;
26 import org.apache.tools.ant.types.Commandline;
27 import org.apache.tools.ant.taskdefs.condition.Os;
28 import org.apache.tools.ant.util.JavaEnvUtils;
29 import org.apache.tools.ant.util.FileUtils;
30
31 /**
32 * Performs a compile using javac externally.
33 *
34 * @since Ant 1.4
35 */
36 public class JavacExternal extends DefaultCompilerAdapter {
37
38 /**
39 * Performs a compile using the Javac externally.
40 * @return true if the compilation succeeded
41 * @throws BuildException on error
42 */
43 public boolean execute() throws BuildException {
44 attributes.log("Using external javac compiler", Project.MSG_VERBOSE);
45
46 Commandline cmd = new Commandline();
47 cmd.setExecutable(getJavac().getJavacExecutable());
48 if (!assumeJava11() && !assumeJava12()) {
49 setupModernJavacCommandlineSwitches(cmd);
50 } else {
51 setupJavacCommandlineSwitches(cmd, true);
52 }
53 int firstFileName = assumeJava11() ? -1 : cmd.size();
54 logAndAddFilesToCompile(cmd);
55 //On VMS platform, we need to create a special java options file
56 //containing the arguments and classpath for the javac command.
57 //The special file is supported by the "-V" switch on the VMS JVM.
58 if (Os.isFamily("openvms")) {
59 return execOnVMS(cmd, firstFileName);
60 }
61 return
62 executeExternalCompile(cmd.getCommandline(), firstFileName,
63 true)
64 == 0;
65 }
66
67 /**
68 * helper method to execute our command on VMS.
69 * @param cmd
70 * @param firstFileName
71 * @return
72 */
73 private boolean execOnVMS(Commandline cmd, int firstFileName) {
74 File vmsFile = null;
75 try {
76 vmsFile = JavaEnvUtils.createVmsJavaOptionFile(cmd.getArguments());
77 String[] commandLine = {cmd.getExecutable(),
78 "-V",
79 vmsFile.getPath()};
80 return 0 == executeExternalCompile(commandLine,
81 firstFileName,
82 true);
83
84 } catch (IOException e) {
85 throw new BuildException("Failed to create a temporary file for \"-V\" switch");
86 } finally {
87 FileUtils.delete(vmsFile);
88 }
89 }
90
91 }
92