1 16 17 package org.apache.jk.ant.compilers; 18 19 import java.io.File ; 20 import java.io.FileWriter ; 21 import java.io.IOException ; 22 import java.io.PrintWriter ; 23 import java.util.Enumeration ; 24 import java.util.Vector ; 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 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 srcList) throws BuildException { 68 Commandline cmd = new Commandline(); 69 File linkOpt = new File (buildDir, "link.opt"); 70 File linkDef = new File (buildDir, "link.def"); 71 72 String libtool=project.getProperty("build.compiler.ld"); 73 if(libtool==null) libtool="link"; 74 75 cmd.setExecutable( libtool ); 76 77 project.log( "Linking " + buildDir + "/" + soFile + ".dll"); 79 80 PrintWriter linkOptPw = null; 82 PrintWriter linkDefPw = null; 83 try 84 { 85 linkOptPw = new PrintWriter (new FileWriter (linkOpt)); 86 linkDefPw = new PrintWriter (new FileWriter (linkDef)); 87 88 linkOptPw.print(" "); 90 Enumeration imps = imports.elements(); 91 while( imps.hasMoreElements() ) { 92 JkData imp = (JkData) imps.nextElement(); 93 String name = imp.getValue(); 94 if( name==null ) continue; 95 linkOptPw.print(name+" "); 96 } 97 98 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 Enumeration opts = linkOpts.elements(); 108 while( opts.hasMoreElements() ) { 109 JkData opt = (JkData) opts.nextElement(); 110 String option = opt.getValue(); 111 if( option == null ) continue; 112 linkOptPw.println( option ); 113 } 114 115 if (optG) 117 { 118 linkOptPw.print("/debug "); 119 } 120 linkOptPw.println("/def:link.def"); 122 for( int i=0; i<srcList.size(); i++ ) { 124 Source source=(Source)srcList.elementAt(i); 125 File srcF = source.getFile(); 126 String name=srcF.getName(); 127 String targetNA[]=co_mapper.mapFileName( name ); 128 if( targetNA!=null ) 129 linkOptPw.println( targetNA[0] ); 130 } 131 Enumeration ress = resources.elements(); 133 while( ress.hasMoreElements() ) { 134 JkData res = (JkData) ress.nextElement(); 135 String name = res.getValue(); 136 if( name==null ) continue; 137 linkOptPw.println(name); 138 } 139 140 linkDefPw.println("LIBRARY\t\""+soFile+"\""); 142 143 Enumeration exps = exports.elements(); 145 if ( exps.hasMoreElements() ) 146 { 147 linkDefPw.println("EXPORTS"); 148 while( exps.hasMoreElements() ) { 149 JkData exp = (JkData) exps.nextElement(); 150 String name = exp.getValue(); 151 if( name==null ) continue; 152 linkDefPw.println("\t" + name); 153 } 154 } 155 } 156 catch (IOException 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 closeStreamHandler(); 191 return true; 192 } 193 } 194 195 | Popular Tags |