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
;
19 import java
.util
.Hashtable
;
20 import java
.util
.Vector
;
22 import net
.sf
.antcontrib
.cpptasks
.compiler
.LinkerConfiguration
;
23 import net
.sf
.antcontrib
.cpptasks
.compiler
.ProcessorConfiguration
;
25 import org
.apache
.tools
.ant
.BuildException
;
27 * This class matches each visited file with an appropriate compiler
31 public final class TargetMatcher
implements FileVisitor
{
32 private LinkerConfiguration linker
;
33 private Vector objectFiles
;
34 private File outputDir
;
35 private ProcessorConfiguration
[] processors
;
36 private final File sourceFiles
[] = new File
[1];
37 private Hashtable targets
;
39 public TargetMatcher(CCTask task
, File outputDir
,
40 ProcessorConfiguration
[] processors
, LinkerConfiguration linker
,
41 Vector objectFiles
, Hashtable targets
) {
43 this.outputDir
= outputDir
;
44 this.processors
= processors
;
45 this.targets
= targets
;
47 this.objectFiles
= objectFiles
;
49 public void visit(File parentDir
, String filename
) throws BuildException
{
51 // see if any processor wants to bid
53 ProcessorConfiguration selectedCompiler
= null;
55 if (processors
!= null) {
56 for (int k
= 0; k
< processors
.length
; k
++) {
57 int newBid
= processors
[k
].bid(filename
);
60 selectedCompiler
= processors
[k
];
65 // no processor interested in file
66 // log diagnostic message
69 int linkerbid
= linker
.bid(filename
);
71 File objFile
= new File(parentDir
, filename
);
72 objectFiles
.addElement(objFile
);
74 task
.log("Unrecognized file type " + objFile
.toString()
75 + " will be passed to linker");
81 // get output file name
83 String outputFileName
= selectedCompiler
84 .getOutputFileName(filename
);
86 // if there is some output for this task
87 // (that is a source file and not an header file)
89 if (outputFileName
!= null) {
90 sourceFiles
[0] = new File(parentDir
, filename
);
92 // see if the same output file has already been registered
94 TargetInfo previousTarget
= (TargetInfo
) targets
96 if (previousTarget
== null) {
97 targets
.put(outputFileName
, new TargetInfo(
98 selectedCompiler
, sourceFiles
, null, new File(
99 outputDir
, outputFileName
),
100 selectedCompiler
.getRebuild()));
102 if (!previousTarget
.getSources()[0].equals(sourceFiles
[0])) {
103 StringBuffer builder
= new StringBuffer(
104 "Output filename conflict: ");
105 builder
.append(outputFileName
);
106 builder
.append(" would be produced from ");
107 builder
.append(previousTarget
.getSources()[0]
109 builder
.append(" and ");
110 builder
.append(filename
);
111 throw new BuildException(builder
.toString());