Makefile 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. empty :=
  2. space := $(empty) $(empty)
  3. PACKAGE := github.com/envoyproxy/protoc-gen-validate
  4. # protoc-gen-go parameters for properly generating the import path for PGV
  5. VALIDATE_IMPORT := Mvalidate/validate.proto=${PACKAGE}/validate
  6. GO_IMPORT_SPACES := ${VALIDATE_IMPORT},\
  7. Mgoogle/protobuf/any.proto=google.golang.org/protobuf/types/known/anypb,\
  8. Mgoogle/protobuf/duration.proto=google.golang.org/protobuf/types/known/durationpb,\
  9. Mgoogle/protobuf/struct.proto=google.golang.org/protobuf/types/known/structpb,\
  10. Mgoogle/protobuf/timestamp.proto=google.golang.org/protobuf/types/known/timestamppb,\
  11. Mgoogle/protobuf/wrappers.proto=google.golang.org/protobuf/types/known/wrapperspb,\
  12. Mgoogle/protobuf/descriptor.proto=google.golang.org/protobuf/types/descriptorpb
  13. GO_IMPORT:=$(subst $(space),,$(GO_IMPORT_SPACES))
  14. .PHONY: build
  15. build: validate/validate.pb.go
  16. # generates the PGV binary and installs it into $$GOPATH/bin
  17. go install .
  18. .PHONY: bazel
  19. bazel:
  20. # generate the PGV plugin with Bazel
  21. bazel build //tests/...
  22. .PHONY: build_generation_tests
  23. build_generation_tests:
  24. bazel build //tests/generation/...
  25. .PHONY: gazelle
  26. gazelle:
  27. # runs gazelle against the codebase to generate Bazel BUILD files
  28. bazel run //:gazelle -- update-repos -from_file=go.mod -prune -to_macro=dependencies.bzl%go_third_party
  29. bazel run //:gazelle
  30. .PHONY: lint
  31. lint: bin/golint bin/shadow
  32. # lints the package for common code smells
  33. test -z "$(gofmt -d -s ./*.go)" || (gofmt -d -s ./*.go && exit 1)
  34. # golint -set_exit_status
  35. # check for variable shadowing
  36. go vet -vettool=$(shell pwd)/bin/shadow ./...
  37. # lints the python code for style enforcement
  38. flake8 --config=python/setup.cfg python/protoc_gen_validate/validator.py
  39. isort --check-only python/protoc_gen_validate/validator.py
  40. bin/shadow:
  41. GOBIN=$(shell pwd)/bin go install golang.org/x/tools/go/analysis/passes/shadow/cmd/shadow
  42. bin/golint:
  43. GOBIN=$(shell pwd)/bin go install golang.org/x/lint/golint
  44. bin/protoc-gen-go:
  45. GOBIN=$(shell pwd)/bin go install google.golang.org/protobuf/cmd/protoc-gen-go
  46. bin/harness:
  47. cd tests && go build -o ../bin/harness ./harness/executor
  48. .PHONY: harness
  49. harness: testcases tests/harness/go/harness.pb.go tests/harness/go/main/go-harness tests/harness/cc/cc-harness bin/harness
  50. # runs the test harness, validating a series of test cases in all supported languages
  51. ./bin/harness -go -cc
  52. .PHONY: bazel-tests
  53. bazel-tests:
  54. # Runs all tests with Bazel
  55. bazel test //tests/... --test_output=errors
  56. .PHONY: example-workspace
  57. example-workspace:
  58. # Run all tests in the example workspace
  59. cd example-workspace && bazel test //... --test_output=errors
  60. .PHONY: testcases
  61. testcases: bin/protoc-gen-go
  62. # generate the test harness case protos
  63. rm -r tests/harness/cases/go || true
  64. mkdir tests/harness/cases/go
  65. rm -r tests/harness/cases/other_package/go || true
  66. mkdir tests/harness/cases/other_package/go
  67. # protoc-gen-go makes us go a package at a time
  68. cd tests/harness/cases/other_package && \
  69. protoc \
  70. -I . \
  71. -I ../../../.. \
  72. --go_out="module=${PACKAGE}/tests/harness/cases/other_package/go,${GO_IMPORT}:./go" \
  73. --plugin=protoc-gen-go=$(shell pwd)/bin/protoc-gen-go \
  74. --validate_out="module=${PACKAGE}/tests/harness/cases/other_package/go,lang=go:./go" \
  75. ./*.proto
  76. cd tests/harness/cases && \
  77. protoc \
  78. -I . \
  79. -I ../../.. \
  80. --go_out="module=${PACKAGE}/tests/harness/cases/go,Mtests/harness/cases/other_package/embed.proto=${PACKAGE}/tests/harness/cases/other_package/go,${GO_IMPORT}:./go" \
  81. --plugin=protoc-gen-go=$(shell pwd)/bin/protoc-gen-go \
  82. --validate_out="module=${PACKAGE}/tests/harness/cases/go,lang=go,Mtests/harness/cases/other_package/embed.proto=${PACKAGE}/tests/harness/cases/other_package/go:./go" \
  83. ./*.proto
  84. validate/validate.pb.go: bin/protoc-gen-go validate/validate.proto
  85. protoc -I . \
  86. --plugin=protoc-gen-go=$(shell pwd)/bin/protoc-gen-go \
  87. --go_opt=paths=source_relative \
  88. --go_out="${GO_IMPORT}:." validate/validate.proto
  89. tests/harness/go/harness.pb.go: bin/protoc-gen-go tests/harness/harness.proto
  90. # generates the test harness protos
  91. cd tests/harness && protoc -I . \
  92. --plugin=protoc-gen-go=$(shell pwd)/bin/protoc-gen-go \
  93. --go_out="module=${PACKAGE}/tests/harness/go,${GO_IMPORT}:./go" harness.proto
  94. tests/harness/go/main/go-harness:
  95. # generates the go-specific test harness
  96. cd tests && go build -o ./harness/go/main/go-harness ./harness/go/main
  97. tests/harness/cc/cc-harness: tests/harness/cc/harness.cc
  98. # generates the C++-specific test harness
  99. # use bazel which knows how to pull in the C++ common proto libraries
  100. bazel build //tests/harness/cc:cc-harness
  101. cp bazel-bin/tests/harness/cc/cc-harness $@
  102. chmod 0755 $@
  103. tests/harness/java/java-harness:
  104. # generates the Java-specific test harness
  105. mvn -q -f java/pom.xml clean package -DskipTests
  106. .PHONY: prepare-python-release
  107. prepare-python-release:
  108. cp validate/validate.proto python/
  109. cp LICENSE python/
  110. .PHONY: python-release
  111. python-release: prepare-python-release
  112. rm -rf python/dist
  113. python3.8 -m build --no-isolation --sdist python
  114. # the below command should be identical to `python3.8 -m build --wheel`
  115. # however that returns mysterious `error: could not create 'build': File exists`.
  116. # setuptools copies source and data files to a temporary build directory,
  117. # but why there's a collision or why setuptools stopped respecting the `build_lib` flag is unclear.
  118. # As a workaround, we build a source distribution and then separately build a wheel from it.
  119. python3.8 -m pip wheel --wheel-dir python/dist --no-deps python/dist/*
  120. python3.8 -m twine upload --verbose --skip-existing --repository ${PYPI_REPO} --username "__token__" --password ${PGV_PYPI_TOKEN} python/dist/*
  121. # Run during CI; this checks that the checked-in generated code matches the generated version.
  122. .PHONY: check-generated
  123. check-generated:
  124. for f in validate/validate.pb.go ; do \
  125. mv $$f $$f.original ; \
  126. make $$f ; \
  127. mv $$f $$f.generated ; \
  128. cp $$f.original $$f ; \
  129. diff $$f.original $$f.generated ; \
  130. done
  131. .PHONY: ci
  132. ci: lint bazel testcases bazel-tests build_generation_tests example-workspace check-generated
  133. .PHONY: clean
  134. clean:
  135. (which bazel && bazel clean) || true
  136. rm -f \
  137. bin/protoc-gen-go \
  138. bin/harness \
  139. tests/harness/cc/cc-harness \
  140. tests/harness/go/main/go-harness \
  141. tests/harness/go/harness.pb.go
  142. rm -rf \
  143. tests/harness/cases/go \
  144. tests/harness/cases/other_package/go
  145. rm -rf \
  146. python/dist \
  147. python/*.egg-info