Skip to content

Commit 953ab4a

Browse files
committed
bazel/sqlite3: add shared-lib wrapper for mattn/go-sqlite3 consumers
Introduce the building blocks required to redirect github.com/mattn/go-sqlite3 to the agent's own libsqlite3, without yet enabling the migration: - deps/sqlite3.BUILD.bazel: cherry-pick the feature defines that the binding applies in its bundled-amalgamation build (sqlite3.go:13-23) so the shared libsqlite3 is feature-equivalent. Add :libsqlite3_headers, :_libsqlite3_shared (cc_import of the cc_shared_library output), and :libsqlite3_dynamic, the cdeps-consumable wrapper. Pattern mirrors //rtloader:rtloader_dynamic. - bazel/rules/go_sqlite3/go_sqlite3_library.bzl: a go_library wrapper that enables cgo and injects @sqlite3//:libsqlite3_dynamic as a cdep, plus a TODO for the install-time rpath fix-up that will be needed once the agent binary is built with Bazel. - bazel/rules/go_sqlite3/BUILD.bazel: alias forwarding to @sqlite3//:libsqlite3_dynamic. @sqlite3 isn't visible from the bzlmod context of the gazelle-generated go-sqlite3 repo; the @@//... label resolves regardless and insulates the macro from canonical-name churn. No behavior change yet: these targets are inert until the gazelle override that emits go_sqlite3_library lands in a follow-up commit.
1 parent 44abfbe commit 953ab4a

3 files changed

Lines changed: 66 additions & 1 deletion

File tree

bazel/rules/go_sqlite3/BUILD.bazel

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# Indirection so go_sqlite3_library can reference libsqlite3_dynamic via a
2+
# main-module label. @sqlite3 isn't visible from inside the bzlmod context of
3+
# the gazelle-generated github.com/mattn/go-sqlite3 repo; @@//... always
4+
# resolves and insulates the macro from canonical-name churn.
5+
alias(
6+
name = "libsqlite3_dynamic",
7+
actual = "@sqlite3//:libsqlite3_dynamic",
8+
visibility = ["//visibility:public"],
9+
)
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
"""go_library wrapper that links github.com/mattn/go-sqlite3 against //deps:sqlite3."""
2+
3+
load("@rules_go//go:def.bzl", _go_library = "go_library")
4+
5+
def go_sqlite3_library(**kwargs):
6+
"""Wraps go_library so the Gazelle-generated rule for mattn/go-sqlite3
7+
enables cgo and pulls in @sqlite3//:libsqlite3_dynamic as a cdep.
8+
9+
Wired in via gazelle:map_kind on github.com/mattn/go-sqlite3 (see
10+
deps/go.MODULE.bazel). The libsqlite3 Go build tag is enabled globally
11+
in .bazelrc, so sqlite3_libsqlite3.go defines USE_LIBSQLITE3 and the
12+
bundled amalgamation in sqlite3-binding.c is a no-op TU.
13+
14+
TODO(agent-build): when the agent binary is built with Bazel, its rpath
15+
will point into bazel-bin/_solib_local/... and won't resolve at install
16+
time. Apply the same packaging path used for cpython: pull
17+
@sqlite3//:sqlite3_pkg into the binary's packaging deps and run the
18+
bazel/rules/rewrite_rpath rule post-link to retarget to the install-tree
19+
libsqlite3.so location.
20+
21+
Args:
22+
**kwargs: forwarded to go_library. cdeps is extended (not replaced) so
23+
Gazelle-emitted entries are preserved.
24+
"""
25+
kwargs["cgo"] = True
26+
cdeps = kwargs.pop("cdeps", [])
27+
label = "@@//bazel/rules/go_sqlite3:libsqlite3_dynamic"
28+
if label not in cdeps:
29+
cdeps = cdeps + [label]
30+
_go_library(cdeps = cdeps, **kwargs)

deps/sqlite3.BUILD.bazel

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
load("@@//bazel/rules/dd_packaging:dd_cc_packaged.bzl", "dd_cc_packaged")
2-
load("@rules_cc//cc:defs.bzl", "cc_library", "cc_shared_library")
2+
load("@rules_cc//cc:defs.bzl", "cc_import", "cc_library", "cc_shared_library")
33
load("@rules_license//rules:license.bzl", "license")
44

55
package(
@@ -26,10 +26,20 @@ cc_library(
2626
srcs = ["sqlite3.c"],
2727
hdrs = _SQLITE3_PUBLIC_HEADERS,
2828
includes = ["."],
29+
# SQLITE_* defines mirror mattn/go-sqlite3's bundled-amalgamation build
30+
# (sqlite3.go:13-23) so this shared library is feature-equivalent to the
31+
# binding's default flavor.
2932
copts = [
3033
"-Wall",
34+
"-DSQLITE_DEFAULT_WAL_SYNCHRONOUS=1",
35+
"-DSQLITE_ENABLE_FTS3",
36+
"-DSQLITE_ENABLE_FTS3_PARENTHESIS",
3137
"-DSQLITE_ENABLE_MATH_FUNCTIONS",
38+
"-DSQLITE_ENABLE_RTREE",
39+
"-DSQLITE_ENABLE_UPDATE_DELETE_LIMIT",
40+
"-DSQLITE_OMIT_DEPRECATED",
3241
"-DSQLITE_THREADSAFE=1",
42+
"-DSQLITE_TRACE_SIZE_LIMIT=15",
3343
"-O2",
3444
],
3545
linkstatic = True,
@@ -42,6 +52,22 @@ cc_shared_library(
4252
visibility = ["//visibility:public"],
4353
)
4454

55+
cc_import(
56+
name = "_libsqlite3_shared",
57+
shared_library = ":sqlite3",
58+
)
59+
60+
# This target is meant to be used as a cdeps for go code that relies on sqlite3
61+
# More specifically, it's intended to avoid rebuilding the sqlite3 amalgamation
62+
# in github.com/mattn/go-sqlite3
63+
cc_library(
64+
name = "libsqlite3_dynamic",
65+
hdrs = _SQLITE3_PUBLIC_HEADERS,
66+
includes = ["."],
67+
visibility = ["//visibility:public"],
68+
deps = [":_libsqlite3_shared"],
69+
)
70+
4571
dd_cc_packaged(
4672
name = "sqlite3_pkg",
4773
input = ":sqlite3",

0 commit comments

Comments
 (0)