]> git.proxmox.com Git - mirror_edk2.git/blob - Tools/Source/Cpptasks/net/sf/antcontrib/cpptasks/compiler/CommandLineAslcompiler.java
Changed spelling to manifest
[mirror_edk2.git] / Tools / Source / Cpptasks / net / sf / antcontrib / cpptasks / compiler / CommandLineAslcompiler.java
1 /*
2 *
3 * Copyright 2001-2005 The Ant-Contrib project
4 *
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
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
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.
16 */
17 package net.sf.antcontrib.cpptasks.compiler;
18
19 import java.io.File;
20 import java.util.Enumeration;
21 import java.util.Vector;
22
23 import net.sf.antcontrib.cpptasks.AslcompilerDef;
24 import net.sf.antcontrib.cpptasks.CCTask;
25 import net.sf.antcontrib.cpptasks.CUtil;
26 import net.sf.antcontrib.cpptasks.ProcessorDef;
27 import net.sf.antcontrib.cpptasks.TargetDef;
28 import net.sf.antcontrib.cpptasks.types.CommandLineArgument;
29
30 import org.apache.tools.ant.BuildException;
31 /**
32 * An abstract ASL Compiler implementation which uses an external program to
33 * perform the ASL compile.
34 *
35 */
36 public abstract class CommandLineAslcompiler extends AbstractAslcompiler{
37
38 private String command;
39 private String identifier;
40 private String identifierArg;
41
42 protected CommandLineAslcompiler(String command, String identifierArg,
43 String[] sourceExtensions, String[] headerExtensions,
44 String outputSuffix) {
45 super(sourceExtensions, headerExtensions, outputSuffix);
46 this.command = command;
47 this.identifierArg = identifierArg;
48 }
49
50 abstract protected void addImpliedArgs(Vector args, boolean debug,
51 Boolean defaultflag);
52
53 /**
54 * Compile a ACPI source file
55 *
56 */
57 public void aslcompiler(CCTask task, File outputDir, String[] sourceFiles,
58 String[] args, String[] endArgs) throws BuildException{
59 String command = getCommand();
60 int baseLength = command.length() + args.length + endArgs.length;
61 for (int i = 0; i < args.length; i++) {
62 baseLength += args[i].length();
63 }
64 for (int i = 0; i < endArgs.length; i++) {
65 baseLength += endArgs[i].length();
66 }
67 if (baseLength > getMaximumCommandLength()) {
68 throw new BuildException(
69 "Command line is over maximum length without sepcifying source file");
70 }
71 int maxInputFilesPerCommand = getMaximumInputFilesPerCommand();
72 int argumentCountPerInputFile = getArgumentCountPerInputFIle();
73 for (int sourceIndex = 0; sourceIndex < sourceFiles.length;) {
74 int cmdLength = baseLength;
75 int firstFileNextExec;
76 for (firstFileNextExec = sourceIndex; firstFileNextExec < sourceFiles.length
77 && (firstFileNextExec - sourceIndex) < maxInputFilesPerCommand; firstFileNextExec++) {
78 cmdLength += getTotalArgumentLengthForInputFile(outputDir,
79 sourceFiles[firstFileNextExec]);
80 if (cmdLength >= getMaximumCommandLength())
81 break;
82 }
83 if (firstFileNextExec == sourceIndex) {
84 throw new BuildException(
85 "Extremely long file name, can't fit on command line");
86 }
87 int argCount = args.length + 1 + endArgs.length
88 + (firstFileNextExec - sourceIndex)
89 * argumentCountPerInputFile;
90 String[] commandline = new String[argCount];
91 int index = 0;
92 commandline[index++] = command;
93 for (int j = 0; j < args.length; j++) {
94 commandline[index++] = args[j];
95 }
96 for (int j = sourceIndex; j < firstFileNextExec; j++) {
97 for (int k = 0; k < argumentCountPerInputFile; k++) {
98 commandline[index++] = getInputFileArgument(outputDir,
99 sourceFiles[j], k);
100 }
101 }
102 for (int j = 0; j < endArgs.length; j++) {
103 commandline[index++] = endArgs[j];
104 }
105 int retval = runCommand(task, outputDir, commandline);
106 // if with monitor, add more code
107 if (retval != 0) {
108 throw new BuildException(this.getCommand()
109 + " failed with return code " + retval,
110 task.getLocation());
111 }
112 sourceIndex = firstFileNextExec;
113 }
114 }
115
116 protected AslcompilerConfiguration createConfiguration(final CCTask task,
117 final LinkType linkType,
118 final ProcessorDef[] baseDefs,
119 final AslcompilerDef specificDef,
120 final TargetDef targetPlatform) {
121 Vector args = new Vector();
122 AslcompilerDef[] defaultProviders = new AslcompilerDef[baseDefs.length +1];
123 for (int i = 0; i < baseDefs.length; i++) {
124 defaultProviders[i + 1] = (AslcompilerDef) baseDefs[i];
125 }
126 defaultProviders[0] = specificDef;
127 Vector cmdArgs = new Vector();
128 //
129 // add command line arguments inherited from <cc> element
130 // any "extends" and finally and specific AslcompilerDef
131 //
132 CommandLineArgument[] commandArgs;
133 for (int i = defaultProviders.length - 1; i >=0; i--){
134 commandArgs = defaultProviders[i].getActiveProcessorArgs();
135 for (int j = 0; j < commandArgs.length; j++) {
136 if (commandArgs[j].getLocation() == 0) {
137 args.addElement(commandArgs[j].getValue());
138 }
139 else {
140 cmdArgs.addElement(commandArgs[j]);
141 }
142 }
143 }
144 // omit param
145 boolean debug = specificDef.getDebug(baseDefs, 0);
146 Boolean defaultflag = specificDef.getDefaultflag(defaultProviders, 1);
147 this.addImpliedArgs(args, debug, defaultflag);
148 Enumeration argEnum = cmdArgs.elements();
149 int endCount = 0;
150 while( argEnum.hasMoreElements()) {
151 CommandLineArgument arg = (CommandLineArgument) argEnum.nextElement();
152 switch (arg.getLocation()) {
153 case 1 :
154 args.addElement(arg.getValue());
155 break;
156 case 2 :
157 endCount++;
158 break;
159 }
160 }
161 String[] endArgs = new String[endCount];
162 argEnum = cmdArgs.elements();
163 int index = 0;
164 while (argEnum.hasMoreElements()) {
165 CommandLineArgument arg = (CommandLineArgument) argEnum.nextElement();
166 if (arg.getLocation() == 2) {
167 endArgs[index++] = arg.getValue();
168 }
169 }
170 String[] argArray = new String[args.size()];
171 args.copyInto(argArray);
172 return new CommandLineAslcompilerConfiguration(this, argArray, true, endArgs);
173 }
174
175 protected int getArgumentCountPerInputFile() {
176 return 1;
177 }
178
179 public String getIdentifier() {
180 if (identifier == null) {
181 if (identifierArg == null) {
182 identifier = getIdentifier(new String[]{command}, command);
183 }
184 else {
185 identifier = getIdentifier(
186 new String[]{command, identifierArg}, command);
187 }
188 }
189 return identifier;
190 }
191
192 public final String getCommand() {
193 return command;
194 }
195 abstract public int getMaximumCommandLength();
196 public void setCommand(String command) {
197 this.command = command;
198 }
199 protected int getTotalArgumentLengthForInputFile(File outputDir,
200 String inputFile) {
201 return inputFile.length() + 1;
202 }
203 protected int runCommand(CCTask task, File workingDir, String[] cmdline)
204 throws BuildException {
205 return CUtil.runCommand(task, workingDir, cmdline, false, null);
206
207 }
208 protected int getMaximumInputFilesPerCommand(){
209 return 1;
210 }
211 protected int getArgumentCountPerInputFIle(){
212 return 1;
213 }
214 protected String getInputFileArgument(File outputDir, String filename, int index) {
215 //
216 // if there is an embedded space,
217 // must enclose in quotes
218 if (filename.indexOf(' ') >= 0) {
219 StringBuffer buf = new StringBuffer("\"");
220 buf.append(filename);
221 buf.append("\"");
222 return buf.toString();
223 }
224 return filename;
225 }
226 }