JOGL v2.6.0-rc-20250712
JOGL, High-Performance Graphics Binding for Java™ (public API).
StaticGLGenTask.java
Go to the documentation of this file.
1package com.jogamp.gluegen.opengl.ant;
2
3/*
4 * Copyright (C) 2003 Rob Grzywinski (rgrzywinski@realityinteractive.com)
5 * Copyright (c) 2003-2005 Sun Microsystems, Inc. All Rights Reserved.
6 * Copyright (c) 2010 JogAmp Community. All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions are
10 * met:
11 *
12 * - Redistribution of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 *
15 * - Redistribution in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 *
19 * Neither the name of Sun Microsystems, Inc. or the names of
20 * contributors may be used to endorse or promote products derived from
21 * this software without specific prior written permission.
22 *
23 * This software is provided "AS IS," without a warranty of any kind. ALL
24 * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES,
25 * INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A
26 * PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN
27 * MICROSYSTEMS, INC. ("SUN") AND ITS LICENSORS SHALL NOT BE LIABLE FOR
28 * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
29 * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL SUN OR
30 * ITS LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR FOR
31 * DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE
32 * DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY,
33 * ARISING OUT OF THE USE OF OR INABILITY TO USE THIS SOFTWARE, EVEN IF
34 * SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
35 *
36 * You acknowledge that this software is not designed or intended for use
37 * in the design, construction, operation or maintenance of any nuclear
38 * facility.
39 */
40
41import java.io.IOException;
42
43import org.apache.tools.ant.BuildException;
44import org.apache.tools.ant.DirectoryScanner;
45import org.apache.tools.ant.Project;
46import org.apache.tools.ant.Task;
47import org.apache.tools.ant.taskdefs.Execute;
48import org.apache.tools.ant.taskdefs.LogStreamHandler;
49import org.apache.tools.ant.types.CommandlineJava;
50import org.apache.tools.ant.types.FileSet;
51import org.apache.tools.ant.types.Path;
52import org.apache.tools.ant.types.PatternSet;
53import org.apache.tools.ant.util.JavaEnvUtils;
54
55/**
56 * <p>An <a href="http://ant.apache.org">ANT</a> {@link org.apache.tools.ant.Task}
57 * for using {@link com.jogamp.gluegen.opengl.BuildStaticGLInfo}.</p>
58 *
59 * <p>Usage:</p>
60 * <pre>
61 &lt;staticglgen package="[generated files package]"
62 headers="[file pattern of GL headers]"
63 outputdir="[directory to output the generated files]" /&gt;
64 * </pre>
65 *
66 * @author Rob Grzywinski <a href="mailto:rgrzywinski@realityinteractive.com">rgrzywinski@yahoo.com</a>
67 */
68// FIXME: blow out javadoc
69public class StaticGLGenTask extends Task
70{
71 /**
72 * <p>The {@link com.jogamp.gluegen.opengl.BuildStaticGLInfo} classname.</p>
73 */
74 private static final String GL_GEN = "com.jogamp.gluegen.opengl.BuildStaticGLInfo";
75
76 // =========================================================================
77 /**
78 * <p>The {@link org.apache.tools.ant.types.CommandlineJava} that is used
79 * to execute {@link com.jogamp.gluegen.opengl.BuildStaticGLInfo}.</p>
80 */
81 private final CommandlineJava glgenCommandline;
82
83 // =========================================================================
84 /**
85 * <p>The package name for the generated files.</p>
86 */
87 private String packageName;
88
89 /**
90 * <p>The output directory.</p>
91 */
92 private String outputDirectory;
93
94 /**
95 * <p>The {@link org.apache.tools.ant.types.FileSet} of GL headers.</p>
96 */
97 private final FileSet headerSet = new FileSet();
98
99 // =========================================================================
100 /**
101 * <p>Create and add the VM and classname to {@link org.apache.tools.ant.types.CommandlineJava}.</p>
102 */
104 {
105 // create the CommandlineJava that will be used to call BuildStaticGLInfo
106 glgenCommandline = new CommandlineJava();
107
108 // set the VM and classname in the commandline
109 glgenCommandline.setVm(JavaEnvUtils.getJreExecutable("java"));
110 glgenCommandline.setClassname(GL_GEN);
111 }
112
113 // =========================================================================
114 // ANT getters and setters
115 /**
116 * <p>Set the package name for the generated files. This is called by ANT.</p>
117 *
118 * @param packageName the name of the package for the generated files
119 */
120 public void setPackage(final String packageName)
121 {
122 log( ("Setting package name to: " + packageName), Project.MSG_VERBOSE);
123 this.packageName = packageName;
124 }
125
126 /**
127 * <p>Set the output directory. This is called by ANT.</p>
128 *
129 * @param directory the output directory
130 */
131 public void setOutputDir(final String directory)
132 {
133 log( ("Setting output directory to: " + directory),
134 Project.MSG_VERBOSE);
135 this.outputDirectory = directory;
136 }
137
138 /**
139 * <p>Add a header file to the list. This is called by ANT for a nested
140 * element.</p>
141 *
142 * @return {@link org.apache.tools.ant.types.PatternSet.NameEntry}
143 */
144 public PatternSet.NameEntry createHeader()
145 {
146 return headerSet.createInclude();
147 }
148
149 /**
150 * <p>Add a header file to the list. This is called by ANT for a nested
151 * element.</p>
152 *
153 * @return {@link org.apache.tools.ant.types.PatternSet.NameEntry}
154 */
155 public PatternSet.NameEntry createHeadersFile()
156 {
157 return headerSet.createIncludesFile();
158 }
159
160 /**
161 * <p>Set the set of header patterns. Patterns may be separated by a comma
162 * or a space. This is called by ANT.</p>
163 *
164 * @param headers the string containing the header patterns
165 */
166 public void setHeaders(final String headers)
167 {
168 headerSet.setIncludes(headers);
169 }
170
171 /**
172 * <p>Add an optional classpath that defines the location of {@link com.jogamp.gluegen.opengl.BuildStaticGLInfo}
173 * and <code>BuildStaticGLInfo</code>'s dependencies.</p>
174 *
175 * @returns {@link org.apache.tools.ant.types.Path}
176 */
177 public Path createClasspath()
178 {
179 return glgenCommandline.createClasspath(project).createPath();
180 }
181
182 // =========================================================================
183 /**
184 * <p>Run the task. This involves validating the set attributes, creating
185 * the command line to be executed and finally executing the command.</p>
186 *
187 * @see org.apache.tools.ant.Task#execute()
188 */
189 @Override
190 public void execute()
191 throws BuildException
192 {
193 // validate that all of the required attributes have been set
194 validateAttributes();
195
196 // TODO: add logic to determine if the generated file needs to be
197 // regenerated
198
199 // add the attributes to the CommandlineJava
200 addAttributes();
201
202 log(glgenCommandline.describeCommand(), Project.MSG_VERBOSE);
203
204 // execute the command and throw on error
205 final int error = execute(glgenCommandline.getCommandline());
206 if(error == 1)
207 throw new BuildException( ("BuildStaticGLInfo returned: " + error), location);
208 }
209
210 /**
211 * <p>Ensure that the user specified all required arguments.</p>
212 *
213 * @throws BuildException if there are required arguments that are not
214 * present or not valid
215 */
216 private void validateAttributes()
217 throws BuildException
218 {
219 // validate that the package name is set
220 if(!isValid(packageName))
221 throw new BuildException("Invalid package name: " + packageName);
222
223 // validate that the output directory is set
224 // TODO: switch to file and ensure that it exists
225 if(!isValid(outputDirectory))
226 throw new BuildException("Invalid output directory name: " + outputDirectory);
227
228 // TODO: validate that there are headers set
229 }
230
231 /**
232 * <p>Is the specified string valid? A valid string is non-<code>null</code>
233 * and has a non-zero length.</p>
234 *
235 * @param string the string to be tested for validity
236 * @return <code>true</code> if the string is valid. <code>false</code>
237 * otherwise.
238 */
239 private boolean isValid(final String string)
240 {
241 // check for null
242 if(string == null)
243 return false;
244
245 // ensure that the string has a non-zero length
246 // NOTE: must trim() to remove leading and trailing whitespace
247 if(string.trim().length() < 1)
248 return false;
249
250 // the string is valid
251 return true;
252 }
253
254 /**
255 * <p>Add all of the attributes to the command line. They have already
256 * been validated.</p>
257 */
258 private void addAttributes()
259 {
260 // add the package name
261 glgenCommandline.createArgument().setValue(packageName);
262
263 // add the output directory name
264 glgenCommandline.createArgument().setValue(outputDirectory);
265
266 // add the header -files- from the FileSet
267 headerSet.setDir(getProject().getBaseDir());
268 final DirectoryScanner directoryScanner = headerSet.getDirectoryScanner(getProject());
269 final String[] directoryFiles = directoryScanner.getIncludedFiles();
270 for(int i=0; i<directoryFiles.length; i++)
271 {
272 glgenCommandline.createArgument().setValue(directoryFiles[i]);
273 }
274 }
275
276 /**
277 * <p>Execute {@link com.jogamp.gluegen.opengl.BuildStaticGLInfo} in a
278 * forked JVM.</p>
279 *
280 * @throws BuildException
281 */
282 private int execute(final String[] command)
283 throws BuildException
284 {
285 // create the object that will perform the command execution
286 final Execute execute = new Execute(new LogStreamHandler(this, Project.MSG_INFO,
287 Project.MSG_WARN),
288 null);
289
290 // set the project and command line
291 execute.setAntRun(project);
292 execute.setCommandline(command);
293 execute.setWorkingDirectory( project.getBaseDir() );
294
295 // execute the command
296 try
297 {
298 return execute.execute();
299 } catch(final IOException ioe)
300 {
301 throw new BuildException(ioe, location);
302 }
303 }
304}