| 1 |
/* |
| 2 |
* Copyright (C) 2014 The Android Open Source Project |
| 3 |
* |
| 4 |
* Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 |
* you may not use this file except in compliance with the License. |
| 6 |
* You may obtain a copy of the License at |
| 7 |
* |
| 8 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 9 |
* |
| 10 |
* Unless required by applicable law or agreed to in writing, software |
| 11 |
* distributed under the License is distributed on an "AS IS" BASIS, |
| 12 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 |
* See the License for the specific language governing permissions and |
| 14 |
* limitations under the License. |
| 15 |
*/ |
| 16 |
|
| 17 |
package com.android.build.gradle.model; |
| 18 |
|
| 19 |
import static com.android.build.gradle.model.ModelConstants.ARTIFACTS; |
| 20 |
import static com.android.build.gradle.model.ModelConstants.EXTERNAL_BUILD_CONFIG; |
| 21 |
import static com.android.build.gradle.model.ModelConstants.NATIVE_BUILD_SYSTEMS; |
| 22 |
import static com.android.build.gradle.model.ModelConstants.NATIVE_DEPENDENCIES; |
| 23 |
|
| 24 |
import com.android.annotations.NonNull; |
| 25 |
import com.android.build.gradle.AndroidGradleOptions; |
| 26 |
import com.android.build.gradle.internal.NativeDependencyLinkage; |
| 27 |
import com.android.build.gradle.internal.ProductFlavorCombo; |
| 28 |
import com.android.build.gradle.internal.core.Abi; |
| 29 |
import com.android.build.gradle.internal.dependency.ArtifactContainer; |
| 30 |
import com.android.build.gradle.internal.dependency.NativeDependencyResolveResult; |
| 31 |
import com.android.build.gradle.internal.dependency.NativeLibraryArtifact; |
| 32 |
import com.android.build.gradle.internal.ndk.NdkHandler; |
| 33 |
import com.android.build.gradle.internal.ndk.Stl; |
| 34 |
import com.android.build.gradle.internal.ndk.StlNativeToolSpecification; |
| 35 |
import com.android.build.gradle.internal.scope.VariantScope; |
| 36 |
import com.android.build.gradle.managed.BuildType; |
| 37 |
import com.android.build.gradle.managed.NativeBuildConfig; |
| 38 |
import com.android.build.gradle.managed.NativeLibrary; |
| 39 |
import com.android.build.gradle.managed.NdkAbiOptions; |
| 40 |
import com.android.build.gradle.managed.NdkConfig; |
| 41 |
import com.android.build.gradle.managed.ProductFlavor; |
| 42 |
import com.android.build.gradle.model.internal.AndroidBinaryInternal; |
| 43 |
import com.android.build.gradle.model.internal.DefaultNativeSourceSet; |
| 44 |
import com.android.build.gradle.ndk.internal.NdkConfiguration; |
| 45 |
import com.android.build.gradle.ndk.internal.NdkExtensionConvention; |
| 46 |
import com.android.build.gradle.ndk.internal.NdkNamingScheme; |
| 47 |
import com.android.build.gradle.ndk.internal.ToolchainConfiguration; |
| 48 |
import com.android.build.gradle.tasks.NativeBuildSystem; |
| 49 |
import com.android.builder.core.BuilderConstants; |
| 50 |
import com.android.builder.core.VariantConfiguration; |
| 51 |
import com.android.utils.ImmutableCollectors; |
| 52 |
import com.android.utils.NativeSourceFileExtensions; |
| 53 |
import com.android.utils.NdkUtils; |
| 54 |
import com.android.utils.StringHelper; |
| 55 |
import com.google.common.base.Joiner; |
| 56 |
import com.google.common.base.Preconditions; |
| 57 |
import com.google.common.base.Strings; |
| 58 |
import com.google.common.collect.ArrayListMultimap; |
| 59 |
import com.google.common.collect.ImmutableSet; |
| 60 |
import com.google.common.collect.Iterables; |
| 61 |
import com.google.common.collect.Lists; |
| 62 |
import com.google.common.collect.Multimap; |
| 63 |
import com.google.common.collect.Sets; |
| 64 |
|
| 65 |
import org.gradle.api.Action; |
| 66 |
import org.gradle.api.BuildableComponentSpec; |
| 67 |
import org.gradle.api.InvalidUserDataException; |
| 68 |
import org.gradle.api.Plugin; |
| 69 |
import org.gradle.api.Project; |
| 70 |
import org.gradle.api.Task; |
| 71 |
import org.gradle.api.internal.project.ProjectIdentifier; |
| 72 |
import org.gradle.api.tasks.TaskContainer; |
| 73 |
import org.gradle.api.tasks.util.PatternFilterable; |
| 74 |
import org.gradle.internal.service.ServiceRegistry; |
| 75 |
import org.gradle.language.base.FunctionalSourceSet; |
| 76 |
import org.gradle.language.base.LanguageSourceSet; |
| 77 |
import org.gradle.language.c.plugins.CPlugin; |
| 78 |
import org.gradle.language.cpp.plugins.CppPlugin; |
| 79 |
import org.gradle.language.nativeplatform.HeaderExportingSourceSet; |
| 80 |
import org.gradle.model.Defaults; |
| 81 |
import org.gradle.model.Finalize; |
| 82 |
import org.gradle.model.Model; |
| 83 |
import org.gradle.model.ModelMap; |
| 84 |
import org.gradle.model.Mutate; |
| 85 |
import org.gradle.model.Path; |
| 86 |
import org.gradle.model.RuleSource; |
| 87 |
import org.gradle.model.Validate; |
| 88 |
import org.gradle.model.internal.core.ModelPath; |
| 89 |
import org.gradle.model.internal.core.ModelReference; |
| 90 |
import org.gradle.model.internal.core.ModelRegistrations; |
| 91 |
import org.gradle.model.internal.registry.ModelRegistry; |
| 92 |
import org.gradle.model.internal.type.ModelType; |
| 93 |
import org.gradle.model.internal.type.ModelTypes; |
| 94 |
import org.gradle.nativeplatform.BuildTypeContainer; |
| 95 |
import org.gradle.nativeplatform.FlavorContainer; |
| 96 |
import org.gradle.nativeplatform.NativeBinarySpec; |
| 97 |
import org.gradle.nativeplatform.NativeDependencySet; |
| 98 |
import org.gradle.nativeplatform.NativeLibraryBinary; |
| 99 |
import org.gradle.nativeplatform.NativeLibraryBinarySpec; |
| 100 |
import org.gradle.nativeplatform.NativeLibrarySpec; |
| 101 |
import org.gradle.nativeplatform.SharedLibraryBinary; |
| 102 |
import org.gradle.nativeplatform.SharedLibraryBinarySpec; |
| 103 |
import org.gradle.nativeplatform.StaticLibraryBinarySpec; |
| 104 |
import org.gradle.nativeplatform.toolchain.NativeToolChainRegistry; |
| 105 |
import org.gradle.platform.base.BinaryContainer; |
| 106 |
import org.gradle.platform.base.BinarySpec; |
| 107 |
import org.gradle.platform.base.ComponentType; |
| 108 |
import org.gradle.platform.base.PlatformContainer; |
| 109 |
import org.gradle.platform.base.TypeBuilder; |
| 110 |
import org.gradle.tooling.provider.model.ToolingModelBuilderRegistry; |
| 111 |
|
| 112 |
import java.io.File; |
| 113 |
import java.util.Arrays; |
| 114 |
import java.util.Collection; |
| 115 |
import java.util.Collections; |
| 116 |
import java.util.List; |
| 117 |
import java.util.Set; |
| 118 |
import java.util.stream.Collectors; |
| 119 |
|
| 120 |
import javax.inject.Inject; |
| 121 |
|
| 122 |
/** |
| 123 |
* Plugin for Android NDK applications. |
| 124 |
*/ |
| 125 |
public class NdkComponentModelPlugin implements Plugin<Project> { |
| 126 |
|
| 127 |
@NonNull |
| 128 |
private final ToolingModelBuilderRegistry toolingRegistry; |
| 129 |
@NonNull |
| 130 |
private final ModelRegistry modelRegistry; |
| 131 |
|
| 132 |
@Inject |
| 133 |
private NdkComponentModelPlugin( |
| 134 |
@NonNull ToolingModelBuilderRegistry toolingRegistry, |
| 135 |
@NonNull ModelRegistry modelRegistry) { |
| 136 |
this.toolingRegistry = toolingRegistry; |
| 137 |
this.modelRegistry = modelRegistry; |
| 138 |
} |
| 139 |
|
| 140 |
@Override |
| 141 |
public void apply(Project project) { |
| 142 |
project.getPluginManager().apply(AndroidComponentModelPlugin.class); |
| 143 |
project.getPluginManager().apply(CPlugin.class); |
| 144 |
project.getPluginManager().apply(CppPlugin.class); |
| 145 |
|
| 146 |
// Remove this when our models no longer depends on Project. |
| 147 |
modelRegistry.register(ModelRegistrations |
| 148 |
.bridgedInstance(ModelReference.of("projectModel", Project.class), project) |
| 149 |
.descriptor("Model of project.").build()); |
| 150 |
|
| 151 |
toolingRegistry.register(new NativeComponentModelBuilder(modelRegistry)); |
| 152 |
} |
| 153 |
|
| 154 |
public static class Rules extends RuleSource { |
| 155 |
|
| 156 |
@ComponentType |
| 157 |
public static void registerNativeSourceSet(TypeBuilder<NativeSourceSet> builder) { |
| 158 |
//builder.setLanguageName("jni"); |
| 159 |
builder.defaultImplementation(DefaultNativeSourceSet.class); |
| 160 |
} |
| 161 |
|
| 162 |
|
| 163 |
@Defaults |
| 164 |
public static void initializeNdkConfig(@Path("android.ndk") NdkConfig ndk) { |
| 165 |
ndk.setModuleName(""); |
| 166 |
ndk.setToolchain(""); |
| 167 |
ndk.setToolchainVersion(""); |
| 168 |
ndk.setStl(""); |
| 169 |
ndk.setRenderscriptNdkMode(false); |
| 170 |
} |
| 171 |
|
| 172 |
@Finalize |
| 173 |
public static void setDefaultNdkExtensionValue(@Path("android.ndk") NdkConfig ndkConfig) { |
| 174 |
NdkExtensionConvention.setExtensionDefault(ndkConfig); |
| 175 |
} |
| 176 |
|
| 177 |
@Validate |
| 178 |
public static void checkNdkDir( |
| 179 |
NdkHandler ndkHandler, |
| 180 |
@Path("android.ndk") NdkConfig ndkConfig) { |
| 181 |
if (!ndkConfig.getModuleName().isEmpty() && !ndkHandler.isConfigured()) { |
| 182 |
throw new InvalidUserDataException( |
| 183 |
"NDK location not found. Define location with ndk.dir in the " |
| 184 |
+ "local.properties file or with an ANDROID_NDK_HOME environment " |
| 185 |
+ "variable."); |
| 186 |
} |
| 187 |
if (ndkHandler.isConfigured()) { |
| 188 |
//noinspection ConstantConditions - isConfigured ensures getNdkDirectory is not null |
| 189 |
if (!ndkHandler.getNdkDirectory().exists()) { |
| 190 |
throw new InvalidUserDataException( |
| 191 |
"Specified NDK location does not exists. Please ensure ndk.dir in " |
| 192 |
+ "local.properties file or ANDROID_NDK_HOME is configured " |
| 193 |
+ "correctly."); |
| 194 |
|
| 195 |
} |
| 196 |
} |
| 197 |
} |
| 198 |
|
| 199 |
@Defaults |
| 200 |
public static void addDefaultNativeSourceSet( |
| 201 |
@Path("android.sources") ModelMap<FunctionalSourceSet> sources) { |
| 202 |
sources.beforeEach(sourceSet -> |
| 203 |
sourceSet.create("jni", NativeSourceSet.class, nativeSourceSet -> { |
| 204 |
nativeSourceSet.getSource().srcDir( |
| 205 |
"src/" + sourceSet.getName() + "/" + "jni"); |
| 206 |
nativeSourceSet.getExportedHeaders().srcDir( |
| 207 |
"src/" + sourceSet.getName() + "/" + "headers"); |
| 208 |
addInclude(nativeSourceSet.getcFilter(), NativeSourceFileExtensions.C_FILE_EXTENSIONS); |
| 209 |
addInclude(nativeSourceSet.getCppFilter(), NativeSourceFileExtensions.CPP_FILE_EXTENSIONS); |
| 210 |
})); |
| 211 |
} |
| 212 |
|
| 213 |
/** |
| 214 |
* Add include to PatternFilterable for a list of file extensions. |
| 215 |
*/ |
| 216 |
private static void addInclude( |
| 217 |
@NonNull PatternFilterable filter, |
| 218 |
@NonNull Iterable<String> fileExtensions) { |
| 219 |
for (String ext : fileExtensions) { |
| 220 |
filter.include("**/*." + ext); |
| 221 |
} |
| 222 |
} |
| 223 |
|
| 224 |
@Model(ModelConstants.NDK_HANDLER) |
| 225 |
public static NdkHandler ndkHandler( |
| 226 |
ProjectIdentifier projectId, |
| 227 |
@Path("android.compileSdkVersion") String compileSdkVersion, |
| 228 |
@Path("android.ndk") NdkConfig ndkConfig) { |
| 229 |
while (projectId.getParentIdentifier() != null) { |
| 230 |
projectId = projectId.getParentIdentifier(); |
| 231 |
} |
| 232 |
|
| 233 |
NdkHandler ndkHandler = new NdkHandler( |
| 234 |
projectId.getProjectDir(), |
| 235 |
ndkConfig.getPlatformVersion(), |
| 236 |
ndkConfig.getToolchain(), |
| 237 |
ndkConfig.getToolchainVersion()); |
| 238 |
ndkHandler.setCompileSdkVersion(compileSdkVersion); |
| 239 |
return ndkHandler; |
| 240 |
} |
| 241 |
|
| 242 |
@Defaults |
| 243 |
public static void initBuildTypeNdk( |
| 244 |
@Path("android.buildTypes") ModelMap<BuildType> buildTypes) { |
| 245 |
buildTypes.named( |
| 246 |
BuilderConstants.DEBUG, |
| 247 |
buildType -> { |
| 248 |
if (buildType.getNdk().getDebuggable() == null) { |
| 249 |
buildType.getNdk().setDebuggable(true); |
| 250 |
} |
| 251 |
}); |
| 252 |
} |
| 253 |
|
| 254 |
@Mutate |
| 255 |
public static void createAndroidPlatforms(PlatformContainer platforms, |
| 256 |
NdkHandler ndkHandler) { |
| 257 |
if (!ndkHandler.isConfigured()) { |
| 258 |
return; |
| 259 |
} |
| 260 |
// Create android platforms. |
| 261 |
ToolchainConfiguration.configurePlatforms(platforms, ndkHandler); |
| 262 |
} |
| 263 |
|
| 264 |
@Validate |
| 265 |
public static void validateAbi(@Path("android.abis") ModelMap<NdkAbiOptions> abiConfigs) { |
| 266 |
for (NdkAbiOptions abiOptions : abiConfigs.values()) { |
| 267 |
if (Abi.getByName(abiOptions.getName()) == null) { |
| 268 |
throw new InvalidUserDataException("Target ABI '" + abiOptions.getName() |
| 269 |
+ "' is not supported."); |
| 270 |
} |
| 271 |
} |
| 272 |
} |
| 273 |
|
| 274 |
@Mutate |
| 275 |
public static void createToolchains( |
| 276 |
NativeToolChainRegistry toolchainRegistry, |
| 277 |
@Path("android.abis") ModelMap<NdkAbiOptions> abis, |
| 278 |
@Path("android.ndk") NdkConfig ndkConfig, |
| 279 |
NdkHandler ndkHandler) { |
| 280 |
if (!ndkHandler.isConfigured()) { |
| 281 |
return; |
| 282 |
} |
| 283 |
// Create toolchain for each ABI. |
| 284 |
ToolchainConfiguration.configureToolchain( |
| 285 |
toolchainRegistry, |
| 286 |
ndkConfig.getToolchain(), |
| 287 |
abis, |
| 288 |
ndkHandler); |
| 289 |
} |
| 290 |
|
| 291 |
@Mutate |
| 292 |
public static void createNativeBuildTypes(BuildTypeContainer nativeBuildTypes, |
| 293 |
@Path("android.buildTypes") ModelMap<BuildType> androidBuildTypes) { |
| 294 |
for (BuildType buildType : androidBuildTypes.values()) { |
| 295 |
nativeBuildTypes.maybeCreate(buildType.getName()); |
| 296 |
} |
| 297 |
} |
| 298 |
|
| 299 |
@Mutate |
| 300 |
public static void createNativeFlavors( |
| 301 |
FlavorContainer nativeFlavors, |
| 302 |
List<ProductFlavorCombo<ProductFlavor>> androidFlavorGroups) { |
| 303 |
if (androidFlavorGroups.isEmpty()) { |
| 304 |
// Create empty native flavor to override Gradle's default name. |
| 305 |
nativeFlavors.maybeCreate(""); |
| 306 |
} else { |
| 307 |
for (ProductFlavorCombo group : androidFlavorGroups) { |
| 308 |
nativeFlavors.maybeCreate(group.getName()); |
| 309 |
} |
| 310 |
} |
| 311 |
} |
| 312 |
|
| 313 |
@Mutate |
| 314 |
public static void createNativeLibrary( |
| 315 |
final ModelMap<NativeLibrarySpec> nativeComponents, |
| 316 |
@Path("android.ndk") final NdkConfig ndkConfig, |
| 317 |
final NdkHandler ndkHandler, |
| 318 |
@Path("android.sources") final ModelMap<FunctionalSourceSet> sources, |
| 319 |
@Path("buildDir") final File buildDir, |
| 320 |
final ServiceRegistry serviceRegistry) { |
| 321 |
if (!ndkHandler.isConfigured()) { |
| 322 |
return; |
| 323 |
} |
| 324 |
if (!ndkConfig.getModuleName().isEmpty()) { |
| 325 |
nativeComponents.create( |
| 326 |
ndkConfig.getModuleName(), |
| 327 |
nativeLib -> { |
| 328 |
NdkConfiguration.configureProperties( |
| 329 |
nativeLib, |
| 330 |
sources, |
| 331 |
buildDir, |
| 332 |
ndkHandler, |
| 333 |
serviceRegistry); |
| 334 |
nativeLib.getBinaries().all(binarySpec -> NdkConfiguration.configureNativeBinaryOutputFile( |
| 335 |
(NativeLibraryBinarySpec)binarySpec, |
| 336 |
buildDir, |
| 337 |
ndkConfig.getModuleName())); |
| 338 |
}); |
| 339 |
} |
| 340 |
} |
| 341 |
|
| 342 |
/** |
| 343 |
* Find the native dependency for each native binaries. |
| 344 |
* |
| 345 |
* TODO: Remove duplication of functionality with NdkConfiguration.configureProperties. |
| 346 |
* We need to predict the NativeBinarySpec that will be produce instead of creating this map |
| 347 |
* after the binaries are created. |
| 348 |
*/ |
| 349 |
@Model(NATIVE_DEPENDENCIES) |
| 350 |
public static Multimap<String, NativeDependencyResolveResult> resolveNativeDependencies( |
| 351 |
@Path("binaries") ModelMap<NativeBinarySpec> nativeBinaries, |
| 352 |
@Path("android.sources") final ModelMap<FunctionalSourceSet> sources, |
| 353 |
final ServiceRegistry serviceRegistry) { |
| 354 |
Multimap<String, NativeDependencyResolveResult> dependencies = |
| 355 |
ArrayListMultimap.create(); |
| 356 |
for (NativeBinarySpec nativeBinary : nativeBinaries.values()) { |
| 357 |
Collection<NativeSourceSet> jniSources = |
| 358 |
NdkConfiguration.findNativeSourceSets(nativeBinary, sources).values(); |
| 359 |
|
| 360 |
for (NativeSourceSet jniSource : jniSources) { |
| 361 |
dependencies.put( |
| 362 |
nativeBinary.getName(), |
| 363 |
NdkConfiguration.resolveDependency( |
| 364 |
serviceRegistry, |
| 365 |
nativeBinary, |
| 366 |
jniSource)); |
| 367 |
} |
| 368 |
} |
| 369 |
return dependencies; |
| 370 |
} |
| 371 |
|
| 372 |
@Mutate |
| 373 |
public static void createAdditionalTasksForNatives( |
| 374 |
final ModelMap<Task> tasks, |
| 375 |
ModelMap<AndroidComponentSpec> specs, |
| 376 |
@Path("android.ndk") final NdkConfig ndkConfig, |
| 377 |
@Path(NATIVE_DEPENDENCIES) final Multimap<String, NativeDependencyResolveResult> dependencies, |
| 378 |
final NdkHandler ndkHandler, |
| 379 |
@Path("binaries") ModelMap<AndroidBinaryInternal> binaries, |
| 380 |
@Path("buildDir") final File buildDir) { |
| 381 |
if (!ndkHandler.isConfigured()) { |
| 382 |
return; |
| 383 |
} |
| 384 |
for (AndroidBinaryInternal binary : binaries.values()) { |
| 385 |
for (NativeBinarySpec nativeBinary : binary.getNativeBinaries()) { |
| 386 |
NdkConfiguration.createTasks( |
| 387 |
tasks, |
| 388 |
nativeBinary, |
| 389 |
buildDir, |
| 390 |
binary.getMergedNdkConfig(), |
| 391 |
ndkHandler, |
| 392 |
dependencies); |
| 393 |
} |
| 394 |
} |
| 395 |
} |
| 396 |
|
| 397 |
@Mutate |
| 398 |
public static void configureNativeBinary( |
| 399 |
BinaryContainer binaries, |
| 400 |
ModelMap<NativeLibrarySpec> specs, |
| 401 |
@Path("android.ndk") final NdkConfig ndkConfig, |
| 402 |
final NdkHandler ndkHandler) { |
| 403 |
final NativeLibrarySpec library = specs.get(ndkConfig.getModuleName()); |
| 404 |
|
| 405 |
final ImmutableSet.Builder<String> builder = ImmutableSet.builder(); |
| 406 |
for(Abi abi : NdkHandler.getAbiList()) { |
| 407 |
builder.add(abi.getName()); |
| 408 |
} |
| 409 |
final ImmutableSet<String> supportedAbis = builder.build(); |
| 410 |
|
| 411 |
binaries.withType( |
| 412 |
AndroidBinaryInternal.class, |
| 413 |
binary -> { |
| 414 |
binary.computeMergedNdk( |
| 415 |
ndkConfig, |
| 416 |
binary.getProductFlavors(), |
| 417 |
binary.getBuildType()); |
| 418 |
if (binary.getMergedNdkConfig().getAbiFilters().isEmpty()) { |
| 419 |
binary.getMergedNdkConfig().getAbiFilters().addAll(supportedAbis); |
| 420 |
} |
| 421 |
|
| 422 |
if (library != null) { |
| 423 |
Collection<NativeLibraryBinarySpec> nativeBinaries = |
| 424 |
getNativeBinaries( |
| 425 |
library, |
| 426 |
binary.getBuildType(), |
| 427 |
binary.getProductFlavors()); |
| 428 |
for (NativeLibraryBinarySpec nativeBin : nativeBinaries) { |
| 429 |
if (binary.getMergedNdkConfig().getAbiFilters().contains( |
| 430 |
nativeBin.getTargetPlatform().getName())) { |
| 431 |
NdkConfiguration.configureBinary( |
| 432 |
nativeBin, |
| 433 |
binary.getMergedNdkConfig(), |
| 434 |
ndkHandler); |
| 435 |
binary.getNativeBinaries().add(nativeBin); |
| 436 |
} |
| 437 |
} |
| 438 |
} |
| 439 |
}); |
| 440 |
} |
| 441 |
|
| 442 |
@Model(NATIVE_BUILD_SYSTEMS) |
| 443 |
public static List<String> createBuildSystemList( |
| 444 |
final ModelMap<NativeLibrarySpec> nativeComponents, |
| 445 |
@Path("android.ndk") final NdkConfig ndkConfig) { |
| 446 |
List<String> buildSystemsUsed = Lists.newArrayList(); |
| 447 |
String moduleName = ndkConfig.getModuleName(); |
| 448 |
if (!Strings.isNullOrEmpty(moduleName) && nativeComponents.containsKey(moduleName)) { |
| 449 |
buildSystemsUsed.add(NativeBuildSystem.GRADLE.getName()); |
| 450 |
} |
| 451 |
return buildSystemsUsed; |
| 452 |
} |
| 453 |
|
| 454 |
/** |
| 455 |
* Create external build config to allow NativeComponentModelBuilder to create native model. |
| 456 |
*/ |
| 457 |
|
| 458 |
@Model(EXTERNAL_BUILD_CONFIG) |
| 459 |
public static void createNativeBuildModel( |
| 460 |
NativeBuildConfig config, |
| 461 |
@Path("binaries") ModelMap<AndroidBinaryInternal> binaries, |
| 462 |
@Path("android.abis") ModelMap<NdkAbiOptions> abiConfigs, |
| 463 |
final NdkHandler ndkHandler) { |
| 464 |
boolean atLeastOneNativeBinarySpec = false; |
| 465 |
for (final AndroidBinaryInternal binary : binaries.values()) { |
| 466 |
for (final NativeLibraryBinarySpec nativeBinary : binary.getNativeBinaries()) { |
| 467 |
if (!(nativeBinary instanceof SharedLibraryBinarySpec)) { |
| 468 |
continue; |
| 469 |
} |
| 470 |
atLeastOneNativeBinarySpec = true; |
| 471 |
final String abi = nativeBinary.getTargetPlatform().getName(); |
| 472 |
config.getLibraries().create( |
| 473 |
binary.getName() + '-' + abi, |
| 474 |
new CreateNativeLibraryAction( |
| 475 |
binary, |
| 476 |
(SharedLibraryBinarySpec) nativeBinary, |
| 477 |
abiConfigs, |
| 478 |
ndkHandler)); |
| 479 |
} |
| 480 |
} |
| 481 |
|
| 482 |
if (atLeastOneNativeBinarySpec) { |
| 483 |
config.getcFileExtensions().addAll(NativeSourceFileExtensions.C_FILE_EXTENSIONS); |
| 484 |
config.getCppFileExtensions().addAll( |
| 485 |
NativeSourceFileExtensions.CPP_FILE_EXTENSIONS); |
| 486 |
|
| 487 |
for (final Abi abi : ndkHandler.getSupportedAbis()) { |
| 488 |
config.getToolchains().create("ndk-" + abi.getName(), |
| 489 |
nativeToolchain -> { |
| 490 |
nativeToolchain.setCCompilerExecutable( |
| 491 |
ndkHandler.getCCompiler(abi)); |
| 492 |
nativeToolchain.setCppCompilerExecutable( |
| 493 |
ndkHandler.getCppCompiler(abi)); |
| 494 |
}); |
| 495 |
} |
| 496 |
} |
| 497 |
} |
| 498 |
private static class CreateNativeLibraryAction implements Action<NativeLibrary> { |
| 499 |
@NonNull |
| 500 |
private final AndroidBinaryInternal binary; |
| 501 |
@NonNull |
| 502 |
private final SharedLibraryBinarySpec nativeBinary; |
| 503 |
@NonNull |
| 504 |
private final ModelMap<NdkAbiOptions> abiConfigs; |
| 505 |
@NonNull |
| 506 |
private final NdkHandler ndkHandler; |
| 507 |
|
| 508 |
public CreateNativeLibraryAction( |
| 509 |
@NonNull AndroidBinaryInternal binary, |
| 510 |
@NonNull SharedLibraryBinarySpec nativeBinary, |
| 511 |
@NonNull ModelMap<NdkAbiOptions> abiConfigs, |
| 512 |
@NonNull NdkHandler ndkHandler) { |
| 513 |
this.binary = binary; |
| 514 |
this.nativeBinary = nativeBinary; |
| 515 |
this.abiConfigs = abiConfigs; |
| 516 |
this.ndkHandler = ndkHandler; |
| 517 |
} |
| 518 |
|
| 519 |
@Override |
| 520 |
public void execute(NativeLibrary nativeLibrary) { |
| 521 |
final Abi abi = Abi.getByName(nativeBinary.getTargetPlatform().getName()); |
| 522 |
Preconditions.checkNotNull(abi); |
| 523 |
|
| 524 |
nativeLibrary.setOutput(nativeBinary.getSharedLibraryFile()); |
| 525 |
Set<File> srcFolders = Sets.newHashSet(); |
| 526 |
nativeLibrary.setGroupName(binary.getName()); |
| 527 |
nativeLibrary.setAssembleTaskName(nativeBinary.getBuildTask().getName()); |
| 528 |
nativeLibrary.setAbi(abi.getName()); |
| 529 |
nativeLibrary.setArtifactName(NdkUtils.getTargetNameFromBuildOutputFile( |
| 530 |
nativeBinary.getSharedLibraryFile())); |
| 531 |
|
| 532 |
final List<String> cFlags = |
| 533 |
Lists.newArrayList(nativeBinary.getcCompiler().getArgs()); |
| 534 |
final List<String> cppFlags = |
| 535 |
Lists.newArrayList(nativeBinary.getCppCompiler().getArgs()); |
| 536 |
|
| 537 |
NdkAbiOptions abiConfig = abiConfigs.get(abi.getName()); |
| 538 |
|
| 539 |
String sysroot = (abiConfig == null || abiConfig.getPlatformVersion() == null) |
| 540 |
? ndkHandler.getSysroot(abi) |
| 541 |
: ndkHandler.getSysroot(abi, abiConfig.getPlatformVersion()); |
| 542 |
cFlags.add("--sysroot=" + sysroot); |
| 543 |
cppFlags.add("--sysroot=" + sysroot); |
| 544 |
|
| 545 |
for (LanguageSourceSet sourceSet : nativeBinary.getSources()) { |
| 546 |
srcFolders.addAll(sourceSet.getSource().getSrcDirs()); |
| 547 |
if (sourceSet instanceof HeaderExportingSourceSet) { |
| 548 |
Set<File> headerDirs = |
| 549 |
((HeaderExportingSourceSet) sourceSet).getExportedHeaders() |
| 550 |
.getSrcDirs(); |
| 551 |
for (File headerDir : headerDirs) { |
| 552 |
if (headerDir.isDirectory() |
| 553 |
&& !nativeLibrary.getExportedHeaders().contains(headerDir)) { |
| 554 |
nativeLibrary.getExportedHeaders().add(headerDir); |
| 555 |
|
| 556 |
// Exported headers are not part of the binary's flag. Need to add |
| 557 |
// it manually. |
| 558 |
cFlags.add("-I" + headerDir); |
| 559 |
cppFlags.add("-I" + headerDir); |
| 560 |
} |
| 561 |
} |
| 562 |
} |
| 563 |
} |
| 564 |
|
| 565 |
for (NativeDependencySet dependency : nativeBinary.getLibs()) { |
| 566 |
for (File includeDir : dependency.getIncludeRoots()) { |
| 567 |
cFlags.add("-I" + includeDir); |
| 568 |
cppFlags.add("-I" + includeDir); |
| 569 |
} |
| 570 |
} |
| 571 |
|
| 572 |
for (final File srcFolder : srcFolders) { |
| 573 |
nativeLibrary.getFolders().create( |
| 574 |
nativeSourceFolder -> { |
| 575 |
nativeSourceFolder.setSrc(srcFolder); |
| 576 |
nativeSourceFolder.setcFlags( |
| 577 |
StringHelper.quoteAndJoinTokens(cFlags)); |
| 578 |
nativeSourceFolder.setCppFlags( |
| 579 |
StringHelper.quoteAndJoinTokens(cppFlags)); |
| 580 |
}); |
| 581 |
} |
| 582 |
|
| 583 |
nativeLibrary.setToolchain("ndk-" + abi.getName()); |
| 584 |
} |
| 585 |
} |
| 586 |
|
| 587 |
@Model("androidInjectedBuildAbi") |
| 588 |
public static String getBuildAbi(Project project) { |
| 589 |
return AndroidGradleOptions.isBuildOnlyTargetAbiEnabled(project) |
| 590 |
? Strings.nullToEmpty(AndroidGradleOptions.getBuildTargetAbi(project)) |
| 591 |
: ""; |
| 592 |
} |
| 593 |
|
| 594 |
@Finalize |
| 595 |
public static void attachNativeTasksToAndroidBinary( |
| 596 |
@Path("binaries") ModelMap<AndroidBinaryInternal> binaries, |
| 597 |
@Path("androidInjectedBuildAbi") final String buildAbi) { |
| 598 |
binaries.afterEach(binary -> { |
| 599 |
// Only build the first supported ABI of the device. |
| 600 |
String deviceAbi = buildAbi.isEmpty() |
| 601 |
? null |
| 602 |
: Iterables.getFirst(Arrays.asList(buildAbi.split(",")), null); |
| 603 |
for (NativeLibraryBinarySpec nativeBinary : binary.getNativeBinaries()) { |
| 604 |
List<String> targetAbi = binary.getTargetAbi(); |
| 605 |
if ((deviceAbi == null |
| 606 |
|| nativeBinary.getTargetPlatform().getName().equals(deviceAbi)) |
| 607 |
&& (targetAbi.isEmpty() |
| 608 |
|| targetAbi.contains(nativeBinary.getTargetPlatform().getName()))) { |
| 609 |
binary.getBuildTask().dependsOn( |
| 610 |
NdkNamingScheme.getNdkBuildTaskName(nativeBinary)); |
| 611 |
} |
| 612 |
} |
| 613 |
}); |
| 614 |
} |
| 615 |
|
| 616 |
@Model(ARTIFACTS) |
| 617 |
public static void createNativeLibraryArtifacts( |
| 618 |
ArtifactContainer artifactContainer, |
| 619 |
@Path("binaries") ModelMap<AndroidBinaryInternal> binaries, |
| 620 |
@Path("android.sources") final ModelMap<FunctionalSourceSet> sources, |
| 621 |
final ModelMap<Task> tasks, |
| 622 |
@Path(NATIVE_DEPENDENCIES) final Multimap<String, NativeDependencyResolveResult> dependencies, |
| 623 |
NdkHandler ndkHandler, |
| 624 |
ProjectIdentifier project) { |
| 625 |
for(final AndroidBinaryInternal binary : binaries.values()) { |
| 626 |
for (final NativeLibraryBinarySpec nativeBinary : binary.getNativeBinaries()) { |
| 627 |
final String linkage = nativeBinary instanceof SharedLibraryBinarySpec |
| 628 |
? "shared" |
| 629 |
: "static"; |
| 630 |
String name = Joiner.on('-').join( |
| 631 |
binary.getName(), |
| 632 |
nativeBinary.getTargetPlatform().getName(), |
| 633 |
linkage); |
| 634 |
|
| 635 |
artifactContainer.getNativeArtifacts().create( |
| 636 |
name, |
| 637 |
new CreateNativeLibraryArtifactAction( |
| 638 |
binary, |
| 639 |
nativeBinary, |
| 640 |
dependencies.get(nativeBinary.getName()), |
| 641 |
ndkHandler, |
| 642 |
project)); |
| 643 |
} |
| 644 |
} |
| 645 |
} |
| 646 |
|
| 647 |
private static class CreateNativeLibraryArtifactAction |
| 648 |
implements Action<NativeLibraryArtifact> { |
| 649 |
private final AndroidBinaryInternal binary; |
| 650 |
private final NativeLibraryBinarySpec nativeBinary; |
| 651 |
private final Collection<NativeDependencyResolveResult> dependencies; |
| 652 |
private final NdkHandler ndkHandler; |
| 653 |
private final ProjectIdentifier projectId; |
| 654 |
|
| 655 |
public CreateNativeLibraryArtifactAction(AndroidBinaryInternal binary, |
| 656 |
NativeLibraryBinarySpec nativeBinary, |
| 657 |
Collection<NativeDependencyResolveResult> dependencies, |
| 658 |
NdkHandler ndkHandler, |
| 659 |
ProjectIdentifier projectId) { |
| 660 |
this.binary = binary; |
| 661 |
this.nativeBinary = nativeBinary; |
| 662 |
this.dependencies = dependencies; |
| 663 |
this.ndkHandler = ndkHandler; |
| 664 |
this.projectId = projectId; |
| 665 |
} |
| 666 |
|
| 667 |
@Override |
| 668 |
public void execute(NativeLibraryArtifact artifact) { |
| 669 |
|
| 670 |
final NativeDependencyLinkage linkage = |
| 671 |
nativeBinary instanceof SharedLibraryBinarySpec |
| 672 |
? NativeDependencyLinkage.SHARED |
| 673 |
: NativeDependencyLinkage.STATIC; |
| 674 |
File output = nativeBinary instanceof SharedLibraryBinarySpec |
| 675 |
? ((SharedLibraryBinarySpec) nativeBinary).getSharedLibraryFile() |
| 676 |
: ((StaticLibraryBinarySpec) nativeBinary).getStaticLibraryFile(); |
| 677 |
|
| 678 |
artifact.getLibraries().add(output); |
| 679 |
artifact.setBuildType(binary.getBuildType().getName()); |
| 680 |
for (ProductFlavor flavor : binary.getProductFlavors()) { |
| 681 |
artifact.getProductFlavors().add(flavor.getName()); |
| 682 |
} |
| 683 |
artifact.setVariantName(binary.getName()); |
| 684 |
artifact.setAbi(nativeBinary.getTargetPlatform().getName()); |
| 685 |
artifact.setTarget(NdkUtils.getTargetNameFromBuildOutputFile(output)); |
| 686 |
artifact.setLinkage(linkage); |
| 687 |
|
| 688 |
List<Object> builtBy = Lists.newArrayList(); |
| 689 |
builtBy.add(nativeBinary); |
| 690 |
builtBy.add(projectId.getPath() + ":" + NdkNamingScheme.getNdkBuildTaskName(nativeBinary)); |
| 691 |
artifact.setBuiltBy(builtBy); |
| 692 |
for (LanguageSourceSet sourceSet : nativeBinary.getSources()) { |
| 693 |
if (sourceSet instanceof HeaderExportingSourceSet) { |
| 694 |
HeaderExportingSourceSet source = (HeaderExportingSourceSet) sourceSet; |
| 695 |
artifact.getExportedHeaderDirectories().addAll( |
| 696 |
source.getExportedHeaders().getSrcDirs()); |
| 697 |
} |
| 698 |
} |
| 699 |
Abi abi = Abi.getByName(nativeBinary.getTargetPlatform().getName()); |
| 700 |
Preconditions.checkNotNull(abi); |
| 701 |
final StlNativeToolSpecification stlConfig = |
| 702 |
ndkHandler.getStlNativeToolSpecification( |
| 703 |
Stl.getById(binary.getMergedNdkConfig().getStl()), |
| 704 |
binary.getMergedNdkConfig().getStlVersion(), |
| 705 |
abi); |
| 706 |
artifact.getLibraries().addAll(stlConfig.getSharedLibs()); |
| 707 |
|
| 708 |
// Include transitive dependencies. |
| 709 |
// Dynamic objects from dependencies needs to be added to the library list. |
| 710 |
for (NativeDependencyResolveResult dependency : dependencies) { |
| 711 |
for (NativeLibraryBinary lib : dependency.getPrebuiltLibraries()) { |
| 712 |
if (lib instanceof SharedLibraryBinary |
| 713 |
&& abi.getName().equals(lib.getTargetPlatform().getName())) { |
| 714 |
artifact.getLibraries().add(((SharedLibraryBinary) lib).getSharedLibraryFile()); |
| 715 |
} |
| 716 |
} |
| 717 |
|
| 718 |
Collection<NativeLibraryArtifact> artifacts = dependency.getNativeArtifacts(); |
| 719 |
for (NativeLibraryArtifact dep : artifacts) { |
| 720 |
if (abi.getName().equals(dep.getAbi())) { |
| 721 |
dep.getLibraries().stream() |
| 722 |
.filter(file -> file.getName().endsWith(".so")) |
| 723 |
.forEach(artifact.getLibraries()::add); |
| 724 |
} |
| 725 |
} |
| 726 |
} |
| 727 |
} |
| 728 |
} |
| 729 |
|
| 730 |
/** |
| 731 |
* Remove unintended tasks created by Gradle native plugin from task list. |
| 732 |
* |
| 733 |
* Gradle native plugins creates static library tasks automatically. This method removes |
| 734 |
* them to avoid cluttering the task list. |
| 735 |
*/ |
| 736 |
@Mutate |
| 737 |
public static void hideNativeTasks( |
| 738 |
TaskContainer tasks, |
| 739 |
@Path("binaries") ModelMap<NativeLibraryBinarySpec> binaries) { |
| 740 |
// Gradle do not support a way to remove created tasks. The best workaround is to clear |
| 741 |
// the group of the task and have another task depends on it. Therefore, we have to |
| 742 |
// create a dummy task to depend on all the tasks that we do not want to show up on the |
| 743 |
// task list. The dummy task dependsOn itself, effectively making it non-executable and |
| 744 |
// invisible unless the --all option is use. |
| 745 |
final Task nonExecutableTask = tasks.create("nonExecutableTask"); |
| 746 |
nonExecutableTask.dependsOn(nonExecutableTask); |
| 747 |
nonExecutableTask.setDescription( |
| 748 |
"Dummy task to hide other unwanted tasks in the task list."); |
| 749 |
|
| 750 |
for (NativeLibraryBinarySpec binary : binaries.values()) { |
| 751 |
Task buildTask = binary.getBuildTask(); |
| 752 |
nonExecutableTask.dependsOn(buildTask); |
| 753 |
buildTask.setGroup(null); |
| 754 |
} |
| 755 |
} |
| 756 |
} |
| 757 |
|
| 758 |
|
| 759 |
public static void configureScopeForNdk(VariantScope scope) { |
| 760 |
VariantConfiguration config = scope.getVariantConfiguration(); |
| 761 |
ImmutableSet.Builder<File> builder = ImmutableSet.builder(); |
| 762 |
for (Abi abi : NdkHandler.getAbiList()) { |
| 763 |
scope.addNdkDebuggableLibraryFolders( |
| 764 |
abi, |
| 765 |
new File( |
| 766 |
scope.getGlobalScope().getBuildDir(), |
| 767 |
NdkNamingScheme.getDebugLibraryDirectoryName( |
| 768 |
config.getBuildType().getName(), |
| 769 |
config.getFlavorName(), |
| 770 |
abi.getName()))); |
| 771 |
|
| 772 |
// Return the parent directory of the binaries' output. |
| 773 |
// If output directory is "/path/to/lib/platformName". We want to return |
| 774 |
// "/path/to/lib". |
| 775 |
builder.add(new File( |
| 776 |
scope.getGlobalScope().getBuildDir(), |
| 777 |
NdkNamingScheme.getOutputDirectoryName( |
| 778 |
config.getBuildType().getName(), |
| 779 |
config.getFlavorName(), |
| 780 |
abi.getName())).getParentFile()); |
| 781 |
} |
| 782 |
scope.setNdkSoFolder(builder.build()); |
| 783 |
} |
| 784 |
|
| 785 |
|
| 786 |
private static Collection<NativeLibraryBinarySpec> getNativeBinaries( |
| 787 |
NativeLibrarySpec library, |
| 788 |
final BuildType buildType, |
| 789 |
final List<ProductFlavor> productFlavors) { |
| 790 |
final ProductFlavorCombo<ProductFlavor> flavorGroup = |
| 791 |
new ProductFlavorCombo<>(productFlavors); |
| 792 |
return library.getBinaries().withType(NativeLibraryBinarySpec.class).values().stream() |
| 793 |
.filter(binary -> |
| 794 |
binary.getBuildType().getName().equals(buildType.getName()) |
| 795 |
&& (binary.getFlavor().getName().equals(flavorGroup.getName()) |
| 796 |
|| (productFlavors.isEmpty() |
| 797 |
&& binary.getFlavor().getName().equals("default")))) |
| 798 |
.collect(ImmutableCollectors.toImmutableList()); |
| 799 |
} |
| 800 |
|
| 801 |
/** |
| 802 |
* Return library binaries for a VariantConfiguration. |
| 803 |
*/ |
| 804 |
@NonNull |
| 805 |
public Collection<? extends BuildableComponentSpec> getBinaries( |
| 806 |
@NonNull final VariantConfiguration variantConfig) { |
| 807 |
if (variantConfig.getType().isForTesting()) { |
| 808 |
// Do not return binaries for test variants as test source set is not supported at the |
| 809 |
// moment. |
| 810 |
return Collections.emptyList(); |
| 811 |
} |
| 812 |
|
| 813 |
ModelMap<BinarySpec> binaries = modelRegistry.realize( |
| 814 |
new ModelPath("binaries"), |
| 815 |
ModelTypes.modelMap(ModelType.of(BinarySpec.class))); |
| 816 |
|
| 817 |
return binaries.values().stream() |
| 818 |
.filter(binary -> binary instanceof AndroidBinary && |
| 819 |
binary.getName().equals(variantConfig.getFullName())) |
| 820 |
.map(binary -> (AndroidBinary) binary) |
| 821 |
.collect(Collectors.toList()); |
| 822 |
} |
| 823 |
} |