|
| 1 | +# sqlc-ydb: engine plugin + ydb-go-sdk codegen plugin for sqlc (v2 config) |
| 2 | +# |
| 3 | +# Prerequisites: |
| 4 | +# - Go 1.24+ |
| 5 | +# - protoc + protoc-gen-go (for proto). go install google.golang.org/protobuf/cmd/protoc-gen-go@latest |
| 6 | +# - go mod download (so SQLC_MOD_DIR is available for proto) |
| 7 | +# - sqlc with plugin support (for examples), on PATH |
| 8 | +# |
| 9 | +# Overrides: |
| 10 | +# BINDIR where to install plugin binaries (default: bin) |
| 11 | +# SQLC sqlc binary for examples (default: bin/sqlc if build-sqlc was run, else sqlc) |
| 12 | +# |
| 13 | +# Default: show help. Run 'make build' then 'make build-sqlc' then 'make examples' to generate code. |
| 14 | +# build-sqlc builds sqlc from ../engine-plugin (required for plugin support). |
| 15 | + |
| 16 | +REPO_ROOT := $(dir $(abspath $(lastword $(MAKEFILE_LIST)))) |
| 17 | +BINDIR ?= bin |
| 18 | +# Prefer bin/sqlc from build-sqlc when present |
| 19 | +SQLC ?= $(firstword $(wildcard $(REPO_ROOT)$(BINDIR)/sqlc) sqlc) |
| 20 | +ENGINE_PLUGIN_DIR := $(REPO_ROOT)../engine-plugin |
| 21 | + |
| 22 | +# Use codegen.proto from the sqlc module (via replace => ../engine-plugin or go get). |
| 23 | +SQLC_MOD_DIR := $(shell go list -m -f '{{.Dir}}' github.com/sqlc-dev/sqlc) |
| 24 | +SQLC_PROTOS := $(SQLC_MOD_DIR)/protos |
| 25 | +PROTO_IN := $(SQLC_PROTOS)/plugin/codegen.proto |
| 26 | +PROTO_OUT = $(REPO_ROOT)internal/codegen/pb |
| 27 | +PBOUT = $(PROTO_OUT)/codegen.pb.go |
| 28 | +ENGINE_BIN = $(BINDIR)/sqlc-engine-ydb |
| 29 | +CODEGEN_BIN = $(BINDIR)/sqlc-gen-ydb-go-sdk |
| 30 | + |
| 31 | +.PHONY: all proto build build-engine build-codegen build-sqlc examples clean help |
| 32 | + |
| 33 | +all: help |
| 34 | + |
| 35 | +help: |
| 36 | + @echo "Targets:" |
| 37 | + @echo " proto - generate codegen.pb.go from sqlc module's protos/plugin/codegen.proto (needs protoc, protoc-gen-go, go mod download)" |
| 38 | + @echo " build-engine - build sqlc-engine-ydb into $(BINDIR)/" |
| 39 | + @echo " build-codegen - build sqlc-gen-ydb-go-sdk into $(BINDIR)/ (depends on proto)" |
| 40 | + @echo " build - proto + build both plugins" |
| 41 | + @echo " build-sqlc - build sqlc from ../engine-plugin into $(BINDIR)/sqlc (needed for examples)" |
| 42 | + @echo " examples - run 'sqlc generate' in examples/authors (requires build + build-sqlc or sqlc on PATH)" |
| 43 | + @echo " clean - remove $(BINDIR)/ and generated example output" |
| 44 | + @echo "" |
| 45 | + @echo "Overrides: BINDIR=$(BINDIR) SQLC=$(SQLC)" |
| 46 | + |
| 47 | +# Generate plugin proto Go from the sqlc module's codegen.proto (via go get / replace). |
| 48 | +# Output goes to internal/codegen/pb with package pb. |
| 49 | +proto: $(PBOUT) |
| 50 | + @echo "ok: $(PBOUT)" |
| 51 | + |
| 52 | +$(PBOUT): $(PROTO_IN) |
| 53 | + @command -v protoc >/dev/null || (echo "error: protoc not found" >&2; exit 1) |
| 54 | + @command -v protoc-gen-go >/dev/null || (echo "error: protoc-gen-go not found (run: go install google.golang.org/protobuf/cmd/protoc-gen-go@latest)" >&2; exit 1) |
| 55 | + @mkdir -p $(PROTO_OUT) |
| 56 | + cd $(REPO_ROOT) && protoc -I$(SQLC_PROTOS) \ |
| 57 | + --go_out=. --go_opt=module=github.com/sqlc-dev/sqlc-engine-ydb \ |
| 58 | + --go_opt=Mplugin/codegen.proto=github.com/sqlc-dev/sqlc-engine-ydb/internal/codegen/pb \ |
| 59 | + plugin/codegen.proto |
| 60 | + @if [ ! -f "$(PBOUT)" ]; then \ |
| 61 | + echo "error: expected $(PBOUT) to be created; protoc may use different paths" >&2; \ |
| 62 | + exit 1; \ |
| 63 | + fi |
| 64 | + @echo "ok: $(PBOUT)" |
| 65 | + |
| 66 | +# Build the YDB engine plugin (parses schema + queries). |
| 67 | +build-engine: $(BINDIR) |
| 68 | + go build -o $(ENGINE_BIN) ./cmd/sqlc-engine-ydb/ |
| 69 | + @echo "ok: $(ENGINE_BIN)" |
| 70 | + |
| 71 | +# Build the ydb-go-sdk codegen plugin. Requires proto. |
| 72 | +build-codegen: proto $(BINDIR) |
| 73 | + go build -o $(CODEGEN_BIN) ./cmd/sqlc-gen-ydb-go-sdk/ |
| 74 | + @echo "ok: $(CODEGEN_BIN)" |
| 75 | + |
| 76 | +# Build both plugins. |
| 77 | +build: build-engine build-codegen |
| 78 | + @echo "Plugins ready in $(BINDIR)/" |
| 79 | + |
| 80 | +# Build sqlc from engine-plugin into BINDIR (so examples use plugin-aware sqlc). |
| 81 | +build-sqlc: $(BINDIR) |
| 82 | + @if [ ! -d "$(ENGINE_PLUGIN_DIR)" ]; then echo "error: $(ENGINE_PLUGIN_DIR) not found (clone engine-plugin next to sqlc-ydb)" >&2; exit 1; fi |
| 83 | + cd $(ENGINE_PLUGIN_DIR) && go build -o $(REPO_ROOT)$(BINDIR)/sqlc ./cmd/sqlc/ |
| 84 | + @echo "ok: $(BINDIR)/sqlc" |
| 85 | + |
| 86 | +$(BINDIR): |
| 87 | + mkdir -p $(BINDIR) |
| 88 | + |
| 89 | +# Run sqlc generate in examples/authors. Uses BINDIR/sqlc if build-sqlc was run, else SQLC from PATH. |
| 90 | +examples: build |
| 91 | + @which $(SQLC) >/dev/null 2>/dev/null || (echo "error: sqlc not found. Run 'make build-sqlc' or set SQLC to a plugin-aware sqlc binary" >&2; exit 1) |
| 92 | + cd $(REPO_ROOT)examples/authors && PATH="$(REPO_ROOT)$(BINDIR):$$PATH" $(SQLC) generate |
| 93 | + @echo "ok: examples/authors/db/ generated" |
| 94 | + |
| 95 | +clean: |
| 96 | + rm -rf $(REPO_ROOT)$(BINDIR) |
| 97 | + rm -rf $(REPO_ROOT)examples/authors/db |
| 98 | + @echo "ok: cleaned" |
0 commit comments