blob: 750a5c197cfa21e94d5d767c5526d5d50b431162 [file] [log] [blame]
# Copyright 2022 The ChromiumOS Authors
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
load("@bazel_tools//tools/build_defs/repo:git_worker.bzl", "git_repo")
load("@bazel_skylib//lib:paths.bzl", "paths")
def _exec(ctx, cmd, msg = None, **kwargs):
env = dict(ctx.os.environ)
if msg:
st = ctx.execute(cmd, environment = env)
if st.return_code:
fail("Error running command %s:\n%s%s" % (cmd, st.stdout, st.stderr))
return st.stdout
def _git(ctx, repo, *args):
cmd = ["git", "-C", repo] + list(args)
return _exec(ctx, cmd)
def _cros_chrome_repository_impl(ctx):
"""Repository rule that downloads the Chromium/Chrome source."""
tar = ctx.which("tar")
if not tar:
fail("tar was not found on the path")
ctx.template(".gclient", ctx.attr._gclient_template, {
"{tag}": ctx.attr.tag,
"{internal}": str(ctx.attr.internal),
git_ = git_repo(ctx, "src")
# The chromium repo is huge and gclient will perform a blind `git fetch`
# that attempts to fetch all the refs. We want to ensure we only pull
# the specific tag, so we override the fetch config
"refs/tags/{}:refs/tags/{}".format(ctx.attr.tag, ctx.attr.tag),
_exec(ctx, [
# This unfortunately doesn't change how much data we fetch.
# This still pulls in 10000 commits. I wish we could specify the depth.
], "Fetching third_party chromium dependencies")
pwd = _exec(ctx, ["pwd"]).strip()
# Set the cache directories so we don't write to the users home directory.
cipd_cache_dir = paths.join(pwd, ".cipd-cache")
vpython_root = paths.join(pwd, ".vpython-root")
depot_tools_path = paths.join(pwd, "src/third_party/depot_tools")
# Never auto update depot_tools since we won't have network access.
ctx.file(paths.join(depot_tools_path, ".disable_auto_update"), "")
# This command will populate the cipd cache, create a python venv, and
# then run all the hooks.
# Run the hooks with the depot tools pinned by chromium
paths.join(depot_tools_path, "gclient"),
"Running chromium hooks",
PATH = "{}:{}".format(depot_tools_path, ctx.os.environ["PATH"]),
CIPD_CACHE_DIR = cipd_cache_dir,
# See
tar_common = [
"1970-1-1 00:00Z",
# r: Apply transformation to regular archive members.
# S: Do not apply transformation to symbolic link targets.
# h: Apply transformation to hard link targets.
# We use zstd since it's way faster than gzip and should be installed by
# default on most distributions. Hopefully the compression algorithm doesn't
# change between hosts, otherwise the output won't be hermetic.
# The compressed src should be around ~5GiB, uncompressed it's about 20 GiB.
tar_common + [
# chromium src and depot_tools with it's own .cipd_bin
# Needed to signal the gclient root
# Hashes of all the dependencies. Useful since we don't include the
# .git directories.
# cipd root created by gclient
# cipd lookup cache
# We don't include the .vpython-root since it contains absolute
# symlinks which we can't use inside the chroot. Since we have the
# cache and pkgs we can recreate it in the chroot without network
# access.
msg = "Tarring up Chromium src",
if ctx.attr.internal:
# We split the src into two tarballs to make it clear that one is only
# meant for internal consumption.
tar_common + ["--file", "src-internal.tar.zst", "src-internal"],
msg = "Tarring up Chrome src",
if ctx.attr.internal:
ctx.file("WORKSPACE", "workspace(name = \"{name}\")\n".format(name =
ctx.template("BUILD.bazel", ctx.attr._build_file)
_cros_sdk_repository_attrs = {
"tag": attr.string(
doc = """The expected SHA-256 of the file downloaded.""",
mandatory = True,
"internal": attr.bool(
doc = """If true download Chrome if false download Chromium.""",
default = False,
"remote": attr.string(
doc = "The URI of the remote Chromium Git repository",
default = "",
"verbose": attr.bool(default = False),
# The DO NOT USE attrs are only here to make `git_repo` happy.
"init_submodules": attr.bool(
default = False,
doc = "DO NOT USE",
"recursive_init_submodules": attr.bool(
default = False,
doc = "DO NOT USE",
"commit": attr.string(
default = "",
doc = "DO NOT USE",
"shallow_since": attr.string(
default = "",
doc = "DO NOT USE",
"gclient": attr.label(
doc = """gclient binary used to fetch chromium.""",
mandatory = True,
"_gclient_template": attr.label(
doc = """.gclient template.""",
default = "",
"_build_file": attr.label(
allow_single_file = True,
default = "",
cros_chrome_repository = repository_rule(
implementation = _cros_chrome_repository_impl,
attrs = _cros_sdk_repository_attrs,