Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -107,13 +107,38 @@ jobs:
- if: failure() && runner.debug == '1'
uses: mxschmitt/action-tmate@v3

examples-native_ext:
name: Examples / Native Ext
runs-on: ubuntu-latest
timeout-minutes: 30
strategy:
fail-fast: false
matrix:
ruby:
- 3.3.6
- jruby-9.4.9.0
defaults:
run:
working-directory: examples/native_ext
steps:
- uses: actions/checkout@v4
- uses: bazel-contrib/setup-bazel@0.10.0
with:
bazelrc: common --announce_rc --color=yes
repository-cache: examples/native_ext/MODULE.bazel
- run: echo ${{ matrix.ruby }} > .ruby-version
- run: bazel build ...
- if: failure() && runner.debug == '1'
uses: mxschmitt/action-tmate@v3

release:
name: Release
if: startsWith(github.ref, 'refs/tags/v')
needs:
- ruleset
- examples-gem
- examples-jekyll
- examples-native_ext
uses: bazel-contrib/.github/.github/workflows/release_ruleset.yaml@v7
with:
mount_bazel_caches: false
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
bazel-bin
bazel-gem
bazel-out
bazel-native_ext
bazel-rules_ruby
bazel-testlogs
2 changes: 2 additions & 0 deletions MODULE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ module(
# the versions resolved in users repositories.
bazel_dep(name = "bazel_skylib", version = "1.3.0")
bazel_dep(name = "platforms", version = "0.0.5")
bazel_dep(name = "rules_cc", version = "0.0.9")
bazel_dep(name = "rules_java", version = "7.2.0")

# Ruleset development dependencies.
bazel_dep(name = "aspect_bazel_lib", version = "2.10.0", dev_dependency = True)
Expand Down
6 changes: 6 additions & 0 deletions examples/native_ext/.bazelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Not ready for the MODULE.lock file yet, as of Bazel 7.0.0 there are still some stability issues.
common --lockfile_mode=off
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

nit: it's better to gitignore it instead. You can get some perf benefits locally by Bazel reading the lockfile from your previous work, and not share it to others


# Allow to run Bazel without pre-installed JDK.
# Docs: https://bazel.build/reference/command-line-reference#flag--java_runtime_version
build --java_runtime_version=remotejdk_11
1 change: 1 addition & 0 deletions examples/native_ext/.ruby-version
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
jruby-9.4.9.0
25 changes: 25 additions & 0 deletions examples/native_ext/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
load("@rules_cc//cc:defs.bzl", "cc_library")
load("@rules_java//java:defs.bzl", "java_library")

cc_library(
name = "ext_c",
srcs = glob([
"ext/**/*.c",
"ext/**/*.h",
]),
target_compatible_with = select({
"@ruby//engine:ruby": [],
"//conditions:default": ["@platforms//:incompatible"],
}),
deps = ["@ruby//:headers"],
)

java_library(
name = "ext_java",
srcs = glob(["ext/**/*.java"]),
target_compatible_with = select({
"@ruby//engine:jruby": [],
"//conditions:default": ["@platforms//:incompatible"],
}),
deps = ["@ruby//:jars"],
)
19 changes: 19 additions & 0 deletions examples/native_ext/MODULE.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
"Bazel dependencies"

bazel_dep(name = "platforms", version = "0.0.10", dev_dependency = True)
bazel_dep(name = "rules_cc", version = "0.1.0", dev_dependency = True)
bazel_dep(name = "rules_java", version = "8.7.1", dev_dependency = True)
bazel_dep(name = "rules_ruby", version = "0.0.0", dev_dependency = True)
local_path_override(
module_name = "rules_ruby",
path = "../..",
)

ruby = use_extension("@rules_ruby//ruby:extensions.bzl", "ruby")
ruby.toolchain(
name = "ruby",
version_file = "//:.ruby-version",
)
use_repo(ruby, "ruby", "ruby_toolchains")

register_toolchains("@ruby_toolchains//:all")
9 changes: 9 additions & 0 deletions examples/native_ext/ext/c/native_ext.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#include "native_ext.h"

VALUE rb_mNativeExt;

RUBY_FUNC_EXPORTED void
Init_native_ext(void)
{
rb_mNativeExt = rb_define_module("NativeExt");
}
6 changes: 6 additions & 0 deletions examples/native_ext/ext/c/native_ext.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#ifndef NATIVE_EXT_H
#define NATIVE_EXT_H 1

#include "ruby.h"

#endif /* NATIVE_EXT_H */
8 changes: 8 additions & 0 deletions examples/native_ext/ext/java/Dummy.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import org.jruby.*;
import org.jruby.anno.JRubyClass;

@JRubyClass(name = "Dummy")
public class Dummy {
public Dummy() {
}
}
10 changes: 10 additions & 0 deletions ruby/private/download.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,15 @@ def _rb_download_impl(repository_ctx):
# https://www.graalvm.org/dev/reference-manual/ruby/UTF8Locale/
env.update({"LANG": "en_US.UTF-8"})

includes = []
if repository_ctx.path("dist/include").exists:
for hdrdir in repository_ctx.path("dist/include").readdir():
if hdrdir.is_dir:
includes.append("dist/include/%s" % hdrdir.basename)
for archdir in hdrdir.readdir():
if archdir.is_dir and archdir.basename != "ruby":
includes.append("dist/include/%s/%s" % (hdrdir.basename, archdir.basename))

repository_ctx.template(
"BUILD",
repository_ctx.attr._build_tpl,
Expand All @@ -122,6 +131,7 @@ def _rb_download_impl(repository_ctx):
"{ruby_binary_name}": ruby_binary_name,
"{gem_binary_name}": gem_binary_name,
"{env}": repr(env),
"{includes}": repr(includes),
},
)

Expand Down
19 changes: 19 additions & 0 deletions ruby/private/download/BUILD.tpl
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
load("@rules_cc//cc:defs.bzl", "cc_library")
load("@rules_java//java:defs.bzl", "java_import")
load("@rules_ruby//ruby:defs.bzl", "rb_binary")
load("@rules_ruby//ruby:toolchain.bzl", "rb_toolchain")

Expand All @@ -16,6 +18,23 @@ rb_binary(
main = ":ruby_file",
)

cc_library(
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

nit: there's some flag in bazel forcing these symbols to be loaded from a ruleset rather than using the now-deprecated native symbols. If a user turns on that flag I think this will be broken for them.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I've added an explicit dependency on rules_cc and rules_java in 0f52ccb, I hope that makes sense. Not sure what is the lowest version I could use so I looked at what other rulesets use.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

I think this is OK but it feels a bit undesirable that you're forced into carrying these CC/Java toolchain deps even if you have no interest in using them. Don't have any good ideas to avoid it though.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

That's why I originally didn't add those and used native rules. @alexeagle Can you advise what would be better here?

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

You don't really have a choice. This repo wants to expose the providers for interop with users repository, and providers are symbols rather than strings, so they have to be loaded from the same place.

We'll hope that rules_java and rules_cc are well-maintained so there aren't breaking changes that mean your users are sensitive to the version you pick. In bzlmod you can pick something old, since you're really picking a lower-bound; the MVS algorithm means users can still resolve to something newer.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

For both rulesets, I used > 1-year-old releases and crossed-check what other dependents do (e.g. rules_python, rules_kotlin).

name = "headers",
hdrs = glob(
["dist/include/**/*.h"],
allow_empty = True,
),
includes = {includes},
)

java_import(
name = "jars",
jars = glob(
["dist/lib/**/*.jar"],
allow_empty = True,
),
)

rb_toolchain(
name = "toolchain",
bundle = select({
Expand Down