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.io.FileWriter;
021: import java.io.IOException;
022: import java.io.PrintWriter;
023: import java.util.Enumeration;
024: import java.util.Vector;
025:
026: import org.apache.jk.ant.JkData;
027: import org.apache.jk.ant.SoTask;
028: import org.apache.jk.ant.Source;
029: import org.apache.tools.ant.BuildException;
030: import org.apache.tools.ant.types.Commandline;
031: import org.apache.tools.ant.util.GlobPatternMapper;
032:
033: /**
034: * Link using libtool.
035: *
036: * @author Costin Manolache
037: */
038: public class MwldLinker extends LinkerAdapter {
039: GlobPatternMapper lo_mapper = new GlobPatternMapper();
040:
041: public MwldLinker() {
042: super ();
043: lo_mapper.setFrom("*.c");
044: lo_mapper.setTo("*.o");
045: }
046:
047: public void setSoTask(SoTask so) {
048: this .so = so;
049: so.setExtension(".nlm");
050: so.duplicateTo(this );
051: project.setProperty("netware", "true");
052:
053: Enumeration e = altSoFiles.elements();
054: while (e.hasMoreElements()) {
055: JkData data = (JkData) e.nextElement();
056: String altSo = data.getValue();
057: if (altSo == null)
058: continue;
059: else {
060: so.setTarget(altSo); // set it on the master copy
061: setTarget(altSo); // set it on ourself
062: break;
063: }
064: }
065: }
066:
067: public void execute() throws BuildException {
068: findSourceFiles();
069: link(this .srcList);
070: }
071:
072: /** Link using libtool.
073: */
074: public boolean link(Vector srcList) throws BuildException {
075: Commandline cmd = new Commandline();
076: File linkOpt = new File(buildDir, "link.opt");
077: File linkDef = new File(buildDir, "link.def");
078: boolean useLibC = false;
079:
080: String libtool = project.getProperty("build.compiler.ld");
081: if (libtool == null)
082: libtool = "mwldnlm";
083:
084: cmd.setExecutable(libtool);
085:
086: // All .obj files must be included
087: project.log("Linking " + buildDir + "/" + soFile + ".nlm");
088:
089: // create a .opt file and a .def file
090: PrintWriter linkOptPw = null;
091: PrintWriter linkDefPw = null;
092: try {
093: String libBase = project.getProperty("build.compiler.base");
094: if (libBase == null)
095: libBase = "\\tools\\mw\\5.3";
096: linkOptPw = new PrintWriter(new FileWriter(linkOpt));
097: linkDefPw = new PrintWriter(new FileWriter(linkDef));
098:
099: // write the link flags out
100: linkOptPw.println("-warnings off");
101: linkOptPw.println("-zerobss");
102: linkOptPw.println("-o " + soFile + ".nlm");
103: linkOptPw.println("-map " + soFile + ".map");
104: linkOptPw.println("-nodefaults");
105:
106: // add debug information in if requested
107: if (optG) {
108: linkOptPw.println("-g");
109: linkOptPw.println("-sym internal");
110: linkOptPw.println("-sym codeview4");
111: linkOptPw.println("-osym " + soFile + ".NCV");
112: }
113:
114: // write out any additional link options
115: Enumeration opts = linkOpts.elements();
116: while (opts.hasMoreElements()) {
117: JkData opt = (JkData) opts.nextElement();
118: String option = opt.getValue();
119: if (option == null)
120: continue;
121:
122: linkOptPw.println(option);
123: option = option.toLowerCase();
124:
125: // check to see if we are building using LibC
126: if (option.indexOf("libc") > 0)
127: useLibC = true;
128: }
129:
130: // add the default startup code to the list of objects
131: if (useLibC)
132: linkOptPw.println("-llibcpre.o");
133: else
134: linkOptPw.println(libBase + "\\lib\\nwpre.obj");
135:
136: // write the objects to link with to the .opt file
137: for (int i = 0; i < srcList.size(); i++) {
138: Source source = (Source) srcList.elementAt(i);
139: File srcF = source.getFile();
140: String name = srcF.getName();
141: String targetNA[] = lo_mapper.mapFileName(name);
142: if (targetNA != null)
143: linkOptPw.println(targetNA[0]);
144: }
145: linkOptPw.println("-commandfile link.def");
146:
147: // write the dependant modules to the .def file
148: Enumeration mods = modules.elements();
149: while (mods.hasMoreElements()) {
150: JkData mod = (JkData) mods.nextElement();
151: String name = mod.getValue();
152: if (name == null)
153: continue;
154: linkDefPw.println("module " + name);
155: }
156:
157: // write the imports to link with to the .def file
158: Enumeration imps = imports.elements();
159: while (imps.hasMoreElements()) {
160: JkData imp = (JkData) imps.nextElement();
161: String name = imp.getValue();
162: if (name == null)
163: continue;
164: if (imp.isFile())
165: linkDefPw.println("Import @" + name);
166: else
167: linkDefPw.println("Import " + name);
168: }
169:
170: // write the exports to link with to the .def file
171: Enumeration exps = exports.elements();
172: while (exps.hasMoreElements()) {
173: JkData exp = (JkData) exps.nextElement();
174: String name = exp.getValue();
175: if (name == null)
176: continue;
177: if (exp.isFile())
178: linkDefPw.println("Export @" + name);
179: else
180: linkDefPw.println("Export " + name);
181: }
182: } catch (IOException ioe) {
183: System.out.println("Caught IOException");
184: } finally {
185: if (linkOptPw != null) {
186: linkOptPw.close();
187: }
188:
189: if (linkDefPw != null) {
190: linkDefPw.close();
191: }
192: }
193:
194: cmd.createArgument().setValue("@link.opt");
195: int result = execute(cmd);
196: if (result != 0) {
197: log("Link failed " + result);
198: log("Command:" + cmd.toString());
199: log("Output:");
200: if (outputstream != null)
201: log(outputstream.toString());
202: log("StdErr:");
203: if (errorstream != null)
204: log(errorstream.toString());
205:
206: throw new BuildException("Link failed " + soFile);
207: }
208: if (null == project.getProperty("save.optionFiles")) {
209: linkOpt.delete();
210: linkDef.delete();
211: }
212: closeStreamHandler();
213: return true;
214: }
215: }
|