xref: /aosp_15_r20/external/sandboxed-api/sandboxed_api/bazel/repositories.bzl (revision ec63e07ab9515d95e79c211197c445ef84cefa6a)
1# Copyright 2019 Google LLC
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#     https://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
15"""Repository rule that runs autotools' configure after extraction."""
16
17def _configure(ctx):
18    bash_exe = ctx.os.environ["BAZEL_SH"] if "BAZEL_SH" in ctx.os.environ else "bash"
19    ctx.report_progress("Running configure script...")
20
21    # Run configure script and move host-specific directory to a well-known
22    # location.
23    ctx.execute(
24        [bash_exe, "-c", """./configure --disable-dependency-tracking {args};
25                            mv $(. config.guess) configure-bazel-gen || true
26                         """.format(args = " ".join(ctx.attr.configure_args))],
27        quiet = ctx.attr.quiet,
28    )
29
30def _buildfile(ctx):
31    bash_exe = ctx.os.environ["BAZEL_SH"] if "BAZEL_SH" in ctx.os.environ else "bash"
32    if ctx.attr.build_file:
33        ctx.execute([bash_exe, "-c", "rm -f BUILD BUILD.bazel"])
34        ctx.symlink(ctx.attr.build_file, "BUILD.bazel")
35    elif ctx.attr.build_file_content:
36        ctx.execute([bash_exe, "-c", "rm -f BUILD.bazel"])
37        ctx.file("BUILD.bazel", ctx.attr.build_file_content)
38
39def _autotools_repository_impl(ctx):
40    if ctx.attr.build_file and ctx.attr.build_file_content:
41        ctx.fail("Only one of build_file and build_file_content can be provided.")
42    ctx.download_and_extract(
43        ctx.attr.urls,
44        "",  # output
45        ctx.attr.sha256,
46        "",  # type
47        ctx.attr.strip_prefix,
48    )
49    _configure(ctx)
50    _buildfile(ctx)
51
52autotools_repository = repository_rule(
53    attrs = {
54        "urls": attr.string_list(
55            mandatory = True,
56            allow_empty = False,
57        ),
58        "sha256": attr.string(),
59        "strip_prefix": attr.string(),
60        "build_file": attr.label(
61            mandatory = True,
62            allow_single_file = [".BUILD"],
63        ),
64        "build_file_content": attr.string(),
65        "configure_args": attr.string_list(
66            allow_empty = True,
67        ),
68        "quiet": attr.bool(
69            default = True,
70        ),
71    },
72    implementation = _autotools_repository_impl,
73)
74