From 1263d9bdb1627f2ec2c030bcef5b53d011be3b77 Mon Sep 17 00:00:00 2001 From: Liz Kammer Date: Fri, 10 Dec 2021 14:28:20 -0500 Subject: [PATCH] Handle includes for generated headers For generated headers, Soong introduces the package to the includes (and re-exports as necessary). Test: bp2build.sh Change-Id: Iea9cda859ddaa3e5393f0f4533c15fc60222c408 --- android/bazel.go | 10 +-- bp2build/cc_binary_conversion_test.go | 1 + bp2build/cc_library_static_conversion_test.go | 27 +++--- cc/bp2build.go | 66 ++++++++++++-- cc/library.go | 87 ++++++++++--------- cc/library_headers.go | 28 +++--- 6 files changed, 139 insertions(+), 80 deletions(-) diff --git a/android/bazel.go b/android/bazel.go index 169f6ba29..0ee65ef6a 100644 --- a/android/bazel.go +++ b/android/bazel.go @@ -422,17 +422,11 @@ var ( // APEX support "com.android.runtime", // http://b/194746715, apex, depends on 'libc_malloc_debug' - "libadbd_core", // http://b/208481704: requijres use_version_lib - "libadbd_services", // http://b/208481704: requires use_version_lib - - "libadbd", // depends on unconverted modules: libadbd_core, libadbd_services - "libgtest_ndk_c++", // b/201816222: Requires sdk_version support. "libgtest_main_ndk_c++", // b/201816222: Requires sdk_version support. - "abb", // depends on unconverted modules: libadbd_core, libadbd_services, - "adb", // depends on unconverted modules: bin2c_fastdeployagent, libadb_crypto, libadb_host, libadb_pairing_connection, libadb_protos, libandroidfw, libapp_processes_protos_full, libfastdeploy_host, libopenscreen-discovery, libopenscreen-platform-impl, libusb, libzstd, AdbWinApi - "adbd", // depends on unconverted modules: libadb_crypto, libadb_pairing_connection, libadb_protos, libadbd, libadbd_core, libapp_processes_protos_lite, libzstd, libadbd_services, libcap, libminijail + "abb", // depends on unconverted modules: libcmd, libbinder + "adb", // depends on unconverted modules: AdbWinApi, libadb_host, libandroidfw, libapp_processes_protos_full, libfastdeploy_host, libopenscreen-discovery, libopenscreen-platform-impl, libusb, bin2c_fastdeployagent, AdbWinUsbApi "linker", // depends on unconverted modules: libdebuggerd_handler_fallback "linker_reloc_bench_main", // depends on unconverted modules: liblinker_reloc_bench_* "versioner", // depends on unconverted modules: libclang_cxx_host, libLLVM_host, of unsupported type llvm_host_prebuilt_library_shared diff --git a/bp2build/cc_binary_conversion_test.go b/bp2build/cc_binary_conversion_test.go index 24468506f..b46248f9f 100644 --- a/bp2build/cc_binary_conversion_test.go +++ b/bp2build/cc_binary_conversion_test.go @@ -303,6 +303,7 @@ genrule { ":not_explicitly_exported_whole_static_dep", ":whole_static_dep", ]`, + "local_includes": `["."]`, }, }, }, diff --git a/bp2build/cc_library_static_conversion_test.go b/bp2build/cc_library_static_conversion_test.go index ef82c6b23..fff0b4eb1 100644 --- a/bp2build/cc_library_static_conversion_test.go +++ b/bp2build/cc_library_static_conversion_test.go @@ -970,7 +970,9 @@ cc_library_static { }`, expectedBazelTargets: []string{ makeBazelTarget("cc_library_static", "foo_static", attrNameToString{ - "hdrs": `[":export_generated_hdr"]`, + "export_includes": `["."]`, + "local_includes": `["."]`, + "hdrs": `[":export_generated_hdr"]`, "srcs": `[ "cpp_src.cpp", ":generated_hdr", @@ -1044,18 +1046,21 @@ cc_library_static { exclude_srcs: ["not-for-everything.cpp"], generated_sources: ["generated_src", "generated_src_other_pkg", "generated_src_not_x86"], generated_headers: ["generated_hdr", "generated_hdr_other_pkg"], + export_generated_headers: ["generated_hdr_other_pkg"], arch: { x86: { srcs: ["for-x86.cpp"], exclude_srcs: ["not-for-x86.cpp"], generated_headers: ["generated_hdr_other_pkg_x86"], exclude_generated_sources: ["generated_src_not_x86"], + export_generated_headers: ["generated_hdr_other_pkg_x86"], }, }, target: { android: { generated_sources: ["generated_src_android"], generated_headers: ["generated_hdr_other_pkg_android"], + export_generated_headers: ["generated_hdr_other_pkg_android"], }, }, @@ -1069,23 +1074,25 @@ cc_library_static { ":generated_src", "//dep:generated_src_other_pkg", ":generated_hdr", - "//dep:generated_hdr_other_pkg", ] + select({ - "//build/bazel/platforms/arch:x86": [ - "for-x86.cpp", - "//dep:generated_hdr_other_pkg_x86", - ], + "//build/bazel/platforms/arch:x86": ["for-x86.cpp"], "//conditions:default": [ "not-for-x86.cpp", ":generated_src_not_x86", ], }) + select({ - "//build/bazel/platforms/os:android": [ - ":generated_src_android", - "//dep:generated_hdr_other_pkg_android", - ], + "//build/bazel/platforms/os:android": [":generated_src_android"], "//conditions:default": [], })`, + "hdrs": `["//dep:generated_hdr_other_pkg"] + select({ + "//build/bazel/platforms/arch:x86": ["//dep:generated_hdr_other_pkg_x86"], + "//conditions:default": [], + }) + select({ + "//build/bazel/platforms/os:android": ["//dep:generated_hdr_other_pkg_android"], + "//conditions:default": [], + })`, + "local_includes": `["."]`, + "export_absolute_includes": `["dep"]`, }), }, }) diff --git a/cc/bp2build.go b/cc/bp2build.go index 09aa1363c..6c1646cbb 100644 --- a/cc/bp2build.go +++ b/cc/bp2build.go @@ -267,6 +267,8 @@ type compilerAttributes struct { localIncludes bazel.StringListAttribute absoluteIncludes bazel.StringListAttribute + includes BazelIncludes + protoSrcs bazel.LabelListAttribute } @@ -412,6 +414,33 @@ func bp2buildResolveCppStdValue(c_std *string, cpp_std *string, gnu_extensions * return c_std_prop, cpp_std_prop } +// packageFromLabel extracts package from a fully-qualified or relative Label and whether the label +// is fully-qualified. +// e.g. fully-qualified "//a/b:foo" -> "a/b", true, relative: ":bar" -> ".", false +func packageFromLabel(label string) (string, bool) { + split := strings.Split(label, ":") + if len(split) != 2 { + return "", false + } + if split[0] == "" { + return ".", false + } + // remove leading "//" + return split[0][2:], true +} + +// includesFromLabelList extracts relative/absolute includes from a bazel.LabelList> +func includesFromLabelList(labelList bazel.LabelList) (relative, absolute []string) { + for _, hdr := range labelList.Includes { + if pkg, hasPkg := packageFromLabel(hdr.Label); hasPkg { + absolute = append(absolute, pkg) + } else if pkg != "" { + relative = append(relative, pkg) + } + } + return relative, absolute +} + // bp2BuildParseCompilerProps returns copts, srcs and hdrs and other attributes. func bp2BuildParseBaseProps(ctx android.Bp2buildMutatorContext, module *Module) baseAttributes { archVariantCompilerProps := module.GetArchVariantProperties(ctx, &BaseCompilerProperties{}) @@ -455,6 +484,18 @@ func bp2BuildParseBaseProps(ctx android.Bp2buildMutatorContext, module *Module) headers := maybePartitionExportedAndImplementationsDeps(ctx, !module.Binary(), allHdrs, exportHdrs, android.BazelLabelForModuleDeps) implementationHdrs.SetSelectValue(axis, config, headers.implementation) compilerAttrs.hdrs.SetSelectValue(axis, config, headers.export) + + exportIncludes, exportAbsoluteIncludes := includesFromLabelList(headers.export) + compilerAttrs.includes.Includes.SetSelectValue(axis, config, exportIncludes) + compilerAttrs.includes.AbsoluteIncludes.SetSelectValue(axis, config, exportAbsoluteIncludes) + + includes, absoluteIncludes := includesFromLabelList(headers.implementation) + currAbsoluteIncludes := compilerAttrs.absoluteIncludes.SelectValue(axis, config) + currAbsoluteIncludes = android.FirstUniqueStrings(append(currAbsoluteIncludes, absoluteIncludes...)) + compilerAttrs.absoluteIncludes.SetSelectValue(axis, config, currAbsoluteIncludes) + currIncludes := compilerAttrs.localIncludes.SelectValue(axis, config) + currIncludes = android.FirstUniqueStrings(append(currIncludes, includes...)) + compilerAttrs.localIncludes.SetSelectValue(axis, config, currIncludes) } } @@ -692,13 +733,14 @@ func bp2BuildMakePathsRelativeToModule(ctx android.BazelConversionPathContext, p // BazelIncludes contains information about -I and -isystem paths from a module converted to Bazel // attributes. type BazelIncludes struct { - Includes bazel.StringListAttribute - SystemIncludes bazel.StringListAttribute + AbsoluteIncludes bazel.StringListAttribute + Includes bazel.StringListAttribute + SystemIncludes bazel.StringListAttribute } -func bp2BuildParseExportedIncludes(ctx android.BazelConversionPathContext, module *Module) BazelIncludes { +func bp2BuildParseExportedIncludes(ctx android.BazelConversionPathContext, module *Module, existingIncludes BazelIncludes) BazelIncludes { libraryDecorator := module.linker.(*libraryDecorator) - return bp2BuildParseExportedIncludesHelper(ctx, module, libraryDecorator) + return bp2BuildParseExportedIncludesHelper(ctx, module, libraryDecorator, &existingIncludes) } // Bp2buildParseExportedIncludesForPrebuiltLibrary returns a BazelIncludes with Bazel-ified values @@ -706,25 +748,31 @@ func bp2BuildParseExportedIncludes(ctx android.BazelConversionPathContext, modul func Bp2BuildParseExportedIncludesForPrebuiltLibrary(ctx android.BazelConversionPathContext, module *Module) BazelIncludes { prebuiltLibraryLinker := module.linker.(*prebuiltLibraryLinker) libraryDecorator := prebuiltLibraryLinker.libraryDecorator - return bp2BuildParseExportedIncludesHelper(ctx, module, libraryDecorator) + return bp2BuildParseExportedIncludesHelper(ctx, module, libraryDecorator, nil) } // bp2BuildParseExportedIncludes creates a string list attribute contains the // exported included directories of a module. -func bp2BuildParseExportedIncludesHelper(ctx android.BazelConversionPathContext, module *Module, libraryDecorator *libraryDecorator) BazelIncludes { - exported := BazelIncludes{} +func bp2BuildParseExportedIncludesHelper(ctx android.BazelConversionPathContext, module *Module, libraryDecorator *libraryDecorator, includes *BazelIncludes) BazelIncludes { + var exported BazelIncludes + if includes != nil { + exported = *includes + } else { + exported = BazelIncludes{} + } for axis, configToProps := range module.GetArchVariantProperties(ctx, &FlagExporterProperties{}) { for config, props := range configToProps { if flagExporterProperties, ok := props.(*FlagExporterProperties); ok { if len(flagExporterProperties.Export_include_dirs) > 0 { - exported.Includes.SetSelectValue(axis, config, flagExporterProperties.Export_include_dirs) + exported.Includes.SetSelectValue(axis, config, android.FirstUniqueStrings(append(exported.Includes.SelectValue(axis, config), flagExporterProperties.Export_include_dirs...))) } if len(flagExporterProperties.Export_system_include_dirs) > 0 { - exported.SystemIncludes.SetSelectValue(axis, config, flagExporterProperties.Export_system_include_dirs) + exported.SystemIncludes.SetSelectValue(axis, config, android.FirstUniqueStrings(append(exported.SystemIncludes.SelectValue(axis, config), flagExporterProperties.Export_system_include_dirs...))) } } } } + exported.AbsoluteIncludes.DeduplicateAxesFromBase() exported.Includes.DeduplicateAxesFromBase() exported.SystemIncludes.DeduplicateAxesFromBase() diff --git a/cc/library.go b/cc/library.go index 771a6df36..15c4ec3a9 100644 --- a/cc/library.go +++ b/cc/library.go @@ -300,7 +300,7 @@ func CcLibraryBp2Build(ctx android.TopDownMutatorContext) { baseAttributes := bp2BuildParseBaseProps(ctx, m) compilerAttrs := baseAttributes.compilerAttributes linkerAttrs := baseAttributes.linkerAttributes - exportedIncludes := bp2BuildParseExportedIncludes(ctx, m) + exportedIncludes := bp2BuildParseExportedIncludes(ctx, m, compilerAttrs.includes) srcs := compilerAttrs.srcs @@ -351,15 +351,16 @@ func CcLibraryBp2Build(ctx android.TopDownMutatorContext) { Conlyflags: compilerAttrs.conlyFlags, Asflags: asFlags, - Export_includes: exportedIncludes.Includes, - Export_system_includes: exportedIncludes.SystemIncludes, - Local_includes: compilerAttrs.localIncludes, - Absolute_includes: compilerAttrs.absoluteIncludes, - Use_libcrt: linkerAttrs.useLibcrt, - Rtti: compilerAttrs.rtti, - Stl: compilerAttrs.stl, - Cpp_std: compilerAttrs.cppStd, - C_std: compilerAttrs.cStd, + Export_includes: exportedIncludes.Includes, + Export_absolute_includes: exportedIncludes.AbsoluteIncludes, + Export_system_includes: exportedIncludes.SystemIncludes, + Local_includes: compilerAttrs.localIncludes, + Absolute_includes: compilerAttrs.absoluteIncludes, + Use_libcrt: linkerAttrs.useLibcrt, + Rtti: compilerAttrs.rtti, + Stl: compilerAttrs.stl, + Cpp_std: compilerAttrs.cppStd, + C_std: compilerAttrs.cStd, Features: linkerAttrs.features, } @@ -370,17 +371,18 @@ func CcLibraryBp2Build(ctx android.TopDownMutatorContext) { Conlyflags: compilerAttrs.conlyFlags, Asflags: asFlags, - Export_includes: exportedIncludes.Includes, - Export_system_includes: exportedIncludes.SystemIncludes, - Local_includes: compilerAttrs.localIncludes, - Absolute_includes: compilerAttrs.absoluteIncludes, - Linkopts: linkerAttrs.linkopts, - Link_crt: linkerAttrs.linkCrt, - Use_libcrt: linkerAttrs.useLibcrt, - Rtti: compilerAttrs.rtti, - Stl: compilerAttrs.stl, - Cpp_std: compilerAttrs.cppStd, - C_std: compilerAttrs.cStd, + Export_includes: exportedIncludes.Includes, + Export_absolute_includes: exportedIncludes.AbsoluteIncludes, + Export_system_includes: exportedIncludes.SystemIncludes, + Local_includes: compilerAttrs.localIncludes, + Absolute_includes: compilerAttrs.absoluteIncludes, + Linkopts: linkerAttrs.linkopts, + Link_crt: linkerAttrs.linkCrt, + Use_libcrt: linkerAttrs.useLibcrt, + Rtti: compilerAttrs.rtti, + Stl: compilerAttrs.stl, + Cpp_std: compilerAttrs.cppStd, + C_std: compilerAttrs.cStd, Additional_linker_inputs: linkerAttrs.additionalLinkerInputs, @@ -2434,7 +2436,7 @@ func ccSharedOrStaticBp2BuildMutatorInternal(ctx android.TopDownMutatorContext, compilerAttrs := baseAttributes.compilerAttributes linkerAttrs := baseAttributes.linkerAttributes - exportedIncludes := bp2BuildParseExportedIncludes(ctx, module) + exportedIncludes := bp2BuildParseExportedIncludes(ctx, module, compilerAttrs.includes) // Append shared/static{} stanza properties. These won't be specified on // cc_library_* itself, but may be specified in cc_defaults that this module @@ -2483,14 +2485,16 @@ func ccSharedOrStaticBp2BuildMutatorInternal(ctx android.TopDownMutatorContext, Use_libcrt: linkerAttrs.useLibcrt, Use_version_lib: linkerAttrs.useVersionLib, - Rtti: compilerAttrs.rtti, - Stl: compilerAttrs.stl, - Cpp_std: compilerAttrs.cppStd, - C_std: compilerAttrs.cStd, - Export_includes: exportedIncludes.Includes, - Export_system_includes: exportedIncludes.SystemIncludes, - Local_includes: compilerAttrs.localIncludes, - Absolute_includes: compilerAttrs.absoluteIncludes, + Rtti: compilerAttrs.rtti, + Stl: compilerAttrs.stl, + Cpp_std: compilerAttrs.cppStd, + C_std: compilerAttrs.cStd, + + Export_includes: exportedIncludes.Includes, + Export_absolute_includes: exportedIncludes.AbsoluteIncludes, + Export_system_includes: exportedIncludes.SystemIncludes, + Local_includes: compilerAttrs.localIncludes, + Absolute_includes: compilerAttrs.absoluteIncludes, Cppflags: compilerAttrs.cppFlags, Conlyflags: compilerAttrs.conlyFlags, @@ -2519,6 +2523,7 @@ func ccSharedOrStaticBp2BuildMutatorInternal(ctx android.TopDownMutatorContext, C_std: compilerAttrs.cStd, Export_includes: exportedIncludes.Includes, + Export_absolute_includes: exportedIncludes.AbsoluteIncludes, Export_system_includes: exportedIncludes.SystemIncludes, Local_includes: compilerAttrs.localIncludes, Absolute_includes: compilerAttrs.absoluteIncludes, @@ -2556,11 +2561,12 @@ type bazelCcLibraryStaticAttributes struct { Cpp_std *string C_std *string - Export_includes bazel.StringListAttribute - Export_system_includes bazel.StringListAttribute - Local_includes bazel.StringListAttribute - Absolute_includes bazel.StringListAttribute - Hdrs bazel.LabelListAttribute + Export_includes bazel.StringListAttribute + Export_absolute_includes bazel.StringListAttribute + Export_system_includes bazel.StringListAttribute + Local_includes bazel.StringListAttribute + Absolute_includes bazel.StringListAttribute + Hdrs bazel.LabelListAttribute Cppflags bazel.StringListAttribute Conlyflags bazel.StringListAttribute @@ -2596,11 +2602,12 @@ type bazelCcLibrarySharedAttributes struct { Cpp_std *string C_std *string - Export_includes bazel.StringListAttribute - Export_system_includes bazel.StringListAttribute - Local_includes bazel.StringListAttribute - Absolute_includes bazel.StringListAttribute - Hdrs bazel.LabelListAttribute + Export_includes bazel.StringListAttribute + Export_absolute_includes bazel.StringListAttribute + Export_system_includes bazel.StringListAttribute + Local_includes bazel.StringListAttribute + Absolute_includes bazel.StringListAttribute + Hdrs bazel.LabelListAttribute Strip stripAttributes Additional_linker_inputs bazel.LabelListAttribute diff --git a/cc/library_headers.go b/cc/library_headers.go index 141a2a56b..ce8c39116 100644 --- a/cc/library_headers.go +++ b/cc/library_headers.go @@ -108,12 +108,13 @@ func prebuiltLibraryHeaderFactory() android.Module { } type bazelCcLibraryHeadersAttributes struct { - Hdrs bazel.LabelListAttribute - Export_includes bazel.StringListAttribute - Export_system_includes bazel.StringListAttribute - Deps bazel.LabelListAttribute - Implementation_deps bazel.LabelListAttribute - System_dynamic_deps bazel.LabelListAttribute + Hdrs bazel.LabelListAttribute + Export_includes bazel.StringListAttribute + Export_absolute_includes bazel.StringListAttribute + Export_system_includes bazel.StringListAttribute + Deps bazel.LabelListAttribute + Implementation_deps bazel.LabelListAttribute + System_dynamic_deps bazel.LabelListAttribute } func CcLibraryHeadersBp2Build(ctx android.TopDownMutatorContext) { @@ -131,17 +132,18 @@ func CcLibraryHeadersBp2Build(ctx android.TopDownMutatorContext) { return } - exportedIncludes := bp2BuildParseExportedIncludes(ctx, module) baseAttributes := bp2BuildParseBaseProps(ctx, module) + exportedIncludes := bp2BuildParseExportedIncludes(ctx, module, baseAttributes.includes) linkerAttrs := baseAttributes.linkerAttributes attrs := &bazelCcLibraryHeadersAttributes{ - Export_includes: exportedIncludes.Includes, - Export_system_includes: exportedIncludes.SystemIncludes, - Implementation_deps: linkerAttrs.implementationDeps, - Deps: linkerAttrs.deps, - System_dynamic_deps: linkerAttrs.systemDynamicDeps, - Hdrs: baseAttributes.hdrs, + Export_includes: exportedIncludes.Includes, + Export_absolute_includes: exportedIncludes.AbsoluteIncludes, + Export_system_includes: exportedIncludes.SystemIncludes, + Implementation_deps: linkerAttrs.implementationDeps, + Deps: linkerAttrs.deps, + System_dynamic_deps: linkerAttrs.systemDynamicDeps, + Hdrs: baseAttributes.hdrs, } props := bazel.BazelTargetModuleProperties{