blob: dc2290cce283c3636ce0a8efefd7c9d1da749be3 [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"
Yu Liu71f1ea32025-02-26 23:39:20 +000021
22 "github.com/google/blueprint"
Jaewoong Jung5b425e22019-06-17 17:40:56 -070023)
24
Yu Liu71f1ea32025-02-26 23:39:20 +000025func modulesOutputDirs(ctx BuilderContext, modules ...ModuleProxy) []string {
Bob Badoureef4c1c2022-05-16 12:20:04 -070026 dirs := make([]string, 0, len(modules))
27 for _, module := range modules {
28 paths, err := outputFilesForModule(ctx, module, "")
29 if err != nil {
30 continue
31 }
32 for _, path := range paths {
33 if path != nil {
34 dirs = append(dirs, filepath.Dir(path.String()))
35 }
36 }
37 }
38 return SortedUniqueStrings(dirs)
Bob Badourde6a0872022-04-01 18:00:00 +000039}
40
Yu Liuec810542024-08-26 18:09:15 +000041type BuilderAndOtherModuleProviderContext interface {
42 BuilderContext
43 OtherModuleProviderContext
44}
45
Yu Liu71f1ea32025-02-26 23:39:20 +000046func modulesLicenseMetadata(ctx OtherModuleProviderContext, modules ...ModuleProxy) Paths {
Bob Badoureef4c1c2022-05-16 12:20:04 -070047 result := make(Paths, 0, len(modules))
Yu Liuec810542024-08-26 18:09:15 +000048 mctx, isMctx := ctx.(ModuleContext)
Bob Badoureef4c1c2022-05-16 12:20:04 -070049 for _, module := range modules {
Yu Liuec810542024-08-26 18:09:15 +000050 var mf Path
Yu Liu71f1ea32025-02-26 23:39:20 +000051 if isMctx && EqualModules(mctx.Module(), module) {
Yu Liuec810542024-08-26 18:09:15 +000052 mf = mctx.LicenseMetadataFile()
53 } else {
54 mf = OtherModuleProviderOrDefault(ctx, module, InstallFilesProvider).LicenseMetadataFile
55 }
56 if mf != nil {
Bob Badoureef4c1c2022-05-16 12:20:04 -070057 result = append(result, mf)
58 }
59 }
60 return result
61}
62
63// buildNoticeOutputFromLicenseMetadata writes out a notice file.
Bob Badourc6ec9fb2022-06-08 15:59:35 -070064func buildNoticeOutputFromLicenseMetadata(
Yu Liuec810542024-08-26 18:09:15 +000065 ctx BuilderAndOtherModuleProviderContext, tool, ruleName string, outputFile WritablePath,
Yu Liu71f1ea32025-02-26 23:39:20 +000066 libraryName string, stripPrefix []string, modules ...ModuleProxy) {
Bob Badourde6a0872022-04-01 18:00:00 +000067 depsFile := outputFile.ReplaceExtension(ctx, strings.TrimPrefix(outputFile.Ext()+".d", "."))
68 rule := NewRuleBuilder(pctx, ctx)
Bob Badoureef4c1c2022-05-16 12:20:04 -070069 if len(modules) == 0 {
70 if mctx, ok := ctx.(ModuleContext); ok {
Yu Liu71f1ea32025-02-26 23:39:20 +000071 modules = []ModuleProxy{{blueprint.CreateModuleProxy(mctx.Module())}}
Bob Badoureef4c1c2022-05-16 12:20:04 -070072 } else {
Bob Badoura5ea2472022-05-20 16:37:26 -070073 panic(fmt.Errorf("%s %q needs a module to generate the notice for", ruleName, libraryName))
Bob Badoureef4c1c2022-05-16 12:20:04 -070074 }
75 }
76 if libraryName == "" {
77 libraryName = modules[0].Name()
78 }
79 cmd := rule.Command().
80 BuiltTool(tool).
Bob Badourde6a0872022-04-01 18:00:00 +000081 FlagWithOutput("-o ", outputFile).
Bob Badoureef4c1c2022-05-16 12:20:04 -070082 FlagWithDepFile("-d ", depsFile)
Bob Badourc6ec9fb2022-06-08 15:59:35 -070083 if len(stripPrefix) > 0 {
84 cmd = cmd.FlagForEachArg("--strip_prefix ", stripPrefix)
Bob Badoureef4c1c2022-05-16 12:20:04 -070085 }
86 outputs := modulesOutputDirs(ctx, modules...)
87 if len(outputs) > 0 {
88 cmd = cmd.FlagForEachArg("--strip_prefix ", outputs)
89 }
90 if libraryName != "" {
91 cmd = cmd.FlagWithArg("--product ", libraryName)
92 }
93 cmd = cmd.Inputs(modulesLicenseMetadata(ctx, modules...))
Bob Badoura5ea2472022-05-20 16:37:26 -070094 rule.Build(ruleName, "container notice file")
Bob Badoureef4c1c2022-05-16 12:20:04 -070095}
96
97// BuildNoticeTextOutputFromLicenseMetadata writes out a notice text file based
98// on the license metadata files for the input `modules` defaulting to the
99// current context module if none given.
Bob Badourc6ec9fb2022-06-08 15:59:35 -0700100func BuildNoticeTextOutputFromLicenseMetadata(
Yu Liuec810542024-08-26 18:09:15 +0000101 ctx BuilderAndOtherModuleProviderContext, outputFile WritablePath, ruleName, libraryName string,
Yu Liu71f1ea32025-02-26 23:39:20 +0000102 stripPrefix []string, modules ...ModuleProxy) {
Bob Badourc6ec9fb2022-06-08 15:59:35 -0700103 buildNoticeOutputFromLicenseMetadata(ctx, "textnotice", "text_notice_"+ruleName,
104 outputFile, libraryName, stripPrefix, modules...)
Bob Badoureef4c1c2022-05-16 12:20:04 -0700105}
106
107// BuildNoticeHtmlOutputFromLicenseMetadata writes out a notice text file based
108// on the license metadata files for the input `modules` defaulting to the
109// current context module if none given.
Bob Badourc6ec9fb2022-06-08 15:59:35 -0700110func BuildNoticeHtmlOutputFromLicenseMetadata(
Yu Liuec810542024-08-26 18:09:15 +0000111 ctx BuilderAndOtherModuleProviderContext, outputFile WritablePath, ruleName, libraryName string,
Yu Liu71f1ea32025-02-26 23:39:20 +0000112 stripPrefix []string, modules ...ModuleProxy) {
Bob Badourc6ec9fb2022-06-08 15:59:35 -0700113 buildNoticeOutputFromLicenseMetadata(ctx, "htmlnotice", "html_notice_"+ruleName,
114 outputFile, libraryName, stripPrefix, modules...)
Bob Badoureef4c1c2022-05-16 12:20:04 -0700115}
116
117// BuildNoticeXmlOutputFromLicenseMetadata writes out a notice text file based
118// on the license metadata files for the input `modules` defaulting to the
119// current context module if none given.
Bob Badourc6ec9fb2022-06-08 15:59:35 -0700120func BuildNoticeXmlOutputFromLicenseMetadata(
Yu Liuec810542024-08-26 18:09:15 +0000121 ctx BuilderAndOtherModuleProviderContext, outputFile WritablePath, ruleName, libraryName string,
Yu Liu71f1ea32025-02-26 23:39:20 +0000122 stripPrefix []string, modules ...ModuleProxy) {
Bob Badourc6ec9fb2022-06-08 15:59:35 -0700123 buildNoticeOutputFromLicenseMetadata(ctx, "xmlnotice", "xml_notice_"+ruleName,
124 outputFile, libraryName, stripPrefix, modules...)
Dan Willemsen9fe14102021-07-13 21:52:04 -0700125}