Skip to content

Commit

Permalink
Make go_sdk's package_list optional (bazel-contrib#1625)
Browse files Browse the repository at this point in the history
This will eventually be removed when old versions of Gazelle are no
longer supported and nothing depends on the file by name.

If not provided as an input, go_sdk will generate the file itself.
  • Loading branch information
jayconrod authored Aug 6, 2018
1 parent d325f5b commit 1d380b3
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 16 deletions.
35 changes: 21 additions & 14 deletions go/private/rules/sdk.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,15 @@ load(
)

def _go_sdk_impl(ctx):
package_list = ctx.file.package_list
if package_list == None:
package_list = ctx.actions.declare_file("packages.txt")
_build_package_list(ctx, ctx.files.srcs, ctx.file.root_file, package_list)
return [GoSDK(
goos = ctx.attr.goos,
goarch = ctx.attr.goarch,
root_file = ctx.file.root_file,
package_list = ctx.file.package_list,
package_list = package_list,
libs = ctx.files.libs,
headers = ctx.files.headers,
srcs = ctx.files.srcs,
Expand All @@ -47,7 +51,6 @@ go_sdk = rule(
doc = "A file in the SDK root directory. Used to determine GOROOT.",
),
"package_list": attr.label(
mandatory = True,
allow_single_file = True,
doc = ("A text file containing a list of packages in the " +
"standard library that may be imported."),
Expand Down Expand Up @@ -86,18 +89,7 @@ go_sdk = rule(
)

def _package_list_impl(ctx):
packages = {}
src_dir = ctx.file.root_file.dirname + "/src/"
for src in ctx.files.srcs:
pkg_src_dir = src.dirname
if not pkg_src_dir.startswith(src_dir):
continue
pkg_name = pkg_src_dir[len(src_dir):]
if any([prefix in pkg_name for prefix in ("vendor/", "cmd/")]):
continue
packages[pkg_name] = None
content = "\n".join(sorted(packages.keys())) + "\n"
ctx.actions.write(ctx.outputs.out, content)
_build_package_list(ctx, ctx.files.srcs, ctx.file.root_file, ctx.outputs.out)
return [DefaultInfo(files = depset([ctx.outputs.out]))]

package_list = rule(
Expand All @@ -122,3 +114,18 @@ package_list = rule(
),
},
)

def _build_package_list(ctx, srcs, root_file, out):
packages = {}
src_dir = root_file.dirname + "/src/"
for src in srcs:
pkg_src_dir = src.dirname
if not pkg_src_dir.startswith(src_dir):
continue
pkg_name = pkg_src_dir[len(src_dir):]
if any([prefix in pkg_name for prefix in ("vendor/", "cmd/")]):
continue
packages[pkg_name] = None
content = "\n".join(sorted(packages.keys())) + "\n"
ctx.actions.write(out, content)

4 changes: 2 additions & 2 deletions go/toolchains.rst
Original file line number Diff line number Diff line change
Expand Up @@ -379,10 +379,10 @@ usually won't need to declare this rule explicitly.
+--------------------------------+-----------------------------+-----------------------------------+
| A file in the SDK root directory. Used to determine GOROOT. |
+--------------------------------+-----------------------------+-----------------------------------+
| :param:`package_list` | :type:`label` | |mandatory| |
| :param:`package_list` | :type:`label` | :value:`None` |
+--------------------------------+-----------------------------+-----------------------------------+
| A text file containing a list of packages in the standard library that may |
| be imported. |
| be imported. If unspecified, this will be generated from ``srcs``. |
+--------------------------------+-----------------------------+-----------------------------------+
| :param:`libs` | :type:`label_list` | :value:`[]` |
+--------------------------------+-----------------------------+-----------------------------------+
Expand Down

0 comments on commit 1d380b3

Please sign in to comment.