tables.go 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. /*
  2. Copyright 2016 Google Inc. All Rights Reserved.
  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. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. // Tables about what Buildifier can and cannot edit.
  14. // Perhaps eventually this will be
  15. // derived from the BUILD encyclopedia.
  16. package tables
  17. // IsLabelArg: a named argument to a rule call is considered to have a value
  18. // that can be treated as a label or list of labels if the name
  19. // is one of these names. There is a separate blacklist for
  20. // rule-specific exceptions.
  21. var IsLabelArg = map[string]bool{
  22. "app_target": true,
  23. "appdir": true,
  24. "base_package": true,
  25. "build_deps": true,
  26. "cc_deps": true,
  27. "ccdeps": true,
  28. "common_deps": true,
  29. "compile_deps": true,
  30. "compiler": true,
  31. "data": true,
  32. "default_visibility": true,
  33. "dep": true,
  34. "deps": true,
  35. "deps_java": true,
  36. "dont_depend_on": true,
  37. "env_deps": true,
  38. "envscripts": true,
  39. "exported_deps": true,
  40. "exports": true,
  41. "externs_list": true,
  42. "files": true,
  43. "globals": true,
  44. "implementation": true,
  45. "implements": true,
  46. "includes": true,
  47. "interface": true,
  48. "jar": true,
  49. "jars": true,
  50. "javadeps": true,
  51. "lib_deps": true,
  52. "library": true,
  53. "malloc": true,
  54. "model": true,
  55. "mods": true,
  56. "module_deps": true,
  57. "module_target": true,
  58. "of": true,
  59. "plugins": true,
  60. "proto_deps": true,
  61. "proto_target": true,
  62. "protos": true,
  63. "resource": true,
  64. "resources": true,
  65. "runtime_deps": true,
  66. "scope": true,
  67. "shared_deps": true,
  68. "similar_deps": true,
  69. "source_jar": true,
  70. "src": true,
  71. "srcs": true,
  72. "stripped_targets": true,
  73. "suites": true,
  74. "swigdeps": true,
  75. "target": true,
  76. "target_devices": true,
  77. "target_platforms": true,
  78. "template": true,
  79. "test": true,
  80. "tests": true,
  81. "tests_deps": true,
  82. "tool": true,
  83. "tools": true,
  84. "visibility": true,
  85. }
  86. // LabelBlacklist is the list of call arguments that cannot be
  87. // shortened, because they are not interpreted using the same
  88. // rules as for other labels.
  89. var LabelBlacklist = map[string]bool{
  90. // Shortening this can cause visibility checks to fail.
  91. "package_group.includes": true,
  92. }
  93. // By default, edit.types.IsList consults lang.TypeOf to determine if an arg is a list.
  94. // You may override this using IsListArg. Specifying a name here overrides any value
  95. // in lang.TypeOf.
  96. var IsListArg = map[string]bool{}
  97. // IsSortableListArg: a named argument to a rule call is considered to be a sortable list
  98. // if the name is one of these names. There is a separate blacklist for
  99. // rule-specific exceptions.
  100. var IsSortableListArg = map[string]bool{
  101. "cc_deps": true,
  102. "common_deps": true,
  103. "compile_deps": true,
  104. "configs": true,
  105. "constraints": true,
  106. "data": true,
  107. "default_visibility": true,
  108. "deps": true,
  109. "deps_java": true,
  110. "exported_deps": true,
  111. "exports": true,
  112. "filegroups": true,
  113. "files": true,
  114. "hdrs": true,
  115. "imports": true,
  116. "includes": true,
  117. "inherits": true,
  118. "javadeps": true,
  119. "lib_deps": true,
  120. "module_deps": true,
  121. "out": true,
  122. "outs": true,
  123. "packages": true,
  124. "plugin_modules": true,
  125. "proto_deps": true,
  126. "protos": true,
  127. "pubs": true,
  128. "resources": true,
  129. "runtime_deps": true,
  130. "shared_deps": true,
  131. "similar_deps": true,
  132. "srcs": true,
  133. "swigdeps": true,
  134. "swig_includes": true,
  135. "tags": true,
  136. "tests": true,
  137. "tools": true,
  138. "to_start_extensions": true,
  139. "visibility": true,
  140. }
  141. // SortableBlacklist records specific rule arguments that must not be reordered.
  142. var SortableBlacklist = map[string]bool{
  143. "genrule.outs": true,
  144. "genrule.srcs": true,
  145. }
  146. // SortableWhitelist records specific rule arguments that are guaranteed
  147. // to be reorderable, because bazel re-sorts the list itself after reading the BUILD file.
  148. var SortableWhitelist = map[string]bool{
  149. "cc_inc_library.hdrs": true,
  150. "cc_library.hdrs": true,
  151. "java_library.srcs": true,
  152. "java_library.resources": true,
  153. "java_binary.srcs": true,
  154. "java_binary.resources": true,
  155. "java_test.srcs": true,
  156. "java_test.resources": true,
  157. "java_library.constraints": true,
  158. "java_import.constraints": true,
  159. }
  160. // NamePriority maps an argument name to its sorting priority.
  161. //
  162. // NOTE(bazel-team): These are the old buildifier rules. It is likely that this table
  163. // will change, perhaps swapping in a separate table for each call,
  164. // derived from the order used in the Build Encyclopedia.
  165. var NamePriority = map[string]int{
  166. "name": -99,
  167. "gwt_name": -98,
  168. "package_name": -97,
  169. "visible_node_name": -96, // for boq_initial_css_modules and boq_jswire_test_suite
  170. "size": -95,
  171. "timeout": -94,
  172. "testonly": -93,
  173. "src": -92,
  174. "srcdir": -91,
  175. "srcs": -90,
  176. "out": -89,
  177. "outs": -88,
  178. "hdrs": -87,
  179. "has_services": -86, // before api versions, for proto
  180. "include": -85, // before exclude, for glob
  181. "of": -84, // for check_dependencies
  182. "baseline": -83, // for searchbox_library
  183. // All others sort here, at 0.
  184. "destdir": 1,
  185. "exports": 2,
  186. "runtime_deps": 3,
  187. "deps": 4,
  188. "implementation": 5,
  189. "implements": 6,
  190. "alwayslink": 7,
  191. // default condition in a dictionary literal passed to select should be
  192. // the last one by convention.
  193. "//conditions:default": 50,
  194. }
  195. var StripLabelLeadingSlashes = false
  196. var ShortenAbsoluteLabelsToRelative = false
  197. // AndroidNativeRules lists all Android rules that are being migrated from Native to Starlark.
  198. var AndroidNativeRules = []string{
  199. "aar_import",
  200. "android_binary",
  201. "android_device",
  202. "android_instrumentation_test",
  203. "android_library",
  204. "android_local_test",
  205. "android_ndk_respository",
  206. "android_sdk_repository",
  207. }
  208. // AndroidLoadPath is the load path for the Starlark Android Rules.
  209. var AndroidLoadPath = "@rules_android//android:rules.bzl"
  210. // CcNativeRules lists all C++ rules that are being migrated from Native to Starlark.
  211. var CcNativeRules = []string{
  212. "cc_binary",
  213. "cc_test",
  214. "cc_library",
  215. "cc_import",
  216. "cc_proto_library",
  217. "fdo_prefetch_hints",
  218. "fdo_profile",
  219. "cc_toolchain",
  220. "cc_toolchain_suite",
  221. "objc_library",
  222. "objc_import",
  223. }
  224. // CcLoadPath is the load path for the Starlark C++ Rules.
  225. var CcLoadPath = "@rules_cc//cc:defs.bzl"
  226. // JavaNativeRules lists all Java rules that are being migrated from Native to Starlark.
  227. var JavaNativeRules = []string{
  228. "java_binary",
  229. "java_import",
  230. "java_library",
  231. "java_lite_proto_library",
  232. "java_proto_library",
  233. "java_test",
  234. "java_package_configuration",
  235. "java_plugin",
  236. "java_runtime",
  237. "java_toolchain",
  238. }
  239. // JavaLoadPath is the load path for the Starlark Java Rules.
  240. var JavaLoadPath = "@rules_java//java:defs.bzl"
  241. // PyNativeRules lists all Python rules that are being migrated from Native to Starlark.
  242. var PyNativeRules = []string{
  243. "py_library",
  244. "py_binary",
  245. "py_test",
  246. "py_runtime",
  247. }
  248. // PyLoadPath is the load path for the Starlark Python Rules.
  249. var PyLoadPath = "@rules_python//python:defs.bzl"
  250. // ProtoNativeRules lists all Proto rules that are being migrated from Native to Starlark.
  251. var ProtoNativeRules = []string{
  252. "proto_lang_toolchain",
  253. "proto_library",
  254. }
  255. // ProtoNativeSymbols lists all Proto symbols that are being migrated from Native to Starlark.
  256. var ProtoNativeSymbols = []string{
  257. "ProtoInfo",
  258. "proto_common",
  259. }
  260. // ProtoLoadPath is the load path for the Starlark Proto Rules.
  261. var ProtoLoadPath = "@rules_proto//proto:defs.bzl"
  262. // OverrideTables allows a user of the build package to override the special-case rules. The user-provided tables replace the built-in tables.
  263. func OverrideTables(labelArg, blacklist, listArg, sortableListArg, sortBlacklist, sortWhitelist map[string]bool, namePriority map[string]int, stripLabelLeadingSlashes, shortenAbsoluteLabelsToRelative bool) {
  264. IsLabelArg = labelArg
  265. LabelBlacklist = blacklist
  266. IsListArg = listArg
  267. IsSortableListArg = sortableListArg
  268. SortableBlacklist = sortBlacklist
  269. SortableWhitelist = sortWhitelist
  270. NamePriority = namePriority
  271. StripLabelLeadingSlashes = stripLabelLeadingSlashes
  272. ShortenAbsoluteLabelsToRelative = shortenAbsoluteLabelsToRelative
  273. }
  274. // MergeTables allows a user of the build package to override the special-case rules. The user-provided tables are merged into the built-in tables.
  275. func MergeTables(labelArg, blacklist, listArg, sortableListArg, sortBlacklist, sortWhitelist map[string]bool, namePriority map[string]int, stripLabelLeadingSlashes, shortenAbsoluteLabelsToRelative bool) {
  276. for k, v := range labelArg {
  277. IsLabelArg[k] = v
  278. }
  279. for k, v := range blacklist {
  280. LabelBlacklist[k] = v
  281. }
  282. for k, v := range listArg {
  283. IsListArg[k] = v
  284. }
  285. for k, v := range sortableListArg {
  286. IsSortableListArg[k] = v
  287. }
  288. for k, v := range sortBlacklist {
  289. SortableBlacklist[k] = v
  290. }
  291. for k, v := range sortWhitelist {
  292. SortableWhitelist[k] = v
  293. }
  294. for k, v := range namePriority {
  295. NamePriority[k] = v
  296. }
  297. StripLabelLeadingSlashes = stripLabelLeadingSlashes || StripLabelLeadingSlashes
  298. ShortenAbsoluteLabelsToRelative = shortenAbsoluteLabelsToRelative || ShortenAbsoluteLabelsToRelative
  299. }