• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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
15package android
16
17import (
18	"fmt"
19	"path/filepath"
20	"strings"
21
22	"github.com/google/blueprint"
23)
24
25func modulesOutputDirs(ctx BuilderContext, modules ...ModuleProxy) []string {
26	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)
39}
40
41type BuilderAndOtherModuleProviderContext interface {
42	BuilderContext
43	OtherModuleProviderContext
44}
45
46func modulesLicenseMetadata(ctx OtherModuleProviderContext, modules ...ModuleProxy) Paths {
47	result := make(Paths, 0, len(modules))
48	mctx, isMctx := ctx.(ModuleContext)
49	for _, module := range modules {
50		var mf Path
51		if isMctx && EqualModules(mctx.Module(), module) {
52			mf = mctx.LicenseMetadataFile()
53		} else {
54			mf = OtherModuleProviderOrDefault(ctx, module, InstallFilesProvider).LicenseMetadataFile
55		}
56		if mf != nil {
57			result = append(result, mf)
58		}
59	}
60	return result
61}
62
63// buildNoticeOutputFromLicenseMetadata writes out a notice file.
64func buildNoticeOutputFromLicenseMetadata(
65	ctx BuilderAndOtherModuleProviderContext, tool, ruleName string, outputFile WritablePath,
66	libraryName string, stripPrefix []string, modules ...ModuleProxy) {
67	depsFile := outputFile.ReplaceExtension(ctx, strings.TrimPrefix(outputFile.Ext()+".d", "."))
68	rule := NewRuleBuilder(pctx, ctx)
69	if len(modules) == 0 {
70		if mctx, ok := ctx.(ModuleContext); ok {
71			modules = []ModuleProxy{{blueprint.CreateModuleProxy(mctx.Module())}}
72		} else {
73			panic(fmt.Errorf("%s %q needs a module to generate the notice for", ruleName, libraryName))
74		}
75	}
76	if libraryName == "" {
77		libraryName = modules[0].Name()
78	}
79	cmd := rule.Command().
80		BuiltTool(tool).
81		FlagWithOutput("-o ", outputFile).
82		FlagWithDepFile("-d ", depsFile)
83	if len(stripPrefix) > 0 {
84		cmd = cmd.FlagForEachArg("--strip_prefix ", stripPrefix)
85	}
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...))
94	rule.Build(ruleName, "container notice file")
95}
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.
100func BuildNoticeTextOutputFromLicenseMetadata(
101	ctx BuilderAndOtherModuleProviderContext, outputFile WritablePath, ruleName, libraryName string,
102	stripPrefix []string, modules ...ModuleProxy) {
103	buildNoticeOutputFromLicenseMetadata(ctx, "textnotice", "text_notice_"+ruleName,
104		outputFile, libraryName, stripPrefix, modules...)
105}
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.
110func BuildNoticeHtmlOutputFromLicenseMetadata(
111	ctx BuilderAndOtherModuleProviderContext, outputFile WritablePath, ruleName, libraryName string,
112	stripPrefix []string, modules ...ModuleProxy) {
113	buildNoticeOutputFromLicenseMetadata(ctx, "htmlnotice", "html_notice_"+ruleName,
114		outputFile, libraryName, stripPrefix, modules...)
115}
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.
120func BuildNoticeXmlOutputFromLicenseMetadata(
121	ctx BuilderAndOtherModuleProviderContext, outputFile WritablePath, ruleName, libraryName string,
122	stripPrefix []string, modules ...ModuleProxy) {
123	buildNoticeOutputFromLicenseMetadata(ctx, "xmlnotice", "xml_notice_"+ruleName,
124		outputFile, libraryName, stripPrefix, modules...)
125}
126