Skip to content

Commit c4ee3b1

Browse files
committed
Additional polish while adding bzlmod support
1 parent af5fd45 commit c4ee3b1

File tree

4 files changed

+51
-43
lines changed

4 files changed

+51
-43
lines changed

ImplementationReadme.md

+14-3
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,25 @@ Hello, and thanks for your interest in the codebase :)
1212

1313
We'd recommend setting up for rapid iteration as follows:
1414

15-
Get a local copy. (e.g. `git clone git@github.com:hedronvision/bazel-compile-commands-extractor.git`)
15+
Get a local copy. (e.g. `git clone https://github.com/hedronvision/bazel-compile-commands-extractor`, though you probably want to make a fork and clone that so you can easily contribute back.)
1616

17-
Then point your main repo (from which you're seeing issues) to use your local development copy of the compile commands. To do that, open `WORKSPACE`, and swap out the `http_archive` loading `hedron_compile_commands` with the following.
17+
Then point your main repo (from which you're seeing issues) to use your local development copy of the compile commands by doing the following.
18+
19+
If you're bringing in this tool via your `MODULE.bazel` file, swap out the `git_override` loading `hedron_compile_commands` with the following.
20+
```Starlark
21+
local_path_override(
22+
module_name = "hedron_compile_commands",
23+
path = "../bazel-compile-commands-extractor" # Assuming this tool is cloned next to your project's repo
24+
)
25+
``````
26+
27+
28+
If you're bringing in this tool via your `WORKSPACE` file, swap out the `http_archive` loading `hedron_compile_commands` with the following.
1829

1930
```Starlark
2031
local_repository(
2132
name = "hedron_compile_commands",
22-
path = "../bazel-compile-commands-extractor", # Or wherever you put it.
33+
path = "../bazel-compile-commands-extractor", # Assuming this tool is cloned next to your project's repo
2334
)
2435
```
2536

MODULE.bazel

-12
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,3 @@
1-
"""bzlmod implementation for bazel-compile-commands-extractor
2-
This allows people using the new bzlmod system to add bazel-compile-commands-extractor as a module directly
3-
4-
Local Development Note:
5-
Rather than using the git override method in the README.md, you can use the local_path_override method: (Add this to the MODULE.bazel of your project)
6-
7-
bazel_dep(name = "hedron_compile_commands", dev_dependency = True)
8-
local_path_override(
9-
module_name = "hedron_compile_commands",
10-
path = "../bazel-compile-commands-extractor" # Assuming this repo is cloned next to your project
11-
)
12-
"""
131
module(
142
name = "hedron_compile_commands",
153
version = "1.0.0",

README.md

+23-21
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,27 @@ Howdy, Bazel user 🤠. Let's get you set up fast with some awesome tooling for
4242

4343
There's a bunch of text here but only because we're trying to spell things out and make them easy. If you have issues, let us know; we'd love your help making things even better and more complete—and we'd love to help you!
4444

45-
### If you're using the WORKSPACE system (you probably are), do the usual WORKSPACE setup:
45+
### First, add this tool to your Bazel setup.
4646

47-
Copy this into your Bazel `WORKSPACE` file to add this repo as an external dependency, making sure to update to the [latest commit](https://github.com/hedronvision/bazel-compile-commands-extractor/commits/main) per the instructions below.
47+
#### If you have a MODULE.bazel file and are using the new [bzlmod](https://bazel.build/external/migration) system
48+
Copy this into your `MODULE.bazel`, making sure to update to the [latest commit](https://github.com/hedronvision/bazel-compile-commands-extractor/commits/main) per the instructions below.
49+
50+
```Starlark
51+
# Hedron's Compile Commands Extractor for Bazel
52+
# https://github.com/hedronvision/bazel-compile-commands-extractor
53+
bazel_dep(name = "hedron_compile_commands", dev_dependency = True)
54+
git_override(
55+
module_name = "hedron_compile_commands",
56+
remote = "https://github.com/hedronvision/bazel-compile-commands-extractor.git",
57+
commit = "25ac20bc2b9b491c22798a54a1c9ecbc6b026749",
58+
# Replace the commit hash (above) with the latest (https://github.com/hedronvision/bazel-compile-commands-extractor/commits/main); the stale one here is too old to work with bzlmod.
59+
# Even better, set up Renovate and let it do the work for you (see "Suggestion: Updates" in the README).
60+
)
61+
```
62+
63+
#### If you're using the traditional WORKSPACE system
64+
65+
Copy this into your Bazel `WORKSPACE` file, making sure to update to the [latest commit](https://github.com/hedronvision/bazel-compile-commands-extractor/commits/main) per the instructions below.
4866

4967
```Starlark
5068
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
@@ -55,7 +73,7 @@ load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
5573
http_archive(
5674
name = "hedron_compile_commands",
5775

58-
# Replace the commit hash in both places (below) with the latest, rather than using the stale one here.
76+
# Replace the commit hash (ed994039a951b736091776d677f324b3903ef939) in both places (below) with the latest (https://github.com/hedronvision/bazel-compile-commands-extractor/commits/main), rather than using the stale one here.
5977
# Even better, set up Renovate and let it do the work for you (see "Suggestion: Updates" in the README).
6078
url = "https://github.com/hedronvision/bazel-compile-commands-extractor/archive/ed994039a951b736091776d677f324b3903ef939.tar.gz",
6179
strip_prefix = "bazel-compile-commands-extractor-ed994039a951b736091776d677f324b3903ef939",
@@ -65,29 +83,15 @@ load("@hedron_compile_commands//:workspace_setup.bzl", "hedron_compile_commands_
6583
hedron_compile_commands_setup()
6684
```
6785

68-
### If you're using Bazel's new [bzlmod](https://bazel.build/external/migration) system:
69-
You're probably using bzlmod if you have `common --enable_bzlmod` in your `.bazelrc` (or are using Bazel v7+). If so, copy this into your Bazel `MODULE.bazel` file to add this repo as an external dependency, making sure to update to the [latest commit](https://github.com/hedronvision/bazel-compile-commands-extractor/commits/main) per the instructions below.
70-
71-
```Starlark
72-
bazel_dep(name = "hedron_compile_commands", dev_dependency = True)
73-
git_override(
74-
module_name = "hedron_compile_commands",
75-
remote = "https://github.com/hedronvision/bazel-compile-commands-extractor.git",
76-
commit = "25ac20bc2b9b491c22798a54a1c9ecbc6b026749",
77-
# Replace the commit hash (above) with the latest, the stale one here is too old to work with bzlmod.
78-
# Even better, set up Renovate and let it do the work for you (see "Suggestion: Updates" in the README).
79-
)
80-
```
81-
82-
#### Suggestion: Updates
86+
#### Either way: Get Updates via Renovate
8387

8488
Improvements come frequently, so we'd recommend keeping up-to-date.
8589

8690
We'd strongly recommend you set up [Renovate](https://github.com/renovatebot/renovate) (or similar) at some point to keep this dependency (and others) up-to-date by default. [We aren't affiliated with Renovate or anything, but we think it's awesome. It watches for new versions and sends you PRs for review or automated testing. It's free and easy to set up. It's been astoundingly useful in our codebase, and we've worked with the wonderful maintainer to make things great for Bazel use. And it's used in official Bazel repositories.] Here's a [Renovate configuration example from one of our projects](https://github.com/hedronvision/bazel-cc-filesystem-backport/blob/main/renovate.json5), in the hope that it might save you time.
8791

8892
If not now, maybe come back to this step later, or watch this repo for updates. [Or hey, maybe give us a quick star, while you're thinking about watching.] Like Abseil, we live at head; the latest commit to the main branch is the commit you want. So don't rely on release notifications; use [Renovate](https://github.com/renovatebot/renovate) or poll manually for new commits.
8993

90-
### Get the extractor running.
94+
### Second, get the extractor running.
9195

9296
We'll generate a `compile_commands.json` file in the root of the Bazel workspace.
9397

@@ -236,8 +240,6 @@ If you're using Bazel for the C language family, you'll likely also want some of
236240
1. A good way of making secure network requests: [hedronvision/bazel-make-cc-https-easy](https://github.com/hedronvision/bazel-make-cc-https-easy)
237241
2. A way to use std::filesystem across platforms: [hedronvision/bazel-cc-filesystem-backport](https://github.com/hedronvision/bazel-cc-filesystem-backport)
238242

239-
If so, please check out our other project, [hedronvision/bazel-make-cc-https-easy](https://github.com/hedronvision/bazel-make-cc-https-easy).
240-
241243
---
242244
*Looking for implementation details instead? Want to dive into the codebase?*
243245
See [ImplementationReadme.md](./ImplementationReadme.md).

workspace_setup.bzl

+14-7
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,27 @@
11
# Do not change the filename; it is part of the user interface.
22

3-
def hedron_compile_commands_setup(ctx = None):
4-
"""Set up a WORKSPACE to have hedron_compile_commands used within it."""
3+
def hedron_compile_commands_setup(module_ctx = None):
4+
"""Set up a WORKSPACE to have hedron_compile_commands used within it.
5+
6+
module_ctx is for automatic-implicit use by bzlmod only.
7+
"""
58

69
# Unified setup for users' WORKSPACES and this workspace when used standalone.
710
# See invocations in:
8-
# README.md (for users)
11+
# README.md (for WORKSPACE users)
12+
# MODULE.bazel (for bzlmod users)
913
# WORKSPACE (for working on this repo standalone)
1014

15+
# If adding dependencies available via bzlmod, consider adding them to MODULE.bazel, too, and only loading them the WORKSPACE way when needed.
16+
# if not module_ctx:
17+
# # Load bzlmod-available packages.
18+
1119
# Currently nothing to do -> no-op.
1220
# So why is this even here? Enables future expansion (e.g to add transitive dependencies) without changing the user interface.
1321
pass
1422

15-
hedron_compile_commands_extension = module_extension(
23+
hedron_compile_commands_extension = module_extension( # Note: Doesn't break loading from WORKSPACE as far back as Bazel 5.0.0
1624
implementation = hedron_compile_commands_setup,
17-
# This extension is loaded when using bzlmod (MODULE.bazel) and will run the same function as WORKSPACE,
18-
# but passes in a module_ctx object for advanced context of the whole project, allowing for complex, project wide modifiying extensions
19-
# It's not currently used but is always passed in. ctx is defaulted to None for compatibility with workspace setup.
25+
# This extension is automatically loaded when using bzlmod (from MODULE.bazel) and will run the same function as WORKSPACE,
26+
# 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.
2027
)

0 commit comments

Comments
 (0)