code_generation.bzl 3.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. # Copyright 2018 The Kubernetes Authors.
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. load("//build:kazel_generated.bzl", "go_prefix", "tags_values_pkgs")
  15. load("//build:openapi.bzl", "openapi_vendor_prefix")
  16. load("@io_k8s_repo_infra//defs:go.bzl", "go_genrule")
  17. def bazel_go_library(pkg):
  18. """Returns the Bazel label for the Go library for the provided package.
  19. This is intended to be used with the //build:kazel_generated.bzl tag dictionaries; for example:
  20. load("//build:kazel_generated.bzl", "tags_values_pkgs")
  21. some_rule(
  22. ...
  23. deps = [bazel_go_library(pkg) for pkg in tags_values_pkgs["openapi-gen"]["true"]],
  24. ...
  25. )
  26. """
  27. return "//%s:go_default_library" % pkg
  28. def go_pkg(pkg):
  29. """Returns the full Go package name for the provided workspace-relative package.
  30. This is suitable to pass to tools depending on the Go build library.
  31. If any packages are in staging/src, they are remapped to their intended path in vendor/.
  32. This is intended to be used with the //build:kazel_generated.bzl tag dictionaries.
  33. For example:
  34. load("//build:kazel_generated.bzl", "tags_values_pkgs")
  35. genrule(
  36. ...
  37. cmd = "do something --pkgs=%s" % ",".join([go_pkg(pkg) for pkg in tags_values_pkgs["openapi-gen"]["true"]]),
  38. ...
  39. )
  40. """
  41. return go_prefix + "/" + pkg.replace("staging/src/", "vendor/", maxsplit = 1)
  42. def openapi_deps():
  43. deps = [
  44. "//vendor/github.com/go-openapi/spec:go_default_library",
  45. "//vendor/k8s.io/kube-openapi/pkg/common:go_default_library",
  46. ]
  47. deps.extend([bazel_go_library(pkg) for pkg in tags_values_pkgs["openapi-gen"]["true"]])
  48. return deps
  49. def gen_openapi(outs, output_pkg):
  50. """Calls openapi-gen to produce the zz_generated.openapi.go file,
  51. which should be provided in outs.
  52. output_pkg should be set to the full go package name for this generated file.
  53. """
  54. go_genrule(
  55. name = "zz_generated.openapi",
  56. srcs = ["//" + openapi_vendor_prefix + "hack/boilerplate:boilerplate.generatego.txt"],
  57. outs = outs,
  58. # In order for vendored dependencies to be imported correctly,
  59. # the generator must run from the repo root inside the generated GOPATH.
  60. # All of bazel's $(location)s are relative to the original working directory, however.
  61. cmd = " ".join([
  62. "cd $$GOPATH/src/" + go_prefix + ";",
  63. "$$GO_GENRULE_EXECROOT/$(location //vendor/k8s.io/kube-openapi/cmd/openapi-gen)",
  64. "--v 1",
  65. "--logtostderr",
  66. "--go-header-file $$GO_GENRULE_EXECROOT/$(location //" + openapi_vendor_prefix + "hack/boilerplate:boilerplate.generatego.txt)",
  67. "--output-file-base zz_generated.openapi",
  68. "--output-package " + output_pkg,
  69. "--report-filename tmp_api_violations.report",
  70. "--input-dirs " + ",".join([go_pkg(pkg) for pkg in tags_values_pkgs["openapi-gen"]["true"]]),
  71. "&& cp $$GOPATH/src/" + output_pkg + "/zz_generated.openapi.go $$GO_GENRULE_EXECROOT/$(location :zz_generated.openapi.go)",
  72. "&& rm tmp_api_violations.report",
  73. ]),
  74. go_deps = openapi_deps(),
  75. tools = ["//vendor/k8s.io/kube-openapi/cmd/openapi-gen"],
  76. )