KickJava   Java API By Example, From Geeks To Geeks.

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


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
21 import org.apache.jk.ant.Source;
22 import org.apache.tools.ant.BuildException;
23 import org.apache.tools.ant.types.Commandline;
24
25 /**
26  * Compile using Gcj. This is ( even more ) experimental.
27  *
28  * @author Costin Manolache
29  */

30 public class GcjCompiler extends CcCompiler {
31     
32     public GcjCompiler() {
33     super();
34     co_mapper.setFrom("*.java");
35     co_mapper.setTo("*.o");
36     }
37
38     public String JavaDoc[] getTargetFiles( Source src ) {
39         File JavaDoc srcFile = src.getFile();
40         String JavaDoc name=srcFile.getName();
41         if( name.endsWith( ".java" ) ) {
42             return co_mapper.mapFileName( name );
43         } else {
44             return new String JavaDoc[]
45             { name + ".o" };
46         }
47     }
48
49     
50     /** Compile using libtool.
51      */

52     public void compileSingleFile(Source sourceObj) throws BuildException {
53     File JavaDoc f=sourceObj.getFile();
54     String JavaDoc source=f.toString();
55     Commandline cmd = new Commandline();
56
57     cmd.setExecutable( "gcj" );
58
59     cmd.createArgument().setValue("-c" );
60     
61     if( optG ) {
62             // cmd.createArgument().setValue("-g" );
63
cmd.createArgument().setValue("-ggdb3" );
64             // cmd.createArgument().setValue("-save-temps" );
65
// cmd.createArgument().setValue("-Wall");
66
}
67     addOptimize( cmd );
68     addExtraFlags( cmd );
69     cmd.createArgument().setValue("-fPIC" );
70     addIncludes( cmd );
71         String JavaDoc targetDir=sourceObj.getPackage();
72     try {
73         File JavaDoc f1=new File JavaDoc( buildDir, targetDir );
74         f1.mkdirs();
75             cmd.createArgument().setValue( "-o" );
76             String JavaDoc targetO[]=getTargetFiles( sourceObj );
77             if( targetO==null ) {
78                 log("no target for " + sourceObj.getFile() );
79                 return;
80             }
81             File JavaDoc ff=new File JavaDoc( f1, targetO[0]);
82             cmd.createArgument().setValue( ff.toString() );
83     } catch( Exception JavaDoc ex ) {
84         ex.printStackTrace();
85     }
86     
87         if( ! source.endsWith(".java") ) {
88             cmd.createArgument().setValue("-R" );
89             cmd.createArgument().setValue( targetDir + "/" + f.getName() );
90             //System.out.println("XXX resource " + targetDir + "/" + f.getName() );
91
} else {
92             // cmd.createArgument().setValue("-fno-bounds-check" );
93
}
94         cmd.createArgument().setValue( source );
95     project.log( "Compiling " + source);
96
97     if( debug > 0 )
98         project.log( "Command: " + cmd );
99     int result=execute( cmd );
100     if( result!=0 ) {
101         displayError( result, source, cmd );
102     }
103     closeStreamHandler();
104     }
105
106     protected void addIncludes(Commandline cmd) {
107     String JavaDoc [] includeList = ( includes==null ) ?
108         new String JavaDoc[] {} : includes.getIncludePatterns(project);
109     for( int i=0; i<includeList.length; i++ ) {
110         cmd.createArgument().setValue("-I" + includeList[i] );
111     }
112     }
113
114
115 }
116
117
Popular Tags