KickJava   Java API By Example, From Geeks To Geeks.

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


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.util.Enumeration JavaDoc;
21 import java.util.Vector JavaDoc;
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 /* Modeled after javac
30  */

31
32 /**
33  * s/javac/C compiler/
34  *
35  * The interface that all compiler adapters must adher to.
36  *
37  * <p>A compiler adapter is an adapter that interprets the javac's
38  * parameters in preperation to be passed off to the compier this
39  * adapter represents. As all the necessary values are stored in the
40  * Javac task itself, the only thing all adapters need is the javac
41  * task, the execute command and a parameterless constructor (for
42  * reflection).</p>
43  *
44  * @author Jay Dickon Glanville <a HREF="mailto:jayglanville@home.com">jayglanville@home.com</a>
45  * @author Costin Manolache
46  */

47 public abstract class CompilerAdapter extends SoTask {
48     SoTask so;
49     Vector JavaDoc 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 // /** @deprecated
61
// */
62
// public GlobPatternMapper getOMapper() {
63
// return null;
64
// }
65

66     /** Return the files that depend on a src file.
67      * The first item should be the .o file used for linking.
68      */

69     public abstract String JavaDoc[] getTargetFiles( Source src );
70
71     public void execute() throws BuildException {
72         super.findSourceFiles();
73         Vector JavaDoc compileList=findCompileList(srcList);
74     compile( compileList );
75     }
76
77     /** Verify if a .c file needs compilation.
78      * As with javac, we assume a fixed build structure, where all .o
79      * files are in a separate directory from the sources ( no mess ).
80      *
81      * XXX Hack makedepend somehow into this.
82      */

83     public boolean needCompile( Source source ) {
84     // For each .c file we'll have a .o file in the build dir,
85
// with the same name.
86
File JavaDoc srcF = source.getFile();
87     if( !srcF.exists() ) {
88             if( debug > 0 )
89                 log("No source file " + srcF );
90             return false;
91         }
92
93     String JavaDoc targetNames[]= getTargetFiles( source );
94     if( targetNames==null || targetNames.length==0 ) {
95             if( debug > 0 )
96                 log("No target files " + srcF );
97         return true; // strange, probably different extension ?
98
}
99         String JavaDoc targetName=targetNames[0];
100
101         String JavaDoc targetDir=source.getPackage();
102         File JavaDoc f1=new File JavaDoc( buildDir, targetDir );
103         File JavaDoc target=new File JavaDoc( f1, targetName );
104     // System.out.println("XXX " + target );
105
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     /** Remove all generated files, cleanup
123      */

124     public void removeOFiles( Vector JavaDoc srcList ) {
125         for (int i = 0; i < srcList.size(); i++) {
126             // log( "Checking " + (Source)srcList.elementAt(i));
127
Source source=(Source)srcList.elementAt(i);
128         String JavaDoc targetNA[]=getTargetFiles(source);
129         if( targetNA==null )
130         continue;
131             String JavaDoc targetDir=source.getPackage();
132             File JavaDoc f1=new File JavaDoc( buildDir, targetDir );
133             for( int j=0; j<targetNA.length; j++ ) {
134                 File JavaDoc target=new File JavaDoc( f1, targetNA[j] );
135                 // Check the dependency
136
if( target.exists() ) {
137                     // Remove it - we'll do a full build
138
target.delete();
139                     log("Removing " + target );
140                 }
141             }
142         }
143     }
144
145     // XXX modified as side-effect of checking files with needCompile()
146
long oldestO=System.currentTimeMillis();
147     File JavaDoc oldestOFile=null;
148
149     /** Find the subset of the source list that needs compilation.
150      */

151     protected Vector JavaDoc findCompileList(Vector JavaDoc srcList) throws BuildException {
152         Vector JavaDoc compileList=new Vector JavaDoc();
153
154         for (int i = 0; i < srcList.size(); i++) {
155         Source source=(Source)srcList.elementAt(i);
156         File JavaDoc srcFile=source.getFile();
157        
158             if (!srcFile.exists()) {
159                 throw new BuildException("Source \"" + srcFile.getPath() +
160                                          "\" does not exist!", location);
161             }
162
163         // Check the dependency
164
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 JavaDoc();
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     /** Return the files that were actually compiled
185      */

186     public Vector JavaDoc getCompiledFiles() {
187         return compileList;
188     }
189
190     /** Compile the source files. The compiler adapter can override either this method
191      * ( if it can compile multiple files at once ) or compileSingleFile().
192      * Note that the list includes _all_ sources, findCompileList() can be used to
193      * avoid compiling files that are up-to-date.
194      */

195     public void compile(Vector JavaDoc sourceFiles ) throws BuildException {
196         compileList=findCompileList(sourceFiles);
197
198         log("Compiling " + compileList.size() + " out of " + sourceFiles.size());
199     Enumeration JavaDoc en=compileList.elements();
200     while( en.hasMoreElements() ) {
201         Source source=(Source)en.nextElement();
202         compileSingleFile(source);
203     }
204     }
205     
206     /** Compile single file
207      */

208     public void compileSingleFile(Source sourceObj) throws BuildException {
209     }
210
211
212     protected void displayError( int result, String JavaDoc source, Commandline cmd )
213     throws BuildException
214     {
215         if( result == 0 ) {
216             String JavaDoc 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 JavaDoc [] includeList = ( includes==null ) ?
239         new String JavaDoc[] {} : includes.getIncludePatterns(project);
240     for( int i=0; i<includeList.length; i++ ) {
241         cmd.createArgument().setValue("-I" + includeList[i] );
242     }
243     }
244
245     /** Common cc parameters
246      */

247     protected void addExtraFlags(Commandline cmd ) {
248     String JavaDoc extra_cflags=project.getProperty("build.native.extra_cflags");
249     String JavaDoc 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         // Define by default the OS ( as known to java )
263
String JavaDoc os=System.getProperty("java.os");
264
265         if( defines.size() > 0 ) {
266         Enumeration JavaDoc defs=defines.elements();
267         while( defs.hasMoreElements() ) {
268         Def d=(Def)defs.nextElement();
269         String JavaDoc name=d.getName();
270         String JavaDoc val=d.getValue();
271         if( name==null ) continue;
272         String JavaDoc 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