summaryrefslogtreecommitdiff
path: root/Tools/Java/Source/Cpptasks/net/sf/antcontrib/cpptasks/types/LibrarySet.java
blob: d522ccc36ff37e6f8eadc00a4de0e6c49e83fa81 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
/*
 * 
 * Copyright 2001-2004 The Ant-Contrib project
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package net.sf.antcontrib.cpptasks.types;
import java.io.File;

import net.sf.antcontrib.cpptasks.CUtil;
import net.sf.antcontrib.cpptasks.FileVisitor;
import net.sf.antcontrib.cpptasks.compiler.Linker;

import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.DirectoryScanner;
import org.apache.tools.ant.Project;
import org.apache.tools.ant.ProjectComponent;
import org.apache.tools.ant.types.FileSet;
import org.apache.tools.ant.types.PatternSet;
/**
 * A set of library names. Libraries can also be added to a link by specifying
 * them in a fileset.
 * 
 * For most Unix-like compilers, libset will result in a series of -l and -L
 * linker arguments. For Windows compilers, the library names will be used to
 * locate the appropriate library files which will be added to the linkers
 * input file list as if they had been specified in a fileset.
 * 
 * @author Mark A Russell <a
 *         href="mailto:mark_russell@csgsystems.com">mark_russell@csg_systems.com
 *         </a>
 * @author Adam Murdoch
 * @author Curt Arnold
 */
public class LibrarySet extends ProjectComponent {
    private String dataset;
    private boolean explicitCaseSensitive;
    private String ifCond;
    private String[] libnames;
    private final FileSet set = new FileSet();
    private String unlessCond;
    private LibraryTypeEnum libraryType;
    public LibrarySet() {
        libnames = new String[0];
    }
    public void execute() throws org.apache.tools.ant.BuildException {
        throw new org.apache.tools.ant.BuildException(
                "Not an actual task, but looks like one for documentation purposes");
    }
    /**
     * Gets the dataset. Used on OS390 if the libs are in a dataset.
     * 
     * @return Returns a String
     */
    public String getDataset() {
        return dataset;
    }
    public File getDir(Project project) {
        return set.getDir(project);
    }
    protected FileSet getFileSet() {
        return set;
    }
    public String[] getLibs() {
        String[] retval = (String[]) libnames.clone();
        return retval;
    }
    
    /**
     * Gets preferred library type
     * 
     * @return library type, may be null.
     */
    public LibraryTypeEnum getType() {
    	return libraryType;
    }
    /**
     * Returns true if the define's if and unless conditions (if any) are
     * satisfied.
     */
    public boolean isActive(org.apache.tools.ant.Project p) {
        if (p == null) {
            throw new NullPointerException("p");
        }
        if (ifCond != null) {
            String ifValue = p.getProperty(ifCond);
            if (ifValue != null) {
                if (ifValue.equals("no") || ifValue.equals("false")) {
                    throw new BuildException(
                            "property "
                                    + ifCond
                                    + " used as if condition has value "
                                    + ifValue
                                    + " which suggests a misunderstanding of if attributes");
                }
            } else {
                return false;
            }
        }
        if (unlessCond != null) {
            String unlessValue = p.getProperty(unlessCond);
            if (unlessValue != null) {
                if (unlessValue.equals("no") || unlessValue.equals("false")) {
                    throw new BuildException(
                            "property "
                                    + unlessCond
                                    + " used as unless condition has value "
                                    + unlessValue
                                    + " which suggests a misunderstanding of unless attributes");
                }
                return false;
            }
        }
        if (libnames.length == 0) {
            p.log("libnames not specified or empty.", Project.MSG_WARN);
            return false;
        }
        return true;
    }
    /**
     * Sets case sensitivity of the file system. If not set, will default to
     * the linker's case sensitivity.
     * 
     * @param isCaseSensitive
     *            "true"|"on"|"yes" if file system is case sensitive,
     *            "false"|"off"|"no" when not.
     */
    public void setCaseSensitive(boolean isCaseSensitive) {
        explicitCaseSensitive = true;
        set.setCaseSensitive(isCaseSensitive);
    }
    /**
     * Sets the dataset. Used on OS390 if the libs are in a dataset.
     * 
     * @param dataset
     *            The dataset to set
     */
    public void setDataset(String dataset) {
        this.dataset = dataset;
    }
    /**
     * Library directory.
     * 
     * @param dir
     *            library directory
     *  
     */
    public void setDir(File dir) throws BuildException {
        set.setDir(dir);
    }
    /**
     * Sets the property name for the 'if' condition.
     * 
     * The library set will be ignored unless the property is defined.
     * 
     * The value of the property is insignificant, but values that would imply
     * misinterpretation ("false", "no") will throw an exception when
     * evaluated.
     * 
     * @param propName
     *            property name
     */
    public void setIf(String propName) {
        ifCond = propName;
    }
    /**
     * Comma-separated list of library names without leading prefixes, such as
     * "lib", or extensions, such as ".so" or ".a".
     *  
     */
    public void setLibs(CUtil.StringArrayBuilder libs) throws BuildException {
        libnames = libs.getValue();
        // If this is not active.. then it's ok if the lib names are invalid.
        // so we can do a: <libset if="x.lib" dir="." libs="${x.lib}"/>
        if (!isActive(getProject()))
            return;
        for (int i = 0; i < libnames.length; i++) {
            int lastDot = libnames[i].lastIndexOf('.');
            if (lastDot >= 0) {
                String extension = libnames[i].substring(lastDot);
                if (extension.equalsIgnoreCase(".lib")
                        || extension.equalsIgnoreCase(".so")
                        || extension.equalsIgnoreCase(".a")) {
                    getProject().log(
                            "Suspicious library name ending with \""
                                    + extension + "\": " + libnames[i], Project.MSG_DEBUG );
                }
            }
            if (libnames[i].length() >= 3
                    && libnames[i].substring(0, 3).equalsIgnoreCase("lib")) {
                getProject().log(
                        "Suspicious library name starting with \"lib\": "
                                + libnames[i], Project.MSG_DEBUG);
            }
        }
    }
    public void setProject(Project project) {
        set.setProject(project);
        super.setProject(project);
    }
    /**
     * Set the property name for the 'unless' condition.
     * 
     * If named property is set, the library set will be ignored.
     * 
     * The value of the property is insignificant, but values that would imply
     * misinterpretation ("false", "no") of the behavior will throw an
     * exception when evaluated.
     * 
     * @param propName
     *            name of property
     */
    public void setUnless(String propName) {
        unlessCond = propName;
    }
    
    /**
     * Sets the preferred library type. Supported values "shared", "static", and
     * "framework".  "framework" is equivalent to "shared" on non-Darwin platforms. 
     */
    public void setType(LibraryTypeEnum type) {
    	this.libraryType = type;
    }
    
    public void visitLibraries(Project project, Linker linker, File[] libpath,
            FileVisitor visitor) throws BuildException {
        FileSet localSet = (FileSet) set.clone();
        //
        //   unless explicitly set
        //      will default to the linker case sensitivity
        //
        if (!explicitCaseSensitive) {
            boolean linkerCaseSensitive = linker.isCaseSensitive();
            localSet.setCaseSensitive(linkerCaseSensitive);
        }
        //
        //  if there was a libs attribute then
        //     add the corresponding patterns to the FileSet
        //
        if (libnames != null && libnames.length > 0) {
            String[] patterns = linker.getLibraryPatterns(libnames, libraryType);
            //
            //  if no patterns, then linker does not support libraries
            //
            if (patterns.length > 0) {
		        for (int i = 0; i < patterns.length; i++) {
		             PatternSet.NameEntry entry = localSet.createInclude();
		             entry.setName(patterns[i]);
		        }
		        //
		        //  if there was no specified directory then
		        //     run through the libpath backwards
		        //
		        if (localSet.getDir(project) == null) {
		            //
		            //  scan libpath in reverse order
		            //     to give earlier entries priority
		            //
		            for (int j = libpath.length - 1; j >= 0; j--) {
		                FileSet clone = (FileSet) localSet.clone();
		                clone.setDir(libpath[j]);
		                DirectoryScanner scanner = clone.getDirectoryScanner(project);
		                File basedir = scanner.getBasedir();
		                String[] files = scanner.getIncludedFiles();
		                for (int k = 0; k < files.length; k++) {
		                    visitor.visit(basedir, files[k]);
		                }
		            }
		        } else {
		            DirectoryScanner scanner = localSet.getDirectoryScanner(project);
		            File basedir = scanner.getBasedir();
		            String[] files = scanner.getIncludedFiles();
		            for (int k = 0; k < files.length; k++) {
		                visitor.visit(basedir, files[k]);
		            }
		        }
        	}
        }
    }
}