001: /*
002: * Copyright 1999-2004 The Apache Software Foundation
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package org.apache.jk.ant.compilers;
018:
019: import java.io.File;
020: import java.util.Enumeration;
021: import java.util.Vector;
022:
023: import org.apache.jk.ant.JkData;
024: import org.apache.jk.ant.SoTask;
025: import org.apache.jk.ant.Source;
026: import org.apache.tools.ant.BuildException;
027: import org.apache.tools.ant.types.Commandline;
028: import org.apache.tools.ant.util.GlobPatternMapper;
029:
030: /**
031: * Link java using gcj.
032: *
033: * @author Costin Manolache
034: */
035: public class GcjLinker extends LinkerAdapter {
036: SoTask so;
037: protected static GlobPatternMapper co_mapper = new GlobPatternMapper();
038: static {
039: co_mapper.setFrom("*.java");
040: co_mapper.setTo("*.o");
041: }
042:
043: public GcjLinker() {
044: so = this ;
045: }
046:
047: public String[] getTargetFiles(Source src) {
048: File srcFile = src.getFile();
049: String name = srcFile.getName();
050: if (name.endsWith(".java"))
051: return co_mapper.mapFileName(name);
052: else
053: return new String[] { name + ".o" };
054: }
055:
056: public void setSoTask(SoTask so) {
057: this .so = so;
058: so.duplicateTo(this );
059: }
060:
061: public void execute() throws BuildException {
062: findSourceFiles();
063: link(this .srcList);
064: }
065:
066: /** Link using libtool.
067: */
068: public boolean link(Vector srcList) throws BuildException {
069: // link( srcList, false );
070: link(srcList, true);
071: return true;
072: }
073:
074: public boolean link(Vector srcList, boolean shared)
075: throws BuildException {
076: Commandline cmd = new Commandline();
077:
078: cmd.setExecutable("gcj");
079:
080: if (shared)
081: cmd.createArgument().setValue("--shared");
082: cmd.createArgument().setValue("-o");
083: if (shared)
084: cmd.createArgument().setValue(soFile + ".so");
085: else
086: cmd.createArgument().setValue(soFile + ".a");
087:
088: project.log("Linking " + buildDir + "/" + soFile + ".so");
089:
090: // write out any additional link options
091: Enumeration opts = linkOpts.elements();
092: while (opts.hasMoreElements()) {
093: JkData opt = (JkData) opts.nextElement();
094: String option = opt.getValue();
095: if (option == null)
096: continue;
097:
098: cmd.createArgument().setValue(option);
099: }
100:
101: for (int i = 0; i < srcList.size(); i++) {
102: Source source = (Source) srcList.elementAt(i);
103: File f1 = new File(buildDir, source.getPackage());
104: File srcF = new File(f1, getTargetFiles(source)[0]);
105: cmd.createArgument().setValue(srcF.toString());
106: }
107:
108: int result = execute(cmd);
109: if (result != 0) {
110: log("Link failed " + result);
111: log("Command:" + cmd.toString());
112: log("Output:");
113: if (outputstream != null)
114: log(outputstream.toString());
115: log("StdErr:");
116: if (errorstream != null)
117: log(errorstream.toString());
118:
119: throw new BuildException("Link failed " + soFile);
120: }
121: closeStreamHandler();
122:
123: return true;
124: }
125: }
|