1 16 17 package org.apache.jk.ant.compilers; 18 19 import java.io.File ; 20 import java.util.Enumeration ; 21 import java.util.Vector ; 22 23 import org.apache.jk.ant.Def; 24 import org.apache.jk.ant.SoTask; 25 import org.apache.jk.ant.Source; 26 import org.apache.tools.ant.BuildException; 27 import org.apache.tools.ant.types.Commandline; 28 29 31 32 47 public abstract class CompilerAdapter extends SoTask { 48 SoTask so; 49 Vector compileList; 50 51 public CompilerAdapter() { 52 so=this; 53 }; 54 55 public void setSoTask(SoTask so ) { 56 this.so=so; 57 so.duplicateTo( this ); 58 } 59 60 66 69 public abstract String [] getTargetFiles( Source src ); 70 71 public void execute() throws BuildException { 72 super.findSourceFiles(); 73 Vector compileList=findCompileList(srcList); 74 compile( compileList ); 75 } 76 77 83 public boolean needCompile( Source source ) { 84 File srcF = source.getFile(); 87 if( !srcF.exists() ) { 88 if( debug > 0 ) 89 log("No source file " + srcF ); 90 return false; 91 } 92 93 String targetNames[]= getTargetFiles( source ); 94 if( targetNames==null || targetNames.length==0 ) { 95 if( debug > 0 ) 96 log("No target files " + srcF ); 97 return true; } 99 String targetName=targetNames[0]; 100 101 String targetDir=source.getPackage(); 102 File f1=new File ( buildDir, targetDir ); 103 File target=new File ( f1, targetName ); 104 if( ! target.exists() ) { 106 if( debug > 0 ) 107 log("Target doesn't exist " + target ); 108 return true; 109 } 110 if( oldestO > target.lastModified() ) { 111 oldestO=target.lastModified(); 112 oldestOFile=target; 113 } 114 if( srcF.lastModified() > target.lastModified() ) 115 return true; 116 117 if( debug > 0 ) 118 log("No need to compile " + srcF + " target " + target ); 119 return false; 120 } 121 122 124 public void removeOFiles( Vector srcList ) { 125 for (int i = 0; i < srcList.size(); i++) { 126 Source source=(Source)srcList.elementAt(i); 128 String targetNA[]=getTargetFiles(source); 129 if( targetNA==null ) 130 continue; 131 String targetDir=source.getPackage(); 132 File f1=new File ( buildDir, targetDir ); 133 for( int j=0; j<targetNA.length; j++ ) { 134 File target=new File ( f1, targetNA[j] ); 135 if( target.exists() ) { 137 target.delete(); 139 log("Removing " + target ); 140 } 141 } 142 } 143 } 144 145 long oldestO=System.currentTimeMillis(); 147 File oldestOFile=null; 148 149 151 protected Vector findCompileList(Vector srcList) throws BuildException { 152 Vector compileList=new Vector (); 153 154 for (int i = 0; i < srcList.size(); i++) { 155 Source source=(Source)srcList.elementAt(i); 156 File srcFile=source.getFile(); 157 158 if (!srcFile.exists()) { 159 throw new BuildException("Source \"" + srcFile.getPath() + 160 "\" does not exist!", location); 161 } 162 163 if( needCompile( source ) ) 165 compileList.addElement( source ); 166 } 167 168 if( checkDepend(oldestO, oldestOFile) ) { 169 log("Dependency expired, removing " 170 + srcList.size() + " .o files and doing a full build "); 171 removeOFiles(srcList); 172 compileList=new Vector (); 173 for(int i=0; i<srcList.size(); i++ ) { 174 Source source=(Source)srcList.elementAt(i); 175 compileList.addElement( source ); 176 } 177 return compileList; 178 } 179 180 181 return compileList; 182 } 183 184 186 public Vector getCompiledFiles() { 187 return compileList; 188 } 189 190 195 public void compile(Vector sourceFiles ) throws BuildException { 196 compileList=findCompileList(sourceFiles); 197 198 log("Compiling " + compileList.size() + " out of " + sourceFiles.size()); 199 Enumeration en=compileList.elements(); 200 while( en.hasMoreElements() ) { 201 Source source=(Source)en.nextElement(); 202 compileSingleFile(source); 203 } 204 } 205 206 208 public void compileSingleFile(Source sourceObj) throws BuildException { 209 } 210 211 212 protected void displayError( int result, String source, Commandline cmd ) 213 throws BuildException 214 { 215 if( result == 0 ) { 216 String err=errorstream.toString(); 217 if(err==null ) return; 218 if( err.indexOf( "warning" ) <= 0 ) 219 return; 220 log("Warnings: "); 221 log( err ); 222 return; 223 } 224 225 log("Compile failed " + result + " " + source ); 226 log("Command:" + cmd.toString()); 227 log("Output:" ); 228 if( outputstream!=null ) 229 log( outputstream.toString()); 230 log("StdErr:" ); 231 if( errorstream!=null ) 232 log( errorstream.toString()); 233 234 throw new BuildException("Compile failed " + source); 235 } 236 237 protected void addIncludes(Commandline cmd) { 238 String [] includeList = ( includes==null ) ? 239 new String [] {} : includes.getIncludePatterns(project); 240 for( int i=0; i<includeList.length; i++ ) { 241 cmd.createArgument().setValue("-I" + includeList[i] ); 242 } 243 } 244 245 247 protected void addExtraFlags(Commandline cmd ) { 248 String extra_cflags=project.getProperty("build.native.extra_cflags"); 249 String localCflags=cflags; 250 if( localCflags==null ) { 251 localCflags=extra_cflags; 252 } else { 253 if( extra_cflags!=null ) { 254 localCflags+=" " + extra_cflags; 255 } 256 } 257 if( localCflags != null ) 258 cmd.createArgument().setLine( localCflags ); 259 } 260 261 protected void addDefines( Commandline cmd ) { 262 String os=System.getProperty("java.os"); 264 265 if( defines.size() > 0 ) { 266 Enumeration defs=defines.elements(); 267 while( defs.hasMoreElements() ) { 268 Def d=(Def)defs.nextElement(); 269 String name=d.getName(); 270 String val=d.getValue(); 271 if( name==null ) continue; 272 String arg="-D" + name; 273 if( val!=null ) 274 arg+= "=" + val; 275 cmd.createArgument().setValue( arg ); 276 } 277 } 278 } 279 280 protected void addDebug(Commandline cmd) { 281 } 282 283 protected void addOptimize( Commandline cmd ) { 284 } 285 286 protected void addProfile( Commandline cmd ) { 287 } 288 } 289 | Popular Tags |