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;
21  
22  import java.io.File;
23  
24  import com.github.maven_nar.cpptasks.compiler.ProcessorConfiguration;
25  
26  /**
27   * A description of a file built or to be built
28   */
29  public final class TargetInfo {
30    private static final File[] emptyFileArray = new File[0];
31    private final/* final */ProcessorConfiguration config;
32    private final/* final */File output;
33    private boolean rebuild;
34    private final/* final */File[] sources;
35    private File[] sysSources;
36  
37    public TargetInfo(final ProcessorConfiguration config, final File[] sources, final File[] sysSources,
38        final File output, boolean rebuild) {
39      if (config == null) {
40        throw new NullPointerException("config");
41      }
42      if (sources == null) {
43        throw new NullPointerException("sources");
44      }
45      if (output == null) {
46        throw new NullPointerException("output");
47      }
48      this.config = config;
49      this.sources = sources.clone();
50      if (sysSources == null) {
51        this.sysSources = emptyFileArray;
52      } else {
53        this.sysSources = sysSources.clone();
54      }
55      this.output = output;
56      this.rebuild = rebuild;
57      //
58      // if the output doesn't exist, must rebuild it
59      //
60      if (!output.exists()) {
61        rebuild = true;
62      }
63    }
64  
65    public String[] getAllSourcePaths() {
66      final String[] paths = new String[this.sysSources.length + this.sources.length];
67      for (int i = 0; i < this.sysSources.length; i++) {
68        paths[i] = this.sysSources[i].toString();
69      }
70      final int offset = this.sysSources.length;
71      for (int i = 0; i < this.sources.length; i++) {
72        paths[offset + i] = this.sources[i].toString();
73      }
74      return paths;
75    }
76  
77    public File[] getAllSources() {
78      final File[] allSources = new File[this.sources.length + this.sysSources.length];
79      System.arraycopy(this.sysSources, 0, allSources, 0, this.sysSources.length);
80      final int offset = this.sysSources.length;
81      System.arraycopy(this.sources, 0, allSources, 0 + offset, this.sources.length);
82      return allSources;
83    }
84  
85    public ProcessorConfiguration getConfiguration() {
86      return this.config;
87    }
88  
89    public File getOutput() {
90      return this.output;
91    }
92  
93    public boolean getRebuild() {
94      return this.rebuild;
95    }
96  
97    /**
98     * Returns an array of SourceHistory objects (contains relative path and
99     * last modified time) for the source[s] of this target
100    */
101   public SourceHistory[] getSourceHistories(final String basePath) {
102     final SourceHistory[] histories = new SourceHistory[this.sources.length];
103     for (int i = 0; i < this.sources.length; i++) {
104       final String relativeName = CUtil.getRelativePath(basePath, this.sources[i]);
105       final long lastModified = this.sources[i].lastModified();
106       histories[i] = new SourceHistory(relativeName, lastModified);
107     }
108     return histories;
109   }
110 
111   public String[] getSourcePaths() {
112     final String[] paths = new String[this.sources.length];
113     for (int i = 0; i < this.sources.length; i++) {
114       paths[i] = this.sources[i].toString();
115     }
116     return paths;
117   }
118 
119   public File[] getSources() {
120     final File[] clone = this.sources.clone();
121     return clone;
122   }
123 
124   public String[] getSysSourcePaths() {
125     final String[] paths = new String[this.sysSources.length];
126     for (int i = 0; i < this.sysSources.length; i++) {
127       paths[i] = this.sysSources[i].toString();
128     }
129     return paths;
130   }
131 
132   public File[] getSysSources() {
133     final File[] clone = this.sysSources.clone();
134     return clone;
135   }
136 
137   public void mustRebuild() {
138     this.rebuild = true;
139   }
140 }