1// Copyright 2016 Google Inc. All rights reserved. 2// 3// Licensed under the Apache License, Version 2.0 (the "License"); 4// you may not use this file except in compliance with the License. 5// You may obtain a copy of the License at 6// 7// http://www.apache.org/licenses/LICENSE-2.0 8// 9// Unless required by applicable law or agreed to in writing, software 10// distributed under the License is distributed on an "AS IS" BASIS, 11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12// See the License for the specific language governing permissions and 13// limitations under the License. 14 15package cc 16 17import ( 18 "fmt" 19 "io" 20 "path/filepath" 21 "regexp" 22 "strconv" 23 "strings" 24 "sync" 25 26 "android/soong/android" 27 "android/soong/bazel" 28 "android/soong/bazel/cquery" 29 30 "github.com/google/blueprint" 31 "github.com/google/blueprint/pathtools" 32 "github.com/google/blueprint/proptools" 33) 34 35// LibraryProperties is a collection of properties shared by cc library rules/cc. 36type LibraryProperties struct { 37 // local file name to pass to the linker as -unexported_symbols_list 38 Unexported_symbols_list *string `android:"path,arch_variant"` 39 // local file name to pass to the linker as -force_symbols_not_weak_list 40 Force_symbols_not_weak_list *string `android:"path,arch_variant"` 41 // local file name to pass to the linker as -force_symbols_weak_list 42 Force_symbols_weak_list *string `android:"path,arch_variant"` 43 44 // rename host libraries to prevent overlap with system installed libraries 45 Unique_host_soname *bool 46 47 Aidl struct { 48 // export headers generated from .aidl sources 49 Export_aidl_headers *bool 50 } 51 52 Proto struct { 53 // export headers generated from .proto sources 54 Export_proto_headers *bool 55 } 56 57 Sysprop struct { 58 // Whether platform owns this sysprop library. 59 Platform *bool 60 } `blueprint:"mutated"` 61 62 Static_ndk_lib *bool 63 64 // Generate stubs to make this library accessible to APEXes. 65 Stubs struct { 66 // Relative path to the symbol map. The symbol map provides the list of 67 // symbols that are exported for stubs variant of this library. 68 Symbol_file *string `android:"path"` 69 70 // List versions to generate stubs libs for. The version name "current" is always 71 // implicitly added. 72 Versions []string 73 74 // Whether to not require the implementation of the library to be installed if a 75 // client of the stubs is installed. Defaults to true; set to false if the 76 // implementation is made available by some other means, e.g. in a Microdroid 77 // virtual machine. 78 Implementation_installable *bool 79 } 80 81 // set the name of the output 82 Stem *string `android:"arch_variant"` 83 84 // set suffix of the name of the output 85 Suffix *string `android:"arch_variant"` 86 87 // Properties for ABI compatibility checker. 88 Header_abi_checker headerAbiCheckerProperties 89 90 Target struct { 91 Vendor, Product struct { 92 // set suffix of the name of the output 93 Suffix *string `android:"arch_variant"` 94 95 Header_abi_checker headerAbiCheckerProperties 96 } 97 98 Platform struct { 99 Header_abi_checker headerAbiCheckerProperties 100 } 101 } 102 103 // Names of modules to be overridden. Listed modules can only be other shared libraries 104 // (in Make or Soong). 105 // This does not completely prevent installation of the overridden libraries, but if both 106 // binaries would be installed by default (in PRODUCT_PACKAGES) the other library will be removed 107 // from PRODUCT_PACKAGES. 108 Overrides []string 109 110 // Inject boringssl hash into the shared library. This is only intended for use by external/boringssl. 111 Inject_bssl_hash *bool `android:"arch_variant"` 112 113 // If this is an LLNDK library, properties to describe the LLNDK stubs. Will be copied from 114 // the module pointed to by llndk_stubs if it is set. 115 Llndk llndkLibraryProperties 116 117 // If this is a vendor public library, properties to describe the vendor public library stubs. 118 Vendor_public_library vendorPublicLibraryProperties 119} 120 121// StaticProperties is a properties stanza to affect only attributes of the "static" variants of a 122// library module. 123type StaticProperties struct { 124 Static StaticOrSharedProperties `android:"arch_variant"` 125} 126 127// SharedProperties is a properties stanza to affect only attributes of the "shared" variants of a 128// library module. 129type SharedProperties struct { 130 Shared StaticOrSharedProperties `android:"arch_variant"` 131} 132 133// StaticOrSharedProperties is an embedded struct representing properties to affect attributes of 134// either only the "static" variants or only the "shared" variants of a library module. These override 135// the base properties of the same name. 136// Use `StaticProperties` or `SharedProperties`, depending on which variant is needed. 137// `StaticOrSharedProperties` exists only to avoid duplication. 138type StaticOrSharedProperties struct { 139 Srcs []string `android:"path,arch_variant"` 140 141 Tidy_disabled_srcs []string `android:"path,arch_variant"` 142 143 Tidy_timeout_srcs []string `android:"path,arch_variant"` 144 145 Sanitized Sanitized `android:"arch_variant"` 146 147 Cflags []string `android:"arch_variant"` 148 149 Enabled *bool `android:"arch_variant"` 150 Whole_static_libs []string `android:"arch_variant"` 151 Static_libs []string `android:"arch_variant"` 152 Shared_libs []string `android:"arch_variant"` 153 System_shared_libs []string `android:"arch_variant"` 154 155 Export_shared_lib_headers []string `android:"arch_variant"` 156 Export_static_lib_headers []string `android:"arch_variant"` 157 158 Apex_available []string `android:"arch_variant"` 159 160 Installable *bool `android:"arch_variant"` 161} 162 163type LibraryMutatedProperties struct { 164 // Build a static variant 165 BuildStatic bool `blueprint:"mutated"` 166 // Build a shared variant 167 BuildShared bool `blueprint:"mutated"` 168 // This variant is shared 169 VariantIsShared bool `blueprint:"mutated"` 170 // This variant is static 171 VariantIsStatic bool `blueprint:"mutated"` 172 173 // This variant is a stubs lib 174 BuildStubs bool `blueprint:"mutated"` 175 // This variant is the latest version 176 IsLatestVersion bool `blueprint:"mutated"` 177 // Version of the stubs lib 178 StubsVersion string `blueprint:"mutated"` 179 // List of all stubs versions associated with an implementation lib 180 AllStubsVersions []string `blueprint:"mutated"` 181} 182 183type FlagExporterProperties struct { 184 // list of directories relative to the Blueprints file that will 185 // be added to the include path (using -I) for this module and any module that links 186 // against this module. Directories listed in export_include_dirs do not need to be 187 // listed in local_include_dirs. 188 Export_include_dirs []string `android:"arch_variant,variant_prepend"` 189 190 // list of directories that will be added to the system include path 191 // using -isystem for this module and any module that links against this module. 192 Export_system_include_dirs []string `android:"arch_variant,variant_prepend"` 193 194 Target struct { 195 Vendor, Product struct { 196 // list of exported include directories, like 197 // export_include_dirs, that will be applied to 198 // vendor or product variant of this library. 199 // This will overwrite any other declarations. 200 Override_export_include_dirs []string 201 } 202 } 203} 204 205func init() { 206 RegisterLibraryBuildComponents(android.InitRegistrationContext) 207} 208 209func RegisterLibraryBuildComponents(ctx android.RegistrationContext) { 210 ctx.RegisterModuleType("cc_library_static", LibraryStaticFactory) 211 ctx.RegisterModuleType("cc_library_shared", LibrarySharedFactory) 212 ctx.RegisterModuleType("cc_library", LibraryFactory) 213 ctx.RegisterModuleType("cc_library_host_static", LibraryHostStaticFactory) 214 ctx.RegisterModuleType("cc_library_host_shared", LibraryHostSharedFactory) 215} 216 217// TODO(b/199902614): Can this be factored to share with the other Attributes? 218// For bp2build conversion. 219type bazelCcLibraryAttributes struct { 220 // Attributes pertaining to both static and shared variants. 221 Srcs bazel.LabelListAttribute 222 Srcs_c bazel.LabelListAttribute 223 Srcs_as bazel.LabelListAttribute 224 225 Copts bazel.StringListAttribute 226 Cppflags bazel.StringListAttribute 227 Conlyflags bazel.StringListAttribute 228 Asflags bazel.StringListAttribute 229 230 Hdrs bazel.LabelListAttribute 231 232 Deps bazel.LabelListAttribute 233 Implementation_deps bazel.LabelListAttribute 234 Dynamic_deps bazel.LabelListAttribute 235 Implementation_dynamic_deps bazel.LabelListAttribute 236 Whole_archive_deps bazel.LabelListAttribute 237 Implementation_whole_archive_deps bazel.LabelListAttribute 238 System_dynamic_deps bazel.LabelListAttribute 239 240 Export_includes bazel.StringListAttribute 241 Export_system_includes bazel.StringListAttribute 242 Local_includes bazel.StringListAttribute 243 Absolute_includes bazel.StringListAttribute 244 Linkopts bazel.StringListAttribute 245 Rtti bazel.BoolAttribute 246 247 Stl *string 248 Cpp_std *string 249 C_std *string 250 251 // This is shared only. 252 Additional_linker_inputs bazel.LabelListAttribute 253 254 // Common properties shared between both shared and static variants. 255 Shared staticOrSharedAttributes 256 Static staticOrSharedAttributes 257 258 Strip stripAttributes 259 260 Features bazel.StringListAttribute 261} 262 263type aidlLibraryAttributes struct { 264 Srcs bazel.LabelListAttribute 265 Include_dir *string 266 Tags bazel.StringListAttribute 267} 268 269type ccAidlLibraryAttributes struct { 270 Deps bazel.LabelListAttribute 271 Implementation_deps bazel.LabelListAttribute 272 Implementation_dynamic_deps bazel.LabelListAttribute 273 Tags bazel.StringListAttribute 274 sdkAttributes 275} 276 277type stripAttributes struct { 278 Keep_symbols bazel.BoolAttribute 279 Keep_symbols_and_debug_frame bazel.BoolAttribute 280 Keep_symbols_list bazel.StringListAttribute 281 All bazel.BoolAttribute 282 None bazel.BoolAttribute 283} 284 285func stripAttrsFromLinkerAttrs(la *linkerAttributes) stripAttributes { 286 return stripAttributes{ 287 Keep_symbols: la.stripKeepSymbols, 288 Keep_symbols_and_debug_frame: la.stripKeepSymbolsAndDebugFrame, 289 Keep_symbols_list: la.stripKeepSymbolsList, 290 All: la.stripAll, 291 None: la.stripNone, 292 } 293} 294 295func libraryBp2Build(ctx android.TopDownMutatorContext, m *Module) { 296 sharedAttrs := bp2BuildParseSharedProps(ctx, m) 297 staticAttrs := bp2BuildParseStaticProps(ctx, m) 298 baseAttributes := bp2BuildParseBaseProps(ctx, m) 299 compilerAttrs := baseAttributes.compilerAttributes 300 linkerAttrs := baseAttributes.linkerAttributes 301 exportedIncludes := bp2BuildParseExportedIncludes(ctx, m, &compilerAttrs.includes) 302 303 srcs := compilerAttrs.srcs 304 305 sharedAttrs.Dynamic_deps.Add(baseAttributes.protoDependency) 306 staticAttrs.Deps.Add(baseAttributes.protoDependency) 307 308 asFlags := compilerAttrs.asFlags 309 if compilerAttrs.asSrcs.IsEmpty() && sharedAttrs.Srcs_as.IsEmpty() && staticAttrs.Srcs_as.IsEmpty() { 310 // Skip asflags for BUILD file simplicity if there are no assembly sources. 311 asFlags = bazel.MakeStringListAttribute(nil) 312 } 313 314 staticCommonAttrs := staticOrSharedAttributes{ 315 Srcs: *srcs.Clone().Append(staticAttrs.Srcs), 316 Srcs_c: *compilerAttrs.cSrcs.Clone().Append(staticAttrs.Srcs_c), 317 Srcs_as: *compilerAttrs.asSrcs.Clone().Append(staticAttrs.Srcs_as), 318 Copts: *compilerAttrs.copts.Clone().Append(staticAttrs.Copts), 319 Hdrs: *compilerAttrs.hdrs.Clone().Append(staticAttrs.Hdrs), 320 321 Deps: *linkerAttrs.deps.Clone().Append(staticAttrs.Deps), 322 Implementation_deps: *linkerAttrs.implementationDeps.Clone().Append(staticAttrs.Implementation_deps), 323 Dynamic_deps: *linkerAttrs.dynamicDeps.Clone().Append(staticAttrs.Dynamic_deps), 324 Implementation_dynamic_deps: *linkerAttrs.implementationDynamicDeps.Clone().Append(staticAttrs.Implementation_dynamic_deps), 325 Implementation_whole_archive_deps: linkerAttrs.implementationWholeArchiveDeps, 326 Whole_archive_deps: *linkerAttrs.wholeArchiveDeps.Clone().Append(staticAttrs.Whole_archive_deps), 327 System_dynamic_deps: *linkerAttrs.systemDynamicDeps.Clone().Append(staticAttrs.System_dynamic_deps), 328 Runtime_deps: linkerAttrs.runtimeDeps, 329 sdkAttributes: bp2BuildParseSdkAttributes(m), 330 Native_coverage: baseAttributes.Native_coverage, 331 } 332 333 sharedCommonAttrs := staticOrSharedAttributes{ 334 Srcs: *srcs.Clone().Append(sharedAttrs.Srcs), 335 Srcs_c: *compilerAttrs.cSrcs.Clone().Append(sharedAttrs.Srcs_c), 336 Srcs_as: *compilerAttrs.asSrcs.Clone().Append(sharedAttrs.Srcs_as), 337 Copts: *compilerAttrs.copts.Clone().Append(sharedAttrs.Copts), 338 Hdrs: *compilerAttrs.hdrs.Clone().Append(sharedAttrs.Hdrs), 339 340 Deps: *linkerAttrs.deps.Clone().Append(sharedAttrs.Deps), 341 Implementation_deps: *linkerAttrs.implementationDeps.Clone().Append(sharedAttrs.Implementation_deps), 342 Dynamic_deps: *linkerAttrs.dynamicDeps.Clone().Append(sharedAttrs.Dynamic_deps), 343 Implementation_dynamic_deps: *linkerAttrs.implementationDynamicDeps.Clone().Append(sharedAttrs.Implementation_dynamic_deps), 344 Whole_archive_deps: *linkerAttrs.wholeArchiveDeps.Clone().Append(sharedAttrs.Whole_archive_deps), 345 Implementation_whole_archive_deps: linkerAttrs.implementationWholeArchiveDeps, 346 System_dynamic_deps: *linkerAttrs.systemDynamicDeps.Clone().Append(sharedAttrs.System_dynamic_deps), 347 Runtime_deps: linkerAttrs.runtimeDeps, 348 sdkAttributes: bp2BuildParseSdkAttributes(m), 349 Native_coverage: baseAttributes.Native_coverage, 350 } 351 352 staticTargetAttrs := &bazelCcLibraryStaticAttributes{ 353 staticOrSharedAttributes: staticCommonAttrs, 354 355 Cppflags: compilerAttrs.cppFlags, 356 Conlyflags: compilerAttrs.conlyFlags, 357 Asflags: asFlags, 358 359 Export_includes: exportedIncludes.Includes, 360 Export_absolute_includes: exportedIncludes.AbsoluteIncludes, 361 Export_system_includes: exportedIncludes.SystemIncludes, 362 Local_includes: compilerAttrs.localIncludes, 363 Absolute_includes: compilerAttrs.absoluteIncludes, 364 Rtti: compilerAttrs.rtti, 365 Stl: compilerAttrs.stl, 366 Cpp_std: compilerAttrs.cppStd, 367 C_std: compilerAttrs.cStd, 368 369 Features: baseAttributes.features, 370 } 371 372 sharedTargetAttrs := &bazelCcLibrarySharedAttributes{ 373 staticOrSharedAttributes: sharedCommonAttrs, 374 Cppflags: compilerAttrs.cppFlags, 375 Conlyflags: compilerAttrs.conlyFlags, 376 Asflags: asFlags, 377 378 Export_includes: exportedIncludes.Includes, 379 Export_absolute_includes: exportedIncludes.AbsoluteIncludes, 380 Export_system_includes: exportedIncludes.SystemIncludes, 381 Local_includes: compilerAttrs.localIncludes, 382 Absolute_includes: compilerAttrs.absoluteIncludes, 383 Linkopts: linkerAttrs.linkopts, 384 Rtti: compilerAttrs.rtti, 385 Stl: compilerAttrs.stl, 386 Cpp_std: compilerAttrs.cppStd, 387 C_std: compilerAttrs.cStd, 388 Use_version_lib: linkerAttrs.useVersionLib, 389 390 Additional_linker_inputs: linkerAttrs.additionalLinkerInputs, 391 392 Strip: stripAttrsFromLinkerAttrs(&linkerAttrs), 393 Features: baseAttributes.features, 394 bazelCcHeaderAbiCheckerAttributes: bp2buildParseAbiCheckerProps(ctx, m), 395 396 Fdo_profile: compilerAttrs.fdoProfile, 397 } 398 399 if compilerAttrs.stubsSymbolFile != nil && len(compilerAttrs.stubsVersions.Value) > 0 { 400 sharedTargetAttrs.Stubs_symbol_file = compilerAttrs.stubsSymbolFile 401 } 402 403 sharedTargetAttrs.Suffix = compilerAttrs.suffix 404 405 for axis, configToProps := range m.GetArchVariantProperties(ctx, &LibraryProperties{}) { 406 for cfg, props := range configToProps { 407 if props, ok := props.(*LibraryProperties); ok { 408 if props.Inject_bssl_hash != nil { 409 // This is an edge case applies only to libcrypto 410 if m.Name() == "libcrypto" || m.Name() == "libcrypto_for_testing" { 411 sharedTargetAttrs.Inject_bssl_hash.SetSelectValue(axis, cfg, props.Inject_bssl_hash) 412 } else { 413 ctx.PropertyErrorf("inject_bssl_hash", "only applies to libcrypto") 414 } 415 } 416 } 417 } 418 } 419 420 staticProps := bazel.BazelTargetModuleProperties{ 421 Rule_class: "cc_library_static", 422 Bzl_load_location: "//build/bazel/rules/cc:cc_library_static.bzl", 423 } 424 sharedProps := bazel.BazelTargetModuleProperties{ 425 Rule_class: "cc_library_shared", 426 Bzl_load_location: "//build/bazel/rules/cc:cc_library_shared.bzl", 427 } 428 429 var tagsForStaticVariant bazel.StringListAttribute 430 if compilerAttrs.stubsSymbolFile == nil && len(compilerAttrs.stubsVersions.Value) == 0 { 431 tagsForStaticVariant = android.ApexAvailableTags(m) 432 } 433 tagsForStaticVariant.Append(bazel.StringListAttribute{Value: staticAttrs.Apex_available}) 434 435 tagsForSharedVariant := android.ApexAvailableTags(m) 436 tagsForSharedVariant.Append(bazel.StringListAttribute{Value: sharedAttrs.Apex_available}) 437 438 ctx.CreateBazelTargetModuleWithRestrictions(staticProps, 439 android.CommonAttributes{ 440 Name: m.Name() + "_bp2build_cc_library_static", 441 Tags: tagsForStaticVariant, 442 }, 443 staticTargetAttrs, staticAttrs.Enabled) 444 ctx.CreateBazelTargetModuleWithRestrictions(sharedProps, 445 android.CommonAttributes{ 446 Name: m.Name(), 447 Tags: tagsForSharedVariant, 448 }, 449 sharedTargetAttrs, sharedAttrs.Enabled) 450 451 createStubsBazelTargetIfNeeded(ctx, m, compilerAttrs, exportedIncludes, baseAttributes) 452} 453 454func createStubsBazelTargetIfNeeded(ctx android.TopDownMutatorContext, m *Module, compilerAttrs compilerAttributes, exportedIncludes BazelIncludes, baseAttributes baseAttributes) { 455 if compilerAttrs.stubsSymbolFile != nil && len(compilerAttrs.stubsVersions.Value) > 0 { 456 stubSuitesProps := bazel.BazelTargetModuleProperties{ 457 Rule_class: "cc_stub_suite", 458 Bzl_load_location: "//build/bazel/rules/cc:cc_stub_library.bzl", 459 } 460 soname := m.Name() + ".so" 461 stubSuitesAttrs := &bazelCcStubSuiteAttributes{ 462 Symbol_file: compilerAttrs.stubsSymbolFile, 463 Versions: compilerAttrs.stubsVersions, 464 Export_includes: exportedIncludes.Includes, 465 Soname: &soname, 466 Source_library_label: proptools.StringPtr(m.GetBazelLabel(ctx, m)), 467 Deps: baseAttributes.deps, 468 } 469 ctx.CreateBazelTargetModule(stubSuitesProps, 470 android.CommonAttributes{Name: m.Name() + "_stub_libs"}, 471 stubSuitesAttrs) 472 473 // Add alias for the stub shared_library in @api_surfaces repository 474 currentModuleLibApiDir := ctx.Config().ApiSurfacesDir(android.ModuleLibApi, "current") 475 actualLabelInMainWorkspace := bazel.Label{ 476 Label: fmt.Sprintf("@//%s:%s%s", ctx.ModuleDir(), m.Name(), stubsSuffix), 477 } 478 ctx.CreateBazelTargetAliasInDir(currentModuleLibApiDir, m.Name(), actualLabelInMainWorkspace) 479 480 // Add alias for headers exported by the stub library 481 headerLabelInMainWorkspace := bazel.Label{ 482 // This label is generated from cc_stub_suite macro 483 Label: fmt.Sprintf("@//%s:%s_stub_libs_%s_headers", ctx.ModuleDir(), m.Name(), android.ModuleLibApi.String()), 484 } 485 headerAlias := m.Name() + "_headers" 486 ctx.CreateBazelTargetAliasInDir(currentModuleLibApiDir, headerAlias, headerLabelInMainWorkspace) 487 } 488} 489 490func apiContributionBp2Build(ctx android.TopDownMutatorContext, module *Module) { 491 apiSurfaces := make([]string, 0) 492 apiHeaders := make([]string, 0) 493 // module-libapi for apexes (non-null `stubs` property) 494 if module.HasStubsVariants() { 495 apiSurfaces = append(apiSurfaces, android.ModuleLibApi.String()) 496 apiIncludes := getModuleLibApiIncludes(ctx, module) 497 if !apiIncludes.isEmpty() { 498 createApiHeaderTarget(ctx, apiIncludes) 499 apiHeaders = append(apiHeaders, apiIncludes.name) 500 } 501 } 502 // vendorapi (non-null `llndk` property) 503 if module.HasLlndkStubs() { 504 apiSurfaces = append(apiSurfaces, android.VendorApi.String()) 505 apiIncludes := getVendorApiIncludes(ctx, module) 506 if !apiIncludes.isEmpty() { 507 createApiHeaderTarget(ctx, apiIncludes) 508 apiHeaders = append(apiHeaders, apiIncludes.name) 509 } 510 } 511 // create a target only if this module contributes to an api surface 512 // TODO: Currently this does not distinguish modulelibapi-only headers and vendrorapi-only headers 513 // TODO: Update so that modulelibapi-only headers do not get exported to vendorapi (and vice-versa) 514 if len(apiSurfaces) > 0 { 515 props := bazel.BazelTargetModuleProperties{ 516 Rule_class: "cc_api_contribution", 517 Bzl_load_location: "//build/bazel/rules/apis:cc_api_contribution.bzl", 518 } 519 attrs := &bazelCcApiContributionAttributes{ 520 Library_name: module.Name(), 521 Api_surfaces: bazel.MakeStringListAttribute(apiSurfaces), 522 Api: apiLabelAttribute(ctx, module), 523 Hdrs: bazel.MakeLabelListAttribute( 524 bazel.MakeLabelListFromTargetNames(apiHeaders), 525 ), 526 } 527 ctx.CreateBazelTargetModule( 528 props, 529 android.CommonAttributes{ 530 Name: android.ApiContributionTargetName(module.Name()), 531 SkipData: proptools.BoolPtr(true), 532 }, 533 attrs, 534 ) 535 } 536} 537 538// Native apis are versioned in a single .map.txt for all api surfaces 539// Pick any one of the .map.txt files 540func apiLabelAttribute(ctx android.TopDownMutatorContext, module *Module) bazel.LabelAttribute { 541 var apiFile *string 542 linker := module.linker.(*libraryDecorator) 543 if llndkApi := linker.Properties.Llndk.Symbol_file; llndkApi != nil { 544 apiFile = llndkApi 545 } else if moduleLibApi := linker.Properties.Stubs.Symbol_file; moduleLibApi != nil { 546 apiFile = moduleLibApi 547 } else { 548 ctx.ModuleErrorf("API surface library does not have any API file") 549 } 550 apiLabel := android.BazelLabelForModuleSrcSingle(ctx, proptools.String(apiFile)).Label 551 return *bazel.MakeLabelAttribute(apiLabel) 552} 553 554// wrapper struct to flatten the arch and os specific export_include_dirs 555// flattening is necessary since we want to export apis of all arches even when we build for x86 (e.g.) 556type bazelCcApiLibraryHeadersAttributes struct { 557 bazelCcLibraryHeadersAttributes 558 559 Arch *string 560} 561 562func (a *bazelCcApiLibraryHeadersAttributes) isEmpty() bool { 563 return a.Export_includes.IsEmpty() && 564 a.Export_system_includes.IsEmpty() && 565 a.Deps.IsEmpty() 566} 567 568type apiIncludes struct { 569 name string // name of the Bazel target in the generated bp2build workspace 570 attrs bazelCcApiLibraryHeadersAttributes 571} 572 573func (includes *apiIncludes) isEmpty() bool { 574 return includes.attrs.isEmpty() 575} 576 577func (includes *apiIncludes) addDep(name string) { 578 l := bazel.Label{Label: ":" + name} 579 ll := bazel.MakeLabelList([]bazel.Label{l}) 580 lla := bazel.MakeLabelListAttribute(ll) 581 includes.attrs.Deps.Append(lla) 582} 583 584// includes provided to the module-lib API surface. This API surface is used by apexes. 585func getModuleLibApiIncludes(ctx android.TopDownMutatorContext, c *Module) apiIncludes { 586 flagProps := c.library.(*libraryDecorator).flagExporter.Properties 587 linkProps := c.library.(*libraryDecorator).baseLinker.Properties 588 includes := android.FirstUniqueStrings(flagProps.Export_include_dirs) 589 systemIncludes := android.FirstUniqueStrings(flagProps.Export_system_include_dirs) 590 headerLibs := android.FirstUniqueStrings(linkProps.Export_header_lib_headers) 591 attrs := bazelCcLibraryHeadersAttributes{ 592 Export_includes: bazel.MakeStringListAttribute(includes), 593 Export_system_includes: bazel.MakeStringListAttribute(systemIncludes), 594 Deps: bazel.MakeLabelListAttribute(apiHeaderLabels(ctx, headerLibs)), 595 } 596 597 return apiIncludes{ 598 name: c.Name() + ".module-libapi.headers", 599 attrs: bazelCcApiLibraryHeadersAttributes{ 600 bazelCcLibraryHeadersAttributes: attrs, 601 }, 602 } 603} 604 605func getVendorApiIncludes(ctx android.TopDownMutatorContext, c *Module) apiIncludes { 606 baseProps := c.library.(*libraryDecorator).flagExporter.Properties 607 llndkProps := c.library.(*libraryDecorator).Properties.Llndk 608 includes := baseProps.Export_include_dirs 609 systemIncludes := baseProps.Export_system_include_dirs 610 // LLNDK can override the base includes 611 if llndkIncludes := llndkProps.Override_export_include_dirs; llndkIncludes != nil { 612 includes = llndkIncludes 613 } 614 if proptools.Bool(llndkProps.Export_headers_as_system) { 615 systemIncludes = append(systemIncludes, includes...) 616 includes = nil 617 } 618 619 attrs := bazelCcLibraryHeadersAttributes{ 620 Export_includes: bazel.MakeStringListAttribute(includes), 621 Export_system_includes: bazel.MakeStringListAttribute(systemIncludes), 622 Deps: bazel.MakeLabelListAttribute(apiHeaderLabels(ctx, llndkProps.Export_llndk_headers)), 623 } 624 return apiIncludes{ 625 name: c.Name() + ".vendorapi.headers", 626 attrs: bazelCcApiLibraryHeadersAttributes{ 627 bazelCcLibraryHeadersAttributes: attrs, 628 }, 629 } 630} 631 632// cc_library creates both static and/or shared libraries for a device and/or 633// host. By default, a cc_library has a single variant that targets the device. 634// Specifying `host_supported: true` also creates a library that targets the 635// host. 636func LibraryFactory() android.Module { 637 module, _ := NewLibrary(android.HostAndDeviceSupported) 638 // Can be used as both a static and a shared library. 639 module.sdkMemberTypes = []android.SdkMemberType{ 640 sharedLibrarySdkMemberType, 641 staticLibrarySdkMemberType, 642 staticAndSharedLibrarySdkMemberType, 643 } 644 module.bazelable = true 645 module.bazelHandler = &ccLibraryBazelHandler{module: module} 646 return module.Init() 647} 648 649// cc_library_static creates a static library for a device and/or host binary. 650func LibraryStaticFactory() android.Module { 651 module, library := NewLibrary(android.HostAndDeviceSupported) 652 library.BuildOnlyStatic() 653 module.sdkMemberTypes = []android.SdkMemberType{staticLibrarySdkMemberType} 654 module.bazelable = true 655 module.bazelHandler = &ccLibraryBazelHandler{module: module} 656 return module.Init() 657} 658 659// cc_library_shared creates a shared library for a device and/or host. 660func LibrarySharedFactory() android.Module { 661 module, library := NewLibrary(android.HostAndDeviceSupported) 662 library.BuildOnlyShared() 663 module.sdkMemberTypes = []android.SdkMemberType{sharedLibrarySdkMemberType} 664 module.bazelable = true 665 module.bazelHandler = &ccLibraryBazelHandler{module: module} 666 return module.Init() 667} 668 669// cc_library_host_static creates a static library that is linkable to a host 670// binary. 671func LibraryHostStaticFactory() android.Module { 672 module, library := NewLibrary(android.HostSupported) 673 library.BuildOnlyStatic() 674 module.sdkMemberTypes = []android.SdkMemberType{staticLibrarySdkMemberType} 675 module.bazelable = true 676 module.bazelHandler = &ccLibraryBazelHandler{module: module} 677 return module.Init() 678} 679 680// cc_library_host_shared creates a shared library that is usable on a host. 681func LibraryHostSharedFactory() android.Module { 682 module, library := NewLibrary(android.HostSupported) 683 library.BuildOnlyShared() 684 module.sdkMemberTypes = []android.SdkMemberType{sharedLibrarySdkMemberType} 685 module.bazelable = true 686 module.bazelHandler = &ccLibraryBazelHandler{module: module} 687 return module.Init() 688} 689 690// flagExporter is a separated portion of libraryDecorator pertaining to exported 691// include paths and flags. Keeping this dependency-related information separate 692// from the rest of library information is helpful in keeping data more structured 693// and explicit. 694type flagExporter struct { 695 Properties FlagExporterProperties 696 697 dirs android.Paths // Include directories to be included with -I 698 systemDirs android.Paths // System include directories to be included with -isystem 699 flags []string // Exported raw flags. 700 deps android.Paths 701 headers android.Paths 702} 703 704// exportedIncludes returns the effective include paths for this module and 705// any module that links against this module. This is obtained from 706// the export_include_dirs property in the appropriate target stanza. 707func (f *flagExporter) exportedIncludes(ctx ModuleContext) android.Paths { 708 if ctx.inVendor() && f.Properties.Target.Vendor.Override_export_include_dirs != nil { 709 return android.PathsForModuleSrc(ctx, f.Properties.Target.Vendor.Override_export_include_dirs) 710 } 711 if ctx.inProduct() && f.Properties.Target.Product.Override_export_include_dirs != nil { 712 return android.PathsForModuleSrc(ctx, f.Properties.Target.Product.Override_export_include_dirs) 713 } 714 return android.PathsForModuleSrc(ctx, f.Properties.Export_include_dirs) 715} 716 717// exportIncludes registers the include directories and system include directories to be exported 718// transitively to modules depending on this module. 719func (f *flagExporter) exportIncludes(ctx ModuleContext) { 720 f.dirs = append(f.dirs, f.exportedIncludes(ctx)...) 721 f.systemDirs = append(f.systemDirs, android.PathsForModuleSrc(ctx, f.Properties.Export_system_include_dirs)...) 722} 723 724// exportIncludesAsSystem registers the include directories and system include directories to be 725// exported transitively both as system include directories to modules depending on this module. 726func (f *flagExporter) exportIncludesAsSystem(ctx ModuleContext) { 727 // all dirs are force exported as system 728 f.systemDirs = append(f.systemDirs, f.exportedIncludes(ctx)...) 729 f.systemDirs = append(f.systemDirs, android.PathsForModuleSrc(ctx, f.Properties.Export_system_include_dirs)...) 730} 731 732// reexportDirs registers the given directories as include directories to be exported transitively 733// to modules depending on this module. 734func (f *flagExporter) reexportDirs(dirs ...android.Path) { 735 f.dirs = append(f.dirs, dirs...) 736} 737 738// reexportSystemDirs registers the given directories as system include directories 739// to be exported transitively to modules depending on this module. 740func (f *flagExporter) reexportSystemDirs(dirs ...android.Path) { 741 f.systemDirs = append(f.systemDirs, dirs...) 742} 743 744// reexportFlags registers the flags to be exported transitively to modules depending on this 745// module. 746func (f *flagExporter) reexportFlags(flags ...string) { 747 if android.PrefixInList(flags, "-I") || android.PrefixInList(flags, "-isystem") { 748 panic(fmt.Errorf("Exporting invalid flag %q: "+ 749 "use reexportDirs or reexportSystemDirs to export directories", flag)) 750 } 751 f.flags = append(f.flags, flags...) 752} 753 754func (f *flagExporter) reexportDeps(deps ...android.Path) { 755 f.deps = append(f.deps, deps...) 756} 757 758// addExportedGeneratedHeaders does nothing but collects generated header files. 759// This can be differ to exportedDeps which may contain phony files to minimize ninja. 760func (f *flagExporter) addExportedGeneratedHeaders(headers ...android.Path) { 761 f.headers = append(f.headers, headers...) 762} 763 764func (f *flagExporter) setProvider(ctx android.ModuleContext) { 765 ctx.SetProvider(FlagExporterInfoProvider, FlagExporterInfo{ 766 // Comes from Export_include_dirs property, and those of exported transitive deps 767 IncludeDirs: android.FirstUniquePaths(f.dirs), 768 // Comes from Export_system_include_dirs property, and those of exported transitive deps 769 SystemIncludeDirs: android.FirstUniquePaths(f.systemDirs), 770 // Used in very few places as a one-off way of adding extra defines. 771 Flags: f.flags, 772 // Used sparingly, for extra files that need to be explicitly exported to dependers, 773 // or for phony files to minimize ninja. 774 Deps: f.deps, 775 // For exported generated headers, such as exported aidl headers, proto headers, or 776 // sysprop headers. 777 GeneratedHeaders: f.headers, 778 }) 779} 780 781// libraryDecorator wraps baseCompiler, baseLinker and baseInstaller to provide library-specific 782// functionality: static vs. shared linkage, reusing object files for shared libraries 783type libraryDecorator struct { 784 Properties LibraryProperties 785 StaticProperties StaticProperties 786 SharedProperties SharedProperties 787 MutatedProperties LibraryMutatedProperties 788 789 // For reusing static library objects for shared library 790 reuseObjects Objects 791 792 // table-of-contents file to optimize out relinking when possible 793 tocFile android.OptionalPath 794 795 flagExporter 796 flagExporterInfo *FlagExporterInfo 797 stripper Stripper 798 799 // For whole_static_libs 800 objects Objects 801 wholeStaticLibsFromPrebuilts android.Paths 802 803 // Uses the module's name if empty, but can be overridden. Does not include 804 // shlib suffix. 805 libName string 806 807 sabi *sabi 808 809 // Output archive of gcno coverage information files 810 coverageOutputFile android.OptionalPath 811 812 // linked Source Abi Dump 813 sAbiOutputFile android.OptionalPath 814 815 // Source Abi Diff 816 sAbiDiff android.Paths 817 818 // Location of the static library in the sysroot. Empty if the library is 819 // not included in the NDK. 820 ndkSysrootPath android.Path 821 822 // Location of the linked, unstripped library for shared libraries 823 unstrippedOutputFile android.Path 824 825 // Location of the file that should be copied to dist dir when requested 826 distFile android.Path 827 828 versionScriptPath android.OptionalPath 829 830 postInstallCmds []string 831 832 // If useCoreVariant is true, the vendor variant of a VNDK library is 833 // not installed. 834 useCoreVariant bool 835 checkSameCoreVariant bool 836 837 skipAPIDefine bool 838 839 // Decorated interfaces 840 *baseCompiler 841 *baseLinker 842 *baseInstaller 843 844 collectedSnapshotHeaders android.Paths 845 846 apiListCoverageXmlPath android.ModuleOutPath 847} 848 849type ccLibraryBazelHandler struct { 850 module *Module 851} 852 853var _ BazelHandler = (*ccLibraryBazelHandler)(nil) 854 855// generateStaticBazelBuildActions constructs the StaticLibraryInfo Soong 856// provider from a Bazel shared library's CcInfo provider. 857func (handler *ccLibraryBazelHandler) generateStaticBazelBuildActions(ctx android.ModuleContext, label string, ccInfo cquery.CcInfo) { 858 rootStaticArchives := ccInfo.RootStaticArchives 859 if len(rootStaticArchives) != 1 { 860 ctx.ModuleErrorf("expected exactly one root archive file for '%s', but got %s", label, rootStaticArchives) 861 return 862 } 863 var outputFilePath android.Path = android.PathForBazelOut(ctx, rootStaticArchives[0]) 864 if len(ccInfo.TidyFiles) > 0 { 865 handler.module.tidyFiles = android.PathsForBazelOut(ctx, ccInfo.TidyFiles) 866 outputFilePath = android.AttachValidationActions(ctx, outputFilePath, handler.module.tidyFiles) 867 } 868 handler.module.outputFile = android.OptionalPathForPath(outputFilePath) 869 870 objPaths := ccInfo.CcObjectFiles 871 objFiles := make(android.Paths, len(objPaths)) 872 for i, objPath := range objPaths { 873 objFiles[i] = android.PathForBazelOut(ctx, objPath) 874 } 875 objects := Objects{ 876 objFiles: objFiles, 877 } 878 879 ctx.SetProvider(StaticLibraryInfoProvider, StaticLibraryInfo{ 880 StaticLibrary: outputFilePath, 881 ReuseObjects: objects, 882 Objects: objects, 883 884 // TODO(b/190524881): Include transitive static libraries in this provider to support 885 // static libraries with deps. 886 TransitiveStaticLibrariesForOrdering: android.NewDepSetBuilder(android.TOPOLOGICAL). 887 Direct(outputFilePath). 888 Build(), 889 }) 890 891 return 892} 893 894// generateSharedBazelBuildActions constructs the SharedLibraryInfo Soong 895// provider from a Bazel shared library's CcInfo provider. 896func (handler *ccLibraryBazelHandler) generateSharedBazelBuildActions(ctx android.ModuleContext, label string, ccInfo cquery.CcInfo) { 897 rootDynamicLibraries := ccInfo.RootDynamicLibraries 898 899 if len(rootDynamicLibraries) != 1 { 900 ctx.ModuleErrorf("expected exactly one root dynamic library file for '%s', but got %s", label, rootDynamicLibraries) 901 return 902 } 903 var outputFilePath android.Path = android.PathForBazelOut(ctx, rootDynamicLibraries[0]) 904 if len(ccInfo.TidyFiles) > 0 { 905 handler.module.tidyFiles = android.PathsForBazelOut(ctx, ccInfo.TidyFiles) 906 outputFilePath = android.AttachValidationActions(ctx, outputFilePath, handler.module.tidyFiles) 907 } 908 909 handler.module.outputFile = android.OptionalPathForPath(outputFilePath) 910 handler.module.linker.(*libraryDecorator).unstrippedOutputFile = android.PathForBazelOut(ctx, ccInfo.UnstrippedOutput) 911 912 var tocFile android.OptionalPath 913 if len(ccInfo.TocFile) > 0 { 914 tocFile = android.OptionalPathForPath(android.PathForBazelOut(ctx, ccInfo.TocFile)) 915 } 916 handler.module.linker.(*libraryDecorator).tocFile = tocFile 917 918 if len(ccInfo.AbiDiffFiles) > 0 { 919 handler.module.linker.(*libraryDecorator).sAbiDiff = android.PathsForBazelOut(ctx, ccInfo.AbiDiffFiles) 920 } 921 922 ctx.SetProvider(SharedLibraryInfoProvider, SharedLibraryInfo{ 923 TableOfContents: tocFile, 924 SharedLibrary: outputFilePath, 925 Target: ctx.Target(), 926 // TODO(b/190524881): Include transitive static libraries in this provider to support 927 // static libraries with deps. The provider key for this is TransitiveStaticLibrariesForOrdering. 928 }) 929} 930 931func (handler *ccLibraryBazelHandler) QueueBazelCall(ctx android.BaseModuleContext, label string) { 932 bazelCtx := ctx.Config().BazelContext 933 bazelCtx.QueueBazelRequest(label, cquery.GetCcInfo, android.GetConfigKeyApexVariant(ctx, GetApexConfigKey(ctx))) 934 if v := handler.module.library.stubsVersion(); v != "" { 935 stubsLabel := label + "_stub_libs-" + v 936 bazelCtx.QueueBazelRequest(stubsLabel, cquery.GetCcInfo, android.GetConfigKeyApexVariant(ctx, GetApexConfigKey(ctx))) 937 } 938} 939 940func (handler *ccLibraryBazelHandler) ProcessBazelQueryResponse(ctx android.ModuleContext, label string) { 941 if v := handler.module.library.stubsVersion(); v != "" { 942 // if we are a stubs variant, just use the Bazel stubs target 943 label = label + "_stub_libs-" + v 944 } 945 bazelCtx := ctx.Config().BazelContext 946 ccInfo, err := bazelCtx.GetCcInfo(label, android.GetConfigKeyApexVariant(ctx, GetApexConfigKey(ctx))) 947 if err != nil { 948 ctx.ModuleErrorf("Error getting Bazel CcInfo: %s", err) 949 return 950 } 951 952 if handler.module.static() { 953 handler.generateStaticBazelBuildActions(ctx, label, ccInfo) 954 } else if handler.module.Shared() { 955 handler.generateSharedBazelBuildActions(ctx, label, ccInfo) 956 } else { 957 ctx.ModuleErrorf("Unhandled bazel case for %s (neither shared nor static!)", ctx.ModuleName()) 958 } 959 960 handler.module.linker.(*libraryDecorator).setFlagExporterInfoFromCcInfo(ctx, ccInfo) 961 handler.module.maybeUnhideFromMake() 962 963 if i, ok := handler.module.linker.(snapshotLibraryInterface); ok { 964 // Dependencies on this library will expect collectedSnapshotHeaders to 965 // be set, otherwise validation will fail. For now, set this to an empty 966 // list. 967 // TODO(b/190533363): More closely mirror the collectHeadersForSnapshot 968 // implementation. 969 i.(*libraryDecorator).collectedSnapshotHeaders = android.Paths{} 970 } 971 972 handler.module.setAndroidMkVariablesFromCquery(ccInfo.CcAndroidMkInfo) 973 974 cctx := moduleContextFromAndroidModuleContext(ctx, handler.module) 975 addStubDependencyProviders(cctx) 976} 977 978func (library *libraryDecorator) setFlagExporterInfoFromCcInfo(ctx android.ModuleContext, ccInfo cquery.CcInfo) { 979 flagExporterInfo := flagExporterInfoFromCcInfo(ctx, ccInfo) 980 // flag exporters consolidates properties like includes, flags, dependencies that should be 981 // exported from this module to other modules 982 ctx.SetProvider(FlagExporterInfoProvider, flagExporterInfo) 983 // Store flag info to be passed along to androidmk 984 // TODO(b/184387147): Androidmk should be done in Bazel, not Soong. 985 library.flagExporterInfo = &flagExporterInfo 986} 987 988func GlobHeadersForSnapshot(ctx android.ModuleContext, paths android.Paths) android.Paths { 989 ret := android.Paths{} 990 991 // Headers in the source tree should be globbed. On the contrast, generated headers 992 // can't be globbed, and they should be manually collected. 993 // So, we first filter out intermediate directories (which contains generated headers) 994 // from exported directories, and then glob headers under remaining directories. 995 for _, path := range paths { 996 dir := path.String() 997 // Skip if dir is for generated headers 998 if strings.HasPrefix(dir, ctx.Config().OutDir()) { 999 continue 1000 } 1001 1002 // Filter out the generated headers from bazel. 1003 if strings.HasPrefix(dir, android.PathForBazelOut(ctx, "bazel-out").String()) { 1004 continue 1005 } 1006 1007 // libeigen wrongly exports the root directory "external/eigen". But only two 1008 // subdirectories "Eigen" and "unsupported" contain exported header files. Even worse 1009 // some of them have no extension. So we need special treatment for libeigen in order 1010 // to glob correctly. 1011 if dir == "external/eigen" { 1012 // Only these two directories contains exported headers. 1013 for _, subdir := range []string{"Eigen", "unsupported/Eigen"} { 1014 globDir := "external/eigen/" + subdir + "/**/*" 1015 glob, err := ctx.GlobWithDeps(globDir, nil) 1016 if err != nil { 1017 ctx.ModuleErrorf("glob of %q failed: %s", globDir, err) 1018 return nil 1019 } 1020 for _, header := range glob { 1021 if strings.HasSuffix(header, "/") { 1022 continue 1023 } 1024 ext := filepath.Ext(header) 1025 if ext != "" && ext != ".h" { 1026 continue 1027 } 1028 ret = append(ret, android.PathForSource(ctx, header)) 1029 } 1030 } 1031 continue 1032 } 1033 globDir := dir + "/**/*" 1034 glob, err := ctx.GlobWithDeps(globDir, nil) 1035 if err != nil { 1036 ctx.ModuleErrorf("glob of %q failed: %s", globDir, err) 1037 return nil 1038 } 1039 isLibcxx := strings.HasPrefix(dir, "external/libcxx/include") 1040 for _, header := range glob { 1041 if isLibcxx { 1042 // Glob all files under this special directory, because of C++ headers with no 1043 // extension. 1044 if strings.HasSuffix(header, "/") { 1045 continue 1046 } 1047 } else { 1048 // Filter out only the files with extensions that are headers. 1049 found := false 1050 for _, ext := range HeaderExts { 1051 if strings.HasSuffix(header, ext) { 1052 found = true 1053 break 1054 } 1055 } 1056 if !found { 1057 continue 1058 } 1059 } 1060 ret = append(ret, android.PathForSource(ctx, header)) 1061 } 1062 } 1063 return ret 1064} 1065 1066func GlobGeneratedHeadersForSnapshot(_ android.ModuleContext, paths android.Paths) android.Paths { 1067 ret := android.Paths{} 1068 for _, header := range paths { 1069 // TODO(b/148123511): remove exportedDeps after cleaning up genrule 1070 if strings.HasSuffix(header.Base(), "-phony") { 1071 continue 1072 } 1073 ret = append(ret, header) 1074 } 1075 return ret 1076} 1077 1078// collectHeadersForSnapshot collects all exported headers from library. 1079// It globs header files in the source tree for exported include directories, 1080// and tracks generated header files separately. 1081// 1082// This is to be called from GenerateAndroidBuildActions, and then collected 1083// header files can be retrieved by snapshotHeaders(). 1084func (l *libraryDecorator) collectHeadersForSnapshot(ctx android.ModuleContext) { 1085 ret := android.Paths{} 1086 1087 // Headers in the source tree should be globbed. On the contrast, generated headers 1088 // can't be globbed, and they should be manually collected. 1089 // So, we first filter out intermediate directories (which contains generated headers) 1090 // from exported directories, and then glob headers under remaining directories. 1091 ret = append(ret, GlobHeadersForSnapshot(ctx, append(android.CopyOfPaths(l.flagExporter.dirs), l.flagExporter.systemDirs...))...) 1092 1093 // Collect generated headers 1094 ret = append(ret, GlobGeneratedHeadersForSnapshot(ctx, append(android.CopyOfPaths(l.flagExporter.headers), l.flagExporter.deps...))...) 1095 1096 l.collectedSnapshotHeaders = ret 1097} 1098 1099// This returns all exported header files, both generated ones and headers from source tree. 1100// collectHeadersForSnapshot() must be called before calling this. 1101func (l *libraryDecorator) snapshotHeaders() android.Paths { 1102 if l.collectedSnapshotHeaders == nil { 1103 panic("snapshotHeaders() must be called after collectHeadersForSnapshot()") 1104 } 1105 return l.collectedSnapshotHeaders 1106} 1107 1108// linkerProps returns the list of properties structs relevant for this library. (For example, if 1109// the library is cc_shared_library, then static-library properties are omitted.) 1110func (library *libraryDecorator) linkerProps() []interface{} { 1111 var props []interface{} 1112 props = append(props, library.baseLinker.linkerProps()...) 1113 props = append(props, 1114 &library.Properties, 1115 &library.MutatedProperties, 1116 &library.flagExporter.Properties, 1117 &library.stripper.StripProperties) 1118 1119 if library.MutatedProperties.BuildShared { 1120 props = append(props, &library.SharedProperties) 1121 } 1122 if library.MutatedProperties.BuildStatic { 1123 props = append(props, &library.StaticProperties) 1124 } 1125 1126 return props 1127} 1128 1129// linkerFlags takes a Flags struct and augments it to contain linker flags that are defined by this 1130// library, or that are implied by attributes of this library (such as whether this library is a 1131// shared library). 1132func (library *libraryDecorator) linkerFlags(ctx ModuleContext, flags Flags) Flags { 1133 flags = library.baseLinker.linkerFlags(ctx, flags) 1134 1135 // MinGW spits out warnings about -fPIC even for -fpie?!) being ignored because 1136 // all code is position independent, and then those warnings get promoted to 1137 // errors. 1138 if !ctx.Windows() { 1139 flags.Global.CFlags = append(flags.Global.CFlags, "-fPIC") 1140 } 1141 1142 if library.static() { 1143 flags.Local.CFlags = append(flags.Local.CFlags, library.StaticProperties.Static.Cflags...) 1144 } else if library.shared() { 1145 flags.Local.CFlags = append(flags.Local.CFlags, library.SharedProperties.Shared.Cflags...) 1146 } 1147 1148 if library.shared() { 1149 libName := library.getLibName(ctx) 1150 var f []string 1151 if ctx.toolchain().Bionic() { 1152 f = append(f, 1153 "-nostdlib", 1154 "-Wl,--gc-sections", 1155 ) 1156 } 1157 1158 if ctx.Darwin() { 1159 f = append(f, 1160 "-dynamiclib", 1161 "-install_name @rpath/"+libName+flags.Toolchain.ShlibSuffix(), 1162 ) 1163 if ctx.Arch().ArchType == android.X86 { 1164 f = append(f, 1165 "-read_only_relocs suppress", 1166 ) 1167 } 1168 } else { 1169 f = append(f, "-shared") 1170 if !ctx.Windows() { 1171 f = append(f, "-Wl,-soname,"+libName+flags.Toolchain.ShlibSuffix()) 1172 } 1173 } 1174 1175 flags.Global.LdFlags = append(flags.Global.LdFlags, f...) 1176 } 1177 1178 return flags 1179} 1180 1181// compilerFlags takes a Flags and augments it to contain compile flags from global values, 1182// per-target values, module type values, per-module Blueprints properties, extra flags from 1183// `flags`, and generated sources from `deps`. 1184func (library *libraryDecorator) compilerFlags(ctx ModuleContext, flags Flags, deps PathDeps) Flags { 1185 exportIncludeDirs := library.flagExporter.exportedIncludes(ctx) 1186 if len(exportIncludeDirs) > 0 { 1187 f := includeDirsToFlags(exportIncludeDirs) 1188 flags.Local.CommonFlags = append(flags.Local.CommonFlags, f) 1189 flags.Local.YasmFlags = append(flags.Local.YasmFlags, f) 1190 } 1191 1192 flags = library.baseCompiler.compilerFlags(ctx, flags, deps) 1193 if ctx.IsLlndk() { 1194 // LLNDK libraries ignore most of the properties on the cc_library and use the 1195 // LLNDK-specific properties instead. 1196 // Wipe all the module-local properties, leaving only the global properties. 1197 flags.Local = LocalOrGlobalFlags{} 1198 } 1199 if library.buildStubs() { 1200 // Remove -include <file> when compiling stubs. Otherwise, the force included 1201 // headers might cause conflicting types error with the symbols in the 1202 // generated stubs source code. e.g. 1203 // double acos(double); // in header 1204 // void acos() {} // in the generated source code 1205 removeInclude := func(flags []string) []string { 1206 ret := flags[:0] 1207 for _, f := range flags { 1208 if strings.HasPrefix(f, "-include ") { 1209 continue 1210 } 1211 ret = append(ret, f) 1212 } 1213 return ret 1214 } 1215 flags.Local.CommonFlags = removeInclude(flags.Local.CommonFlags) 1216 flags.Local.CFlags = removeInclude(flags.Local.CFlags) 1217 1218 flags = addStubLibraryCompilerFlags(flags) 1219 } 1220 return flags 1221} 1222 1223func (library *libraryDecorator) getHeaderAbiCheckerProperties(ctx android.BaseModuleContext) headerAbiCheckerProperties { 1224 m := ctx.Module().(*Module) 1225 variantProps := &library.Properties.Target.Platform.Header_abi_checker 1226 if m.InVendor() { 1227 variantProps = &library.Properties.Target.Vendor.Header_abi_checker 1228 } else if m.InProduct() { 1229 variantProps = &library.Properties.Target.Product.Header_abi_checker 1230 } 1231 props := library.Properties.Header_abi_checker 1232 err := proptools.AppendProperties(&props, variantProps, nil) 1233 if err != nil { 1234 ctx.ModuleErrorf("Cannot merge headerAbiCheckerProperties: %s", err.Error()) 1235 } 1236 return props 1237} 1238 1239func (library *libraryDecorator) compile(ctx ModuleContext, flags Flags, deps PathDeps) Objects { 1240 if ctx.IsLlndk() { 1241 // This is the vendor variant of an LLNDK library, build the LLNDK stubs. 1242 vndkVer := ctx.Module().(*Module).VndkVersion() 1243 if !inList(vndkVer, ctx.Config().PlatformVersionActiveCodenames()) || vndkVer == "" { 1244 // For non-enforcing devices, vndkVer is empty. Use "current" in that case, too. 1245 vndkVer = "current" 1246 } 1247 if library.stubsVersion() != "" { 1248 vndkVer = library.stubsVersion() 1249 } 1250 nativeAbiResult := parseNativeAbiDefinition(ctx, 1251 String(library.Properties.Llndk.Symbol_file), 1252 android.ApiLevelOrPanic(ctx, vndkVer), "--llndk") 1253 objs := compileStubLibrary(ctx, flags, nativeAbiResult.stubSrc) 1254 if !Bool(library.Properties.Llndk.Unversioned) { 1255 library.versionScriptPath = android.OptionalPathForPath( 1256 nativeAbiResult.versionScript) 1257 } 1258 return objs 1259 } 1260 if ctx.IsVendorPublicLibrary() { 1261 nativeAbiResult := parseNativeAbiDefinition(ctx, 1262 String(library.Properties.Vendor_public_library.Symbol_file), 1263 android.FutureApiLevel, "") 1264 objs := compileStubLibrary(ctx, flags, nativeAbiResult.stubSrc) 1265 if !Bool(library.Properties.Vendor_public_library.Unversioned) { 1266 library.versionScriptPath = android.OptionalPathForPath(nativeAbiResult.versionScript) 1267 } 1268 return objs 1269 } 1270 if library.buildStubs() { 1271 symbolFile := String(library.Properties.Stubs.Symbol_file) 1272 if symbolFile != "" && !strings.HasSuffix(symbolFile, ".map.txt") { 1273 ctx.PropertyErrorf("symbol_file", "%q doesn't have .map.txt suffix", symbolFile) 1274 return Objects{} 1275 } 1276 // b/239274367 --apex and --systemapi filters symbols tagged with # apex and # 1277 // systemapi, respectively. The former is for symbols defined in platform libraries 1278 // and the latter is for symbols defined in APEXes. 1279 // A single library can contain either # apex or # systemapi, but not both. 1280 // The stub generator (ndkstubgen) is additive, so passing _both_ of these to it should be a no-op. 1281 // However, having this distinction helps guard accidental 1282 // promotion or demotion of API and also helps the API review process b/191371676 1283 var flag string 1284 if ctx.Module().(android.ApexModule).NotInPlatform() { 1285 flag = "--apex" 1286 } else { 1287 flag = "--systemapi" 1288 } 1289 // b/184712170, unless the lib is an NDK library, exclude all public symbols from 1290 // the stub so that it is mandated that all symbols are explicitly marked with 1291 // either apex or systemapi. 1292 if !ctx.Module().(*Module).IsNdk(ctx.Config()) { 1293 flag = flag + " --no-ndk" 1294 } 1295 nativeAbiResult := parseNativeAbiDefinition(ctx, symbolFile, 1296 android.ApiLevelOrPanic(ctx, library.MutatedProperties.StubsVersion), flag) 1297 objs := compileStubLibrary(ctx, flags, nativeAbiResult.stubSrc) 1298 library.versionScriptPath = android.OptionalPathForPath( 1299 nativeAbiResult.versionScript) 1300 1301 // Parse symbol file to get API list for coverage 1302 if library.stubsVersion() == "current" && ctx.PrimaryArch() && !ctx.inRecovery() && !ctx.inProduct() && !ctx.inVendor() { 1303 library.apiListCoverageXmlPath = parseSymbolFileForAPICoverage(ctx, symbolFile) 1304 } 1305 1306 return objs 1307 } 1308 1309 if !library.buildShared() && !library.buildStatic() { 1310 if len(library.baseCompiler.Properties.Srcs) > 0 { 1311 ctx.PropertyErrorf("srcs", "cc_library_headers must not have any srcs") 1312 } 1313 if len(library.StaticProperties.Static.Srcs) > 0 { 1314 ctx.PropertyErrorf("static.srcs", "cc_library_headers must not have any srcs") 1315 } 1316 if len(library.SharedProperties.Shared.Srcs) > 0 { 1317 ctx.PropertyErrorf("shared.srcs", "cc_library_headers must not have any srcs") 1318 } 1319 return Objects{} 1320 } 1321 if library.sabi.shouldCreateSourceAbiDump() { 1322 exportIncludeDirs := library.flagExporter.exportedIncludes(ctx) 1323 var SourceAbiFlags []string 1324 for _, dir := range exportIncludeDirs.Strings() { 1325 SourceAbiFlags = append(SourceAbiFlags, "-I"+dir) 1326 } 1327 for _, reexportedInclude := range library.sabi.Properties.ReexportedIncludes { 1328 SourceAbiFlags = append(SourceAbiFlags, "-I"+reexportedInclude) 1329 } 1330 flags.SAbiFlags = SourceAbiFlags 1331 totalLength := len(library.baseCompiler.Properties.Srcs) + len(deps.GeneratedSources) + 1332 len(library.SharedProperties.Shared.Srcs) + len(library.StaticProperties.Static.Srcs) 1333 if totalLength > 0 { 1334 flags.SAbiDump = true 1335 } 1336 } 1337 objs := library.baseCompiler.compile(ctx, flags, deps) 1338 library.reuseObjects = objs 1339 buildFlags := flagsToBuilderFlags(flags) 1340 1341 if library.static() { 1342 srcs := android.PathsForModuleSrc(ctx, library.StaticProperties.Static.Srcs) 1343 objs = objs.Append(compileObjs(ctx, buildFlags, android.DeviceStaticLibrary, srcs, 1344 android.PathsForModuleSrc(ctx, library.StaticProperties.Static.Tidy_disabled_srcs), 1345 android.PathsForModuleSrc(ctx, library.StaticProperties.Static.Tidy_timeout_srcs), 1346 library.baseCompiler.pathDeps, library.baseCompiler.cFlagsDeps)) 1347 } else if library.shared() { 1348 srcs := android.PathsForModuleSrc(ctx, library.SharedProperties.Shared.Srcs) 1349 objs = objs.Append(compileObjs(ctx, buildFlags, android.DeviceSharedLibrary, srcs, 1350 android.PathsForModuleSrc(ctx, library.SharedProperties.Shared.Tidy_disabled_srcs), 1351 android.PathsForModuleSrc(ctx, library.SharedProperties.Shared.Tidy_timeout_srcs), 1352 library.baseCompiler.pathDeps, library.baseCompiler.cFlagsDeps)) 1353 } 1354 1355 return objs 1356} 1357 1358type libraryInterface interface { 1359 versionedInterface 1360 1361 static() bool 1362 shared() bool 1363 objs() Objects 1364 reuseObjs() Objects 1365 toc() android.OptionalPath 1366 1367 // Returns true if the build options for the module have selected a static or shared build 1368 buildStatic() bool 1369 buildShared() bool 1370 1371 // Sets whether a specific variant is static or shared 1372 setStatic() 1373 setShared() 1374 1375 // Gets the ABI properties for vendor, product, or platform variant 1376 getHeaderAbiCheckerProperties(ctx android.BaseModuleContext) headerAbiCheckerProperties 1377 1378 // Write LOCAL_ADDITIONAL_DEPENDENCIES for ABI diff 1379 androidMkWriteAdditionalDependenciesForSourceAbiDiff(w io.Writer) 1380 1381 availableFor(string) bool 1382 1383 getAPIListCoverageXMLPath() android.ModuleOutPath 1384 1385 installable() *bool 1386} 1387 1388type versionedInterface interface { 1389 buildStubs() bool 1390 setBuildStubs(isLatest bool) 1391 hasStubsVariants() bool 1392 isStubsImplementationRequired() bool 1393 setStubsVersion(string) 1394 stubsVersion() string 1395 1396 stubsVersions(ctx android.BaseMutatorContext) []string 1397 setAllStubsVersions([]string) 1398 allStubsVersions() []string 1399 1400 implementationModuleName(name string) string 1401 hasLLNDKStubs() bool 1402 hasLLNDKHeaders() bool 1403 hasVendorPublicLibrary() bool 1404} 1405 1406var _ libraryInterface = (*libraryDecorator)(nil) 1407var _ versionedInterface = (*libraryDecorator)(nil) 1408 1409func (library *libraryDecorator) getLibNameHelper(baseModuleName string, inVendor bool, inProduct bool) string { 1410 name := library.libName 1411 if name == "" { 1412 name = String(library.Properties.Stem) 1413 if name == "" { 1414 name = baseModuleName 1415 } 1416 } 1417 1418 suffix := "" 1419 if inVendor { 1420 suffix = String(library.Properties.Target.Vendor.Suffix) 1421 } else if inProduct { 1422 suffix = String(library.Properties.Target.Product.Suffix) 1423 } 1424 if suffix == "" { 1425 suffix = String(library.Properties.Suffix) 1426 } 1427 1428 return name + suffix 1429} 1430 1431// getLibName returns the actual canonical name of the library (the name which 1432// should be passed to the linker via linker flags). 1433func (library *libraryDecorator) getLibName(ctx BaseModuleContext) string { 1434 name := library.getLibNameHelper(ctx.baseModuleName(), ctx.inVendor(), ctx.inProduct()) 1435 1436 if ctx.IsVndkExt() { 1437 // vndk-ext lib should have the same name with original lib 1438 ctx.VisitDirectDepsWithTag(vndkExtDepTag, func(module android.Module) { 1439 originalName := module.(*Module).outputFile.Path() 1440 name = strings.TrimSuffix(originalName.Base(), originalName.Ext()) 1441 }) 1442 } 1443 1444 if ctx.Host() && Bool(library.Properties.Unique_host_soname) { 1445 if !strings.HasSuffix(name, "-host") { 1446 name = name + "-host" 1447 } 1448 } 1449 1450 return name 1451} 1452 1453var versioningMacroNamesListMutex sync.Mutex 1454 1455func (library *libraryDecorator) linkerInit(ctx BaseModuleContext) { 1456 location := InstallInSystem 1457 if library.baseLinker.sanitize.inSanitizerDir() { 1458 location = InstallInSanitizerDir 1459 } 1460 library.baseInstaller.location = location 1461 library.baseLinker.linkerInit(ctx) 1462 // Let baseLinker know whether this variant is for stubs or not, so that 1463 // it can omit things that are not required for linking stubs. 1464 library.baseLinker.dynamicProperties.BuildStubs = library.buildStubs() 1465 1466 if library.buildStubs() { 1467 macroNames := versioningMacroNamesList(ctx.Config()) 1468 myName := versioningMacroName(ctx.ModuleName()) 1469 versioningMacroNamesListMutex.Lock() 1470 defer versioningMacroNamesListMutex.Unlock() 1471 if (*macroNames)[myName] == "" { 1472 (*macroNames)[myName] = ctx.ModuleName() 1473 } else if (*macroNames)[myName] != ctx.ModuleName() { 1474 ctx.ModuleErrorf("Macro name %q for versioning conflicts with macro name from module %q ", myName, (*macroNames)[myName]) 1475 } 1476 } 1477} 1478 1479func (library *libraryDecorator) compilerDeps(ctx DepsContext, deps Deps) Deps { 1480 if ctx.IsLlndk() { 1481 // LLNDK libraries ignore most of the properties on the cc_library and use the 1482 // LLNDK-specific properties instead. 1483 return deps 1484 } 1485 1486 deps = library.baseCompiler.compilerDeps(ctx, deps) 1487 1488 return deps 1489} 1490 1491func (library *libraryDecorator) linkerDeps(ctx DepsContext, deps Deps) Deps { 1492 if ctx.IsLlndk() { 1493 // LLNDK libraries ignore most of the properties on the cc_library and use the 1494 // LLNDK-specific properties instead. 1495 deps.HeaderLibs = append([]string(nil), library.Properties.Llndk.Export_llndk_headers...) 1496 deps.ReexportHeaderLibHeaders = append([]string(nil), library.Properties.Llndk.Export_llndk_headers...) 1497 return deps 1498 } 1499 if ctx.IsVendorPublicLibrary() { 1500 headers := library.Properties.Vendor_public_library.Export_public_headers 1501 deps.HeaderLibs = append([]string(nil), headers...) 1502 deps.ReexportHeaderLibHeaders = append([]string(nil), headers...) 1503 return deps 1504 } 1505 1506 if library.static() { 1507 // Compare with nil because an empty list needs to be propagated. 1508 if library.StaticProperties.Static.System_shared_libs != nil { 1509 library.baseLinker.Properties.System_shared_libs = library.StaticProperties.Static.System_shared_libs 1510 } 1511 } else if library.shared() { 1512 // Compare with nil because an empty list needs to be propagated. 1513 if library.SharedProperties.Shared.System_shared_libs != nil { 1514 library.baseLinker.Properties.System_shared_libs = library.SharedProperties.Shared.System_shared_libs 1515 } 1516 } 1517 1518 deps = library.baseLinker.linkerDeps(ctx, deps) 1519 1520 if library.static() { 1521 deps.WholeStaticLibs = append(deps.WholeStaticLibs, 1522 library.StaticProperties.Static.Whole_static_libs...) 1523 deps.StaticLibs = append(deps.StaticLibs, library.StaticProperties.Static.Static_libs...) 1524 deps.SharedLibs = append(deps.SharedLibs, library.StaticProperties.Static.Shared_libs...) 1525 1526 deps.ReexportSharedLibHeaders = append(deps.ReexportSharedLibHeaders, library.StaticProperties.Static.Export_shared_lib_headers...) 1527 deps.ReexportStaticLibHeaders = append(deps.ReexportStaticLibHeaders, library.StaticProperties.Static.Export_static_lib_headers...) 1528 } else if library.shared() { 1529 if library.baseLinker.Properties.crt() { 1530 deps.CrtBegin = append(deps.CrtBegin, ctx.toolchain().CrtBeginSharedLibrary()...) 1531 deps.CrtEnd = append(deps.CrtEnd, ctx.toolchain().CrtEndSharedLibrary()...) 1532 } 1533 deps.WholeStaticLibs = append(deps.WholeStaticLibs, library.SharedProperties.Shared.Whole_static_libs...) 1534 deps.StaticLibs = append(deps.StaticLibs, library.SharedProperties.Shared.Static_libs...) 1535 deps.SharedLibs = append(deps.SharedLibs, library.SharedProperties.Shared.Shared_libs...) 1536 1537 deps.ReexportSharedLibHeaders = append(deps.ReexportSharedLibHeaders, library.SharedProperties.Shared.Export_shared_lib_headers...) 1538 deps.ReexportStaticLibHeaders = append(deps.ReexportStaticLibHeaders, library.SharedProperties.Shared.Export_static_lib_headers...) 1539 } 1540 if ctx.inVendor() { 1541 deps.WholeStaticLibs = removeListFromList(deps.WholeStaticLibs, library.baseLinker.Properties.Target.Vendor.Exclude_static_libs) 1542 deps.SharedLibs = removeListFromList(deps.SharedLibs, library.baseLinker.Properties.Target.Vendor.Exclude_shared_libs) 1543 deps.StaticLibs = removeListFromList(deps.StaticLibs, library.baseLinker.Properties.Target.Vendor.Exclude_static_libs) 1544 deps.ReexportSharedLibHeaders = removeListFromList(deps.ReexportSharedLibHeaders, library.baseLinker.Properties.Target.Vendor.Exclude_shared_libs) 1545 deps.ReexportStaticLibHeaders = removeListFromList(deps.ReexportStaticLibHeaders, library.baseLinker.Properties.Target.Vendor.Exclude_static_libs) 1546 } 1547 if ctx.inProduct() { 1548 deps.WholeStaticLibs = removeListFromList(deps.WholeStaticLibs, library.baseLinker.Properties.Target.Product.Exclude_static_libs) 1549 deps.SharedLibs = removeListFromList(deps.SharedLibs, library.baseLinker.Properties.Target.Product.Exclude_shared_libs) 1550 deps.StaticLibs = removeListFromList(deps.StaticLibs, library.baseLinker.Properties.Target.Product.Exclude_static_libs) 1551 deps.ReexportSharedLibHeaders = removeListFromList(deps.ReexportSharedLibHeaders, library.baseLinker.Properties.Target.Product.Exclude_shared_libs) 1552 deps.ReexportStaticLibHeaders = removeListFromList(deps.ReexportStaticLibHeaders, library.baseLinker.Properties.Target.Product.Exclude_static_libs) 1553 } 1554 if ctx.inRecovery() { 1555 deps.WholeStaticLibs = removeListFromList(deps.WholeStaticLibs, library.baseLinker.Properties.Target.Recovery.Exclude_static_libs) 1556 deps.SharedLibs = removeListFromList(deps.SharedLibs, library.baseLinker.Properties.Target.Recovery.Exclude_shared_libs) 1557 deps.StaticLibs = removeListFromList(deps.StaticLibs, library.baseLinker.Properties.Target.Recovery.Exclude_static_libs) 1558 deps.ReexportSharedLibHeaders = removeListFromList(deps.ReexportSharedLibHeaders, library.baseLinker.Properties.Target.Recovery.Exclude_shared_libs) 1559 deps.ReexportStaticLibHeaders = removeListFromList(deps.ReexportStaticLibHeaders, library.baseLinker.Properties.Target.Recovery.Exclude_static_libs) 1560 } 1561 if ctx.inRamdisk() { 1562 deps.WholeStaticLibs = removeListFromList(deps.WholeStaticLibs, library.baseLinker.Properties.Target.Ramdisk.Exclude_static_libs) 1563 deps.SharedLibs = removeListFromList(deps.SharedLibs, library.baseLinker.Properties.Target.Ramdisk.Exclude_shared_libs) 1564 deps.StaticLibs = removeListFromList(deps.StaticLibs, library.baseLinker.Properties.Target.Ramdisk.Exclude_static_libs) 1565 deps.ReexportSharedLibHeaders = removeListFromList(deps.ReexportSharedLibHeaders, library.baseLinker.Properties.Target.Ramdisk.Exclude_shared_libs) 1566 deps.ReexportStaticLibHeaders = removeListFromList(deps.ReexportStaticLibHeaders, library.baseLinker.Properties.Target.Ramdisk.Exclude_static_libs) 1567 } 1568 if ctx.inVendorRamdisk() { 1569 deps.WholeStaticLibs = removeListFromList(deps.WholeStaticLibs, library.baseLinker.Properties.Target.Vendor_ramdisk.Exclude_static_libs) 1570 deps.SharedLibs = removeListFromList(deps.SharedLibs, library.baseLinker.Properties.Target.Vendor_ramdisk.Exclude_shared_libs) 1571 deps.StaticLibs = removeListFromList(deps.StaticLibs, library.baseLinker.Properties.Target.Vendor_ramdisk.Exclude_static_libs) 1572 deps.ReexportSharedLibHeaders = removeListFromList(deps.ReexportSharedLibHeaders, library.baseLinker.Properties.Target.Vendor_ramdisk.Exclude_shared_libs) 1573 deps.ReexportStaticLibHeaders = removeListFromList(deps.ReexportStaticLibHeaders, library.baseLinker.Properties.Target.Vendor_ramdisk.Exclude_static_libs) 1574 } 1575 1576 return deps 1577} 1578 1579func (library *libraryDecorator) linkerSpecifiedDeps(specifiedDeps specifiedDeps) specifiedDeps { 1580 specifiedDeps = library.baseLinker.linkerSpecifiedDeps(specifiedDeps) 1581 var properties StaticOrSharedProperties 1582 if library.static() { 1583 properties = library.StaticProperties.Static 1584 } else if library.shared() { 1585 properties = library.SharedProperties.Shared 1586 } 1587 1588 specifiedDeps.sharedLibs = append(specifiedDeps.sharedLibs, properties.Shared_libs...) 1589 1590 // Must distinguish nil and [] in system_shared_libs - ensure that [] in 1591 // either input list doesn't come out as nil. 1592 if specifiedDeps.systemSharedLibs == nil { 1593 specifiedDeps.systemSharedLibs = properties.System_shared_libs 1594 } else { 1595 specifiedDeps.systemSharedLibs = append(specifiedDeps.systemSharedLibs, properties.System_shared_libs...) 1596 } 1597 1598 specifiedDeps.sharedLibs = android.FirstUniqueStrings(specifiedDeps.sharedLibs) 1599 if len(specifiedDeps.systemSharedLibs) > 0 { 1600 // Skip this if systemSharedLibs is either nil or [], to ensure they are 1601 // retained. 1602 specifiedDeps.systemSharedLibs = android.FirstUniqueStrings(specifiedDeps.systemSharedLibs) 1603 } 1604 return specifiedDeps 1605} 1606 1607func (library *libraryDecorator) linkStatic(ctx ModuleContext, 1608 flags Flags, deps PathDeps, objs Objects) android.Path { 1609 1610 library.objects = deps.WholeStaticLibObjs.Copy() 1611 library.objects = library.objects.Append(objs) 1612 library.wholeStaticLibsFromPrebuilts = android.CopyOfPaths(deps.WholeStaticLibsFromPrebuilts) 1613 1614 fileName := ctx.ModuleName() + staticLibraryExtension 1615 outputFile := android.PathForModuleOut(ctx, fileName) 1616 builderFlags := flagsToBuilderFlags(flags) 1617 1618 if Bool(library.baseLinker.Properties.Use_version_lib) { 1619 if ctx.Host() { 1620 versionedOutputFile := outputFile 1621 outputFile = android.PathForModuleOut(ctx, "unversioned", fileName) 1622 library.injectVersionSymbol(ctx, outputFile, versionedOutputFile) 1623 } else { 1624 versionedOutputFile := android.PathForModuleOut(ctx, "versioned", fileName) 1625 library.distFile = versionedOutputFile 1626 library.injectVersionSymbol(ctx, outputFile, versionedOutputFile) 1627 } 1628 } 1629 1630 transformObjToStaticLib(ctx, library.objects.objFiles, deps.WholeStaticLibsFromPrebuilts, builderFlags, outputFile, nil, objs.tidyDepFiles) 1631 1632 library.coverageOutputFile = transformCoverageFilesToZip(ctx, library.objects, ctx.ModuleName()) 1633 1634 ctx.CheckbuildFile(outputFile) 1635 1636 if library.static() { 1637 ctx.SetProvider(StaticLibraryInfoProvider, StaticLibraryInfo{ 1638 StaticLibrary: outputFile, 1639 ReuseObjects: library.reuseObjects, 1640 Objects: library.objects, 1641 WholeStaticLibsFromPrebuilts: library.wholeStaticLibsFromPrebuilts, 1642 1643 TransitiveStaticLibrariesForOrdering: android.NewDepSetBuilder(android.TOPOLOGICAL). 1644 Direct(outputFile). 1645 Transitive(deps.TranstiveStaticLibrariesForOrdering). 1646 Build(), 1647 }) 1648 } 1649 1650 if library.header() { 1651 ctx.SetProvider(HeaderLibraryInfoProvider, HeaderLibraryInfo{}) 1652 } 1653 1654 return outputFile 1655} 1656 1657func ndkSharedLibDeps(ctx ModuleContext) android.Paths { 1658 if ctx.Module().(*Module).IsSdkVariant() { 1659 // The NDK sysroot timestamp file depends on all the NDK 1660 // sysroot header and shared library files. 1661 return android.Paths{getNdkBaseTimestampFile(ctx)} 1662 } 1663 return nil 1664} 1665 1666func (library *libraryDecorator) linkShared(ctx ModuleContext, 1667 flags Flags, deps PathDeps, objs Objects) android.Path { 1668 1669 var linkerDeps android.Paths 1670 linkerDeps = append(linkerDeps, flags.LdFlagsDeps...) 1671 linkerDeps = append(linkerDeps, ndkSharedLibDeps(ctx)...) 1672 1673 unexportedSymbols := ctx.ExpandOptionalSource(library.Properties.Unexported_symbols_list, "unexported_symbols_list") 1674 forceNotWeakSymbols := ctx.ExpandOptionalSource(library.Properties.Force_symbols_not_weak_list, "force_symbols_not_weak_list") 1675 forceWeakSymbols := ctx.ExpandOptionalSource(library.Properties.Force_symbols_weak_list, "force_symbols_weak_list") 1676 if !ctx.Darwin() { 1677 if unexportedSymbols.Valid() { 1678 ctx.PropertyErrorf("unexported_symbols_list", "Only supported on Darwin") 1679 } 1680 if forceNotWeakSymbols.Valid() { 1681 ctx.PropertyErrorf("force_symbols_not_weak_list", "Only supported on Darwin") 1682 } 1683 if forceWeakSymbols.Valid() { 1684 ctx.PropertyErrorf("force_symbols_weak_list", "Only supported on Darwin") 1685 } 1686 } else { 1687 if unexportedSymbols.Valid() { 1688 flags.Local.LdFlags = append(flags.Local.LdFlags, "-Wl,-unexported_symbols_list,"+unexportedSymbols.String()) 1689 linkerDeps = append(linkerDeps, unexportedSymbols.Path()) 1690 } 1691 if forceNotWeakSymbols.Valid() { 1692 flags.Local.LdFlags = append(flags.Local.LdFlags, "-Wl,-force_symbols_not_weak_list,"+forceNotWeakSymbols.String()) 1693 linkerDeps = append(linkerDeps, forceNotWeakSymbols.Path()) 1694 } 1695 if forceWeakSymbols.Valid() { 1696 flags.Local.LdFlags = append(flags.Local.LdFlags, "-Wl,-force_symbols_weak_list,"+forceWeakSymbols.String()) 1697 linkerDeps = append(linkerDeps, forceWeakSymbols.Path()) 1698 } 1699 } 1700 if library.versionScriptPath.Valid() { 1701 linkerScriptFlags := "-Wl,--version-script," + library.versionScriptPath.String() 1702 flags.Local.LdFlags = append(flags.Local.LdFlags, linkerScriptFlags) 1703 linkerDeps = append(linkerDeps, library.versionScriptPath.Path()) 1704 } 1705 1706 fileName := library.getLibName(ctx) + flags.Toolchain.ShlibSuffix() 1707 outputFile := android.PathForModuleOut(ctx, fileName) 1708 unstrippedOutputFile := outputFile 1709 1710 var implicitOutputs android.WritablePaths 1711 if ctx.Windows() { 1712 importLibraryPath := android.PathForModuleOut(ctx, pathtools.ReplaceExtension(fileName, "lib")) 1713 1714 flags.Local.LdFlags = append(flags.Local.LdFlags, "-Wl,--out-implib="+importLibraryPath.String()) 1715 implicitOutputs = append(implicitOutputs, importLibraryPath) 1716 } 1717 1718 builderFlags := flagsToBuilderFlags(flags) 1719 1720 if ctx.Darwin() && deps.DarwinSecondArchOutput.Valid() { 1721 fatOutputFile := outputFile 1722 outputFile = android.PathForModuleOut(ctx, "pre-fat", fileName) 1723 transformDarwinUniversalBinary(ctx, fatOutputFile, outputFile, deps.DarwinSecondArchOutput.Path()) 1724 } 1725 1726 // Optimize out relinking against shared libraries whose interface hasn't changed by 1727 // depending on a table of contents file instead of the library itself. 1728 tocFile := outputFile.ReplaceExtension(ctx, flags.Toolchain.ShlibSuffix()[1:]+".toc") 1729 library.tocFile = android.OptionalPathForPath(tocFile) 1730 TransformSharedObjectToToc(ctx, outputFile, tocFile) 1731 1732 stripFlags := flagsToStripFlags(flags) 1733 needsStrip := library.stripper.NeedsStrip(ctx) 1734 if library.buildStubs() { 1735 // No need to strip stubs libraries 1736 needsStrip = false 1737 } 1738 if needsStrip { 1739 if ctx.Darwin() { 1740 stripFlags.StripUseGnuStrip = true 1741 } 1742 strippedOutputFile := outputFile 1743 outputFile = android.PathForModuleOut(ctx, "unstripped", fileName) 1744 library.stripper.StripExecutableOrSharedLib(ctx, outputFile, strippedOutputFile, stripFlags) 1745 } 1746 library.unstrippedOutputFile = outputFile 1747 1748 outputFile = maybeInjectBoringSSLHash(ctx, outputFile, library.Properties.Inject_bssl_hash, fileName) 1749 1750 if Bool(library.baseLinker.Properties.Use_version_lib) { 1751 if ctx.Host() { 1752 versionedOutputFile := outputFile 1753 outputFile = android.PathForModuleOut(ctx, "unversioned", fileName) 1754 library.injectVersionSymbol(ctx, outputFile, versionedOutputFile) 1755 } else { 1756 versionedOutputFile := android.PathForModuleOut(ctx, "versioned", fileName) 1757 library.distFile = versionedOutputFile 1758 1759 if library.stripper.NeedsStrip(ctx) { 1760 out := android.PathForModuleOut(ctx, "versioned-stripped", fileName) 1761 library.distFile = out 1762 library.stripper.StripExecutableOrSharedLib(ctx, versionedOutputFile, out, stripFlags) 1763 } 1764 1765 library.injectVersionSymbol(ctx, outputFile, versionedOutputFile) 1766 } 1767 } 1768 1769 sharedLibs := deps.EarlySharedLibs 1770 sharedLibs = append(sharedLibs, deps.SharedLibs...) 1771 sharedLibs = append(sharedLibs, deps.LateSharedLibs...) 1772 1773 linkerDeps = append(linkerDeps, deps.EarlySharedLibsDeps...) 1774 linkerDeps = append(linkerDeps, deps.SharedLibsDeps...) 1775 linkerDeps = append(linkerDeps, deps.LateSharedLibsDeps...) 1776 transformObjToDynamicBinary(ctx, objs.objFiles, sharedLibs, 1777 deps.StaticLibs, deps.LateStaticLibs, deps.WholeStaticLibs, 1778 linkerDeps, deps.CrtBegin, deps.CrtEnd, false, builderFlags, outputFile, implicitOutputs, objs.tidyDepFiles) 1779 1780 objs.coverageFiles = append(objs.coverageFiles, deps.StaticLibObjs.coverageFiles...) 1781 objs.coverageFiles = append(objs.coverageFiles, deps.WholeStaticLibObjs.coverageFiles...) 1782 objs.sAbiDumpFiles = append(objs.sAbiDumpFiles, deps.StaticLibObjs.sAbiDumpFiles...) 1783 objs.sAbiDumpFiles = append(objs.sAbiDumpFiles, deps.WholeStaticLibObjs.sAbiDumpFiles...) 1784 1785 library.coverageOutputFile = transformCoverageFilesToZip(ctx, objs, library.getLibName(ctx)) 1786 library.linkSAbiDumpFiles(ctx, objs, fileName, unstrippedOutputFile) 1787 1788 var transitiveStaticLibrariesForOrdering *android.DepSet 1789 if static := ctx.GetDirectDepsWithTag(staticVariantTag); len(static) > 0 { 1790 s := ctx.OtherModuleProvider(static[0], StaticLibraryInfoProvider).(StaticLibraryInfo) 1791 transitiveStaticLibrariesForOrdering = s.TransitiveStaticLibrariesForOrdering 1792 } 1793 1794 ctx.SetProvider(SharedLibraryInfoProvider, SharedLibraryInfo{ 1795 TableOfContents: android.OptionalPathForPath(tocFile), 1796 SharedLibrary: unstrippedOutputFile, 1797 TransitiveStaticLibrariesForOrdering: transitiveStaticLibrariesForOrdering, 1798 Target: ctx.Target(), 1799 }) 1800 1801 addStubDependencyProviders(ctx) 1802 1803 return unstrippedOutputFile 1804} 1805 1806func addStubDependencyProviders(ctx ModuleContext) { 1807 stubs := ctx.GetDirectDepsWithTag(stubImplDepTag) 1808 if len(stubs) > 0 { 1809 var stubsInfo []SharedStubLibrary 1810 for _, stub := range stubs { 1811 stubInfo := ctx.OtherModuleProvider(stub, SharedLibraryInfoProvider).(SharedLibraryInfo) 1812 flagInfo := ctx.OtherModuleProvider(stub, FlagExporterInfoProvider).(FlagExporterInfo) 1813 stubsInfo = append(stubsInfo, SharedStubLibrary{ 1814 Version: moduleLibraryInterface(stub).stubsVersion(), 1815 SharedLibraryInfo: stubInfo, 1816 FlagExporterInfo: flagInfo, 1817 }) 1818 } 1819 ctx.SetProvider(SharedLibraryStubsProvider, SharedLibraryStubsInfo{ 1820 SharedStubLibraries: stubsInfo, 1821 IsLLNDK: ctx.IsLlndk(), 1822 }) 1823 } 1824} 1825 1826func (library *libraryDecorator) unstrippedOutputFilePath() android.Path { 1827 return library.unstrippedOutputFile 1828} 1829 1830func (library *libraryDecorator) disableStripping() { 1831 library.stripper.StripProperties.Strip.None = BoolPtr(true) 1832} 1833 1834func (library *libraryDecorator) nativeCoverage() bool { 1835 if library.header() || library.buildStubs() { 1836 return false 1837 } 1838 return true 1839} 1840 1841func (library *libraryDecorator) coverageOutputFilePath() android.OptionalPath { 1842 return library.coverageOutputFile 1843} 1844 1845func getRefAbiDumpFile(ctx android.ModuleInstallPathContext, 1846 versionedDumpDir, fileName string) android.OptionalPath { 1847 1848 currentArchType := ctx.Arch().ArchType 1849 primaryArchType := ctx.Config().DevicePrimaryArchType() 1850 archName := currentArchType.String() 1851 if currentArchType != primaryArchType { 1852 archName += "_" + primaryArchType.String() 1853 } 1854 1855 return android.ExistentPathForSource(ctx, versionedDumpDir, archName, "source-based", 1856 fileName+".lsdump") 1857} 1858 1859func getRefAbiDumpDir(isNdk, isVndk bool) string { 1860 var dirName string 1861 if isNdk { 1862 dirName = "ndk" 1863 } else if isVndk { 1864 dirName = "vndk" 1865 } else { 1866 dirName = "platform" 1867 } 1868 return filepath.Join("prebuilts", "abi-dumps", dirName) 1869} 1870 1871func prevRefAbiDumpVersion(ctx ModuleContext, dumpDir string) int { 1872 sdkVersionInt := ctx.Config().PlatformSdkVersion().FinalInt() 1873 sdkVersionStr := ctx.Config().PlatformSdkVersion().String() 1874 1875 if ctx.Config().PlatformSdkFinal() { 1876 return sdkVersionInt - 1 1877 } else { 1878 // The platform SDK version can be upgraded before finalization while the corresponding abi dumps hasn't 1879 // been generated. Thus the Cross-Version Check chooses PLATFORM_SDK_VERION - 1 as previous version. 1880 // This situation could be identified by checking the existence of the PLATFORM_SDK_VERION dump directory. 1881 versionedDumpDir := android.ExistentPathForSource(ctx, dumpDir, sdkVersionStr) 1882 if versionedDumpDir.Valid() { 1883 return sdkVersionInt 1884 } else { 1885 return sdkVersionInt - 1 1886 } 1887 } 1888} 1889 1890func currRefAbiDumpVersion(ctx ModuleContext, isVndk bool) string { 1891 if isVndk { 1892 // Each version of VNDK is independent, so follow the VNDK version which is the codename or PLATFORM_SDK_VERSION. 1893 return ctx.Module().(*Module).VndkVersion() 1894 } else if ctx.Config().PlatformSdkFinal() { 1895 // After sdk finalization, the ABI of the latest API level must be consistent with the source code, 1896 // so choose PLATFORM_SDK_VERSION as the current version. 1897 return ctx.Config().PlatformSdkVersion().String() 1898 } else { 1899 return "current" 1900 } 1901} 1902 1903// sourceAbiDiff registers a build statement to compare linked sAbi dump files (.lsdump). 1904func (library *libraryDecorator) sourceAbiDiff(ctx android.ModuleContext, referenceDump android.Path, 1905 baseName, nameExt string, isLlndkOrNdk, allowExtensions bool, 1906 sourceVersion, errorMessage string) { 1907 1908 sourceDump := library.sAbiOutputFile.Path() 1909 1910 extraFlags := []string{"-target-version", sourceVersion} 1911 headerAbiChecker := library.getHeaderAbiCheckerProperties(ctx) 1912 if Bool(headerAbiChecker.Check_all_apis) { 1913 extraFlags = append(extraFlags, "-check-all-apis") 1914 } else { 1915 extraFlags = append(extraFlags, 1916 "-allow-unreferenced-changes", 1917 "-allow-unreferenced-elf-symbol-changes") 1918 } 1919 if isLlndkOrNdk { 1920 extraFlags = append(extraFlags, "-consider-opaque-types-different") 1921 } 1922 if allowExtensions { 1923 extraFlags = append(extraFlags, "-allow-extensions") 1924 } 1925 extraFlags = append(extraFlags, headerAbiChecker.Diff_flags...) 1926 1927 library.sAbiDiff = append( 1928 library.sAbiDiff, 1929 transformAbiDumpToAbiDiff(ctx, sourceDump, referenceDump, 1930 baseName, nameExt, extraFlags, errorMessage)) 1931} 1932 1933func (library *libraryDecorator) crossVersionAbiDiff(ctx android.ModuleContext, referenceDump android.Path, 1934 baseName string, isLlndkOrNdk bool, sourceVersion, prevVersion string) { 1935 1936 errorMessage := "error: Please follow https://android.googlesource.com/platform/development/+/master/vndk/tools/header-checker/README.md#configure-cross_version-abi-check to resolve the ABI difference between your source code and version " + prevVersion + "." 1937 1938 library.sourceAbiDiff(ctx, referenceDump, baseName, prevVersion, 1939 isLlndkOrNdk, true /* allowExtensions */, sourceVersion, errorMessage) 1940} 1941 1942func (library *libraryDecorator) sameVersionAbiDiff(ctx android.ModuleContext, referenceDump android.Path, 1943 baseName string, isLlndkOrNdk, allowExtensions bool) { 1944 1945 libName := strings.TrimSuffix(baseName, filepath.Ext(baseName)) 1946 errorMessage := "error: Please update ABI references with: $$ANDROID_BUILD_TOP/development/vndk/tools/header-checker/utils/create_reference_dumps.py -l " + libName 1947 1948 library.sourceAbiDiff(ctx, referenceDump, baseName, "", 1949 isLlndkOrNdk, allowExtensions, "current", errorMessage) 1950} 1951 1952func (library *libraryDecorator) optInAbiDiff(ctx android.ModuleContext, referenceDump android.Path, 1953 baseName, nameExt string, isLlndkOrNdk bool, refDumpDir string) { 1954 1955 libName := strings.TrimSuffix(baseName, filepath.Ext(baseName)) 1956 errorMessage := "error: Please update ABI references with: $$ANDROID_BUILD_TOP/development/vndk/tools/header-checker/utils/create_reference_dumps.py -l " + libName + " -ref-dump-dir $$ANDROID_BUILD_TOP/" + refDumpDir 1957 1958 library.sourceAbiDiff(ctx, referenceDump, baseName, nameExt, 1959 isLlndkOrNdk, false /* allowExtensions */, "current", errorMessage) 1960} 1961 1962func (library *libraryDecorator) linkSAbiDumpFiles(ctx ModuleContext, objs Objects, fileName string, soFile android.Path) { 1963 if library.sabi.shouldCreateSourceAbiDump() { 1964 exportIncludeDirs := library.flagExporter.exportedIncludes(ctx) 1965 var SourceAbiFlags []string 1966 for _, dir := range exportIncludeDirs.Strings() { 1967 SourceAbiFlags = append(SourceAbiFlags, "-I"+dir) 1968 } 1969 for _, reexportedInclude := range library.sabi.Properties.ReexportedIncludes { 1970 SourceAbiFlags = append(SourceAbiFlags, "-I"+reexportedInclude) 1971 } 1972 exportedHeaderFlags := strings.Join(SourceAbiFlags, " ") 1973 headerAbiChecker := library.getHeaderAbiCheckerProperties(ctx) 1974 library.sAbiOutputFile = transformDumpToLinkedDump(ctx, objs.sAbiDumpFiles, soFile, fileName, exportedHeaderFlags, 1975 android.OptionalPathForModuleSrc(ctx, library.symbolFileForAbiCheck(ctx)), 1976 headerAbiChecker.Exclude_symbol_versions, 1977 headerAbiChecker.Exclude_symbol_tags) 1978 1979 addLsdumpPath(classifySourceAbiDump(ctx) + ":" + library.sAbiOutputFile.String()) 1980 1981 // The logic must be consistent with classifySourceAbiDump. 1982 isVndk := ctx.useVndk() && ctx.isVndk() 1983 isNdk := ctx.isNdk(ctx.Config()) 1984 isLlndk := ctx.isImplementationForLLNDKPublic() 1985 dumpDir := getRefAbiDumpDir(isNdk, isVndk) 1986 binderBitness := ctx.DeviceConfig().BinderBitness() 1987 // If NDK or PLATFORM library, check against previous version ABI. 1988 if !isVndk { 1989 prevVersionInt := prevRefAbiDumpVersion(ctx, dumpDir) 1990 prevVersion := strconv.Itoa(prevVersionInt) 1991 prevDumpDir := filepath.Join(dumpDir, prevVersion, binderBitness) 1992 prevDumpFile := getRefAbiDumpFile(ctx, prevDumpDir, fileName) 1993 if prevDumpFile.Valid() { 1994 library.crossVersionAbiDiff(ctx, prevDumpFile.Path(), 1995 fileName, isLlndk || isNdk, 1996 strconv.Itoa(prevVersionInt+1), prevVersion) 1997 } 1998 } 1999 // Check against the current version. 2000 currVersion := currRefAbiDumpVersion(ctx, isVndk) 2001 currDumpDir := filepath.Join(dumpDir, currVersion, binderBitness) 2002 currDumpFile := getRefAbiDumpFile(ctx, currDumpDir, fileName) 2003 if currDumpFile.Valid() { 2004 library.sameVersionAbiDiff(ctx, currDumpFile.Path(), 2005 fileName, isLlndk || isNdk, ctx.IsVndkExt()) 2006 } 2007 // Check against the opt-in reference dumps. 2008 for i, optInDumpDir := range headerAbiChecker.Ref_dump_dirs { 2009 optInDumpDirPath := android.PathForModuleSrc(ctx, optInDumpDir) 2010 // Ref_dump_dirs are not versioned. 2011 // They do not contain subdir for binder bitness because 64-bit binder has been mandatory. 2012 optInDumpFile := getRefAbiDumpFile(ctx, optInDumpDirPath.String(), fileName) 2013 if !optInDumpFile.Valid() { 2014 continue 2015 } 2016 library.optInAbiDiff(ctx, optInDumpFile.Path(), 2017 fileName, "opt"+strconv.Itoa(i), isLlndk || isNdk, 2018 optInDumpDirPath.String()) 2019 } 2020 } 2021} 2022 2023func processLLNDKHeaders(ctx ModuleContext, srcHeaderDir string, outDir android.ModuleGenPath) (timestamp android.Path, installPaths android.WritablePaths) { 2024 srcDir := android.PathForModuleSrc(ctx, srcHeaderDir) 2025 srcFiles := ctx.GlobFiles(filepath.Join(srcDir.String(), "**/*.h"), nil) 2026 2027 for _, header := range srcFiles { 2028 headerDir := filepath.Dir(header.String()) 2029 relHeaderDir, err := filepath.Rel(srcDir.String(), headerDir) 2030 if err != nil { 2031 ctx.ModuleErrorf("filepath.Rel(%q, %q) failed: %s", 2032 srcDir.String(), headerDir, err) 2033 continue 2034 } 2035 2036 installPaths = append(installPaths, outDir.Join(ctx, relHeaderDir, header.Base())) 2037 } 2038 2039 return processHeadersWithVersioner(ctx, srcDir, outDir, srcFiles, installPaths), installPaths 2040} 2041 2042// link registers actions to link this library, and sets various fields 2043// on this library to reflect information that should be exported up the build 2044// tree (for example, exported flags and include paths). 2045func (library *libraryDecorator) link(ctx ModuleContext, 2046 flags Flags, deps PathDeps, objs Objects) android.Path { 2047 2048 if ctx.IsLlndk() { 2049 if len(library.Properties.Llndk.Export_preprocessed_headers) > 0 { 2050 // This is the vendor variant of an LLNDK library with preprocessed headers. 2051 genHeaderOutDir := android.PathForModuleGen(ctx, "include") 2052 2053 var timestampFiles android.Paths 2054 for _, dir := range library.Properties.Llndk.Export_preprocessed_headers { 2055 timestampFile, installPaths := processLLNDKHeaders(ctx, dir, genHeaderOutDir) 2056 timestampFiles = append(timestampFiles, timestampFile) 2057 library.addExportedGeneratedHeaders(installPaths.Paths()...) 2058 } 2059 2060 if Bool(library.Properties.Llndk.Export_headers_as_system) { 2061 library.reexportSystemDirs(genHeaderOutDir) 2062 } else { 2063 library.reexportDirs(genHeaderOutDir) 2064 } 2065 2066 library.reexportDeps(timestampFiles...) 2067 } 2068 2069 // override the module's export_include_dirs with llndk.override_export_include_dirs 2070 // if it is set. 2071 if override := library.Properties.Llndk.Override_export_include_dirs; override != nil { 2072 library.flagExporter.Properties.Export_include_dirs = override 2073 } 2074 2075 if Bool(library.Properties.Llndk.Export_headers_as_system) { 2076 library.flagExporter.Properties.Export_system_include_dirs = append( 2077 library.flagExporter.Properties.Export_system_include_dirs, 2078 library.flagExporter.Properties.Export_include_dirs...) 2079 library.flagExporter.Properties.Export_include_dirs = nil 2080 } 2081 } 2082 2083 if ctx.IsVendorPublicLibrary() { 2084 // override the module's export_include_dirs with vendor_public_library.override_export_include_dirs 2085 // if it is set. 2086 if override := library.Properties.Vendor_public_library.Override_export_include_dirs; override != nil { 2087 library.flagExporter.Properties.Export_include_dirs = override 2088 } 2089 } 2090 2091 // Linking this library consists of linking `deps.Objs` (.o files in dependencies 2092 // of this library), together with `objs` (.o files created by compiling this 2093 // library). 2094 objs = deps.Objs.Copy().Append(objs) 2095 var out android.Path 2096 if library.static() || library.header() { 2097 out = library.linkStatic(ctx, flags, deps, objs) 2098 } else { 2099 out = library.linkShared(ctx, flags, deps, objs) 2100 } 2101 2102 // Export include paths and flags to be propagated up the tree. 2103 library.exportIncludes(ctx) 2104 library.reexportDirs(deps.ReexportedDirs...) 2105 library.reexportSystemDirs(deps.ReexportedSystemDirs...) 2106 library.reexportFlags(deps.ReexportedFlags...) 2107 library.reexportDeps(deps.ReexportedDeps...) 2108 library.addExportedGeneratedHeaders(deps.ReexportedGeneratedHeaders...) 2109 2110 // Optionally export aidl headers. 2111 if Bool(library.Properties.Aidl.Export_aidl_headers) { 2112 if library.baseCompiler.hasSrcExt(".aidl") { 2113 dir := android.PathForModuleGen(ctx, "aidl") 2114 library.reexportDirs(dir) 2115 2116 library.reexportDeps(library.baseCompiler.aidlOrderOnlyDeps...) 2117 library.addExportedGeneratedHeaders(library.baseCompiler.aidlHeaders...) 2118 } 2119 } 2120 2121 // Optionally export proto headers. 2122 if Bool(library.Properties.Proto.Export_proto_headers) { 2123 if library.baseCompiler.hasSrcExt(".proto") { 2124 var includes android.Paths 2125 if flags.proto.CanonicalPathFromRoot { 2126 includes = append(includes, flags.proto.SubDir) 2127 } 2128 includes = append(includes, flags.proto.Dir) 2129 library.reexportDirs(includes...) 2130 2131 library.reexportDeps(library.baseCompiler.protoOrderOnlyDeps...) 2132 library.addExportedGeneratedHeaders(library.baseCompiler.protoHeaders...) 2133 } 2134 } 2135 2136 // If the library is sysprop_library, expose either public or internal header selectively. 2137 if library.baseCompiler.hasSrcExt(".sysprop") { 2138 dir := android.PathForModuleGen(ctx, "sysprop", "include") 2139 if library.Properties.Sysprop.Platform != nil { 2140 isOwnerPlatform := Bool(library.Properties.Sysprop.Platform) 2141 2142 // If the owner is different from the user, expose public header. That is, 2143 // 1) if the user is product (as owner can only be platform / vendor) 2144 // 2) if the owner is platform and the client is vendor 2145 // We don't care Platform -> Vendor dependency as it's already forbidden. 2146 if ctx.Device() && (ctx.ProductSpecific() || (isOwnerPlatform && ctx.inVendor())) { 2147 dir = android.PathForModuleGen(ctx, "sysprop/public", "include") 2148 } 2149 } 2150 2151 // Make sure to only export headers which are within the include directory. 2152 _, headers := android.FilterPathListPredicate(library.baseCompiler.syspropHeaders, func(path android.Path) bool { 2153 _, isRel := android.MaybeRel(ctx, dir.String(), path.String()) 2154 return isRel 2155 }) 2156 2157 // Add sysprop-related directories to the exported directories of this library. 2158 library.reexportDirs(dir) 2159 library.reexportDeps(library.baseCompiler.syspropOrderOnlyDeps...) 2160 library.addExportedGeneratedHeaders(headers...) 2161 } 2162 2163 // Add stub-related flags if this library is a stub library. 2164 library.exportVersioningMacroIfNeeded(ctx) 2165 2166 // Propagate a Provider containing information about exported flags, deps, and include paths. 2167 library.flagExporter.setProvider(ctx) 2168 2169 return out 2170} 2171 2172func (library *libraryDecorator) exportVersioningMacroIfNeeded(ctx android.BaseModuleContext) { 2173 if library.buildStubs() && library.stubsVersion() != "" && !library.skipAPIDefine { 2174 name := versioningMacroName(ctx.Module().(*Module).ImplementationModuleName(ctx)) 2175 apiLevel, err := android.ApiLevelFromUser(ctx, library.stubsVersion()) 2176 if err != nil { 2177 ctx.ModuleErrorf("Can't export version macro: %s", err.Error()) 2178 } 2179 library.reexportFlags("-D" + name + "=" + strconv.Itoa(apiLevel.FinalOrPreviewInt())) 2180 } 2181} 2182 2183// buildStatic returns true if this library should be built as a static library. 2184func (library *libraryDecorator) buildStatic() bool { 2185 return library.MutatedProperties.BuildStatic && 2186 BoolDefault(library.StaticProperties.Static.Enabled, true) 2187} 2188 2189// buildShared returns true if this library should be built as a shared library. 2190func (library *libraryDecorator) buildShared() bool { 2191 return library.MutatedProperties.BuildShared && 2192 BoolDefault(library.SharedProperties.Shared.Enabled, true) 2193} 2194 2195func (library *libraryDecorator) objs() Objects { 2196 return library.objects 2197} 2198 2199func (library *libraryDecorator) reuseObjs() Objects { 2200 return library.reuseObjects 2201} 2202 2203func (library *libraryDecorator) toc() android.OptionalPath { 2204 return library.tocFile 2205} 2206 2207func (library *libraryDecorator) installSymlinkToRuntimeApex(ctx ModuleContext, file android.Path) { 2208 dir := library.baseInstaller.installDir(ctx) 2209 dirOnDevice := android.InstallPathToOnDevicePath(ctx, dir) 2210 // libc_hwasan has relative_install_dir set, which would mess up the dir.Base() logic. 2211 // hardcode here because it's the only target, if we have other targets that use this 2212 // we can generalise this. 2213 var target string 2214 if ctx.baseModuleName() == "libc_hwasan" { 2215 target = "/" + filepath.Join("apex", "com.android.runtime", "lib64", "bionic", "hwasan", file.Base()) 2216 } else { 2217 base := dir.Base() 2218 target = "/" + filepath.Join("apex", "com.android.runtime", base, "bionic", file.Base()) 2219 } 2220 ctx.InstallAbsoluteSymlink(dir, file.Base(), target) 2221 library.postInstallCmds = append(library.postInstallCmds, makeSymlinkCmd(dirOnDevice, file.Base(), target)) 2222} 2223 2224func (library *libraryDecorator) install(ctx ModuleContext, file android.Path) { 2225 if library.shared() { 2226 if ctx.Device() && ctx.useVndk() { 2227 // set subDir for VNDK extensions 2228 if ctx.IsVndkExt() { 2229 if ctx.isVndkSp() { 2230 library.baseInstaller.subDir = "vndk-sp" 2231 } else { 2232 library.baseInstaller.subDir = "vndk" 2233 } 2234 } 2235 2236 // In some cases we want to use core variant for VNDK-Core libs. 2237 // Skip product variant since VNDKs use only the vendor variant. 2238 if ctx.isVndk() && !ctx.isVndkSp() && !ctx.IsVndkExt() && !ctx.inProduct() { 2239 mayUseCoreVariant := true 2240 2241 if ctx.mustUseVendorVariant() { 2242 mayUseCoreVariant = false 2243 } 2244 2245 if ctx.Config().CFIEnabledForPath(ctx.ModuleDir()) { 2246 mayUseCoreVariant = false 2247 } 2248 2249 if mayUseCoreVariant { 2250 library.checkSameCoreVariant = true 2251 if ctx.DeviceConfig().VndkUseCoreVariant() { 2252 library.useCoreVariant = true 2253 } 2254 } 2255 } 2256 2257 // do not install vndk libs 2258 // vndk libs are packaged into VNDK APEX 2259 if ctx.isVndk() && !ctx.IsVndkExt() { 2260 return 2261 } 2262 } else if library.hasStubsVariants() && !ctx.Host() && ctx.directlyInAnyApex() { 2263 // Bionic libraries (e.g. libc.so) is installed to the bootstrap subdirectory. 2264 // The original path becomes a symlink to the corresponding file in the 2265 // runtime APEX. 2266 translatedArch := ctx.Target().NativeBridge == android.NativeBridgeEnabled 2267 if InstallToBootstrap(ctx.baseModuleName(), ctx.Config()) && !library.buildStubs() && 2268 !translatedArch && !ctx.inRamdisk() && !ctx.inVendorRamdisk() && !ctx.inRecovery() { 2269 if ctx.Device() { 2270 library.installSymlinkToRuntimeApex(ctx, file) 2271 } 2272 library.baseInstaller.subDir = "bootstrap" 2273 } 2274 } else if ctx.directlyInAnyApex() && ctx.IsLlndk() && !isBionic(ctx.baseModuleName()) { 2275 // Skip installing LLNDK (non-bionic) libraries moved to APEX. 2276 ctx.Module().HideFromMake() 2277 } 2278 2279 library.baseInstaller.install(ctx, file) 2280 } 2281 2282 if Bool(library.Properties.Static_ndk_lib) && library.static() && 2283 !ctx.useVndk() && !ctx.inRamdisk() && !ctx.inVendorRamdisk() && !ctx.inRecovery() && ctx.Device() && 2284 library.baseLinker.sanitize.isUnsanitizedVariant() && 2285 ctx.isForPlatform() && !ctx.isPreventInstall() { 2286 installPath := getUnversionedLibraryInstallPath(ctx).Join(ctx, file.Base()) 2287 2288 ctx.ModuleBuild(pctx, android.ModuleBuildParams{ 2289 Rule: android.Cp, 2290 Description: "install " + installPath.Base(), 2291 Output: installPath, 2292 Input: file, 2293 }) 2294 2295 library.ndkSysrootPath = installPath 2296 } 2297} 2298 2299func (library *libraryDecorator) everInstallable() bool { 2300 // Only shared and static libraries are installed. Header libraries (which are 2301 // neither static or shared) are not installed. 2302 return library.shared() || library.static() 2303} 2304 2305// static returns true if this library is for a "static' variant. 2306func (library *libraryDecorator) static() bool { 2307 return library.MutatedProperties.VariantIsStatic 2308} 2309 2310// shared returns true if this library is for a "shared' variant. 2311func (library *libraryDecorator) shared() bool { 2312 return library.MutatedProperties.VariantIsShared 2313} 2314 2315// header returns true if this library is for a header-only variant. 2316func (library *libraryDecorator) header() bool { 2317 // Neither "static" nor "shared" implies this library is header-only. 2318 return !library.static() && !library.shared() 2319} 2320 2321// setStatic marks the library variant as "static". 2322func (library *libraryDecorator) setStatic() { 2323 library.MutatedProperties.VariantIsStatic = true 2324 library.MutatedProperties.VariantIsShared = false 2325} 2326 2327// setShared marks the library variant as "shared". 2328func (library *libraryDecorator) setShared() { 2329 library.MutatedProperties.VariantIsStatic = false 2330 library.MutatedProperties.VariantIsShared = true 2331} 2332 2333// BuildOnlyStatic disables building this library as a shared library. 2334func (library *libraryDecorator) BuildOnlyStatic() { 2335 library.MutatedProperties.BuildShared = false 2336} 2337 2338// BuildOnlyShared disables building this library as a static library. 2339func (library *libraryDecorator) BuildOnlyShared() { 2340 library.MutatedProperties.BuildStatic = false 2341} 2342 2343// HeaderOnly disables building this library as a shared or static library; 2344// the library only exists to propagate header file dependencies up the build graph. 2345func (library *libraryDecorator) HeaderOnly() { 2346 library.MutatedProperties.BuildShared = false 2347 library.MutatedProperties.BuildStatic = false 2348} 2349 2350// hasLLNDKStubs returns true if this cc_library module has a variant that will build LLNDK stubs. 2351func (library *libraryDecorator) hasLLNDKStubs() bool { 2352 return String(library.Properties.Llndk.Symbol_file) != "" 2353} 2354 2355// hasLLNDKStubs returns true if this cc_library module has a variant that will build LLNDK stubs. 2356func (library *libraryDecorator) hasLLNDKHeaders() bool { 2357 return Bool(library.Properties.Llndk.Llndk_headers) 2358} 2359 2360// hasVendorPublicLibrary returns true if this cc_library module has a variant that will build 2361// vendor public library stubs. 2362func (library *libraryDecorator) hasVendorPublicLibrary() bool { 2363 return String(library.Properties.Vendor_public_library.Symbol_file) != "" 2364} 2365 2366func (library *libraryDecorator) implementationModuleName(name string) string { 2367 return name 2368} 2369 2370func (library *libraryDecorator) buildStubs() bool { 2371 return library.MutatedProperties.BuildStubs 2372} 2373 2374func (library *libraryDecorator) symbolFileForAbiCheck(ctx ModuleContext) *string { 2375 if props := library.getHeaderAbiCheckerProperties(ctx); props.Symbol_file != nil { 2376 return props.Symbol_file 2377 } 2378 if ctx.Module().(*Module).IsLlndk() { 2379 return library.Properties.Llndk.Symbol_file 2380 } 2381 if library.hasStubsVariants() && library.Properties.Stubs.Symbol_file != nil { 2382 return library.Properties.Stubs.Symbol_file 2383 } 2384 return nil 2385} 2386 2387func (library *libraryDecorator) hasStubsVariants() bool { 2388 // Just having stubs.symbol_file is enough to create a stub variant. In that case 2389 // the stub for the future API level is created. 2390 return library.Properties.Stubs.Symbol_file != nil || 2391 len(library.Properties.Stubs.Versions) > 0 2392} 2393 2394func (library *libraryDecorator) isStubsImplementationRequired() bool { 2395 return BoolDefault(library.Properties.Stubs.Implementation_installable, true) 2396} 2397 2398func (library *libraryDecorator) stubsVersions(ctx android.BaseMutatorContext) []string { 2399 if !library.hasStubsVariants() { 2400 return nil 2401 } 2402 2403 if library.hasLLNDKStubs() && ctx.Module().(*Module).UseVndk() { 2404 // LLNDK libraries only need a single stubs variant. 2405 return []string{android.FutureApiLevel.String()} 2406 } 2407 2408 // Future API level is implicitly added if there isn't 2409 return addCurrentVersionIfNotPresent(library.Properties.Stubs.Versions) 2410} 2411 2412func addCurrentVersionIfNotPresent(vers []string) []string { 2413 if inList(android.FutureApiLevel.String(), vers) { 2414 return vers 2415 } 2416 // In some cases, people use the raw value "10000" in the versions property. 2417 // We shouldn't add the future API level in that case, otherwise there will 2418 // be two identical versions. 2419 if inList(strconv.Itoa(android.FutureApiLevel.FinalOrFutureInt()), vers) { 2420 return vers 2421 } 2422 return append(vers, android.FutureApiLevel.String()) 2423} 2424 2425func (library *libraryDecorator) setStubsVersion(version string) { 2426 library.MutatedProperties.StubsVersion = version 2427} 2428 2429func (library *libraryDecorator) stubsVersion() string { 2430 return library.MutatedProperties.StubsVersion 2431} 2432 2433func (library *libraryDecorator) setBuildStubs(isLatest bool) { 2434 library.MutatedProperties.BuildStubs = true 2435 library.MutatedProperties.IsLatestVersion = isLatest 2436} 2437 2438func (library *libraryDecorator) setAllStubsVersions(versions []string) { 2439 library.MutatedProperties.AllStubsVersions = versions 2440} 2441 2442func (library *libraryDecorator) allStubsVersions() []string { 2443 return library.MutatedProperties.AllStubsVersions 2444} 2445 2446func (library *libraryDecorator) isLatestStubVersion() bool { 2447 return library.MutatedProperties.IsLatestVersion 2448} 2449 2450func (library *libraryDecorator) availableFor(what string) bool { 2451 var list []string 2452 if library.static() { 2453 list = library.StaticProperties.Static.Apex_available 2454 } else if library.shared() { 2455 list = library.SharedProperties.Shared.Apex_available 2456 } 2457 if len(list) == 0 { 2458 return false 2459 } 2460 return android.CheckAvailableForApex(what, list) 2461} 2462 2463func (library *libraryDecorator) installable() *bool { 2464 if library.static() { 2465 return library.StaticProperties.Static.Installable 2466 } else if library.shared() { 2467 return library.SharedProperties.Shared.Installable 2468 } 2469 return nil 2470} 2471 2472func (library *libraryDecorator) makeUninstallable(mod *Module) { 2473 if library.static() && library.buildStatic() && !library.buildStubs() { 2474 // If we're asked to make a static library uninstallable we don't do 2475 // anything since AndroidMkEntries always sets LOCAL_UNINSTALLABLE_MODULE 2476 // for these entries. This is done to still get the make targets for NOTICE 2477 // files from notice_files.mk, which other libraries might depend on. 2478 return 2479 } 2480 mod.ModuleBase.MakeUninstallable() 2481} 2482 2483func (library *libraryDecorator) getPartition() string { 2484 return library.path.Partition() 2485} 2486 2487func (library *libraryDecorator) getAPIListCoverageXMLPath() android.ModuleOutPath { 2488 return library.apiListCoverageXmlPath 2489} 2490 2491func (library *libraryDecorator) overriddenModules() []string { 2492 return library.Properties.Overrides 2493} 2494 2495var _ overridable = (*libraryDecorator)(nil) 2496 2497var versioningMacroNamesListKey = android.NewOnceKey("versioningMacroNamesList") 2498 2499// versioningMacroNamesList returns a singleton map, where keys are "version macro names", 2500// and values are the module name responsible for registering the version macro name. 2501// 2502// Version macros are used when building against stubs, to provide version information about 2503// the stub. Only stub libraries should have an entry in this list. 2504// 2505// For example, when building against libFoo#ver, __LIBFOO_API__ macro is set to ver so 2506// that headers from libFoo can be conditionally compiled (this may hide APIs 2507// that are not available for the version). 2508// 2509// This map is used to ensure that there aren't conflicts between these version macro names. 2510func versioningMacroNamesList(config android.Config) *map[string]string { 2511 return config.Once(versioningMacroNamesListKey, func() interface{} { 2512 m := make(map[string]string) 2513 return &m 2514 }).(*map[string]string) 2515} 2516 2517// alphanumeric and _ characters are preserved. 2518// other characters are all converted to _ 2519var charsNotForMacro = regexp.MustCompile("[^a-zA-Z0-9_]+") 2520 2521// versioningMacroName returns the canonical version macro name for the given module. 2522func versioningMacroName(moduleName string) string { 2523 macroName := charsNotForMacro.ReplaceAllString(moduleName, "_") 2524 macroName = strings.ToUpper(macroName) 2525 return "__" + macroName + "_API__" 2526} 2527 2528// NewLibrary builds and returns a new Module corresponding to a C++ library. 2529// Individual module implementations which comprise a C++ library (or something like 2530// a C++ library) should call this function, set some fields on the result, and 2531// then call the Init function. 2532func NewLibrary(hod android.HostOrDeviceSupported) (*Module, *libraryDecorator) { 2533 module := newModule(hod, android.MultilibBoth) 2534 2535 library := &libraryDecorator{ 2536 MutatedProperties: LibraryMutatedProperties{ 2537 BuildShared: true, 2538 BuildStatic: true, 2539 }, 2540 baseCompiler: NewBaseCompiler(), 2541 baseLinker: NewBaseLinker(module.sanitize), 2542 baseInstaller: NewBaseInstaller("lib", "lib64", InstallInSystem), 2543 sabi: module.sabi, 2544 } 2545 2546 module.compiler = library 2547 module.linker = library 2548 module.installer = library 2549 module.library = library 2550 2551 return module, library 2552} 2553 2554// connects a shared library to a static library in order to reuse its .o files to avoid 2555// compiling source files twice. 2556func reuseStaticLibrary(mctx android.BottomUpMutatorContext, static, shared *Module) { 2557 if staticCompiler, ok := static.compiler.(*libraryDecorator); ok { 2558 sharedCompiler := shared.compiler.(*libraryDecorator) 2559 2560 // Check libraries in addition to cflags, since libraries may be exporting different 2561 // include directories. 2562 if len(staticCompiler.StaticProperties.Static.Cflags) == 0 && 2563 len(sharedCompiler.SharedProperties.Shared.Cflags) == 0 && 2564 len(staticCompiler.StaticProperties.Static.Whole_static_libs) == 0 && 2565 len(sharedCompiler.SharedProperties.Shared.Whole_static_libs) == 0 && 2566 len(staticCompiler.StaticProperties.Static.Static_libs) == 0 && 2567 len(sharedCompiler.SharedProperties.Shared.Static_libs) == 0 && 2568 len(staticCompiler.StaticProperties.Static.Shared_libs) == 0 && 2569 len(sharedCompiler.SharedProperties.Shared.Shared_libs) == 0 && 2570 // Compare System_shared_libs properties with nil because empty lists are 2571 // semantically significant for them. 2572 staticCompiler.StaticProperties.Static.System_shared_libs == nil && 2573 sharedCompiler.SharedProperties.Shared.System_shared_libs == nil { 2574 2575 mctx.AddInterVariantDependency(reuseObjTag, shared, static) 2576 sharedCompiler.baseCompiler.Properties.OriginalSrcs = 2577 sharedCompiler.baseCompiler.Properties.Srcs 2578 sharedCompiler.baseCompiler.Properties.Srcs = nil 2579 sharedCompiler.baseCompiler.Properties.Generated_sources = nil 2580 } 2581 2582 // This dep is just to reference static variant from shared variant 2583 mctx.AddInterVariantDependency(staticVariantTag, shared, static) 2584 } 2585} 2586 2587// LinkageMutator adds "static" or "shared" variants for modules depending 2588// on whether the module can be built as a static library or a shared library. 2589func LinkageMutator(mctx android.BottomUpMutatorContext) { 2590 ccPrebuilt := false 2591 if m, ok := mctx.Module().(*Module); ok && m.linker != nil { 2592 _, ccPrebuilt = m.linker.(prebuiltLibraryInterface) 2593 } 2594 if ccPrebuilt { 2595 library := mctx.Module().(*Module).linker.(prebuiltLibraryInterface) 2596 2597 // Differentiate between header only and building an actual static/shared library 2598 buildStatic := library.buildStatic() 2599 buildShared := library.buildShared() 2600 if buildStatic || buildShared { 2601 // Always create both the static and shared variants for prebuilt libraries, and then disable the one 2602 // that is not being used. This allows them to share the name of a cc_library module, which requires that 2603 // all the variants of the cc_library also exist on the prebuilt. 2604 modules := mctx.CreateLocalVariations("static", "shared") 2605 static := modules[0].(*Module) 2606 shared := modules[1].(*Module) 2607 2608 static.linker.(prebuiltLibraryInterface).setStatic() 2609 shared.linker.(prebuiltLibraryInterface).setShared() 2610 2611 if buildShared { 2612 mctx.AliasVariation("shared") 2613 } else if buildStatic { 2614 mctx.AliasVariation("static") 2615 } 2616 2617 if !buildStatic { 2618 static.linker.(prebuiltLibraryInterface).disablePrebuilt() 2619 } 2620 if !buildShared { 2621 shared.linker.(prebuiltLibraryInterface).disablePrebuilt() 2622 } 2623 } else { 2624 // Header only 2625 } 2626 2627 } else if library, ok := mctx.Module().(LinkableInterface); ok && library.CcLibraryInterface() { 2628 2629 // Non-cc.Modules may need an empty variant for their mutators. 2630 variations := []string{} 2631 if library.NonCcVariants() { 2632 variations = append(variations, "") 2633 } 2634 2635 isLLNDK := false 2636 if m, ok := mctx.Module().(*Module); ok { 2637 isLLNDK = m.IsLlndk() 2638 } 2639 buildStatic := library.BuildStaticVariant() && !isLLNDK 2640 buildShared := library.BuildSharedVariant() 2641 if buildStatic && buildShared { 2642 variations := append([]string{"static", "shared"}, variations...) 2643 2644 modules := mctx.CreateLocalVariations(variations...) 2645 static := modules[0].(LinkableInterface) 2646 shared := modules[1].(LinkableInterface) 2647 2648 static.SetStatic() 2649 shared.SetShared() 2650 2651 if _, ok := library.(*Module); ok { 2652 reuseStaticLibrary(mctx, static.(*Module), shared.(*Module)) 2653 } 2654 mctx.AliasVariation("shared") 2655 } else if buildStatic { 2656 variations := append([]string{"static"}, variations...) 2657 2658 modules := mctx.CreateLocalVariations(variations...) 2659 modules[0].(LinkableInterface).SetStatic() 2660 mctx.AliasVariation("static") 2661 } else if buildShared { 2662 variations := append([]string{"shared"}, variations...) 2663 2664 modules := mctx.CreateLocalVariations(variations...) 2665 modules[0].(LinkableInterface).SetShared() 2666 mctx.AliasVariation("shared") 2667 } else if len(variations) > 0 { 2668 mctx.CreateLocalVariations(variations...) 2669 mctx.AliasVariation(variations[0]) 2670 } 2671 } 2672} 2673 2674// normalizeVersions modifies `versions` in place, so that each raw version 2675// string becomes its normalized canonical form. 2676// Validates that the versions in `versions` are specified in least to greatest order. 2677func normalizeVersions(ctx android.BazelConversionPathContext, versions []string) { 2678 var previous android.ApiLevel 2679 for i, v := range versions { 2680 ver, err := android.ApiLevelFromUser(ctx, v) 2681 if err != nil { 2682 ctx.PropertyErrorf("versions", "%s", err.Error()) 2683 return 2684 } 2685 if i > 0 && ver.LessThanOrEqualTo(previous) { 2686 ctx.PropertyErrorf("versions", "not sorted: %v", versions) 2687 } 2688 versions[i] = ver.String() 2689 previous = ver 2690 } 2691} 2692 2693func createVersionVariations(mctx android.BottomUpMutatorContext, versions []string) { 2694 // "" is for the non-stubs (implementation) variant for system modules, or the LLNDK variant 2695 // for LLNDK modules. 2696 variants := append(android.CopyOf(versions), "") 2697 2698 m := mctx.Module().(*Module) 2699 isLLNDK := m.IsLlndk() 2700 isVendorPublicLibrary := m.IsVendorPublicLibrary() 2701 isImportedApiLibrary := m.isImportedApiLibrary() 2702 2703 modules := mctx.CreateLocalVariations(variants...) 2704 for i, m := range modules { 2705 2706 if variants[i] != "" || isLLNDK || isVendorPublicLibrary || isImportedApiLibrary { 2707 // A stubs or LLNDK stubs variant. 2708 c := m.(*Module) 2709 c.sanitize = nil 2710 c.stl = nil 2711 c.Properties.PreventInstall = true 2712 lib := moduleLibraryInterface(m) 2713 isLatest := i == (len(versions) - 1) 2714 lib.setBuildStubs(isLatest) 2715 2716 if variants[i] != "" { 2717 // A non-LLNDK stubs module is hidden from make and has a dependency from the 2718 // implementation module to the stubs module. 2719 c.Properties.HideFromMake = true 2720 lib.setStubsVersion(variants[i]) 2721 mctx.AddInterVariantDependency(stubImplDepTag, modules[len(modules)-1], modules[i]) 2722 } 2723 } 2724 } 2725 mctx.AliasVariation("") 2726 latestVersion := "" 2727 if len(versions) > 0 { 2728 latestVersion = versions[len(versions)-1] 2729 } 2730 mctx.CreateAliasVariation("latest", latestVersion) 2731} 2732 2733func createPerApiVersionVariations(mctx android.BottomUpMutatorContext, minSdkVersion string) { 2734 from, err := nativeApiLevelFromUser(mctx, minSdkVersion) 2735 if err != nil { 2736 mctx.PropertyErrorf("min_sdk_version", err.Error()) 2737 return 2738 } 2739 2740 versionStrs := ndkLibraryVersions(mctx, from) 2741 modules := mctx.CreateLocalVariations(versionStrs...) 2742 2743 for i, module := range modules { 2744 module.(*Module).Properties.Sdk_version = StringPtr(versionStrs[i]) 2745 module.(*Module).Properties.Min_sdk_version = StringPtr(versionStrs[i]) 2746 } 2747} 2748 2749func canBeOrLinkAgainstVersionVariants(module interface { 2750 Host() bool 2751 InRamdisk() bool 2752 InVendorRamdisk() bool 2753}) bool { 2754 return !module.Host() && !module.InRamdisk() && !module.InVendorRamdisk() 2755} 2756 2757func canBeVersionVariant(module interface { 2758 Host() bool 2759 InRamdisk() bool 2760 InVendorRamdisk() bool 2761 CcLibraryInterface() bool 2762 Shared() bool 2763}) bool { 2764 return canBeOrLinkAgainstVersionVariants(module) && 2765 module.CcLibraryInterface() && module.Shared() 2766} 2767 2768func moduleLibraryInterface(module blueprint.Module) libraryInterface { 2769 if m, ok := module.(*Module); ok { 2770 return m.library 2771 } 2772 return nil 2773} 2774 2775// setStubsVersions normalizes the versions in the Stubs.Versions property into MutatedProperties.AllStubsVersions. 2776func setStubsVersions(mctx android.BottomUpMutatorContext, library libraryInterface, module *Module) { 2777 if !library.buildShared() || !canBeVersionVariant(module) { 2778 return 2779 } 2780 versions := library.stubsVersions(mctx) 2781 if len(versions) <= 0 { 2782 return 2783 } 2784 normalizeVersions(mctx, versions) 2785 if mctx.Failed() { 2786 return 2787 } 2788 // Set the versions on the pre-mutated module so they can be read by any llndk modules that 2789 // depend on the implementation library and haven't been mutated yet. 2790 library.setAllStubsVersions(versions) 2791} 2792 2793// versionMutator splits a module into the mandatory non-stubs variant 2794// (which is unnamed) and zero or more stubs variants. 2795func versionMutator(mctx android.BottomUpMutatorContext) { 2796 if mctx.Os() != android.Android { 2797 return 2798 } 2799 2800 m, ok := mctx.Module().(*Module) 2801 if library := moduleLibraryInterface(mctx.Module()); library != nil && canBeVersionVariant(m) { 2802 setStubsVersions(mctx, library, m) 2803 2804 createVersionVariations(mctx, library.allStubsVersions()) 2805 return 2806 } 2807 2808 if ok { 2809 if m.SplitPerApiLevel() && m.IsSdkVariant() { 2810 createPerApiVersionVariations(mctx, m.MinSdkVersion()) 2811 } 2812 } 2813} 2814 2815// maybeInjectBoringSSLHash adds a rule to run bssl_inject_hash on the output file if the module has the 2816// inject_bssl_hash or if any static library dependencies have inject_bssl_hash set. It returns the output path 2817// that the linked output file should be written to. 2818// TODO(b/137267623): Remove this in favor of a cc_genrule when they support operating on shared libraries. 2819func maybeInjectBoringSSLHash(ctx android.ModuleContext, outputFile android.ModuleOutPath, 2820 inject *bool, fileName string) android.ModuleOutPath { 2821 // TODO(b/137267623): Remove this in favor of a cc_genrule when they support operating on shared libraries. 2822 injectBoringSSLHash := Bool(inject) 2823 ctx.VisitDirectDeps(func(dep android.Module) { 2824 if tag, ok := ctx.OtherModuleDependencyTag(dep).(libraryDependencyTag); ok && tag.static() { 2825 if cc, ok := dep.(*Module); ok { 2826 if library, ok := cc.linker.(*libraryDecorator); ok { 2827 if Bool(library.Properties.Inject_bssl_hash) { 2828 injectBoringSSLHash = true 2829 } 2830 } 2831 } 2832 } 2833 }) 2834 if injectBoringSSLHash { 2835 hashedOutputfile := outputFile 2836 outputFile = android.PathForModuleOut(ctx, "unhashed", fileName) 2837 2838 rule := android.NewRuleBuilder(pctx, ctx) 2839 rule.Command(). 2840 BuiltTool("bssl_inject_hash"). 2841 FlagWithInput("-in-object ", outputFile). 2842 FlagWithOutput("-o ", hashedOutputfile) 2843 rule.Build("injectCryptoHash", "inject crypto hash") 2844 } 2845 2846 return outputFile 2847} 2848 2849func bp2buildParseAbiCheckerProps(ctx android.TopDownMutatorContext, module *Module) bazelCcHeaderAbiCheckerAttributes { 2850 lib, ok := module.linker.(*libraryDecorator) 2851 if !ok { 2852 return bazelCcHeaderAbiCheckerAttributes{} 2853 } 2854 2855 abiChecker := lib.getHeaderAbiCheckerProperties(ctx) 2856 2857 abiCheckerAttrs := bazelCcHeaderAbiCheckerAttributes{ 2858 Abi_checker_enabled: abiChecker.Enabled, 2859 Abi_checker_exclude_symbol_versions: abiChecker.Exclude_symbol_versions, 2860 Abi_checker_exclude_symbol_tags: abiChecker.Exclude_symbol_tags, 2861 Abi_checker_check_all_apis: abiChecker.Check_all_apis, 2862 Abi_checker_diff_flags: abiChecker.Diff_flags, 2863 } 2864 if abiChecker.Symbol_file != nil { 2865 symbolFile := android.BazelLabelForModuleSrcSingle(ctx, *abiChecker.Symbol_file) 2866 abiCheckerAttrs.Abi_checker_symbol_file = &symbolFile 2867 } 2868 2869 return abiCheckerAttrs 2870} 2871 2872func sharedOrStaticLibraryBp2Build(ctx android.TopDownMutatorContext, module *Module, isStatic bool) { 2873 baseAttributes := bp2BuildParseBaseProps(ctx, module) 2874 compilerAttrs := baseAttributes.compilerAttributes 2875 linkerAttrs := baseAttributes.linkerAttributes 2876 2877 exportedIncludes := bp2BuildParseExportedIncludes(ctx, module, &compilerAttrs.includes) 2878 2879 // Append shared/static{} stanza properties. These won't be specified on 2880 // cc_library_* itself, but may be specified in cc_defaults that this module 2881 // depends on. 2882 libSharedOrStaticAttrs := bp2BuildParseLibProps(ctx, module, isStatic) 2883 2884 compilerAttrs.srcs.Append(libSharedOrStaticAttrs.Srcs) 2885 compilerAttrs.cSrcs.Append(libSharedOrStaticAttrs.Srcs_c) 2886 compilerAttrs.asSrcs.Append(libSharedOrStaticAttrs.Srcs_as) 2887 compilerAttrs.copts.Append(libSharedOrStaticAttrs.Copts) 2888 2889 linkerAttrs.deps.Append(libSharedOrStaticAttrs.Deps) 2890 linkerAttrs.implementationDeps.Append(libSharedOrStaticAttrs.Implementation_deps) 2891 linkerAttrs.dynamicDeps.Append(libSharedOrStaticAttrs.Dynamic_deps) 2892 linkerAttrs.implementationDynamicDeps.Append(libSharedOrStaticAttrs.Implementation_dynamic_deps) 2893 linkerAttrs.systemDynamicDeps.Append(libSharedOrStaticAttrs.System_dynamic_deps) 2894 2895 asFlags := compilerAttrs.asFlags 2896 if compilerAttrs.asSrcs.IsEmpty() { 2897 // Skip asflags for BUILD file simplicity if there are no assembly sources. 2898 asFlags = bazel.MakeStringListAttribute(nil) 2899 } 2900 2901 commonAttrs := staticOrSharedAttributes{ 2902 Srcs: compilerAttrs.srcs, 2903 Srcs_c: compilerAttrs.cSrcs, 2904 Srcs_as: compilerAttrs.asSrcs, 2905 Copts: compilerAttrs.copts, 2906 Hdrs: compilerAttrs.hdrs, 2907 2908 Deps: linkerAttrs.deps, 2909 Implementation_deps: linkerAttrs.implementationDeps, 2910 Dynamic_deps: linkerAttrs.dynamicDeps, 2911 Implementation_dynamic_deps: linkerAttrs.implementationDynamicDeps, 2912 Whole_archive_deps: linkerAttrs.wholeArchiveDeps, 2913 Implementation_whole_archive_deps: linkerAttrs.implementationWholeArchiveDeps, 2914 System_dynamic_deps: linkerAttrs.systemDynamicDeps, 2915 sdkAttributes: bp2BuildParseSdkAttributes(module), 2916 Runtime_deps: linkerAttrs.runtimeDeps, 2917 Native_coverage: baseAttributes.Native_coverage, 2918 } 2919 2920 module.convertTidyAttributes(ctx, &commonAttrs.tidyAttributes) 2921 2922 var attrs interface{} 2923 if isStatic { 2924 commonAttrs.Deps.Add(baseAttributes.protoDependency) 2925 attrs = &bazelCcLibraryStaticAttributes{ 2926 staticOrSharedAttributes: commonAttrs, 2927 Rtti: compilerAttrs.rtti, 2928 Stl: compilerAttrs.stl, 2929 Cpp_std: compilerAttrs.cppStd, 2930 C_std: compilerAttrs.cStd, 2931 2932 Export_includes: exportedIncludes.Includes, 2933 Export_absolute_includes: exportedIncludes.AbsoluteIncludes, 2934 Export_system_includes: exportedIncludes.SystemIncludes, 2935 Local_includes: compilerAttrs.localIncludes, 2936 Absolute_includes: compilerAttrs.absoluteIncludes, 2937 2938 Cppflags: compilerAttrs.cppFlags, 2939 Conlyflags: compilerAttrs.conlyFlags, 2940 Asflags: asFlags, 2941 2942 Features: baseAttributes.features, 2943 } 2944 } else { 2945 commonAttrs.Dynamic_deps.Add(baseAttributes.protoDependency) 2946 2947 sharedLibAttrs := &bazelCcLibrarySharedAttributes{ 2948 staticOrSharedAttributes: commonAttrs, 2949 2950 Cppflags: compilerAttrs.cppFlags, 2951 Conlyflags: compilerAttrs.conlyFlags, 2952 Asflags: asFlags, 2953 2954 Linkopts: linkerAttrs.linkopts, 2955 Use_version_lib: linkerAttrs.useVersionLib, 2956 2957 Rtti: compilerAttrs.rtti, 2958 Stl: compilerAttrs.stl, 2959 Cpp_std: compilerAttrs.cppStd, 2960 C_std: compilerAttrs.cStd, 2961 2962 Export_includes: exportedIncludes.Includes, 2963 Export_absolute_includes: exportedIncludes.AbsoluteIncludes, 2964 Export_system_includes: exportedIncludes.SystemIncludes, 2965 Local_includes: compilerAttrs.localIncludes, 2966 Absolute_includes: compilerAttrs.absoluteIncludes, 2967 Additional_linker_inputs: linkerAttrs.additionalLinkerInputs, 2968 2969 Strip: stripAttrsFromLinkerAttrs(&linkerAttrs), 2970 2971 Features: baseAttributes.features, 2972 2973 Suffix: compilerAttrs.suffix, 2974 2975 bazelCcHeaderAbiCheckerAttributes: bp2buildParseAbiCheckerProps(ctx, module), 2976 2977 Fdo_profile: compilerAttrs.fdoProfile, 2978 } 2979 if compilerAttrs.stubsSymbolFile != nil && len(compilerAttrs.stubsVersions.Value) > 0 { 2980 sharedLibAttrs.Stubs_symbol_file = compilerAttrs.stubsSymbolFile 2981 } 2982 attrs = sharedLibAttrs 2983 } 2984 2985 var modType string 2986 if isStatic { 2987 modType = "cc_library_static" 2988 } else { 2989 modType = "cc_library_shared" 2990 createStubsBazelTargetIfNeeded(ctx, module, compilerAttrs, exportedIncludes, baseAttributes) 2991 } 2992 props := bazel.BazelTargetModuleProperties{ 2993 Rule_class: modType, 2994 Bzl_load_location: fmt.Sprintf("//build/bazel/rules/cc:%s.bzl", modType), 2995 } 2996 2997 tags := android.ApexAvailableTags(module) 2998 2999 ctx.CreateBazelTargetModule(props, android.CommonAttributes{Name: module.Name(), Tags: tags}, attrs) 3000} 3001 3002// TODO(b/199902614): Can this be factored to share with the other Attributes? 3003type bazelCcLibraryStaticAttributes struct { 3004 staticOrSharedAttributes 3005 3006 Use_version_lib bazel.BoolAttribute 3007 Rtti bazel.BoolAttribute 3008 Stl *string 3009 Cpp_std *string 3010 C_std *string 3011 3012 Export_includes bazel.StringListAttribute 3013 Export_absolute_includes bazel.StringListAttribute 3014 Export_system_includes bazel.StringListAttribute 3015 Local_includes bazel.StringListAttribute 3016 Absolute_includes bazel.StringListAttribute 3017 Hdrs bazel.LabelListAttribute 3018 3019 Cppflags bazel.StringListAttribute 3020 Conlyflags bazel.StringListAttribute 3021 Asflags bazel.StringListAttribute 3022 3023 Features bazel.StringListAttribute 3024} 3025 3026// TODO(b/199902614): Can this be factored to share with the other Attributes? 3027type bazelCcLibrarySharedAttributes struct { 3028 staticOrSharedAttributes 3029 3030 Linkopts bazel.StringListAttribute 3031 Use_version_lib bazel.BoolAttribute 3032 3033 Rtti bazel.BoolAttribute 3034 Stl *string 3035 Cpp_std *string 3036 C_std *string 3037 3038 Export_includes bazel.StringListAttribute 3039 Export_absolute_includes bazel.StringListAttribute 3040 Export_system_includes bazel.StringListAttribute 3041 Local_includes bazel.StringListAttribute 3042 Absolute_includes bazel.StringListAttribute 3043 Hdrs bazel.LabelListAttribute 3044 3045 Strip stripAttributes 3046 Additional_linker_inputs bazel.LabelListAttribute 3047 3048 Cppflags bazel.StringListAttribute 3049 Conlyflags bazel.StringListAttribute 3050 Asflags bazel.StringListAttribute 3051 3052 Features bazel.StringListAttribute 3053 3054 Stubs_symbol_file *string 3055 3056 Inject_bssl_hash bazel.BoolAttribute 3057 3058 Suffix bazel.StringAttribute 3059 3060 bazelCcHeaderAbiCheckerAttributes 3061 3062 Fdo_profile bazel.LabelAttribute 3063} 3064 3065type bazelCcStubSuiteAttributes struct { 3066 Symbol_file *string 3067 Versions bazel.StringListAttribute 3068 Export_includes bazel.StringListAttribute 3069 Source_library_label *string 3070 Soname *string 3071 Deps bazel.LabelListAttribute 3072} 3073 3074type bazelCcHeaderAbiCheckerAttributes struct { 3075 Abi_checker_enabled *bool 3076 Abi_checker_symbol_file *bazel.Label 3077 Abi_checker_exclude_symbol_versions []string 3078 Abi_checker_exclude_symbol_tags []string 3079 Abi_checker_check_all_apis *bool 3080 Abi_checker_diff_flags []string 3081} 3082