blob: 9e20ec637911cca940ef1c02a61112874239bbd8 [file] [log] [blame]
Aditya Choudhary8094b6b2023-10-12 19:40:17 +00001// Copyright 2020 Google Inc. All rights reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15package testing
16
17import (
18 "path/filepath"
19
20 "android/soong/android"
21 "android/soong/testing/code_metadata_internal_proto"
22 "github.com/google/blueprint"
23 "google.golang.org/protobuf/proto"
24)
25
26func CodeMetadataFactory() android.Module {
27 module := &CodeMetadataModule{}
28
29 android.InitAndroidModule(module)
30 android.InitDefaultableModule(module)
31 module.AddProperties(&module.properties)
32
33 return module
34}
35
36type CodeMetadataModule struct {
37 android.ModuleBase
38 android.DefaultableModuleBase
Aditya Choudhary8094b6b2023-10-12 19:40:17 +000039
40 // Properties for "code_metadata"
41 properties struct {
42 // Specifies the name of the code_config.
43 Name string
44 // Specifies the team ID.
45 TeamId string
46 // Specifies the list of modules that this code_metadata covers.
47 Code []string
48 // An optional field to specify if multiple ownerships for source files is allowed.
49 MultiOwnership bool
50 }
51}
52
53type codeDepTagType struct {
54 blueprint.BaseDependencyTag
55}
56
57var codeDepTag = codeDepTagType{}
58
59func (module *CodeMetadataModule) DepsMutator(ctx android.BottomUpMutatorContext) {
60 // Validate Properties
61 if len(module.properties.TeamId) == 0 {
62 ctx.PropertyErrorf(
63 "TeamId",
64 "Team Id not found in the code_metadata module. Hint: Maybe the teamId property hasn't been properly specified.",
65 )
66 }
67 if !isInt(module.properties.TeamId) {
68 ctx.PropertyErrorf(
69 "TeamId", "Invalid value for Team ID. The Team ID must be an integer.",
70 )
71 }
72 if len(module.properties.Code) == 0 {
73 ctx.PropertyErrorf(
74 "Code",
75 "Targets to be attributed cannot be empty. Hint: Maybe the code property hasn't been properly specified.",
76 )
77 }
78 ctx.AddDependency(ctx.Module(), codeDepTag, module.properties.Code...)
79}
80
81// Provider published by CodeMetadata
82type CodeMetadataProviderData struct {
83 IntermediatePath android.WritablePath
84}
85
86var CodeMetadataProviderKey = blueprint.NewProvider(CodeMetadataProviderData{})
87
88func (module *CodeMetadataModule) GenerateAndroidBuildActions(ctx android.ModuleContext) {
89 metadataList := make(
90 []*code_metadata_internal_proto.CodeMetadataInternal_TargetOwnership, 0,
91 len(module.properties.Code),
92 )
93 bpFilePath := filepath.Join(ctx.ModuleDir(), ctx.BlueprintsFile())
94
95 for _, m := range ctx.GetDirectDepsWithTag(codeDepTag) {
96 targetName := m.Name()
Aditya Choudhary26df39f2023-11-29 16:42:42 +000097 var moduleSrcs []string
98 if ctx.OtherModuleHasProvider(m, blueprint.SrcsFileProviderKey) {
Aditya Choudhary8094b6b2023-10-12 19:40:17 +000099 moduleSrcs = ctx.OtherModuleProvider(
Aditya Choudhary26df39f2023-11-29 16:42:42 +0000100 m, blueprint.SrcsFileProviderKey,
101 ).(blueprint.SrcsFileProviderData).SrcPaths
Aditya Choudhary8094b6b2023-10-12 19:40:17 +0000102 }
103 if module.properties.MultiOwnership {
104 metadata := &code_metadata_internal_proto.CodeMetadataInternal_TargetOwnership{
105 TargetName: &targetName,
106 TrendyTeamId: &module.properties.TeamId,
107 Path: &bpFilePath,
108 MultiOwnership: &module.properties.MultiOwnership,
Aditya Choudhary26df39f2023-11-29 16:42:42 +0000109 SourceFiles: moduleSrcs,
Aditya Choudhary8094b6b2023-10-12 19:40:17 +0000110 }
111 metadataList = append(metadataList, metadata)
112 } else {
113 metadata := &code_metadata_internal_proto.CodeMetadataInternal_TargetOwnership{
114 TargetName: &targetName,
115 TrendyTeamId: &module.properties.TeamId,
116 Path: &bpFilePath,
Aditya Choudhary26df39f2023-11-29 16:42:42 +0000117 SourceFiles: moduleSrcs,
Aditya Choudhary8094b6b2023-10-12 19:40:17 +0000118 }
119 metadataList = append(metadataList, metadata)
120 }
121
122 }
123 codeMetadata := &code_metadata_internal_proto.CodeMetadataInternal{TargetOwnershipList: metadataList}
124 protoData, err := proto.Marshal(codeMetadata)
125 if err != nil {
126 ctx.ModuleErrorf("Error marshaling code metadata: %s", err.Error())
127 return
128 }
129 intermediatePath := android.PathForModuleOut(
130 ctx, "intermediateCodeMetadata.pb",
131 )
132 android.WriteFileRule(ctx, intermediatePath, string(protoData))
133
134 ctx.SetProvider(
135 CodeMetadataProviderKey,
136 CodeMetadataProviderData{IntermediatePath: intermediatePath},
137 )
138}