KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > jk > ant > compilers > MwldLinker


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.apache.jk.ant.compilers;
18
19 import java.io.File JavaDoc;
20 import java.io.FileWriter JavaDoc;
21 import java.io.IOException JavaDoc;
22 import java.io.PrintWriter JavaDoc;
23 import java.util.Enumeration JavaDoc;
24 import java.util.Vector JavaDoc;
25
26 import org.apache.jk.ant.JkData;
27 import org.apache.jk.ant.SoTask;
28 import org.apache.jk.ant.Source;
29 import org.apache.tools.ant.BuildException;
30 import org.apache.tools.ant.types.Commandline;
31 import org.apache.tools.ant.util.GlobPatternMapper;
32
33 /**
34  * Link using libtool.
35  *
36  * @author Costin Manolache
37  */

38 public class MwldLinker extends LinkerAdapter {
39     GlobPatternMapper lo_mapper=new GlobPatternMapper();
40     
41     public MwldLinker() {
42         super();
43         lo_mapper.setFrom("*.c");
44     lo_mapper.setTo("*.o");
45     }
46
47     public void setSoTask(SoTask so ) {
48         this.so=so;
49         so.setExtension(".nlm");
50         so.duplicateTo( this );
51         project.setProperty("netware", "true");
52
53         Enumeration JavaDoc e=altSoFiles.elements();
54         while (e.hasMoreElements())
55         {
56             JkData data = (JkData) e.nextElement();
57             String JavaDoc altSo = data.getValue();
58             if (altSo == null)
59                 continue;
60             else
61             {
62                 so.setTarget(altSo); // set it on the master copy
63
setTarget(altSo); // set it on ourself
64
break;
65             }
66         }
67     }
68
69     public void execute() throws BuildException {
70         findSourceFiles();
71         link(this.srcList);
72     }
73
74     /** Link using libtool.
75      */

76     public boolean link(Vector JavaDoc srcList) throws BuildException {
77         Commandline cmd = new Commandline();
78         File JavaDoc linkOpt = new File JavaDoc(buildDir, "link.opt");
79         File JavaDoc linkDef = new File JavaDoc(buildDir, "link.def");
80         boolean useLibC = false;
81
82         String JavaDoc libtool=project.getProperty("build.compiler.ld");
83         if(libtool==null) libtool="mwldnlm";
84
85         cmd.setExecutable( libtool );
86         
87         // All .obj files must be included
88
project.log( "Linking " + buildDir + "/" + soFile + ".nlm");
89
90         // create a .opt file and a .def file
91
PrintWriter JavaDoc linkOptPw = null;
92         PrintWriter JavaDoc linkDefPw = null;
93         try
94         {
95             String JavaDoc libBase = project.getProperty("build.compiler.base");
96             if (libBase == null) libBase = "\\tools\\mw\\5.3";
97             linkOptPw = new PrintWriter JavaDoc(new FileWriter JavaDoc(linkOpt));
98             linkDefPw = new PrintWriter JavaDoc(new FileWriter JavaDoc(linkDef));
99
100             // write the link flags out
101
linkOptPw.println("-warnings off");
102             linkOptPw.println("-zerobss");
103             linkOptPw.println("-o " + soFile + ".nlm");
104             linkOptPw.println("-map " + soFile + ".map");
105             linkOptPw.println("-nodefaults");
106
107             // add debug information in if requested
108
if (optG)
109             {
110                 linkOptPw.println("-g");
111                 linkOptPw.println("-sym internal");
112                 linkOptPw.println("-sym codeview4");
113                 linkOptPw.println("-osym " + soFile + ".NCV");
114             }
115
116             // write out any additional link options
117
Enumeration JavaDoc opts = linkOpts.elements();
118             while( opts.hasMoreElements() ) {
119                 JkData opt = (JkData) opts.nextElement();
120                 String JavaDoc option = opt.getValue();
121                 if( option == null ) continue;
122
123                 linkOptPw.println( option );
124                 option = option.toLowerCase();
125
126                 // check to see if we are building using LibC
127
if (option.indexOf("libc") > 0)
128                     useLibC = true;
129             }
130
131             // add the default startup code to the list of objects
132
if (useLibC)
133                 linkOptPw.println("-llibcpre.o");
134             else
135                 linkOptPw.println(libBase + "\\lib\\nwpre.obj");
136
137             // write the objects to link with to the .opt file
138
for( int i=0; i<srcList.size(); i++ ) {
139                 Source source=(Source)srcList.elementAt(i);
140                 File JavaDoc srcF = source.getFile();
141                 String JavaDoc name=srcF.getName();
142                 String JavaDoc targetNA[]=lo_mapper.mapFileName( name );
143                 if( targetNA!=null )
144                     linkOptPw.println( targetNA[0] );
145             }
146             linkOptPw.println("-commandfile link.def");
147
148             // write the dependant modules to the .def file
149
Enumeration JavaDoc mods = modules.elements();
150             while( mods.hasMoreElements() ) {
151                 JkData mod = (JkData) mods.nextElement();
152                 String JavaDoc name = mod.getValue();
153                 if( name==null ) continue;
154                 linkDefPw.println("module " + name);
155             }
156
157             // write the imports to link with to the .def file
158
Enumeration JavaDoc imps = imports.elements();
159             while( imps.hasMoreElements() ) {
160                 JkData imp = (JkData) imps.nextElement();
161                 String JavaDoc name = imp.getValue();
162                 if( name==null ) continue;
163                 if (imp.isFile())
164                     linkDefPw.println("Import @" + name);
165                 else
166                     linkDefPw.println("Import " + name);
167             }
168
169             // write the exports to link with to the .def file
170
Enumeration JavaDoc exps = exports.elements();
171             while( exps.hasMoreElements() ) {
172                 JkData exp = (JkData) exps.nextElement();
173                 String JavaDoc name = exp.getValue();
174                 if( name==null ) continue;
175                 if (exp.isFile())
176                     linkDefPw.println("Export @" + name);
177                 else
178                     linkDefPw.println("Export " + name);
179             }
180         }
181         catch (IOException JavaDoc ioe)
182         {
183             log("Caught IOException");
184         }
185         finally
186         {
187             if (linkOptPw != null)
188             {
189                 linkOptPw.close();
190             }
191
192             if (linkDefPw != null)
193             {
194                 linkDefPw.close();
195             }
196         }
197
198
199         cmd.createArgument().setValue( "@link.opt" );
200         int result=execute( cmd );
201         if( result!=0 ) {
202             log("Link failed " + result );
203             log("Command:" + cmd.toString());
204             log("Output:" );
205             if( outputstream!=null )
206                 log( outputstream.toString());
207             log("StdErr:" );
208             if( errorstream!=null )
209                 log( errorstream.toString());
210             
211             throw new BuildException("Link failed " + soFile);
212         }
213         if (null == project.getProperty("save.optionFiles"))
214         {
215             linkOpt.delete();
216             linkDef.delete();
217         }
218         closeStreamHandler();
219         return true;
220     }
221 }
222
223
Popular Tags