notices.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. // Copyright 2019 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. "path/filepath"
  18. "strings"
  19. )
  20. func modulesOutputDirs(ctx BuilderContext, modules ...Module) []string {
  21. dirs := make([]string, 0, len(modules))
  22. for _, module := range modules {
  23. paths, err := outputFilesForModule(ctx, module, "")
  24. if err != nil {
  25. continue
  26. }
  27. for _, path := range paths {
  28. if path != nil {
  29. dirs = append(dirs, filepath.Dir(path.String()))
  30. }
  31. }
  32. }
  33. return SortedUniqueStrings(dirs)
  34. }
  35. func modulesLicenseMetadata(ctx BuilderContext, modules ...Module) Paths {
  36. result := make(Paths, 0, len(modules))
  37. for _, module := range modules {
  38. if mf := module.base().licenseMetadataFile; mf != nil {
  39. result = append(result, mf)
  40. }
  41. }
  42. return result
  43. }
  44. // buildNoticeOutputFromLicenseMetadata writes out a notice file.
  45. func buildNoticeOutputFromLicenseMetadata(
  46. ctx BuilderContext, tool, ruleName string, outputFile WritablePath,
  47. libraryName string, stripPrefix []string, modules ...Module) {
  48. depsFile := outputFile.ReplaceExtension(ctx, strings.TrimPrefix(outputFile.Ext()+".d", "."))
  49. rule := NewRuleBuilder(pctx, ctx)
  50. if len(modules) == 0 {
  51. if mctx, ok := ctx.(ModuleContext); ok {
  52. modules = []Module{mctx.Module()}
  53. } else {
  54. panic(fmt.Errorf("%s %q needs a module to generate the notice for", ruleName, libraryName))
  55. }
  56. }
  57. if libraryName == "" {
  58. libraryName = modules[0].Name()
  59. }
  60. cmd := rule.Command().
  61. BuiltTool(tool).
  62. FlagWithOutput("-o ", outputFile).
  63. FlagWithDepFile("-d ", depsFile)
  64. if len(stripPrefix) > 0 {
  65. cmd = cmd.FlagForEachArg("--strip_prefix ", stripPrefix)
  66. }
  67. outputs := modulesOutputDirs(ctx, modules...)
  68. if len(outputs) > 0 {
  69. cmd = cmd.FlagForEachArg("--strip_prefix ", outputs)
  70. }
  71. if libraryName != "" {
  72. cmd = cmd.FlagWithArg("--product ", libraryName)
  73. }
  74. cmd = cmd.Inputs(modulesLicenseMetadata(ctx, modules...))
  75. rule.Build(ruleName, "container notice file")
  76. }
  77. // BuildNoticeTextOutputFromLicenseMetadata writes out a notice text file based
  78. // on the license metadata files for the input `modules` defaulting to the
  79. // current context module if none given.
  80. func BuildNoticeTextOutputFromLicenseMetadata(
  81. ctx BuilderContext, outputFile WritablePath, ruleName, libraryName string,
  82. stripPrefix []string, modules ...Module) {
  83. buildNoticeOutputFromLicenseMetadata(ctx, "textnotice", "text_notice_"+ruleName,
  84. outputFile, libraryName, stripPrefix, modules...)
  85. }
  86. // BuildNoticeHtmlOutputFromLicenseMetadata writes out a notice text file based
  87. // on the license metadata files for the input `modules` defaulting to the
  88. // current context module if none given.
  89. func BuildNoticeHtmlOutputFromLicenseMetadata(
  90. ctx BuilderContext, outputFile WritablePath, ruleName, libraryName string,
  91. stripPrefix []string, modules ...Module) {
  92. buildNoticeOutputFromLicenseMetadata(ctx, "htmlnotice", "html_notice_"+ruleName,
  93. outputFile, libraryName, stripPrefix, modules...)
  94. }
  95. // BuildNoticeXmlOutputFromLicenseMetadata writes out a notice text file based
  96. // on the license metadata files for the input `modules` defaulting to the
  97. // current context module if none given.
  98. func BuildNoticeXmlOutputFromLicenseMetadata(
  99. ctx BuilderContext, outputFile WritablePath, ruleName, libraryName string,
  100. stripPrefix []string, modules ...Module) {
  101. buildNoticeOutputFromLicenseMetadata(ctx, "xmlnotice", "xml_notice_"+ruleName,
  102. outputFile, libraryName, stripPrefix, modules...)
  103. }