2019-06-18 02:40:56 +02:00
|
|
|
// Copyright 2019 Google Inc. All rights reserved.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
package android
|
|
|
|
|
|
|
|
import (
|
2022-05-16 21:20:04 +02:00
|
|
|
"fmt"
|
|
|
|
"path/filepath"
|
2022-01-28 23:49:24 +01:00
|
|
|
"strings"
|
2019-06-18 02:40:56 +02:00
|
|
|
)
|
|
|
|
|
2022-05-16 21:20:04 +02:00
|
|
|
func modulesOutputDirs(ctx BuilderContext, modules ...Module) []string {
|
|
|
|
dirs := make([]string, 0, len(modules))
|
|
|
|
for _, module := range modules {
|
|
|
|
paths, err := outputFilesForModule(ctx, module, "")
|
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
for _, path := range paths {
|
|
|
|
if path != nil {
|
|
|
|
dirs = append(dirs, filepath.Dir(path.String()))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return SortedUniqueStrings(dirs)
|
2022-04-01 20:00:00 +02:00
|
|
|
}
|
|
|
|
|
2022-05-16 21:20:04 +02:00
|
|
|
func modulesLicenseMetadata(ctx BuilderContext, modules ...Module) Paths {
|
|
|
|
result := make(Paths, 0, len(modules))
|
|
|
|
for _, module := range modules {
|
|
|
|
if mf := module.base().licenseMetadataFile; mf != nil {
|
|
|
|
result = append(result, mf)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
|
|
|
// buildNoticeOutputFromLicenseMetadata writes out a notice file.
|
2022-06-09 00:59:35 +02:00
|
|
|
func buildNoticeOutputFromLicenseMetadata(
|
|
|
|
ctx BuilderContext, tool, ruleName string, outputFile WritablePath,
|
|
|
|
libraryName string, stripPrefix []string, modules ...Module) {
|
2022-04-01 20:00:00 +02:00
|
|
|
depsFile := outputFile.ReplaceExtension(ctx, strings.TrimPrefix(outputFile.Ext()+".d", "."))
|
|
|
|
rule := NewRuleBuilder(pctx, ctx)
|
2022-05-16 21:20:04 +02:00
|
|
|
if len(modules) == 0 {
|
|
|
|
if mctx, ok := ctx.(ModuleContext); ok {
|
|
|
|
modules = []Module{mctx.Module()}
|
|
|
|
} else {
|
2022-05-21 01:37:26 +02:00
|
|
|
panic(fmt.Errorf("%s %q needs a module to generate the notice for", ruleName, libraryName))
|
2022-05-16 21:20:04 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if libraryName == "" {
|
|
|
|
libraryName = modules[0].Name()
|
|
|
|
}
|
|
|
|
cmd := rule.Command().
|
|
|
|
BuiltTool(tool).
|
2022-04-01 20:00:00 +02:00
|
|
|
FlagWithOutput("-o ", outputFile).
|
2022-05-16 21:20:04 +02:00
|
|
|
FlagWithDepFile("-d ", depsFile)
|
2022-06-09 00:59:35 +02:00
|
|
|
if len(stripPrefix) > 0 {
|
|
|
|
cmd = cmd.FlagForEachArg("--strip_prefix ", stripPrefix)
|
2022-05-16 21:20:04 +02:00
|
|
|
}
|
|
|
|
outputs := modulesOutputDirs(ctx, modules...)
|
|
|
|
if len(outputs) > 0 {
|
|
|
|
cmd = cmd.FlagForEachArg("--strip_prefix ", outputs)
|
|
|
|
}
|
|
|
|
if libraryName != "" {
|
|
|
|
cmd = cmd.FlagWithArg("--product ", libraryName)
|
|
|
|
}
|
|
|
|
cmd = cmd.Inputs(modulesLicenseMetadata(ctx, modules...))
|
2022-05-21 01:37:26 +02:00
|
|
|
rule.Build(ruleName, "container notice file")
|
2022-05-16 21:20:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// BuildNoticeTextOutputFromLicenseMetadata writes out a notice text file based
|
|
|
|
// on the license metadata files for the input `modules` defaulting to the
|
|
|
|
// current context module if none given.
|
2022-06-09 00:59:35 +02:00
|
|
|
func BuildNoticeTextOutputFromLicenseMetadata(
|
|
|
|
ctx BuilderContext, outputFile WritablePath, ruleName, libraryName string,
|
|
|
|
stripPrefix []string, modules ...Module) {
|
|
|
|
buildNoticeOutputFromLicenseMetadata(ctx, "textnotice", "text_notice_"+ruleName,
|
|
|
|
outputFile, libraryName, stripPrefix, modules...)
|
2022-05-16 21:20:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// BuildNoticeHtmlOutputFromLicenseMetadata writes out a notice text file based
|
|
|
|
// on the license metadata files for the input `modules` defaulting to the
|
|
|
|
// current context module if none given.
|
2022-06-09 00:59:35 +02:00
|
|
|
func BuildNoticeHtmlOutputFromLicenseMetadata(
|
|
|
|
ctx BuilderContext, outputFile WritablePath, ruleName, libraryName string,
|
|
|
|
stripPrefix []string, modules ...Module) {
|
|
|
|
buildNoticeOutputFromLicenseMetadata(ctx, "htmlnotice", "html_notice_"+ruleName,
|
|
|
|
outputFile, libraryName, stripPrefix, modules...)
|
2022-05-16 21:20:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// BuildNoticeXmlOutputFromLicenseMetadata writes out a notice text file based
|
|
|
|
// on the license metadata files for the input `modules` defaulting to the
|
|
|
|
// current context module if none given.
|
2022-06-09 00:59:35 +02:00
|
|
|
func BuildNoticeXmlOutputFromLicenseMetadata(
|
|
|
|
ctx BuilderContext, outputFile WritablePath, ruleName, libraryName string,
|
|
|
|
stripPrefix []string, modules ...Module) {
|
|
|
|
buildNoticeOutputFromLicenseMetadata(ctx, "xmlnotice", "xml_notice_"+ruleName,
|
|
|
|
outputFile, libraryName, stripPrefix, modules...)
|
2021-07-14 06:52:04 +02:00
|
|
|
}
|