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