github.com/stackb/rules_proto@v0.0.0-20240221195024-5428336c51f1/rules/private/proto_repository_tools.bzl (about)

     1  # Copyright 2019 The Bazel Authors. All rights reserved.
     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  """"""
    15  
    16  load("//rules/private:execution.bzl", "env_execute", "executable_extension")
    17  load("@bazel_gazelle//internal:go_repository_cache.bzl", "read_cache_env")
    18  load("@build_stack_rules_proto//rules/private:proto_repository_tools_srcs.bzl", "PROTO_REPOSITORY_TOOLS_SRCS")
    19  
    20  _PROTO_REPOSITORY_TOOLS_BUILD_FILE = """
    21  package(default_visibility = ["//visibility:public"])
    22  
    23  filegroup(
    24      name = "gazelle",
    25      srcs = ["bin/gazelle{extension}"],
    26  )
    27  """
    28  
    29  def _proto_repository_tools_impl(ctx):
    30      # Create a link to the rules_proto repo. This will be our GOPATH.
    31      env = read_cache_env(ctx, str(ctx.path(ctx.attr.go_cache)))
    32      extension = executable_extension(ctx)
    33      go_tool = env["GOROOT"] + "/bin/go" + extension
    34  
    35      rules_proto_path = ctx.path(Label("@build_stack_rules_proto//:WORKSPACE"))
    36      ctx.symlink(
    37          rules_proto_path.dirname,
    38          "src/github.com/stackb/rules_proto",
    39      )
    40  
    41      env.update({
    42          "GOPATH": str(ctx.path(".")),
    43          # TODO(gravypod): make this more hermetic
    44          "GO111MODULE": "off",
    45          # workaround: avoid the Go SDK paths from leaking into the binary
    46          "GOROOT_FINAL": "GOROOT",
    47          # workaround: avoid cgo paths in /tmp leaking into binary
    48          "CGO_ENABLED": "0",
    49      })
    50  
    51      if "PATH" in ctx.os.environ:
    52          # workaround: to find gcc for go link tool on Arm platform
    53          env["PATH"] = ctx.os.environ["PATH"]
    54      if "GOPROXY" in ctx.os.environ:
    55          env["GOPROXY"] = ctx.os.environ["GOPROXY"]
    56  
    57      # Make sure the list of source is up to date.
    58      # We don't want to run the script, then resolve each source file it returns.
    59      # If many of the sources changed even slightly, Bazel would restart this
    60      # rule each time. Compiling the script is relatively slow.
    61      # Don't try this on Windows: bazel does not set up symbolic links.
    62      if "windows" not in ctx.os.name:
    63          result = env_execute(
    64              ctx,
    65              [
    66                  go_tool,
    67                  "run",
    68                  ctx.path(ctx.attr._list_repository_tools_srcs),
    69                  "-dir=src/github.com/stackb/rules_proto",
    70                  # "-check=rules/private/proto_repository_tools_srcs.bzl",
    71                  # Run it under 'generate' to recreate the list'
    72                  "-generate=rules/private/proto_repository_tools_srcs.bzl",
    73              ],
    74              environment = env,
    75          )
    76          if result.return_code:
    77              fail("list_repository_tools_srcs: " + result.stderr)
    78  
    79      # Build the tools
    80      args = [
    81          go_tool,
    82          "install",
    83          "-ldflags",
    84          "-w -s",
    85          "-gcflags",
    86          "all=-trimpath=" + env["GOPATH"],
    87          "-asmflags",
    88          "all=-trimpath=" + env["GOPATH"],
    89          "github.com/stackb/rules_proto/cmd/gazelle",
    90      ]
    91      result = env_execute(ctx, args, environment = env)
    92      if result.return_code:
    93          fail("failed to build tools: " + result.stderr)
    94  
    95      # add a build file to export the tools
    96      ctx.file(
    97          "BUILD.bazel",
    98          _PROTO_REPOSITORY_TOOLS_BUILD_FILE.format(extension = executable_extension(ctx)),
    99          False,
   100      )
   101  
   102  proto_repository_tools = repository_rule(
   103      _proto_repository_tools_impl,
   104      attrs = {
   105          "go_cache": attr.label(
   106              mandatory = True,
   107              allow_single_file = True,
   108          ),
   109          "_proto_repository_tools_srcs": attr.label_list(
   110              default = PROTO_REPOSITORY_TOOLS_SRCS,
   111          ),
   112          "_list_repository_tools_srcs": attr.label(
   113              default = "@build_stack_rules_proto//rules/private:list_repository_tools_srcs.go",
   114          ),
   115      },
   116      environ = [
   117          "GOCACHE",
   118          "GOPATH",
   119          "GO_REPOSITORY_USE_HOST_CACHE",
   120      ],
   121  )