KickJava   Java API By Example, From Geeks To Geeks.

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


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 import org.apache.tools.ant.util.GlobPatternMapper;
31
32 /**
33  * Compile using Microsoft Visual C++ v6.0
34  *
35  * @author Costin Manolache
36  * @author Ignacio J. Ortega
37  * @author Mike Anderson
38  * @author Larry Isaacs
39  */

40 public class MsvcCompiler extends CompilerAdapter {
41     GlobPatternMapper co_mapperS=new GlobPatternMapper();
42     
43     public MsvcCompiler() {
44         super();
45     co_mapperS.setFrom("*.c");
46     co_mapperS.setTo("*.obj");
47     }
48
49     public String JavaDoc[] getTargetFiles( Source src ) {
50         File JavaDoc srcFile = src.getFile();
51         String JavaDoc name=srcFile.getName();
52         
53         return co_mapperS.mapFileName( name );
54     }
55
56     public void setSoTask(SoTask so ) {
57         this.so=so;
58         so.setExtension(".dll");
59         so.duplicateTo( this );
60         project.setProperty("win32", "true");
61         if (optG)
62             project.setProperty("win32.debug", "true");
63         else
64             project.setProperty("win32.release", "true");
65     }
66
67     /** Compile using msvc
68      */

69     public void compileSingleFile(Source sourceObj) throws BuildException {
70     File JavaDoc f=sourceObj.getFile();
71     String JavaDoc source=f.toString();
72         String JavaDoc [] includeList = ( includes==null ) ?
73             new String JavaDoc[] {} : includes.getIncludePatterns(project);
74
75         Commandline cmd = new Commandline();
76
77         String JavaDoc cc=project.getProperty("build.compiler.cc");
78         if(cc==null) cc="cl";
79         
80         cmd.setExecutable( cc );
81         addCCArgs( cmd, source, includeList );
82
83         int result=execute( cmd );
84         if( result!=0 ) {
85             log("Compile failed " + result + " " + source );
86             log("Output:" );
87             if( outputstream!=null )
88                 log( outputstream.toString());
89             log("StdErr:" );
90             if( errorstream!=null )
91                 log( errorstream.toString());
92             
93             throw new BuildException("Compile failed " + source);
94         }
95         File JavaDoc ccOpt = new File JavaDoc(buildDir, "cc.opt");
96         ccOpt.delete();
97         closeStreamHandler();
98
99     }
100
101     /** common compiler args
102      */

103     private void addCCArgs(Commandline cmd, String JavaDoc source, String JavaDoc includeList[]) {
104         String JavaDoc extra_cflags=project.getProperty("build.native.extra_cflags");
105         String JavaDoc localCflags=cflags;
106         File JavaDoc ccOpt = new File JavaDoc(buildDir, "cc.opt");
107         if( localCflags==null ) {
108             localCflags=new String JavaDoc("-nologo -W3 -GX -O2 -c");
109             if( extra_cflags!=null ) {
110                 localCflags+=" " + extra_cflags;
111             }
112         }
113
114         if (optG)
115             localCflags += " -MTd -Zi";
116         else
117             localCflags += " -MT";
118
119         // create a cc.opt file
120
PrintWriter JavaDoc ccpw = null;
121         try
122         {
123             ccpw = new PrintWriter JavaDoc(new FileWriter JavaDoc(ccOpt));
124             // write the compilation flags out
125
ccpw.println(localCflags);
126             for( int i=0; i<includeList.length; i++ ) {
127                 ccpw.print("-I");
128                 if (!includeList[i].startsWith("\"")) {
129                     ccpw.print("\"");
130                 }
131                 ccpw.print(includeList[i] );
132                 if (!includeList[i].endsWith("\"")) {
133                     ccpw.print("\"");
134                 }
135                 ccpw.println();
136             }
137
138             if( defines.size() > 0 ) {
139                 Enumeration JavaDoc defs=defines.elements();
140                 while( defs.hasMoreElements() ) {
141                     Def d=(Def)defs.nextElement();
142                     String JavaDoc name=d.getName();
143                     String JavaDoc val=d.getValue();
144                     if( name==null ) continue;
145                     String JavaDoc arg="-D" + name;
146                     if( val!=null )
147                         arg+= "=" + val;
148                     ccpw.println(arg);
149                 }
150             }
151         }
152         catch (IOException JavaDoc ioe)
153         {
154             log("Caught IOException");
155         }
156         finally
157         {
158             if (ccpw != null)
159             {
160                 ccpw.close();
161             }
162         }
163
164         project.log( "Compiling " + source);
165         cmd.createArgument().setValue( source );
166         cmd.createArgument().setValue( "@cc.opt" );
167     }
168 }
169
170
Popular Tags