-
Notifications
You must be signed in to change notification settings - Fork 4.6k
/
AbstractNativeCompileTask.java
315 lines (282 loc) · 10.6 KB
/
AbstractNativeCompileTask.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
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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
/*
* Copyright 2014 the original author or authors.
*
* 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 org.gradle.language.nativeplatform.tasks;
import org.gradle.api.DefaultTask;
import org.gradle.api.Transformer;
import org.gradle.api.file.ConfigurableFileCollection;
import org.gradle.api.file.DirectoryProperty;
import org.gradle.api.file.FileCollection;
import org.gradle.api.internal.file.FileCollectionFactory;
import org.gradle.api.internal.file.TaskFileVarFactory;
import org.gradle.api.model.ObjectFactory;
import org.gradle.api.provider.ListProperty;
import org.gradle.api.provider.Property;
import org.gradle.api.tasks.IgnoreEmptyDirectories;
import org.gradle.api.tasks.Input;
import org.gradle.api.tasks.InputFiles;
import org.gradle.api.tasks.Internal;
import org.gradle.api.tasks.Nested;
import org.gradle.api.tasks.OutputDirectory;
import org.gradle.api.tasks.PathSensitive;
import org.gradle.api.tasks.PathSensitivity;
import org.gradle.api.tasks.SkipWhenEmpty;
import org.gradle.api.tasks.TaskAction;
import org.gradle.api.tasks.WorkResult;
import org.gradle.internal.Cast;
import org.gradle.internal.operations.logging.BuildOperationLogger;
import org.gradle.internal.operations.logging.BuildOperationLoggerFactory;
import org.gradle.language.base.internal.compile.Compiler;
import org.gradle.language.nativeplatform.internal.incremental.IncrementalCompilerBuilder;
import org.gradle.nativeplatform.internal.BuildOperationLoggingCompilerDecorator;
import org.gradle.nativeplatform.platform.NativePlatform;
import org.gradle.nativeplatform.platform.internal.NativePlatformInternal;
import org.gradle.nativeplatform.toolchain.Clang;
import org.gradle.nativeplatform.toolchain.Gcc;
import org.gradle.nativeplatform.toolchain.NativeToolChain;
import org.gradle.nativeplatform.toolchain.internal.NativeCompileSpec;
import org.gradle.nativeplatform.toolchain.internal.NativeToolChainInternal;
import org.gradle.nativeplatform.toolchain.internal.PlatformToolProvider;
import org.gradle.work.DisableCachingByDefault;
import org.gradle.work.Incremental;
import org.gradle.work.InputChanges;
import javax.inject.Inject;
import java.util.LinkedHashMap;
import java.util.Map;
/**
* Compiles native source files into object files.
*/
@DisableCachingByDefault(because = "Abstract super-class, not to be instantiated directly")
public abstract class AbstractNativeCompileTask extends DefaultTask {
private final Property<NativePlatform> targetPlatform;
private final Property<NativeToolChain> toolChain;
private boolean positionIndependentCode;
private boolean debug;
private boolean optimize;
private final DirectoryProperty objectFileDir;
private final ConfigurableFileCollection includes;
private final ConfigurableFileCollection systemIncludes;
private final ConfigurableFileCollection source;
private final Map<String, String> macros = new LinkedHashMap<String, String>();
private final ListProperty<String> compilerArgs;
private final IncrementalCompilerBuilder.IncrementalCompiler incrementalCompiler;
public AbstractNativeCompileTask() {
ObjectFactory objectFactory = getProject().getObjects();
this.includes = getProject().files();
this.systemIncludes = getProject().files();
dependsOn(includes);
dependsOn(systemIncludes);
this.source = getTaskFileVarFactory().newInputFileCollection(this);
this.objectFileDir = objectFactory.directoryProperty();
this.compilerArgs = getProject().getObjects().listProperty(String.class);
this.targetPlatform = objectFactory.property(NativePlatform.class);
this.toolChain = objectFactory.property(NativeToolChain.class);
this.incrementalCompiler = getIncrementalCompilerBuilder().newCompiler(this, source, includes.plus(systemIncludes), macros, toolChain.map(new Transformer<Boolean, NativeToolChain>() {
@Override
public Boolean transform(NativeToolChain nativeToolChain) {
return nativeToolChain instanceof Gcc || nativeToolChain instanceof Clang;
}
}));
}
@Inject
protected TaskFileVarFactory getTaskFileVarFactory() {
throw new UnsupportedOperationException();
}
@Inject
protected IncrementalCompilerBuilder getIncrementalCompilerBuilder() {
throw new UnsupportedOperationException();
}
@Inject
protected BuildOperationLoggerFactory getOperationLoggerFactory() {
throw new UnsupportedOperationException();
}
@Inject
protected FileCollectionFactory getFileCollectionFactory() {
throw new UnsupportedOperationException();
}
@TaskAction
protected void compile(InputChanges inputs) {
BuildOperationLogger operationLogger = getOperationLoggerFactory().newOperationLogger(getName(), getTemporaryDir());
NativeCompileSpec spec = createCompileSpec();
spec.setTargetPlatform(targetPlatform.get());
spec.setTempDir(getTemporaryDir());
spec.setObjectFileDir(objectFileDir.get().getAsFile());
spec.include(includes);
spec.systemInclude(systemIncludes);
spec.source(getSource());
spec.setMacros(getMacros());
spec.args(getCompilerArgs().get());
spec.setPositionIndependentCode(isPositionIndependentCode());
spec.setDebuggable(isDebuggable());
spec.setOptimized(isOptimized());
spec.setIncrementalCompile(inputs.isIncremental());
spec.setOperationLogger(operationLogger);
configureSpec(spec);
NativeToolChainInternal nativeToolChain = (NativeToolChainInternal) toolChain.get();
NativePlatformInternal nativePlatform = (NativePlatformInternal) targetPlatform.get();
PlatformToolProvider platformToolProvider = nativeToolChain.select(nativePlatform);
setDidWork(doCompile(spec, platformToolProvider).getDidWork());
}
protected void configureSpec(NativeCompileSpec spec) {
}
private <T extends NativeCompileSpec> WorkResult doCompile(T spec, PlatformToolProvider platformToolProvider) {
Class<T> specType = Cast.uncheckedCast(spec.getClass());
Compiler<T> baseCompiler = platformToolProvider.newCompiler(specType);
Compiler<T> incrementalCompiler = this.incrementalCompiler.createCompiler(baseCompiler);
Compiler<T> loggingCompiler = BuildOperationLoggingCompilerDecorator.wrap(incrementalCompiler);
return loggingCompiler.execute(spec);
}
protected abstract NativeCompileSpec createCompileSpec();
/**
* The tool chain used for compilation.
*
* @since 4.7
*/
@Internal
public Property<NativeToolChain> getToolChain() {
return toolChain;
}
/**
* The platform being compiled for.
*
* @since 4.7
*/
@Nested
public Property<NativePlatform> getTargetPlatform() {
return targetPlatform;
}
/**
* Should the compiler generate position independent code?
*/
@Input
public boolean isPositionIndependentCode() {
return positionIndependentCode;
}
public void setPositionIndependentCode(boolean positionIndependentCode) {
this.positionIndependentCode = positionIndependentCode;
}
/**
* Should the compiler generate debuggable code?
*
* @since 4.3
*/
@Input
public boolean isDebuggable() {
return debug;
}
/**
* Should the compiler generate debuggable code?
*
* @since 4.3
*/
public void setDebuggable(boolean debug) {
this.debug = debug;
}
/**
* Should the compiler generate optimized code?
*
* @since 4.3
*/
@Input
public boolean isOptimized() {
return optimize;
}
/**
* Should the compiler generate optimized code?
*
* @since 4.3
*/
public void setOptimized(boolean optimize) {
this.optimize = optimize;
}
/**
* The directory where object files will be generated.
*
* @since 4.3
*/
@OutputDirectory
public DirectoryProperty getObjectFileDir() {
return objectFileDir;
}
/**
* Returns the header directories to be used for compilation.
*/
@Internal("The paths for include directories are tracked via the includePaths property, the contents are tracked via discovered inputs")
public ConfigurableFileCollection getIncludes() {
return includes;
}
/**
* Add directories where the compiler should search for header files.
*/
public void includes(Object includeRoots) {
includes.from(includeRoots);
}
/**
* Returns the system include directories to be used for compilation.
*
* @since 4.8
*/
@Internal("The paths for include directories are tracked via the includePaths property, the contents are tracked via discovered inputs")
public ConfigurableFileCollection getSystemIncludes() {
return systemIncludes;
}
/**
* Returns the source files to be compiled.
*/
@InputFiles
@SkipWhenEmpty
@IgnoreEmptyDirectories
@PathSensitive(PathSensitivity.RELATIVE)
public ConfigurableFileCollection getSource() {
return source;
}
/**
* Adds a set of source files to be compiled. The provided sourceFiles object is evaluated as per {@link org.gradle.api.Project#files(Object...)}.
*/
public void source(Object sourceFiles) {
source.from(sourceFiles);
}
/**
* Macros that should be defined for the compiler.
*/
@Input
public Map<String, String> getMacros() {
return macros;
}
public void setMacros(Map<String, String> macros) {
this.macros.clear();
this.macros.putAll(macros);
}
/**
* <em>Additional</em> arguments to provide to the compiler.
*
* @since 4.3
*/
@Input
public ListProperty<String> getCompilerArgs() {
return compilerArgs;
}
/**
* The set of dependent headers. This is used for up-to-date checks only.
*
* @since 4.3
*/
@Incremental
@InputFiles
@PathSensitive(PathSensitivity.NAME_ONLY)
protected FileCollection getHeaderDependencies() {
return incrementalCompiler.getHeaderFiles();
}
}