Chris Frantz | 74b0740 | 2022-04-07 18:07:34 -0700 | [diff] [blame] | 1 | # Copyright lowRISC contributors. |
| 2 | # Licensed under the Apache License, Version 2.0, see LICENSE for details. |
| 3 | # SPDX-License-Identifier: Apache-2.0 |
| 4 | load("@python3//:defs.bzl", "interpreter") |
| 5 | |
| 6 | def _bitstreams_repo_impl(rctx): |
Timothy Trippel | abfcfb0 | 2022-05-04 14:29:21 -0700 | [diff] [blame] | 7 | # 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 Trippel | 47587f3 | 2022-05-04 13:03:20 -0700 | [diff] [blame] | 13 | result = rctx.execute( |
Chris Frantz | 74b0740 | 2022-04-07 18:07:34 -0700 | [diff] [blame] | 14 | [ |
| 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 Trippel | abfcfb0 | 2022-05-04 14:29:21 -0700 | [diff] [blame] | 19 | "--cache={}".format(cache_path), |
Chris Frantz | 74b0740 | 2022-04-07 18:07:34 -0700 | [diff] [blame] | 20 | "--refresh-time={}".format(rctx.attr.refresh_time), |
| 21 | ], |
| 22 | quiet = False, |
| 23 | ) |
Timothy Trippel | 47587f3 | 2022-05-04 13:03:20 -0700 | [diff] [blame] | 24 | if result.return_code != 0: |
| 25 | fail("Bitstream cache not initialized properly.") |
Chris Frantz | 74b0740 | 2022-04-07 18:07:34 -0700 | [diff] [blame] | 26 | |
| 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 Williams | 8365ac4 | 2022-08-17 17:07:28 -0700 | [diff] [blame] | 37 | # The behavior of the cache manager can be controlled via the BITSTREAM |
Chris Frantz | 74b0740 | 2022-04-07 18:07:34 -0700 | [diff] [blame] | 38 | # 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. |
| 52 | bitstreams_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 Trippel | abfcfb0 | 2022-05-04 14:29:21 -0700 | [diff] [blame] | 77 | environ = ["BAZEL_BITSTREAMS_CACHE", "BITSTREAM"], |
Chris Frantz | 74b0740 | 2022-04-07 18:07:34 -0700 | [diff] [blame] | 78 | local = True, |
| 79 | ) |