-
Notifications
You must be signed in to change notification settings - Fork 19
/
BuildTask.java
254 lines (210 loc) · 13.6 KB
/
BuildTask.java
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
/*
* Copyright (C) 2020-2022 Samuel Audet
*
* Licensed either under the Apache License, Version 2.0, or (at your option)
* under the terms of the GNU General Public License as published by
* the Free Software Foundation (subject to the "Classpath" exception),
* either version 2, or any later version (collectively, 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
* http://www.gnu.org/licenses/
* http://www.gnu.org/software/classpath/license.html
*
* or as provided in the LICENSE.txt file that accompanied this code.
* 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 org.bytedeco.gradle.javacpp;
import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.Map;
import java.util.Properties;
import org.bytedeco.javacpp.Loader;
import org.bytedeco.javacpp.tools.Builder;
import org.bytedeco.javacpp.tools.Logger;
import org.bytedeco.javacpp.tools.ParserException;
import org.bytedeco.javacpp.tools.Slf4jLogger;
import org.gradle.api.DefaultTask;
import org.gradle.api.Task;
import org.gradle.api.plugins.ExtraPropertiesExtension;
import org.gradle.api.specs.Spec;
import org.gradle.api.tasks.Classpath;
import org.gradle.api.tasks.Input;
import org.gradle.api.tasks.InputDirectory;
import org.gradle.api.tasks.InputFile;
import org.gradle.api.tasks.InputFiles;
import org.gradle.api.tasks.Optional;
import org.gradle.api.tasks.OutputDirectory;
import org.gradle.api.tasks.TaskAction;
/**
* A Gradle task that wraps {@link Builder}.
*
* @author Samuel Audet
*/
public class BuildTask extends DefaultTask {
/** Load user classes from classPath. */
String[] classPath = null;
/** Add the path to the "platform.includepath" property. */
String[] includePath = null;
/** Add the path to the "platform.includeresource" property. */
String[] includeResource = null;
/** Add the path to the "platform.buildpath" property. */
String[] buildPath = null;
/** Add the path to the "platform.buildresource" property. */
String[] buildResource = null;
/** Add the path to the "platform.linkpath" property. */
String[] linkPath = null;
/** Add the path to the "platform.linkresource" property. */
String[] linkResource = null;
/** Add the path to the "platform.preloadpath" property. */
String[] preloadPath = null;
/** Add the path to the "platform.preloadresource" property. */
String[] preloadResource = null;
/** Add the path to the "platform.resourcepath" property. */
String[] resourcePath = null;
/** Add the path to the "platform.executablepath" property. */
String[] executablePath = null;
/** Specify the character encoding used for input and output. */
String encoding = null;
/** Output all generated files to outputDirectory. */
File outputDirectory = null;
/** Output everything in a file named after given outputName. */
String outputName = null;
/** Delete all files from {@link #outputDirectory} before generating anything in it. */
boolean clean = false;
/** Generate .cpp files from Java interfaces if found, parsing from header files if not. */
boolean generate = true;
/** Compile and delete the generated .cpp files. */
boolean compile = true;
/** Delete generated C++ JNI files after compilation */
boolean deleteJniFiles = true;
/** Generate header file with declarations of callbacks functions. */
boolean header = false;
/** Copy to output directory dependent libraries (link and preload). */
boolean copyLibs = false;
/** Copy to output directory resources listed in properties. */
boolean copyResources = false;
/** Also create config files for GraalVM native-image in directory. */
File configDirectory = null;
/** Also create a JAR file named {@code <jarPrefix>-<platform>.jar}. */
String jarPrefix = null;
/** Load all properties from resource. */
String properties = null;
/** Load all properties from file. */
File propertyFile = null;
/** Set property keys to values. */
Properties propertyKeysAndValues = null;
/** Process only these classes or packages (suffixed with .* or .**). */
String[] classOrPackageNames = null;
/** Execute a build command instead of JavaCPP itself, and return. */
String[] buildCommand = null;
/** Add to Maven project source directory of Java files generated by buildCommand. */
String[] targetDirectory = null;
/** Set the working directory of the build subprocess. */
File workingDirectory = null;
/** Add environment variables to the compiler subprocess. */
Map<String,String> environmentVariables = null;
/** Pass compilerOptions directly to compiler. */
String[] compilerOptions = null;
/** Skip the execution. */
boolean skip = false;
public BuildTask() {
// disable incremental builds until we get proper support for them
getOutputs().upToDateWhen(new Spec<Task>() { public boolean isSatisfiedBy(Task t) { return false; }});
}
@Optional @Classpath
@InputFiles public String[] getClassPath() { return classPath; } public void setClassPath (String[] s) { classPath = s; }
@Optional @Input public String[] getIncludePath() { return includePath; } public void setIncludePath (String[] s) { includePath = s; }
@Optional @Input public String[] getIncludeResource() { return includeResource; } public void setIncludeResource(String[] s) { includeResource = s; }
@Optional @Input public String[] getBuildPath() { return buildPath; } public void setBuildPath (String[] s) { buildPath = s; }
@Optional @Input public String[] getBuildResource() { return buildResource; } public void setBuildResource (String[] s) { buildResource = s; }
@Optional @Input public String[] getLinkPath() { return linkPath; } public void setLinkPath (String[] s) { linkPath = s; }
@Optional @Input public String[] getLinkResource() { return linkResource; } public void setLinkResource (String[] s) { linkResource = s; }
@Optional @Input public String[] getPreloadPath() { return preloadPath; } public void setPreloadPath (String[] s) { preloadPath = s; }
@Optional @Input public String[] getPreloadResource() { return preloadResource; } public void setPreloadResource(String[] s) { preloadResource = s; }
@Optional @Input public String[] getResourcePath() { return resourcePath; } public void setResourcePath (String[] s) { resourcePath = s; }
@Optional @Input public String[] getExecutablePath() { return executablePath; } public void setExecutablePath (String[] s) { executablePath = s; }
@Optional @Input public String getEncoding() { return encoding; } public void setEncoding (String s) { encoding = s; }
@Optional
@OutputDirectory public File getOutputDirectory() { return outputDirectory; } public void setOutputDirectory(File f) { outputDirectory = f; }
@Optional @Input public String getOutputName() { return outputName; } public void setOutputName (String s) { outputName = s; }
@Input public boolean getClean() { return clean; } public void setClean (boolean b) { clean = b; }
@Input public boolean getGenerate() { return generate; } public void setGenerate (boolean b) { generate = b; }
@Input public boolean getCompile() { return compile; } public void setCompile (boolean b) { compile = b; }
@Input public boolean getDeleteJniFiles() { return deleteJniFiles; } public void setDeleteJniFiles (boolean b) { deleteJniFiles = b; }
@Input public boolean getHeader() { return header; } public void setHeader (boolean b) { header = b; }
@Input public boolean getCopyLibs() { return copyLibs; } public void setCopyLibs (boolean b) { copyLibs = b; }
@Input public boolean getCopyResources() { return copyResources; } public void setCopyResources (boolean b) { copyResources = b; }
@Optional
@OutputDirectory public File getConfigDirectory() { return configDirectory; } public void setConfigDirectory(File f) { configDirectory = f; }
@Optional @Input public String getJarPrefix() { return jarPrefix; } public void setJarPrefix (String s) { jarPrefix = s; }
@Optional @Input public String getProperties() { return properties; } public void setProperties (String s) { properties = s; }
@Optional @InputFile public File getPropertyFile() { return propertyFile; } public void setPropertyFile (File f) { propertyFile = f; }
@Optional @Input public Properties getPropertyKeysAndValues() { return propertyKeysAndValues; } public void setPropertyKeysAndValues(Properties p) { propertyKeysAndValues = p; }
@Optional @Input public String[] getClassOrPackageNames() { return classOrPackageNames; } public void setClassOrPackageNames (String[] s) { classOrPackageNames = s; }
@Optional @Input public String[] getBuildCommand() { return buildCommand; } public void setBuildCommand (String[] s) { buildCommand = s; }
@Optional @Input public String[] getTargetDirectory() { return targetDirectory; } public void setTargetDirectory (String[] s) { targetDirectory = s; }
@Optional
@InputDirectory public File getWorkingDirectory() { return workingDirectory; } public void setWorkingDirectory (File f) { workingDirectory = f; }
@Optional @Input public Map<String, String> getEnvironmentVariables() { return environmentVariables; } public void setEnvironmentVariables (Map<String, String> m) { environmentVariables = m; }
@Optional @Input public String[] getCompilerOptions() { return compilerOptions; } public void setCompilerOptions (String[] s) { compilerOptions = s; }
@Input public boolean getSkip() { return skip; } public void setSkip (boolean b) { skip = b; }
@TaskAction public void build() throws IOException, ClassNotFoundException, NoClassDefFoundError, InterruptedException, ParserException {
Logger logger = new Slf4jLogger(Builder.class);
if (getSkip()) {
logger.info("Skipping execution of JavaCPP Builder");
return;
}
Builder builder = new Builder(logger)
.classPaths(getClassPath())
.encoding(getEncoding())
.outputDirectory(getOutputDirectory())
.outputName(getOutputName())
.clean(getClean())
.generate(getGenerate())
.compile(getCompile())
.deleteJniFiles(getDeleteJniFiles())
.header(getHeader())
.copyLibs(getCopyLibs())
.copyResources(getCopyResources())
.configDirectory(getConfigDirectory())
.jarPrefix(getJarPrefix())
.properties(getProperties())
.propertyFile(getPropertyFile())
.properties(getPropertyKeysAndValues())
.classesOrPackages(getClassOrPackageNames())
.buildCommand(getBuildCommand())
.workingDirectory(getWorkingDirectory())
.environmentVariables(getEnvironmentVariables())
.compilerOptions(getCompilerOptions())
.commandExecutor(new BuildExecutor(logger))
.addProperty("platform.buildpath", getBuildPath())
.addProperty("platform.buildresource", getBuildResource())
.addProperty("platform.includepath", getIncludePath())
.addProperty("platform.includeresource", getIncludeResource())
.addProperty("platform.linkpath", getLinkPath())
.addProperty("platform.linkresource", getLinkResource())
.addProperty("platform.preloadpath", getPreloadPath())
.addProperty("platform.preloadresource", getPreloadResource())
.addProperty("platform.resourcepath", getResourcePath())
.addProperty("platform.executablepath", getExecutablePath());
String extension = builder.getProperty("platform.extension");
getLogger().info("Detected platform \"" + Loader.Detector.getPlatform() + "\"");
getLogger().info("Building platform \"" + builder.getProperty("platform") + "\""
+ (extension != null && extension.length() > 0 ? " with extension \"" + extension + "\"" : ""));
ExtraPropertiesExtension projectProperties = getProject().getExtensions().getExtraProperties();
for (String key : builder.getProperties().stringPropertyNames()) {
projectProperties.set("javacpp." + key, builder.getProperties().getProperty(key));
}
File[] outputFiles = builder.build();
if (getLogger().isDebugEnabled()) {
getLogger().debug("outputFiles: " + Arrays.deepToString(outputFiles));
}
}
}