Dan Willemsen | a03cf6d | 2016-09-26 15:45:04 -0700 | [diff] [blame] | 1 | // Copyright 2016 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 cc |
| 16 | |
| 17 | import ( |
Chih-Hung Hsieh | 794b81d | 2022-06-11 18:10:58 -0700 | [diff] [blame] | 18 | "fmt" |
Chih-Hung Hsieh | 8078377 | 2021-10-11 16:46:56 -0700 | [diff] [blame] | 19 | "path/filepath" |
Chih-Hung Hsieh | 1b4934a | 2021-01-14 15:45:25 -0800 | [diff] [blame] | 20 | "regexp" |
Dan Willemsen | a03cf6d | 2016-09-26 15:45:04 -0700 | [diff] [blame] | 21 | "strings" |
| 22 | |
| 23 | "github.com/google/blueprint/proptools" |
| 24 | |
Chih-Hung Hsieh | 9f94c36 | 2021-02-10 21:56:03 -0800 | [diff] [blame] | 25 | "android/soong/android" |
Dan Willemsen | a03cf6d | 2016-09-26 15:45:04 -0700 | [diff] [blame] | 26 | "android/soong/cc/config" |
| 27 | ) |
| 28 | |
| 29 | type TidyProperties struct { |
| 30 | // whether to run clang-tidy over C-like sources. |
| 31 | Tidy *bool |
| 32 | |
| 33 | // Extra flags to pass to clang-tidy |
| 34 | Tidy_flags []string |
| 35 | |
| 36 | // Extra checks to enable or disable in clang-tidy |
| 37 | Tidy_checks []string |
Nikita Ioffe | 32c4986 | 2019-03-26 20:33:49 +0000 | [diff] [blame] | 38 | |
| 39 | // Checks that should be treated as errors. |
| 40 | Tidy_checks_as_errors []string |
Dan Willemsen | a03cf6d | 2016-09-26 15:45:04 -0700 | [diff] [blame] | 41 | } |
| 42 | |
| 43 | type tidyFeature struct { |
| 44 | Properties TidyProperties |
| 45 | } |
| 46 | |
Chih-Hung Hsieh | 217e09a | 2021-02-22 17:03:15 -0800 | [diff] [blame] | 47 | var quotedFlagRegexp, _ = regexp.Compile(`^-?-[^=]+=('|").*('|")$`) |
| 48 | |
| 49 | // When passing flag -name=value, if user add quotes around 'value', |
| 50 | // the quotation marks will be preserved by NinjaAndShellEscapeList |
| 51 | // and the 'value' string with quotes won't work like the intended value. |
| 52 | // So here we report an error if -*='*' is found. |
| 53 | func checkNinjaAndShellEscapeList(ctx ModuleContext, prop string, slice []string) []string { |
| 54 | for _, s := range slice { |
| 55 | if quotedFlagRegexp.MatchString(s) { |
| 56 | ctx.PropertyErrorf(prop, "Extra quotes in: %s", s) |
| 57 | } |
| 58 | } |
| 59 | return proptools.NinjaAndShellEscapeList(slice) |
| 60 | } |
| 61 | |
Dan Willemsen | a03cf6d | 2016-09-26 15:45:04 -0700 | [diff] [blame] | 62 | func (tidy *tidyFeature) props() []interface{} { |
| 63 | return []interface{}{&tidy.Properties} |
| 64 | } |
| 65 | |
Chih-Hung Hsieh | 794b81d | 2022-06-11 18:10:58 -0700 | [diff] [blame] | 66 | // Set this const to true when all -warnings-as-errors in tidy_flags |
| 67 | // are replaced with tidy_checks_as_errors. |
| 68 | // Then, that old style usage will be obsolete and an error. |
Chih-Hung Hsieh | 9f876e9 | 2022-06-12 20:28:00 -0700 | [diff] [blame] | 69 | const NoWarningsAsErrorsInTidyFlags = true |
Chih-Hung Hsieh | 794b81d | 2022-06-11 18:10:58 -0700 | [diff] [blame] | 70 | |
Dan Willemsen | a03cf6d | 2016-09-26 15:45:04 -0700 | [diff] [blame] | 71 | func (tidy *tidyFeature) flags(ctx ModuleContext, flags Flags) Flags { |
Colin Cross | 379d2cb | 2016-12-05 17:11:06 -0800 | [diff] [blame] | 72 | CheckBadTidyFlags(ctx, "tidy_flags", tidy.Properties.Tidy_flags) |
| 73 | CheckBadTidyChecks(ctx, "tidy_checks", tidy.Properties.Tidy_checks) |
| 74 | |
Dan Willemsen | a03cf6d | 2016-09-26 15:45:04 -0700 | [diff] [blame] | 75 | // Check if tidy is explicitly disabled for this module |
| 76 | if tidy.Properties.Tidy != nil && !*tidy.Properties.Tidy { |
| 77 | return flags |
| 78 | } |
Chih-Hung Hsieh | 140c40a | 2022-10-05 13:52:30 -0700 | [diff] [blame] | 79 | // Some projects like external/* and vendor/* have clang-tidy disabled by default, |
| 80 | // unless they are enabled explicitly with the "tidy:true" property or |
| 81 | // when TIDY_EXTERNAL_VENDOR is set to true. |
Chih-Hung Hsieh | ff2efae | 2022-10-18 14:43:27 -0700 | [diff] [blame] | 82 | if !proptools.Bool(tidy.Properties.Tidy) && |
| 83 | config.NoClangTidyForDir( |
| 84 | ctx.Config().IsEnvTrue("TIDY_EXTERNAL_VENDOR"), |
| 85 | ctx.ModuleDir()) { |
Chih-Hung Hsieh | 1a46753 | 2022-09-01 17:14:22 -0700 | [diff] [blame] | 86 | return flags |
| 87 | } |
Chih-Hung Hsieh | 7540a78 | 2022-01-08 19:56:09 -0800 | [diff] [blame] | 88 | // If not explicitly disabled, set flags.Tidy to generate .tidy rules. |
| 89 | // Note that libraries and binaries will depend on .tidy files ONLY if |
| 90 | // the global WITH_TIDY or module 'tidy' property is true. |
Dan Willemsen | a03cf6d | 2016-09-26 15:45:04 -0700 | [diff] [blame] | 91 | flags.Tidy = true |
| 92 | |
Chih-Hung Hsieh | 104f51f | 2022-04-20 15:48:41 -0700 | [diff] [blame] | 93 | // If explicitly enabled, by global WITH_TIDY or local tidy:true property, |
Chih-Hung Hsieh | 7540a78 | 2022-01-08 19:56:09 -0800 | [diff] [blame] | 94 | // set flags.NeedTidyFiles to make this module depend on .tidy files. |
Chih-Hung Hsieh | 104f51f | 2022-04-20 15:48:41 -0700 | [diff] [blame] | 95 | // Note that locally set tidy:true is ignored if ALLOW_LOCAL_TIDY_TRUE is not set to true. |
| 96 | if ctx.Config().IsEnvTrue("WITH_TIDY") || (ctx.Config().IsEnvTrue("ALLOW_LOCAL_TIDY_TRUE") && Bool(tidy.Properties.Tidy)) { |
Chih-Hung Hsieh | 7540a78 | 2022-01-08 19:56:09 -0800 | [diff] [blame] | 97 | flags.NeedTidyFiles = true |
| 98 | } |
| 99 | |
Chih-Hung Hsieh | 9e5d8a6 | 2018-09-21 15:12:44 -0700 | [diff] [blame] | 100 | // Add global WITH_TIDY_FLAGS and local tidy_flags. |
| 101 | withTidyFlags := ctx.Config().Getenv("WITH_TIDY_FLAGS") |
| 102 | if len(withTidyFlags) > 0 { |
| 103 | flags.TidyFlags = append(flags.TidyFlags, withTidyFlags) |
| 104 | } |
Chih-Hung Hsieh | 217e09a | 2021-02-22 17:03:15 -0800 | [diff] [blame] | 105 | esc := checkNinjaAndShellEscapeList |
| 106 | flags.TidyFlags = append(flags.TidyFlags, esc(ctx, "tidy_flags", tidy.Properties.Tidy_flags)...) |
Chih-Hung Hsieh | 9f94c36 | 2021-02-10 21:56:03 -0800 | [diff] [blame] | 107 | // If TidyFlags does not contain -header-filter, add default header filter. |
| 108 | // Find the substring because the flag could also appear as --header-filter=... |
| 109 | // and with or without single or double quotes. |
| 110 | if !android.SubstringInList(flags.TidyFlags, "-header-filter=") { |
| 111 | defaultDirs := ctx.Config().Getenv("DEFAULT_TIDY_HEADER_DIRS") |
| 112 | headerFilter := "-header-filter=" |
Chih-Hung Hsieh | 5fe637a | 2022-05-09 11:02:25 -0700 | [diff] [blame] | 113 | // Default header filter should include only the module directory, |
| 114 | // not the out/soong/.../ModuleDir/... |
| 115 | // Otherwise, there will be too many warnings from generated files in out/... |
| 116 | // If a module wants to see warnings in the generated source files, |
| 117 | // it should specify its own -header-filter flag. |
Chih-Hung Hsieh | 9f94c36 | 2021-02-10 21:56:03 -0800 | [diff] [blame] | 118 | if defaultDirs == "" { |
Chih-Hung Hsieh | 5fe637a | 2022-05-09 11:02:25 -0700 | [diff] [blame] | 119 | headerFilter += "^" + ctx.ModuleDir() + "/" |
Chih-Hung Hsieh | 9f94c36 | 2021-02-10 21:56:03 -0800 | [diff] [blame] | 120 | } else { |
Chih-Hung Hsieh | 5fe637a | 2022-05-09 11:02:25 -0700 | [diff] [blame] | 121 | headerFilter += "\"(^" + ctx.ModuleDir() + "/|" + defaultDirs + ")\"" |
Chih-Hung Hsieh | 9f94c36 | 2021-02-10 21:56:03 -0800 | [diff] [blame] | 122 | } |
Dan Willemsen | a03cf6d | 2016-09-26 15:45:04 -0700 | [diff] [blame] | 123 | flags.TidyFlags = append(flags.TidyFlags, headerFilter) |
| 124 | } |
Chih-Hung Hsieh | 63d59eb | 2022-02-04 17:42:02 -0800 | [diff] [blame] | 125 | // Work around RBE bug in parsing clang-tidy flags, replace "--flag" with "-flag". |
| 126 | // Some C/C++ modules added local tidy flags like --header-filter= and --extra-arg-before=. |
| 127 | doubleDash := regexp.MustCompile("^('?)--(.*)$") |
| 128 | for i, s := range flags.TidyFlags { |
| 129 | flags.TidyFlags[i] = doubleDash.ReplaceAllString(s, "$1-$2") |
| 130 | } |
Dan Willemsen | a03cf6d | 2016-09-26 15:45:04 -0700 | [diff] [blame] | 131 | |
Chih-Hung Hsieh | dc0c030 | 2017-12-15 20:57:48 -0800 | [diff] [blame] | 132 | // If clang-tidy is not enabled globally, add the -quiet flag. |
| 133 | if !ctx.Config().ClangTidy() { |
| 134 | flags.TidyFlags = append(flags.TidyFlags, "-quiet") |
Chih-Hung Hsieh | 669cb91 | 2018-01-04 01:41:16 -0800 | [diff] [blame] | 135 | flags.TidyFlags = append(flags.TidyFlags, "-extra-arg-before=-fno-caret-diagnostics") |
Chih-Hung Hsieh | dc0c030 | 2017-12-15 20:57:48 -0800 | [diff] [blame] | 136 | } |
| 137 | |
Sam Delmerico | ee030d2 | 2022-11-10 14:33:40 -0500 | [diff] [blame] | 138 | for _, f := range config.TidyExtraArgFlags() { |
George Burgess IV | 030ccee | 2018-05-14 16:30:46 -0700 | [diff] [blame] | 139 | flags.TidyFlags = append(flags.TidyFlags, "-extra-arg-before="+f) |
| 140 | } |
George Burgess IV | 561a3fe | 2017-05-03 18:13:08 -0700 | [diff] [blame] | 141 | |
Dan Willemsen | a03cf6d | 2016-09-26 15:45:04 -0700 | [diff] [blame] | 142 | tidyChecks := "-checks=" |
Colin Cross | 6510f91 | 2017-11-29 00:27:14 -0800 | [diff] [blame] | 143 | if checks := ctx.Config().TidyChecks(); len(checks) > 0 { |
Dan Willemsen | a03cf6d | 2016-09-26 15:45:04 -0700 | [diff] [blame] | 144 | tidyChecks += checks |
| 145 | } else { |
| 146 | tidyChecks += config.TidyChecksForDir(ctx.ModuleDir()) |
| 147 | } |
| 148 | if len(tidy.Properties.Tidy_checks) > 0 { |
Chih-Hung Hsieh | 80e3e03 | 2022-06-02 19:55:15 -0700 | [diff] [blame] | 149 | // If Tidy_checks contains "-*", ignore all checks before "-*". |
| 150 | localChecks := tidy.Properties.Tidy_checks |
| 151 | ignoreGlobalChecks := false |
| 152 | for n, check := range tidy.Properties.Tidy_checks { |
| 153 | if check == "-*" { |
| 154 | ignoreGlobalChecks = true |
| 155 | localChecks = tidy.Properties.Tidy_checks[n:] |
| 156 | } |
| 157 | } |
| 158 | if ignoreGlobalChecks { |
| 159 | tidyChecks = "-checks=" + strings.Join(esc(ctx, "tidy_checks", |
| 160 | config.ClangRewriteTidyChecks(localChecks)), ",") |
| 161 | } else { |
| 162 | tidyChecks = tidyChecks + "," + strings.Join(esc(ctx, "tidy_checks", |
| 163 | config.ClangRewriteTidyChecks(localChecks)), ",") |
| 164 | } |
Dan Willemsen | a03cf6d | 2016-09-26 15:45:04 -0700 | [diff] [blame] | 165 | } |
Chih-Hung Hsieh | 794b81d | 2022-06-11 18:10:58 -0700 | [diff] [blame] | 166 | tidyChecks = tidyChecks + config.TidyGlobalNoChecks() |
Chih-Hung Hsieh | 327b6f0 | 2018-12-10 16:28:56 -0800 | [diff] [blame] | 167 | if ctx.Windows() { |
| 168 | // https://b.corp.google.com/issues/120614316 |
| 169 | // mingw32 has cert-dcl16-c warning in NO_ERROR, |
| 170 | // which is used in many Android files. |
Chih-Hung Hsieh | 794b81d | 2022-06-11 18:10:58 -0700 | [diff] [blame] | 171 | tidyChecks += ",-cert-dcl16-c" |
Chih-Hung Hsieh | 327b6f0 | 2018-12-10 16:28:56 -0800 | [diff] [blame] | 172 | } |
Chih-Hung Hsieh | 43b920e | 2022-06-09 17:58:41 -0700 | [diff] [blame] | 173 | |
Dan Willemsen | a03cf6d | 2016-09-26 15:45:04 -0700 | [diff] [blame] | 174 | flags.TidyFlags = append(flags.TidyFlags, tidyChecks) |
| 175 | |
Chih-Hung Hsieh | 794b81d | 2022-06-11 18:10:58 -0700 | [diff] [blame] | 176 | // Embedding -warnings-as-errors in tidy_flags is error-prone. |
| 177 | // It should be replaced with the tidy_checks_as_errors list. |
| 178 | for i, s := range flags.TidyFlags { |
| 179 | if strings.Contains(s, "-warnings-as-errors=") { |
| 180 | if NoWarningsAsErrorsInTidyFlags { |
| 181 | ctx.PropertyErrorf("tidy_flags", "should not contain "+s+"; use tidy_checks_as_errors instead.") |
| 182 | } else { |
| 183 | fmt.Printf("%s: warning: module %s's tidy_flags should not contain %s, which is replaced with -warnings-as-errors=-*; use tidy_checks_as_errors for your own as-error warnings instead.\n", |
| 184 | ctx.BlueprintsFile(), ctx.ModuleName(), s) |
| 185 | flags.TidyFlags[i] = "-warnings-as-errors=-*" |
Yasin Kilicdere | 5a8ce13 | 2022-06-10 12:18:07 +0000 | [diff] [blame] | 186 | } |
Chih-Hung Hsieh | 794b81d | 2022-06-11 18:10:58 -0700 | [diff] [blame] | 187 | break // there is at most one -warnings-as-errors |
Chih-Hung Hsieh | 1b4934a | 2021-01-14 15:45:25 -0800 | [diff] [blame] | 188 | } |
Chih-Hung Hsieh | 794b81d | 2022-06-11 18:10:58 -0700 | [diff] [blame] | 189 | } |
| 190 | // Default clang-tidy flags does not contain -warning-as-errors. |
| 191 | // If a module has tidy_checks_as_errors, add the list to -warnings-as-errors |
| 192 | // and then append the TidyGlobalNoErrorChecks. |
| 193 | if len(tidy.Properties.Tidy_checks_as_errors) > 0 { |
| 194 | tidyChecksAsErrors := "-warnings-as-errors=" + |
| 195 | strings.Join(esc(ctx, "tidy_checks_as_errors", tidy.Properties.Tidy_checks_as_errors), ",") + |
| 196 | config.TidyGlobalNoErrorChecks() |
Nikita Ioffe | 32c4986 | 2019-03-26 20:33:49 +0000 | [diff] [blame] | 197 | flags.TidyFlags = append(flags.TidyFlags, tidyChecksAsErrors) |
| 198 | } |
Dan Willemsen | a03cf6d | 2016-09-26 15:45:04 -0700 | [diff] [blame] | 199 | return flags |
| 200 | } |
Chih-Hung Hsieh | 8078377 | 2021-10-11 16:46:56 -0700 | [diff] [blame] | 201 | |
| 202 | func init() { |
LaMont Jones | 0c10e4d | 2023-05-16 00:58:37 +0000 | [diff] [blame] | 203 | android.RegisterParallelSingletonType("tidy_phony_targets", TidyPhonySingleton) |
Chih-Hung Hsieh | 8078377 | 2021-10-11 16:46:56 -0700 | [diff] [blame] | 204 | } |
| 205 | |
| 206 | // This TidyPhonySingleton generates both tidy-* and obj-* phony targets for C/C++ files. |
| 207 | func TidyPhonySingleton() android.Singleton { |
| 208 | return &tidyPhonySingleton{} |
| 209 | } |
| 210 | |
| 211 | type tidyPhonySingleton struct{} |
| 212 | |
| 213 | // Given a final module, add its tidy/obj phony targets to tidy/objModulesInDirGroup. |
| 214 | func collectTidyObjModuleTargets(ctx android.SingletonContext, module android.Module, |
| 215 | tidyModulesInDirGroup, objModulesInDirGroup map[string]map[string]android.Paths) { |
| 216 | allObjFileGroups := make(map[string]android.Paths) // variant group name => obj file Paths |
| 217 | allTidyFileGroups := make(map[string]android.Paths) // variant group name => tidy file Paths |
| 218 | subsetObjFileGroups := make(map[string]android.Paths) // subset group name => obj file Paths |
| 219 | subsetTidyFileGroups := make(map[string]android.Paths) // subset group name => tidy file Paths |
| 220 | |
| 221 | // (1) Collect all obj/tidy files into OS-specific groups. |
Yu Liu | ec7043d | 2024-11-05 18:22:20 +0000 | [diff] [blame] | 222 | ctx.VisitAllModuleVariantProxies(module, func(variant android.ModuleProxy) { |
Yu Liu | b527532 | 2024-11-13 18:40:43 +0000 | [diff] [blame^] | 223 | osName := android.OtherModuleProviderOrDefault(ctx, variant, android.CommonModuleInfoKey).CompileTarget.Os.Name |
Yu Liu | ec7043d | 2024-11-05 18:22:20 +0000 | [diff] [blame] | 224 | info := android.OtherModuleProviderOrDefault(ctx, variant, CcObjectInfoProvider) |
| 225 | addToOSGroup(osName, info.objFiles, allObjFileGroups, subsetObjFileGroups) |
| 226 | addToOSGroup(osName, info.tidyFiles, allTidyFileGroups, subsetTidyFileGroups) |
Chih-Hung Hsieh | 8078377 | 2021-10-11 16:46:56 -0700 | [diff] [blame] | 227 | }) |
| 228 | |
| 229 | // (2) Add an all-OS group, with "" or "subset" name, to include all os-specific phony targets. |
| 230 | addAllOSGroup(ctx, module, allObjFileGroups, "", "obj") |
| 231 | addAllOSGroup(ctx, module, allTidyFileGroups, "", "tidy") |
| 232 | addAllOSGroup(ctx, module, subsetObjFileGroups, "subset", "obj") |
| 233 | addAllOSGroup(ctx, module, subsetTidyFileGroups, "subset", "tidy") |
| 234 | |
| 235 | tidyTargetGroups := make(map[string]android.Path) |
| 236 | objTargetGroups := make(map[string]android.Path) |
| 237 | genObjTidyPhonyTargets(ctx, module, "obj", allObjFileGroups, objTargetGroups) |
| 238 | genObjTidyPhonyTargets(ctx, module, "obj", subsetObjFileGroups, objTargetGroups) |
| 239 | genObjTidyPhonyTargets(ctx, module, "tidy", allTidyFileGroups, tidyTargetGroups) |
| 240 | genObjTidyPhonyTargets(ctx, module, "tidy", subsetTidyFileGroups, tidyTargetGroups) |
| 241 | |
| 242 | moduleDir := ctx.ModuleDir(module) |
| 243 | appendToModulesInDirGroup(tidyTargetGroups, moduleDir, tidyModulesInDirGroup) |
| 244 | appendToModulesInDirGroup(objTargetGroups, moduleDir, objModulesInDirGroup) |
| 245 | } |
| 246 | |
| 247 | func (m *tidyPhonySingleton) GenerateBuildActions(ctx android.SingletonContext) { |
| 248 | // For tidy-* directory phony targets, there are different variant groups. |
| 249 | // tidyModulesInDirGroup[G][D] is for group G, directory D, with Paths |
| 250 | // of all phony targets to be included into direct dependents of tidy-D_G. |
| 251 | tidyModulesInDirGroup := make(map[string]map[string]android.Paths) |
| 252 | // Also for obj-* directory phony targets. |
| 253 | objModulesInDirGroup := make(map[string]map[string]android.Paths) |
| 254 | |
| 255 | // Collect tidy/obj targets from the 'final' modules. |
| 256 | ctx.VisitAllModules(func(module android.Module) { |
Yu Liu | 88ea9ff | 2024-11-07 19:19:42 +0000 | [diff] [blame] | 257 | if ctx.IsFinalModule(module) { |
Chih-Hung Hsieh | 8078377 | 2021-10-11 16:46:56 -0700 | [diff] [blame] | 258 | collectTidyObjModuleTargets(ctx, module, tidyModulesInDirGroup, objModulesInDirGroup) |
| 259 | } |
| 260 | }) |
| 261 | |
| 262 | suffix := "" |
| 263 | if ctx.Config().KatiEnabled() { |
| 264 | suffix = "-soong" |
| 265 | } |
| 266 | generateObjTidyPhonyTargets(ctx, suffix, "obj", objModulesInDirGroup) |
| 267 | generateObjTidyPhonyTargets(ctx, suffix, "tidy", tidyModulesInDirGroup) |
| 268 | } |
| 269 | |
| 270 | // The name for an obj/tidy module variant group phony target is Name_group-obj/tidy, |
| 271 | func objTidyModuleGroupName(module android.Module, group string, suffix string) string { |
| 272 | if group == "" { |
| 273 | return module.Name() + "-" + suffix |
| 274 | } |
| 275 | return module.Name() + "_" + group + "-" + suffix |
| 276 | } |
| 277 | |
| 278 | // Generate obj-* or tidy-* phony targets. |
| 279 | func generateObjTidyPhonyTargets(ctx android.SingletonContext, suffix string, prefix string, objTidyModulesInDirGroup map[string]map[string]android.Paths) { |
| 280 | // For each variant group, create a <prefix>-<directory>_group target that |
| 281 | // depends on all subdirectories and modules in the directory. |
| 282 | for group, modulesInDir := range objTidyModulesInDirGroup { |
| 283 | groupSuffix := "" |
| 284 | if group != "" { |
| 285 | groupSuffix = "_" + group |
| 286 | } |
| 287 | mmTarget := func(dir string) string { |
| 288 | return prefix + "-" + strings.Replace(filepath.Clean(dir), "/", "-", -1) + groupSuffix |
| 289 | } |
| 290 | dirs, topDirs := android.AddAncestors(ctx, modulesInDir, mmTarget) |
| 291 | // Create a <prefix>-soong_group target that depends on all <prefix>-dir_group of top level dirs. |
| 292 | var topDirPaths android.Paths |
| 293 | for _, dir := range topDirs { |
| 294 | topDirPaths = append(topDirPaths, android.PathForPhony(ctx, mmTarget(dir))) |
| 295 | } |
| 296 | ctx.Phony(prefix+suffix+groupSuffix, topDirPaths...) |
| 297 | // Create a <prefix>-dir_group target that depends on all targets in modulesInDir[dir] |
| 298 | for _, dir := range dirs { |
| 299 | if dir != "." && dir != "" { |
| 300 | ctx.Phony(mmTarget(dir), modulesInDir[dir]...) |
| 301 | } |
| 302 | } |
| 303 | } |
| 304 | } |
| 305 | |
| 306 | // Append (obj|tidy)TargetGroups[group] into (obj|tidy)ModulesInDirGroups[group][moduleDir]. |
| 307 | func appendToModulesInDirGroup(targetGroups map[string]android.Path, moduleDir string, modulesInDirGroup map[string]map[string]android.Paths) { |
| 308 | for group, phonyPath := range targetGroups { |
| 309 | if _, found := modulesInDirGroup[group]; !found { |
| 310 | modulesInDirGroup[group] = make(map[string]android.Paths) |
| 311 | } |
| 312 | modulesInDirGroup[group][moduleDir] = append(modulesInDirGroup[group][moduleDir], phonyPath) |
| 313 | } |
| 314 | } |
| 315 | |
| 316 | // Add given files to the OS group and subset group. |
| 317 | func addToOSGroup(osName string, files android.Paths, allGroups, subsetGroups map[string]android.Paths) { |
| 318 | if len(files) > 0 { |
| 319 | subsetName := osName + "_subset" |
| 320 | allGroups[osName] = append(allGroups[osName], files...) |
| 321 | // Now include only the first variant in the subsetGroups. |
| 322 | // If clang and clang-tidy get faster, we might include more variants. |
| 323 | if _, found := subsetGroups[subsetName]; !found { |
| 324 | subsetGroups[subsetName] = files |
| 325 | } |
| 326 | } |
| 327 | } |
| 328 | |
| 329 | // Add an all-OS group, with groupName, to include all os-specific phony targets. |
| 330 | func addAllOSGroup(ctx android.SingletonContext, module android.Module, phonyTargetGroups map[string]android.Paths, groupName string, objTidyName string) { |
| 331 | if len(phonyTargetGroups) > 0 { |
| 332 | var targets android.Paths |
| 333 | for group, _ := range phonyTargetGroups { |
| 334 | targets = append(targets, android.PathForPhony(ctx, objTidyModuleGroupName(module, group, objTidyName))) |
| 335 | } |
| 336 | phonyTargetGroups[groupName] = targets |
| 337 | } |
| 338 | } |
| 339 | |
| 340 | // Create one phony targets for each group and add them to the targetGroups. |
| 341 | func genObjTidyPhonyTargets(ctx android.SingletonContext, module android.Module, objTidyName string, fileGroups map[string]android.Paths, targetGroups map[string]android.Path) { |
| 342 | for group, files := range fileGroups { |
| 343 | groupName := objTidyModuleGroupName(module, group, objTidyName) |
| 344 | ctx.Phony(groupName, files...) |
| 345 | targetGroups[group] = android.PathForPhony(ctx, groupName) |
| 346 | } |
| 347 | } |