001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.modules.cnd.makeproject.api.compilers;
043:
044: import java.io.File;
045: import java.io.IOException;
046: import java.io.InputStream;
047: import java.util.ArrayList;
048: import java.util.Iterator;
049: import java.util.List;
050: import org.netbeans.modules.cnd.api.compilers.CompilerSet.CompilerFlavor;
051: import org.netbeans.modules.cnd.api.utils.Path;
052:
053: public class CCCCompiler extends BasicCompiler {
054: private static File tmpFile = null;
055:
056: public CCCCompiler(CompilerFlavor flavor, int kind, String name,
057: String displayName, String path) {
058: super (flavor, kind, name, displayName, path);
059: }
060:
061: // To be overridden
062: public String getMTLevelOptions(int value) {
063: return ""; // NOI18N
064: }
065:
066: // To be overridden
067: public String getLibraryLevelOptions(int value) {
068: return ""; // NOI18N
069: }
070:
071: // To be overridden
072: public String getStandardsEvolutionOptions(int value) {
073: return ""; // NOI18N
074: }
075:
076: // To be overridden
077: public String getLanguageExtOptions(int value) {
078: return ""; // NOI18N
079: }
080:
081: protected void getSystemIncludesAndDefines(String path,
082: String command, boolean stdout) throws IOException {
083: Process process;
084: if (path == null) {
085: path = ""; // NOI18N
086: }
087: ArrayList<String> envp = new ArrayList<String>();
088: for (String key : System.getenv().keySet()) {
089: String value = System.getenv().get(key);
090: if (key.equals(Path.getPathName())) {
091: envp.add(Path.getPathName() + "=" + path
092: + File.pathSeparatorChar + value); // NOI18N
093: } else {
094: String entry = key + "=" + (value != null ? value : ""); // NOI18N
095: envp.add(entry);
096: }
097: }
098: //String[] envp = { Path.getPathName() + '=' + path + File.pathSeparatorChar + CppSettings.getDefault().getPath() }; // NOI18N
099: process = Runtime.getRuntime().exec(command + " " + tmpFile(),
100: (String[]) envp.toArray(new String[envp.size()])); // NOI18N
101: if (stdout)
102: parseCompilerOutput(process.getInputStream());
103: else
104: parseCompilerOutput(process.getErrorStream());
105: }
106:
107: // To be overridden
108: public void saveSystemIncludesAndDefines() {
109: }
110:
111: // To be overridden
112: public void resetSystemIncludesAndDefines() {
113: }
114:
115: // To be overridden
116: protected void parseCompilerOutput(InputStream is) {
117: }
118:
119: /**
120: * Determines whether the given macro presents in the list
121: * @param macrosList list of macros strings (in the form "macro=value" or just "macro")
122: * @param macroToFind the name of the macro to search for
123: * @return true if macro with the given name is found, otherwise false
124: */
125: protected boolean containsMacro(List macrosList, String macroToFind) {
126: int len = macroToFind.length();
127: for (Iterator it = macrosList.iterator(); it.hasNext();) {
128: String macro = (String) it.next();
129: if (macro.startsWith(macroToFind)) {
130: if (macro.length() == len) {
131: return true; // they are just equal
132: }
133: if (macro.charAt(len) == '=') {
134: return true; // it presents in the form macro=value
135: }
136: }
137: }
138: return false;
139: }
140:
141: protected void parseUserMacros(final String line,
142: final PersistentList preprocessorList) {
143: int defineIndex = line.indexOf("-D"); // NOI18N
144: while (defineIndex > 0) {
145: String token;
146: int spaceIndex = line.indexOf(" ", defineIndex + 1); // NOI18N
147: if (spaceIndex > 0) {
148: token = line.substring(defineIndex + 2, spaceIndex);
149: preprocessorList.add(token);
150: defineIndex = line.indexOf("-D", spaceIndex); // NOI18N
151: } else {
152: token = line.substring(defineIndex + 2);
153: preprocessorList.add(token);
154: break;
155: }
156: }
157: }
158:
159: private String tmpFile() {
160: if (tmpFile == null) {
161: try {
162: tmpFile = File.createTempFile("xyz", ".c"); // NOI18N
163: } catch (IOException ioe) {
164: }
165: tmpFile.deleteOnExit();
166: }
167: if (tmpFile != null)
168: return tmpFile.getAbsolutePath();
169: else
170: return "/dev/null"; // NOI18N
171: }
172:
173: protected String getUniqueID() {
174: return getClass().getName() + getPath().hashCode() + "."; // NOI18N
175: }
176: }
|