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:
031: /**
032: * Compile using MetroWerks.
033: *
034: * It extends SoTask so we can debug it or use it independently of <so>.
035: * For normal use you should use the generic task, and system-specific
036: * properties to choose the right compiler plugin ( like we select
037: * jikes ).
038: *
039: * @author Mike Anderson
040: */
041: public class MwccCompiler extends CcCompiler {
042:
043: public MwccCompiler() {
044: super ();
045: };
046:
047: public void setSoTask(SoTask so) {
048: this .so = so;
049: so.setExtension(".nlm");
050: so.duplicateTo(this );
051: project.setProperty("netware", "true");
052: }
053:
054: /** Compile using mwccnlm.
055: */
056: public void compileSingleFile(Source sourceObj)
057: throws BuildException {
058: File f = sourceObj.getFile();
059: String source = f.toString();
060: String[] includeList = (includes == null) ? new String[] {}
061: : includes.getIncludePatterns(project);
062:
063: Commandline cmd = new Commandline();
064:
065: String cc = project.getProperty("build.compiler.cc");
066: if (cc == null)
067: cc = "mwccnlm";
068:
069: cmd.setExecutable(cc);
070: addCCArgs(cmd, source, includeList);
071:
072: int result = execute(cmd);
073: if (result != 0) {
074: log("Compile failed " + result + " " + source);
075: log("Output:");
076: if (outputstream != null)
077: log(outputstream.toString());
078: log("StdErr:");
079: if (errorstream != null)
080: log(errorstream.toString());
081:
082: throw new BuildException("Compile failed " + source);
083: }
084: if (null == project.getProperty("save.optionFiles")) {
085: File ccOpt = new File(buildDir, "cc.opt");
086: ccOpt.delete();
087: }
088: closeStreamHandler();
089:
090: }
091:
092: /** common compiler args
093: */
094: private void addCCArgs(Commandline cmd, String source,
095: String includeList[]) {
096: String extra_cflags = project
097: .getProperty("build.native.extra_cflags");
098: String localCflags = cflags;
099: File ccOpt = new File(buildDir, "cc.opt");
100: boolean useLibC = false;
101:
102: // create a cc.opt file
103: PrintWriter ccpw = null;
104: try {
105: ccpw = new PrintWriter(new FileWriter(ccOpt));
106:
107: for (int i = 0; i < includeList.length; i++) {
108: ccpw.print("-I");
109: ccpw.println(includeList[i]);
110: }
111:
112: if (defines.size() > 0) {
113: Enumeration defs = defines.elements();
114: while (defs.hasMoreElements()) {
115: Def d = (Def) defs.nextElement();
116: String name = d.getName();
117: String val = d.getValue();
118: if (name == null)
119: continue;
120:
121: String arg = "-D" + name;
122: if (val != null)
123: arg += "=" + val;
124: ccpw.println(arg);
125:
126: // check to see if we are building using LibC
127: if (name.equals("__NOVELL_LIBC__"))
128: useLibC = true;
129: }
130: }
131:
132: // finalize the cflags
133: if (localCflags == null) {
134: localCflags = new String(
135: "-nosyspath -c -w nocmdline -bool on");
136: if (useLibC)
137: localCflags += " -align 4";
138: else
139: localCflags += " -align 1";
140:
141: if (extra_cflags != null)
142: localCflags += " " + extra_cflags;
143: }
144:
145: if (optG)
146: localCflags += " -g";
147:
148: // write the compilation flags out
149: ccpw.println(localCflags);
150: } catch (IOException ioe) {
151: System.out.println("Caught IOException");
152: } finally {
153: if (ccpw != null) {
154: ccpw.close();
155: }
156: }
157:
158: project.log("Compiling " + source);
159: cmd.createArgument().setValue(source);
160: cmd.createArgument().setValue("@cc.opt");
161: }
162: }
|