Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

perf: use orjson for writing compile_commands #118

Closed
wants to merge 11 commits into from
Prev Previous commit
Next Next commit
build: use bazel to install orjson (#1)
  • Loading branch information
garymm authored and aminya committed Jan 3, 2024
commit 4fb3ceb6b26ec25852a4e48e1afe477e83782bf9
43 changes: 43 additions & 0 deletions WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,48 @@

workspace(name = "hedron_compile_commands")

load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")

# TODO(cpsauer): move everything above hedron_compile_commands_setup() into setup macros.
BAZEL_SKYLIB_VERSION = "1.4.2"

http_archive(
name = "bazel_skylib",
sha256 = "66ffd9315665bfaafc96b52278f57c7e2dd09f5ede279ea6d39b2be471e7e3aa",
urls = [
"https://mirror.bazel.build/github.com/bazelbuild/bazel-skylib/releases/download/{0}/bazel-skylib-{0}.tar.gz".format(BAZEL_SKYLIB_VERSION),
"https://github.com/bazelbuild/bazel-skylib/releases/download/{0}/bazel-skylib-{0}.tar.gz".format(BAZEL_SKYLIB_VERSION),
],
)

http_archive(
name = "rules_python",
sha256 = "84aec9e21cc56fbc7f1335035a71c850d1b9b5cc6ff497306f84cced9a769841",
strip_prefix = "rules_python-0.23.1",
url = "https://github.com/bazelbuild/rules_python/releases/download/0.23.1/rules_python-0.23.1.tar.gz",
)

load("@rules_python//python:repositories.bzl", "python_register_toolchains")

python_register_toolchains(
name = "python_toolchain",
python_version = "3.11",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

BTW if this works, then you can assume python 3.11 in refresh.template.py and simplify the script quite a bit. Not suggesting that should be in this PR, probably a later PR.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It works, but the requirement is adding things to the workspace of the user, which I am not sure if it is desirable:
#118 (comment)

)

# For re-generating python_requirements_lock.bzl:
# * update python_requirements_lock.txt
# * Un-comment the below
# * run `bazel build @pip//...`,
# * cp external/pip/requirements.bzl python_requirements_lock.bzl

# load("@python_toolchain//:defs.bzl", "interpreter")
# load("@rules_python//python:pip.bzl", "pip_parse")
# pip_parse(
# name = "pip",
# python_interpreter_target = interpreter,
# requirements_lock = "//:python_requirements_lock.txt",
# )

load("@hedron_compile_commands//:workspace_setup.bzl", "hedron_compile_commands_setup")

hedron_compile_commands_setup()
52 changes: 52 additions & 0 deletions python_requirements_lock.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
"""Starlark representation of locked requirements.

@generated by rules_python pip_parse repository rule
from @//:python_requirements_lock.txt
"""

load("@rules_python//python/pip_install:pip_repository.bzl", "whl_library")

all_requirements = ["@pip_orjson//:pkg"]

all_whl_requirements = ["@pip_orjson//:whl"]

_packages = [("pip_orjson", "orjson==3.9.1")]
_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}
_annotations = {}

def _clean_name(name):
return name.replace("-", "_").replace(".", "_").lower()

def requirement(name):
return "@pip_" + _clean_name(name) + "//:pkg"

def whl_requirement(name):
return "@pip_" + _clean_name(name) + "//:whl"

def data_requirement(name):
return "@pip_" + _clean_name(name) + "//:data"

def dist_info_requirement(name):
return "@pip_" + _clean_name(name) + "//:dist_info"

def entry_point(pkg, script = None):
if not script:
script = pkg
return "@pip_" + _clean_name(pkg) + "//:rules_python_wheel_entry_point_" + script

def _get_annotation(requirement):
# This expects to parse `setuptools==58.2.0 --hash=sha256:2551203ae6955b9876741a26ab3e767bb3242dafe86a32a749ea0d78b6792f11`
# down to `setuptools`.
name = requirement.split(" ")[0].split("=")[0].split("[")[0]
return _annotations.get(name)

def install_deps(**whl_library_kwargs):
whl_config = dict(_config)
whl_config.update(whl_library_kwargs)
for name, requirement in _packages:
whl_library(
name = name,
requirement = requirement,
annotation = _get_annotation(requirement),
**whl_config
)
1 change: 1 addition & 0 deletions python_requirements_lock.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
orjson==3.9.1
3 changes: 2 additions & 1 deletion refresh_compile_commands.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,11 @@ refresh_compile_commands(
```
"""


########################################
# Implementation

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

def refresh_compile_commands(
name,
Expand Down Expand Up @@ -99,6 +99,7 @@ def refresh_compile_commands(
script_name,
],
imports = [''], # Allows binary to import templated script, even if this macro is being called inside a sub package. See https://github.com/hedronvision/bazel-compile-commands-extractor/issues/137
deps = ["@pip_orjson//:pkg"],
**kwargs
)

Expand Down
Empty file added requirements.bzl
Empty file.
4 changes: 4 additions & 0 deletions workspace_setup.bzl
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
"""Workspace setup macros."""

# Do not change the filename; it is part of the user interface.

def hedron_compile_commands_setup(module_ctx = None):
Expand Down Expand Up @@ -25,3 +27,5 @@ hedron_compile_commands_extension = module_extension( # Note: Doesn't break load
# This extension is automatically loaded when using bzlmod (from MODULE.bazel) and will run the same function as WORKSPACE,
# but passes in a module_ctx object for advanced context of the whole project, allowing for complex, project wide modifiying extensions and distinguishing between WORKSPACE and bzlmod setups.
)

install_python_deps()