01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * 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",
45: Project.MSG_VERBOSE);
46:
47: Commandline cmd = new Commandline();
48: cmd.setExecutable(getJavac().getJavacExecutable());
49: if (!assumeJava11() && !assumeJava12()) {
50: setupModernJavacCommandlineSwitches(cmd);
51: } else {
52: setupJavacCommandlineSwitches(cmd, true);
53: }
54: int firstFileName = assumeJava11() ? -1 : cmd.size();
55: logAndAddFilesToCompile(cmd);
56: //On VMS platform, we need to create a special java options file
57: //containing the arguments and classpath for the javac command.
58: //The special file is supported by the "-V" switch on the VMS JVM.
59: if (Os.isFamily("openvms")) {
60: return execOnVMS(cmd, firstFileName);
61: }
62: return executeExternalCompile(cmd.getCommandline(),
63: firstFileName, true) == 0;
64: }
65:
66: /**
67: * helper method to execute our command on VMS.
68: * @param cmd
69: * @param firstFileName
70: * @return
71: */
72: private boolean execOnVMS(Commandline cmd, int firstFileName) {
73: File vmsFile = null;
74: try {
75: vmsFile = JavaEnvUtils.createVmsJavaOptionFile(cmd
76: .getArguments());
77: String[] commandLine = { cmd.getExecutable(), "-V",
78: vmsFile.getPath() };
79: return 0 == executeExternalCompile(commandLine,
80: firstFileName, true);
81:
82: } catch (IOException e) {
83: throw new BuildException(
84: "Failed to create a temporary file for \"-V\" switch");
85: } finally {
86: FileUtils.delete(vmsFile);
87: }
88: }
89:
90: }
|