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