Merge "Replace generatedFile with android.WriteFileRule" into main

This commit is contained in:
Cole Faust
2024-03-08 18:49:59 +00:00
committed by Gerrit Code Review

View File

@@ -89,19 +89,6 @@ type generatedContents struct {
indentLevel int indentLevel int
} }
// generatedFile abstracts operations for writing contents into a file and emit a build rule
// for the file.
type generatedFile struct {
generatedContents
path android.OutputPath
}
func newGeneratedFile(ctx android.ModuleContext, path ...string) *generatedFile {
return &generatedFile{
path: android.PathForModuleOut(ctx, path...).OutputPath,
}
}
func (gc *generatedContents) Indent() { func (gc *generatedContents) Indent() {
gc.indentLevel++ gc.indentLevel++
} }
@@ -122,26 +109,6 @@ func (gc *generatedContents) UnindentedPrintf(format string, args ...interface{}
_, _ = fmt.Fprintf(&(gc.content), format, args...) _, _ = fmt.Fprintf(&(gc.content), format, args...)
} }
func (gf *generatedFile) build(pctx android.PackageContext, ctx android.BuilderContext, implicits android.Paths) {
rb := android.NewRuleBuilder(pctx, ctx)
content := gf.content.String()
// ninja consumes newline characters in rspfile_content. Prevent it by
// escaping the backslash in the newline character. The extra backslash
// is removed when the rspfile is written to the actual script file
content = strings.ReplaceAll(content, "\n", "\\n")
rb.Command().
Implicits(implicits).
Text("echo -n").Text(proptools.ShellEscape(content)).
// convert \\n to \n
Text("| sed 's/\\\\n/\\n/g' >").Output(gf.path)
rb.Command().
Text("chmod a+x").Output(gf.path)
rb.Build(gf.path.Base(), "Build "+gf.path.Base())
}
// Collect all the members. // Collect all the members.
// //
// Updates the sdk module with a list of sdkMemberVariantDep instances and details as to which // Updates the sdk module with a list of sdkMemberVariantDep instances and details as to which
@@ -170,7 +137,7 @@ func (s *sdk) collectMembers(ctx android.ModuleContext) {
var container android.Module var container android.Module
if parent != ctx.Module() { if parent != ctx.Module() {
container = parent.(android.Module) container = parent
} }
minApiLevel := android.MinApiLevelForSdkSnapshot(ctx, child) minApiLevel := android.MinApiLevelForSdkSnapshot(ctx, child)
@@ -179,7 +146,7 @@ func (s *sdk) collectMembers(ctx android.ModuleContext) {
s.memberVariantDeps = append(s.memberVariantDeps, sdkMemberVariantDep{ s.memberVariantDeps = append(s.memberVariantDeps, sdkMemberVariantDep{
sdkVariant: s, sdkVariant: s,
memberType: memberType, memberType: memberType,
variant: child.(android.Module), variant: child,
minApiLevel: minApiLevel, minApiLevel: minApiLevel,
container: container, container: container,
export: export, export: export,
@@ -375,7 +342,7 @@ func (s *sdk) buildSnapshot(ctx android.ModuleContext, sdkVariants []*sdk) {
snapshotDir := android.PathForModuleOut(ctx, "snapshot") snapshotDir := android.PathForModuleOut(ctx, "snapshot")
bp := newGeneratedFile(ctx, "snapshot", "Android.bp") bp := android.PathForModuleOut(ctx, "snapshot", "Android.bp")
bpFile := &bpFile{ bpFile := &bpFile{
modules: make(map[string]*bpModule), modules: make(map[string]*bpModule),
@@ -389,7 +356,7 @@ func (s *sdk) buildSnapshot(ctx android.ModuleContext, sdkVariants []*sdk) {
sdk: s, sdk: s,
snapshotDir: snapshotDir.OutputPath, snapshotDir: snapshotDir.OutputPath,
copies: make(map[string]string), copies: make(map[string]string),
filesToZip: []android.Path{bp.path}, filesToZip: []android.Path{bp},
bpFile: bpFile, bpFile: bpFile,
prebuiltModules: make(map[string]*bpModule), prebuiltModules: make(map[string]*bpModule),
allMembersByName: allMembersByName, allMembersByName: allMembersByName,
@@ -463,17 +430,14 @@ be unnecessary as every module in the sdk already has its own licenses property.
} }
// generate Android.bp // generate Android.bp
bp = newGeneratedFile(ctx, "snapshot", "Android.bp") contents := generateBpContents(bpFile)
generateBpContents(&bp.generatedContents, bpFile)
contents := bp.content.String()
// If the snapshot is being generated for the current build release then check the syntax to make // If the snapshot is being generated for the current build release then check the syntax to make
// sure that it is compatible. // sure that it is compatible.
if targetBuildRelease == buildReleaseCurrent { if targetBuildRelease == buildReleaseCurrent {
syntaxCheckSnapshotBpFile(ctx, contents) syntaxCheckSnapshotBpFile(ctx, contents)
} }
bp.build(pctx, ctx, nil) android.WriteFileRuleVerbatim(ctx, bp, contents)
// Copy the build number file into the snapshot. // Copy the build number file into the snapshot.
builder.CopyToSnapshot(ctx.Config().BuildNumberFile(ctx), BUILD_NUMBER_FILE) builder.CopyToSnapshot(ctx.Config().BuildNumberFile(ctx), BUILD_NUMBER_FILE)
@@ -522,16 +486,14 @@ be unnecessary as every module in the sdk already has its own licenses property.
modules := s.generateInfoData(ctx, memberVariantDeps) modules := s.generateInfoData(ctx, memberVariantDeps)
// Output the modules information as pretty printed JSON. // Output the modules information as pretty printed JSON.
info := newGeneratedFile(ctx, fmt.Sprintf("%s%s.info", ctx.ModuleName(), snapshotFileSuffix)) info := android.PathForModuleOut(ctx, fmt.Sprintf("%s%s.info", ctx.ModuleName(), snapshotFileSuffix))
output, err := json.MarshalIndent(modules, "", " ") output, err := json.MarshalIndent(modules, "", " ")
if err != nil { if err != nil {
ctx.ModuleErrorf("error generating %q: %s", info, err) ctx.ModuleErrorf("error generating %q: %s", info, err)
} }
builder.infoContents = string(output) builder.infoContents = string(output)
info.generatedContents.UnindentedPrintf("%s", output) android.WriteFileRuleVerbatim(ctx, info, builder.infoContents)
info.build(pctx, ctx, nil) installedInfo := ctx.InstallFile(android.PathForMainlineSdksInstall(ctx), info.Base(), info)
infoPath := info.path
installedInfo := ctx.InstallFile(android.PathForMainlineSdksInstall(ctx), infoPath.Base(), infoPath)
s.infoFile = android.OptionalPathForPath(installedInfo) s.infoFile = android.OptionalPathForPath(installedInfo)
// Install the zip, making sure that the info file has been installed as well. // Install the zip, making sure that the info file has been installed as well.
@@ -885,7 +847,8 @@ func (t pruneEmptySetTransformer) transformPropertySetAfterContents(_ string, pr
} }
} }
func generateBpContents(contents *generatedContents, bpFile *bpFile) { func generateBpContents(bpFile *bpFile) string {
contents := &generatedContents{}
contents.IndentedPrintf("// This is auto-generated. DO NOT EDIT.\n") contents.IndentedPrintf("// This is auto-generated. DO NOT EDIT.\n")
for _, bpModule := range bpFile.order { for _, bpModule := range bpFile.order {
contents.IndentedPrintf("\n") contents.IndentedPrintf("\n")
@@ -893,6 +856,7 @@ func generateBpContents(contents *generatedContents, bpFile *bpFile) {
outputPropertySet(contents, bpModule.bpPropertySet) outputPropertySet(contents, bpModule.bpPropertySet)
contents.IndentedPrintf("}\n") contents.IndentedPrintf("}\n")
} }
return contents.content.String()
} }
func outputPropertySet(contents *generatedContents, set *bpPropertySet) { func outputPropertySet(contents *generatedContents, set *bpPropertySet) {
@@ -1018,9 +982,7 @@ func multiLineValue(value reflect.Value) bool {
} }
func (s *sdk) GetAndroidBpContentsForTests() string { func (s *sdk) GetAndroidBpContentsForTests() string {
contents := &generatedContents{} return generateBpContents(s.builderForTests.bpFile)
generateBpContents(contents, s.builderForTests.bpFile)
return contents.content.String()
} }
func (s *sdk) GetInfoContentsForTests() string { func (s *sdk) GetInfoContentsForTests() string {