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