--- /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.compiler;\r
+import org.apache.tools.ant.types.Environment;\r
+/**\r
+ * An abstract processor (compiler/linker) implementation.\r
+ * \r
+ * @author Curt Arnold\r
+ */\r
+public abstract class AbstractProcessor implements Processor, Cloneable {\r
+ /**\r
+ * default bid for a file name that the processor recognizes but does not\r
+ * process and does not want to fall through to the linker\r
+ */\r
+ public final static int DEFAULT_DISCARD_BID = 1;\r
+ /**\r
+ * default bid for a file name that the processor desires to process\r
+ */\r
+ public final static int DEFAULT_PROCESS_BID = 100;\r
+ /**\r
+ * Determines the identification of a command line processor by capture the\r
+ * first line of its output for a specific command.\r
+ * \r
+ * @param command\r
+ * array of command line arguments starting with executable\r
+ * name. For example, { "cl" }\r
+ * @param fallback\r
+ * start of identifier if there is an error in executing the\r
+ * command\r
+ * @return identifier for the processor\r
+ */\r
+ protected static String getIdentifier(String[] command, String fallback) {\r
+ String identifier = fallback;\r
+ try {\r
+ String[] cmdout = CaptureStreamHandler.run(command);\r
+ if (cmdout.length > 0) {\r
+ identifier = cmdout[0];\r
+ }\r
+ } catch (Throwable ex) {\r
+ identifier = fallback + ":" + ex.toString();\r
+ }\r
+ return identifier;\r
+ }\r
+ private final String[] headerExtensions;\r
+ private final String[] sourceExtensions;\r
+ protected AbstractProcessor(String[] sourceExtensions,\r
+ String[] headerExtensions) {\r
+ this.sourceExtensions = (String[]) sourceExtensions.clone();\r
+ this.headerExtensions = (String[]) headerExtensions.clone();\r
+ }\r
+ /**\r
+ * Returns the bid of the processor for the file.\r
+ * \r
+ * @param inputFile\r
+ * filename of input file\r
+ * @return bid for the file, 0 indicates no interest, 1 indicates that the\r
+ * processor recognizes the file but doesn't process it (header\r
+ * files, for example), 100 indicates strong interest\r
+ */\r
+ public int bid(String inputFile) {\r
+ String lower = inputFile.toLowerCase();\r
+ for (int i = 0; i < sourceExtensions.length; i++) {\r
+ if (lower.endsWith(sourceExtensions[i])) {\r
+ return DEFAULT_PROCESS_BID;\r
+ }\r
+ }\r
+ for (int i = 0; i < headerExtensions.length; i++) {\r
+ if (lower.endsWith(headerExtensions[i])) {\r
+ return DEFAULT_DISCARD_BID;\r
+ }\r
+ }\r
+ return 0;\r
+ }\r
+ public Processor changeEnvironment(boolean newEnvironment, Environment env) {\r
+ return this;\r
+ }\r
+ protected Object clone() throws CloneNotSupportedException {\r
+ return super.clone();\r
+ }\r
+ public String[] getHeaderExtensions() {\r
+ return (String[]) this.headerExtensions.clone();\r
+ }\r
+ abstract public String getIdentifier();\r
+ /**\r
+ * Gets the target operating system architecture\r
+ * \r
+ * @return String target operating system architecture\r
+ */\r
+ protected String getOSArch() {\r
+ return System.getProperty("os.arch");\r
+ }\r
+ /**\r
+ * Gets the target operating system name\r
+ * \r
+ * @return String target operating system name\r
+ */\r
+ protected String getOSName() {\r
+ return System.getProperty("os.name");\r
+ }\r
+ public String[] getSourceExtensions() {\r
+ return (String[]) this.sourceExtensions.clone();\r
+ }\r
+ /**\r
+ * Returns true if the target operating system is Mac OS X or Darwin.\r
+ * \r
+ * @return boolean\r
+ */\r
+ protected boolean isDarwin() {\r
+ String osName = getOSName();\r
+ return "Mac OS X".equals(osName);\r
+ }\r
+ public final String toString() {\r
+ return getIdentifier();\r
+ }\r
+}\r