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