androidbp_to_build_templates.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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. package bp2build
  15. const (
  16. // The default `load` preamble for every generated queryview BUILD file.
  17. soongModuleLoad = `package(default_visibility = ["//visibility:public"])
  18. load("//build/bazel/queryview_rules:soong_module.bzl", "soong_module")
  19. `
  20. // A macro call in the BUILD file representing a Soong module, with space
  21. // for expanding more attributes.
  22. soongModuleTargetTemplate = `soong_module(
  23. name = "%s",
  24. soong_module_name = "%s",
  25. soong_module_type = "%s",
  26. soong_module_variant = "%s",
  27. soong_module_deps = %s,
  28. %s)`
  29. ruleTargetTemplate = `%s(
  30. name = "%s",
  31. %s)`
  32. unnamedRuleTargetTemplate = `%s(
  33. %s)`
  34. // A simple provider to mark and differentiate Soong module rule shims from
  35. // regular Bazel rules. Every Soong module rule shim returns a
  36. // SoongModuleInfo provider, and can only depend on rules returning
  37. // SoongModuleInfo in the `soong_module_deps` attribute.
  38. providersBzl = `SoongModuleInfo = provider(
  39. fields = {
  40. "name": "Name of module",
  41. "type": "Type of module",
  42. "variant": "Variant of module",
  43. },
  44. )
  45. `
  46. // The soong_module rule implementation in a .bzl file.
  47. soongModuleBzl = `
  48. %s
  49. load("//build/bazel/queryview_rules:providers.bzl", "SoongModuleInfo")
  50. def _generic_soong_module_impl(ctx):
  51. return [
  52. SoongModuleInfo(
  53. name = ctx.attr.soong_module_name,
  54. type = ctx.attr.soong_module_type,
  55. variant = ctx.attr.soong_module_variant,
  56. ),
  57. ]
  58. generic_soong_module = rule(
  59. implementation = _generic_soong_module_impl,
  60. attrs = {
  61. "soong_module_name": attr.string(mandatory = True),
  62. "soong_module_type": attr.string(mandatory = True),
  63. "soong_module_variant": attr.string(),
  64. "soong_module_deps": attr.label_list(providers = [SoongModuleInfo]),
  65. },
  66. )
  67. soong_module_rule_map = {
  68. %s}
  69. _SUPPORTED_TYPES = ["bool", "int", "string"]
  70. def _is_supported_type(value):
  71. if type(value) in _SUPPORTED_TYPES:
  72. return True
  73. elif type(value) == "list":
  74. supported = True
  75. for v in value:
  76. supported = supported and type(v) in _SUPPORTED_TYPES
  77. return supported
  78. else:
  79. return False
  80. # soong_module is a macro that supports arbitrary kwargs, and uses soong_module_type to
  81. # expand to the right underlying shim.
  82. def soong_module(name, soong_module_type, **kwargs):
  83. soong_module_rule = soong_module_rule_map.get(soong_module_type)
  84. if soong_module_rule == None:
  85. # This module type does not have an existing rule to map to, so use the
  86. # generic_soong_module rule instead.
  87. generic_soong_module(
  88. name = name,
  89. soong_module_type = soong_module_type,
  90. soong_module_name = kwargs.pop("soong_module_name", ""),
  91. soong_module_variant = kwargs.pop("soong_module_variant", ""),
  92. soong_module_deps = kwargs.pop("soong_module_deps", []),
  93. )
  94. else:
  95. supported_kwargs = dict()
  96. for key, value in kwargs.items():
  97. if _is_supported_type(value):
  98. supported_kwargs[key] = value
  99. soong_module_rule(
  100. name = name,
  101. **supported_kwargs,
  102. )
  103. `
  104. // A rule shim for representing a Soong module type and its properties.
  105. moduleRuleShim = `
  106. def _%[1]s_impl(ctx):
  107. return [SoongModuleInfo()]
  108. %[1]s = rule(
  109. implementation = _%[1]s_impl,
  110. attrs = %[2]s
  111. )
  112. `
  113. )