Merge "Generate wrapper lib.rs with one module per sysprop file." into main

This commit is contained in:
Treehugger Robot 2024-03-18 22:41:05 +00:00 committed by Gerrit Code Review
commit e430878f48
2 changed files with 60 additions and 36 deletions

View file

@ -54,15 +54,13 @@ type syspropJavaGenRule struct {
} }
type syspropRustGenRule struct { type syspropRustGenRule struct {
android.ModuleBase *rust.BaseSourceProvider
properties syspropGenProperties properties rustLibraryProperties
genSrcs android.Paths
} }
var _ android.OutputFileProducer = (*syspropJavaGenRule)(nil) var _ android.OutputFileProducer = (*syspropJavaGenRule)(nil)
var _ android.OutputFileProducer = (*syspropRustGenRule)(nil) var _ rust.SourceProvider = (*syspropRustGenRule)(nil)
var ( var (
syspropJava = pctx.AndroidStaticRule("syspropJava", syspropJava = pctx.AndroidStaticRule("syspropJava",
@ -144,7 +142,7 @@ func syspropJavaGenFactory() android.Module {
// syspropRustGenRule module generates rust source files containing generated rust APIs. // syspropRustGenRule module generates rust source files containing generated rust APIs.
// It also depends on check api rule, so api check has to pass to use sysprop_library. // It also depends on check api rule, so api check has to pass to use sysprop_library.
func (g *syspropRustGenRule) GenerateAndroidBuildActions(ctx android.ModuleContext) { func (g *syspropRustGenRule) GenerateSource(ctx rust.ModuleContext, deps rust.PathDeps) android.Path {
var checkApiFileTimeStamp android.WritablePath var checkApiFileTimeStamp android.WritablePath
ctx.VisitDirectDeps(func(dep android.Module) { ctx.VisitDirectDeps(func(dep android.Module) {
@ -153,25 +151,47 @@ func (g *syspropRustGenRule) GenerateAndroidBuildActions(ctx android.ModuleConte
} }
}) })
for _, syspropFile := range android.PathsForModuleSrc(ctx, g.properties.Srcs) { outputDir := android.PathForModuleOut(ctx, "src")
syspropDir := android.GenPathWithExt(ctx, "sysprop", syspropFile, "srcrust") libFile := outputDir.Join(ctx, "lib.rs")
outputDir := syspropDir.Join(ctx, "src") g.BaseSourceProvider.OutputFiles = append(g.BaseSourceProvider.OutputFiles, libFile)
libPath := syspropDir.Join(ctx, "src", "lib.rs") libFileLines := []string{"//! Autogenerated system property accessors."}
for _, syspropFile := range android.PathsForModuleSrc(ctx, g.properties.Sysprop_srcs) {
moduleName := syspropPathToRustModule(syspropFile)
moduleDir := outputDir.Join(ctx, moduleName)
modulePath := moduleDir.Join(ctx, "mod.rs")
ctx.Build(pctx, android.BuildParams{ ctx.Build(pctx, android.BuildParams{
Rule: syspropRust, Rule: syspropRust,
Description: "sysprop_rust " + syspropFile.Rel(), Description: "sysprop_rust " + syspropFile.Rel(),
Outputs: android.WritablePaths{libPath}, Output: modulePath,
Input: syspropFile, Input: syspropFile,
Implicit: checkApiFileTimeStamp, Implicit: checkApiFileTimeStamp,
Args: map[string]string{ Args: map[string]string{
"scope": g.properties.Scope, "scope": g.properties.Scope,
"out_dir": outputDir.String(), "out_dir": moduleDir.String(),
}, },
}) })
g.genSrcs = append(g.genSrcs, libPath) g.BaseSourceProvider.OutputFiles = append(g.BaseSourceProvider.OutputFiles, modulePath)
libFileLines = append(libFileLines, fmt.Sprintf("pub mod %s;", moduleName))
} }
libFileSource := strings.Join(libFileLines, "\n")
android.WriteFileRule(ctx, libFile, libFileSource)
return libFile
}
func (g *syspropRustGenRule) SourceProviderProps() []interface{} {
return append(g.BaseSourceProvider.SourceProviderProps(), &g.Properties)
}
// syspropPathToRustModule takes a path to a .sysprop file and returns the name to use for the
// corresponding Rust module.
func syspropPathToRustModule(syspropFilename android.Path) string {
filenameBase := strings.TrimSuffix(syspropFilename.Base(), ".sysprop")
return strings.ToLower(filenameBase)
} }
func (g *syspropRustGenRule) DepsMutator(ctx android.BottomUpMutatorContext) { func (g *syspropRustGenRule) DepsMutator(ctx android.BottomUpMutatorContext) {
@ -180,15 +200,13 @@ func (g *syspropRustGenRule) DepsMutator(ctx android.BottomUpMutatorContext) {
ctx.AddFarVariationDependencies(nil, nil, proptools.String(g.properties.Check_api)) ctx.AddFarVariationDependencies(nil, nil, proptools.String(g.properties.Check_api))
} }
func (g *syspropRustGenRule) OutputFiles(_ string) (android.Paths, error) {
return g.genSrcs, nil
}
func syspropRustGenFactory() android.Module { func syspropRustGenFactory() android.Module {
g := &syspropRustGenRule{} g := &syspropRustGenRule{
g.AddProperties(&g.properties) BaseSourceProvider: rust.NewSourceProvider(),
android.InitAndroidModule(g) }
return g sourceProvider := rust.NewSourceProviderModule(android.DeviceSupported, g, false, false)
sourceProvider.AddProperties(&g.properties)
return sourceProvider.Init()
} }
type syspropLibrary struct { type syspropLibrary struct {
@ -308,10 +326,6 @@ func (m *syspropLibrary) javaGenPublicStubName() string {
return m.BaseModuleName() + "_java_gen_public" return m.BaseModuleName() + "_java_gen_public"
} }
func (m *syspropLibrary) rustGenModuleName() string {
return m.rustCrateName() + "_rust_gen"
}
func (m *syspropLibrary) rustGenStubName() string { func (m *syspropLibrary) rustGenStubName() string {
return "lib" + m.rustCrateName() + "_rust" return "lib" + m.rustCrateName() + "_rust"
} }
@ -528,6 +542,9 @@ type javaLibraryProperties struct {
type rustLibraryProperties struct { type rustLibraryProperties struct {
Name *string Name *string
Sysprop_srcs []string `android:"path"`
Scope string
Check_api *string
Srcs []string Srcs []string
Installable *bool Installable *bool
Crate_name string Crate_name string
@ -667,18 +684,15 @@ func syspropLibraryHook(ctx android.LoadHookContext, m *syspropLibrary) {
} }
// Generate a Rust implementation library. // Generate a Rust implementation library.
ctx.CreateModule(syspropRustGenFactory, &syspropGenProperties{
Srcs: m.properties.Srcs,
Scope: scope,
Name: proptools.StringPtr(m.rustGenModuleName()),
Check_api: proptools.StringPtr(ctx.ModuleName()),
})
rustProps := rustLibraryProperties{ rustProps := rustLibraryProperties{
Name: proptools.StringPtr(m.rustGenStubName()), Name: proptools.StringPtr(m.rustGenStubName()),
Srcs: []string{":" + m.rustGenModuleName()}, Sysprop_srcs: m.properties.Srcs,
Installable: proptools.BoolPtr(false), Scope: scope,
Crate_name: m.rustCrateName(), Check_api: proptools.StringPtr(ctx.ModuleName()),
Installable: proptools.BoolPtr(false),
Crate_name: m.rustCrateName(),
Rustlibs: []string{ Rustlibs: []string{
"liblog_rust",
"librustutils", "librustutils",
}, },
Vendor_available: m.properties.Vendor_available, Vendor_available: m.properties.Vendor_available,
@ -686,7 +700,7 @@ func syspropLibraryHook(ctx android.LoadHookContext, m *syspropLibrary) {
Apex_available: m.ApexProperties.Apex_available, Apex_available: m.ApexProperties.Apex_available,
Min_sdk_version: proptools.StringPtr("29"), Min_sdk_version: proptools.StringPtr("29"),
} }
ctx.CreateModule(rust.RustLibraryFactory, &rustProps) ctx.CreateModule(syspropRustGenFactory, &rustProps)
// syspropLibraries will be used by property_contexts to check types. // syspropLibraries will be used by property_contexts to check types.
// Record absolute paths of sysprop_library to prevent soong_namespace problem. // Record absolute paths of sysprop_library to prevent soong_namespace problem.

View file

@ -72,6 +72,15 @@ func test(t *testing.T, bp string) *android.TestResult {
vendor_available: true, vendor_available: true,
min_sdk_version: "29", min_sdk_version: "29",
} }
rust_library {
name: "liblog_rust",
crate_name: "log",
srcs: ["log/src/lib.rs"],
product_available: true,
vendor_available: true,
min_sdk_version: "29",
}
` `
mockFS := android.MockFS{ mockFS := android.MockFS{
@ -115,6 +124,7 @@ func test(t *testing.T, bp string) *android.TestResult {
"com/android2/OdmProperties.sysprop": nil, "com/android2/OdmProperties.sysprop": nil,
"librustutils/lib.rs": nil, "librustutils/lib.rs": nil,
"log/src/lib.rs": nil,
} }
result := android.GroupFixturePreparers( result := android.GroupFixturePreparers(