blob: 562a156e2bc09133bb21724e3453600ffb05c603 [file] [log] [blame]
Jaewoong Jung5b425e22019-06-17 17:40:56 -07001// 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
15package android
16
17import (
Bob Badoureef4c1c2022-05-16 12:20:04 -070018 "fmt"
19 "path/filepath"
Colin Crossaa1cab02022-01-28 14:49:24 -080020 "strings"
Jaewoong Jung5b425e22019-06-17 17:40:56 -070021)
22
Bob Badoureef4c1c2022-05-16 12:20:04 -070023func modulesOutputDirs(ctx BuilderContext, modules ...Module) []string {
24 dirs := make([]string, 0, len(modules))
25 for _, module := range modules {
26 paths, err := outputFilesForModule(ctx, module, "")
27 if err != nil {
28 continue
29 }
30 for _, path := range paths {
31 if path != nil {
32 dirs = append(dirs, filepath.Dir(path.String()))
33 }
34 }
35 }
36 return SortedUniqueStrings(dirs)
Bob Badourde6a0872022-04-01 18:00:00 +000037}
38
Bob Badoureef4c1c2022-05-16 12:20:04 -070039func modulesLicenseMetadata(ctx BuilderContext, modules ...Module) Paths {
40 result := make(Paths, 0, len(modules))
41 for _, module := range modules {
42 if mf := module.base().licenseMetadataFile; mf != nil {
43 result = append(result, mf)
44 }
45 }
46 return result
47}
48
49// buildNoticeOutputFromLicenseMetadata writes out a notice file.
Bob Badoura5ea2472022-05-20 16:37:26 -070050func buildNoticeOutputFromLicenseMetadata(ctx BuilderContext, tool, ruleName string, outputFile WritablePath, libraryName, stripPrefix string, modules ...Module) {
Bob Badourde6a0872022-04-01 18:00:00 +000051 depsFile := outputFile.ReplaceExtension(ctx, strings.TrimPrefix(outputFile.Ext()+".d", "."))
52 rule := NewRuleBuilder(pctx, ctx)
Bob Badoureef4c1c2022-05-16 12:20:04 -070053 if len(modules) == 0 {
54 if mctx, ok := ctx.(ModuleContext); ok {
55 modules = []Module{mctx.Module()}
56 } else {
Bob Badoura5ea2472022-05-20 16:37:26 -070057 panic(fmt.Errorf("%s %q needs a module to generate the notice for", ruleName, libraryName))
Bob Badoureef4c1c2022-05-16 12:20:04 -070058 }
59 }
60 if libraryName == "" {
61 libraryName = modules[0].Name()
62 }
63 cmd := rule.Command().
64 BuiltTool(tool).
Bob Badourde6a0872022-04-01 18:00:00 +000065 FlagWithOutput("-o ", outputFile).
Bob Badoureef4c1c2022-05-16 12:20:04 -070066 FlagWithDepFile("-d ", depsFile)
67 if stripPrefix != "" {
68 cmd = cmd.FlagWithArg("--strip_prefix ", stripPrefix)
69 }
70 outputs := modulesOutputDirs(ctx, modules...)
71 if len(outputs) > 0 {
72 cmd = cmd.FlagForEachArg("--strip_prefix ", outputs)
73 }
74 if libraryName != "" {
75 cmd = cmd.FlagWithArg("--product ", libraryName)
76 }
77 cmd = cmd.Inputs(modulesLicenseMetadata(ctx, modules...))
Bob Badoura5ea2472022-05-20 16:37:26 -070078 rule.Build(ruleName, "container notice file")
Bob Badoureef4c1c2022-05-16 12:20:04 -070079}
80
81// BuildNoticeTextOutputFromLicenseMetadata writes out a notice text file based
82// on the license metadata files for the input `modules` defaulting to the
83// current context module if none given.
Bob Badoura5ea2472022-05-20 16:37:26 -070084func BuildNoticeTextOutputFromLicenseMetadata(ctx BuilderContext, outputFile WritablePath, ruleName, libraryName, stripPrefix string, modules ...Module) {
85 buildNoticeOutputFromLicenseMetadata(ctx, "textnotice", "text_notice_"+ruleName, outputFile, libraryName, stripPrefix, modules...)
Bob Badoureef4c1c2022-05-16 12:20:04 -070086}
87
88// BuildNoticeHtmlOutputFromLicenseMetadata writes out a notice text file based
89// on the license metadata files for the input `modules` defaulting to the
90// current context module if none given.
Bob Badoura5ea2472022-05-20 16:37:26 -070091func BuildNoticeHtmlOutputFromLicenseMetadata(ctx BuilderContext, outputFile WritablePath, ruleName, libraryName, stripPrefix string, modules ...Module) {
92 buildNoticeOutputFromLicenseMetadata(ctx, "htmlnotice", "html_notice_"+ruleName, outputFile, libraryName, stripPrefix, modules...)
Bob Badoureef4c1c2022-05-16 12:20:04 -070093}
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.
Bob Badoura5ea2472022-05-20 16:37:26 -070098func BuildNoticeXmlOutputFromLicenseMetadata(ctx BuilderContext, outputFile WritablePath, ruleName, libraryName, stripPrefix string, modules ...Module) {
99 buildNoticeOutputFromLicenseMetadata(ctx, "xmlnotice", "xml_notice_"+ruleName, outputFile, libraryName, stripPrefix, modules...)
Dan Willemsen9fe14102021-07-13 21:52:04 -0700100}