Skip to content

Commit 258db29

Browse files
committed
Cleanup some cgo handling
1 parent 7ef8fb1 commit 258db29

File tree

6 files changed

+21
-31
lines changed

6 files changed

+21
-31
lines changed

go/private/actions/archive.bzl

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ def emit_archive(go, source = None, _recompile_suffix = "", recompile_internal_d
144144
is_external_pkg = is_external_pkg,
145145
)
146146
else:
147-
cgo_deps = depset()
147+
cgo_deps = []
148148
emit_compilepkg(
149149
go,
150150
sources = source.srcs,
@@ -204,26 +204,21 @@ def emit_archive(go, source = None, _recompile_suffix = "", recompile_internal_d
204204
runfiles = source.runfiles,
205205
_validation_output = out_nogo_validation,
206206
_nogo_diagnostics = out_diagnostics,
207-
_cgo_deps = cgo_deps,
207+
_cgo_deps = depset(cgo_deps),
208208
)
209209
x_defs = dict(source.x_defs)
210210
for a in direct:
211211
x_defs.update(a.x_defs)
212212

213-
# Ensure that the _cgo_export.h of the current target comes first when cgo_exports is iterated
214-
# by prepending it and specifying the order explicitly. This is required as the CcInfo attached
215-
# to the archive only exposes a single header rather than combining all headers.
216-
cgo_exports_direct = [out_cgo_export_h] if out_cgo_export_h else []
217-
cgo_exports = depset(direct = cgo_exports_direct, transitive = [a.cgo_exports for a in direct], order = "preorder")
218213
return GoArchive(
219214
source = source,
220215
data = data,
221216
direct = direct,
222217
libs = depset(direct = [out_lib], transitive = [a.libs for a in direct]),
223218
transitive = depset([data], transitive = [a.transitive for a in direct]),
224219
x_defs = x_defs,
225-
cgo_deps = depset(transitive = [cgo_deps] + [a.cgo_deps for a in direct]),
226-
cgo_exports = cgo_exports,
220+
cgo_deps = depset(cgo_deps, transitive = [a.cgo_deps for a in direct]),
221+
cgo_export = out_cgo_export_h,
227222
runfiles = runfiles,
228223
_headers = headers,
229224
)

go/private/rules/binary.bzl

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,6 @@ load(
4949
"non_go_transition",
5050
)
5151

52-
_EMPTY_DEPSET = depset([])
53-
5452
def _include_path(hdr):
5553
if not hdr.root.path:
5654
fail("Expected hdr to be a generated file, got source file: " + hdr.path)
@@ -66,9 +64,10 @@ def _include_path(hdr):
6664

6765
def new_cc_import(
6866
go,
69-
hdrs = _EMPTY_DEPSET,
70-
defines = _EMPTY_DEPSET,
71-
local_defines = _EMPTY_DEPSET,
67+
headers = None,
68+
includes = None,
69+
defines = None,
70+
local_defines = None,
7271
dynamic_library = None,
7372
static_library = None,
7473
alwayslink = False,
@@ -77,8 +76,8 @@ def new_cc_import(
7776
compilation_context = cc_common.create_compilation_context(
7877
defines = defines,
7978
local_defines = local_defines,
80-
headers = hdrs,
81-
includes = depset([_include_path(hdr) for hdr in hdrs.to_list()]),
79+
headers = headers,
80+
includes = includes,
8281
),
8382
linking_context = cc_common.create_linking_context(
8483
linker_inputs = depset([
@@ -137,9 +136,7 @@ def _go_binary_impl(ctx):
137136
importable = False,
138137
is_main = is_main,
139138
)
140-
name = ctx.attr.basename
141-
if not name:
142-
name = ctx.label.name
139+
name = ctx.attr.basename or ctx.label.name
143140
executable = None
144141
if ctx.attr.out:
145142
# Use declare_file instead of attr.output(). When users set output files
@@ -161,7 +158,7 @@ def _go_binary_impl(ctx):
161158
providers = [
162159
archive,
163160
OutputGroupInfo(
164-
cgo_exports = archive.cgo_exports,
161+
cgo_exports = [archive.cgo_export] if archive.cgo_export else [],
165162
compilation_outputs = [archive.data.file],
166163
nogo_fix = [nogo_diagnostics] if nogo_diagnostics else [],
167164
_validation = [validation_output] if validation_output else [],
@@ -202,14 +199,14 @@ def _go_binary_impl(ctx):
202199
"windows": ["-mthreads"],
203200
}.get(go.mode.goos, ["-pthread"]),
204201
}
205-
cgo_exports = archive.cgo_exports.to_list()
206-
if cgo_exports:
202+
if archive.cgo_export:
207203
header = ctx.actions.declare_file("{}.h".format(name))
208204
ctx.actions.symlink(
209205
output = header,
210-
target_file = cgo_exports[0],
206+
target_file = archive.cgo_export,
211207
)
212-
cc_import_kwargs["hdrs"] = depset([header])
208+
cc_import_kwargs["headers"] = depset([header])
209+
cc_import_kwargs["includes"] = depset([_include_path(header)])
213210
if go.mode.linkmode == LINKMODE_C_SHARED:
214211
cc_import_kwargs["dynamic_library"] = executable
215212
elif go.mode.linkmode == LINKMODE_C_ARCHIVE:

go/private/rules/cgo.bzl

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ def cgo_configure(go, srcs, cdeps, cppopts, copts, cxxopts, clinkopts):
8989

9090
inputs_direct = []
9191
inputs_transitive = []
92-
deps_direct = []
92+
deps = []
9393
lib_opts = []
9494
runfiles = go._ctx.runfiles(collect_data = True)
9595
seen_alwayslink_libs = {}
@@ -104,7 +104,7 @@ def cgo_configure(go, srcs, cdeps, cppopts, copts, cxxopts, clinkopts):
104104
inputs_transitive.append(cc_transitive_headers)
105105
cc_libs, cc_link_flags = _cc_libs_and_flags(d)
106106
inputs_direct.extend(cc_libs)
107-
deps_direct.extend(cc_libs)
107+
deps.extend(cc_libs)
108108
cc_defines = d[CcInfo].compilation_context.defines.to_list()
109109
cppopts.extend(["-D" + define for define in cc_defines])
110110
cc_includes = d[CcInfo].compilation_context.includes.to_list()
@@ -170,7 +170,6 @@ def cgo_configure(go, srcs, cdeps, cppopts, copts, cxxopts, clinkopts):
170170
fail("unknown library has neither cc nor objc providers: %s" % d.label)
171171

172172
inputs = depset(direct = inputs_direct, transitive = inputs_transitive)
173-
deps = depset(direct = deps_direct)
174173

175174
# HACK: some C/C++ toolchains will ignore libraries (including dynamic libs
176175
# specified with -l flags) unless they appear after .o or .a files with

go/private/rules/library.bzl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ def _go_library_impl(ctx):
6767
extensions = ["go"],
6868
),
6969
OutputGroupInfo(
70-
cgo_exports = archive.cgo_exports,
70+
cgo_exports = [archive.cgo_export] if archive.cgo_export else [],
7171
compilation_outputs = [archive.data.file],
7272
nogo_fix = [nogo_diagnostics] if nogo_diagnostics else [],
7373
_validation = [validation_output] if validation_output else [],

go/private/rules/test.bzl

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -737,7 +737,6 @@ def _recompile_external_deps(go, external_go_info, internal_archive, library_lab
737737
transitive = depset(direct = [arc_data], transitive = [a.transitive for a in deps]),
738738
x_defs = go_info.x_defs,
739739
cgo_deps = depset(transitive = [arc_data._cgo_deps] + [a.cgo_deps for a in deps]),
740-
cgo_exports = depset(transitive = [a.cgo_exports for a in deps]),
741740
runfiles = go_info.runfiles,
742741
mode = go.mode,
743742
_headers = internal_archive._headers,

go/providers.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -285,9 +285,9 @@ which is available through the :param:`data` field.
285285
| The direct cgo dependencies of this library. |
286286
| This has the same constraints as things that can appear in the deps of a cc_library_. |
287287
+--------------------------------+-----------------------------------------------------------------+
288-
| :param:`cgo_exports` | :type:`depset of GoInfo` |
288+
| :param:`cgo_export` | :type:`File` |
289289
+--------------------------------+-----------------------------------------------------------------+
290-
| The transitive set of c headers needed to reference exports of this archive. |
290+
| The c headers needed to reference exports of this archive. |
291291
+--------------------------------+-----------------------------------------------------------------+
292292
| :param:`runfiles` | runfiles_ |
293293
+--------------------------------+-----------------------------------------------------------------+

0 commit comments

Comments
 (0)