Skip to content

Commit

Permalink
Add better support for building and testing through Bazel. (y-scope#6)
Browse files Browse the repository at this point in the history
  • Loading branch information
davidlion authored Jun 27, 2024
1 parent 41aa8ec commit 898c8c2
Show file tree
Hide file tree
Showing 18 changed files with 1,906 additions and 61 deletions.
12 changes: 12 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,18 @@ on:
workflow_call:

jobs:
bazel-test:
strategy:
matrix:
go: ["1.22"]
os: ["ubuntu-latest", "macos-latest"]
runs-on: "${{ matrix.os }}"
steps:
- uses: "actions/checkout@v4"

- run: |
bazel test //ir:ir_test
package-test:
strategy:
matrix:
Expand Down
13 changes: 11 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,13 @@
build/
# Bazel-generated files
bazel-bin
bazel-ffi-go
bazel-out
bazel-testlogs

# C++ build generated files
cpp/.cache/
cpp/clp/
cpp/build/
**/compile_commands.json

# Submodules
cpp/clp
18 changes: 0 additions & 18 deletions BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,18 +0,0 @@
load("@rules_cc//cc:defs.bzl", "cc_library")

cc_library(
name = "libclp_ffi",
srcs = glob(["cpp/src/ffi_go/**"]) + [
],
hdrs = glob(["cpp/src/ffi_go/**/*.h"]),
includes = [
"cpp/src",
],
deps = [
"@com_github_y_scope_clp//:libclp_core",
],
copts = [
"-std=c++20",
],
visibility = ["//visibility:public"],
)
21 changes: 21 additions & 0 deletions MODULE.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
module(
name = "com_github_y_scope_clp_ffi_go",
version = "0.0.5-beta",
)

bazel_dep(name = "gazelle", version = "0.37.0")
bazel_dep(name = "rules_go", version = "0.48.1", repo_name = "io_bazel_rules_go")
bazel_dep(name = "platforms", version = "0.0.10")

go_sdk = use_extension("@io_bazel_rules_go//go:extensions.bzl", "go_sdk")
go_sdk.download(version = "1.22.4")

go_deps = use_extension("@gazelle//:extensions.bzl", "go_deps")
go_deps.from_file(go_mod = "//:go.mod")
use_repo(
go_deps,
"com_github_klauspost_compress",
)

clp_ffi_go_ext_deps = use_extension("//cpp:deps.bzl", "clp_ffi_go_ext_deps")
use_repo(clp_ffi_go_ext_deps, "com_github_y_scope_clp")
1,694 changes: 1,694 additions & 0 deletions MODULE.bazel.lock

Large diffs are not rendered by default.

68 changes: 55 additions & 13 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,17 @@ clp-ffi-go
:alt: CLP on Zulip
:target: https://yscope-clp.zulipchat.com/

This module provides Go packages to interface with `CLP's core features`__
through CLP's FFI (foreign function interface). For complete technical
documentation see the Go docs: https://pkg.go.dev/github.com/y-scope/clp-ffi-go
This module provides Go packages to interface with `CLP's core features`__ through CLP's FFI
(foreign function interface). For complete technical documentation, see the Go docs:
https://pkg.go.dev/github.com/y-scope/clp-ffi-go

__ https://github.com/y-scope/clp/tree/main/components/core

Getting started
---------------
To add the module to your project run: ``go get github.com/y-scope/clp-ffi-go``

Here's an example showing how to decode each log event containing "ERROR" from
a CLP IR byte stream.
Here's an example showing how to decode each log event containing "ERROR" from a CLP IR byte stream.

.. code:: golang
Expand Down Expand Up @@ -51,13 +50,13 @@ a CLP IR byte stream.
Building
--------
We use the ``go generate`` command to build the C++ interface to CLP's FFI code
as well as stringify ``Enum`` style types.
We use the ``go generate`` command to build the C++ interface to CLP's FFI code as well as stringify
``Enum`` style types.

1. Install requirements:

a. A C++ compiler that supports C++17
#. CMake 3.11 or higher
a. A C++ compiler that supports C++20
#. CMake 3.23 or higher
#. The Stringer tool: https://pkg.go.dev/golang.org/x/tools/cmd/stringer

- ``go install golang.org/x/tools/cmd/stringer@latest``
Expand All @@ -68,11 +67,54 @@ as well as stringify ``Enum`` style types.

Bazel support
'''''''''''''
We provide Bazel build files for each Go package in the repository, enabling
you to add any package to your `build dependency list`__ with no extra
arguments or modifications.
We provide a Bazel module and build files for each Go package in the repository.
Additionally, we provide a module extension for the FFI core component of CLP necessary to build the
native library.

__ https://github.com/bazelbuild/rules_go/blob/master/docs/go/core/rules.md#go_library-deps
The following is an example to pull in the ``ir`` Go package as a dependency through Bazel. For
development and testing it may be useful to use the commented `local_path_override`_ snippet instead
of the `git_override`_ code.

.. _local_path_override: https://bazel.build/versions/6.0.0/rules/lib/globals#local_path_override

.. _git_override: https://bazel.build/versions/6.0.0/rules/lib/globals#git_override

.. code:: bazel
# Add to MODULE.bazel
bazel_dep(name = "com_github_y_scope_clp_ffi_go", version = "<version>")
_com_github_y_scope_clp_ffi_go_commit = "<commit hash>"
archive_override(
module_name = "com_github_y_scope_clp_ffi_go",
integrity = "sha256/512-<base 64 sha of commit>",
urls = [
"https://github.com/y-scope/clp-ffi-go/archive/{}.zip".format(
_com_github_y_scope_clp_ffi_go_commit
),
],
strip_prefix = "clp-ffi-go-{}".format(_com_github_y_scope_clp_ffi_go_commit),
)
# Use as an alternative to archive_override for local development
# local_path_override(
# module_name = "com_github_y_scope_clp_ffi_go",
# path = "/home/user/clp-ffi-go",
# )
clp_ffi_go_ext_deps = use_extension(
"@com_github_y_scope_clp_ffi_go//cpp:deps.bzl", "clp_ffi_go_ext_deps"
)
use_repo(clp_ffi_go_ext_deps, "com_github_y_scope_clp")
.. code:: bazel
# Add the ffi-go package as a dependency in a BUILD.bazel file
go_binary(
name = "example",
srcs = ["example.go"],
visibility = ["//visibility:public"],
deps = ["@com_github_y_scope_clp_ffi_go//ir"],
)
Why not build with cgo?
'''''''''''''''''''''''
Expand Down
15 changes: 15 additions & 0 deletions cpp/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
cc_library(
name = "libclp_ffi_go",
srcs = glob(["src/ffi_go/**"]),
hdrs = glob(["src/ffi_go/**/*.h"]),
includes = [
"src",
],
deps = [
"@com_github_y_scope_clp//:libclp_ffi_core",
],
copts = [
"-std=c++20",
],
visibility = ["//visibility:public"],
)
88 changes: 88 additions & 0 deletions cpp/deps.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")

_build_clp_ext_com_github_nlohmann_json = """
cc_library(
name = "libjson",
srcs = ["json/single_include/nlohmann/json.hpp"],
hdrs = ["json/single_include/nlohmann/json.hpp"],
includes = ["."],
visibility = ["//visibility:public"],
)
"""

_build_com_github_y_scope_clp = """
cc_library(
name = "libclp_ffi_core",
srcs = [
"clp/components/core/src/BufferReader.cpp",
"clp/components/core/src/ReaderInterface.cpp",
"clp/components/core/src/string_utils.cpp",
"clp/components/core/src/ffi/encoding_methods.cpp",
"clp/components/core/src/ffi/ir_stream/encoding_methods.cpp",
"clp/components/core/src/ffi/ir_stream/decoding_methods.cpp",
],
hdrs = [
"clp/components/core/src/BufferReader.hpp",
"clp/components/core/src/Defs.h",
"clp/components/core/src/ErrorCode.hpp",
"clp/components/core/src/ReaderInterface.hpp",
"clp/components/core/src/string_utils.hpp",
"clp/components/core/src/string_utils.inc",
"clp/components/core/src/TraceableException.hpp",
"clp/components/core/src/type_utils.hpp",
"clp/components/core/src/ffi/encoding_methods.hpp",
"clp/components/core/src/ffi/encoding_methods.inc",
"clp/components/core/src/ffi/ir_stream/byteswap.hpp",
"clp/components/core/src/ffi/ir_stream/encoding_methods.hpp",
"clp/components/core/src/ffi/ir_stream/decoding_methods.hpp",
"clp/components/core/src/ffi/ir_stream/decoding_methods.inc",
"clp/components/core/src/ffi/ir_stream/protocol_constants.hpp",
],
includes = ["."],
copts = [
"-std=c++20",
] + select({
"@platforms//os:osx": [
"-mmacosx-version-min=10.15",
],
"//conditions:default": [],
}),
deps = [
"@clp_ext_com_github_nlohmann_json//:libjson",
],
visibility = ["//visibility:public"],
)
"""

def _clp_ext_com_github_nlohmann_json():
commit = "fec56a1a16c6e1c1b1f4e116a20e79398282626c"
commit_sha256 = "8cbda3504fd1624fbce641d3f6b884c76e5afead1fa6d6abfcbea4b734dc634b"
http_archive(
name = "clp_ext_com_github_nlohmann_json",
sha256 = commit_sha256,
urls = ["https://github.com/nlohmann/json/archive/{}.zip".format(commit)],
strip_prefix = "json-{}".format(commit),
add_prefix = "json",
build_file_content = _build_clp_ext_com_github_nlohmann_json,
)

def com_github_y_scope_clp():
_clp_ext_com_github_nlohmann_json()

commit = "084efa35b7e9a63aecc5e327b97aea2a1cef83bc"
commit_sha256 = "3aea613f00b8ca2e07803c5774a2faf8d7a315d983093eb4ce23a14a73414f72"
http_archive(
name = "com_github_y_scope_clp",
sha256 = commit_sha256,
urls = ["https://github.com/y-scope/clp/archive/{}.zip".format(commit)],
strip_prefix = "clp-{}".format(commit),
add_prefix = "clp",
build_file_content = _build_com_github_y_scope_clp,
)

def _clp_ffi_go_ext_deps_impl(_):
com_github_y_scope_clp()

clp_ffi_go_ext_deps = module_extension(
implementation = _clp_ffi_go_ext_deps_impl,
)
4 changes: 1 addition & 3 deletions ffi/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@ load("@io_bazel_rules_go//go:def.bzl", "go_library")

go_library(
name = "ffi",
srcs = [
"ffi.go",
],
srcs = glob(["*.go"], exclude=["build_*.go", "*_test.go"]),
importpath = "github.com/y-scope/clp-ffi-go/ffi",
visibility = ["//visibility:public"],
)
Expand Down
20 changes: 3 additions & 17 deletions ir/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,10 @@ load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test")

go_library(
name = "ir",
srcs = [
"cgo_defs.go",
"decoder.go",
"deserializer.go",
"encoder.go",
"ir.go",
"irerror.go",
"irerror_string.go",
"reader.go",
"serializer.go",
],
srcs = glob(["*.go"], exclude=["build_*.go", "*_test.go"]),
cgo = True,
cdeps = [
"//:libclp_ffi",
"//cpp:libclp_ffi_go",
],
importpath = "github.com/y-scope/clp-ffi-go/ir",
visibility = ["//visibility:public"],
Expand All @@ -33,11 +23,7 @@ alias(

go_test(
name = "ir_test",
srcs = [
"ir_test.go",
"reader_test.go",
"serder_test.go",
],
srcs = glob([ "*_test.go"]),
embed = [":ir"],
deps = [
"@com_github_klauspost_compress//zstd",
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
8 changes: 4 additions & 4 deletions ir/ir_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,9 +100,9 @@ func TestLogMessagesLongLogs(t *testing.T) {
func assertEndOfIr(
t *testing.T,
reader io.Reader,
irreader *Reader,
irReader *Reader,
) {
_, err := irreader.Read()
_, err := irReader.Read()
if EndOfIr != err {
t.Fatalf("assertEndOfIr failed got: %v", err)
}
Expand All @@ -111,10 +111,10 @@ func assertEndOfIr(
func assertIrLogEvent(
t *testing.T,
reader io.Reader,
irreader *Reader,
irReader *Reader,
event ffi.LogEvent,
) {
log, err := irreader.Read()
log, err := irReader.Read()
if nil != err {
t.Fatalf("Reader.Read failed: %v", err)
}
Expand Down
6 changes: 2 additions & 4 deletions search/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,10 @@ load("@io_bazel_rules_go//go:def.bzl", "go_library")

go_library(
name = "search",
srcs = [
"wildcard_query.go",
],
srcs = glob(["*.go"], exclude=["build_*.go", "*_test.go"]),
cgo = True,
cdeps = [
"//:libclp_ffi",
"//cpp:libclp_ffi_go",
],
importpath = "github.com/y-scope/clp-ffi-go/search",
visibility = ["//visibility:public"],
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.

0 comments on commit 898c8c2

Please sign in to comment.