KickJava   Java API By Example, From Geeks To Geeks.

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


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 using libtool.
32  *
33  * @author Costin Manolache
34  */

35 public class LibtoolLinker extends LinkerAdapter {
36     SoTask so;
37     GlobPatternMapper lo_mapper=new GlobPatternMapper();
38     public LibtoolLinker() {
39     so=this;
40     lo_mapper.setFrom("*.c");
41     lo_mapper.setTo("*.lo");
42     }
43
44     /** Link using libtool.
45      */

46     public boolean link(Vector JavaDoc srcList) throws BuildException {
47     so.duplicateTo( this );
48     Commandline cmd = new Commandline();
49
50     String JavaDoc libtool=project.getProperty("build.native.libtool");
51     if(libtool==null) libtool="libtool";
52
53     cmd.setExecutable( libtool );
54     
55     cmd.createArgument().setValue("--mode=link");
56
57     String JavaDoc cc=project.getProperty("build.native.cc");
58     if(cc==null) cc="cc";
59
60     cmd.createArgument().setValue( cc );
61     
62     cmd.createArgument().setValue("-module");
63     cmd.createArgument().setValue("-avoid-version");
64     cmd.createArgument().setValue("-rpath");
65     cmd.createArgument().setValue( buildDir.getAbsolutePath());
66
67     cmd.createArgument().setValue( "-o" );
68     cmd.createArgument().setValue( soFile + ".la" );
69
70     if( profile )
71         cmd.createArgument().setValue("-pg" );
72
73         // write out any additional link options
74
Enumeration JavaDoc opts = linkOpts.elements();
75         while( opts.hasMoreElements() ) {
76             JkData opt = (JkData) opts.nextElement();
77             String JavaDoc option = opt.getValue();
78             if( option == null ) continue;
79
80             cmd.createArgument().setValue( option );
81         }
82         
83     // All .o files must be included
84
project.log( "Linking " + buildDir + "/" + soFile + ".so");
85
86         if( libs!=null ) {
87             String JavaDoc libsA[]=libs.list();
88             for( int i=0; i< libsA.length; i++ ) {
89                 cmd.createArgument().setValue( "-l" + libsA[i] );
90                 //XXX debug
91
project.log("XXX -l" + libsA[i] );
92             }
93         }
94     
95     for( int i=0; i<srcList.size(); i++ ) {
96         Source sourceObj=(Source)srcList.elementAt(i);
97         
98         File JavaDoc ff=new File JavaDoc( buildDir, sourceObj.getTargetFile(lo_mapper));
99         cmd.createArgument().setValue( ff.toString() );
100     }
101     
102     int result=execute( cmd );
103     if( result!=0 ) {
104         log("Link failed " + result );
105         log("Command:" + cmd.toString());
106         log("Output:" );
107         if( outputstream!=null )
108         log( outputstream.toString());
109         log("StdErr:" );
110         if( errorstream!=null )
111         log( errorstream.toString());
112         
113         throw new BuildException("Link failed " + soFile);
114     }
115     closeStreamHandler();
116
117     executeLibtoolInstall();
118     return true;
119     }
120
121     /** Final step using libtool.
122      */

123     private void executeLibtoolInstall() throws BuildException {
124     Commandline cmd = new Commandline();
125
126     String JavaDoc libtool=project.getProperty("build.native.libtool");
127     if(libtool==null) libtool="libtool";
128
129     cmd.setExecutable( libtool );
130     
131     cmd.createArgument().setValue("--mode=install");
132
133     cmd.createArgument().setValue( "cp" );
134
135     File JavaDoc laFile=new File JavaDoc( buildDir, soFile + ".la" );
136     cmd.createArgument().setValue( laFile.getAbsolutePath());
137     
138     File JavaDoc soFileF=new File JavaDoc( buildDir, soFile + ".so" );
139     cmd.createArgument().setValue( soFileF.getAbsolutePath());
140
141     int result=execute( cmd );
142     if( result!=0 ) {
143         log("Link/install failed " + result );
144         log("Command:" + cmd.toString());
145         log("Output:" );
146         if( outputstream!=null )
147         log( outputstream.toString());
148         log("StdErr:" );
149         if( errorstream!=null )
150         log( errorstream.toString());
151         
152         throw new BuildException("Link failed " + soFile);
153     }
154     closeStreamHandler();
155     }
156 }
157
158
Popular Tags