View Javadoc

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