KickJava   Java API By Example, From Geeks To Geeks.

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


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.io.FileWriter JavaDoc;
21 import java.io.IOException JavaDoc;
22 import java.io.PrintWriter JavaDoc;
23 import java.util.Enumeration JavaDoc;
24
25 import org.apache.jk.ant.Def;
26 import org.apache.jk.ant.SoTask;
27 import org.apache.jk.ant.Source;
28 import org.apache.tools.ant.BuildException;
29 import org.apache.tools.ant.types.Commandline;
30
31 /**
32  * Compile using MetroWerks.
33  *
34  * It extends SoTask so we can debug it or use it independently of <so>.
35  * For normal use you should use the generic task, and system-specific
36  * properties to choose the right compiler plugin ( like we select
37  * jikes ).
38  *
39  * @author Mike Anderson
40  */

41 public class MwccCompiler extends CcCompiler {
42     
43     public MwccCompiler() {
44         super();
45     };
46
47     public void setSoTask(SoTask so ) {
48         this.so=so;
49         so.setExtension(".nlm");
50         so.duplicateTo( this );
51         project.setProperty("netware", "true");
52     }
53
54     /** Compile using mwccnlm.
55      */

56     public void compileSingleFile(Source sourceObj) throws BuildException {
57     File JavaDoc f=sourceObj.getFile();
58     String JavaDoc source=f.toString();
59         String JavaDoc [] includeList = ( includes==null ) ?
60             new String JavaDoc[] {} : includes.getIncludePatterns(project);
61
62         Commandline cmd = new Commandline();
63
64         String JavaDoc cc=project.getProperty("build.compiler.cc");
65         if(cc==null) cc="mwccnlm";
66         
67         cmd.setExecutable( cc );
68         addCCArgs( cmd, source, includeList );
69
70         int result=execute( cmd );
71         if( result!=0 ) {
72             log("Compile failed " + result + " " + source );
73             log("Output:" );
74             if( outputstream!=null )
75                 log( outputstream.toString());
76             log("StdErr:" );
77             if( errorstream!=null )
78                 log( errorstream.toString());
79             
80             throw new BuildException("Compile failed " + source);
81         }
82         if (null == project.getProperty("save.optionFiles"))
83         {
84             File JavaDoc ccOpt = new File JavaDoc(buildDir, "cc.opt");
85             ccOpt.delete();
86         }
87         closeStreamHandler();
88
89     }
90
91     /** common compiler args
92      */

93     private void addCCArgs(Commandline cmd, String JavaDoc source, String JavaDoc includeList[]) {
94         String JavaDoc extra_cflags=project.getProperty("build.native.extra_cflags");
95         String JavaDoc localCflags=cflags;
96         File JavaDoc ccOpt = new File JavaDoc(buildDir, "cc.opt");
97         boolean useLibC = false;
98
99         // create a cc.opt file
100
PrintWriter JavaDoc ccpw = null;
101         try
102         {
103             ccpw = new PrintWriter JavaDoc(new FileWriter JavaDoc(ccOpt));
104
105             for( int i=0; i<includeList.length; i++ ) {
106                 ccpw.print("-I");
107                 ccpw.println(includeList[i] );
108             }
109
110             if( defines.size() > 0 ) {
111                 Enumeration JavaDoc defs=defines.elements();
112                 while( defs.hasMoreElements() ) {
113                     Def d=(Def)defs.nextElement();
114                     String JavaDoc name=d.getName();
115                     String JavaDoc val=d.getValue();
116                     if( name==null ) continue;
117                     
118                     String JavaDoc arg="-D" + name;
119                     if( val!=null )
120                         arg+= "=" + val;
121                     ccpw.println(arg);
122
123                     // check to see if we are building using LibC
124
if (name.equals("__NOVELL_LIBC__"))
125                         useLibC = true;
126                 }
127             }
128
129             // finalize the cflags
130
if( localCflags==null ) {
131                 localCflags=new String JavaDoc("-nosyspath -c -w nocmdline -bool on");
132                 if (useLibC)
133                     localCflags += " -align 4";
134                 else
135                     localCflags += " -align 1";
136
137                 if( extra_cflags!=null )
138                     localCflags+=" " + extra_cflags;
139             }
140
141             if (optG)
142                 localCflags += " -g";
143
144             // write the compilation flags out
145
ccpw.println(localCflags);
146         }
147         catch (IOException JavaDoc ioe)
148         {
149             log("Caught IOException");
150         }
151         finally
152         {
153             if (ccpw != null)
154             {
155                 ccpw.close();
156             }
157         }
158
159         project.log( "Compiling " + source);
160         cmd.createArgument().setValue( source );
161         cmd.createArgument().setValue( "@cc.opt" );
162     }
163 }
164
165
Popular Tags