diff options
author | Yu Liu <yudiliu@google.com> | 2022-07-23 21:22:26 -0700 |
---|---|---|
committer | Yu Liu <yudiliu@google.com> | 2022-08-10 10:05:06 -0700 |
commit | 973c8d5cf3929da69e5aa63d80d59bf2978949f7 (patch) | |
tree | c2105a445edfc54fc5b1f86b736a736d74a7f435 /rules/hidl | |
parent | fd1aa22da3c591052fc5193604f766c62f1d39d9 (diff) | |
download | bazel-973c8d5cf3929da69e5aa63d80d59bf2978949f7.tar.gz |
Support bp2build conversion of hidl_interface.
Bug: 232520859
Test: Manual tests
Change-Id: Ie2cfb5e3a247315f604a94a1c39293e6b1d40b1a
Diffstat (limited to 'rules/hidl')
-rw-r--r-- | rules/hidl/BUILD | 18 | ||||
-rw-r--r-- | rules/hidl/hidl_interface.bzl | 48 | ||||
-rw-r--r-- | rules/hidl/hidl_library.bzl | 79 | ||||
-rw-r--r-- | rules/hidl/hidl_library_test.bzl | 163 |
4 files changed, 308 insertions, 0 deletions
diff --git a/rules/hidl/BUILD b/rules/hidl/BUILD new file mode 100644 index 00000000..81213bcc --- /dev/null +++ b/rules/hidl/BUILD @@ -0,0 +1,18 @@ +"""Copyright (C) 2022 The Android Open Source Project + +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. +""" + +load(":hidl_library_test.bzl", "hidl_library_test_suite") + +hidl_library_test_suite(name = "hidl_library_tests") diff --git a/rules/hidl/hidl_interface.bzl b/rules/hidl/hidl_interface.bzl new file mode 100644 index 00000000..97daf017 --- /dev/null +++ b/rules/hidl/hidl_interface.bzl @@ -0,0 +1,48 @@ +""" +Copyright (C) 2022 The Android Open Source Project + +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. +""" + +load("//build/bazel/rules/cc:cc_hidl_library.bzl", "cc_hidl_library") +load("//build/bazel/rules/hidl:hidl_library.bzl", "hidl_library") + +INTERFACE_SUFFIX = "_interface" + +def hidl_interface( + name, + srcs = [], + deps = [], + root = "", + root_interface_file = "", + min_sdk_version = ""): + "Bazel macro to correspond with the hidl_interface Soong module." + + interface_name = name + INTERFACE_SUFFIX + interface_deps = [dep + INTERFACE_SUFFIX for dep in deps] + + hidl_library( + name = interface_name, + srcs = srcs, + deps = interface_deps, + fq_name = name, + root = root, + root_interface_file = root_interface_file, + ) + + cc_hidl_library( + name = name, + interface = interface_name, + dynamic_deps = deps, + min_sdk_version = min_sdk_version, + ) diff --git a/rules/hidl/hidl_library.bzl b/rules/hidl/hidl_library.bzl new file mode 100644 index 00000000..209a22b0 --- /dev/null +++ b/rules/hidl/hidl_library.bzl @@ -0,0 +1,79 @@ +""" +Copyright (C) 2022 The Android Open Source Project + +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. +""" + +load("@bazel_skylib//lib:paths.bzl", "paths") + +HidlInfo = provider(fields = [ + "srcs", + "transitive_srcs", + "transitive_roots", + "transitive_root_interface_files", + "fq_name", + "root", + "root_interface_file", +]) + +def _hidl_library_rule_impl(ctx): + transitive_srcs = [] + transitive_root_interface_files = [] + transitive_roots = [] + + for dep in ctx.attr.deps: + transitive_srcs.append(dep[HidlInfo].transitive_srcs) + transitive_root_interface_files.append(dep[HidlInfo].transitive_root_interface_files) + transitive_roots.append(dep[HidlInfo].transitive_roots) + + root_interface_path = ctx.file.root_interface_file.path + return [ + DefaultInfo(files = depset(ctx.files.srcs)), + HidlInfo( + srcs = depset(ctx.files.srcs), + transitive_srcs = depset( + direct = ctx.files.srcs, + transitive = transitive_srcs, + ), + # These transitive roots will be used as -r arguments later when calling + # hidl-gen, for example, -r android.hardware:hardware/interfaces + transitive_roots = depset( + direct = [ctx.attr.root + ":" + paths.dirname(root_interface_path)], + transitive = transitive_roots, + ), + transitive_root_interface_files = depset( + direct = [ctx.file.root_interface_file], + transitive = transitive_root_interface_files, + ), + fq_name = ctx.attr.fq_name, + root = ctx.attr.root, + root_interface_file = ctx.attr.root_interface_file, + ), + ] + +hidl_library = rule( + implementation = _hidl_library_rule_impl, + attrs = { + "srcs": attr.label_list( + allow_files = [".hal"], + ), + "deps": attr.label_list( + providers = [HidlInfo], + doc = "hidl_interface targets that this one depends on", + ), + "fq_name": attr.string(), + "root": attr.string(), + "root_interface_file": attr.label(allow_single_file = ["current.txt"]), + }, + provides = [HidlInfo], +) diff --git a/rules/hidl/hidl_library_test.bzl b/rules/hidl/hidl_library_test.bzl new file mode 100644 index 00000000..5d2db53d --- /dev/null +++ b/rules/hidl/hidl_library_test.bzl @@ -0,0 +1,163 @@ +"""Copyright (C) 2022 The Android Open Source Project + +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. +""" + +load("@bazel_skylib//lib:paths.bzl", "paths") +load("@bazel_skylib//lib:unittest.bzl", "analysistest", "asserts") +load(":hidl_library.bzl", "HidlInfo", "hidl_library") + +SRC_NAME = "src.hal" +DEP1_NAME = "dep1.hal" +DEP2_NAME = "dep2.hal" +DEP3_NAME = "dep3.hal" +ROOT = "android.hardware" +ROOT_INTERFACE_FILE_LABEL = "//hardware/interfaces:current.txt" +ROOT_INTERFACE_FILE = "hardware/interfaces/current.txt" +ROOT_ARGUMENT = "android.hardware:hardware/interfaces" +ROOT1 = "android.system" +ROOT1_INTERFACE_FILE_LABEL = "//system/hardware/interfaces:current.txt" +ROOT1_INTERFACE_FILE = "system/hardware/interfaces/current.txt" +ROOT1_ARGUMENT = "android.system:system/hardware/interfaces" +ROOT2 = "android.hidl" +ROOT2_INTERFACE_FILE_LABEL = "//system/libhidl/transport:current.txt" +ROOT2_INTERFACE_FILE = "system/libhidl/transport/current.txt" +ROOT2_ARGUMENT = "android.hidl:system/libhidl/transport" + +def _hidl_info_simple_test_impl(ctx): + env = analysistest.begin(ctx) + target_under_test = analysistest.target_under_test(env) + package_root = paths.dirname(ctx.build_file_path) + + asserts.equals( + env, + expected = [ + paths.join(package_root, "src.hal"), + ], + actual = [ + file.short_path + for file in target_under_test[HidlInfo].srcs.to_list() + ], + ) + + asserts.equals( + env, + expected = sorted([ + paths.join(package_root, DEP1_NAME), + paths.join(package_root, DEP3_NAME), + paths.join(package_root, DEP2_NAME), + paths.join(package_root, SRC_NAME), + ]), + actual = sorted([ + file.short_path + for file in target_under_test[HidlInfo].transitive_srcs.to_list() + ]), + ) + + asserts.equals( + env, + expected = [ + ROOT, + Label(ROOT_INTERFACE_FILE_LABEL), + ], + actual = [ + target_under_test[HidlInfo].root, + target_under_test[HidlInfo].root_interface_file.label, + ], + ) + + asserts.equals( + env, + expected = sorted([ + ROOT1_ARGUMENT, + ROOT2_ARGUMENT, + ROOT_ARGUMENT, + ]), + actual = sorted(target_under_test[HidlInfo].transitive_roots.to_list()), + ) + + asserts.equals( + env, + expected = sorted([ + ROOT1_INTERFACE_FILE, + ROOT2_INTERFACE_FILE, + ROOT_INTERFACE_FILE, + ]), + actual = sorted([ + file.short_path + for file in target_under_test[HidlInfo].transitive_root_interface_files.to_list() + ]), + ) + + return analysistest.end(env) + +hidl_info_simple_test = analysistest.make( + _hidl_info_simple_test_impl, +) + +def _test_hidl_info_simple(): + test_base_name = "hidl_info_simple" + test_name = test_base_name + "_test" + dep1 = test_base_name + "_dep1" + dep2 = test_base_name + "_dep2" + dep3 = test_base_name + "_dep3" + + hidl_library( + name = test_base_name, + srcs = [SRC_NAME], + deps = [ + ":" + dep1, + ":" + dep2, + ], + root = ROOT, + root_interface_file = ROOT_INTERFACE_FILE_LABEL, + tags = ["manual"], + ) + hidl_library( + name = dep1, + srcs = [DEP1_NAME], + root = ROOT1, + root_interface_file = ROOT1_INTERFACE_FILE_LABEL, + tags = ["manual"], + ) + hidl_library( + name = dep2, + srcs = [DEP2_NAME], + deps = [ + ":" + dep3, + ], + root = ROOT2, + root_interface_file = ROOT2_INTERFACE_FILE_LABEL, + tags = ["manual"], + ) + hidl_library( + name = dep3, + srcs = [DEP3_NAME], + root = ROOT2, + root_interface_file = ROOT2_INTERFACE_FILE_LABEL, + tags = ["manual"], + ) + hidl_info_simple_test( + name = test_name, + target_under_test = test_base_name, + ) + + return test_name + +def hidl_library_test_suite(name): + native.test_suite( + name = name, + tests = [ + _test_hidl_info_simple(), + ], + ) |