gen_notice.go 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. // Copyright 2020 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. "fmt"
  17. "strings"
  18. "github.com/google/blueprint/proptools"
  19. )
  20. func init() {
  21. RegisterGenNoticeBuildComponents(InitRegistrationContext)
  22. }
  23. // Register the gen_notice module type.
  24. func RegisterGenNoticeBuildComponents(ctx RegistrationContext) {
  25. ctx.RegisterSingletonType("gen_notice_build_rules", GenNoticeBuildRulesFactory)
  26. ctx.RegisterModuleType("gen_notice", GenNoticeFactory)
  27. }
  28. type genNoticeBuildRules struct{}
  29. func (s *genNoticeBuildRules) GenerateBuildActions(ctx SingletonContext) {
  30. ctx.VisitAllModules(func(m Module) {
  31. gm, ok := m.(*genNoticeModule)
  32. if !ok {
  33. return
  34. }
  35. if len(gm.missing) > 0 {
  36. missingReferencesRule(ctx, gm)
  37. return
  38. }
  39. out := BuildNoticeTextOutputFromLicenseMetadata
  40. if proptools.Bool(gm.properties.Xml) {
  41. out = BuildNoticeXmlOutputFromLicenseMetadata
  42. } else if proptools.Bool(gm.properties.Html) {
  43. out = BuildNoticeHtmlOutputFromLicenseMetadata
  44. }
  45. defaultName := ""
  46. if len(gm.properties.For) > 0 {
  47. defaultName = gm.properties.For[0]
  48. }
  49. modules := make([]Module, 0)
  50. for _, name := range gm.properties.For {
  51. mods := ctx.ModuleVariantsFromName(gm, name)
  52. for _, mod := range mods {
  53. if mod == nil {
  54. continue
  55. }
  56. modules = append(modules, mod)
  57. }
  58. }
  59. if ctx.Failed() {
  60. return
  61. }
  62. out(ctx, gm.output, ctx.ModuleName(gm),
  63. proptools.StringDefault(gm.properties.ArtifactName, defaultName),
  64. []string{
  65. ctx.Config().OutDir() + "/",
  66. ctx.Config().SoongOutDir() + "/",
  67. }, modules...)
  68. })
  69. }
  70. func GenNoticeBuildRulesFactory() Singleton {
  71. return &genNoticeBuildRules{}
  72. }
  73. type genNoticeProperties struct {
  74. // For specifies the modules for which to generate a notice file.
  75. For []string
  76. // ArtifactName specifies the internal name to use for the notice file.
  77. // It appears in the "used by:" list for targets whose entire name is stripped by --strip_prefix.
  78. ArtifactName *string
  79. // Stem specifies the base name of the output file.
  80. Stem *string `android:"arch_variant"`
  81. // Html indicates an html-format file is needed. The default is text. Can be Html or Xml but not both.
  82. Html *bool
  83. // Xml indicates an xml-format file is needed. The default is text. Can be Html or Xml but not both.
  84. Xml *bool
  85. // Gzipped indicates the output file must be compressed with gzip. Will append .gz to suffix if not there.
  86. Gzipped *bool
  87. // Suffix specifies the file extension to use. Defaults to .html for html, .xml for xml, or no extension for text.
  88. Suffix *string
  89. // Visibility specifies where this license can be used
  90. Visibility []string
  91. }
  92. type genNoticeModule struct {
  93. ModuleBase
  94. DefaultableModuleBase
  95. properties genNoticeProperties
  96. output OutputPath
  97. missing []string
  98. }
  99. func (m *genNoticeModule) DepsMutator(ctx BottomUpMutatorContext) {
  100. if ctx.ContainsProperty("licenses") {
  101. ctx.PropertyErrorf("licenses", "not supported on \"gen_notice\" modules")
  102. }
  103. if proptools.Bool(m.properties.Html) && proptools.Bool(m.properties.Xml) {
  104. ctx.ModuleErrorf("can be html or xml but not both")
  105. }
  106. if !ctx.Config().AllowMissingDependencies() {
  107. var missing []string
  108. // Verify the modules for which to generate notices exist.
  109. for _, otherMod := range m.properties.For {
  110. if !ctx.OtherModuleExists(otherMod) {
  111. missing = append(missing, otherMod)
  112. }
  113. }
  114. if len(missing) == 1 {
  115. ctx.PropertyErrorf("for", "no %q module exists", missing[0])
  116. } else if len(missing) > 1 {
  117. ctx.PropertyErrorf("for", "modules \"%s\" do not exist", strings.Join(missing, "\", \""))
  118. }
  119. }
  120. }
  121. func (m *genNoticeModule) getStem() string {
  122. stem := m.base().BaseModuleName()
  123. if m.properties.Stem != nil {
  124. stem = proptools.String(m.properties.Stem)
  125. }
  126. return stem
  127. }
  128. func (m *genNoticeModule) getSuffix() string {
  129. suffix := ""
  130. if m.properties.Suffix == nil {
  131. if proptools.Bool(m.properties.Html) {
  132. suffix = ".html"
  133. } else if proptools.Bool(m.properties.Xml) {
  134. suffix = ".xml"
  135. }
  136. } else {
  137. suffix = proptools.String(m.properties.Suffix)
  138. }
  139. if proptools.Bool(m.properties.Gzipped) && !strings.HasSuffix(suffix, ".gz") {
  140. suffix += ".gz"
  141. }
  142. return suffix
  143. }
  144. func (m *genNoticeModule) GenerateAndroidBuildActions(ctx ModuleContext) {
  145. if ctx.Config().AllowMissingDependencies() {
  146. // Verify the modules for which to generate notices exist.
  147. for _, otherMod := range m.properties.For {
  148. if !ctx.OtherModuleExists(otherMod) {
  149. m.missing = append(m.missing, otherMod)
  150. }
  151. }
  152. m.missing = append(m.missing, ctx.GetMissingDependencies()...)
  153. m.missing = FirstUniqueStrings(m.missing)
  154. }
  155. out := m.getStem() + m.getSuffix()
  156. m.output = PathForModuleOut(ctx, out).OutputPath
  157. }
  158. func GenNoticeFactory() Module {
  159. module := &genNoticeModule{}
  160. base := module.base()
  161. module.AddProperties(&base.nameProperties, &module.properties)
  162. // The visibility property needs to be checked and parsed by the visibility module.
  163. setPrimaryVisibilityProperty(module, "visibility", &module.properties.Visibility)
  164. InitAndroidArchModule(module, DeviceSupported, MultilibCommon)
  165. InitDefaultableModule(module)
  166. return module
  167. }
  168. var _ OutputFileProducer = (*genNoticeModule)(nil)
  169. // Implements OutputFileProducer
  170. func (m *genNoticeModule) OutputFiles(tag string) (Paths, error) {
  171. if tag == "" {
  172. return Paths{m.output}, nil
  173. }
  174. return nil, fmt.Errorf("unrecognized tag %q", tag)
  175. }
  176. var _ AndroidMkEntriesProvider = (*genNoticeModule)(nil)
  177. // Implements AndroidMkEntriesProvider
  178. func (m *genNoticeModule) AndroidMkEntries() []AndroidMkEntries {
  179. return []AndroidMkEntries{AndroidMkEntries{
  180. Class: "ETC",
  181. OutputFile: OptionalPathForPath(m.output),
  182. }}
  183. }
  184. // missingReferencesRule emits an ErrorRule for missing module references.
  185. func missingReferencesRule(ctx BuilderContext, m *genNoticeModule) {
  186. if len(m.missing) < 1 {
  187. panic(fmt.Errorf("missing references rule requested with no missing references"))
  188. }
  189. ctx.Build(pctx, BuildParams{
  190. Rule: ErrorRule,
  191. Output: m.output,
  192. Description: "notice for " + proptools.StringDefault(m.properties.ArtifactName, "container"),
  193. Args: map[string]string{
  194. "error": m.Name() + " references missing module(s): " + strings.Join(m.missing, ", "),
  195. },
  196. })
  197. }