]>
Commit | Line | Data |
---|---|---|
878ddf1f | 1 | /*\r |
2 | * \r | |
3 | * Copyright 2002-2004 The Ant-Contrib project\r | |
4 | *\r | |
5 | * Licensed under the Apache License, Version 2.0 (the "License");\r | |
6 | * you may not use this file except in compliance with the License.\r | |
7 | * You may obtain a copy of the License at\r | |
8 | *\r | |
9 | * http://www.apache.org/licenses/LICENSE-2.0\r | |
10 | *\r | |
11 | * Unless required by applicable law or agreed to in writing, software\r | |
12 | * distributed under the License is distributed on an "AS IS" BASIS,\r | |
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r | |
14 | * See the License for the specific language governing permissions and\r | |
15 | * limitations under the License.\r | |
16 | */\r | |
17 | package net.sf.antcontrib.cpptasks.compiler;\r | |
18 | import org.apache.tools.ant.types.Environment;\r | |
19 | /**\r | |
20 | * An abstract processor (compiler/linker) implementation.\r | |
21 | * \r | |
22 | * @author Curt Arnold\r | |
23 | */\r | |
24 | public abstract class AbstractProcessor implements Processor, Cloneable {\r | |
25 | /**\r | |
26 | * default bid for a file name that the processor recognizes but does not\r | |
27 | * process and does not want to fall through to the linker\r | |
28 | */\r | |
29 | public final static int DEFAULT_DISCARD_BID = 1;\r | |
30 | /**\r | |
31 | * default bid for a file name that the processor desires to process\r | |
32 | */\r | |
33 | public final static int DEFAULT_PROCESS_BID = 100;\r | |
34 | /**\r | |
35 | * Determines the identification of a command line processor by capture the\r | |
36 | * first line of its output for a specific command.\r | |
37 | * \r | |
38 | * @param command\r | |
39 | * array of command line arguments starting with executable\r | |
40 | * name. For example, { "cl" }\r | |
41 | * @param fallback\r | |
42 | * start of identifier if there is an error in executing the\r | |
43 | * command\r | |
44 | * @return identifier for the processor\r | |
45 | */\r | |
46 | protected static String getIdentifier(String[] command, String fallback) {\r | |
47 | String identifier = fallback;\r | |
48 | try {\r | |
49 | String[] cmdout = CaptureStreamHandler.run(command);\r | |
50 | if (cmdout.length > 0) {\r | |
51 | identifier = cmdout[0];\r | |
52 | }\r | |
53 | } catch (Throwable ex) {\r | |
54 | identifier = fallback + ":" + ex.toString();\r | |
55 | }\r | |
56 | return identifier;\r | |
57 | }\r | |
58 | private final String[] headerExtensions;\r | |
59 | private final String[] sourceExtensions;\r | |
60 | protected AbstractProcessor(String[] sourceExtensions,\r | |
61 | String[] headerExtensions) {\r | |
62 | this.sourceExtensions = (String[]) sourceExtensions.clone();\r | |
63 | this.headerExtensions = (String[]) headerExtensions.clone();\r | |
64 | }\r | |
65 | /**\r | |
66 | * Returns the bid of the processor for the file.\r | |
67 | * \r | |
68 | * @param inputFile\r | |
69 | * filename of input file\r | |
70 | * @return bid for the file, 0 indicates no interest, 1 indicates that the\r | |
71 | * processor recognizes the file but doesn't process it (header\r | |
72 | * files, for example), 100 indicates strong interest\r | |
73 | */\r | |
74 | public int bid(String inputFile) {\r | |
75 | String lower = inputFile.toLowerCase();\r | |
76 | for (int i = 0; i < sourceExtensions.length; i++) {\r | |
77 | if (lower.endsWith(sourceExtensions[i])) {\r | |
78 | return DEFAULT_PROCESS_BID;\r | |
79 | }\r | |
80 | }\r | |
81 | for (int i = 0; i < headerExtensions.length; i++) {\r | |
82 | if (lower.endsWith(headerExtensions[i])) {\r | |
83 | return DEFAULT_DISCARD_BID;\r | |
84 | }\r | |
85 | }\r | |
86 | return 0;\r | |
87 | }\r | |
88 | public Processor changeEnvironment(boolean newEnvironment, Environment env) {\r | |
89 | return this;\r | |
90 | }\r | |
91 | protected Object clone() throws CloneNotSupportedException {\r | |
92 | return super.clone();\r | |
93 | }\r | |
94 | public String[] getHeaderExtensions() {\r | |
95 | return (String[]) this.headerExtensions.clone();\r | |
96 | }\r | |
97 | abstract public String getIdentifier();\r | |
98 | /**\r | |
99 | * Gets the target operating system architecture\r | |
100 | * \r | |
101 | * @return String target operating system architecture\r | |
102 | */\r | |
103 | protected String getOSArch() {\r | |
104 | return System.getProperty("os.arch");\r | |
105 | }\r | |
106 | /**\r | |
107 | * Gets the target operating system name\r | |
108 | * \r | |
109 | * @return String target operating system name\r | |
110 | */\r | |
111 | protected String getOSName() {\r | |
112 | return System.getProperty("os.name");\r | |
113 | }\r | |
114 | public String[] getSourceExtensions() {\r | |
115 | return (String[]) this.sourceExtensions.clone();\r | |
116 | }\r | |
117 | /**\r | |
118 | * Returns true if the target operating system is Mac OS X or Darwin.\r | |
119 | * \r | |
120 | * @return boolean\r | |
121 | */\r | |
122 | protected boolean isDarwin() {\r | |
123 | String osName = getOSName();\r | |
124 | return "Mac OS X".equals(osName);\r | |
125 | }\r | |
126 | public final String toString() {\r | |
127 | return getIdentifier();\r | |
128 | }\r | |
129 | }\r |