| Colin Cross | 4acaea9 | 2021-12-10 23:05:02 +0000 | [diff] [blame] | 1 | // 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 |  | 
|  | 15 | package android | 
|  | 16 |  | 
|  | 17 | import ( | 
| Colin Cross | 4acaea9 | 2021-12-10 23:05:02 +0000 | [diff] [blame] | 18 | "sort" | 
|  | 19 | "strings" | 
|  | 20 |  | 
|  | 21 | "github.com/google/blueprint" | 
|  | 22 | "github.com/google/blueprint/proptools" | 
|  | 23 | ) | 
|  | 24 |  | 
|  | 25 | var ( | 
|  | 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 Liu | ddc2833 | 2024-08-09 22:48:30 +0000 | [diff] [blame] | 36 | func buildLicenseMetadata(ctx *moduleContext, licenseMetadataFile WritablePath) { | 
| Colin Cross | 4acaea9 | 2021-12-10 23:05:02 +0000 | [diff] [blame] | 37 | base := ctx.Module().base() | 
|  | 38 |  | 
| Cole Faust | a963b94 | 2024-04-11 17:43:00 -0700 | [diff] [blame] | 39 | if !base.Enabled(ctx) { | 
| Colin Cross | 4acaea9 | 2021-12-10 23:05:02 +0000 | [diff] [blame] | 40 | return | 
|  | 41 | } | 
|  | 42 |  | 
|  | 43 | if exemptFromRequiredApplicableLicensesProperty(ctx.Module()) { | 
|  | 44 | return | 
|  | 45 | } | 
|  | 46 |  | 
| Colin Cross | aff21fb | 2022-01-12 10:57:57 -0800 | [diff] [blame] | 47 | var outputFiles Paths | 
| mrziwang | 01201ed | 2024-07-11 10:21:57 -0700 | [diff] [blame] | 48 | if outputFiles, err := outputFilesForModule(ctx, ctx.Module(), ""); err == nil { | 
| Colin Cross | aff21fb | 2022-01-12 10:57:57 -0800 | [diff] [blame] | 49 | outputFiles = PathsIfNonNil(outputFiles...) | 
|  | 50 | } | 
|  | 51 |  | 
| Colin Cross | 5c1d5fb | 2023-11-15 12:39:40 -0800 | [diff] [blame] | 52 | // 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 Liu | ddc2833 | 2024-08-09 22:48:30 +0000 | [diff] [blame] | 55 | if len(ctx.installFiles) > 0 { | 
|  | 56 | installFiles = InstallPaths{ctx.installFiles[len(ctx.installFiles)-1]} | 
| Colin Cross | 5c1d5fb | 2023-11-15 12:39:40 -0800 | [diff] [blame] | 57 | } | 
|  | 58 |  | 
|  | 59 | isContainer := isContainerFromFileExtensions(installFiles, outputFiles) | 
| Colin Cross | aff21fb | 2022-01-12 10:57:57 -0800 | [diff] [blame] | 60 |  | 
| Colin Cross | 4acaea9 | 2021-12-10 23:05:02 +0000 | [diff] [blame] | 61 | var allDepMetadataFiles Paths | 
|  | 62 | var allDepMetadataArgs []string | 
|  | 63 | var allDepOutputFiles Paths | 
| Colin Cross | c85750b | 2022-04-21 12:50:51 -0700 | [diff] [blame] | 64 | var allDepMetadataDepSets []*DepSet[Path] | 
| Colin Cross | 4acaea9 | 2021-12-10 23:05:02 +0000 | [diff] [blame] | 65 |  | 
|  | 66 | ctx.VisitDirectDepsBlueprint(func(bpdep blueprint.Module) { | 
|  | 67 | dep, _ := bpdep.(Module) | 
|  | 68 | if dep == nil { | 
|  | 69 | return | 
|  | 70 | } | 
| Cole Faust | a963b94 | 2024-04-11 17:43:00 -0700 | [diff] [blame] | 71 | if !dep.Enabled(ctx) { | 
| Colin Cross | 4acaea9 | 2021-12-10 23:05:02 +0000 | [diff] [blame] | 72 | return | 
|  | 73 | } | 
|  | 74 |  | 
| Bob Badour | 7fd521e | 2022-07-29 11:18:12 -0700 | [diff] [blame] | 75 | // Defaults add properties and dependencies that get processed on their own. | 
|  | 76 | if ctx.OtherModuleDependencyTag(dep) == DefaultsDepTag { | 
|  | 77 | return | 
|  | 78 | } | 
| Jiyong Park | 8bcf3c6 | 2024-03-18 18:37:10 +0900 | [diff] [blame] | 79 | // 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 Badour | 7fd521e | 2022-07-29 11:18:12 -0700 | [diff] [blame] | 84 |  | 
| Colin Cross | 313aa54 | 2023-12-13 13:47:44 -0800 | [diff] [blame] | 85 | if info, ok := OtherModuleProvider(ctx, dep, LicenseMetadataProvider); ok { | 
| Colin Cross | 4acaea9 | 2021-12-10 23:05:02 +0000 | [diff] [blame] | 86 | allDepMetadataFiles = append(allDepMetadataFiles, info.LicenseMetadataPath) | 
| Colin Cross | bd3a16b | 2023-04-25 11:30:51 -0700 | [diff] [blame] | 87 | if isContainer || isInstallDepNeeded(dep, ctx.OtherModuleDependencyTag(dep)) { | 
| Colin Cross | aff21fb | 2022-01-12 10:57:57 -0800 | [diff] [blame] | 88 | allDepMetadataDepSets = append(allDepMetadataDepSets, info.LicenseMetadataDepSet) | 
|  | 89 | } | 
| Colin Cross | 4acaea9 | 2021-12-10 23:05:02 +0000 | [diff] [blame] | 90 |  | 
|  | 91 | depAnnotations := licenseAnnotationsFromTag(ctx.OtherModuleDependencyTag(dep)) | 
|  | 92 |  | 
|  | 93 | allDepMetadataArgs = append(allDepMetadataArgs, info.LicenseMetadataPath.String()+depAnnotations) | 
|  | 94 |  | 
| Yu Liu | d46e5ae | 2024-08-15 18:46:17 +0000 | [diff] [blame] | 95 | if depInstallFiles := OtherModuleProviderOrDefault(ctx, dep, InstallFilesProvider).InstallFiles; len(depInstallFiles) > 0 { | 
| Colin Cross | 4acaea9 | 2021-12-10 23:05:02 +0000 | [diff] [blame] | 96 | 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 Kanouche | 4992078 | 2022-10-13 18:37:24 +0000 | [diff] [blame] | 111 | if n := ctx.ModuleName(); n != "" { | 
|  | 112 | args = append(args, | 
|  | 113 | "-mn "+proptools.NinjaAndShellEscape(n)) | 
|  | 114 | } | 
|  | 115 |  | 
| Colin Cross | 4acaea9 | 2021-12-10 23:05:02 +0000 | [diff] [blame] | 116 | 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 Badour | a10d5a2 | 2022-06-21 11:12:01 -0700 | [diff] [blame] | 127 | `-p `+proptools.NinjaAndShellEscapeIncludingSpaces(*p)) | 
| Colin Cross | 4acaea9 | 2021-12-10 23:05:02 +0000 | [diff] [blame] | 128 | } | 
|  | 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 Cross | aff21fb | 2022-01-12 10:57:57 -0800 | [diff] [blame] | 139 | if isContainer { | 
| Colin Cross | c85750b | 2022-04-21 12:50:51 -0700 | [diff] [blame] | 140 | transitiveDeps := Paths(NewDepSet[Path](TOPOLOGICAL, nil, allDepMetadataDepSets).ToList()) | 
| Colin Cross | aff21fb | 2022-01-12 10:57:57 -0800 | [diff] [blame] | 141 | args = append(args, | 
| Colin Cross | aa1cab0 | 2022-01-28 14:49:24 -0800 | [diff] [blame] | 142 | JoinWithPrefix(proptools.NinjaAndShellEscapeListIncludingSpaces(transitiveDeps.Strings()), "-d ")) | 
|  | 143 | orderOnlyDeps = append(orderOnlyDeps, transitiveDeps...) | 
| Colin Cross | aff21fb | 2022-01-12 10:57:57 -0800 | [diff] [blame] | 144 | } else { | 
|  | 145 | args = append(args, | 
|  | 146 | JoinWithPrefix(proptools.NinjaAndShellEscapeListIncludingSpaces(allDepMetadataArgs), "-d ")) | 
|  | 147 | orderOnlyDeps = append(orderOnlyDeps, allDepMetadataFiles...) | 
|  | 148 | } | 
| Colin Cross | 4acaea9 | 2021-12-10 23:05:02 +0000 | [diff] [blame] | 149 |  | 
|  | 150 | args = append(args, | 
|  | 151 | JoinWithPrefix(proptools.NinjaAndShellEscapeListIncludingSpaces(allDepOutputFiles.Strings()), "-s ")) | 
|  | 152 |  | 
|  | 153 | // Install map | 
|  | 154 | args = append(args, | 
| Yu Liu | bad1eef | 2024-08-21 22:37:35 +0000 | [diff] [blame] | 155 | JoinWithPrefix(proptools.NinjaAndShellEscapeListIncludingSpaces(ctx.licenseInstallMap), "-m ")) | 
| Colin Cross | 4acaea9 | 2021-12-10 23:05:02 +0000 | [diff] [blame] | 156 |  | 
|  | 157 | // Built files | 
| Colin Cross | 4acaea9 | 2021-12-10 23:05:02 +0000 | [diff] [blame] | 158 | if len(outputFiles) > 0 { | 
|  | 159 | args = append(args, | 
|  | 160 | JoinWithPrefix(proptools.NinjaAndShellEscapeListIncludingSpaces(outputFiles.Strings()), "-t ")) | 
| Colin Cross | 4acaea9 | 2021-12-10 23:05:02 +0000 | [diff] [blame] | 161 | } | 
|  | 162 |  | 
|  | 163 | // Installed files | 
|  | 164 | args = append(args, | 
| Yu Liu | ddc2833 | 2024-08-09 22:48:30 +0000 | [diff] [blame] | 165 | JoinWithPrefix(proptools.NinjaAndShellEscapeListIncludingSpaces(ctx.installFiles.Strings()), "-i ")) | 
| Colin Cross | 4acaea9 | 2021-12-10 23:05:02 +0000 | [diff] [blame] | 166 |  | 
| Colin Cross | 4acaea9 | 2021-12-10 23:05:02 +0000 | [diff] [blame] | 167 | if isContainer { | 
|  | 168 | args = append(args, "--is_container") | 
|  | 169 | } | 
|  | 170 |  | 
| Colin Cross | 4acaea9 | 2021-12-10 23:05:02 +0000 | [diff] [blame] | 171 | 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 Cross | 4021302 | 2023-12-13 15:19:49 -0800 | [diff] [blame] | 181 | SetProvider(ctx, LicenseMetadataProvider, &LicenseMetadataInfo{ | 
| Colin Cross | aff21fb | 2022-01-12 10:57:57 -0800 | [diff] [blame] | 182 | LicenseMetadataPath:   licenseMetadataFile, | 
| Colin Cross | c85750b | 2022-04-21 12:50:51 -0700 | [diff] [blame] | 183 | LicenseMetadataDepSet: NewDepSet(TOPOLOGICAL, Paths{licenseMetadataFile}, allDepMetadataDepSets), | 
| Colin Cross | 4acaea9 | 2021-12-10 23:05:02 +0000 | [diff] [blame] | 184 | }) | 
|  | 185 | } | 
|  | 186 |  | 
|  | 187 | func 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 Han | 4191da7 | 2024-05-03 11:41:11 +0900 | [diff] [blame] | 197 | case ".zip", ".tar", ".tgz", ".tar.gz", ".img", ".srcszip", ".apex", ".capex": | 
| Colin Cross | 4acaea9 | 2021-12-10 23:05:02 +0000 | [diff] [blame] | 198 | return true | 
|  | 199 | } | 
|  | 200 | } | 
|  | 201 |  | 
|  | 202 | return false | 
|  | 203 | } | 
|  | 204 |  | 
|  | 205 | // LicenseMetadataProvider is used to propagate license metadata paths between modules. | 
| Colin Cross | bc7d76c | 2023-12-12 16:39:03 -0800 | [diff] [blame] | 206 | var LicenseMetadataProvider = blueprint.NewProvider[*LicenseMetadataInfo]() | 
| Colin Cross | 4acaea9 | 2021-12-10 23:05:02 +0000 | [diff] [blame] | 207 |  | 
|  | 208 | // LicenseMetadataInfo stores the license metadata path for a module. | 
|  | 209 | type LicenseMetadataInfo struct { | 
| Colin Cross | aff21fb | 2022-01-12 10:57:57 -0800 | [diff] [blame] | 210 | LicenseMetadataPath   Path | 
| Colin Cross | c85750b | 2022-04-21 12:50:51 -0700 | [diff] [blame] | 211 | LicenseMetadataDepSet *DepSet[Path] | 
| Colin Cross | 4acaea9 | 2021-12-10 23:05:02 +0000 | [diff] [blame] | 212 | } | 
|  | 213 |  | 
|  | 214 | // licenseAnnotationsFromTag returns the LicenseAnnotations for a tag (if any) converted into | 
|  | 215 | // a string, or an empty string if there are none. | 
|  | 216 | func 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. | 
|  | 232 | type 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. | 
|  | 238 | type LicenseAnnotation string | 
|  | 239 |  | 
|  | 240 | const ( | 
|  | 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 Cross | ce56425 | 2022-01-12 11:13:32 -0800 | [diff] [blame] | 244 | // | 
|  | 245 | // Dependency tags that need to always return LicenseAnnotationSharedDependency | 
|  | 246 | // can embed LicenseAnnotationSharedDependencyTag to implement LicenseAnnotations. | 
| Colin Cross | 4acaea9 | 2021-12-10 23:05:02 +0000 | [diff] [blame] | 247 | 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 Cross | ce56425 | 2022-01-12 11:13:32 -0800 | [diff] [blame] | 257 | // LicenseAnnotationSharedDependencyTag can be embedded in a dependency tag to implement | 
|  | 258 | // LicenseAnnotations that always returns LicenseAnnotationSharedDependency. | 
|  | 259 | type LicenseAnnotationSharedDependencyTag struct{} | 
|  | 260 |  | 
|  | 261 | func (LicenseAnnotationSharedDependencyTag) LicenseAnnotations() []LicenseAnnotation { | 
|  | 262 | return []LicenseAnnotation{LicenseAnnotationSharedDependency} | 
|  | 263 | } | 
|  | 264 |  | 
| Colin Cross | 4acaea9 | 2021-12-10 23:05:02 +0000 | [diff] [blame] | 265 | // LicenseAnnotationToolchainDependencyTag can be embedded in a dependency tag to implement | 
|  | 266 | // LicenseAnnotations that always returns LicenseAnnotationToolchain. | 
|  | 267 | type LicenseAnnotationToolchainDependencyTag struct{} | 
|  | 268 |  | 
|  | 269 | func (LicenseAnnotationToolchainDependencyTag) LicenseAnnotations() []LicenseAnnotation { | 
|  | 270 | return []LicenseAnnotation{LicenseAnnotationToolchain} | 
|  | 271 | } |