]>
Commit | Line | Data |
---|---|---|
878ddf1f | 1 | /*\r |
2 | * \r | |
3 | * Copyright 2001-2004 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 | |
509693cc | 17 | package net.sf.antcontrib.cpptasks.userdefine;\r |
18 | \r | |
19 | import java.io.File;\r | |
20 | import java.util.Iterator;\r | |
21 | import java.util.LinkedHashSet;\r | |
22 | import java.util.Set;\r | |
23 | import java.util.StringTokenizer;\r | |
24 | import java.util.Vector;\r | |
25 | \r | |
26 | import net.sf.antcontrib.cpptasks.CCTask;\r | |
27 | import net.sf.antcontrib.cpptasks.CUtil;\r | |
28 | import net.sf.antcontrib.cpptasks.types.CommandLineArgument;\r | |
29 | import net.sf.antcontrib.cpptasks.types.ConditionalFileSet;\r | |
30 | \r | |
31 | import org.apache.tools.ant.BuildException;\r | |
32 | import org.apache.tools.ant.DirectoryScanner;\r | |
33 | import org.apache.tools.ant.Project;\r | |
34 | \r | |
878ddf1f | 35 | public class CommandLineUserDefine {\r |
36 | \r | |
878ddf1f | 37 | String includePathDelimiter = null;\r |
38 | \r | |
509693cc | 39 | String outputDelimiter = null;\r |
40 | \r | |
878ddf1f | 41 | public void command(CCTask cctask, UserDefineDef userdefine){\r |
bf0ce309 | 42 | boolean isGccCommand = userdefine.getFamily().equalsIgnoreCase("GCC");\r |
878ddf1f | 43 | File workdir;\r |
878ddf1f | 44 | Project project = cctask.getProject();\r |
45 | if(userdefine.getWorkdir() == null) {\r | |
46 | workdir = new File(".");\r | |
47 | }\r | |
48 | else {\r | |
49 | workdir = userdefine.getWorkdir();\r | |
50 | } \r | |
51 | \r | |
509693cc | 52 | //\r |
53 | // generate cmdline= command + args + includepath + endargs + outfile\r | |
54 | // \r | |
878ddf1f | 55 | Vector args = new Vector();\r |
56 | Vector argsWithoutSpace = new Vector();\r | |
57 | Vector endargs = new Vector();\r | |
58 | Vector endargsWithoutSpace = new Vector();\r | |
59 | Vector includePath = new Vector();\r | |
60 | \r | |
509693cc | 61 | //\r |
62 | // Generate cmdline = command + \r | |
63 | // general args + \r | |
64 | // outputflag + outputfile\r | |
65 | // includpath + \r | |
66 | // endargs + \r | |
67 | // \r | |
68 | \r | |
69 | //\r | |
70 | // get Args.\r | |
71 | //\r | |
878ddf1f | 72 | CommandLineArgument[] argument = userdefine.getActiveProcessorArgs();\r |
73 | for (int j = 0; j < argument.length; j++) {\r | |
74 | if (argument[j].getLocation() == 0) {\r | |
75 | args.addElement(argument[j].getValue());\r | |
76 | } else {\r | |
77 | endargs.addElement(argument[j].getValue());\r | |
78 | }\r | |
79 | }\r | |
509693cc | 80 | \r |
81 | //\r | |
82 | // get include path.\r | |
83 | //\r | |
878ddf1f | 84 | String[] incPath = userdefine.getActiveIncludePaths();\r |
85 | for (int j = 0; j < incPath.length; j++) {\r | |
86 | if(incPath[j].indexOf(' ') >= 0) {\r | |
87 | includePath.addElement( includePathDelimiter + incPath[j]);\r | |
88 | //includePath.addElement( includePathDelimiter + "\"" + incPath[j] + "\"");\r | |
89 | }\r | |
90 | else {\r | |
91 | includePath.addElement( includePathDelimiter + incPath[j]);\r | |
92 | }\r | |
93 | }\r | |
509693cc | 94 | \r |
95 | //\r | |
96 | // Remove space in args and endargs.\r | |
97 | //\r | |
878ddf1f | 98 | for ( int i=0; i < args.size(); i++) {\r |
99 | String str = (String)args.get(i);\r | |
509693cc | 100 | StringTokenizer st = new StringTokenizer(str, " \t");\r |
878ddf1f | 101 | while(st.hasMoreTokens()) {\r |
102 | argsWithoutSpace.addElement(st.nextToken());\r | |
103 | }\r | |
104 | }\r | |
105 | for ( int i=0; i < endargs.size(); i++) {\r | |
106 | String str = (String)endargs.get(i);\r | |
509693cc | 107 | StringTokenizer st = new StringTokenizer(str, " \t");\r |
878ddf1f | 108 | while(st.hasMoreTokens()) {\r |
109 | endargsWithoutSpace.addElement(st.nextToken());\r | |
110 | }\r | |
111 | }\r | |
112 | \r | |
113 | int cmdLen = 0;\r | |
509693cc | 114 | //\r |
115 | // command + args + endargs + includepath + sourcefile\r | |
116 | //\r | |
117 | cmdLen = 1 + argsWithoutSpace.size() + endargsWithoutSpace.size() + includePath.size() + 1;\r | |
118 | String[] libSet = userdefine.get_libset();\r | |
119 | if (libSet != null && libSet.length > 0){\r | |
120 | cmdLen = cmdLen + libSet.length;\r | |
bf0ce309 | 121 | if (isGccCommand) {\r |
122 | cmdLen += 2; // we need -( and -) to group libs for GCC\r | |
123 | }\r | |
878ddf1f | 124 | }\r |
509693cc | 125 | \r |
126 | //\r | |
127 | // In gcc the "cr" flag should follow space then add outputfile name, otherwise\r | |
128 | // it will pop error. \r | |
129 | // TBD\r | |
130 | if (outputDelimiter != null && userdefine.getOutputFile() != null && outputDelimiter.trim().length() > 0){\r | |
131 | if (outputDelimiter.trim().equalsIgnoreCase("-cr")){\r | |
878ddf1f | 132 | cmdLen = cmdLen + 2;\r |
133 | }else {\r | |
134 | cmdLen++;\r | |
135 | }\r | |
878ddf1f | 136 | }\r |
509693cc | 137 | \r |
138 | //\r | |
139 | // for every source file\r | |
140 | // if file is header file, just skip it (add later)\r | |
141 | //\r | |
878ddf1f | 142 | Vector srcSets = userdefine.getSrcSets();\r |
509693cc | 143 | // System.out.println("##" + userdefine.getSrcSets());\r |
878ddf1f | 144 | \r |
145 | //\r | |
146 | // if have source file append source file in command land.\r | |
147 | //\r | |
509693cc | 148 | Set allSrcFiles = new LinkedHashSet();\r |
878ddf1f | 149 | for (int i = 0; i < srcSets.size(); i++) {\r |
150 | ConditionalFileSet srcSet = (ConditionalFileSet) srcSets\r | |
151 | .elementAt(i);\r | |
152 | if (srcSet.isActive()) {\r | |
153 | // Find matching source files\r | |
154 | DirectoryScanner scanner = srcSet.getDirectoryScanner(project);\r | |
155 | // Check each source file - see if it needs compilation\r | |
156 | String[] fileNames = scanner.getIncludedFiles();\r | |
157 | for (int j = 0; j < fileNames.length; j++){\r | |
878ddf1f | 158 | // execute the command\r |
509693cc | 159 | allSrcFiles.add(scanner.getBasedir() + "/" + fileNames[j]);\r |
878ddf1f | 160 | }\r |
161 | }\r | |
162 | }\r | |
509693cc | 163 | \r |
164 | String[] fileNames = (String[])allSrcFiles.toArray(new String[allSrcFiles.size()]);\r | |
165 | String[] cmd = new String[cmdLen - 1 + fileNames.length];\r | |
166 | int index = 0;\r | |
167 | cmd[index++] = userdefine.getCmd();\r | |
168 | \r | |
169 | Iterator iter = argsWithoutSpace.iterator();\r | |
170 | while (iter.hasNext()) {\r | |
171 | cmd[index++] = project.replaceProperties((String)iter.next());\r | |
172 | }\r | |
173 | \r | |
174 | iter = endargsWithoutSpace.iterator();\r | |
175 | while (iter.hasNext()) {\r | |
176 | cmd[index++] = project.replaceProperties((String)iter.next());\r | |
177 | }\r | |
178 | \r | |
179 | //\r | |
180 | // Add outputFileFlag and output file to cmd\r | |
181 | //\r | |
182 | if (outputDelimiter != null && userdefine.getOutputFile() != null && outputDelimiter.length()> 0){\r | |
183 | if (outputDelimiter.trim().equalsIgnoreCase("-cr")){\r | |
184 | cmd[index++] = outputDelimiter;\r | |
185 | cmd[index++] = userdefine.getOutputFile();\r | |
186 | }else {\r | |
187 | cmd[index++] = outputDelimiter + userdefine.getOutputFile();\r | |
188 | }\r | |
189 | }\r | |
190 | \r | |
191 | iter = includePath.iterator();\r | |
192 | while (iter.hasNext()) {\r | |
193 | cmd[index++] = (String)iter.next();\r | |
194 | }\r | |
195 | \r | |
196 | if (libSet != null && libSet.length > 0){\r | |
bf0ce309 | 197 | if (isGccCommand) {\r |
198 | cmd[index++] = "-(";\r | |
199 | }\r | |
509693cc | 200 | for (int k = 0; k < libSet.length ; k++){\r |
201 | cmd[index++] = libSet[k];\r | |
202 | }\r | |
bf0ce309 | 203 | if (isGccCommand) {\r |
204 | cmd[index++] = "-)";\r | |
205 | }\r | |
509693cc | 206 | }\r |
207 | for (int j = 0; j < fileNames.length; j++){\r | |
208 | // execute the command\r | |
209 | cmd[index++] = fileNames[j];\r | |
210 | }\r | |
211 | \r | |
212 | int retval = runCommand(cctask, workdir, cmd);\r | |
213 | // if with monitor, add more code\r | |
214 | if (retval != 0) {\r | |
215 | throw new BuildException(userdefine.getCmd()\r | |
216 | + " failed with return code " + retval,\r | |
217 | cctask.getLocation());\r | |
218 | }\r | |
878ddf1f | 219 | }\r |
220 | \r | |
221 | protected int runCommand(CCTask task, File workingDir, String[] cmdline)\r | |
222 | throws BuildException {\r | |
223 | return CUtil.runCommand(task, workingDir, cmdline, false, null);\r | |
224 | \r | |
225 | }\r | |
226 | \r | |
227 | protected String getInputFileArgument(File outputDir, String filename,\r | |
228 | int index) {\r | |
229 | //\r | |
230 | // if there is an embedded space,\r | |
231 | // must enclose in quotes\r | |
232 | if (filename.indexOf(' ') >= 0) {\r | |
233 | StringBuffer buf = new StringBuffer("\"");\r | |
234 | buf.append(filename);\r | |
235 | buf.append("\"");\r | |
236 | return buf.toString();\r | |
237 | }\r | |
238 | return filename;\r | |
239 | }\r | |
240 | \r | |
241 | }\r |