2022-03-19 01:33:24 +01:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2022 The Android Open Source Project
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package provenance
|
|
|
|
|
|
|
|
import (
|
|
|
|
"android/soong/android"
|
|
|
|
"github.com/google/blueprint"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
pctx = android.NewPackageContext("android/soong/provenance")
|
|
|
|
rule = pctx.HostBinToolVariable("gen_provenance_metadata", "gen_provenance_metadata")
|
|
|
|
|
|
|
|
genProvenanceMetaData = pctx.AndroidStaticRule("genProvenanceMetaData",
|
|
|
|
blueprint.RuleParams{
|
|
|
|
Command: `rm -rf "$out" && ` +
|
|
|
|
`${gen_provenance_metadata} --module_name=${module_name} ` +
|
|
|
|
`--artifact_path=$in --install_path=${install_path} --metadata_path=$out`,
|
|
|
|
CommandDeps: []string{"${gen_provenance_metadata}"},
|
|
|
|
}, "module_name", "install_path")
|
|
|
|
|
|
|
|
mergeProvenanceMetaData = pctx.AndroidStaticRule("mergeProvenanceMetaData",
|
|
|
|
blueprint.RuleParams{
|
2022-06-24 00:17:12 +02:00
|
|
|
Command: `rm -rf $out && ` +
|
2022-05-04 01:13:00 +02:00
|
|
|
`echo "# proto-file: build/soong/provenance/proto/provenance_metadata.proto" > $out && ` +
|
|
|
|
`echo "# proto-message: ProvenanceMetaDataList" >> $out && ` +
|
2023-08-25 08:19:30 +02:00
|
|
|
`cat $out.rsp | tr ' ' '\n' | while read -r file || [ -n "$$file" ]; do echo '' >> $out; echo 'metadata {' | cat - $$file | grep -Ev "^#.*|^$$" >> $out; echo '}' >> $out; done`,
|
|
|
|
Rspfile: `$out.rsp`,
|
|
|
|
RspfileContent: `$in`,
|
2022-03-19 01:33:24 +01:00
|
|
|
})
|
|
|
|
)
|
|
|
|
|
|
|
|
type ProvenanceMetadata interface {
|
|
|
|
ProvenanceMetaDataFile() android.OutputPath
|
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
RegisterProvenanceSingleton(android.InitRegistrationContext)
|
|
|
|
}
|
|
|
|
|
|
|
|
func RegisterProvenanceSingleton(ctx android.RegistrationContext) {
|
2023-05-16 02:58:37 +02:00
|
|
|
ctx.RegisterParallelSingletonType("provenance_metadata_singleton", provenanceInfoSingletonFactory)
|
2022-03-19 01:33:24 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
var PrepareForTestWithProvenanceSingleton = android.FixtureRegisterWithContext(RegisterProvenanceSingleton)
|
|
|
|
|
|
|
|
func provenanceInfoSingletonFactory() android.Singleton {
|
|
|
|
return &provenanceInfoSingleton{}
|
|
|
|
}
|
|
|
|
|
|
|
|
type provenanceInfoSingleton struct {
|
2022-04-21 01:59:47 +02:00
|
|
|
mergedMetaDataFile android.OutputPath
|
2022-03-19 01:33:24 +01:00
|
|
|
}
|
|
|
|
|
2022-04-21 01:59:47 +02:00
|
|
|
func (p *provenanceInfoSingleton) GenerateBuildActions(context android.SingletonContext) {
|
2022-03-19 01:33:24 +01:00
|
|
|
allMetaDataFiles := make([]android.Path, 0)
|
|
|
|
context.VisitAllModulesIf(moduleFilter, func(module android.Module) {
|
|
|
|
if p, ok := module.(ProvenanceMetadata); ok {
|
|
|
|
allMetaDataFiles = append(allMetaDataFiles, p.ProvenanceMetaDataFile())
|
|
|
|
}
|
|
|
|
})
|
2022-04-21 01:59:47 +02:00
|
|
|
p.mergedMetaDataFile = android.PathForOutput(context, "provenance_metadata.textproto")
|
2022-03-19 01:33:24 +01:00
|
|
|
context.Build(pctx, android.BuildParams{
|
|
|
|
Rule: mergeProvenanceMetaData,
|
|
|
|
Description: "merge provenance metadata",
|
|
|
|
Inputs: allMetaDataFiles,
|
2022-04-21 01:59:47 +02:00
|
|
|
Output: p.mergedMetaDataFile,
|
2022-03-19 01:33:24 +01:00
|
|
|
})
|
|
|
|
|
|
|
|
context.Build(pctx, android.BuildParams{
|
|
|
|
Rule: blueprint.Phony,
|
|
|
|
Description: "phony rule of merge provenance metadata",
|
2022-04-21 01:59:47 +02:00
|
|
|
Inputs: []android.Path{p.mergedMetaDataFile},
|
2022-03-19 01:33:24 +01:00
|
|
|
Output: android.PathForPhony(context, "provenance_metadata"),
|
|
|
|
})
|
2022-04-20 02:29:16 +02:00
|
|
|
|
|
|
|
context.Phony("droidcore", android.PathForPhony(context, "provenance_metadata"))
|
2022-03-19 01:33:24 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func moduleFilter(module android.Module) bool {
|
|
|
|
if !module.Enabled() || module.IsSkipInstall() {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if p, ok := module.(ProvenanceMetadata); ok {
|
|
|
|
return p.ProvenanceMetaDataFile().String() != ""
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
func GenerateArtifactProvenanceMetaData(ctx android.ModuleContext, artifactPath android.Path, installedFile android.InstallPath) android.OutputPath {
|
|
|
|
onDevicePathOfInstalledFile := android.InstallPathToOnDevicePath(ctx, installedFile)
|
|
|
|
artifactMetaDataFile := android.PathForIntermediates(ctx, "provenance_metadata", ctx.ModuleDir(), ctx.ModuleName(), "provenance_metadata.textproto")
|
|
|
|
ctx.Build(pctx, android.BuildParams{
|
|
|
|
Rule: genProvenanceMetaData,
|
|
|
|
Description: "generate artifact provenance metadata",
|
|
|
|
Inputs: []android.Path{artifactPath},
|
|
|
|
Output: artifactMetaDataFile,
|
|
|
|
Args: map[string]string{
|
|
|
|
"module_name": ctx.ModuleName(),
|
|
|
|
"install_path": onDevicePathOfInstalledFile,
|
|
|
|
}})
|
|
|
|
|
|
|
|
return artifactMetaDataFile
|
|
|
|
}
|
2022-04-21 01:59:47 +02:00
|
|
|
|
|
|
|
func (p *provenanceInfoSingleton) MakeVars(ctx android.MakeVarsContext) {
|
|
|
|
ctx.DistForGoal("droidcore", p.mergedMetaDataFile)
|
|
|
|
}
|
|
|
|
|
|
|
|
var _ android.SingletonMakeVarsProvider = (*provenanceInfoSingleton)(nil)
|