diff --git a/bp2build/cc_library_conversion_test.go b/bp2build/cc_library_conversion_test.go index 364929cb7..c543ef8cf 100644 --- a/bp2build/cc_library_conversion_test.go +++ b/bp2build/cc_library_conversion_test.go @@ -331,6 +331,68 @@ cc_library { )`, `cc_library( name = "mylib", copts = ["-Ifoo/bar"], +)`}, + }, + { + description: "cc_library pack_relocations test", + moduleTypeUnderTest: "cc_library", + moduleTypeUnderTestFactory: cc.LibraryFactory, + moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryBp2Build, + depsMutators: []android.RegisterMutatorFunc{cc.RegisterDepsBp2Build}, + dir: "foo/bar", + filesystem: map[string]string{ + "foo/bar/Android.bp": ` +cc_library { + name: "a", + srcs: ["a.cpp"], + pack_relocations: false, + bazel_module: { bp2build_available: true }, +} + +cc_library { + name: "b", + srcs: ["b.cpp"], + arch: { + x86_64: { + pack_relocations: false, + }, + }, + bazel_module: { bp2build_available: true }, +} + +cc_library { + name: "c", + srcs: ["c.cpp"], + target: { + darwin: { + pack_relocations: false, + }, + }, + bazel_module: { bp2build_available: true }, +}`, + }, + bp: soongCcLibraryPreamble, + expectedBazelTargets: []string{`cc_library( + name = "a", + copts = ["-Ifoo/bar"], + linkopts = ["-Wl,--pack-dyn-relocs=none"], + srcs = ["a.cpp"], +)`, `cc_library( + name = "b", + copts = ["-Ifoo/bar"], + linkopts = select({ + "//build/bazel/platforms/arch:x86_64": ["-Wl,--pack-dyn-relocs=none"], + "//conditions:default": [], + }), + srcs = ["b.cpp"], +)`, `cc_library( + name = "c", + copts = ["-Ifoo/bar"], + linkopts = select({ + "//build/bazel/platforms/os:darwin": ["-Wl,--pack-dyn-relocs=none"], + "//conditions:default": [], + }), + srcs = ["c.cpp"], )`}, }, } diff --git a/cc/bp2build.go b/cc/bp2build.go index 7f2554fc1..67f88e26a 100644 --- a/cc/bp2build.go +++ b/cc/bp2build.go @@ -252,6 +252,15 @@ type linkerAttributes struct { versionScript bazel.LabelAttribute } +// FIXME(b/187655838): Use the existing linkerFlags() function instead of duplicating logic here +func getBp2BuildLinkerFlags(linkerProperties *BaseLinkerProperties) []string { + flags := linkerProperties.Ldflags + if !BoolDefault(linkerProperties.Pack_relocations, true) { + flags = append(flags, "-Wl,--pack-dyn-relocs=none") + } + return flags +} + // bp2BuildParseLinkerProps parses the linker properties of a module, including // configurable attribute values. func bp2BuildParseLinkerProps(ctx android.TopDownMutatorContext, module *Module) linkerAttributes { @@ -269,7 +278,7 @@ func bp2BuildParseLinkerProps(ctx android.TopDownMutatorContext, module *Module) libs = android.SortedUniqueStrings(libs) deps = bazel.MakeLabelListAttribute(android.BazelLabelForModuleDeps(ctx, libs)) - linkopts.Value = baseLinkerProps.Ldflags + linkopts.Value = getBp2BuildLinkerFlags(baseLinkerProps) if baseLinkerProps.Version_script != nil { versionScript.Value = android.BazelLabelForModuleSrcSingle(ctx, *baseLinkerProps.Version_script) @@ -291,7 +300,7 @@ func bp2BuildParseLinkerProps(ctx android.TopDownMutatorContext, module *Module) libs = android.SortedUniqueStrings(libs) deps.SetValueForArch(arch.Name, android.BazelLabelForModuleDeps(ctx, libs)) - linkopts.SetValueForArch(arch.Name, baseLinkerProps.Ldflags) + linkopts.SetValueForArch(arch.Name, getBp2BuildLinkerFlags(baseLinkerProps)) if baseLinkerProps.Version_script != nil { versionScript.SetValueForArch(arch.Name, @@ -312,7 +321,7 @@ func bp2BuildParseLinkerProps(ctx android.TopDownMutatorContext, module *Module) libs = android.SortedUniqueStrings(libs) deps.SetValueForOS(os.Name, android.BazelLabelForModuleDeps(ctx, libs)) - linkopts.SetValueForOS(os.Name, baseLinkerProps.Ldflags) + linkopts.SetValueForOS(os.Name, getBp2BuildLinkerFlags(baseLinkerProps)) sharedLibs := baseLinkerProps.Shared_libs dynamicDeps.SetValueForOS(os.Name, android.BazelLabelForModuleDeps(ctx, sharedLibs))