check.go 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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. // This file contains utility functions to check for bad or illegal cflags
  16. // specified by a module
  17. import (
  18. "path/filepath"
  19. "strings"
  20. "android/soong/cc/config"
  21. )
  22. // Check for invalid c/conly/cpp/asflags and suggest alternatives. Only use this
  23. // for flags explicitly passed by the user, since these flags may be used internally.
  24. func CheckBadCompilerFlags(ctx BaseModuleContext, prop string, flags []string) {
  25. for _, flag := range flags {
  26. flag = strings.TrimSpace(flag)
  27. if !strings.HasPrefix(flag, "-") {
  28. ctx.PropertyErrorf(prop, "Flag `%s` must start with `-`", flag)
  29. } else if strings.HasPrefix(flag, "-I") || strings.HasPrefix(flag, "-isystem") {
  30. ctx.PropertyErrorf(prop, "Bad flag `%s`, use local_include_dirs or include_dirs instead", flag)
  31. } else if inList(flag, config.IllegalFlags) {
  32. ctx.PropertyErrorf(prop, "Illegal flag `%s`", flag)
  33. } else if flag == "--coverage" {
  34. ctx.PropertyErrorf(prop, "Bad flag: `%s`, use native_coverage instead", flag)
  35. } else if flag == "-fwhole-program-vtables" {
  36. ctx.PropertyErrorf(prop, "Bad flag: `%s`, use whole_program_vtables instead", flag)
  37. } else if flag == "-Weverything" {
  38. if !ctx.Config().IsEnvTrue("ANDROID_TEMPORARILY_ALLOW_WEVERYTHING") {
  39. ctx.PropertyErrorf(prop, "-Weverything is not allowed in Android.bp files. "+
  40. "Build with `m ANDROID_TEMPORARILY_ALLOW_WEVERYTHING=true` to experiment locally with -Weverything.")
  41. }
  42. } else if strings.Contains(flag, " ") {
  43. args := strings.Split(flag, " ")
  44. if args[0] == "-include" {
  45. if len(args) > 2 {
  46. ctx.PropertyErrorf(prop, "`-include` only takes one argument: `%s`", flag)
  47. }
  48. path := filepath.Clean(args[1])
  49. if strings.HasPrefix("/", path) {
  50. ctx.PropertyErrorf(prop, "Path must not be an absolute path: %s", flag)
  51. } else if strings.HasPrefix("../", path) {
  52. ctx.PropertyErrorf(prop, "Path must not start with `../`: `%s`. Use include_dirs to -include from a different directory", flag)
  53. }
  54. } else if args[0] == "-mllvm" {
  55. if len(args) > 2 {
  56. ctx.PropertyErrorf(prop, "`-mllvm` only takes one argument: `%s`", flag)
  57. }
  58. } else if strings.HasPrefix(flag, "-D") && strings.Contains(flag, "=") {
  59. // Do nothing in this case.
  60. // For now, we allow space characters in -DNAME=def form to allow use cases
  61. // like -DNAME="value with string". Later, this check should be done more
  62. // correctly to prevent multi flag cases like -DNAME=value -O2.
  63. } else {
  64. ctx.PropertyErrorf(prop, "Bad flag: `%s` is not an allowed multi-word flag. Should it be split into multiple flags?", flag)
  65. }
  66. }
  67. }
  68. }
  69. // Check for bad ldflags and suggest alternatives. Only use this for flags
  70. // explicitly passed by the user, since these flags may be used internally.
  71. func CheckBadLinkerFlags(ctx BaseModuleContext, prop string, flags []string) {
  72. for _, flag := range flags {
  73. flag = strings.TrimSpace(flag)
  74. if !strings.HasPrefix(flag, "-") {
  75. ctx.PropertyErrorf(prop, "Flag `%s` must start with `-`", flag)
  76. } else if strings.HasPrefix(flag, "-l") {
  77. if ctx.Host() {
  78. ctx.PropertyErrorf(prop, "Bad flag: `%s`, use shared_libs or host_ldlibs instead", flag)
  79. } else {
  80. ctx.PropertyErrorf(prop, "Bad flag: `%s`, use shared_libs instead", flag)
  81. }
  82. } else if strings.HasPrefix(flag, "-L") {
  83. ctx.PropertyErrorf(prop, "Bad flag: `%s` is not allowed", flag)
  84. } else if strings.HasPrefix(flag, "-Wl,--version-script") {
  85. ctx.PropertyErrorf(prop, "Bad flag: `%s`, use version_script instead", flag)
  86. } else if flag == "-T" || strings.HasPrefix(flag, "--script") {
  87. ctx.PropertyErrorf(prop, "Bad flag: `%s`, use linker_scripts instead", flag)
  88. } else if flag == "--coverage" {
  89. ctx.PropertyErrorf(prop, "Bad flag: `%s`, use native_coverage instead", flag)
  90. } else if strings.Contains(flag, " ") {
  91. args := strings.Split(flag, " ")
  92. if args[0] == "-z" {
  93. if len(args) > 2 {
  94. ctx.PropertyErrorf(prop, "`-z` only takes one argument: `%s`", flag)
  95. }
  96. } else {
  97. ctx.PropertyErrorf(prop, "Bad flag: `%s` is not an allowed multi-word flag. Should it be split into multiple flags?", flag)
  98. }
  99. }
  100. }
  101. }
  102. // Check for bad host_ldlibs
  103. func CheckBadHostLdlibs(ctx ModuleContext, prop string, flags []string) {
  104. allowedLdlibs := ctx.toolchain().AvailableLibraries()
  105. if !ctx.Host() {
  106. panic("Invalid call to CheckBadHostLdlibs")
  107. }
  108. for _, flag := range flags {
  109. flag = strings.TrimSpace(flag)
  110. // TODO: Probably should just redo this property to prefix -l in Soong
  111. if !strings.HasPrefix(flag, "-l") && !strings.HasPrefix(flag, "-framework") {
  112. ctx.PropertyErrorf(prop, "Invalid flag: `%s`, must start with `-l` or `-framework`", flag)
  113. } else if !inList(flag, allowedLdlibs) {
  114. ctx.PropertyErrorf(prop, "Host library `%s` not available", flag)
  115. }
  116. }
  117. }
  118. // Check for bad clang tidy flags
  119. func CheckBadTidyFlags(ctx ModuleContext, prop string, flags []string) {
  120. for _, flag := range flags {
  121. flag = strings.TrimSpace(flag)
  122. if !strings.HasPrefix(flag, "-") {
  123. ctx.PropertyErrorf(prop, "Flag `%s` must start with `-`", flag)
  124. } else if strings.HasPrefix(flag, "-fix") {
  125. ctx.PropertyErrorf(prop, "Flag `%s` is not allowed, since it could cause multiple writes to the same source file", flag)
  126. } else if strings.HasPrefix(flag, "-checks=") {
  127. ctx.PropertyErrorf(prop, "Flag `%s` is not allowed, use `tidy_checks` property instead", flag)
  128. } else if strings.Contains(flag, " ") {
  129. ctx.PropertyErrorf(prop, "Bad flag: `%s` is not an allowed multi-word flag. Should it be split into multiple flags?", flag)
  130. }
  131. }
  132. }
  133. // Check for bad clang tidy checks
  134. func CheckBadTidyChecks(ctx ModuleContext, prop string, checks []string) {
  135. for _, check := range checks {
  136. if strings.Contains(check, " ") {
  137. ctx.PropertyErrorf("tidy_checks", "Check `%s` invalid, cannot contain spaces", check)
  138. } else if strings.Contains(check, ",") {
  139. ctx.PropertyErrorf("tidy_checks", "Check `%s` invalid, cannot contain commas. Split each entry into it's own string instead", check)
  140. }
  141. }
  142. }