From b24e970c74bc2ba1dc421243428a2232d9d2dca6 Mon Sep 17 00:00:00 2001 From: Jingwen Chen Date: Wed, 18 Nov 2020 08:53:38 -0500 Subject: [PATCH] Refactor queryview templates into a different file. Make queryview.go smaller. Test: m queryview && bazel query --config=queryview //... Change-Id: I3d3ba688eca6f05d33d1875076e3076ca1cac6ee --- cmd/soong_build/Android.bp | 1 + cmd/soong_build/queryview.go | 109 ---------------------- cmd/soong_build/queryview_templates.go | 124 +++++++++++++++++++++++++ 3 files changed, 125 insertions(+), 109 deletions(-) create mode 100644 cmd/soong_build/queryview_templates.go diff --git a/cmd/soong_build/Android.bp b/cmd/soong_build/Android.bp index 680c00a9b..441ea0d54 100644 --- a/cmd/soong_build/Android.bp +++ b/cmd/soong_build/Android.bp @@ -27,6 +27,7 @@ bootstrap_go_binary { "main.go", "writedocs.go", "queryview.go", + "queryview_templates.go", ], testSrcs: [ "queryview_test.go", diff --git a/cmd/soong_build/queryview.go b/cmd/soong_build/queryview.go index 27856b53a..f5aa685d5 100644 --- a/cmd/soong_build/queryview.go +++ b/cmd/soong_build/queryview.go @@ -28,115 +28,6 @@ import ( "github.com/google/blueprint/proptools" ) -const ( - // The default `load` preamble for every generated BUILD file. - soongModuleLoad = `package(default_visibility = ["//visibility:public"]) -load("//build/bazel/queryview_rules:soong_module.bzl", "soong_module") - -` - - // A macro call in the BUILD file representing a Soong module, with space - // for expanding more attributes. - soongModuleTarget = `soong_module( - name = "%s", - module_name = "%s", - module_type = "%s", - module_variant = "%s", - module_deps = %s, -%s)` - - // A simple provider to mark and differentiate Soong module rule shims from - // regular Bazel rules. Every Soong module rule shim returns a - // SoongModuleInfo provider, and can only depend on rules returning - // SoongModuleInfo in the `module_deps` attribute. - providersBzl = `SoongModuleInfo = provider( - fields = { - "name": "Name of module", - "type": "Type of module", - "variant": "Variant of module", - }, -) -` - - // The soong_module rule implementation in a .bzl file. - soongModuleBzl = ` -%s - -load("//build/bazel/queryview_rules:providers.bzl", "SoongModuleInfo") - -def _generic_soong_module_impl(ctx): - return [ - SoongModuleInfo( - name = ctx.attr.module_name, - type = ctx.attr.module_type, - variant = ctx.attr.module_variant, - ), - ] - -generic_soong_module = rule( - implementation = _generic_soong_module_impl, - attrs = { - "module_name": attr.string(mandatory = True), - "module_type": attr.string(mandatory = True), - "module_variant": attr.string(), - "module_deps": attr.label_list(providers = [SoongModuleInfo]), - }, -) - -soong_module_rule_map = { -%s} - -_SUPPORTED_TYPES = ["bool", "int", "string"] - -def _is_supported_type(value): - if type(value) in _SUPPORTED_TYPES: - return True - elif type(value) == "list": - supported = True - for v in value: - supported = supported and type(v) in _SUPPORTED_TYPES - return supported - else: - return False - -# soong_module is a macro that supports arbitrary kwargs, and uses module_type to -# expand to the right underlying shim. -def soong_module(name, module_type, **kwargs): - soong_module_rule = soong_module_rule_map.get(module_type) - - if soong_module_rule == None: - # This module type does not have an existing rule to map to, so use the - # generic_soong_module rule instead. - generic_soong_module( - name = name, - module_type = module_type, - module_name = kwargs.pop("module_name", ""), - module_variant = kwargs.pop("module_variant", ""), - module_deps = kwargs.pop("module_deps", []), - ) - else: - supported_kwargs = dict() - for key, value in kwargs.items(): - if _is_supported_type(value): - supported_kwargs[key] = value - soong_module_rule( - name = name, - **supported_kwargs, - ) -` - - // A rule shim for representing a Soong module type and its properties. - moduleRuleShim = ` -def _%[1]s_impl(ctx): - return [SoongModuleInfo()] - -%[1]s = rule( - implementation = _%[1]s_impl, - attrs = %[2]s -) -` -) - var ( // An allowlist of prop types that are surfaced from module props to rule // attributes. (nested) dictionaries are notably absent here, because while diff --git a/cmd/soong_build/queryview_templates.go b/cmd/soong_build/queryview_templates.go new file mode 100644 index 000000000..359c0d824 --- /dev/null +++ b/cmd/soong_build/queryview_templates.go @@ -0,0 +1,124 @@ +// Copyright 2020 Google Inc. All rights reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package main + +const ( + // The default `load` preamble for every generated BUILD file. + soongModuleLoad = `package(default_visibility = ["//visibility:public"]) +load("//build/bazel/queryview_rules:soong_module.bzl", "soong_module") + +` + + // A macro call in the BUILD file representing a Soong module, with space + // for expanding more attributes. + soongModuleTarget = `soong_module( + name = "%s", + module_name = "%s", + module_type = "%s", + module_variant = "%s", + module_deps = %s, +%s)` + + // A simple provider to mark and differentiate Soong module rule shims from + // regular Bazel rules. Every Soong module rule shim returns a + // SoongModuleInfo provider, and can only depend on rules returning + // SoongModuleInfo in the `module_deps` attribute. + providersBzl = `SoongModuleInfo = provider( + fields = { + "name": "Name of module", + "type": "Type of module", + "variant": "Variant of module", + }, +) +` + + // The soong_module rule implementation in a .bzl file. + soongModuleBzl = ` +%s + +load("//build/bazel/queryview_rules:providers.bzl", "SoongModuleInfo") + +def _generic_soong_module_impl(ctx): + return [ + SoongModuleInfo( + name = ctx.attr.module_name, + type = ctx.attr.module_type, + variant = ctx.attr.module_variant, + ), + ] + +generic_soong_module = rule( + implementation = _generic_soong_module_impl, + attrs = { + "module_name": attr.string(mandatory = True), + "module_type": attr.string(mandatory = True), + "module_variant": attr.string(), + "module_deps": attr.label_list(providers = [SoongModuleInfo]), + }, +) + +soong_module_rule_map = { +%s} + +_SUPPORTED_TYPES = ["bool", "int", "string"] + +def _is_supported_type(value): + if type(value) in _SUPPORTED_TYPES: + return True + elif type(value) == "list": + supported = True + for v in value: + supported = supported and type(v) in _SUPPORTED_TYPES + return supported + else: + return False + +# soong_module is a macro that supports arbitrary kwargs, and uses module_type to +# expand to the right underlying shim. +def soong_module(name, module_type, **kwargs): + soong_module_rule = soong_module_rule_map.get(module_type) + + if soong_module_rule == None: + # This module type does not have an existing rule to map to, so use the + # generic_soong_module rule instead. + generic_soong_module( + name = name, + module_type = module_type, + module_name = kwargs.pop("module_name", ""), + module_variant = kwargs.pop("module_variant", ""), + module_deps = kwargs.pop("module_deps", []), + ) + else: + supported_kwargs = dict() + for key, value in kwargs.items(): + if _is_supported_type(value): + supported_kwargs[key] = value + soong_module_rule( + name = name, + **supported_kwargs, + ) +` + + // A rule shim for representing a Soong module type and its properties. + moduleRuleShim = ` +def _%[1]s_impl(ctx): + return [SoongModuleInfo()] + +%[1]s = rule( + implementation = _%[1]s_impl, + attrs = %[2]s +) +` +)