Skip to content

Commit

Permalink
build: Improve sqlite implementation selection method.
Browse files Browse the repository at this point in the history
The build option is now compatible with go build.

To build with mattn sqlite, pass the flag "-tag sqlite_mattn". To build with modernc sqlite, pass the flag "-tag sqlite_modernc".

When building via Bazel, that flag is injected via .blazerc with the line:

"common --show_timestamps --define gotags=sqlite_modernc"

Gazelle also needs to be supplied with that tag. For gazelle gotags must be passed as:

"-build_tags tag1,tag2,..."

This is accomplished by adding a pair of config_setting() calls that translate the gotags define into a setting tag and by using a select keyed on that to pass the appropriate flag to Gazelle. Gazelle update-repos does not need to know about the go tag.

On the Makefile side, Gazelle is now called with "-args" to pass those arguments that aare variables in the Makefile, allowing the others to be chosen and appended by Blaze rules.

It was noticed that make licenses implies a full build and therefore should be done last. (At least not before gazelle.)
  • Loading branch information
jiceatscion committed Jul 25, 2023
1 parent d758060 commit 1e7051c
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 16 deletions.
3 changes: 2 additions & 1 deletion .bazelrc
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
### common options for all subcommands (help, query, build, ...)
common --show_timestamps
common --show_timestamps --define gotags=sqlite_modernc

# connect to buchgr/bazel-remote cache
# These flags can unfortunately not be specified for `common`, as they are not accepted by all subcommands (help, version, dump)
build --remote_cache=grpc://localhost:9092 --experimental_remote_downloader=grpc://localhost:9092
Expand Down
35 changes: 34 additions & 1 deletion BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,40 @@ load("@cgrindel_bazel_starlib//updatesrc:defs.bzl", "updatesrc_update_all")
# gazelle:exclude doc/**
# gazelle:exclude rules_openapi/tools/node_modules/**
# gazelle:exclude tools/lint/**/testdata/src/**
gazelle(name = "gazelle")

# This is simplistic but the complete, by-the-everchanging-bazel-book, solution
# is ludicrously complicated. Go there if and when needed.
config_setting(
name = "sqlite_mattn",
define_values = {
"gotags": "sqlite_mattn",
},
)

config_setting(
name = "sqlite_modernc",
define_values = {
"gotags": "sqlite_modernc",
},
)

gazelle(
name = "gazelle",
build_tags = select({
":sqlite_modernc": ["sqlite_modernc"],
":sqlite_mattn": ["sqlite_mattn"],
}),
command = "update",
extra_args = [
"-go_naming_convention",
"go_default_library",
],
)

gazelle(
name = "gazelle_update_repos",
command = "update-repos",
)

go_lint_config(
name = "go_lint_config",
Expand Down
11 changes: 6 additions & 5 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ build: bazel
# Use NOTPARALLEL to force correct order.
# Note: From GNU make 4.4, this still allows building any other targets (e.g. lint) in parallel.
.NOTPARALLEL: all
all: go_deps.bzl protobuf mocks gazelle licenses build antlr write_all_source_files
all: go_deps.bzl protobuf mocks gazelle build antlr write_all_source_files licenses

clean:
bazel clean
Expand All @@ -32,7 +32,8 @@ go.mod:
bazel run --config=quiet @go_sdk//:bin/go -- mod tidy

go_deps.bzl: go.mod
bazel run --config=quiet //:gazelle -- update-repos -prune -from_file=go.mod -to_macro=go_deps.bzl%go_deps
@# gazelle is run with "-args"; so our arguments are added to those from the gazelle() rule.
bazel run --verbose_failures --config=quiet //:gazelle_update_repos -- -args -prune -from_file=go.mod -to_macro=go_deps.bzl%go_deps
@# XXX(matzf): clean up; gazelle update-repose inconsistently inserts blank lines (see bazelbuild/bazel-gazelle#1088).
@sed -e '/def go_deps/,$${/^$$/d}' -i go_deps.bzl

Expand All @@ -58,10 +59,10 @@ mocks:
tools/gomocks.py

gazelle: go_deps.bzl
tools/licenses.sh
bazel run //:gazelle --config=quiet -- update -mode=$(GAZELLE_MODE) -go_naming_convention go_default_library $(GAZELLE_DIRS)
@# call gazelle with -args, which appends our arguments to those from the gazelle() rule
bazel run //:gazelle --verbose_failures --config=quiet -- -args -mode=$(GAZELLE_MODE) $(GAZELLE_DIRS)

licenses:
licenses: bazel
tools/licenses.sh

antlr:
Expand Down
9 changes: 0 additions & 9 deletions private/storage/db/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,15 +1,7 @@
load("//tools/lint:go.bzl", "go_library", "go_test")

# Gazelle cannot deal with a select() in srcs, and neither can lint.
# replace sqlite_mattn.go with sqlite_modernc.go in the list below if
# you want to use the modernc implementation.
# DO NOT remove the keep comment.
# In the deps attribute, only one of the two sqlite libraries
# is needed at any one time, but since Gazelle can't deal with it,
# just leave both. It doesn't hurt.
go_library(
name = "go_default_library",
# keep
srcs = [
"doc.go",
"errors.go",
Expand All @@ -24,7 +16,6 @@ go_library(
"//pkg/private/common:go_default_library",
"//pkg/private/prom:go_default_library",
"//pkg/private/serrors:go_default_library",
"@com_github_mattn_go_sqlite3//:go_default_library",
"@org_modernc_sqlite//:go_default_library",
],
)
Expand Down
3 changes: 3 additions & 0 deletions private/storage/db/sqlite_mattn.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@
// See the License for the specific language governing permissions and
// limitations under the License.

// go:build (sqlite_mattn)
// +build sqlite_mattn

package db

import (
Expand Down
3 changes: 3 additions & 0 deletions private/storage/db/sqlite_modernc.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@
// See the License for the specific language governing permissions and
// limitations under the License.

// go:build (sqlite_modernc)
// +build sqlite_modernc

package db

import (
Expand Down

0 comments on commit 1e7051c

Please sign in to comment.