001: /*
002: * Copyright 1999-2004 The Apache Software Foundation
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package org.apache.jk.ant.compilers;
018:
019: import java.io.File;
020: import java.io.FileWriter;
021: import java.io.IOException;
022: import java.io.PrintWriter;
023: import java.util.Enumeration;
024:
025: import org.apache.jk.ant.Def;
026: import org.apache.jk.ant.SoTask;
027: import org.apache.jk.ant.Source;
028: import org.apache.tools.ant.BuildException;
029: import org.apache.tools.ant.types.Commandline;
030: import org.apache.tools.ant.util.GlobPatternMapper;
031:
032: /**
033: * Compile using Microsoft Visual C++ v6.0
034: *
035: * @author Costin Manolache
036: * @author Ignacio J. Ortega
037: * @author Mike Anderson
038: * @author Larry Isaacs
039: */
040: public class MsvcCompiler extends CompilerAdapter {
041: GlobPatternMapper co_mapperS = new GlobPatternMapper();
042:
043: public MsvcCompiler() {
044: super ();
045: co_mapperS.setFrom("*.c");
046: co_mapperS.setTo("*.obj");
047: }
048:
049: public String[] getTargetFiles(Source src) {
050: File srcFile = src.getFile();
051: String name = srcFile.getName();
052:
053: return co_mapperS.mapFileName(name);
054: }
055:
056: public void setSoTask(SoTask so) {
057: this .so = so;
058: so.setExtension(".dll");
059: so.duplicateTo(this );
060: project.setProperty("win32", "true");
061: if (optG)
062: project.setProperty("win32.debug", "true");
063: else
064: project.setProperty("win32.release", "true");
065: }
066:
067: /** Compile using msvc
068: */
069: public void compileSingleFile(Source sourceObj)
070: throws BuildException {
071: File f = sourceObj.getFile();
072: String source = f.toString();
073: String[] includeList = (includes == null) ? new String[] {}
074: : includes.getIncludePatterns(project);
075:
076: Commandline cmd = new Commandline();
077:
078: String cc = project.getProperty("build.compiler.cc");
079: if (cc == null)
080: cc = "cl";
081:
082: cmd.setExecutable(cc);
083: addCCArgs(cmd, source, includeList);
084:
085: int result = execute(cmd);
086: if (result != 0) {
087: log("Compile failed " + result + " " + source);
088: log("Output:");
089: if (outputstream != null)
090: log(outputstream.toString());
091: log("StdErr:");
092: if (errorstream != null)
093: log(errorstream.toString());
094:
095: throw new BuildException("Compile failed " + source);
096: }
097: File ccOpt = new File(buildDir, "cc.opt");
098: ccOpt.delete();
099: closeStreamHandler();
100:
101: }
102:
103: /** common compiler args
104: */
105: private void addCCArgs(Commandline cmd, String source,
106: String includeList[]) {
107: String extra_cflags = project
108: .getProperty("build.native.extra_cflags");
109: String localCflags = cflags;
110: File ccOpt = new File(buildDir, "cc.opt");
111: if (localCflags == null) {
112: localCflags = new String("-nologo -W3 -GX -O2 -c");
113: if (extra_cflags != null) {
114: localCflags += " " + extra_cflags;
115: }
116: }
117:
118: if (optG)
119: localCflags += " -MTd -Zi";
120: else
121: localCflags += " -MT";
122:
123: // create a cc.opt file
124: PrintWriter ccpw = null;
125: try {
126: ccpw = new PrintWriter(new FileWriter(ccOpt));
127: // write the compilation flags out
128: ccpw.println(localCflags);
129: for (int i = 0; i < includeList.length; i++) {
130: ccpw.print("-I");
131: if (!includeList[i].startsWith("\"")) {
132: ccpw.print("\"");
133: }
134: ccpw.print(includeList[i]);
135: if (!includeList[i].endsWith("\"")) {
136: ccpw.print("\"");
137: }
138: ccpw.println();
139: }
140:
141: if (defines.size() > 0) {
142: Enumeration defs = defines.elements();
143: while (defs.hasMoreElements()) {
144: Def d = (Def) defs.nextElement();
145: String name = d.getName();
146: String val = d.getValue();
147: if (name == null)
148: continue;
149: String arg = "-D" + name;
150: if (val != null)
151: arg += "=" + val;
152: ccpw.println(arg);
153: }
154: }
155: } catch (IOException ioe) {
156: System.out.println("Caught IOException");
157: } finally {
158: if (ccpw != null) {
159: ccpw.close();
160: }
161: }
162:
163: project.log("Compiling " + source);
164: cmd.createArgument().setValue(source);
165: cmd.createArgument().setValue("@cc.opt");
166: }
167: }
|