license_metadata.go 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  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. package android
  15. import (
  16. "sort"
  17. "strings"
  18. "github.com/google/blueprint"
  19. "github.com/google/blueprint/proptools"
  20. )
  21. var (
  22. _ = pctx.HostBinToolVariable("licenseMetadataCmd", "build_license_metadata")
  23. licenseMetadataRule = pctx.AndroidStaticRule("licenseMetadataRule", blueprint.RuleParams{
  24. Command: "${licenseMetadataCmd} -o $out @${out}.rsp",
  25. CommandDeps: []string{"${licenseMetadataCmd}"},
  26. Rspfile: "${out}.rsp",
  27. RspfileContent: "${args}",
  28. }, "args")
  29. )
  30. func buildLicenseMetadata(ctx ModuleContext, licenseMetadataFile WritablePath) {
  31. base := ctx.Module().base()
  32. if !base.Enabled() {
  33. return
  34. }
  35. if exemptFromRequiredApplicableLicensesProperty(ctx.Module()) {
  36. return
  37. }
  38. var outputFiles Paths
  39. if outputFileProducer, ok := ctx.Module().(OutputFileProducer); ok {
  40. outputFiles, _ = outputFileProducer.OutputFiles("")
  41. outputFiles = PathsIfNonNil(outputFiles...)
  42. }
  43. isContainer := isContainerFromFileExtensions(base.installFiles, outputFiles)
  44. var allDepMetadataFiles Paths
  45. var allDepMetadataArgs []string
  46. var allDepOutputFiles Paths
  47. var allDepMetadataDepSets []*DepSet[Path]
  48. ctx.VisitDirectDepsBlueprint(func(bpdep blueprint.Module) {
  49. dep, _ := bpdep.(Module)
  50. if dep == nil {
  51. return
  52. }
  53. if !dep.Enabled() {
  54. return
  55. }
  56. // Defaults add properties and dependencies that get processed on their own.
  57. if ctx.OtherModuleDependencyTag(dep) == DefaultsDepTag {
  58. return
  59. }
  60. if ctx.OtherModuleHasProvider(dep, LicenseMetadataProvider) {
  61. info := ctx.OtherModuleProvider(dep, LicenseMetadataProvider).(*LicenseMetadataInfo)
  62. allDepMetadataFiles = append(allDepMetadataFiles, info.LicenseMetadataPath)
  63. if isContainer || isInstallDepNeeded(dep, ctx.OtherModuleDependencyTag(dep)) {
  64. allDepMetadataDepSets = append(allDepMetadataDepSets, info.LicenseMetadataDepSet)
  65. }
  66. depAnnotations := licenseAnnotationsFromTag(ctx.OtherModuleDependencyTag(dep))
  67. allDepMetadataArgs = append(allDepMetadataArgs, info.LicenseMetadataPath.String()+depAnnotations)
  68. if depInstallFiles := dep.base().installFiles; len(depInstallFiles) > 0 {
  69. allDepOutputFiles = append(allDepOutputFiles, depInstallFiles.Paths()...)
  70. } else if depOutputFiles, err := outputFilesForModule(ctx, dep, ""); err == nil {
  71. depOutputFiles = PathsIfNonNil(depOutputFiles...)
  72. allDepOutputFiles = append(allDepOutputFiles, depOutputFiles...)
  73. }
  74. }
  75. })
  76. allDepMetadataFiles = SortedUniquePaths(allDepMetadataFiles)
  77. sort.Strings(allDepMetadataArgs)
  78. allDepOutputFiles = SortedUniquePaths(allDepOutputFiles)
  79. var orderOnlyDeps Paths
  80. var args []string
  81. if n := ctx.ModuleName(); n != "" {
  82. args = append(args,
  83. "-mn "+proptools.NinjaAndShellEscape(n))
  84. }
  85. if t := ctx.ModuleType(); t != "" {
  86. args = append(args,
  87. "-mt "+proptools.NinjaAndShellEscape(t))
  88. }
  89. args = append(args,
  90. "-r "+proptools.NinjaAndShellEscape(ctx.ModuleDir()),
  91. "-mc UNKNOWN")
  92. if p := base.commonProperties.Effective_package_name; p != nil {
  93. args = append(args,
  94. `-p `+proptools.NinjaAndShellEscapeIncludingSpaces(*p))
  95. }
  96. args = append(args,
  97. JoinWithPrefix(proptools.NinjaAndShellEscapeListIncludingSpaces(base.commonProperties.Effective_license_kinds), "-k "))
  98. args = append(args,
  99. JoinWithPrefix(proptools.NinjaAndShellEscapeListIncludingSpaces(base.commonProperties.Effective_license_conditions), "-c "))
  100. args = append(args,
  101. JoinWithPrefix(proptools.NinjaAndShellEscapeListIncludingSpaces(base.commonProperties.Effective_license_text.Strings()), "-n "))
  102. if isContainer {
  103. transitiveDeps := Paths(NewDepSet[Path](TOPOLOGICAL, nil, allDepMetadataDepSets).ToList())
  104. args = append(args,
  105. JoinWithPrefix(proptools.NinjaAndShellEscapeListIncludingSpaces(transitiveDeps.Strings()), "-d "))
  106. orderOnlyDeps = append(orderOnlyDeps, transitiveDeps...)
  107. } else {
  108. args = append(args,
  109. JoinWithPrefix(proptools.NinjaAndShellEscapeListIncludingSpaces(allDepMetadataArgs), "-d "))
  110. orderOnlyDeps = append(orderOnlyDeps, allDepMetadataFiles...)
  111. }
  112. args = append(args,
  113. JoinWithPrefix(proptools.NinjaAndShellEscapeListIncludingSpaces(allDepOutputFiles.Strings()), "-s "))
  114. // Install map
  115. args = append(args,
  116. JoinWithPrefix(proptools.NinjaAndShellEscapeListIncludingSpaces(base.licenseInstallMap), "-m "))
  117. // Built files
  118. if len(outputFiles) > 0 {
  119. args = append(args,
  120. JoinWithPrefix(proptools.NinjaAndShellEscapeListIncludingSpaces(outputFiles.Strings()), "-t "))
  121. }
  122. // Installed files
  123. args = append(args,
  124. JoinWithPrefix(proptools.NinjaAndShellEscapeListIncludingSpaces(base.installFiles.Strings()), "-i "))
  125. if isContainer {
  126. args = append(args, "--is_container")
  127. }
  128. ctx.Build(pctx, BuildParams{
  129. Rule: licenseMetadataRule,
  130. Output: licenseMetadataFile,
  131. OrderOnly: orderOnlyDeps,
  132. Description: "license metadata",
  133. Args: map[string]string{
  134. "args": strings.Join(args, " "),
  135. },
  136. })
  137. ctx.SetProvider(LicenseMetadataProvider, &LicenseMetadataInfo{
  138. LicenseMetadataPath: licenseMetadataFile,
  139. LicenseMetadataDepSet: NewDepSet(TOPOLOGICAL, Paths{licenseMetadataFile}, allDepMetadataDepSets),
  140. })
  141. }
  142. func isContainerFromFileExtensions(installPaths InstallPaths, builtPaths Paths) bool {
  143. var paths Paths
  144. if len(installPaths) > 0 {
  145. paths = installPaths.Paths()
  146. } else {
  147. paths = builtPaths
  148. }
  149. for _, path := range paths {
  150. switch path.Ext() {
  151. case ".zip", ".tar", ".tgz", ".tar.gz", ".img", ".srcszip", ".apex":
  152. return true
  153. }
  154. }
  155. return false
  156. }
  157. // LicenseMetadataProvider is used to propagate license metadata paths between modules.
  158. var LicenseMetadataProvider = blueprint.NewProvider(&LicenseMetadataInfo{})
  159. // LicenseMetadataInfo stores the license metadata path for a module.
  160. type LicenseMetadataInfo struct {
  161. LicenseMetadataPath Path
  162. LicenseMetadataDepSet *DepSet[Path]
  163. }
  164. // licenseAnnotationsFromTag returns the LicenseAnnotations for a tag (if any) converted into
  165. // a string, or an empty string if there are none.
  166. func licenseAnnotationsFromTag(tag blueprint.DependencyTag) string {
  167. if annoTag, ok := tag.(LicenseAnnotationsDependencyTag); ok {
  168. annos := annoTag.LicenseAnnotations()
  169. if len(annos) > 0 {
  170. annoStrings := make([]string, len(annos))
  171. for i, s := range annos {
  172. annoStrings[i] = string(s)
  173. }
  174. return ":" + strings.Join(annoStrings, ",")
  175. }
  176. }
  177. return ""
  178. }
  179. // LicenseAnnotationsDependencyTag is implemented by dependency tags in order to provide a
  180. // list of license dependency annotations.
  181. type LicenseAnnotationsDependencyTag interface {
  182. LicenseAnnotations() []LicenseAnnotation
  183. }
  184. // LicenseAnnotation is an enum of annotations that can be applied to dependencies for propagating
  185. // license information.
  186. type LicenseAnnotation string
  187. const (
  188. // LicenseAnnotationSharedDependency should be returned by LicenseAnnotations implementations
  189. // of dependency tags when the usage of the dependency is dynamic, for example a shared library
  190. // linkage for native modules or as a classpath library for java modules.
  191. //
  192. // Dependency tags that need to always return LicenseAnnotationSharedDependency
  193. // can embed LicenseAnnotationSharedDependencyTag to implement LicenseAnnotations.
  194. LicenseAnnotationSharedDependency LicenseAnnotation = "dynamic"
  195. // LicenseAnnotationToolchain should be returned by LicenseAnnotations implementations of
  196. // dependency tags when the dependency is used as a toolchain.
  197. //
  198. // Dependency tags that need to always return LicenseAnnotationToolchain
  199. // can embed LicenseAnnotationToolchainDependencyTag to implement LicenseAnnotations.
  200. LicenseAnnotationToolchain LicenseAnnotation = "toolchain"
  201. )
  202. // LicenseAnnotationSharedDependencyTag can be embedded in a dependency tag to implement
  203. // LicenseAnnotations that always returns LicenseAnnotationSharedDependency.
  204. type LicenseAnnotationSharedDependencyTag struct{}
  205. func (LicenseAnnotationSharedDependencyTag) LicenseAnnotations() []LicenseAnnotation {
  206. return []LicenseAnnotation{LicenseAnnotationSharedDependency}
  207. }
  208. // LicenseAnnotationToolchainDependencyTag can be embedded in a dependency tag to implement
  209. // LicenseAnnotations that always returns LicenseAnnotationToolchain.
  210. type LicenseAnnotationToolchainDependencyTag struct{}
  211. func (LicenseAnnotationToolchainDependencyTag) LicenseAnnotations() []LicenseAnnotation {
  212. return []LicenseAnnotation{LicenseAnnotationToolchain}
  213. }