From 107c0de80e8f7f2b303211e25ee28a12b059f3f5 Mon Sep 17 00:00:00 2001 From: Jingwen Chen Date: Fri, 9 Apr 2021 10:43:12 +0000 Subject: [PATCH] bp2build: refactor compiler/linker prop function. This changes the return value into a compiler/linker attr struct to standardize callsites and make it easier to retrieve the parsed attrs. Test: TH Change-Id: I1e3956e7cb5d924ce8472ece940faea459beef37 --- cc/bp2build.go | 36 ++++++++++++++++++++++++++---------- cc/library.go | 36 +++++++++++++++++++----------------- cc/library_headers.go | 8 ++++---- cc/object.go | 14 ++++---------- 4 files changed, 53 insertions(+), 41 deletions(-) diff --git a/cc/bp2build.go b/cc/bp2build.go index e7e4aa8b0..0bca30a75 100644 --- a/cc/bp2build.go +++ b/cc/bp2build.go @@ -59,13 +59,17 @@ func depsBp2BuildMutator(ctx android.BottomUpMutatorContext) { ctx.AddDependency(module, nil, android.SortedUniqueStrings(allDeps)...) } +// Convenience struct to hold all attributes parsed from compiler properties. +type compilerAttributes struct { + copts bazel.StringListAttribute + srcs bazel.LabelListAttribute + hdrs bazel.LabelListAttribute +} + // bp2BuildParseCompilerProps returns copts, srcs and hdrs and other attributes. -func bp2BuildParseCompilerProps( - ctx android.TopDownMutatorContext, - module *Module) ( - copts bazel.StringListAttribute, - srcs bazel.LabelListAttribute, - hdrs bazel.LabelListAttribute) { +func bp2BuildParseCompilerProps(ctx android.TopDownMutatorContext, module *Module) compilerAttributes { + var hdrs, srcs bazel.LabelListAttribute + var copts bazel.StringListAttribute hdrsAndSrcs := func(baseCompilerProps *BaseCompilerProperties) (bazel.LabelList, bazel.LabelList) { srcsList := android.BazelLabelForModuleSrcExcludes( @@ -100,13 +104,22 @@ func bp2BuildParseCompilerProps( } } - return copts, srcs, hdrs + return compilerAttributes{ + hdrs: hdrs, + srcs: srcs, + copts: copts, + } +} + +// Convenience struct to hold all attributes parsed from linker properties. +type linkerAttributes struct { + deps bazel.LabelListAttribute + linkopts bazel.StringListAttribute } // bp2BuildParseLinkerProps creates a label list attribute containing the header library deps of a module, including // configurable attribute values. -func bp2BuildParseLinkerProps( - ctx android.TopDownMutatorContext, module *Module) (bazel.LabelListAttribute, bazel.StringListAttribute) { +func bp2BuildParseLinkerProps(ctx android.TopDownMutatorContext, module *Module) linkerAttributes { var deps bazel.LabelListAttribute var linkopts bazel.StringListAttribute @@ -142,7 +155,10 @@ func bp2BuildParseLinkerProps( } } - return deps, linkopts + return linkerAttributes{ + deps: deps, + linkopts: linkopts, + } } func bp2BuildListHeadersInDir(ctx android.TopDownMutatorContext, includeDir string) bazel.LabelList { diff --git a/cc/library.go b/cc/library.go index 0ebcbaad5..738b45fc7 100644 --- a/cc/library.go +++ b/cc/library.go @@ -256,17 +256,17 @@ func CcLibraryBp2Build(ctx android.TopDownMutatorContext) { return } - copts, srcs, hdrs := bp2BuildParseCompilerProps(ctx, m) - deps, linkopts := bp2BuildParseLinkerProps(ctx, m) + compilerAttrs := bp2BuildParseCompilerProps(ctx, m) + linkerAttrs := bp2BuildParseLinkerProps(ctx, m) exportedIncludes, exportedIncludesHeaders := bp2BuildParseExportedIncludes(ctx, m) - hdrs.Append(exportedIncludesHeaders) + compilerAttrs.hdrs.Append(exportedIncludesHeaders) attrs := &bazelCcLibraryAttributes{ - Srcs: srcs, - Hdrs: hdrs, - Copts: copts, - Linkopts: linkopts, - Deps: deps, + Srcs: compilerAttrs.srcs, + Hdrs: compilerAttrs.hdrs, + Copts: compilerAttrs.copts, + Linkopts: linkerAttrs.linkopts, + Deps: linkerAttrs.deps, Includes: exportedIncludes, } @@ -2154,12 +2154,13 @@ func CcLibraryStaticBp2Build(ctx android.TopDownMutatorContext) { return } - copts, srcs, hdrs := bp2BuildParseCompilerProps(ctx, module) + compilerAttrs := bp2BuildParseCompilerProps(ctx, module) var includeDirs []string var localIncludeDirs []string for _, props := range module.compiler.compilerProps() { if baseCompilerProps, ok := props.(*BaseCompilerProperties); ok { + // TODO: these should be arch and os specific. includeDirs = bp2BuildMakePathsRelativeToModule(ctx, baseCompilerProps.Include_dirs) localIncludeDirs = bp2BuildMakePathsRelativeToModule(ctx, baseCompilerProps.Local_include_dirs) break @@ -2174,15 +2175,16 @@ func CcLibraryStaticBp2Build(ctx android.TopDownMutatorContext) { // For Bazel, be more explicit about headers - list all header files in include dirs as srcs for _, includeDir := range includeDirs { - srcs.Value.Append(bp2BuildListHeadersInDir(ctx, includeDir)) + compilerAttrs.srcs.Value.Append(bp2BuildListHeadersInDir(ctx, includeDir)) } for _, localIncludeDir := range localIncludeDirs { - srcs.Value.Append(bp2BuildListHeadersInDir(ctx, localIncludeDir)) + compilerAttrs.srcs.Value.Append(bp2BuildListHeadersInDir(ctx, localIncludeDir)) } var staticLibs []string var wholeStaticLibs []string for _, props := range module.linker.linkerProps() { + // TODO: move this into bp2buildParseLinkerProps if baseLinkerProperties, ok := props.(*BaseLinkerProperties); ok { staticLibs = baseLinkerProperties.Static_libs wholeStaticLibs = baseLinkerProperties.Whole_static_libs @@ -2204,18 +2206,18 @@ func CcLibraryStaticBp2Build(ctx android.TopDownMutatorContext) { allIncludes.Value = append(allIncludes.Value, includeDirs...) allIncludes.Value = append(allIncludes.Value, localIncludeDirs...) - hdrs.Append(exportedIncludesHeaders) + compilerAttrs.hdrs.Append(exportedIncludesHeaders) - headerLibsLabels, _ := bp2BuildParseLinkerProps(ctx, module) - depsLabels.Append(headerLibsLabels.Value) + linkerAttrs := bp2BuildParseLinkerProps(ctx, module) + depsLabels.Append(linkerAttrs.deps.Value) attrs := &bazelCcLibraryStaticAttributes{ - Copts: copts, - Srcs: srcs, + Copts: compilerAttrs.copts, + Srcs: compilerAttrs.srcs, Deps: bazel.MakeLabelListAttribute(depsLabels), Linkstatic: true, Includes: allIncludes, - Hdrs: hdrs, + Hdrs: compilerAttrs.hdrs, } props := bazel.BazelTargetModuleProperties{ diff --git a/cc/library_headers.go b/cc/library_headers.go index 0f7f8f846..076ce80a5 100644 --- a/cc/library_headers.go +++ b/cc/library_headers.go @@ -96,14 +96,14 @@ func CcLibraryHeadersBp2Build(ctx android.TopDownMutatorContext) { } exportedIncludes, exportedIncludesHeaders := bp2BuildParseExportedIncludes(ctx, module) - copts, _, _ := bp2BuildParseCompilerProps(ctx, module) - headerLibs, _ := bp2BuildParseLinkerProps(ctx, module) + compilerAttrs := bp2BuildParseCompilerProps(ctx, module) + linkerAttrs := bp2BuildParseLinkerProps(ctx, module) attrs := &bazelCcLibraryHeadersAttributes{ - Copts: copts, + Copts: compilerAttrs.copts, Includes: exportedIncludes, Hdrs: exportedIncludesHeaders, - Deps: headerLibs, + Deps: linkerAttrs.deps, } props := bazel.BazelTargetModuleProperties{ diff --git a/cc/object.go b/cc/object.go index de45293dc..9bb279a3b 100644 --- a/cc/object.go +++ b/cc/object.go @@ -157,7 +157,7 @@ func ObjectBp2Build(ctx android.TopDownMutatorContext) { } // Set arch-specific configurable attributes - copts, srcs, hdrs := bp2BuildParseCompilerProps(ctx, m) + compilerAttrs := bp2BuildParseCompilerProps(ctx, m) var localIncludeDirs []string var asFlags []string for _, props := range m.compiler.compilerProps() { @@ -196,17 +196,11 @@ func ObjectBp2Build(ctx android.TopDownMutatorContext) { } // TODO(b/183595872) warn/error if we're not handling product variables - for arch, p := range m.GetArchProperties(&BaseCompilerProperties{}) { - if cProps, ok := p.(*BaseCompilerProperties); ok { - srcs.SetValueForArch(arch.Name, android.BazelLabelForModuleSrcExcludes(ctx, cProps.Srcs, cProps.Exclude_srcs)) - } - } - attrs := &bazelObjectAttributes{ - Srcs: srcs, - Hdrs: hdrs, + Srcs: compilerAttrs.srcs, + Hdrs: compilerAttrs.hdrs, Deps: deps, - Copts: copts, + Copts: compilerAttrs.copts, Asflags: asFlags, Local_include_dirs: localIncludeDirs, }