KickJava   Java API By Example, From Geeks To Geeks.

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


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 MSVC Linker
35  *
36  * @author Costin Manolache
37  * @author Ignacio J. Ortega
38  * @author Mike Anderson
39  * @author Larry Isaacs
40  */

41 public class MsvcLinker extends LinkerAdapter {
42     SoTask so;
43     GlobPatternMapper co_mapper=new GlobPatternMapper();
44     
45     public MsvcLinker() {
46         so=this;
47         co_mapper.setFrom("*.c");
48         co_mapper.setTo("*.obj");
49     }
50
51     public void setSoTask(SoTask so ) {
52         this.so=so;
53         so.setExtension(".dll");
54         so.duplicateTo( this );
55         project.setProperty("win32", "true");
56         if (optG)
57             project.setProperty("win32.debug", "true");
58         else
59             project.setProperty("win32.release", "true");
60     }
61
62     public void execute() throws BuildException {
63         findSourceFiles();
64         link(this.srcList);
65     }
66
67     public boolean link(Vector JavaDoc srcList) throws BuildException {
68         Commandline cmd = new Commandline();
69         File JavaDoc linkOpt = new File JavaDoc(buildDir, "link.opt");
70         File JavaDoc linkDef = new File JavaDoc(buildDir, "link.def");
71
72         String JavaDoc libtool=project.getProperty("build.compiler.ld");
73         if(libtool==null) libtool="link";
74
75         cmd.setExecutable( libtool );
76
77         // All .obj files must be included
78
project.log( "Linking " + buildDir + "/" + soFile + ".dll");
79
80         // create a .opt file and a .def file
81
PrintWriter JavaDoc linkOptPw = null;
82         PrintWriter JavaDoc linkDefPw = null;
83         try
84         {
85             linkOptPw = new PrintWriter JavaDoc(new FileWriter JavaDoc(linkOpt));
86             linkDefPw = new PrintWriter JavaDoc(new FileWriter JavaDoc(linkDef));
87
88             // write the imports to link with to the .opt file
89
linkOptPw.print(" ");
90             Enumeration JavaDoc imps = imports.elements();
91             while( imps.hasMoreElements() ) {
92                 JkData imp = (JkData) imps.nextElement();
93                 String JavaDoc name = imp.getValue();
94                 if( name==null ) continue;
95                 linkOptPw.print(name+" ");
96             }
97
98             // write the link flags out
99

100             linkOptPw.print("/machine:I386 ");
101             linkOptPw.print("/out:" + soFile + ".dll ");
102             linkOptPw.print("/nologo ");
103             linkOptPw.print("/dll ");
104             linkOptPw.print("/incremental:no ");
105
106             // write out any additional link options
107
Enumeration JavaDoc opts = linkOpts.elements();
108             while( opts.hasMoreElements() ) {
109                 JkData opt = (JkData) opts.nextElement();
110                 String JavaDoc option = opt.getValue();
111                 if( option == null ) continue;
112                 linkOptPw.println( option );
113             }
114
115             // add debug information in if requested
116
if (optG)
117             {
118                 linkOptPw.print("/debug ");
119             }
120             // def file
121
linkOptPw.println("/def:link.def");
122             // write the objects to link with to the .opt file
123
for( int i=0; i<srcList.size(); i++ ) {
124                 Source source=(Source)srcList.elementAt(i);
125                 File JavaDoc srcF = source.getFile();
126                 String JavaDoc name=srcF.getName();
127                 String JavaDoc targetNA[]=co_mapper.mapFileName( name );
128                 if( targetNA!=null )
129                     linkOptPw.println( targetNA[0] );
130             }
131             // Write the resources to link to .opt file
132
Enumeration JavaDoc ress = resources.elements();
133             while( ress.hasMoreElements() ) {
134                 JkData res = (JkData) ress.nextElement();
135                 String JavaDoc name = res.getValue();
136                 if( name==null ) continue;
137                 linkOptPw.println(name);
138             }
139             
140             // Write the library name to the def file
141
linkDefPw.println("LIBRARY\t\""+soFile+"\"");
142
143             // write the exported symbols to the .def file
144
Enumeration JavaDoc exps = exports.elements();
145             if ( exps.hasMoreElements() )
146             {
147                 linkDefPw.println("EXPORTS");
148                 while( exps.hasMoreElements() ) {
149                     JkData exp = (JkData) exps.nextElement();
150                     String JavaDoc name = exp.getValue();
151                     if( name==null ) continue;
152                     linkDefPw.println("\t" + name);
153                 }
154             }
155         }
156         catch (IOException JavaDoc ioe)
157         {
158             log("Caught IOException");
159         }
160         finally
161         {
162             if (linkOptPw != null)
163             {
164                 linkOptPw.close();
165             }
166
167             if (linkDefPw != null)
168             {
169                 linkDefPw.close();
170             }
171         }
172
173
174         cmd.createArgument().setValue( "@link.opt" );
175         int result=execute( cmd );
176         if( result!=0 ) {
177             log("Link failed " + result );
178             log("Command:" + cmd.toString());
179             log("Output:" );
180             if( outputstream!=null )
181                 log( outputstream.toString());
182             log("StdErr:" );
183             if( errorstream!=null )
184                 log( errorstream.toString());
185
186             throw new BuildException("Link failed " + soFile);
187         }
188         // linkOpt.delete();
189
// linkDef.delete();
190
closeStreamHandler();
191         return true;
192     }
193 }
194
195
Popular Tags