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