1 16 17 package org.apache.jk.ant; 18 19 import java.io.ByteArrayOutputStream ; 20 import java.io.File ; 21 import java.io.IOException ; 22 import java.io.PrintStream ; 23 import java.util.Enumeration ; 24 import java.util.Vector ; 25 26 import org.apache.jk.ant.compilers.CcCompiler; 27 import org.apache.jk.ant.compilers.CompilerAdapter; 28 import org.apache.jk.ant.compilers.GcjCompiler; 29 import org.apache.jk.ant.compilers.GcjLinker; 30 import org.apache.jk.ant.compilers.LibtoolCompiler; 31 import org.apache.jk.ant.compilers.LibtoolLinker; 32 import org.apache.jk.ant.compilers.LinkerAdapter; 33 import org.apache.jk.ant.compilers.MsvcCompiler; 34 import org.apache.jk.ant.compilers.MsvcLinker; 35 import org.apache.jk.ant.compilers.MwccCompiler; 36 import org.apache.jk.ant.compilers.MwldLinker; 37 import org.apache.tools.ant.BuildException; 38 import org.apache.tools.ant.DirectoryScanner; 39 import org.apache.tools.ant.Task; 40 import org.apache.tools.ant.taskdefs.Execute; 41 import org.apache.tools.ant.taskdefs.ExecuteStreamHandler; 42 import org.apache.tools.ant.taskdefs.PumpStreamHandler; 43 import org.apache.tools.ant.types.Commandline; 44 import org.apache.tools.ant.types.FileSet; 45 import org.apache.tools.ant.types.Path; 46 import org.apache.tools.ant.types.PatternSet; 47 48 62 67 68 69 86 public class SoTask extends Task { 87 protected String apxs; 88 protected Vector src; protected PatternSet includes; 91 protected Path depends; 92 protected Path libs; 93 protected String module; 94 protected String soFile; 95 protected String soExt = ".so"; 96 protected String cflags; 97 protected File buildDir; 98 protected int debug; 99 100 protected boolean optG=true; 101 protected boolean optWgcc=true; 102 protected boolean optimize=false; 103 protected boolean profile=false; 104 protected Vector defines = new Vector (); 105 protected Vector imports = new Vector (); protected Vector exports = new Vector (); protected Vector modules = new Vector (); protected Vector linkOpts = new Vector (); protected Vector altSoFiles = new Vector (); protected Vector resources = new Vector (); 112 protected Vector srcList=new Vector (); 115 protected CompilerAdapter compiler; 116 118 public SoTask() {}; 119 120 public void duplicateTo(SoTask so) { 123 so.project=project; 125 so.target=target; 126 so.location=location; 127 so.taskName=taskName; 128 so.taskType=taskType; 129 130 so.apxs=apxs; 131 so.src=src; 132 so.includes=includes; 133 so.depends=depends; 134 so.libs=libs; 135 so.module=module; 136 so.soFile=soFile; 137 so.soExt=soExt; 138 so.cflags=cflags; 139 so.buildDir=buildDir; 140 so.debug=debug; 141 so.optG=optG; 142 so.optWgcc=optWgcc; 143 so.optimize=optimize; 144 so.profile=profile; 145 so.defines=defines; 146 so.imports=imports; 147 so.exports=exports; 148 so.resources=resources; 149 so.modules=modules; 150 so.linkOpts=linkOpts; 151 so.srcList=srcList; 152 so.compiler=compiler; 154 so.altSoFiles=altSoFiles; 156 } 157 158 160 public void setSoFile(String s ) { 161 soFile=s; 162 } 163 164 166 public void setDebug(boolean b) { 167 optG=b; 168 } 169 170 172 public void setOptimize(boolean b) { 173 optimize=b; 174 } 175 176 178 public void setProfile(boolean b) { 179 profile=b; 180 } 181 182 184 public void setGccWarn(boolean b) { 185 optWgcc=b; 186 } 187 188 190 public void setTaskDebug(int i) { 191 debug=i; 192 } 193 194 197 public void addDef(Def var ) { 198 var.setProject( project ); 199 defines.addElement(var); 200 } 201 202 207 public void addImport(JkData imp) { 208 imp.setProject( project ); 209 imports.add(imp); 210 } 211 212 217 public void addExport(JkData exp) { 218 exp.setProject( project ); 219 exports.add(exp); 220 } 221 222 227 public void addResource(JkData res) { 228 res.setProject( project ); 229 resources.add(res); 230 } 231 232 237 public void addLinkOpt(JkData option) { 238 option.setProject( project ); 239 linkOpts.add(option); 240 } 241 242 247 public void addNLMModule(JkData module) { 248 module.setProject( project ); 249 modules.add(module); 250 } 251 252 257 public void addAltSoFile(JkData altSo) { 258 altSo.setProject( project ); 259 altSoFiles.add(altSo); 260 } 261 262 266 public void setTarget(String s ) { 267 soFile=s; 268 } 269 270 273 public void setExtension(String s ) { 274 soExt=s; 275 } 276 277 280 public void setBuildDir( File s ) { 281 buildDir=s; 282 } 283 284 public void setCflags(String s ) { 285 cflags=s; 286 } 287 288 290 public void setSoDir( String s ) { 291 292 } 293 294 public void addJniConfig( JniConfig jniCfg ) { 295 296 } 297 298 public void addApacheConfig( ApacheConfig apacheCfg ) { 299 300 } 301 302 303 308 public void addSrc(FileSet fl) { 309 if( SRC==null ) SRC=new Vector (); 310 src.addElement(fl); 311 } 312 313 316 public PatternSet createIncludes() { 317 includes=new PatternSet(); return includes; 319 } 320 321 330 public Path createDepends() { 331 depends=new Path(project); 332 return depends; 333 } 334 335 338 public Path createLibs() { 339 libs=new Path(project); 340 return libs; 341 } 342 343 344 348 public void setModule(String modName) { 349 this.module = modName; } 351 352 355 public void execute() throws BuildException { 358 compiler=findCompilerAdapter(); 359 LinkerAdapter linker=findLinkerAdapter(); 361 362 if( soFile==null ) 363 throw new BuildException("No target ( " + soExt + " file )"); 364 if (src == null) 365 throw new BuildException("No source files"); 366 367 findSourceFiles(); 370 371 this.duplicateTo(compiler); 373 compiler.compile( srcList ); 374 375 File soTarget=new File ( buildDir, soFile + soExt ); 377 if( compiler.getCompiledFiles().size() == 0 && soTarget.exists()) { 378 return; 380 } 381 382 this.duplicateTo(linker); 383 linker.link(srcList); 384 } 385 386 public CompilerAdapter findCompilerAdapter() { 387 CompilerAdapter compilerAdapter; 388 String cc; 389 cc=project.getProperty("build.compiler.cc"); 390 if( cc!=null ) { 391 if( "cc".equals( cc ) ) { 392 compilerAdapter=new CcCompiler(); 393 compilerAdapter.setSoTask( this ); 394 return compilerAdapter; 395 } 396 if( "gcj".equals( cc ) ) { 397 compilerAdapter=new GcjCompiler(); 398 compilerAdapter.setSoTask( this ); 399 return compilerAdapter; 400 } 401 if( cc.indexOf("mwccnlm") != -1 ) { 402 compilerAdapter=new MwccCompiler(); 403 compilerAdapter.setSoTask( this ); 404 return compilerAdapter; 405 } 406 if( cc.indexOf("cl") != -1 ) { 407 compilerAdapter=new MsvcCompiler(); 408 compilerAdapter.setSoTask( this ); 409 return compilerAdapter; 410 } 411 } 412 413 compilerAdapter=new LibtoolCompiler(); 414 compilerAdapter.setSoTask( this ); 415 return compilerAdapter; 416 } 417 418 public LinkerAdapter findLinkerAdapter() { 419 LinkerAdapter linkerAdapter; 420 String ld=project.getProperty("build.compiler.ld"); 421 if( ld!=null ) { 422 if( ld.indexOf("mwldnlm") != -1 ) { 423 linkerAdapter=new MwldLinker(); 424 linkerAdapter.setSoTask( this ); 425 return linkerAdapter; 426 } 427 if( ld.indexOf("link") != -1 ) { 428 linkerAdapter=new MsvcLinker(); 429 linkerAdapter.setSoTask( this ); 430 return linkerAdapter; 431 } 432 } 438 439 String cc=project.getProperty("build.compiler.cc"); 440 if( "gcj".equals( cc ) ) { 441 linkerAdapter=new GcjLinker(); 442 linkerAdapter.setSoTask( this ); 443 return linkerAdapter; 444 } 445 446 447 linkerAdapter=new LibtoolLinker(); 448 linkerAdapter.setSoTask( this ); 449 return linkerAdapter; 450 } 451 452 454 public void findSourceFiles() { 455 if (buildDir == null) buildDir = project.getBaseDir(); 456 457 Enumeration e=src.elements(); 458 while( e.hasMoreElements() ) { 459 FileSet fs=(FileSet)e.nextElement(); 460 DirectoryScanner ds=fs.getDirectoryScanner( project ); 461 String localList[]= ds.getIncludedFiles(); 462 if (localList.length == 0) 463 throw new BuildException("No source files "); 464 for( int i=0; i<localList.length; i++ ) { 465 srcList.addElement( new Source( fs.getDir(project), localList[i])); 466 } 467 } 468 } 469 470 473 public boolean checkDepend(long oldestO, File oldestOFile) { 474 if( depends==null ) 475 return false; 476 String dependsA[]=depends.list(); 477 for( int i=0; i< dependsA.length; i++ ) { 478 File f=new File ( dependsA[i] ); 479 if( ! f.exists() ) { 480 log("Depend not found " + f ); 481 return true; 482 } 483 if( f.lastModified() > oldestO ) { 484 log( "Depend " + f + " newer than " + oldestOFile ); 485 return true; 486 } 487 } 488 return false; 489 } 490 491 493 protected ExecuteStreamHandler streamhandler = null; 494 protected ByteArrayOutputStream outputstream = null; 495 protected ByteArrayOutputStream errorstream = null; 496 497 public int execute( Commandline cmd ) throws BuildException 498 { 499 createStreamHandler(); 500 Execute exe = new Execute(streamhandler, null); 501 exe.setAntRun(project); 502 503 exe.setWorkingDirectory(buildDir); 504 505 exe.setCommandline(cmd.getCommandline()); 506 int result=0; 507 try { 508 result=exe.execute(); 509 } catch (IOException e) { 510 throw new BuildException(e, location); 511 } 512 return result; 513 } 514 515 public void createStreamHandler() throws BuildException { 516 outputstream= new ByteArrayOutputStream (); 518 errorstream = new ByteArrayOutputStream (); 519 520 streamhandler = 521 new PumpStreamHandler(new PrintStream (outputstream), 522 new PrintStream (errorstream)); 523 } 527 528 public void closeStreamHandler() { 529 try { 530 if (outputstream != null) 531 outputstream.close(); 532 if (errorstream != null) 533 errorstream.close(); 534 outputstream=null; 535 errorstream=null; 536 } catch (IOException e) {} 537 } 538 } 539 540 | Popular Tags |