--- /dev/null
+/*\r
+ * \r
+ * Copyright 2002-2004 The Ant-Contrib project\r
+ *\r
+ * Licensed under the Apache License, Version 2.0 (the "License");\r
+ * you may not use this file except in compliance with the License.\r
+ * You may obtain a copy of the License at\r
+ *\r
+ * http://www.apache.org/licenses/LICENSE-2.0\r
+ *\r
+ * Unless required by applicable law or agreed to in writing, software\r
+ * distributed under the License is distributed on an "AS IS" BASIS,\r
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
+ * See the License for the specific language governing permissions and\r
+ * limitations under the License.\r
+ */\r
+package net.sf.antcontrib.cpptasks.gcc.cross.sparc_sun_solaris2;\r
+import java.io.BufferedReader;\r
+import java.io.File;\r
+import java.io.FileReader;\r
+import java.io.IOException;\r
+import java.util.Vector;\r
+\r
+import net.sf.antcontrib.cpptasks.CUtil;\r
+import net.sf.antcontrib.cpptasks.compiler.CaptureStreamHandler;\r
+import net.sf.antcontrib.cpptasks.types.LibraryTypeEnum;\r
+\r
+/**\r
+ * A add-in class for Gcc processors\r
+ * \r
+ * \r
+ */\r
+public class GccProcessor {\r
+ // the results from gcc -dumpmachine\r
+ private static String machine;\r
+ private static String[] specs;\r
+ // the results from gcc -dumpversion\r
+ private static String version;\r
+ private static int addLibraryPatterns(String[] libnames, StringBuffer buf,\r
+ String prefix, String extension, String[] patterns, int offset) {\r
+ for (int i = 0; i < libnames.length; i++) {\r
+ buf.setLength(0);\r
+ buf.append(prefix);\r
+ buf.append(libnames[i]);\r
+ buf.append(extension);\r
+ patterns[offset + i] = buf.toString();\r
+ }\r
+ return offset + libnames.length;\r
+ }\r
+ /**\r
+ * Converts absolute Cygwin file or directory names to the corresponding\r
+ * Win32 name.\r
+ * \r
+ * @param names\r
+ * array of names, some elements may be null, will be changed in\r
+ * place.\r
+ */\r
+ public static void convertCygwinFilenames(String[] names) {\r
+ if (names == null) {\r
+ throw new NullPointerException("names");\r
+ }\r
+ File gccDir = CUtil.getExecutableLocation(GccCCompiler.CMD_PREFIX\r
+ + "gcc.exe");\r
+ if (gccDir != null) {\r
+ String prefix = gccDir.getAbsolutePath() + "/..";\r
+ StringBuffer buf = new StringBuffer();\r
+ for (int i = 0; i < names.length; i++) {\r
+ String name = names[i];\r
+ if (name != null && name.length() > 1 && name.charAt(0) == '/') {\r
+ buf.setLength(0);\r
+ buf.append(prefix);\r
+ buf.append(name);\r
+ names[i] = buf.toString();\r
+ }\r
+ }\r
+ }\r
+ }\r
+ public static String[] getLibraryPatterns(String[] libnames, LibraryTypeEnum libType) {\r
+ StringBuffer buf = new StringBuffer();\r
+ String[] patterns = new String[libnames.length * 2];\r
+ int offset = addLibraryPatterns(libnames, buf, "lib", ".a", patterns, 0);\r
+ if (isHPUX()) {\r
+ offset = addLibraryPatterns(libnames, buf, "lib", ".sl", patterns,\r
+ offset);\r
+ } else {\r
+ offset = addLibraryPatterns(libnames, buf, "lib", ".so", patterns,\r
+ offset);\r
+ }\r
+ return patterns;\r
+ }\r
+ public static String getMachine() {\r
+ if (machine == null) {\r
+ String[] args = new String[]{GccCCompiler.CMD_PREFIX + "gcc",\r
+ "-dumpmachine"};\r
+ String[] cmdout = CaptureStreamHandler.run(args);\r
+ if (cmdout.length == 0) {\r
+ machine = "nomachine";\r
+ } else {\r
+ machine = cmdout[0];\r
+ }\r
+ }\r
+ return machine;\r
+ }\r
+ public static String[] getOutputFileSwitch(String letter, String outputFile) {\r
+ StringBuffer buf = new StringBuffer();\r
+ if (outputFile.indexOf(' ') >= 0) {\r
+ buf.append('"');\r
+ buf.append(outputFile.replace('\\', '/'));\r
+ buf.append('"');\r
+ } else {\r
+ buf.append(outputFile.replace('\\', '/'));\r
+ }\r
+ String[] retval = new String[]{letter, buf.toString()};\r
+ return retval;\r
+ }\r
+ /**\r
+ * Returns the contents of the gcc specs file.\r
+ * \r
+ * The implementation locates gcc.exe in the executable path and then\r
+ * builds a relative path name from the results of -dumpmachine and\r
+ * -dumpversion. Attempts to use gcc -dumpspecs to provide this information\r
+ * resulted in stalling on the Execute.run\r
+ * \r
+ * @returns contents of the specs file\r
+ */\r
+ public static String[] getSpecs() {\r
+ if (specs == null) {\r
+ File gccParent = CUtil\r
+ .getExecutableLocation(GccCCompiler.CMD_PREFIX + "gcc.exe");\r
+ if (gccParent != null) {\r
+ //\r
+ // build a relative path like\r
+ // ../lib/gcc-lib/i686-pc-cygwin/2.95.3-5/specs\r
+ //\r
+ StringBuffer buf = new StringBuffer("../lib/gcc-lib/");\r
+ buf.append(getMachine());\r
+ buf.append('/');\r
+ buf.append(getVersion());\r
+ buf.append("/specs");\r
+ //\r
+ // resolve it relative to the location of gcc.exe\r
+ //\r
+ String relativePath = buf.toString();\r
+ File specsFile = new File(gccParent, relativePath);\r
+ //\r
+ // found the specs file\r
+ //\r
+ try {\r
+ //\r
+ // read the lines in the file\r
+ //\r
+ BufferedReader reader = new BufferedReader(new FileReader(\r
+ specsFile));\r
+ Vector lines = new Vector(100);\r
+ String line = reader.readLine();\r
+ while (line != null) {\r
+ lines.addElement(line);\r
+ line = reader.readLine();\r
+ }\r
+ specs = new String[lines.size()];\r
+ lines.copyInto(specs);\r
+ } catch (IOException ex) {\r
+ }\r
+ }\r
+ }\r
+ if (specs == null) {\r
+ specs = new String[0];\r
+ }\r
+ return specs;\r
+ }\r
+ public static String getVersion() {\r
+ if (version == null) {\r
+ String[] args = new String[]{GccCCompiler.CMD_PREFIX + "gcc",\r
+ "-dumpversion"};\r
+ String[] cmdout = CaptureStreamHandler.run(args);\r
+ if (cmdout.length == 0) {\r
+ version = "noversion";\r
+ } else {\r
+ version = cmdout[0];\r
+ }\r
+ }\r
+ return version;\r
+ }\r
+ public static boolean isCaseSensitive() {\r
+ return true;\r
+ }\r
+ /**\r
+ * Determines if task is running with cygwin\r
+ * \r
+ * @return true if cygwin was detected\r
+ */\r
+ public static boolean isCygwin() {\r
+ return getMachine().indexOf("cygwin") > 0;\r
+ }\r
+ private static boolean isHPUX() {\r
+ String osname = System.getProperty("os.name").toLowerCase();\r
+ if (osname.indexOf("hp") >= 0 && osname.indexOf("ux") >= 0) {\r
+ return true;\r
+ }\r
+ return false;\r
+ }\r
+ /**\r
+ * \r
+ * Parses the results of the specs file for a specific processor and\r
+ * options\r
+ * \r
+ * @param specsContent\r
+ * Contents of specs file as returned from getSpecs\r
+ * @param specSectionStart\r
+ * start of spec section, for example "*cpp:"\r
+ * @param options\r
+ * command line switches such as "-istart"\r
+ */\r
+ public static String[][] parseSpecs(String[] specsContent,\r
+ String specSectionStart, String[] options) {\r
+ if (specsContent == null) {\r
+ throw new NullPointerException("specsContent");\r
+ }\r
+ if (specSectionStart == null) {\r
+ throw new NullPointerException("specSectionStart");\r
+ }\r
+ if (options == null) {\r
+ throw new NullPointerException("option");\r
+ }\r
+ String[][] optionValues = new String[options.length][];\r
+ StringBuffer optionValue = new StringBuffer(40);\r
+ for (int i = 0; i < specsContent.length; i++) {\r
+ String specLine = specsContent[i];\r
+ //\r
+ // if start of section then start paying attention\r
+ //\r
+ if (specLine.startsWith(specSectionStart)) {\r
+ Vector[] optionVectors = new Vector[options.length];\r
+ for (int j = 0; j < options.length; j++) {\r
+ optionVectors[j] = new Vector(10);\r
+ }\r
+ //\r
+ // go to next line and examine contents\r
+ // and repeat until end of file\r
+ //\r
+ for (i++; i < specsContent.length; i++) {\r
+ specLine = specsContent[i];\r
+ for (int j = 0; j < options.length; j++) {\r
+ int optionStart = specLine.indexOf(options[j]);\r
+ while (optionStart >= 0) {\r
+ optionValue.setLength(0);\r
+ //\r
+ // walk rest of line looking for first non\r
+ // whitespace\r
+ // and then next space\r
+ boolean hasNonBlank = false;\r
+ int k = optionStart + options[j].length();\r
+ for (; k < specLine.length(); k++) {\r
+ //\r
+ // either a blank or a "}" (close of\r
+ // conditional)\r
+ // section will end the path\r
+ //\r
+ if (specLine.charAt(k) == ' '\r
+ || specLine.charAt(k) == '}') {\r
+ if (hasNonBlank) {\r
+ break;\r
+ }\r
+ } else {\r
+ hasNonBlank = true;\r
+ optionValue.append(specLine.charAt(k));\r
+ }\r
+ }\r
+ //\r
+ // transition back to whitespace\r
+ // value is over, add it to vector\r
+ if (hasNonBlank) {\r
+ optionVectors[j].addElement(optionValue\r
+ .toString());\r
+ }\r
+ //\r
+ // find next occurance on line\r
+ //\r
+ optionStart = specLine.indexOf(options[j], k);\r
+ }\r
+ }\r
+ }\r
+ //\r
+ // copy vectors over to option arrays\r
+ //\r
+ for (int j = 0; j < options.length; j++) {\r
+ optionValues[j] = new String[optionVectors[j].size()];\r
+ optionVectors[j].copyInto(optionValues[j]);\r
+ }\r
+ }\r
+ }\r
+ //\r
+ // fill in any missing option values with\r
+ // a zero-length string array\r
+ for (int i = 0; i < optionValues.length; i++) {\r
+ String[] zeroLenArray = new String[0];\r
+ if (optionValues[i] == null) {\r
+ optionValues[i] = zeroLenArray;\r
+ }\r
+ }\r
+ return optionValues;\r
+ }\r
+ private GccProcessor() {\r
+ }\r
+}\r