KickJava   Java API By Example, From Geeks To Geeks.

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


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.JkData;
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 import org.apache.tools.ant.util.GlobPatternMapper;
29
30 /**
31  * Link java using gcj.
32  *
33  * @author Costin Manolache
34  */

35 public class GcjLinker extends LinkerAdapter {
36     SoTask so;
37     protected static GlobPatternMapper co_mapper=new GlobPatternMapper();
38     static {
39     co_mapper.setFrom("*.java");
40     co_mapper.setTo("*.o");
41     }
42     public GcjLinker() {
43     so=this;
44     }
45
46     public String JavaDoc[] getTargetFiles( Source src ) {
47         File JavaDoc srcFile = src.getFile();
48         String JavaDoc name=srcFile.getName();
49         if( name.endsWith( ".java" ) )
50             return co_mapper.mapFileName( name );
51         else
52             return new String JavaDoc[]
53             { name + ".o" };
54     }
55
56
57     
58     public void setSoTask(SoTask so ) {
59     this.so=so;
60     so.duplicateTo( this );
61     }
62
63     public void execute() throws BuildException {
64     findSourceFiles();
65     link(this.srcList);
66     }
67
68     /** Link using libtool.
69      */

70     public boolean link(Vector JavaDoc srcList) throws BuildException {
71 // link( srcList, false );
72
link( srcList, true );
73     return true;
74     }
75     public boolean link(Vector JavaDoc srcList, boolean shared) throws BuildException {
76     Commandline cmd = new Commandline();
77
78     cmd.setExecutable( "gcj" );
79
80         if( shared )
81             cmd.createArgument().setValue( "--shared" );
82     cmd.createArgument().setValue( "-o" );
83         if( shared )
84             cmd.createArgument().setValue( soFile + ".so" );
85         else
86             cmd.createArgument().setValue( soFile + ".a" );
87
88     project.log( "Linking " + buildDir + "/" + soFile + ".so");
89
90         // write out any additional link options
91
Enumeration JavaDoc opts = linkOpts.elements();
92         while( opts.hasMoreElements() ) {
93             JkData opt = (JkData) opts.nextElement();
94             String JavaDoc option = opt.getValue();
95             if( option == null ) continue;
96
97             cmd.createArgument().setValue( option );
98         }
99
100     for( int i=0; i<srcList.size(); i++ ) {
101         Source source=(Source)srcList.elementAt(i);
102         File JavaDoc f1=new File JavaDoc(buildDir, source.getPackage());
103         File JavaDoc srcF=new File JavaDoc(f1, getTargetFiles(source)[0]);
104         cmd.createArgument().setValue( srcF.toString() );
105     }
106     
107     int result=execute( cmd );
108     if( result!=0 ) {
109         log("Link failed " + result );
110         log("Command:" + cmd.toString());
111         log("Output:" );
112         if( outputstream!=null )
113         log( outputstream.toString());
114         log("StdErr:" );
115         if( errorstream!=null )
116         log( errorstream.toString());
117         
118         throw new BuildException("Link failed " + soFile);
119     }
120     closeStreamHandler();
121
122     return true;
123     }
124 }
125
126
Popular Tags