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