Bob Badour | eef4c1c | 2022-05-16 12:20:04 -0700 | [diff] [blame] | 1 | // Copyright 2020 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 | |
| 15 | package android |
| 16 | |
| 17 | import ( |
| 18 | "fmt" |
Colin Cross | 95f18bd | 2022-12-14 15:43:39 -0800 | [diff] [blame] | 19 | "path/filepath" |
Bob Badour | eef4c1c | 2022-05-16 12:20:04 -0700 | [diff] [blame] | 20 | "strings" |
| 21 | |
Yu Liu | 71f1ea3 | 2025-02-26 23:39:20 +0000 | [diff] [blame] | 22 | "github.com/google/blueprint" |
Bob Badour | eef4c1c | 2022-05-16 12:20:04 -0700 | [diff] [blame] | 23 | "github.com/google/blueprint/proptools" |
| 24 | ) |
| 25 | |
| 26 | func init() { |
| 27 | RegisterGenNoticeBuildComponents(InitRegistrationContext) |
| 28 | } |
| 29 | |
| 30 | // Register the gen_notice module type. |
| 31 | func RegisterGenNoticeBuildComponents(ctx RegistrationContext) { |
LaMont Jones | 0c10e4d | 2023-05-16 00:58:37 +0000 | [diff] [blame] | 32 | ctx.RegisterParallelSingletonType("gen_notice_build_rules", GenNoticeBuildRulesFactory) |
Bob Badour | eef4c1c | 2022-05-16 12:20:04 -0700 | [diff] [blame] | 33 | ctx.RegisterModuleType("gen_notice", GenNoticeFactory) |
| 34 | } |
| 35 | |
| 36 | type genNoticeBuildRules struct{} |
| 37 | |
| 38 | func (s *genNoticeBuildRules) GenerateBuildActions(ctx SingletonContext) { |
Yu Liu | 71f1ea3 | 2025-02-26 23:39:20 +0000 | [diff] [blame] | 39 | ctx.VisitAllModuleProxies(func(m ModuleProxy) { |
| 40 | gm, ok := OtherModuleProvider(ctx, m, GenNoticeInfoProvider) |
Bob Badour | eef4c1c | 2022-05-16 12:20:04 -0700 | [diff] [blame] | 41 | if !ok { |
| 42 | return |
| 43 | } |
Yu Liu | 71f1ea3 | 2025-02-26 23:39:20 +0000 | [diff] [blame] | 44 | if len(gm.Missing) > 0 { |
| 45 | missingReferencesRule(ctx, m, &gm) |
Bob Badour | eef4c1c | 2022-05-16 12:20:04 -0700 | [diff] [blame] | 46 | return |
| 47 | } |
| 48 | out := BuildNoticeTextOutputFromLicenseMetadata |
Yu Liu | 71f1ea3 | 2025-02-26 23:39:20 +0000 | [diff] [blame] | 49 | if gm.Xml { |
Bob Badour | eef4c1c | 2022-05-16 12:20:04 -0700 | [diff] [blame] | 50 | out = BuildNoticeXmlOutputFromLicenseMetadata |
Yu Liu | 71f1ea3 | 2025-02-26 23:39:20 +0000 | [diff] [blame] | 51 | } else if gm.Html { |
Bob Badour | eef4c1c | 2022-05-16 12:20:04 -0700 | [diff] [blame] | 52 | out = BuildNoticeHtmlOutputFromLicenseMetadata |
| 53 | } |
| 54 | defaultName := "" |
Yu Liu | 71f1ea3 | 2025-02-26 23:39:20 +0000 | [diff] [blame] | 55 | if len(gm.For) > 0 { |
| 56 | defaultName = gm.For[0] |
Bob Badour | eef4c1c | 2022-05-16 12:20:04 -0700 | [diff] [blame] | 57 | } |
| 58 | |
Yu Liu | 71f1ea3 | 2025-02-26 23:39:20 +0000 | [diff] [blame] | 59 | modules := make([]ModuleProxy, 0) |
| 60 | for _, name := range gm.For { |
| 61 | mods := ctx.ModuleVariantsFromName(m, name) |
Bob Badour | eef4c1c | 2022-05-16 12:20:04 -0700 | [diff] [blame] | 62 | for _, mod := range mods { |
Yu Liu | ef9e63e | 2025-03-04 19:01:28 +0000 | [diff] [blame^] | 63 | if !OtherModuleProviderOrDefault(ctx, mod, CommonModuleInfoProvider).Enabled { // don't depend on variants without build rules |
Bob Badour | 3800b5a | 2022-11-14 14:17:55 -0800 | [diff] [blame] | 64 | continue |
| 65 | } |
Bob Badour | eef4c1c | 2022-05-16 12:20:04 -0700 | [diff] [blame] | 66 | modules = append(modules, mod) |
| 67 | } |
| 68 | } |
| 69 | if ctx.Failed() { |
| 70 | return |
| 71 | } |
Yu Liu | 71f1ea3 | 2025-02-26 23:39:20 +0000 | [diff] [blame] | 72 | out(ctx, gm.Output, ctx.ModuleName(m), |
| 73 | proptools.StringDefault(gm.ArtifactName, defaultName), |
Bob Badour | c6ec9fb | 2022-06-08 15:59:35 -0700 | [diff] [blame] | 74 | []string{ |
Colin Cross | 95f18bd | 2022-12-14 15:43:39 -0800 | [diff] [blame] | 75 | filepath.Join(ctx.Config().OutDir(), "target", "product", ctx.Config().DeviceName()) + "/", |
Bob Badour | c6ec9fb | 2022-06-08 15:59:35 -0700 | [diff] [blame] | 76 | ctx.Config().OutDir() + "/", |
| 77 | ctx.Config().SoongOutDir() + "/", |
| 78 | }, modules...) |
Bob Badour | eef4c1c | 2022-05-16 12:20:04 -0700 | [diff] [blame] | 79 | }) |
| 80 | } |
| 81 | |
| 82 | func GenNoticeBuildRulesFactory() Singleton { |
| 83 | return &genNoticeBuildRules{} |
| 84 | } |
| 85 | |
| 86 | type genNoticeProperties struct { |
| 87 | // For specifies the modules for which to generate a notice file. |
| 88 | For []string |
| 89 | // ArtifactName specifies the internal name to use for the notice file. |
| 90 | // It appears in the "used by:" list for targets whose entire name is stripped by --strip_prefix. |
| 91 | ArtifactName *string |
| 92 | // Stem specifies the base name of the output file. |
| 93 | Stem *string `android:"arch_variant"` |
| 94 | // Html indicates an html-format file is needed. The default is text. Can be Html or Xml but not both. |
| 95 | Html *bool |
| 96 | // Xml indicates an xml-format file is needed. The default is text. Can be Html or Xml but not both. |
| 97 | Xml *bool |
| 98 | // Gzipped indicates the output file must be compressed with gzip. Will append .gz to suffix if not there. |
| 99 | Gzipped *bool |
| 100 | // Suffix specifies the file extension to use. Defaults to .html for html, .xml for xml, or no extension for text. |
| 101 | Suffix *string |
| 102 | // Visibility specifies where this license can be used |
| 103 | Visibility []string |
| 104 | } |
| 105 | |
| 106 | type genNoticeModule struct { |
| 107 | ModuleBase |
| 108 | DefaultableModuleBase |
| 109 | |
| 110 | properties genNoticeProperties |
| 111 | |
| 112 | output OutputPath |
| 113 | missing []string |
| 114 | } |
| 115 | |
Yu Liu | 71f1ea3 | 2025-02-26 23:39:20 +0000 | [diff] [blame] | 116 | type GenNoticeInfo struct { |
| 117 | // For specifies the modules for which to generate a notice file. |
| 118 | For []string |
| 119 | // ArtifactName specifies the internal name to use for the notice file. |
| 120 | // It appears in the "used by:" list for targets whose entire name is stripped by --strip_prefix. |
| 121 | ArtifactName *string |
| 122 | // Html indicates an html-format file is needed. The default is text. Can be Html or Xml but not both. |
| 123 | Html bool |
| 124 | // Xml indicates an xml-format file is needed. The default is text. Can be Html or Xml but not both. |
| 125 | Xml bool |
| 126 | Output OutputPath |
| 127 | Missing []string |
| 128 | } |
| 129 | |
| 130 | var GenNoticeInfoProvider = blueprint.NewProvider[GenNoticeInfo]() |
| 131 | |
Bob Badour | eef4c1c | 2022-05-16 12:20:04 -0700 | [diff] [blame] | 132 | func (m *genNoticeModule) DepsMutator(ctx BottomUpMutatorContext) { |
Bob Badour | 4660a98 | 2022-09-12 16:06:03 -0700 | [diff] [blame] | 133 | if ctx.ContainsProperty("licenses") { |
| 134 | ctx.PropertyErrorf("licenses", "not supported on \"gen_notice\" modules") |
| 135 | } |
Bob Badour | eef4c1c | 2022-05-16 12:20:04 -0700 | [diff] [blame] | 136 | if proptools.Bool(m.properties.Html) && proptools.Bool(m.properties.Xml) { |
| 137 | ctx.ModuleErrorf("can be html or xml but not both") |
| 138 | } |
| 139 | if !ctx.Config().AllowMissingDependencies() { |
| 140 | var missing []string |
| 141 | // Verify the modules for which to generate notices exist. |
| 142 | for _, otherMod := range m.properties.For { |
| 143 | if !ctx.OtherModuleExists(otherMod) { |
| 144 | missing = append(missing, otherMod) |
| 145 | } |
| 146 | } |
| 147 | if len(missing) == 1 { |
| 148 | ctx.PropertyErrorf("for", "no %q module exists", missing[0]) |
| 149 | } else if len(missing) > 1 { |
| 150 | ctx.PropertyErrorf("for", "modules \"%s\" do not exist", strings.Join(missing, "\", \"")) |
| 151 | } |
| 152 | } |
| 153 | } |
| 154 | |
| 155 | func (m *genNoticeModule) getStem() string { |
| 156 | stem := m.base().BaseModuleName() |
| 157 | if m.properties.Stem != nil { |
| 158 | stem = proptools.String(m.properties.Stem) |
| 159 | } |
| 160 | return stem |
| 161 | } |
| 162 | |
| 163 | func (m *genNoticeModule) getSuffix() string { |
| 164 | suffix := "" |
| 165 | if m.properties.Suffix == nil { |
| 166 | if proptools.Bool(m.properties.Html) { |
| 167 | suffix = ".html" |
| 168 | } else if proptools.Bool(m.properties.Xml) { |
| 169 | suffix = ".xml" |
| 170 | } |
| 171 | } else { |
| 172 | suffix = proptools.String(m.properties.Suffix) |
| 173 | } |
| 174 | if proptools.Bool(m.properties.Gzipped) && !strings.HasSuffix(suffix, ".gz") { |
| 175 | suffix += ".gz" |
| 176 | } |
| 177 | return suffix |
| 178 | } |
| 179 | |
| 180 | func (m *genNoticeModule) GenerateAndroidBuildActions(ctx ModuleContext) { |
| 181 | if ctx.Config().AllowMissingDependencies() { |
| 182 | // Verify the modules for which to generate notices exist. |
| 183 | for _, otherMod := range m.properties.For { |
| 184 | if !ctx.OtherModuleExists(otherMod) { |
| 185 | m.missing = append(m.missing, otherMod) |
| 186 | } |
| 187 | } |
| 188 | m.missing = append(m.missing, ctx.GetMissingDependencies()...) |
| 189 | m.missing = FirstUniqueStrings(m.missing) |
| 190 | } |
| 191 | out := m.getStem() + m.getSuffix() |
| 192 | m.output = PathForModuleOut(ctx, out).OutputPath |
Yu Liu | 71f1ea3 | 2025-02-26 23:39:20 +0000 | [diff] [blame] | 193 | |
| 194 | SetProvider(ctx, GenNoticeInfoProvider, GenNoticeInfo{ |
| 195 | For: m.properties.For, |
| 196 | ArtifactName: m.properties.ArtifactName, |
| 197 | Xml: proptools.Bool(m.properties.Xml), |
| 198 | Html: proptools.Bool(m.properties.Html), |
| 199 | Output: m.output, |
| 200 | Missing: m.missing, |
| 201 | }) |
mrziwang | 1b25df0 | 2024-06-06 15:20:42 -0700 | [diff] [blame] | 202 | ctx.SetOutputFiles(Paths{m.output}, "") |
Bob Badour | eef4c1c | 2022-05-16 12:20:04 -0700 | [diff] [blame] | 203 | } |
| 204 | |
| 205 | func GenNoticeFactory() Module { |
| 206 | module := &genNoticeModule{} |
| 207 | |
| 208 | base := module.base() |
| 209 | module.AddProperties(&base.nameProperties, &module.properties) |
| 210 | |
| 211 | // The visibility property needs to be checked and parsed by the visibility module. |
| 212 | setPrimaryVisibilityProperty(module, "visibility", &module.properties.Visibility) |
| 213 | |
Bob Badour | e383873 | 2022-09-08 12:01:57 -0700 | [diff] [blame] | 214 | InitAndroidArchModule(module, DeviceSupported, MultilibCommon) |
Bob Badour | eef4c1c | 2022-05-16 12:20:04 -0700 | [diff] [blame] | 215 | InitDefaultableModule(module) |
| 216 | |
| 217 | return module |
| 218 | } |
| 219 | |
Bob Badour | 4660a98 | 2022-09-12 16:06:03 -0700 | [diff] [blame] | 220 | var _ AndroidMkEntriesProvider = (*genNoticeModule)(nil) |
| 221 | |
| 222 | // Implements AndroidMkEntriesProvider |
| 223 | func (m *genNoticeModule) AndroidMkEntries() []AndroidMkEntries { |
| 224 | return []AndroidMkEntries{AndroidMkEntries{ |
| 225 | Class: "ETC", |
| 226 | OutputFile: OptionalPathForPath(m.output), |
| 227 | }} |
| 228 | } |
| 229 | |
Bob Badour | eef4c1c | 2022-05-16 12:20:04 -0700 | [diff] [blame] | 230 | // missingReferencesRule emits an ErrorRule for missing module references. |
Yu Liu | 71f1ea3 | 2025-02-26 23:39:20 +0000 | [diff] [blame] | 231 | func missingReferencesRule(ctx BuilderContext, m ModuleProxy, genInfo *GenNoticeInfo) { |
| 232 | if len(genInfo.Missing) < 1 { |
Bob Badour | eef4c1c | 2022-05-16 12:20:04 -0700 | [diff] [blame] | 233 | panic(fmt.Errorf("missing references rule requested with no missing references")) |
| 234 | } |
| 235 | |
| 236 | ctx.Build(pctx, BuildParams{ |
| 237 | Rule: ErrorRule, |
Yu Liu | 71f1ea3 | 2025-02-26 23:39:20 +0000 | [diff] [blame] | 238 | Output: genInfo.Output, |
| 239 | Description: "notice for " + proptools.StringDefault(genInfo.ArtifactName, "container"), |
Bob Badour | eef4c1c | 2022-05-16 12:20:04 -0700 | [diff] [blame] | 240 | Args: map[string]string{ |
Yu Liu | 71f1ea3 | 2025-02-26 23:39:20 +0000 | [diff] [blame] | 241 | "error": m.Name() + " references missing module(s): " + strings.Join(genInfo.Missing, ", "), |
Bob Badour | eef4c1c | 2022-05-16 12:20:04 -0700 | [diff] [blame] | 242 | }, |
| 243 | }) |
| 244 | } |