Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

go_binary: Emit CcInfo directly instead of using cc_{import,library} #2682

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions go/private/actions/link.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,17 @@ def emit_link(
if go.mode.link == LINKMODE_PLUGIN:
tool_args.add("-pluginpath", archive.data.importpath)

if go.mode.link == "c-shared":
root_len = len(executable.root.path)
if root_len > 0:
# Add '/' seperator to `root_len`.
root_len += 1

# TODO(yannic): `-install_name` should be `@rpath/<runtime_solib_name>`,
# but we can't create `SolibSymlinkAction`s from Starlark yet.
extldflags.append("-install_name")
extldflags.append(executable.path[root_len:])

arcs = _transitive_archives_without_test_archives(archive, test_archives)
arcs.extend(test_archives)
if (go.coverage_enabled and go.coverdata and
Expand Down
66 changes: 64 additions & 2 deletions go/private/rules/binary.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@
# See the License for the specific language governing permissions and
# limitations under the License.

load(
"@bazel_tools//tools/cpp:toolchain_utils.bzl",
"find_cpp_toolchain",
)
load(
":context.bzl",
"go_context",
Expand All @@ -33,6 +37,8 @@ load(
)
load(
":mode.bzl",
"LINKMODE_C_ARCHIVE",
"LINKMODE_C_SHARED",
"LINKMODE_PLUGIN",
"LINKMODE_SHARED",
)
Expand Down Expand Up @@ -62,7 +68,8 @@ def _go_binary_impl(ctx):
info_file = ctx.info_file,
executable = executable,
)
return [

providers = [
library,
source,
archive,
Expand All @@ -77,6 +84,56 @@ def _go_binary_impl(ctx):
),
]

if go.mode.link == LINKMODE_C_SHARED or go.mode.link == LINKMODE_C_ARCHIVE:
cc_toolchain = find_cpp_toolchain(ctx)

feature_configuration = cc_common.configure_features(
ctx = ctx,
cc_toolchain = cc_toolchain,
)

create_library_to_link_kwargs = {}
if go.mode.link == LINKMODE_C_SHARED:
create_library_to_link_kwargs["dynamic_library"] = executable
elif go.mode.link == LINKMODE_C_ARCHIVE:
create_library_to_link_kwargs["static_library"] = executable
else:
fail("Unknown linkmode '{}'".format(go.mode.link))
linking_context = cc_common.create_linking_context(
linker_inputs = depset([
cc_common.create_linker_input(
owner = ctx.label,
libraries = depset([
cc_common.create_library_to_link(
actions = ctx.actions,
feature_configuration = feature_configuration,
cc_toolchain = cc_toolchain,
**create_library_to_link_kwargs),
]),
),
]),
)

header = ctx.actions.declare_file("{}.h".format(ctx.attr.name))
ctx.actions.symlink(
output = header,
# TODO(yannic): Find better way to get direct export header.
target_file = archive.cgo_exports.to_list()[0],
)

compilation_context = cc_common.create_compilation_context(
headers = depset([header]),
includes = depset([header.root.path]),
)

cc_info = CcInfo(
compilation_context = compilation_context,
linking_context = linking_context,
)
providers.append(cc_info)

return providers

_go_binary_kwargs = {
"implementation": _go_binary_impl,
"attrs": {
Expand All @@ -101,9 +158,14 @@ _go_binary_kwargs = {
"cxxopts": attr.string_list(),
"clinkopts": attr.string_list(),
"_go_context_data": attr.label(default = "//:go_context_data"),
"_cc_toolchain": attr.label(default = "@bazel_tools//tools/cpp:current_cc_toolchain"),
},
"executable": True,
"toolchains": ["@io_bazel_rules_go//go:toolchain"],
"toolchains": [
"@bazel_tools//tools/cpp:toolchain_type",
"@io_bazel_rules_go//go:toolchain",
],
"fragments": ["cpp"]
}

go_binary = rule(**_go_binary_kwargs)
Expand Down
41 changes: 4 additions & 37 deletions go/private/rules/cgo.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -214,49 +214,16 @@ def go_binary_c_archive_shared(name, kwargs):
linkmode = kwargs.get("linkmode")
if linkmode not in [LINKMODE_C_SHARED, LINKMODE_C_ARCHIVE]:
return
cgo_exports = name + ".cgo_exports"
c_hdrs = name + ".c_hdrs"
cc_import_name = name + ".cc_import"
cc_library_name = name + ".cc"
tags = kwargs.get("tags", ["manual"])
if "manual" not in tags:
# These archives can't be built on all platforms, so use "manual" tags.
tags.append("manual")
native.filegroup(
name = cgo_exports,
srcs = [name],
output_group = "cgo_exports",
visibility = ["//visibility:private"],
tags = tags,
)
native.genrule(
name = c_hdrs,
srcs = [cgo_exports],
outs = ["%s.h" % name],
cmd = "cat $(SRCS) > $(@)",
visibility = ["//visibility:private"],
tags = tags,
)
cc_import_kwargs = {}
if linkmode == LINKMODE_C_SHARED:
cc_import_kwargs["shared_library"] = name
elif linkmode == LINKMODE_C_ARCHIVE:
cc_import_kwargs["static_library"] = name
cc_import_kwargs["alwayslink"] = 1
cc_import(
name = cc_import_name,
visibility = ["//visibility:private"],
tags = tags,
**cc_import_kwargs
)
cc_library(
#
native.alias(
name = cc_library_name,
hdrs = [c_hdrs],
deps = [cc_import_name],
alwayslink = 1,
linkstatic = (linkmode == LINKMODE_C_ARCHIVE and 1 or 0),
copts = _DEFAULT_PLATFORM_COPTS,
linkopts = _DEFAULT_PLATFORM_COPTS,
actual = name,
visibility = ["//visibility:public"],
tags = tags,
deprecation = "Please depend on ':{}' directly.".format(name),
)