/* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You under the Apache License, Version 2.0 * (the "License"); you may not use this file except in compliance with * the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * */ package org.apache.tools.ant.taskdefs.compilers; import org.apache.tools.ant.BuildException; import org.apache.tools.ant.Project; import org.apache.tools.ant.types.Commandline; import org.apache.tools.ant.types.Path; /** * The implementation of the xajavac compiler. */ public class Xajavac extends DefaultCompilerAdapter { /** * Performs a compile using the Xajavac compiler. * @return true if the compilation succeeded * @throws BuildException on error */ public boolean execute() throws BuildException { attributes.log("Using xajavac compiler", Project.MSG_VERBOSE); String xajavac = "xajavac.jar"; if (project.getProperty("xajavac.path")!=null) { xajavac = project.getProperty("xajavac.path"); } Commandline cmd = new Commandline(); cmd.createArgument().setValue("-Xbootclasspath/p:"+xajavac); cmd.createArgument().setValue("com.sun.tools.javac.Main"); Path classpath = getCompileClasspath(); // For -sourcepath, use the "sourcepath" value if present. // Otherwise default to the "srcdir" value. Path sourcepath = null; if (compileSourcepath != null) { sourcepath = compileSourcepath; } else { sourcepath = src; } String memoryParameterPrefix = assumeJava11() ? "-J-" : "-J-X"; if (memoryInitialSize != null) { if (!attributes.isForkedJavac()) { attributes.log("Since fork is false, ignoring " + "memoryInitialSize setting.", Project.MSG_WARN); } else { cmd.createArgument().setValue(memoryParameterPrefix + "ms" + memoryInitialSize); } } if (memoryMaximumSize != null) { if (!attributes.isForkedJavac()) { attributes.log("Since fork is false, ignoring " + "memoryMaximumSize setting.", Project.MSG_WARN); } else { cmd.createArgument().setValue(memoryParameterPrefix + "mx" + memoryMaximumSize); } } if (attributes.getNowarn()) { cmd.createArgument().setValue("-nowarn"); } if (deprecation) { cmd.createArgument().setValue("-deprecation"); } if (destDir != null) { cmd.createArgument().setValue("-d"); cmd.createArgument().setFile(destDir); } cmd.createArgument().setValue("-classpath"); // Just add "sourcepath" to classpath ( for JDK1.1 ) // as well as "bootclasspath" and "extdirs" if (assumeJava11()) { Path cp = new Path(project); Path bp = getBootClassPath(); if (bp.size() > 0) { cp.append(bp); } if (extdirs != null) { cp.addExtdirs(extdirs); } cp.append(classpath); cp.append(sourcepath); cmd.createArgument().setPath(cp); } else { cmd.createArgument().setPath(classpath); // If the buildfile specifies sourcepath="", then don't // output any sourcepath. if (sourcepath.size() > 0) { cmd.createArgument().setValue("-sourcepath"); cmd.createArgument().setPath(sourcepath); } if (target != null) { cmd.createArgument().setValue("-target"); cmd.createArgument().setValue(target); } Path bp = getBootClassPath(); if (bp.size() > 0) { cmd.createArgument().setValue("-bootclasspath"); cmd.createArgument().setPath(bp); } if (extdirs != null && extdirs.size() > 0) { cmd.createArgument().setValue("-extdirs"); cmd.createArgument().setPath(extdirs); } } if (encoding != null) { cmd.createArgument().setValue("-encoding"); cmd.createArgument().setValue(encoding); } if (debug) { String debugLevel = attributes.getDebugLevel(); if (debugLevel != null) { cmd.createArgument().setValue("-g:" + debugLevel); } else { cmd.createArgument().setValue("-g"); } } else if (getNoDebugArgument() != null) { cmd.createArgument().setValue(getNoDebugArgument()); } if (optimize) { cmd.createArgument().setValue("-O"); } if (depend) { if (assumeJava11()) { cmd.createArgument().setValue("-depend"); } else if (assumeJava12()) { cmd.createArgument().setValue("-Xdepend"); } else { attributes.log("depend attribute is not supported by the " + "modern compiler", Project.MSG_WARN); } } if (verbose) { cmd.createArgument().setValue("-verbose"); } addCurrentCompilerArgs(cmd); int firstFileName = cmd.size(); Path boot = getBootClassPath(); if (boot.size() > 0) { cmd.createArgument().setValue("-bootclasspath"); cmd.createArgument().setPath(boot); } if (attributes.getSource() != null && !assumeJava13()) { cmd.createArgument().setValue("-source"); String source = attributes.getSource(); if ((assumeJava14() || assumeJava15()) && (source.equals("1.1") || source.equals("1.2"))) { // support for -source 1.1 and -source 1.2 has been // added with JDK 1.4.2 - and isn't present in 1.5.0 either cmd.createArgument().setValue("1.3"); } else { cmd.createArgument().setValue(source); } } else if ((assumeJava15() || assumeJava16()) && attributes.getTarget() != null) { String t = attributes.getTarget(); if (t.equals("1.1") || t.equals("1.2") || t.equals("1.3") || t.equals("1.4")) { String s = t; if (t.equals("1.1")) { // 1.5.0 doesn't support -source 1.1 s = "1.2"; } attributes.log("", Project.MSG_WARN); attributes.log(" WARNING", Project.MSG_WARN); attributes.log("", Project.MSG_WARN); attributes.log("The -source switch defaults to 1.5 in JDK 1.5 and 1.6.", Project.MSG_WARN); attributes.log("If you specify -target " + t + " you now must also specify -source " + s + ".", Project.MSG_WARN); attributes.log("Ant will implicitly add -source " + s + " for you. Please change your build file.", Project.MSG_WARN); cmd.createArgument().setValue("-source"); cmd.createArgument().setValue(s); } } logAndAddFilesToCompile(cmd); String exec = getJavac().getExecutable(); cmd.setExecutable(exec == null ? "java" : exec); attributes.log("Command line: "+cmd.toString(), Project.MSG_VERBOSE); return executeExternalCompile(cmd.getCommandline(), firstFileName) == 0; } }