provenance_singleton.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. /*
  2. * Copyright (C) 2022 The Android Open Source Project
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package provenance
  17. import (
  18. "android/soong/android"
  19. "github.com/google/blueprint"
  20. )
  21. var (
  22. pctx = android.NewPackageContext("android/soong/provenance")
  23. rule = pctx.HostBinToolVariable("gen_provenance_metadata", "gen_provenance_metadata")
  24. genProvenanceMetaData = pctx.AndroidStaticRule("genProvenanceMetaData",
  25. blueprint.RuleParams{
  26. Command: `rm -rf "$out" && ` +
  27. `${gen_provenance_metadata} --module_name=${module_name} ` +
  28. `--artifact_path=$in --install_path=${install_path} --metadata_path=$out`,
  29. CommandDeps: []string{"${gen_provenance_metadata}"},
  30. }, "module_name", "install_path")
  31. mergeProvenanceMetaData = pctx.AndroidStaticRule("mergeProvenanceMetaData",
  32. blueprint.RuleParams{
  33. Command: `rm -rf $out && ` +
  34. `echo "# proto-file: build/soong/provenance/proto/provenance_metadata.proto" > $out && ` +
  35. `echo "# proto-message: ProvenanceMetaDataList" >> $out && ` +
  36. `for file in $in; do echo '' >> $out; echo 'metadata {' | cat - $$file | grep -Ev "^#.*|^$$" >> $out; echo '}' >> $out; done`,
  37. })
  38. )
  39. type ProvenanceMetadata interface {
  40. ProvenanceMetaDataFile() android.OutputPath
  41. }
  42. func init() {
  43. RegisterProvenanceSingleton(android.InitRegistrationContext)
  44. }
  45. func RegisterProvenanceSingleton(ctx android.RegistrationContext) {
  46. ctx.RegisterParallelSingletonType("provenance_metadata_singleton", provenanceInfoSingletonFactory)
  47. }
  48. var PrepareForTestWithProvenanceSingleton = android.FixtureRegisterWithContext(RegisterProvenanceSingleton)
  49. func provenanceInfoSingletonFactory() android.Singleton {
  50. return &provenanceInfoSingleton{}
  51. }
  52. type provenanceInfoSingleton struct {
  53. mergedMetaDataFile android.OutputPath
  54. }
  55. func (p *provenanceInfoSingleton) GenerateBuildActions(context android.SingletonContext) {
  56. allMetaDataFiles := make([]android.Path, 0)
  57. context.VisitAllModulesIf(moduleFilter, func(module android.Module) {
  58. if p, ok := module.(ProvenanceMetadata); ok {
  59. allMetaDataFiles = append(allMetaDataFiles, p.ProvenanceMetaDataFile())
  60. }
  61. })
  62. p.mergedMetaDataFile = android.PathForOutput(context, "provenance_metadata.textproto")
  63. context.Build(pctx, android.BuildParams{
  64. Rule: mergeProvenanceMetaData,
  65. Description: "merge provenance metadata",
  66. Inputs: allMetaDataFiles,
  67. Output: p.mergedMetaDataFile,
  68. })
  69. context.Build(pctx, android.BuildParams{
  70. Rule: blueprint.Phony,
  71. Description: "phony rule of merge provenance metadata",
  72. Inputs: []android.Path{p.mergedMetaDataFile},
  73. Output: android.PathForPhony(context, "provenance_metadata"),
  74. })
  75. context.Phony("droidcore", android.PathForPhony(context, "provenance_metadata"))
  76. }
  77. func moduleFilter(module android.Module) bool {
  78. if !module.Enabled() || module.IsSkipInstall() {
  79. return false
  80. }
  81. if p, ok := module.(ProvenanceMetadata); ok {
  82. return p.ProvenanceMetaDataFile().String() != ""
  83. }
  84. return false
  85. }
  86. func GenerateArtifactProvenanceMetaData(ctx android.ModuleContext, artifactPath android.Path, installedFile android.InstallPath) android.OutputPath {
  87. onDevicePathOfInstalledFile := android.InstallPathToOnDevicePath(ctx, installedFile)
  88. artifactMetaDataFile := android.PathForIntermediates(ctx, "provenance_metadata", ctx.ModuleDir(), ctx.ModuleName(), "provenance_metadata.textproto")
  89. ctx.Build(pctx, android.BuildParams{
  90. Rule: genProvenanceMetaData,
  91. Description: "generate artifact provenance metadata",
  92. Inputs: []android.Path{artifactPath},
  93. Output: artifactMetaDataFile,
  94. Args: map[string]string{
  95. "module_name": ctx.ModuleName(),
  96. "install_path": onDevicePathOfInstalledFile,
  97. }})
  98. return artifactMetaDataFile
  99. }
  100. func (p *provenanceInfoSingleton) MakeVars(ctx android.MakeVarsContext) {
  101. ctx.DistForGoal("droidcore", p.mergedMetaDataFile)
  102. }
  103. var _ android.SingletonMakeVarsProvider = (*provenanceInfoSingleton)(nil)