Skip to content

Commit

Permalink
Ensure go_binary.embed libraries' importpath is main (#2135)
Browse files Browse the repository at this point in the history
Go 1.13 is stricter on the package path passed to go tool compile -p.
This means that go_library passed in go_binary.embed fails to declare a
main.main function, as they now retain their original import path. Fix
that by adding a is_main boolean to the GoLibrary provider, which will
tell wether a library is supposed to be a main package; that is,
compiled from within a go_binary.

Fixes #2133
  • Loading branch information
steeve authored and Jay Conrod committed Jul 13, 2019
1 parent 216dca5 commit a386ca7
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 8 deletions.
14 changes: 8 additions & 6 deletions go/private/actions/archive.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,8 @@ def emit_archive(go, source = None):
cxxopts = [f for fs in source.cxxopts for f in fs.split(" ")]
clinkopts = [f for fs in source.clinkopts for f in fs.split(" ")]

importpath, _ = effective_importpath_pkgpath(source.library)
importmap = "main" if source.library.is_main else source.library.importmap
if source.cgo and not go.mode.pure:
cgo = cgo_configure(
go,
Expand All @@ -103,8 +105,8 @@ def emit_archive(go, source = None):
go,
sources = split.go + split.c + split.asm + split.cxx + split.objc + split.headers,
cover = source.cover,
importpath = effective_importpath_pkgpath(source.library)[0],
importmap = source.library.importmap,
importpath = importpath,
importmap = importmap,
archives = direct,
out_lib = out_lib,
out_export = out_export,
Expand All @@ -127,8 +129,8 @@ def emit_archive(go, source = None):
go,
sources = split.go + split.c + split.asm + split.cxx + split.objc + split.headers,
cover = source.cover,
importpath = effective_importpath_pkgpath(source.library)[0],
importmap = source.library.importmap,
importpath = importpath,
importmap = importmap,
archives = direct,
out_lib = out_lib,
out_export = out_export,
Expand All @@ -152,7 +154,7 @@ def emit_archive(go, source = None):
go.compile(
go,
sources = split.go,
importpath = source.library.importmap,
importpath = importmap,
archives = direct,
out_lib = out_lib,
out_export = out_export,
Expand All @@ -169,7 +171,7 @@ def emit_archive(go, source = None):
go.compile(
go,
sources = split.go + split.asm + split.headers,
importpath = source.library.importmap,
importpath = importmap,
archives = direct,
out_lib = partial_lib,
out_export = out_export,
Expand Down
3 changes: 2 additions & 1 deletion go/private/context.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ def _tool_args(go):
args.set_param_file_format("multiline")
return args

def _new_library(go, name = None, importpath = None, resolver = None, importable = True, testfilter = None, **kwargs):
def _new_library(go, name = None, importpath = None, resolver = None, importable = True, testfilter = None, is_main = False, **kwargs):
if not importpath:
importpath = go.importpath
importmap = go.importmap
Expand All @@ -164,6 +164,7 @@ def _new_library(go, name = None, importpath = None, resolver = None, importable
pathtype = pathtype,
resolve = resolver,
testfilter = testfilter,
is_main = is_main,
**kwargs
)

Expand Down
2 changes: 1 addition & 1 deletion go/private/rules/binary.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ def _go_binary_impl(ctx):
"""go_binary_impl emits actions for compiling and linking a go executable."""
go = go_context(ctx)

library = go.new_library(go, importable = False)
library = go.new_library(go, importable = False, is_main = True)
source = go.library_to_source(go, ctx.attr, library, ctx.coverage_instrumented())
name = ctx.attr.basename
if not name:
Expand Down
1 change: 1 addition & 0 deletions go/private/rules/nogo.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ def _nogo_impl(ctx):
importmap = "nogomain",
importpath_aliases = (),
pathtype = EXPORT_PATH,
is_main = True,
resolve = None,
)

Expand Down
1 change: 1 addition & 0 deletions go/private/rules/test.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ def _go_test_impl(ctx):
importmap = "testmain",
importpath_aliases = (),
pathtype = INFERRED_PATH,
is_main = True,
resolve = None,
)
test_deps = external_archive.direct + [external_archive]
Expand Down
6 changes: 6 additions & 0 deletions go/providers.rst
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,12 @@ an input to the `library_to_source`_ helper method, which produces GoSource_.
| A function called by `library_to_source`_ that can be used to resolve this |
| library to a mode-specific GoSource_. |
+--------------------------------+-----------------------------------------------------------------+
| :param:`is_main` | :type:`bool` |
+--------------------------------+-----------------------------------------------------------------+
| Indicates whether the library should be compiled as a `main` package. |
| `main` packages may have arbitrary `importpath` and `importmap` values, |
| but the compiler and linker must see them as `main`. |
+--------------------------------+-----------------------------------------------------------------+

GoSource
~~~~~~~~
Expand Down

0 comments on commit a386ca7

Please sign in to comment.