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