• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1"""
2Copyright (C) 2021 The Android Open Source Project
3
4Licensed under the Apache License, Version 2.0 (the "License");
5you may not use this file except in compliance with the License.
6You may obtain a copy of the License at
7
8    http://www.apache.org/licenses/LICENSE-2.0
9
10Unless required by applicable law or agreed to in writing, software
11distributed under the License is distributed on an "AS IS" BASIS,
12WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13See the License for the specific language governing permissions and
14limitations under the License.
15"""
16
17# A repository rule to run soong_ui --make-mode to provide the Bazel standalone
18# build with prebuilts from Make/Soong that Bazel can't build yet.
19def _impl(rctx):
20    target_product = rctx.os.environ.get("TARGET_PRODUCT", "aosp_arm")
21    target_build_variant = rctx.os.environ.get("TARGET_BUILD_VARIANT", "eng")
22
23    binaries = rctx.attr.binaries
24    target_modules = rctx.attr.target_module_files
25
26    build_dir = rctx.path(Label("//:WORKSPACE")).dirname
27    soong_ui_bash = str(build_dir) + "/build/soong/soong_ui.bash"
28    args = [
29        soong_ui_bash,
30        "--make-mode",
31        "--skip-soong-tests",
32    ]
33    all_modules = target_modules.keys() + binaries
34    args += all_modules
35
36    rctx.report_progress("Building modules with Soong: %s" % str(all_modules))
37    out_dir = str(build_dir.dirname) + "/make_injection"
38    exec_result = rctx.execute(
39        args,
40        environment = {
41            "OUT_DIR": out_dir,
42            "TARGET_PRODUCT": target_product,
43            "TARGET_BUILD_VARIANT": target_build_variant,
44            "TOP": str(build_dir.dirname.dirname.dirname),
45        },
46        quiet = False,  # stream stdout so it shows progress
47    )
48    if exec_result.return_code != 0:
49        fail(exec_result.stderr)
50
51    # Get the explicit list of host binary paths to be exported
52    rctx.symlink(out_dir + "/host/linux-x86", "host/linux-x86")
53    binary_path_prefix = "host/linux-x86/bin"
54    binary_paths = ['"%s/%s"' % (binary_path_prefix, binary) for binary in binaries]
55
56    # Get the explicit list of target installed files to be exported
57    rctx.symlink(out_dir + "/target", "target")
58    target_path_prefix = "target/product/generic"
59    target_paths = []
60    for paths in target_modules.values():
61        target_paths.extend(['"%s/%s"' % (target_path_prefix, path) for path in paths])
62
63    exports_files = """exports_files([
64    %s
65])
66""" % ",\n    ".join(binary_paths + target_paths)
67    rctx.file("BUILD", exports_files)
68
69make_injection_repository = repository_rule(
70    implementation = _impl,
71    doc = """This rule exposes Soong prebuilts for migrating the build to Bazel.
72
73This rule allows the Bazel build (i.e. b build //bionic/...) to depend on prebuilts from
74Soong. A use case is to allow the Bazel build to use prebuilt host tools in the
75Bazel rules toolchains without first converting them to Bazel.""",
76    attrs = {
77        "binaries": attr.string_list(default = [], doc = "A list of host binary modules built for linux-x86."),
78        "target_module_files": attr.string_list_dict(default = {}, doc = "A dict of modules to the target files that should be exported."),
79        # See b/210399979
80        "watch_android_bp_files": attr.label_list(allow_files = [".bp"], default = [], doc = "A list of Android.bp files to watch for changes to invalidate this repository rule."),
81    },
82    environ = ["TARGET_PRODUCT", "TARGET_BUILD_VARIANT"],
83)
84