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 using libtool.
032: *
033: * @author Costin Manolache
034: */
035: public class LibtoolLinker extends LinkerAdapter {
036: SoTask so;
037: GlobPatternMapper lo_mapper = new GlobPatternMapper();
038:
039: public LibtoolLinker() {
040: so = this ;
041: lo_mapper.setFrom("*.c");
042: lo_mapper.setTo("*.lo");
043: }
044:
045: /** Link using libtool.
046: */
047: public boolean link(Vector srcList) throws BuildException {
048: so.duplicateTo(this );
049: Commandline cmd = new Commandline();
050:
051: String libtool = project.getProperty("build.native.libtool");
052: if (libtool == null)
053: libtool = "libtool";
054:
055: cmd.setExecutable(libtool);
056:
057: cmd.createArgument().setValue("--mode=link");
058:
059: String cc = project.getProperty("build.native.cc");
060: if (cc == null)
061: cc = "cc";
062:
063: cmd.createArgument().setValue(cc);
064:
065: cmd.createArgument().setValue("-module");
066: cmd.createArgument().setValue("-avoid-version");
067: cmd.createArgument().setValue("-rpath");
068: cmd.createArgument().setValue(buildDir.getAbsolutePath());
069:
070: cmd.createArgument().setValue("-o");
071: cmd.createArgument().setValue(soFile + ".la");
072:
073: if (profile)
074: cmd.createArgument().setValue("-pg");
075:
076: // write out any additional link options
077: Enumeration opts = linkOpts.elements();
078: while (opts.hasMoreElements()) {
079: JkData opt = (JkData) opts.nextElement();
080: String option = opt.getValue();
081: if (option == null)
082: continue;
083:
084: cmd.createArgument().setValue(option);
085: }
086:
087: // All .o files must be included
088: project.log("Linking " + buildDir + "/" + soFile + ".so");
089:
090: if (libs != null) {
091: String libsA[] = libs.list();
092: for (int i = 0; i < libsA.length; i++) {
093: cmd.createArgument().setValue("-l" + libsA[i]);
094: //XXX debug
095: project.log("XXX -l" + libsA[i]);
096: }
097: }
098:
099: for (int i = 0; i < srcList.size(); i++) {
100: Source sourceObj = (Source) srcList.elementAt(i);
101:
102: File ff = new File(buildDir, sourceObj
103: .getTargetFile(lo_mapper));
104: cmd.createArgument().setValue(ff.toString());
105: }
106:
107: int result = execute(cmd);
108: if (result != 0) {
109: log("Link failed " + result);
110: log("Command:" + cmd.toString());
111: log("Output:");
112: if (outputstream != null)
113: log(outputstream.toString());
114: log("StdErr:");
115: if (errorstream != null)
116: log(errorstream.toString());
117:
118: throw new BuildException("Link failed " + soFile);
119: }
120: closeStreamHandler();
121:
122: executeLibtoolInstall();
123: return true;
124: }
125:
126: /** Final step using libtool.
127: */
128: private void executeLibtoolInstall() throws BuildException {
129: Commandline cmd = new Commandline();
130:
131: String libtool = project.getProperty("build.native.libtool");
132: if (libtool == null)
133: libtool = "libtool";
134:
135: cmd.setExecutable(libtool);
136:
137: cmd.createArgument().setValue("--mode=install");
138:
139: cmd.createArgument().setValue("cp");
140:
141: File laFile = new File(buildDir, soFile + ".la");
142: cmd.createArgument().setValue(laFile.getAbsolutePath());
143:
144: File soFileF = new File(buildDir, soFile + ".so");
145: cmd.createArgument().setValue(soFileF.getAbsolutePath());
146:
147: int result = execute(cmd);
148: if (result != 0) {
149: log("Link/install failed " + result);
150: log("Command:" + cmd.toString());
151: log("Output:");
152: if (outputstream != null)
153: log(outputstream.toString());
154: log("StdErr:");
155: if (errorstream != null)
156: log(errorstream.toString());
157:
158: throw new BuildException("Link failed " + soFile);
159: }
160: closeStreamHandler();
161: }
162: }
|