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