blob: c12a01c4c3c925be9ae07abab06e7bb84881149a [file] [log] [blame]
Colin Cross4acaea92021-12-10 23:05:02 +00001// Copyright 2021 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 android
16
17import (
Colin Cross4acaea92021-12-10 23:05:02 +000018 "sort"
19 "strings"
20
21 "github.com/google/blueprint"
22 "github.com/google/blueprint/proptools"
23)
24
25var (
26 _ = pctx.HostBinToolVariable("licenseMetadataCmd", "build_license_metadata")
27
28 licenseMetadataRule = pctx.AndroidStaticRule("licenseMetadataRule", blueprint.RuleParams{
29 Command: "${licenseMetadataCmd} -o $out @${out}.rsp",
30 CommandDeps: []string{"${licenseMetadataCmd}"},
31 Rspfile: "${out}.rsp",
32 RspfileContent: "${args}",
33 }, "args")
34)
35
Yu Liuddc28332024-08-09 22:48:30 +000036func buildLicenseMetadata(ctx *moduleContext, licenseMetadataFile WritablePath) {
Colin Cross4acaea92021-12-10 23:05:02 +000037 base := ctx.Module().base()
38
Cole Fausta963b942024-04-11 17:43:00 -070039 if !base.Enabled(ctx) {
Colin Cross4acaea92021-12-10 23:05:02 +000040 return
41 }
42
43 if exemptFromRequiredApplicableLicensesProperty(ctx.Module()) {
44 return
45 }
46
Colin Crossaff21fb2022-01-12 10:57:57 -080047 var outputFiles Paths
mrziwang01201ed2024-07-11 10:21:57 -070048 if outputFiles, err := outputFilesForModule(ctx, ctx.Module(), ""); err == nil {
Colin Crossaff21fb2022-01-12 10:57:57 -080049 outputFiles = PathsIfNonNil(outputFiles...)
50 }
51
Colin Cross5c1d5fb2023-11-15 12:39:40 -080052 // Only pass the last installed file to isContainerFromFileExtensions so a *.zip file in test data
53 // doesn't mark the whole module as a container.
54 var installFiles InstallPaths
Yu Liuddc28332024-08-09 22:48:30 +000055 if len(ctx.installFiles) > 0 {
56 installFiles = InstallPaths{ctx.installFiles[len(ctx.installFiles)-1]}
Colin Cross5c1d5fb2023-11-15 12:39:40 -080057 }
58
59 isContainer := isContainerFromFileExtensions(installFiles, outputFiles)
Colin Crossaff21fb2022-01-12 10:57:57 -080060
Colin Cross4acaea92021-12-10 23:05:02 +000061 var allDepMetadataFiles Paths
62 var allDepMetadataArgs []string
63 var allDepOutputFiles Paths
Colin Crossc85750b2022-04-21 12:50:51 -070064 var allDepMetadataDepSets []*DepSet[Path]
Colin Cross4acaea92021-12-10 23:05:02 +000065
66 ctx.VisitDirectDepsBlueprint(func(bpdep blueprint.Module) {
67 dep, _ := bpdep.(Module)
68 if dep == nil {
69 return
70 }
Cole Fausta963b942024-04-11 17:43:00 -070071 if !dep.Enabled(ctx) {
Colin Cross4acaea92021-12-10 23:05:02 +000072 return
73 }
74
Bob Badour7fd521e2022-07-29 11:18:12 -070075 // Defaults add properties and dependencies that get processed on their own.
76 if ctx.OtherModuleDependencyTag(dep) == DefaultsDepTag {
77 return
78 }
Jiyong Park8bcf3c62024-03-18 18:37:10 +090079 // The required dependencies just say modules A and B should be installed together.
80 // It doesn't mean that one is built using the other.
81 if ctx.OtherModuleDependencyTag(dep) == RequiredDepTag {
82 return
83 }
Bob Badour7fd521e2022-07-29 11:18:12 -070084
Colin Cross313aa542023-12-13 13:47:44 -080085 if info, ok := OtherModuleProvider(ctx, dep, LicenseMetadataProvider); ok {
Colin Cross4acaea92021-12-10 23:05:02 +000086 allDepMetadataFiles = append(allDepMetadataFiles, info.LicenseMetadataPath)
Colin Crossbd3a16b2023-04-25 11:30:51 -070087 if isContainer || isInstallDepNeeded(dep, ctx.OtherModuleDependencyTag(dep)) {
Colin Crossaff21fb2022-01-12 10:57:57 -080088 allDepMetadataDepSets = append(allDepMetadataDepSets, info.LicenseMetadataDepSet)
89 }
Colin Cross4acaea92021-12-10 23:05:02 +000090
91 depAnnotations := licenseAnnotationsFromTag(ctx.OtherModuleDependencyTag(dep))
92
93 allDepMetadataArgs = append(allDepMetadataArgs, info.LicenseMetadataPath.String()+depAnnotations)
94
Yu Liuddc28332024-08-09 22:48:30 +000095 if depInstallFiles := ModuleFilesToInstall(ctx, dep); len(depInstallFiles) > 0 {
Colin Cross4acaea92021-12-10 23:05:02 +000096 allDepOutputFiles = append(allDepOutputFiles, depInstallFiles.Paths()...)
97 } else if depOutputFiles, err := outputFilesForModule(ctx, dep, ""); err == nil {
98 depOutputFiles = PathsIfNonNil(depOutputFiles...)
99 allDepOutputFiles = append(allDepOutputFiles, depOutputFiles...)
100 }
101 }
102 })
103
104 allDepMetadataFiles = SortedUniquePaths(allDepMetadataFiles)
105 sort.Strings(allDepMetadataArgs)
106 allDepOutputFiles = SortedUniquePaths(allDepOutputFiles)
107
108 var orderOnlyDeps Paths
109 var args []string
110
Ibrahim Kanouche49920782022-10-13 18:37:24 +0000111 if n := ctx.ModuleName(); n != "" {
112 args = append(args,
113 "-mn "+proptools.NinjaAndShellEscape(n))
114 }
115
Colin Cross4acaea92021-12-10 23:05:02 +0000116 if t := ctx.ModuleType(); t != "" {
117 args = append(args,
118 "-mt "+proptools.NinjaAndShellEscape(t))
119 }
120
121 args = append(args,
122 "-r "+proptools.NinjaAndShellEscape(ctx.ModuleDir()),
123 "-mc UNKNOWN")
124
125 if p := base.commonProperties.Effective_package_name; p != nil {
126 args = append(args,
Bob Badoura10d5a22022-06-21 11:12:01 -0700127 `-p `+proptools.NinjaAndShellEscapeIncludingSpaces(*p))
Colin Cross4acaea92021-12-10 23:05:02 +0000128 }
129
130 args = append(args,
131 JoinWithPrefix(proptools.NinjaAndShellEscapeListIncludingSpaces(base.commonProperties.Effective_license_kinds), "-k "))
132
133 args = append(args,
134 JoinWithPrefix(proptools.NinjaAndShellEscapeListIncludingSpaces(base.commonProperties.Effective_license_conditions), "-c "))
135
136 args = append(args,
137 JoinWithPrefix(proptools.NinjaAndShellEscapeListIncludingSpaces(base.commonProperties.Effective_license_text.Strings()), "-n "))
138
Colin Crossaff21fb2022-01-12 10:57:57 -0800139 if isContainer {
Colin Crossc85750b2022-04-21 12:50:51 -0700140 transitiveDeps := Paths(NewDepSet[Path](TOPOLOGICAL, nil, allDepMetadataDepSets).ToList())
Colin Crossaff21fb2022-01-12 10:57:57 -0800141 args = append(args,
Colin Crossaa1cab02022-01-28 14:49:24 -0800142 JoinWithPrefix(proptools.NinjaAndShellEscapeListIncludingSpaces(transitiveDeps.Strings()), "-d "))
143 orderOnlyDeps = append(orderOnlyDeps, transitiveDeps...)
Colin Crossaff21fb2022-01-12 10:57:57 -0800144 } else {
145 args = append(args,
146 JoinWithPrefix(proptools.NinjaAndShellEscapeListIncludingSpaces(allDepMetadataArgs), "-d "))
147 orderOnlyDeps = append(orderOnlyDeps, allDepMetadataFiles...)
148 }
Colin Cross4acaea92021-12-10 23:05:02 +0000149
150 args = append(args,
151 JoinWithPrefix(proptools.NinjaAndShellEscapeListIncludingSpaces(allDepOutputFiles.Strings()), "-s "))
152
153 // Install map
154 args = append(args,
155 JoinWithPrefix(proptools.NinjaAndShellEscapeListIncludingSpaces(base.licenseInstallMap), "-m "))
156
157 // Built files
Colin Cross4acaea92021-12-10 23:05:02 +0000158 if len(outputFiles) > 0 {
159 args = append(args,
160 JoinWithPrefix(proptools.NinjaAndShellEscapeListIncludingSpaces(outputFiles.Strings()), "-t "))
Colin Cross4acaea92021-12-10 23:05:02 +0000161 }
162
163 // Installed files
164 args = append(args,
Yu Liuddc28332024-08-09 22:48:30 +0000165 JoinWithPrefix(proptools.NinjaAndShellEscapeListIncludingSpaces(ctx.installFiles.Strings()), "-i "))
Colin Cross4acaea92021-12-10 23:05:02 +0000166
Colin Cross4acaea92021-12-10 23:05:02 +0000167 if isContainer {
168 args = append(args, "--is_container")
169 }
170
Colin Cross4acaea92021-12-10 23:05:02 +0000171 ctx.Build(pctx, BuildParams{
172 Rule: licenseMetadataRule,
173 Output: licenseMetadataFile,
174 OrderOnly: orderOnlyDeps,
175 Description: "license metadata",
176 Args: map[string]string{
177 "args": strings.Join(args, " "),
178 },
179 })
180
Colin Cross40213022023-12-13 15:19:49 -0800181 SetProvider(ctx, LicenseMetadataProvider, &LicenseMetadataInfo{
Colin Crossaff21fb2022-01-12 10:57:57 -0800182 LicenseMetadataPath: licenseMetadataFile,
Colin Crossc85750b2022-04-21 12:50:51 -0700183 LicenseMetadataDepSet: NewDepSet(TOPOLOGICAL, Paths{licenseMetadataFile}, allDepMetadataDepSets),
Colin Cross4acaea92021-12-10 23:05:02 +0000184 })
185}
186
187func isContainerFromFileExtensions(installPaths InstallPaths, builtPaths Paths) bool {
188 var paths Paths
189 if len(installPaths) > 0 {
190 paths = installPaths.Paths()
191 } else {
192 paths = builtPaths
193 }
194
195 for _, path := range paths {
196 switch path.Ext() {
Jooyung Han4191da72024-05-03 11:41:11 +0900197 case ".zip", ".tar", ".tgz", ".tar.gz", ".img", ".srcszip", ".apex", ".capex":
Colin Cross4acaea92021-12-10 23:05:02 +0000198 return true
199 }
200 }
201
202 return false
203}
204
205// LicenseMetadataProvider is used to propagate license metadata paths between modules.
Colin Crossbc7d76c2023-12-12 16:39:03 -0800206var LicenseMetadataProvider = blueprint.NewProvider[*LicenseMetadataInfo]()
Colin Cross4acaea92021-12-10 23:05:02 +0000207
208// LicenseMetadataInfo stores the license metadata path for a module.
209type LicenseMetadataInfo struct {
Colin Crossaff21fb2022-01-12 10:57:57 -0800210 LicenseMetadataPath Path
Colin Crossc85750b2022-04-21 12:50:51 -0700211 LicenseMetadataDepSet *DepSet[Path]
Colin Cross4acaea92021-12-10 23:05:02 +0000212}
213
214// licenseAnnotationsFromTag returns the LicenseAnnotations for a tag (if any) converted into
215// a string, or an empty string if there are none.
216func licenseAnnotationsFromTag(tag blueprint.DependencyTag) string {
217 if annoTag, ok := tag.(LicenseAnnotationsDependencyTag); ok {
218 annos := annoTag.LicenseAnnotations()
219 if len(annos) > 0 {
220 annoStrings := make([]string, len(annos))
221 for i, s := range annos {
222 annoStrings[i] = string(s)
223 }
224 return ":" + strings.Join(annoStrings, ",")
225 }
226 }
227 return ""
228}
229
230// LicenseAnnotationsDependencyTag is implemented by dependency tags in order to provide a
231// list of license dependency annotations.
232type LicenseAnnotationsDependencyTag interface {
233 LicenseAnnotations() []LicenseAnnotation
234}
235
236// LicenseAnnotation is an enum of annotations that can be applied to dependencies for propagating
237// license information.
238type LicenseAnnotation string
239
240const (
241 // LicenseAnnotationSharedDependency should be returned by LicenseAnnotations implementations
242 // of dependency tags when the usage of the dependency is dynamic, for example a shared library
243 // linkage for native modules or as a classpath library for java modules.
Colin Crossce564252022-01-12 11:13:32 -0800244 //
245 // Dependency tags that need to always return LicenseAnnotationSharedDependency
246 // can embed LicenseAnnotationSharedDependencyTag to implement LicenseAnnotations.
Colin Cross4acaea92021-12-10 23:05:02 +0000247 LicenseAnnotationSharedDependency LicenseAnnotation = "dynamic"
248
249 // LicenseAnnotationToolchain should be returned by LicenseAnnotations implementations of
250 // dependency tags when the dependency is used as a toolchain.
251 //
252 // Dependency tags that need to always return LicenseAnnotationToolchain
253 // can embed LicenseAnnotationToolchainDependencyTag to implement LicenseAnnotations.
254 LicenseAnnotationToolchain LicenseAnnotation = "toolchain"
255)
256
Colin Crossce564252022-01-12 11:13:32 -0800257// LicenseAnnotationSharedDependencyTag can be embedded in a dependency tag to implement
258// LicenseAnnotations that always returns LicenseAnnotationSharedDependency.
259type LicenseAnnotationSharedDependencyTag struct{}
260
261func (LicenseAnnotationSharedDependencyTag) LicenseAnnotations() []LicenseAnnotation {
262 return []LicenseAnnotation{LicenseAnnotationSharedDependency}
263}
264
Colin Cross4acaea92021-12-10 23:05:02 +0000265// LicenseAnnotationToolchainDependencyTag can be embedded in a dependency tag to implement
266// LicenseAnnotations that always returns LicenseAnnotationToolchain.
267type LicenseAnnotationToolchainDependencyTag struct{}
268
269func (LicenseAnnotationToolchainDependencyTag) LicenseAnnotations() []LicenseAnnotation {
270 return []LicenseAnnotation{LicenseAnnotationToolchain}
271}