Skip to content

Commit c974427

Browse files
authored
build: use bazel to install orjson (#1)
1 parent 21450ec commit c974427

6 files changed

+105
-8
lines changed

WORKSPACE

+45-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,51 @@
11
# This file existed originally to enable quick local development via local_repository.
2-
# See ./ImplementationReadme.md for details on local development.
3-
# Why? local_repository didn't work without a WORKSPACE, and new_local_repository required overwriting the BUILD file (as of Bazel 5.0).
2+
# See ./ImplementationReadme.md for details on local development.
3+
# Why? local_repository didn't work without a WORKSPACE, and new_local_repository required overwriting the BUILD file (as of Bazel 5.0).
44

55
workspace(name = "hedron_compile_commands")
66

7+
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
8+
9+
# TODO(cpsauer): move everything above hedron_compile_commands_setup() into setup macros.
10+
BAZEL_SKYLIB_VERSION = "1.4.2"
11+
12+
http_archive(
13+
name = "bazel_skylib",
14+
sha256 = "66ffd9315665bfaafc96b52278f57c7e2dd09f5ede279ea6d39b2be471e7e3aa",
15+
urls = [
16+
"https://mirror.bazel.build/github.com/bazelbuild/bazel-skylib/releases/download/{0}/bazel-skylib-{0}.tar.gz".format(BAZEL_SKYLIB_VERSION),
17+
"https://github.com/bazelbuild/bazel-skylib/releases/download/{0}/bazel-skylib-{0}.tar.gz".format(BAZEL_SKYLIB_VERSION),
18+
],
19+
)
20+
21+
http_archive(
22+
name = "rules_python",
23+
sha256 = "84aec9e21cc56fbc7f1335035a71c850d1b9b5cc6ff497306f84cced9a769841",
24+
strip_prefix = "rules_python-0.23.1",
25+
url = "https://github.com/bazelbuild/rules_python/releases/download/0.23.1/rules_python-0.23.1.tar.gz",
26+
)
27+
28+
load("@rules_python//python:repositories.bzl", "python_register_toolchains")
29+
30+
python_register_toolchains(
31+
name = "python_toolchain",
32+
python_version = "3.11",
33+
)
34+
35+
# For re-generating python_requirements_lock.bzl:
36+
# * update python_requirements_lock.txt
37+
# * Un-comment the below
38+
# * run `bazel build @pip//...`,
39+
# * cp external/pip/requirements.bzl python_requirements_lock.bzl
40+
41+
# load("@python_toolchain//:defs.bzl", "interpreter")
42+
# load("@rules_python//python:pip.bzl", "pip_parse")
43+
# pip_parse(
44+
# name = "pip",
45+
# python_interpreter_target = interpreter,
46+
# requirements_lock = "//:python_requirements_lock.txt",
47+
# )
48+
749
load("@hedron_compile_commands//:workspace_setup.bzl", "hedron_compile_commands_setup")
50+
851
hedron_compile_commands_setup()

python_requirements_lock.bzl

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
"""Starlark representation of locked requirements.
2+
3+
@generated by rules_python pip_parse repository rule
4+
from @//:python_requirements_lock.txt
5+
"""
6+
7+
load("@rules_python//python/pip_install:pip_repository.bzl", "whl_library")
8+
9+
all_requirements = ["@pip_orjson//:pkg"]
10+
11+
all_whl_requirements = ["@pip_orjson//:whl"]
12+
13+
_packages = [("pip_orjson", "orjson==3.9.1")]
14+
_config = {"download_only": False, "enable_implicit_namespace_pkgs": False, "environment": {}, "extra_pip_args": [], "isolated": True, "pip_data_exclude": [], "python_interpreter": "python3", "python_interpreter_target": "@python_toolchain_x86_64-unknown-linux-gnu//:bin/python3", "quiet": True, "repo": "pip", "repo_prefix": "pip_", "timeout": 600}
15+
_annotations = {}
16+
17+
def _clean_name(name):
18+
return name.replace("-", "_").replace(".", "_").lower()
19+
20+
def requirement(name):
21+
return "@pip_" + _clean_name(name) + "//:pkg"
22+
23+
def whl_requirement(name):
24+
return "@pip_" + _clean_name(name) + "//:whl"
25+
26+
def data_requirement(name):
27+
return "@pip_" + _clean_name(name) + "//:data"
28+
29+
def dist_info_requirement(name):
30+
return "@pip_" + _clean_name(name) + "//:dist_info"
31+
32+
def entry_point(pkg, script = None):
33+
if not script:
34+
script = pkg
35+
return "@pip_" + _clean_name(pkg) + "//:rules_python_wheel_entry_point_" + script
36+
37+
def _get_annotation(requirement):
38+
# This expects to parse `setuptools==58.2.0 --hash=sha256:2551203ae6955b9876741a26ab3e767bb3242dafe86a32a749ea0d78b6792f11`
39+
# down to `setuptools`.
40+
name = requirement.split(" ")[0].split("=")[0].split("[")[0]
41+
return _annotations.get(name)
42+
43+
def install_deps(**whl_library_kwargs):
44+
whl_config = dict(_config)
45+
whl_config.update(whl_library_kwargs)
46+
for name, requirement in _packages:
47+
whl_library(
48+
name = name,
49+
requirement = requirement,
50+
annotation = _get_annotation(requirement),
51+
**whl_config
52+
)

python_requirements_lock.txt

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
orjson==3.9.1

refresh_compile_commands.bzl

+2-2
Original file line numberDiff line numberDiff line change
@@ -52,11 +52,11 @@ refresh_compile_commands(
5252
```
5353
"""
5454

55-
5655
########################################
5756
# Implementation
5857

5958
load("@bazel_tools//tools/cpp:toolchain_utils.bzl", "find_cpp_toolchain")
59+
load("@rules_python//python:defs.bzl", "py_binary")
6060

6161
def refresh_compile_commands(
6262
name,
@@ -80,7 +80,7 @@ def refresh_compile_commands(
8080
# Generate runnable python script from template
8181
script_name = name + ".py"
8282
_expand_template(name = script_name, labels_to_flags = targets, exclude_headers = exclude_headers, exclude_external_sources = exclude_external_sources, **kwargs)
83-
native.py_binary(name = name, srcs = [script_name], **kwargs)
83+
py_binary(name = name, srcs = [script_name], deps = ["@pip_orjson//:pkg"], **kwargs)
8484

8585
def _expand_template_impl(ctx):
8686
"""Inject targets of interest into refresh.template.py, and set it up to be run."""

requirements.bzl

Whitespace-only changes.

workspace_setup.bzl

+5-4
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
1+
"""Workspace setup macros."""
2+
13
# Do not change the filename; it is part of the user interface.
24

5+
load("//:python_requirements_lock.bzl", install_python_deps = "install_deps")
6+
37
def hedron_compile_commands_setup():
48
"""Set up a WORKSPACE to have hedron_compile_commands used within it."""
59

610
# Unified setup for users' WORKSPACES and this workspace when used standalone.
711
# See invocations in:
812
# README.md (for users)
913
# WORKSPACE (for working on this repo standalone)
10-
11-
# Currently nothing to do -> no-op.
12-
# So why is this even here? Enables future expansion (e.g to add transitive dependencies) without changing the user interface.
13-
pass
14+
install_python_deps()

0 commit comments

Comments
 (0)