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

Build the project with Bazel. #3312

Closed
wants to merge 1 commit into from
Closed

Conversation

zadlg
Copy link

@zadlg zadlg commented Apr 21, 2024

Build the project with Bazel.

This commit introduces a Bazel-based build system for the core library
of tree-sitter.

In order not to "pollute" the existing source directories, an overlay directory
has been created at bazel/.

People can build the tree-sitter C API by doing:

$ cd bazel/
$ bazel build @tree-sitter

The following two flags are available to enable or disable specific features:

  • --@tree-sitter//build_config:wasm=<true|false>: enable WASM. not yet supported.
  • --@tree-sitter//build_config:hide_symbols=<true|false>: if enabled, do not expose tree-sitter symbols.

Bazel users can import tree-sitter into their existing Bazel workspace by
adding the following to their WORKSPACE file:

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

# The git commit.
_COMMIT = ""

# SHA-256 digest of the archive.
_SHA256SUM = ""
http_archive(
    name = "tree-sitter-raw",
    sha256 = _SHA256SUM,
    urls = [
        "https://github.com/tree-sitter/tree-sitter/archive/{commit}.tar.gz".format(
            commit = _COMMIT,
        ),
    ],
    strip_prefix = "tree-sitter-{commit}".format(
        commit = _COMMIT,
    ),
)

load("@tree-sitter-raw//bazel:configure.bzl", "tree_sitter_configure")

# Configures the repository `tree-sitter`.
tree_sitter_configure(name = "tree-sitter")

# Fetches `tree-sitter` Bazel dependencies.
load("@tree-sitter//:repositories.bzl", "tree_sitter_repositories")

tree_sitter_repositories()

# Loads `tree-sitter` Bazel dependencies.
load("@tree-sitter//:deps.bzl", "tree_sitter_dependencies")

tree_sitter_dependencies()

Then, users can link their libraries/binaries against tree-sitter:

cc_library(
    name = "my-parser",
    srcs = ["my-parser.cc"],
    deps = ["@tree-sitter"],
)

@zadlg zadlg marked this pull request as ready for review April 21, 2024 18:48
@ObserverOfTime
Copy link
Member

Several CMake PRs were rejected for being too much. Now this, this is way too much.

@zadlg
Copy link
Author

zadlg commented Apr 22, 2024

Several CMake PRs were rejected for being too much. Now this, this is way too much.

Hi @ObserverOfTime, thanks for your reply.
Could you elaborate on why you think this is too much?

@ObserverOfTime
Copy link
Member

ObserverOfTime commented Apr 22, 2024

16 files for a build system that very few people use.

@zadlg
Copy link
Author

zadlg commented Apr 22, 2024

16 files for a build system that very few people use.

I'm not going to debate Bazel's popularity, because it's pretty hard to get numbers.

I understand your feeling about having 16 files exclusively for a build system. However, since the project is quite ""simple"" (it doesn't have tons of external dependencies), I think it would be easy to maintain it.

I was inspired by what was done for LLVM's monorepo. I made a directory to self-contain everything related to Bazel (/bazel), so that it does not ""pollute"" the source tree.
About maintaining it, I suggest the following (again, inspired by what was done for LLVM):

  • the Bazel based build system can be broken at anytime, meaning that people who contribute to tree-sitter don't have to care about Bazel.
  • from time to time, someone updates/fixes the Bazel build system.

I've added new GitHub jobs, I'll make them non-blocking to ensure that non-Bazel-aware contributors won't be blocked.

What do you think?

@ObserverOfTime
Copy link
Member

In my opinion, if we were to add another (optional) build system it should be CMake (just one or two files).

@zadlg
Copy link
Author

zadlg commented Apr 22, 2024

It seems that we cannot flag a job as non-blocking, we can only use continue-on-error, which does not make sense here.

However, an admin can flag these jobs as non-required.

@zadlg
Copy link
Author

zadlg commented Apr 22, 2024

In my opinion, if we were to add another (optional) build system it should be CMake (just one or two files).

I can also add an optional CMake-based build system for tree-sitter if you want.
The reason I added Bazel in the first place is that I use it for my personal projects and for a few projects in the company I work for.
In other words, I'm not trying to add Bazel because it's popular (or not).

@ObserverOfTime
Copy link
Member

Downstream projects can use whatever build system they want. It doesn't need to be added here.

@zadlg
Copy link
Author

zadlg commented Apr 22, 2024

Downstream projects can use whatever build system they want. It doesn't need to be added here.

I agree.

I've decided to make this PR in case other people want to use tree-sitter without them writing the build system themselves. As I wrote in my commit's description, the idea is to be able to easily integrate tree-sitter using http_archive, this is quite common in Bazel.
(The ultimate goal being having tree-sitter in the Bazel Central Registry).

This commit introduces a Bazel-based build system for the core library
of `tree-sitter`.

In order not to "pollute" the existing source directories, an overlay directory
has been created at `bazel/`.

People can build the `tree-sitter` C API by doing:

```
$ cd bazel/
$ bazel build @tree-sitter
```

The following two flags are available to enable or disable specific features:

 - `--@tree-sitter//build_config:wasm=<true|false>`: enable WASM. **not yet supported.**
 - `--@tree-sitter//build_config:hide_symbols=<true|false>`: if enabled, do not expose tree-sitter symbols.

Bazel users can import `tree-sitter` into their existing Bazel workspace by
adding the following to their `WORKSPACE` file:

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

# The git commit.
_COMMIT = ""

# SHA-256 digest of the archive.
_SHA256SUM = ""
http_archive(
    name = "tree-sitter-raw",
    sha256 = _SHA256SUM,
    urls = [
        "https://github.com/tree-sitter/tree-sitter/archive/{commit}.tar.gz".format(
            commit = _COMMIT,
        ),
    ],
    strip_prefix = "tree-sitter-{commit}".format(
        commit = _COMMIT,
    ),
)

load("@tree-sitter-raw//bazel:configure.bzl", "tree_sitter_configure")

# Configures the repository `tree-sitter`.
tree_sitter_configure(name = "tree-sitter")

# Fetches `tree-sitter` Bazel dependencies.
load("@tree-sitter//:repositories.bzl", "tree_sitter_repositories")

tree_sitter_repositories()

# Loads `tree-sitter` Bazel dependencies.
load("@tree-sitter//:deps.bzl", "tree_sitter_dependencies")

tree_sitter_dependencies()
```

Then, users can link their libraries/binaries against tree-sitter:

```starlark
cc_library(
    name = "my-parser",
    srcs = ["my-parser.cc"],
    deps = ["@tree-sitter"],
)
```
@maxbrunsfeld
Copy link
Contributor

Hi, this is great work but I don’t want to maintain this in this repository.

@github-actions github-actions bot removed the request for review from maxbrunsfeld April 22, 2024 15:33
@zadlg
Copy link
Author

zadlg commented Apr 22, 2024

Hi, this is great work but I don’t want to maintain this in this repository.

I understand. Thanks anyway for your reply.

zadlg added a commit to zadlg/bazel-central-registry that referenced this pull request Apr 23, 2024
See [`tree-sitter-bazel`] and [`tree-sitter-bazel` v0.22.5].

I attempted to integrate Bazel into the original repo (see tree-sitter/tree-sitter#3312),
but tree-sitter maintainers are not willing to do so, so I decided to create
my own repo for this purpose.

[`tree-sitter-bazel`]: https://github.com/zadlg/tree-sitter-bazel
[`tree-sitter-bazel` v0.22.5]: https://github.com/zadlg/tree-sitter-bazel/releases/tag/v0.22.5
zadlg added a commit to zadlg/bazel-central-registry that referenced this pull request Apr 23, 2024
See [`tree-sitter-bazel`] and [`tree-sitter-bazel` v0.22.5].

I attempted to integrate Bazel into the original repo (see tree-sitter/tree-sitter#3312),
but tree-sitter maintainers are not willing to do so, so I decided to create
my own repo for this purpose.

[`tree-sitter-bazel`]: https://github.com/zadlg/tree-sitter-bazel
[`tree-sitter-bazel` v0.22.5]: https://github.com/zadlg/tree-sitter-bazel/releases/tag/v0.22.5
zadlg added a commit to zadlg/bazel-central-registry that referenced this pull request Apr 23, 2024
See [`tree-sitter-bazel`] and [`tree-sitter-bazel` v0.22.5].

I attempted to integrate Bazel into the original repo (see tree-sitter/tree-sitter#3312),
but tree-sitter maintainers are not willing to do so, so I decided to create
my own repo for this purpose.

[`tree-sitter-bazel`]: https://github.com/zadlg/tree-sitter-bazel
[`tree-sitter-bazel` v0.22.5]: https://github.com/zadlg/tree-sitter-bazel/releases/tag/v0.22.5
zadlg added a commit to zadlg/bazel-central-registry that referenced this pull request Apr 23, 2024
See [`tree-sitter-bazel`] and [`tree-sitter-bazel` v0.22.5].

I attempted to integrate Bazel into the original repo (see tree-sitter/tree-sitter#3312),
but tree-sitter maintainers are not willing to do so, so I decided to create
my own repo for this purpose.

[`tree-sitter-bazel`]: https://github.com/zadlg/tree-sitter-bazel
[`tree-sitter-bazel` v0.22.5]: https://github.com/zadlg/tree-sitter-bazel/releases/tag/v0.22.5
zadlg added a commit to zadlg/bazel-central-registry that referenced this pull request Apr 23, 2024
See [`tree-sitter-bazel`] and [`tree-sitter-bazel` v0.22.5].

I attempted to integrate Bazel into the original repo (see tree-sitter/tree-sitter#3312),
but tree-sitter maintainers are not willing to do so, so I decided to create
my own repo for this purpose.

[`tree-sitter-bazel`]: https://github.com/zadlg/tree-sitter-bazel
[`tree-sitter-bazel` v0.22.5]: https://github.com/zadlg/tree-sitter-bazel/releases/tag/v0.22.5
meteorcloudy pushed a commit to bazelbuild/bazel-central-registry that referenced this pull request Apr 23, 2024
Add tree-sitter-bazel 0.22.5.

See [`tree-sitter-bazel`] and [`tree-sitter-bazel` v0.22.5].

I attempted to integrate Bazel into the original repo (see
tree-sitter/tree-sitter#3312),
but tree-sitter maintainers are not willing to do so, so I decided to
create
my own repo for this purpose.

[`tree-sitter-bazel`]: https://github.com/zadlg/tree-sitter-bazel
[`tree-sitter-bazel` v0.22.5]:
https://github.com/zadlg/tree-sitter-bazel/releases/tag/v0.22.5
@zadlg
Copy link
Author

zadlg commented Apr 23, 2024

Quick note: I've put the overlay in a repository: https://github.com/zadlg/tree-sitter-bazel/
and I've submitted a Bazel module called tree-sitter-bazel: https://registry.bazel.build/modules/tree-sitter-bazel.

@maxbrunsfeld
Copy link
Contributor

Awesome, thank you @zadlg !

aiuto pushed a commit to aiuto/bazel-central-registry that referenced this pull request Jun 3, 2024
Add tree-sitter-bazel 0.22.5.

See [`tree-sitter-bazel`] and [`tree-sitter-bazel` v0.22.5].

I attempted to integrate Bazel into the original repo (see
tree-sitter/tree-sitter#3312),
but tree-sitter maintainers are not willing to do so, so I decided to
create
my own repo for this purpose.

[`tree-sitter-bazel`]: https://github.com/zadlg/tree-sitter-bazel
[`tree-sitter-bazel` v0.22.5]:
https://github.com/zadlg/tree-sitter-bazel/releases/tag/v0.22.5
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants