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 | a14fb6a | 2024-10-23 16:57:06 -0700 | [diff] [blame] | 18 | "github.com/google/blueprint/depset" |
Colin Cross | 4acaea9 | 2021-12-10 23:05:02 +0000 | [diff] [blame] | 19 | "sort" |
| 20 | "strings" |
| 21 | |
| 22 | "github.com/google/blueprint" |
| 23 | "github.com/google/blueprint/proptools" |
| 24 | ) |
| 25 | |
| 26 | var ( |
| 27 | _ = pctx.HostBinToolVariable("licenseMetadataCmd", "build_license_metadata") |
| 28 | |
| 29 | licenseMetadataRule = pctx.AndroidStaticRule("licenseMetadataRule", blueprint.RuleParams{ |
| 30 | Command: "${licenseMetadataCmd} -o $out @${out}.rsp", |
| 31 | CommandDeps: []string{"${licenseMetadataCmd}"}, |
| 32 | Rspfile: "${out}.rsp", |
| 33 | RspfileContent: "${args}", |
| 34 | }, "args") |
| 35 | ) |
| 36 | |
Yu Liu | ddc2833 | 2024-08-09 22:48:30 +0000 | [diff] [blame] | 37 | func buildLicenseMetadata(ctx *moduleContext, licenseMetadataFile WritablePath) { |
Colin Cross | 4acaea9 | 2021-12-10 23:05:02 +0000 | [diff] [blame] | 38 | base := ctx.Module().base() |
| 39 | |
Cole Faust | a963b94 | 2024-04-11 17:43:00 -0700 | [diff] [blame] | 40 | if !base.Enabled(ctx) { |
Colin Cross | 4acaea9 | 2021-12-10 23:05:02 +0000 | [diff] [blame] | 41 | return |
| 42 | } |
| 43 | |
| 44 | if exemptFromRequiredApplicableLicensesProperty(ctx.Module()) { |
| 45 | return |
| 46 | } |
| 47 | |
Colin Cross | aff21fb | 2022-01-12 10:57:57 -0800 | [diff] [blame] | 48 | var outputFiles Paths |
mrziwang | 01201ed | 2024-07-11 10:21:57 -0700 | [diff] [blame] | 49 | if outputFiles, err := outputFilesForModule(ctx, ctx.Module(), ""); err == nil { |
Colin Cross | aff21fb | 2022-01-12 10:57:57 -0800 | [diff] [blame] | 50 | outputFiles = PathsIfNonNil(outputFiles...) |
| 51 | } |
| 52 | |
Colin Cross | 5c1d5fb | 2023-11-15 12:39:40 -0800 | [diff] [blame] | 53 | // Only pass the last installed file to isContainerFromFileExtensions so a *.zip file in test data |
| 54 | // doesn't mark the whole module as a container. |
| 55 | var installFiles InstallPaths |
Yu Liu | ddc2833 | 2024-08-09 22:48:30 +0000 | [diff] [blame] | 56 | if len(ctx.installFiles) > 0 { |
| 57 | installFiles = InstallPaths{ctx.installFiles[len(ctx.installFiles)-1]} |
Colin Cross | 5c1d5fb | 2023-11-15 12:39:40 -0800 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | isContainer := isContainerFromFileExtensions(installFiles, outputFiles) |
Colin Cross | aff21fb | 2022-01-12 10:57:57 -0800 | [diff] [blame] | 61 | |
Colin Cross | 4acaea9 | 2021-12-10 23:05:02 +0000 | [diff] [blame] | 62 | var allDepMetadataFiles Paths |
| 63 | var allDepMetadataArgs []string |
| 64 | var allDepOutputFiles Paths |
Colin Cross | a14fb6a | 2024-10-23 16:57:06 -0700 | [diff] [blame] | 65 | var allDepMetadataDepSets []depset.DepSet[Path] |
Colin Cross | 4acaea9 | 2021-12-10 23:05:02 +0000 | [diff] [blame] | 66 | |
Colin Cross | 648daea | 2024-09-12 14:35:29 -0700 | [diff] [blame] | 67 | ctx.VisitDirectDeps(func(dep Module) { |
Cole Faust | a963b94 | 2024-04-11 17:43:00 -0700 | [diff] [blame] | 68 | if !dep.Enabled(ctx) { |
Colin Cross | 4acaea9 | 2021-12-10 23:05:02 +0000 | [diff] [blame] | 69 | return |
| 70 | } |
| 71 | |
Bob Badour | 7fd521e | 2022-07-29 11:18:12 -0700 | [diff] [blame] | 72 | // Defaults add properties and dependencies that get processed on their own. |
| 73 | if ctx.OtherModuleDependencyTag(dep) == DefaultsDepTag { |
| 74 | return |
| 75 | } |
Jiyong Park | 8bcf3c6 | 2024-03-18 18:37:10 +0900 | [diff] [blame] | 76 | // The required dependencies just say modules A and B should be installed together. |
| 77 | // It doesn't mean that one is built using the other. |
| 78 | if ctx.OtherModuleDependencyTag(dep) == RequiredDepTag { |
| 79 | return |
| 80 | } |
Bob Badour | 7fd521e | 2022-07-29 11:18:12 -0700 | [diff] [blame] | 81 | |
Colin Cross | 313aa54 | 2023-12-13 13:47:44 -0800 | [diff] [blame] | 82 | if info, ok := OtherModuleProvider(ctx, dep, LicenseMetadataProvider); ok { |
Colin Cross | 4acaea9 | 2021-12-10 23:05:02 +0000 | [diff] [blame] | 83 | allDepMetadataFiles = append(allDepMetadataFiles, info.LicenseMetadataPath) |
Colin Cross | bd3a16b | 2023-04-25 11:30:51 -0700 | [diff] [blame] | 84 | if isContainer || isInstallDepNeeded(dep, ctx.OtherModuleDependencyTag(dep)) { |
Colin Cross | aff21fb | 2022-01-12 10:57:57 -0800 | [diff] [blame] | 85 | allDepMetadataDepSets = append(allDepMetadataDepSets, info.LicenseMetadataDepSet) |
| 86 | } |
Colin Cross | 4acaea9 | 2021-12-10 23:05:02 +0000 | [diff] [blame] | 87 | |
| 88 | depAnnotations := licenseAnnotationsFromTag(ctx.OtherModuleDependencyTag(dep)) |
| 89 | |
| 90 | allDepMetadataArgs = append(allDepMetadataArgs, info.LicenseMetadataPath.String()+depAnnotations) |
| 91 | |
Yu Liu | d46e5ae | 2024-08-15 18:46:17 +0000 | [diff] [blame] | 92 | if depInstallFiles := OtherModuleProviderOrDefault(ctx, dep, InstallFilesProvider).InstallFiles; len(depInstallFiles) > 0 { |
Colin Cross | 4acaea9 | 2021-12-10 23:05:02 +0000 | [diff] [blame] | 93 | allDepOutputFiles = append(allDepOutputFiles, depInstallFiles.Paths()...) |
| 94 | } else if depOutputFiles, err := outputFilesForModule(ctx, dep, ""); err == nil { |
| 95 | depOutputFiles = PathsIfNonNil(depOutputFiles...) |
| 96 | allDepOutputFiles = append(allDepOutputFiles, depOutputFiles...) |
| 97 | } |
| 98 | } |
| 99 | }) |
| 100 | |
| 101 | allDepMetadataFiles = SortedUniquePaths(allDepMetadataFiles) |
| 102 | sort.Strings(allDepMetadataArgs) |
| 103 | allDepOutputFiles = SortedUniquePaths(allDepOutputFiles) |
| 104 | |
| 105 | var orderOnlyDeps Paths |
| 106 | var args []string |
| 107 | |
Ibrahim Kanouche | 4992078 | 2022-10-13 18:37:24 +0000 | [diff] [blame] | 108 | if n := ctx.ModuleName(); n != "" { |
| 109 | args = append(args, |
| 110 | "-mn "+proptools.NinjaAndShellEscape(n)) |
| 111 | } |
| 112 | |
Colin Cross | 4acaea9 | 2021-12-10 23:05:02 +0000 | [diff] [blame] | 113 | if t := ctx.ModuleType(); t != "" { |
| 114 | args = append(args, |
| 115 | "-mt "+proptools.NinjaAndShellEscape(t)) |
| 116 | } |
| 117 | |
| 118 | args = append(args, |
| 119 | "-r "+proptools.NinjaAndShellEscape(ctx.ModuleDir()), |
| 120 | "-mc UNKNOWN") |
| 121 | |
| 122 | if p := base.commonProperties.Effective_package_name; p != nil { |
| 123 | args = append(args, |
Bob Badour | a10d5a2 | 2022-06-21 11:12:01 -0700 | [diff] [blame] | 124 | `-p `+proptools.NinjaAndShellEscapeIncludingSpaces(*p)) |
Colin Cross | 4acaea9 | 2021-12-10 23:05:02 +0000 | [diff] [blame] | 125 | } |
| 126 | |
| 127 | args = append(args, |
| 128 | JoinWithPrefix(proptools.NinjaAndShellEscapeListIncludingSpaces(base.commonProperties.Effective_license_kinds), "-k ")) |
| 129 | |
| 130 | args = append(args, |
| 131 | JoinWithPrefix(proptools.NinjaAndShellEscapeListIncludingSpaces(base.commonProperties.Effective_license_conditions), "-c ")) |
| 132 | |
| 133 | args = append(args, |
| 134 | JoinWithPrefix(proptools.NinjaAndShellEscapeListIncludingSpaces(base.commonProperties.Effective_license_text.Strings()), "-n ")) |
| 135 | |
Colin Cross | aff21fb | 2022-01-12 10:57:57 -0800 | [diff] [blame] | 136 | if isContainer { |
Colin Cross | a14fb6a | 2024-10-23 16:57:06 -0700 | [diff] [blame] | 137 | transitiveDeps := Paths(depset.New[Path](depset.TOPOLOGICAL, nil, allDepMetadataDepSets).ToList()) |
Colin Cross | aff21fb | 2022-01-12 10:57:57 -0800 | [diff] [blame] | 138 | args = append(args, |
Colin Cross | aa1cab0 | 2022-01-28 14:49:24 -0800 | [diff] [blame] | 139 | JoinWithPrefix(proptools.NinjaAndShellEscapeListIncludingSpaces(transitiveDeps.Strings()), "-d ")) |
| 140 | orderOnlyDeps = append(orderOnlyDeps, transitiveDeps...) |
Colin Cross | aff21fb | 2022-01-12 10:57:57 -0800 | [diff] [blame] | 141 | } else { |
| 142 | args = append(args, |
| 143 | JoinWithPrefix(proptools.NinjaAndShellEscapeListIncludingSpaces(allDepMetadataArgs), "-d ")) |
| 144 | orderOnlyDeps = append(orderOnlyDeps, allDepMetadataFiles...) |
| 145 | } |
Colin Cross | 4acaea9 | 2021-12-10 23:05:02 +0000 | [diff] [blame] | 146 | |
| 147 | args = append(args, |
| 148 | JoinWithPrefix(proptools.NinjaAndShellEscapeListIncludingSpaces(allDepOutputFiles.Strings()), "-s ")) |
| 149 | |
| 150 | // Install map |
| 151 | args = append(args, |
Yu Liu | bad1eef | 2024-08-21 22:37:35 +0000 | [diff] [blame] | 152 | JoinWithPrefix(proptools.NinjaAndShellEscapeListIncludingSpaces(ctx.licenseInstallMap), "-m ")) |
Colin Cross | 4acaea9 | 2021-12-10 23:05:02 +0000 | [diff] [blame] | 153 | |
| 154 | // Built files |
Colin Cross | 4acaea9 | 2021-12-10 23:05:02 +0000 | [diff] [blame] | 155 | if len(outputFiles) > 0 { |
| 156 | args = append(args, |
| 157 | JoinWithPrefix(proptools.NinjaAndShellEscapeListIncludingSpaces(outputFiles.Strings()), "-t ")) |
Colin Cross | 4acaea9 | 2021-12-10 23:05:02 +0000 | [diff] [blame] | 158 | } |
| 159 | |
| 160 | // Installed files |
| 161 | args = append(args, |
Yu Liu | ddc2833 | 2024-08-09 22:48:30 +0000 | [diff] [blame] | 162 | JoinWithPrefix(proptools.NinjaAndShellEscapeListIncludingSpaces(ctx.installFiles.Strings()), "-i ")) |
Colin Cross | 4acaea9 | 2021-12-10 23:05:02 +0000 | [diff] [blame] | 163 | |
Colin Cross | 4acaea9 | 2021-12-10 23:05:02 +0000 | [diff] [blame] | 164 | if isContainer { |
| 165 | args = append(args, "--is_container") |
| 166 | } |
| 167 | |
Colin Cross | 4acaea9 | 2021-12-10 23:05:02 +0000 | [diff] [blame] | 168 | ctx.Build(pctx, BuildParams{ |
| 169 | Rule: licenseMetadataRule, |
| 170 | Output: licenseMetadataFile, |
| 171 | OrderOnly: orderOnlyDeps, |
| 172 | Description: "license metadata", |
| 173 | Args: map[string]string{ |
| 174 | "args": strings.Join(args, " "), |
| 175 | }, |
| 176 | }) |
| 177 | |
Colin Cross | 4021302 | 2023-12-13 15:19:49 -0800 | [diff] [blame] | 178 | SetProvider(ctx, LicenseMetadataProvider, &LicenseMetadataInfo{ |
Colin Cross | aff21fb | 2022-01-12 10:57:57 -0800 | [diff] [blame] | 179 | LicenseMetadataPath: licenseMetadataFile, |
Colin Cross | a14fb6a | 2024-10-23 16:57:06 -0700 | [diff] [blame] | 180 | LicenseMetadataDepSet: depset.New(depset.TOPOLOGICAL, Paths{licenseMetadataFile}, allDepMetadataDepSets), |
Colin Cross | 4acaea9 | 2021-12-10 23:05:02 +0000 | [diff] [blame] | 181 | }) |
| 182 | } |
| 183 | |
| 184 | func isContainerFromFileExtensions(installPaths InstallPaths, builtPaths Paths) bool { |
| 185 | var paths Paths |
| 186 | if len(installPaths) > 0 { |
| 187 | paths = installPaths.Paths() |
| 188 | } else { |
| 189 | paths = builtPaths |
| 190 | } |
| 191 | |
| 192 | for _, path := range paths { |
| 193 | switch path.Ext() { |
Jooyung Han | 4191da7 | 2024-05-03 11:41:11 +0900 | [diff] [blame] | 194 | case ".zip", ".tar", ".tgz", ".tar.gz", ".img", ".srcszip", ".apex", ".capex": |
Colin Cross | 4acaea9 | 2021-12-10 23:05:02 +0000 | [diff] [blame] | 195 | return true |
| 196 | } |
| 197 | } |
| 198 | |
| 199 | return false |
| 200 | } |
| 201 | |
| 202 | // LicenseMetadataProvider is used to propagate license metadata paths between modules. |
Colin Cross | bc7d76c | 2023-12-12 16:39:03 -0800 | [diff] [blame] | 203 | var LicenseMetadataProvider = blueprint.NewProvider[*LicenseMetadataInfo]() |
Colin Cross | 4acaea9 | 2021-12-10 23:05:02 +0000 | [diff] [blame] | 204 | |
| 205 | // LicenseMetadataInfo stores the license metadata path for a module. |
| 206 | type LicenseMetadataInfo struct { |
Colin Cross | aff21fb | 2022-01-12 10:57:57 -0800 | [diff] [blame] | 207 | LicenseMetadataPath Path |
Colin Cross | a14fb6a | 2024-10-23 16:57:06 -0700 | [diff] [blame] | 208 | LicenseMetadataDepSet depset.DepSet[Path] |
Colin Cross | 4acaea9 | 2021-12-10 23:05:02 +0000 | [diff] [blame] | 209 | } |
| 210 | |
| 211 | // licenseAnnotationsFromTag returns the LicenseAnnotations for a tag (if any) converted into |
| 212 | // a string, or an empty string if there are none. |
| 213 | func licenseAnnotationsFromTag(tag blueprint.DependencyTag) string { |
| 214 | if annoTag, ok := tag.(LicenseAnnotationsDependencyTag); ok { |
| 215 | annos := annoTag.LicenseAnnotations() |
| 216 | if len(annos) > 0 { |
| 217 | annoStrings := make([]string, len(annos)) |
| 218 | for i, s := range annos { |
| 219 | annoStrings[i] = string(s) |
| 220 | } |
| 221 | return ":" + strings.Join(annoStrings, ",") |
| 222 | } |
| 223 | } |
| 224 | return "" |
| 225 | } |
| 226 | |
| 227 | // LicenseAnnotationsDependencyTag is implemented by dependency tags in order to provide a |
| 228 | // list of license dependency annotations. |
| 229 | type LicenseAnnotationsDependencyTag interface { |
| 230 | LicenseAnnotations() []LicenseAnnotation |
| 231 | } |
| 232 | |
| 233 | // LicenseAnnotation is an enum of annotations that can be applied to dependencies for propagating |
| 234 | // license information. |
| 235 | type LicenseAnnotation string |
| 236 | |
| 237 | const ( |
| 238 | // LicenseAnnotationSharedDependency should be returned by LicenseAnnotations implementations |
| 239 | // of dependency tags when the usage of the dependency is dynamic, for example a shared library |
| 240 | // linkage for native modules or as a classpath library for java modules. |
Colin Cross | ce56425 | 2022-01-12 11:13:32 -0800 | [diff] [blame] | 241 | // |
| 242 | // Dependency tags that need to always return LicenseAnnotationSharedDependency |
| 243 | // can embed LicenseAnnotationSharedDependencyTag to implement LicenseAnnotations. |
Colin Cross | 4acaea9 | 2021-12-10 23:05:02 +0000 | [diff] [blame] | 244 | LicenseAnnotationSharedDependency LicenseAnnotation = "dynamic" |
| 245 | |
| 246 | // LicenseAnnotationToolchain should be returned by LicenseAnnotations implementations of |
| 247 | // dependency tags when the dependency is used as a toolchain. |
| 248 | // |
| 249 | // Dependency tags that need to always return LicenseAnnotationToolchain |
| 250 | // can embed LicenseAnnotationToolchainDependencyTag to implement LicenseAnnotations. |
| 251 | LicenseAnnotationToolchain LicenseAnnotation = "toolchain" |
| 252 | ) |
| 253 | |
Colin Cross | ce56425 | 2022-01-12 11:13:32 -0800 | [diff] [blame] | 254 | // LicenseAnnotationSharedDependencyTag can be embedded in a dependency tag to implement |
| 255 | // LicenseAnnotations that always returns LicenseAnnotationSharedDependency. |
| 256 | type LicenseAnnotationSharedDependencyTag struct{} |
| 257 | |
| 258 | func (LicenseAnnotationSharedDependencyTag) LicenseAnnotations() []LicenseAnnotation { |
| 259 | return []LicenseAnnotation{LicenseAnnotationSharedDependency} |
| 260 | } |
| 261 | |
Colin Cross | 4acaea9 | 2021-12-10 23:05:02 +0000 | [diff] [blame] | 262 | // LicenseAnnotationToolchainDependencyTag can be embedded in a dependency tag to implement |
| 263 | // LicenseAnnotations that always returns LicenseAnnotationToolchain. |
| 264 | type LicenseAnnotationToolchainDependencyTag struct{} |
| 265 | |
| 266 | func (LicenseAnnotationToolchainDependencyTag) LicenseAnnotations() []LicenseAnnotation { |
| 267 | return []LicenseAnnotation{LicenseAnnotationToolchain} |
| 268 | } |