tidy.go 14 KB

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