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

Clean up and update Bazel build files #1223

Merged
merged 3 commits into from
Mar 22, 2025
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
12 changes: 11 additions & 1 deletion .github/workflows/build_and_test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ jobs:
- uses: actions/checkout@v4
- uses: actions/setup-python@v5
with:
python-version: '3.9'
python-version: "3.9"
- name: Build
run: |
pip install build
Expand Down Expand Up @@ -89,3 +89,13 @@ jobs:
- name: Test
run: |
bazelisk test --lockfile_mode=error //...

bazel_use_module:
name: Build Bazel example
runs-on: "ubuntu-22.04"
steps:
- uses: actions/checkout@v4
- name: Build
run: |
cd examples/bazel
bazelisk build --lockfile_mode=off //...
12 changes: 2 additions & 10 deletions MODULE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,5 @@ module(
version = "0.21.0-rc2",
)

bazel_dep(name = "googletest", version = "1.11.0", repo_name = "com_google_googletest")
bazel_dep(name = "rules_python", version = "0.40.0")

build_defs = use_extension("//tools/build_defs:extensions.bzl", "build_defs")
use_repo(
build_defs,
"default_python3_headers",
)

register_toolchains("//platform_defs:default_python3_toolchain")
bazel_dep(name = "googletest", version = "1.16.0")
bazel_dep(name = "rules_python", version = "1.2.0")
478 changes: 377 additions & 101 deletions MODULE.bazel.lock

Large diffs are not rendered by default.

19 changes: 0 additions & 19 deletions WORKSPACE

This file was deleted.

4 changes: 3 additions & 1 deletion cmd/BUILD
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package(default_visibility = ["//visibility:public"])
package(default_visibility = ["//visibility:private"])

cc_library(
name = "utils",
Expand All @@ -10,6 +10,7 @@ cc_library(
cc_binary(
name = "jsonnet",
srcs = ["jsonnet.cpp"],
visibility = ["//visibility:public"],
deps = [
":utils",
"//core:libjsonnet",
Expand All @@ -19,6 +20,7 @@ cc_binary(
cc_binary(
name = "jsonnetfmt",
srcs = ["jsonnetfmt.cpp"],
visibility = ["//visibility:public"],
deps = [
":utils",
"//core:libjsonnet",
Expand Down
45 changes: 23 additions & 22 deletions core/BUILD
Original file line number Diff line number Diff line change
@@ -1,45 +1,46 @@
package(default_visibility = ["//visibility:public"])
package(default_visibility = ["//visibility:private"])

cc_library(
name = "libjsonnet",
srcs = [
"desugarer.cpp",
"formatter.cpp",
"lexer.cpp",
"libjsonnet.cpp",
"parser.cpp",
"pass.cpp",
"path_utils.cpp",
"static_analysis.cpp",
"string_utils.cpp",
"vm.cpp",
],
hdrs = [
"ast.h",
"desugarer.cpp",
"desugarer.h",
"formatter.cpp",
"formatter.h",
"json.h",
"lexer.cpp",
"lexer.h",
"libjsonnet.cpp",
"parser.cpp",
"parser.h",
"pass.cpp",
"pass.h",
"path_utils.cpp",
"path_utils.h",
"state.h",
"static_analysis.cpp",
"static_analysis.h",
"static_error.h",
"string_utils.cpp",
"string_utils.h",
"unicode.h",
"vm.cpp",
"vm.h",
],
includes = ["."],
linkopts = ["-lm"],
deps = [
"//include:libjsonnet",
"//include:libjsonnet_fmt",
copts = ["-I."],
implementation_deps = [
"//stdlib:std",
"//third_party/json",
"//third_party/md5:libmd5",
"//third_party/rapidyaml:ryml",
],
linkopts = ["-lm"],
visibility = ["//visibility:public"],
deps = [
"//include:libjsonnet",
"//include:libjsonnet_fmt",
],
)

cc_test(
Expand All @@ -48,7 +49,7 @@ cc_test(
deps = [
":libjsonnet",
# Note: On Ubuntu, apt-get install libgtest-dev google-mock
"@com_google_googletest//:gtest_main",
"@googletest//:gtest_main",
],
)

Expand All @@ -57,7 +58,7 @@ cc_test(
srcs = ["parser_test.cpp"],
deps = [
":libjsonnet",
"@com_google_googletest//:gtest_main",
"@googletest//:gtest_main",
],
)

Expand All @@ -66,7 +67,7 @@ cc_test(
srcs = ["libjsonnet_test.cpp"],
deps = [
":libjsonnet",
"@com_google_googletest//:gtest_main",
"@googletest//:gtest_main",
],
)

Expand All @@ -75,6 +76,6 @@ cc_test(
srcs = ["unicode_test.cpp"],
deps = [
":libjsonnet",
"@com_google_googletest//:gtest_main",
"@googletest//:gtest_main",
],
)
5 changes: 3 additions & 2 deletions cpp/BUILD
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package(default_visibility = ["//visibility:public"])
package(default_visibility = ["//visibility:private"])

cc_library(
name = "libjsonnet++",
srcs = ["libjsonnet++.cpp"],
visibility = ["//visibility:public"],
deps = [
"//core:libjsonnet",
"//include:libjsonnet++",
Expand All @@ -15,6 +16,6 @@ cc_test(
data = ["//cpp/testdata"],
deps = [
":libjsonnet++",
"@com_google_googletest//:gtest_main",
"@googletest//:gtest_main",
],
)
1 change: 1 addition & 0 deletions examples/bazel/.bazelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
common --lockfile_mode=off
1 change: 1 addition & 0 deletions examples/bazel/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
MODULE.bazel.lock
27 changes: 27 additions & 0 deletions examples/bazel/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
load("@rules_python//python:defs.bzl", "py_binary")

cc_binary(
name = "use_cpp_lib",
srcs = ["use_cpp_lib.cc"],
deps = ["@jsonnet//cpp:libjsonnet++"],
)

cc_binary(
name = "use_c_lib",
srcs = ["use_c_lib.c"],
deps = ["@jsonnet//core:libjsonnet"],
)

py_binary(
name = "use_py_module",
srcs = ["use_py_module.py"],
deps = ["@jsonnet//python:_jsonnet"],
)

genrule(
name = "use_jsonnet_cmd",
srcs = ["example.jsonnet"],
outs = ["example.out"],
cmd = "$(location @jsonnet//cmd:jsonnet) $< > $@",
tools = ["@jsonnet//cmd:jsonnet"],
)
5 changes: 5 additions & 0 deletions examples/bazel/MODULE.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Example of using jsonnet by depending on the Bazel module.

bazel_dep(name = "rules_python", version = "1.2.0")
bazel_dep(name = "jsonnet")
local_path_override(module_name = "jsonnet", path = "../..")
8 changes: 8 additions & 0 deletions examples/bazel/example.jsonnet
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
local Person(name='Alice') = {
name: name,
welcome: 'Hello ' + name + '!',
};
{
person1: Person(),
person2: Person('Bob'),
}
10 changes: 10 additions & 0 deletions examples/bazel/expect.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"person1": {
"name": "Alice",
"welcome": "Hello Alice!"
},
"person2": {
"name": "Bob",
"welcome": "Hello Bob!"
}
}
22 changes: 22 additions & 0 deletions examples/bazel/use_c_lib.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#include <libjsonnet.h>
#include <stdio.h>
#include <stdlib.h>

int main(int argc, char **argv) {
int ret = 0;
fprintf(stderr, "Jsonnet version: %s\n", jsonnet_version());
struct JsonnetVm *vm = jsonnet_make();
if (!vm) { return 1; }

int err = 0;
char *result = jsonnet_evaluate_file(vm, "example.jsonnet", &err);
if (!err) {
fprintf(stdout, "%s\n", result ? result : "-- no output --");
} else {
fprintf(stderr, "%s\n", result ? result : "-- no output --");
ret = 2;
}

jsonnet_destroy(vm);
return ret;
}
22 changes: 22 additions & 0 deletions examples/bazel/use_cpp_lib.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#include <libjsonnet++.h>
#include <iostream>

int main(int argc, char **argv) {
int ret = 0;
std::cerr << "Jsonnet version: " << jsonnet::Jsonnet::version() << std::endl;

jsonnet::Jsonnet vm;
if (!vm.init()) {
return 1;
}

std::string result;
bool ok = vm.evaluateFile("example.jsonnet", &result);
if (ok) {
std::cout << result << std::endl;
} else {
std::cerr << vm.lastError() << std::endl;
ret = 2;
}
return ret;
}
5 changes: 5 additions & 0 deletions examples/bazel/use_py_module.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import sys
import _jsonnet

result = _jsonnet.evaluate_file("example.jsonnet")
sys.stdout.write(result)
6 changes: 5 additions & 1 deletion include/BUILD
Original file line number Diff line number Diff line change
@@ -1,19 +1,23 @@
package(default_visibility = ["//visibility:public"])
package(default_visibility = ["//visibility:private"])

cc_library(
name = "libjsonnet",
hdrs = ["libjsonnet.h"],
includes = ["."],
visibility = ["//core:__pkg__"],
)

cc_library(
name = "libjsonnet_fmt",
hdrs = ["libjsonnet_fmt.h"],
includes = ["."],
visibility = ["//core:__pkg__"],
)

cc_library(
name = "libjsonnet++",
hdrs = ["libjsonnet++.h"],
includes = ["."],
visibility = ["//cpp:__pkg__"],
deps = ["//include:libjsonnet"],
)
2 changes: 2 additions & 0 deletions platform_defs/BUILD
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
load("@rules_python//python:defs.bzl", "py_runtime", "py_runtime_pair")

package(default_visibility = ["//visibility:private"])

py_runtime(
name = "python3",
interpreter_path = "/usr/bin/python3",
Expand Down
5 changes: 4 additions & 1 deletion python/BUILD
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
load("@rules_python//python:defs.bzl", "py_library", "py_test")

package(default_visibility = ["//visibility:private"])

cc_binary(
name = "_jsonnet.so",
srcs = ["_jsonnet.c"],
linkshared = 1,
deps = [
"//core:libjsonnet",
"@default_python3_headers//:headers",
"@rules_python//python/cc:current_py_cc_headers",
],
)

Expand All @@ -20,6 +22,7 @@ py_library(

py_test(
name = "_jsonnet_test",
size = "small",
srcs = ["_jsonnet_test.py"],
data = [
"testdata/basic_check.jsonnet",
Expand Down
6 changes: 3 additions & 3 deletions stdlib/BUILD
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package(default_visibility = ["//visibility:public"])
package(default_visibility = ["//visibility:private"])

filegroup(
name = "stdlib",
Expand All @@ -7,10 +7,10 @@ filegroup(

cc_library(
name = "std",
srcs = [":gen-std-jsonnet-h"],
hdrs = ["std.jsonnet.h"],
includes = ["."],
linkstatic = 1,
textual_hdrs = [":gen-std-jsonnet-h"],
visibility = ["//core:__pkg__"],
)

genrule(
Expand Down
Loading