|
| 1 | +# Licensed under the Apache License, Version 2.0 (the "License") |
| 2 | + |
| 3 | +# Plugin name. |
| 4 | +name := protoc-gen-validate |
| 5 | + |
| 6 | +# Root dir returns absolute path of current directory. It has a trailing "/". |
| 7 | +root_dir := $(dir $(abspath $(lastword $(MAKEFILE_LIST)))) |
| 8 | + |
| 9 | +# Local cache directory. |
| 10 | +cache_dir := $(root_dir).cache |
| 11 | + |
| 12 | +# Directory of Go tools. |
| 13 | +go_tools_dir := $(cache_dir)/tools/go |
| 14 | + |
| 15 | +# Directory of prepackaged tools (e.g. protoc). |
| 16 | +prepackaged_tools_dir := $(cache_dir)/tools/prepackaged |
| 17 | + |
| 18 | +# Currently we resolve Go using `which`. But more sophisticated approach is to use infer GOROOT. |
| 19 | +go := $(shell which go) |
| 20 | +goarch := $(shell $(go) env GOARCH) |
| 21 | +goexe := $(shell $(go) env GOEXE) |
| 22 | +goos := $(shell $(go) env GOOS) |
| 23 | + |
| 24 | +# The current binary location for the current runtime (goos, goarch). We install our plugin here. |
| 25 | +current_binary_path := build/$(name)_$(goos)_$(goarch) |
| 26 | +current_binary := $(current_binary_path)/$(name)$(goexe) |
| 27 | + |
| 28 | +# This makes sure protoc can access the installed plugins. |
| 29 | +export PATH := $(root_dir)$(current_binary_path):$(go_tools_dir)/bin:$(prepackaged_tools_dir)/bin:$(PATH) |
| 30 | + |
| 31 | +# The main generated file. |
| 32 | +validate_pb_go := validate/validate.pb.go |
| 33 | + |
| 34 | +# List of harness test cases. |
| 35 | +tests_harness_cases := \ |
| 36 | + /harness \ |
| 37 | + /harness/cases \ |
| 38 | + /harness/cases/other_package \ |
| 39 | + /harness/cases/yet_another_package |
| 40 | + |
| 41 | +# Include versions of tools we build on-demand |
| 42 | +include Tools.mk |
| 43 | +# This provides the "help" target. |
| 44 | +include tools/build/Help.mk |
| 45 | +# This sets some required environment variables. |
| 46 | +include tools/build/Env.mk |
| 47 | + |
| 48 | +# Path to the installed protocol buffer compiler. |
| 49 | +protoc := $(prepackaged_tools_dir)/bin/protoc |
| 50 | + |
| 51 | +# Go based tools. |
| 52 | +bazel := $(go_tools_dir)/bin/bazelisk |
| 53 | +protoc-gen-go := $(go_tools_dir)/bin/protoc-gen-go |
| 54 | + |
| 55 | +test: $(bazel) $(tests_harness_cases) ## Run tests |
| 56 | + @$(bazel) test //tests/... --test_output=errors |
| 57 | + |
| 58 | +build: $(current_binary) ## Build the plugin |
| 59 | + |
| 60 | +clean: ## Clean all build and test artifacts |
| 61 | + @rm -f $(validate_pb_go) |
| 62 | + @rm -f $(current_binary) |
| 63 | + |
| 64 | +check: ## Verify contents of last commit |
| 65 | + @# Make sure the check-in is clean |
| 66 | + @if [ ! -z "`git status -s`" ]; then \ |
| 67 | + echo "The following differences will fail CI until committed:"; \ |
| 68 | + git diff --exit-code; \ |
| 69 | + fi |
| 70 | + |
| 71 | +# This provides shortcut to various bazel related targets. |
| 72 | +sanity: bazel-build bazel-build-tests-generation bazel-test-example-workspace |
| 73 | + |
| 74 | +bazel-build: $(bazel) ## Build the plugin using bazel |
| 75 | + @$(bazel) build //:$(name) |
| 76 | + @mkdir -p $(current_binary_path) |
| 77 | + @cp -f bazel-bin/$(name)_/$(name)$(goexe) $(current_binary) |
| 78 | + |
| 79 | +bazel-build-tests-generation: $(bazel) ## Build tests generation using bazel |
| 80 | + @$(bazel) build //tests/generation/... |
| 81 | + |
| 82 | +bazel-test-example-workspace: $(bazel) ## Test example workspace using bazel |
| 83 | + @cd example-workspace && bazel test //... --test_output=errors |
| 84 | + |
| 85 | +# Generate validate/validate.pb.go from validate/validate.proto. |
| 86 | +$(validate_pb_go): $(protoc) $(protoc-gen-go) validate/validate.proto |
| 87 | + @$(protoc) -I . --go_opt=paths=source_relative --go_out=. $(filter %.proto,$^) |
| 88 | + |
| 89 | +# Build target for current binary. |
| 90 | +build/$(name)_%/$(name)$(goexe): $(validate_pb_go) |
| 91 | + @GOBIN=$(root_dir)$(current_binary_path) $(go) install . |
| 92 | + |
| 93 | +# Generate all required files for harness tests in Go. |
| 94 | +$(tests_harness_cases): $(current_binary) |
| 95 | + $(call generate-test-cases-go,tests$@) |
| 96 | + |
| 97 | +# Generates a test-case for Go. |
| 98 | +define generate-test-cases-go |
| 99 | + @cd $1 && \ |
| 100 | + mkdir -p go && \ |
| 101 | + $(protoc) \ |
| 102 | + -I . \ |
| 103 | + -I $(root_dir) \ |
| 104 | + --go_opt=paths=source_relative \ |
| 105 | + --go_out=go \ |
| 106 | + --validate_opt=paths=source_relative \ |
| 107 | + --validate_out=lang=go:go \ |
| 108 | + *.proto |
| 109 | +endef |
| 110 | + |
| 111 | +include tools/build/Installer.mk |
0 commit comments