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 25 import org.apache.jk.ant.Def; 26 import org.apache.jk.ant.SoTask; 27 import org.apache.jk.ant.Source; 28 import org.apache.tools.ant.BuildException; 29 import org.apache.tools.ant.types.Commandline; 30 import org.apache.tools.ant.util.GlobPatternMapper; 31 32 40 public class MsvcCompiler extends CompilerAdapter { 41 GlobPatternMapper co_mapperS=new GlobPatternMapper(); 42 43 public MsvcCompiler() { 44 super(); 45 co_mapperS.setFrom("*.c"); 46 co_mapperS.setTo("*.obj"); 47 } 48 49 public String [] getTargetFiles( Source src ) { 50 File srcFile = src.getFile(); 51 String name=srcFile.getName(); 52 53 return co_mapperS.mapFileName( name ); 54 } 55 56 public void setSoTask(SoTask so ) { 57 this.so=so; 58 so.setExtension(".dll"); 59 so.duplicateTo( this ); 60 project.setProperty("win32", "true"); 61 if (optG) 62 project.setProperty("win32.debug", "true"); 63 else 64 project.setProperty("win32.release", "true"); 65 } 66 67 69 public void compileSingleFile(Source sourceObj) throws BuildException { 70 File f=sourceObj.getFile(); 71 String source=f.toString(); 72 String [] includeList = ( includes==null ) ? 73 new String [] {} : includes.getIncludePatterns(project); 74 75 Commandline cmd = new Commandline(); 76 77 String cc=project.getProperty("build.compiler.cc"); 78 if(cc==null) cc="cl"; 79 80 cmd.setExecutable( cc ); 81 addCCArgs( cmd, source, includeList ); 82 83 int result=execute( cmd ); 84 if( result!=0 ) { 85 log("Compile failed " + result + " " + source ); 86 log("Output:" ); 87 if( outputstream!=null ) 88 log( outputstream.toString()); 89 log("StdErr:" ); 90 if( errorstream!=null ) 91 log( errorstream.toString()); 92 93 throw new BuildException("Compile failed " + source); 94 } 95 File ccOpt = new File (buildDir, "cc.opt"); 96 ccOpt.delete(); 97 closeStreamHandler(); 98 99 } 100 101 103 private void addCCArgs(Commandline cmd, String source, String includeList[]) { 104 String extra_cflags=project.getProperty("build.native.extra_cflags"); 105 String localCflags=cflags; 106 File ccOpt = new File (buildDir, "cc.opt"); 107 if( localCflags==null ) { 108 localCflags=new String ("-nologo -W3 -GX -O2 -c"); 109 if( extra_cflags!=null ) { 110 localCflags+=" " + extra_cflags; 111 } 112 } 113 114 if (optG) 115 localCflags += " -MTd -Zi"; 116 else 117 localCflags += " -MT"; 118 119 PrintWriter ccpw = null; 121 try 122 { 123 ccpw = new PrintWriter (new FileWriter (ccOpt)); 124 ccpw.println(localCflags); 126 for( int i=0; i<includeList.length; i++ ) { 127 ccpw.print("-I"); 128 if (!includeList[i].startsWith("\"")) { 129 ccpw.print("\""); 130 } 131 ccpw.print(includeList[i] ); 132 if (!includeList[i].endsWith("\"")) { 133 ccpw.print("\""); 134 } 135 ccpw.println(); 136 } 137 138 if( defines.size() > 0 ) { 139 Enumeration defs=defines.elements(); 140 while( defs.hasMoreElements() ) { 141 Def d=(Def)defs.nextElement(); 142 String name=d.getName(); 143 String val=d.getValue(); 144 if( name==null ) continue; 145 String arg="-D" + name; 146 if( val!=null ) 147 arg+= "=" + val; 148 ccpw.println(arg); 149 } 150 } 151 } 152 catch (IOException ioe) 153 { 154 log("Caught IOException"); 155 } 156 finally 157 { 158 if (ccpw != null) 159 { 160 ccpw.close(); 161 } 162 } 163 164 project.log( "Compiling " + source); 165 cmd.createArgument().setValue( source ); 166 cmd.createArgument().setValue( "@cc.opt" ); 167 } 168 } 169 170 | Popular Tags |