code_generation.bzl 4.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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. count = 1
  42. return go_prefix + "/" + pkg.replace("staging/src/", "vendor/", count)
  43. def openapi_deps():
  44. deps = [
  45. "//vendor/github.com/go-openapi/spec:go_default_library",
  46. "//vendor/k8s.io/kube-openapi/pkg/common:go_default_library",
  47. ]
  48. deps.extend([bazel_go_library(pkg) for pkg in tags_values_pkgs["openapi-gen"]["true"]])
  49. return deps
  50. def applies(pkg, prefixes, default):
  51. if prefixes == None or len(prefixes) == 0:
  52. return default
  53. for prefix in prefixes:
  54. if pkg == prefix or pkg.startswith(prefix + "/"):
  55. return True
  56. return False
  57. def gen_openapi(outs, output_pkg, include_pkgs = [], exclude_pkgs = []):
  58. """Calls openapi-gen to produce the zz_generated.openapi.go file,
  59. which should be provided in outs.
  60. output_pkg should be set to the full go package name for this generated file.
  61. """
  62. go_genrule(
  63. name = "zz_generated.openapi",
  64. srcs = ["//" + openapi_vendor_prefix + "hack/boilerplate:boilerplate.generatego.txt"],
  65. outs = outs,
  66. # In order for vendored dependencies to be imported correctly,
  67. # the generator must run from the repo root inside the generated GOPATH.
  68. # All of bazel's $(location)s are relative to the original working directory, however.
  69. cmd = " ".join([
  70. "cd $$GOPATH/src/" + go_prefix + ";",
  71. "$$GO_GENRULE_EXECROOT/$(location //vendor/k8s.io/kube-openapi/cmd/openapi-gen)",
  72. "--v 1",
  73. "--logtostderr",
  74. "--go-header-file $$GO_GENRULE_EXECROOT/$(location //" + openapi_vendor_prefix + "hack/boilerplate:boilerplate.generatego.txt)",
  75. "--output-file-base zz_generated.openapi",
  76. "--output-package " + output_pkg,
  77. "--report-filename tmp_api_violations.report",
  78. "--input-dirs " + ",".join([go_pkg(pkg) for pkg in tags_values_pkgs["openapi-gen"]["true"] if applies(pkg, include_pkgs, True) and not applies(pkg, exclude_pkgs, False)]),
  79. "&& cp $$GOPATH/src/" + output_pkg + "/zz_generated.openapi.go $$GO_GENRULE_EXECROOT/$(location :zz_generated.openapi.go)",
  80. "&& rm tmp_api_violations.report",
  81. ]),
  82. go_deps = openapi_deps(),
  83. tools = ["//vendor/k8s.io/kube-openapi/cmd/openapi-gen"],
  84. )