Express no{,_lib}crt via features vs attrs
Given that we can map them directly to disabling the
corresponding Bazel features: `-{link_,use_lib}crt`
Test: Existing (adapted) Unit Tests
Test: bp2build.sh
Change-Id: Ib502f6fb929ace8e86a1001e3cc21f399317500c
diff --git a/bp2build/cc_binary_conversion_test.go b/bp2build/cc_binary_conversion_test.go
index fe156df..a39ed7d 100644
--- a/bp2build/cc_binary_conversion_test.go
+++ b/bp2build/cc_binary_conversion_test.go
@@ -365,7 +365,7 @@
{
description: "nocrt: true",
soongProperty: `nocrt: true,`,
- bazelAttr: AttrNameToString{"link_crt": `False`},
+ bazelAttr: AttrNameToString{"features": `["-link_crt"]`},
},
{
description: "nocrt: false",
@@ -408,12 +408,12 @@
{
description: "no_libcrt: true",
soongProperty: `no_libcrt: true,`,
- bazelAttr: AttrNameToString{"use_libcrt": `False`},
+ bazelAttr: AttrNameToString{"features": `["-use_libcrt"]`},
},
{
description: "no_libcrt: false",
soongProperty: `no_libcrt: false,`,
- bazelAttr: AttrNameToString{"use_libcrt": `True`},
+ bazelAttr: AttrNameToString{},
},
{
description: "no_libcrt: not set",
diff --git a/bp2build/cc_library_conversion_test.go b/bp2build/cc_library_conversion_test.go
index c11a50d..54be709 100644
--- a/bp2build/cc_library_conversion_test.go
+++ b/bp2build/cc_library_conversion_test.go
@@ -1308,7 +1308,7 @@
func TestCCLibraryNoCrtTrue(t *testing.T) {
runCcLibraryTestCase(t, Bp2buildTestCase{
- Description: "cc_library - nocrt: true emits attribute",
+ Description: "cc_library - nocrt: true disables feature",
ModuleTypeUnderTest: "cc_library",
ModuleTypeUnderTestFactory: cc.LibraryFactory,
Filesystem: map[string]string{
@@ -1323,7 +1323,7 @@
}
`,
ExpectedBazelTargets: makeCcLibraryTargets("foo-lib", AttrNameToString{
- "link_crt": `False`,
+ "features": `["-link_crt"]`,
"srcs": `["impl.cpp"]`,
}),
},
@@ -1375,7 +1375,13 @@
include_build_directory: false,
}
`,
- ExpectedErr: fmt.Errorf("module \"foo-lib\": nocrt is not supported for arch variants"),
+ ExpectedBazelTargets: makeCcLibraryTargets("foo-lib", AttrNameToString{
+ "features": `select({
+ "//build/bazel/platforms/arch:arm": ["-link_crt"],
+ "//conditions:default": [],
+ })`,
+ "srcs": `["impl.cpp"]`,
+ }),
})
}
@@ -1395,8 +1401,8 @@
}
`,
ExpectedBazelTargets: makeCcLibraryTargets("foo-lib", AttrNameToString{
- "srcs": `["impl.cpp"]`,
- "use_libcrt": `False`,
+ "features": `["-use_libcrt"]`,
+ "srcs": `["impl.cpp"]`,
}),
})
}
@@ -1445,8 +1451,7 @@
}
`,
ExpectedBazelTargets: makeCcLibraryTargets("foo-lib", AttrNameToString{
- "srcs": `["impl.cpp"]`,
- "use_libcrt": `True`,
+ "srcs": `["impl.cpp"]`,
}),
})
}
@@ -1475,10 +1480,10 @@
`,
ExpectedBazelTargets: makeCcLibraryTargets("foo-lib", AttrNameToString{
"srcs": `["impl.cpp"]`,
- "use_libcrt": `select({
- "//build/bazel/platforms/arch:arm": False,
- "//build/bazel/platforms/arch:x86": False,
- "//conditions:default": None,
+ "features": `select({
+ "//build/bazel/platforms/arch:arm": ["-use_libcrt"],
+ "//build/bazel/platforms/arch:x86": ["-use_libcrt"],
+ "//conditions:default": [],
})`,
}),
})
@@ -1512,17 +1517,15 @@
}
`,
ExpectedBazelTargets: makeCcLibraryTargets("foo-lib", AttrNameToString{
- "srcs": `["impl.cpp"]`,
- "use_libcrt": `select({
- "//build/bazel/platforms/os_arch:android_arm": False,
- "//build/bazel/platforms/os_arch:android_x86": False,
- "//build/bazel/platforms/os_arch:darwin_arm64": False,
- "//build/bazel/platforms/os_arch:darwin_x86_64": False,
- "//build/bazel/platforms/os_arch:linux_glibc_x86": False,
- "//build/bazel/platforms/os_arch:linux_musl_x86": False,
- "//build/bazel/platforms/os_arch:windows_x86": False,
- "//conditions:default": None,
+ "features": `select({
+ "//build/bazel/platforms/arch:arm": ["-use_libcrt"],
+ "//build/bazel/platforms/arch:x86": ["-use_libcrt"],
+ "//conditions:default": [],
+ }) + select({
+ "//build/bazel/platforms/os:darwin": ["-use_libcrt"],
+ "//conditions:default": [],
})`,
+ "srcs": `["impl.cpp"]`,
}),
})
}
@@ -1557,16 +1560,10 @@
`,
ExpectedBazelTargets: makeCcLibraryTargets("foo-lib", AttrNameToString{
"srcs": `["impl.cpp"]`,
- "use_libcrt": `select({
- "//build/bazel/platforms/os_arch:android_arm": False,
- "//build/bazel/platforms/os_arch:android_x86_64": False,
- "//build/bazel/platforms/os_arch:darwin_arm64": True,
- "//build/bazel/platforms/os_arch:darwin_x86_64": False,
- "//build/bazel/platforms/os_arch:linux_bionic_x86_64": False,
- "//build/bazel/platforms/os_arch:linux_glibc_x86_64": False,
- "//build/bazel/platforms/os_arch:linux_musl_x86_64": False,
- "//build/bazel/platforms/os_arch:windows_x86_64": False,
- "//conditions:default": None,
+ "features": `select({
+ "//build/bazel/platforms/arch:arm": ["-use_libcrt"],
+ "//build/bazel/platforms/arch:x86_64": ["-use_libcrt"],
+ "//conditions:default": [],
})`,
}),
})
diff --git a/bp2build/cc_library_shared_conversion_test.go b/bp2build/cc_library_shared_conversion_test.go
index 017df6f..6207421 100644
--- a/bp2build/cc_library_shared_conversion_test.go
+++ b/bp2build/cc_library_shared_conversion_test.go
@@ -15,7 +15,6 @@
package bp2build
import (
- "fmt"
"testing"
"android/soong/android"
@@ -405,7 +404,7 @@
func TestCcLibrarySharedNoCrtTrue(t *testing.T) {
runCcLibrarySharedTestCase(t, Bp2buildTestCase{
- Description: "cc_library_shared - nocrt: true emits attribute",
+ Description: "cc_library_shared - nocrt: true disables feature",
Filesystem: map[string]string{
"impl.cpp": "",
},
@@ -419,7 +418,7 @@
`,
ExpectedBazelTargets: []string{
MakeBazelTarget("cc_library_shared", "foo_shared", AttrNameToString{
- "link_crt": `False`,
+ "features": `["-link_crt"]`,
"srcs": `["impl.cpp"]`,
}),
},
@@ -428,7 +427,7 @@
func TestCcLibrarySharedNoCrtFalse(t *testing.T) {
runCcLibrarySharedTestCase(t, Bp2buildTestCase{
- Description: "cc_library_shared - nocrt: false doesn't emit attribute",
+ Description: "cc_library_shared - nocrt: false doesn't disable feature",
Filesystem: map[string]string{
"impl.cpp": "",
},
@@ -469,7 +468,15 @@
include_build_directory: false,
}
`,
- ExpectedErr: fmt.Errorf("module \"foo_shared\": nocrt is not supported for arch variants"),
+ ExpectedBazelTargets: []string{
+ MakeBazelTarget("cc_library_shared", "foo_shared", AttrNameToString{
+ "features": `select({
+ "//build/bazel/platforms/arch:arm": ["-link_crt"],
+ "//conditions:default": [],
+ })`,
+ "srcs": `["impl.cpp"]`,
+ }),
+ },
})
}
diff --git a/cc/binary.go b/cc/binary.go
index a04b174..532b42a 100644
--- a/cc/binary.go
+++ b/cc/binary.go
@@ -151,7 +151,7 @@
// modules common to most binaries, such as bionic libraries.
func (binary *binaryDecorator) linkerDeps(ctx DepsContext, deps Deps) Deps {
deps = binary.baseLinker.linkerDeps(ctx, deps)
- if !Bool(binary.baseLinker.Properties.Nocrt) {
+ if binary.baseLinker.Properties.crt() {
if binary.static() {
deps.CrtBegin = ctx.toolchain().CrtBeginStaticBinary()
deps.CrtEnd = ctx.toolchain().CrtEndStaticBinary()
@@ -630,8 +630,6 @@
Local_includes: baseAttrs.localIncludes,
Absolute_includes: baseAttrs.absoluteIncludes,
Linkopts: baseAttrs.linkopts,
- Link_crt: baseAttrs.linkCrt,
- Use_libcrt: baseAttrs.useLibcrt,
Use_version_lib: baseAttrs.useVersionLib,
Rtti: baseAttrs.rtti,
Stl: baseAttrs.stl,
@@ -695,10 +693,7 @@
Linkopts bazel.StringListAttribute
Additional_linker_inputs bazel.LabelListAttribute
-
- Link_crt bazel.BoolAttribute
- Use_libcrt bazel.BoolAttribute
- Use_version_lib bazel.BoolAttribute
+ Use_version_lib bazel.BoolAttribute
Rtti bazel.BoolAttribute
Stl *string
diff --git a/cc/bp2build.go b/cc/bp2build.go
index 6c5505a..250241c 100644
--- a/cc/bp2build.go
+++ b/cc/bp2build.go
@@ -965,8 +965,6 @@
systemDynamicDeps bazel.LabelListAttribute
usedSystemDynamicDepAsDynamicDep map[string]bool
- linkCrt bazel.BoolAttribute
- useLibcrt bazel.BoolAttribute
useVersionLib bazel.BoolAttribute
linkopts bazel.StringListAttribute
additionalLinkerInputs bazel.LabelListAttribute
@@ -1138,6 +1136,13 @@
}
}
+ if !props.libCrt() {
+ axisFeatures = append(axisFeatures, "-use_libcrt")
+ }
+ if !props.crt() {
+ axisFeatures = append(axisFeatures, "-link_crt")
+ }
+
// This must happen before the addition of flags for Version Script and
// Dynamic List, as these flags must be split on spaces and those must not
linkerFlags = parseCommandLineFlags(linkerFlags, filterOutClangUnknownCflags)
@@ -1157,16 +1162,6 @@
la.additionalLinkerInputs.SetSelectValue(axis, config, additionalLinkerInputs)
la.linkopts.SetSelectValue(axis, config, linkerFlags)
- la.useLibcrt.SetSelectValue(axis, config, props.libCrt())
-
- // it's very unlikely for nocrt to be arch variant, so bp2build doesn't support it.
- if props.crt() != nil {
- if axis == bazel.NoConfigAxis {
- la.linkCrt.SetSelectValue(axis, config, props.crt())
- } else if axis == bazel.ArchConfigurationAxis {
- ctx.ModuleErrorf("nocrt is not supported for arch variants")
- }
- }
if axisFeatures != nil {
la.features.SetSelectValue(axis, config, axisFeatures)
diff --git a/cc/library.go b/cc/library.go
index 9421007..f33f37e 100644
--- a/cc/library.go
+++ b/cc/library.go
@@ -243,7 +243,6 @@
Local_includes bazel.StringListAttribute
Absolute_includes bazel.StringListAttribute
Linkopts bazel.StringListAttribute
- Use_libcrt bazel.BoolAttribute
Rtti bazel.BoolAttribute
Stl *string
@@ -251,7 +250,6 @@
C_std *string
// This is shared only.
- Link_crt bazel.BoolAttribute
Additional_linker_inputs bazel.LabelListAttribute
// Common properties shared between both shared and static variants.
@@ -360,7 +358,6 @@
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,
@@ -381,8 +378,6 @@
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,
@@ -1500,7 +1495,7 @@
deps.ReexportSharedLibHeaders = append(deps.ReexportSharedLibHeaders, library.StaticProperties.Static.Export_shared_lib_headers...)
deps.ReexportStaticLibHeaders = append(deps.ReexportStaticLibHeaders, library.StaticProperties.Static.Export_static_lib_headers...)
} else if library.shared() {
- if !Bool(library.baseLinker.Properties.Nocrt) {
+ if library.baseLinker.Properties.crt() {
deps.CrtBegin = append(deps.CrtBegin, ctx.toolchain().CrtBeginSharedLibrary()...)
deps.CrtEnd = append(deps.CrtEnd, ctx.toolchain().CrtEndSharedLibrary()...)
}
@@ -2884,13 +2879,10 @@
commonAttrs.Deps.Add(baseAttributes.protoDependency)
attrs = &bazelCcLibraryStaticAttributes{
staticOrSharedAttributes: commonAttrs,
-
- Use_libcrt: linkerAttrs.useLibcrt,
-
- Rtti: compilerAttrs.rtti,
- Stl: compilerAttrs.stl,
- Cpp_std: compilerAttrs.cppStd,
- C_std: compilerAttrs.cStd,
+ Rtti: compilerAttrs.rtti,
+ Stl: compilerAttrs.stl,
+ Cpp_std: compilerAttrs.cppStd,
+ C_std: compilerAttrs.cStd,
Export_includes: exportedIncludes.Includes,
Export_absolute_includes: exportedIncludes.AbsoluteIncludes,
@@ -2915,8 +2907,6 @@
Asflags: asFlags,
Linkopts: linkerAttrs.linkopts,
- Link_crt: linkerAttrs.linkCrt,
- Use_libcrt: linkerAttrs.useLibcrt,
Use_version_lib: linkerAttrs.useVersionLib,
Rtti: compilerAttrs.rtti,
@@ -2974,13 +2964,11 @@
type bazelCcLibraryStaticAttributes struct {
staticOrSharedAttributes
- Use_libcrt bazel.BoolAttribute
Use_version_lib bazel.BoolAttribute
-
- Rtti bazel.BoolAttribute
- Stl *string
- Cpp_std *string
- C_std *string
+ Rtti bazel.BoolAttribute
+ Stl *string
+ Cpp_std *string
+ C_std *string
Export_includes bazel.StringListAttribute
Export_absolute_includes bazel.StringListAttribute
@@ -3000,10 +2988,7 @@
type bazelCcLibrarySharedAttributes struct {
staticOrSharedAttributes
- Linkopts bazel.StringListAttribute
- Link_crt bazel.BoolAttribute // Only for linking shared library (and cc_binary)
-
- Use_libcrt bazel.BoolAttribute
+ Linkopts bazel.StringListAttribute
Use_version_lib bazel.BoolAttribute
Rtti bazel.BoolAttribute
diff --git a/cc/linker.go b/cc/linker.go
index 371d78d..e49b97d 100644
--- a/cc/linker.go
+++ b/cc/linker.go
@@ -237,29 +237,14 @@
Exclude_shared_libs []string `android:"arch_variant"`
}
-func invertBoolPtr(value *bool) *bool {
- if value == nil {
- return nil
- }
- ret := !(*value)
- return &ret
+func (blp *BaseLinkerProperties) crt() bool {
+ // Since crt is enabled for almost every module compiling against the Bionic runtime,
+ // we interpret `nil` as enabled.
+ return blp.Nocrt == nil || !*blp.Nocrt
}
-func (blp *BaseLinkerProperties) crt() *bool {
- val := invertBoolPtr(blp.Nocrt)
- if val != nil && *val {
- // == True
- //
- // Since crt is enabled for almost every module compiling against the Bionic runtime,
- // use `nil` when it's enabled, and rely on the Starlark macro to set it to True by default.
- // This keeps the BUILD files clean.
- return nil
- }
- return val // can be False or nil
-}
-
-func (blp *BaseLinkerProperties) libCrt() *bool {
- return invertBoolPtr(blp.No_libcrt)
+func (blp *BaseLinkerProperties) libCrt() bool {
+ return blp.No_libcrt == nil || !*blp.No_libcrt
}
func NewBaseLinker(sanitize *sanitize) *baseLinker {
@@ -392,7 +377,7 @@
if ctx.toolchain().Bionic() {
// libclang_rt.builtins has to be last on the command line
- if !Bool(linker.Properties.No_libcrt) && !ctx.header() {
+ if linker.Properties.libCrt() && !ctx.header() {
deps.UnexportedStaticLibs = append(deps.UnexportedStaticLibs, config.BuiltinsRuntimeLibrary(ctx.toolchain()))
}
@@ -415,7 +400,7 @@
ctx.PropertyErrorf("system_shared_libs", "libdl must be after libc")
}
} else if ctx.toolchain().Musl() {
- if !Bool(linker.Properties.No_libcrt) && !ctx.header() {
+ if linker.Properties.libCrt() && !ctx.header() {
deps.UnexportedStaticLibs = append(deps.UnexportedStaticLibs, config.BuiltinsRuntimeLibrary(ctx.toolchain()))
}
}