blob: d59fe98b0478607694f38b302ed32a37f2c4319a [file] [log] [blame]
Chris Frantz74b07402022-04-07 18:07:34 -07001# Copyright lowRISC contributors.
2# Licensed under the Apache License, Version 2.0, see LICENSE for details.
3# SPDX-License-Identifier: Apache-2.0
4load("@python3//:defs.bzl", "interpreter")
5
6def _bitstreams_repo_impl(rctx):
Timothy Trippelabfcfb02022-05-04 14:29:21 -07007 # First, check if an existing pre-built bitstream cache repo exists, and if
8 # so, use it instead of building one.
9 cache_path = rctx.os.environ.get(
10 "BAZEL_BITSTREAMS_CACHE",
11 rctx.attr.cache,
12 )
Timothy Trippel47587f32022-05-04 13:03:20 -070013 result = rctx.execute(
Chris Frantz74b07402022-04-07 18:07:34 -070014 [
15 rctx.path(rctx.attr.python_interpreter),
16 rctx.attr._cache_manager,
17 "--build-file=BUILD.bazel",
18 "--bucket-url={}".format(rctx.attr.bucket_url),
Timothy Trippelabfcfb02022-05-04 14:29:21 -070019 "--cache={}".format(cache_path),
Chris Frantz74b07402022-04-07 18:07:34 -070020 "--refresh-time={}".format(rctx.attr.refresh_time),
21 ],
22 quiet = False,
23 )
Timothy Trippel47587f32022-05-04 13:03:20 -070024 if result.return_code != 0:
25 fail("Bitstream cache not initialized properly.")
Chris Frantz74b07402022-04-07 18:07:34 -070026
27# The bitstream repo is evaluated on every invocation of bazel.
28# Once the cache is initialized, a typical invocation will find the latest
29# cached bitstream and configure it for use as a test artifact.
30#
31# The `refresh_time` sets the interval at which the cache manager will
32# check for new bitstreams.
33#
34# By default, the cache manager will configure the latest available bitstream
35# as the default bitstream. It will refresh every 18 hours.
36#
Alexander Williams8365ac42022-08-17 17:07:28 -070037# The behavior of the cache manager can be controlled via the BITSTREAM
Chris Frantz74b07402022-04-07 18:07:34 -070038# environment variable. The environment variable can be any command line
39# arguments accepted by the cache manager script.
40#
41# For example, to force a refresh of the cache:
42# BITSTREAM=--refresh bazel build @bitstreams//...
43#
44# To use an exact bitstream in a test:
45# BITSTREAM=3732655dbd225b5c1ae94a79b54cc9dc8cd8e391 bazel test <label>
46#
47# You can use any valid git commit identifier to select a bitstream:
48# BITSTREAM=HEAD~1 bazel test <label>
49#
50# If you ask for a bitstream that does not exist in the GCP bucket, the
51# next oldest bitstream will be chosen.
52bitstreams_repo = repository_rule(
53 implementation = _bitstreams_repo_impl,
54 attrs = {
55 "bucket_url": attr.string(
56 doc = "Location of the GCP bitstream bucket.",
57 default = "https://storage.googleapis.com/opentitan-bitstreams/",
58 ),
59 "cache": attr.string(
60 doc = "Location of bitstreams cache.",
61 default = "~/.cache/opentitan-bitstreams",
62 ),
63 "refresh_time": attr.int(
64 doc = "How often to check for new bitstreams (seconds).",
65 default = 18 * 3600, # Refresh every 18h
66 ),
67 "python_interpreter": attr.label(
68 default = interpreter,
69 allow_single_file = True,
70 doc = "Python interpreter to use.",
71 ),
72 "_cache_manager": attr.label(
73 default = Label("//rules/scripts:bitstreams_workspace.py"),
74 allow_files = True,
75 ),
76 },
Timothy Trippelabfcfb02022-05-04 14:29:21 -070077 environ = ["BAZEL_BITSTREAMS_CACHE", "BITSTREAM"],
Chris Frantz74b07402022-04-07 18:07:34 -070078 local = True,
79)