provenance_singleton.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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 $out.temp && ` +
  34. `echo -e "# proto-file: build/soong/provenance/proto/provenance_metadata.proto\n# proto-message: ProvenanceMetaDataList" > $out && ` +
  35. `touch $out.temp && cat $out.temp $in | grep -v "^#.*" >> $out && rm -rf $out.temp`,
  36. })
  37. )
  38. type ProvenanceMetadata interface {
  39. ProvenanceMetaDataFile() android.OutputPath
  40. }
  41. func init() {
  42. RegisterProvenanceSingleton(android.InitRegistrationContext)
  43. }
  44. func RegisterProvenanceSingleton(ctx android.RegistrationContext) {
  45. ctx.RegisterSingletonType("provenance_metadata_singleton", provenanceInfoSingletonFactory)
  46. }
  47. var PrepareForTestWithProvenanceSingleton = android.FixtureRegisterWithContext(RegisterProvenanceSingleton)
  48. func provenanceInfoSingletonFactory() android.Singleton {
  49. return &provenanceInfoSingleton{}
  50. }
  51. type provenanceInfoSingleton struct {
  52. }
  53. func (b *provenanceInfoSingleton) GenerateBuildActions(context android.SingletonContext) {
  54. allMetaDataFiles := make([]android.Path, 0)
  55. context.VisitAllModulesIf(moduleFilter, func(module android.Module) {
  56. if p, ok := module.(ProvenanceMetadata); ok {
  57. allMetaDataFiles = append(allMetaDataFiles, p.ProvenanceMetaDataFile())
  58. }
  59. })
  60. mergedMetaDataFile := android.PathForOutput(context, "provenance_metadata.textproto")
  61. context.Build(pctx, android.BuildParams{
  62. Rule: mergeProvenanceMetaData,
  63. Description: "merge provenance metadata",
  64. Inputs: allMetaDataFiles,
  65. Output: mergedMetaDataFile,
  66. })
  67. context.Build(pctx, android.BuildParams{
  68. Rule: blueprint.Phony,
  69. Description: "phony rule of merge provenance metadata",
  70. Inputs: []android.Path{mergedMetaDataFile},
  71. Output: android.PathForPhony(context, "provenance_metadata"),
  72. })
  73. }
  74. func moduleFilter(module android.Module) bool {
  75. if !module.Enabled() || module.IsSkipInstall() {
  76. return false
  77. }
  78. if p, ok := module.(ProvenanceMetadata); ok {
  79. return p.ProvenanceMetaDataFile().String() != ""
  80. }
  81. return false
  82. }
  83. func GenerateArtifactProvenanceMetaData(ctx android.ModuleContext, artifactPath android.Path, installedFile android.InstallPath) android.OutputPath {
  84. onDevicePathOfInstalledFile := android.InstallPathToOnDevicePath(ctx, installedFile)
  85. artifactMetaDataFile := android.PathForIntermediates(ctx, "provenance_metadata", ctx.ModuleDir(), ctx.ModuleName(), "provenance_metadata.textproto")
  86. ctx.Build(pctx, android.BuildParams{
  87. Rule: genProvenanceMetaData,
  88. Description: "generate artifact provenance metadata",
  89. Inputs: []android.Path{artifactPath},
  90. Output: artifactMetaDataFile,
  91. Args: map[string]string{
  92. "module_name": ctx.ModuleName(),
  93. "install_path": onDevicePathOfInstalledFile,
  94. }})
  95. return artifactMetaDataFile
  96. }