3 * Copyright 2002-2004 The Ant-Contrib project
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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package net
.sf
.antcontrib
.cpptasks
.compiler
;
18 import org
.apache
.tools
.ant
.types
.Environment
;
20 * An abstract processor (compiler/linker) implementation.
24 public abstract class AbstractProcessor
implements Processor
, Cloneable
{
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
29 public final static int DEFAULT_DISCARD_BID
= 1;
31 * default bid for a file name that the processor desires to process
33 public final static int DEFAULT_PROCESS_BID
= 100;
35 * Determines the identification of a command line processor by capture the
36 * first line of its output for a specific command.
39 * array of command line arguments starting with executable
40 * name. For example, { "cl" }
42 * start of identifier if there is an error in executing the
44 * @return identifier for the processor
46 protected static String
getIdentifier(String
[] command
, String fallback
) {
47 String identifier
= fallback
;
49 String
[] cmdout
= CaptureStreamHandler
.run(command
);
50 if (cmdout
.length
> 0) {
51 identifier
= cmdout
[0];
53 } catch (Throwable ex
) {
54 identifier
= fallback
+ ":" + ex
.toString();
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();
66 * Returns the bid of the processor for the file.
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
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
;
81 for (int i
= 0; i
< headerExtensions
.length
; i
++) {
82 if (lower
.endsWith(headerExtensions
[i
])) {
83 return DEFAULT_DISCARD_BID
;
88 public Processor
changeEnvironment(boolean newEnvironment
, Environment env
) {
91 protected Object
clone() throws CloneNotSupportedException
{
94 public String
[] getHeaderExtensions() {
95 return (String
[]) this.headerExtensions
.clone();
97 abstract public String
getIdentifier();
99 * Gets the target operating system architecture
101 * @return String target operating system architecture
103 protected String
getOSArch() {
104 return System
.getProperty("os.arch");
107 * Gets the target operating system name
109 * @return String target operating system name
111 protected String
getOSName() {
112 return System
.getProperty("os.name");
114 public String
[] getSourceExtensions() {
115 return (String
[]) this.sourceExtensions
.clone();
118 * Returns true if the target operating system is Mac OS X or Darwin.
122 protected boolean isDarwin() {
123 String osName
= getOSName();
124 return "Mac OS X".equals(osName
);
126 public final String
toString() {
127 return getIdentifier();