]>
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 | |
17 | package net.sf.antcontrib.cpptasks.types;\r | |
18 | import java.io.File;\r | |
19 | \r | |
20 | import net.sf.antcontrib.cpptasks.CUtil;\r | |
21 | import net.sf.antcontrib.cpptasks.FileVisitor;\r | |
22 | import net.sf.antcontrib.cpptasks.compiler.Linker;\r | |
23 | \r | |
24 | import org.apache.tools.ant.BuildException;\r | |
25 | import org.apache.tools.ant.DirectoryScanner;\r | |
26 | import org.apache.tools.ant.Project;\r | |
27 | import org.apache.tools.ant.ProjectComponent;\r | |
28 | import org.apache.tools.ant.types.FileSet;\r | |
29 | import org.apache.tools.ant.types.PatternSet;\r | |
30 | /**\r | |
31 | * A set of library names. Libraries can also be added to a link by specifying\r | |
32 | * them in a fileset.\r | |
33 | * \r | |
34 | * For most Unix-like compilers, libset will result in a series of -l and -L\r | |
35 | * linker arguments. For Windows compilers, the library names will be used to\r | |
36 | * locate the appropriate library files which will be added to the linkers\r | |
37 | * input file list as if they had been specified in a fileset.\r | |
38 | * \r | |
39 | * @author Mark A Russell <a\r | |
40 | * href="mailto:mark_russell@csgsystems.com">mark_russell@csg_systems.com\r | |
41 | * </a>\r | |
42 | * @author Adam Murdoch\r | |
43 | * @author Curt Arnold\r | |
44 | */\r | |
45 | public class LibrarySet extends ProjectComponent {\r | |
46 | private String dataset;\r | |
47 | private boolean explicitCaseSensitive;\r | |
48 | private String ifCond;\r | |
49 | private String[] libnames;\r | |
50 | private final FileSet set = new FileSet();\r | |
51 | private String unlessCond;\r | |
52 | private LibraryTypeEnum libraryType;\r | |
53 | public LibrarySet() {\r | |
54 | libnames = new String[0];\r | |
55 | }\r | |
56 | public void execute() throws org.apache.tools.ant.BuildException {\r | |
57 | throw new org.apache.tools.ant.BuildException(\r | |
58 | "Not an actual task, but looks like one for documentation purposes");\r | |
59 | }\r | |
60 | /**\r | |
61 | * Gets the dataset. Used on OS390 if the libs are in a dataset.\r | |
62 | * \r | |
63 | * @return Returns a String\r | |
64 | */\r | |
65 | public String getDataset() {\r | |
66 | return dataset;\r | |
67 | }\r | |
68 | public File getDir(Project project) {\r | |
69 | return set.getDir(project);\r | |
70 | }\r | |
71 | protected FileSet getFileSet() {\r | |
72 | return set;\r | |
73 | }\r | |
74 | public String[] getLibs() {\r | |
75 | String[] retval = (String[]) libnames.clone();\r | |
76 | return retval;\r | |
77 | }\r | |
78 | \r | |
79 | /**\r | |
80 | * Gets preferred library type\r | |
81 | * \r | |
82 | * @return library type, may be null.\r | |
83 | */\r | |
84 | public LibraryTypeEnum getType() {\r | |
85 | return libraryType;\r | |
86 | }\r | |
87 | /**\r | |
88 | * Returns true if the define's if and unless conditions (if any) are\r | |
89 | * satisfied.\r | |
90 | */\r | |
91 | public boolean isActive(org.apache.tools.ant.Project p) {\r | |
92 | if (p == null) {\r | |
93 | throw new NullPointerException("p");\r | |
94 | }\r | |
95 | if (ifCond != null) {\r | |
96 | String ifValue = p.getProperty(ifCond);\r | |
97 | if (ifValue != null) {\r | |
98 | if (ifValue.equals("no") || ifValue.equals("false")) {\r | |
99 | throw new BuildException(\r | |
100 | "property "\r | |
101 | + ifCond\r | |
102 | + " used as if condition has value "\r | |
103 | + ifValue\r | |
104 | + " which suggests a misunderstanding of if attributes");\r | |
105 | }\r | |
106 | } else {\r | |
107 | return false;\r | |
108 | }\r | |
109 | }\r | |
110 | if (unlessCond != null) {\r | |
111 | String unlessValue = p.getProperty(unlessCond);\r | |
112 | if (unlessValue != null) {\r | |
113 | if (unlessValue.equals("no") || unlessValue.equals("false")) {\r | |
114 | throw new BuildException(\r | |
115 | "property "\r | |
116 | + unlessCond\r | |
117 | + " used as unless condition has value "\r | |
118 | + unlessValue\r | |
119 | + " which suggests a misunderstanding of unless attributes");\r | |
120 | }\r | |
121 | return false;\r | |
122 | }\r | |
123 | }\r | |
124 | if (libnames.length == 0) {\r | |
125 | p.log("libnames not specified or empty.", Project.MSG_WARN);\r | |
126 | return false;\r | |
127 | }\r | |
128 | return true;\r | |
129 | }\r | |
130 | /**\r | |
131 | * Sets case sensitivity of the file system. If not set, will default to\r | |
132 | * the linker's case sensitivity.\r | |
133 | * \r | |
134 | * @param isCaseSensitive\r | |
135 | * "true"|"on"|"yes" if file system is case sensitive,\r | |
136 | * "false"|"off"|"no" when not.\r | |
137 | */\r | |
138 | public void setCaseSensitive(boolean isCaseSensitive) {\r | |
139 | explicitCaseSensitive = true;\r | |
140 | set.setCaseSensitive(isCaseSensitive);\r | |
141 | }\r | |
142 | /**\r | |
143 | * Sets the dataset. Used on OS390 if the libs are in a dataset.\r | |
144 | * \r | |
145 | * @param dataset\r | |
146 | * The dataset to set\r | |
147 | */\r | |
148 | public void setDataset(String dataset) {\r | |
149 | this.dataset = dataset;\r | |
150 | }\r | |
151 | /**\r | |
152 | * Library directory.\r | |
153 | * \r | |
154 | * @param dir\r | |
155 | * library directory\r | |
156 | * \r | |
157 | */\r | |
158 | public void setDir(File dir) throws BuildException {\r | |
159 | set.setDir(dir);\r | |
160 | }\r | |
161 | /**\r | |
162 | * Sets the property name for the 'if' condition.\r | |
163 | * \r | |
164 | * The library set will be ignored unless the property is defined.\r | |
165 | * \r | |
166 | * The value of the property is insignificant, but values that would imply\r | |
167 | * misinterpretation ("false", "no") will throw an exception when\r | |
168 | * evaluated.\r | |
169 | * \r | |
170 | * @param propName\r | |
171 | * property name\r | |
172 | */\r | |
173 | public void setIf(String propName) {\r | |
174 | ifCond = propName;\r | |
175 | }\r | |
176 | /**\r | |
177 | * Comma-separated list of library names without leading prefixes, such as\r | |
178 | * "lib", or extensions, such as ".so" or ".a".\r | |
179 | * \r | |
180 | */\r | |
181 | public void setLibs(CUtil.StringArrayBuilder libs) throws BuildException {\r | |
182 | libnames = libs.getValue();\r | |
183 | // If this is not active.. then it's ok if the lib names are invalid.\r | |
184 | // so we can do a: <libset if="x.lib" dir="." libs="${x.lib}"/>\r | |
185 | if (!isActive(getProject()))\r | |
186 | return;\r | |
187 | for (int i = 0; i < libnames.length; i++) {\r | |
188 | int lastDot = libnames[i].lastIndexOf('.');\r | |
189 | if (lastDot >= 0) {\r | |
190 | String extension = libnames[i].substring(lastDot);\r | |
191 | if (extension.equalsIgnoreCase(".lib")\r | |
192 | || extension.equalsIgnoreCase(".so")\r | |
193 | || extension.equalsIgnoreCase(".a")) {\r | |
194 | getProject().log(\r | |
195 | "Suspicious library name ending with \""\r | |
196 | + extension + "\": " + libnames[i], Project.MSG_DEBUG );\r | |
197 | }\r | |
198 | }\r | |
199 | if (libnames[i].length() >= 3\r | |
200 | && libnames[i].substring(0, 3).equalsIgnoreCase("lib")) {\r | |
201 | getProject().log(\r | |
202 | "Suspicious library name starting with \"lib\": "\r | |
203 | + libnames[i], Project.MSG_DEBUG);\r | |
204 | }\r | |
205 | }\r | |
206 | }\r | |
207 | public void setProject(Project project) {\r | |
208 | set.setProject(project);\r | |
209 | super.setProject(project);\r | |
210 | }\r | |
211 | /**\r | |
212 | * Set the property name for the 'unless' condition.\r | |
213 | * \r | |
214 | * If named property is set, the library set will be ignored.\r | |
215 | * \r | |
216 | * The value of the property is insignificant, but values that would imply\r | |
217 | * misinterpretation ("false", "no") of the behavior will throw an\r | |
218 | * exception when evaluated.\r | |
219 | * \r | |
220 | * @param propName\r | |
221 | * name of property\r | |
222 | */\r | |
223 | public void setUnless(String propName) {\r | |
224 | unlessCond = propName;\r | |
225 | }\r | |
226 | \r | |
227 | /**\r | |
228 | * Sets the preferred library type. Supported values "shared", "static", and\r | |
229 | * "framework". "framework" is equivalent to "shared" on non-Darwin platforms. \r | |
230 | */\r | |
231 | public void setType(LibraryTypeEnum type) {\r | |
232 | this.libraryType = type;\r | |
233 | }\r | |
234 | \r | |
235 | public void visitLibraries(Project project, Linker linker, File[] libpath,\r | |
236 | FileVisitor visitor) throws BuildException {\r | |
237 | FileSet localSet = (FileSet) set.clone();\r | |
238 | //\r | |
239 | // unless explicitly set\r | |
240 | // will default to the linker case sensitivity\r | |
241 | //\r | |
242 | if (!explicitCaseSensitive) {\r | |
243 | boolean linkerCaseSensitive = linker.isCaseSensitive();\r | |
244 | localSet.setCaseSensitive(linkerCaseSensitive);\r | |
245 | }\r | |
246 | //\r | |
247 | // if there was a libs attribute then\r | |
248 | // add the corresponding patterns to the FileSet\r | |
249 | //\r | |
250 | if (libnames != null && libnames.length > 0) {\r | |
251 | String[] patterns = linker.getLibraryPatterns(libnames, libraryType);\r | |
252 | //\r | |
253 | // if no patterns, then linker does not support libraries\r | |
254 | //\r | |
255 | if (patterns.length > 0) {\r | |
256 | for (int i = 0; i < patterns.length; i++) {\r | |
257 | PatternSet.NameEntry entry = localSet.createInclude();\r | |
258 | entry.setName(patterns[i]);\r | |
259 | }\r | |
260 | //\r | |
261 | // if there was no specified directory then\r | |
262 | // run through the libpath backwards\r | |
263 | //\r | |
264 | if (localSet.getDir(project) == null) {\r | |
265 | //\r | |
266 | // scan libpath in reverse order\r | |
267 | // to give earlier entries priority\r | |
268 | //\r | |
269 | for (int j = libpath.length - 1; j >= 0; j--) {\r | |
270 | FileSet clone = (FileSet) localSet.clone();\r | |
271 | clone.setDir(libpath[j]);\r | |
272 | DirectoryScanner scanner = clone.getDirectoryScanner(project);\r | |
273 | File basedir = scanner.getBasedir();\r | |
274 | String[] files = scanner.getIncludedFiles();\r | |
275 | for (int k = 0; k < files.length; k++) {\r | |
276 | visitor.visit(basedir, files[k]);\r | |
277 | }\r | |
278 | }\r | |
279 | } else {\r | |
280 | DirectoryScanner scanner = localSet.getDirectoryScanner(project);\r | |
281 | File basedir = scanner.getBasedir();\r | |
282 | String[] files = scanner.getIncludedFiles();\r | |
283 | for (int k = 0; k < files.length; k++) {\r | |
284 | visitor.visit(basedir, files[k]);\r | |
285 | }\r | |
286 | }\r | |
287 | }\r | |
288 | }\r | |
289 | }\r | |
290 | }\r |