Skip to content

Commit

Permalink
Merge branch 'master' into zbarsky/orig-srcs
Browse files Browse the repository at this point in the history
  • Loading branch information
dzbarsky authored Aug 15, 2024
2 parents c039ecc + a32f3e1 commit 7f6622b
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 40 deletions.
3 changes: 1 addition & 2 deletions go/private/actions/archive.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ load(
"GoArchive",
"GoArchiveData",
"effective_importpath_pkgpath",
"get_archive",
)
load(
"//go/private/actions:compilepkg.bzl",
Expand Down Expand Up @@ -74,7 +73,7 @@ def emit_archive(go, source = None, _recompile_suffix = "", recompile_internal_d
out_nogo_log = None
out_nogo_validation = None

direct = [get_archive(dep) for dep in source.deps]
direct = source.deps

files = []
for a in direct:
Expand Down
16 changes: 9 additions & 7 deletions go/private/actions/compilepkg.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
load("//go/private:common.bzl", "GO_TOOLCHAIN_LABEL", "SUPPORTS_PATH_MAPPING_REQUIREMENT")
load(
"//go/private:mode.bzl",
"link_mode_args",
"link_mode_arg",
)
load("//go/private/actions:utils.bzl", "quote_opts")

Expand Down Expand Up @@ -143,20 +143,22 @@ def emit_compilepkg(
if testfilter:
args.add("-testfilter", testfilter)

gc_flags = list(gc_goopts)
gc_flags.extend(go.mode.gc_goopts)
asm_flags = []
link_mode_flag = link_mode_arg(go.mode)

gc_flags = gc_goopts + go.mode.gc_goopts
if go.mode.race:
gc_flags.append("-race")
if go.mode.msan:
gc_flags.append("-msan")
if go.mode.debug:
gc_flags.extend(["-N", "-l"])
gc_flags.extend(go.toolchain.flags.compile)
gc_flags.extend(link_mode_args(go.mode))
asm_flags.extend(link_mode_args(go.mode))
if link_mode_flag:
gc_flags.append(link_mode_flag)
args.add("-gcflags", quote_opts(gc_flags))
args.add("-asmflags", quote_opts(asm_flags))

if link_mode_flag:
args.add("-asmflags", link_mode_flag)

# cgo and the linker action don't support path mapping yet
# TODO: Remove the second condition after https://github.com/bazelbuild/bazel/pull/21921.
Expand Down
7 changes: 5 additions & 2 deletions go/private/actions/stdlib.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ load(
"//go/private:mode.bzl",
"LINKMODE_NORMAL",
"extldflags_from_cc_toolchain",
"link_mode_args",
"link_mode_arg",
)
load(
"//go/private:providers.bzl",
Expand Down Expand Up @@ -133,7 +133,10 @@ def _build_stdlib(go):
args.add("-package", "std")
if not go.mode.pure:
args.add("-package", "runtime/cgo")
args.add_all(link_mode_args(go.mode))

link_mode_flag = link_mode_arg(go.mode)
if link_mode_flag:
args.add(link_mode_flag)

args.add("-gcflags", quote_opts(go.mode.gc_goopts))

Expand Down
31 changes: 17 additions & 14 deletions go/private/context.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ load(
"GoSource",
"GoStdLib",
"INFERRED_PATH",
"get_archive",
"get_source",
)

Expand Down Expand Up @@ -235,28 +236,25 @@ def _merge_embed(source, embed):
source["clinkopts"] = source["clinkopts"] or s.clinkopts
source["cgo_exports"] = source["cgo_exports"] + s.cgo_exports

def _dedup_deps(deps):
"""Returns a list of deps without duplicate import paths.
def _dedup_archives(archives):
"""Returns a list of archives without duplicate import paths.
Earlier targets take precedence over later targets. This is intended to
Earlier archives take precedence over later targets. This is intended to
allow an embedding library to override the dependencies of its
embedded libraries.
Args:
deps: an iterable containing either Targets or GoArchives.
archives: an iterable of GoArchives.
"""
deduped_deps = []
deduped_archives = []
importpaths = {}
for dep in deps:
if hasattr(dep, "data") and hasattr(dep.data, "importpath"):
importpath = dep.data.importpath
else:
importpath = dep[GoLibrary].importpath
for arc in archives:
importpath = arc.data.importpath
if importpath in importpaths:
continue
importpaths[importpath] = None
deduped_deps.append(dep)
return deduped_deps
deduped_archives.append(arc)
return deduped_archives

def _library_to_source(go, attr, library, coverage_instrumented):
#TODO: stop collapsing a depset in this line...
Expand All @@ -267,6 +265,7 @@ def _library_to_source(go, attr, library, coverage_instrumented):
attr_deps = getattr(attr, "deps", [])
generated_deps = getattr(library, "deps", [])
deps = attr_deps + generated_deps
deps = [get_archive(dep) for dep in deps]
source = {
"library": library,
"mode": go.mode,
Expand All @@ -293,7 +292,9 @@ def _library_to_source(go, attr, library, coverage_instrumented):
for e in getattr(attr, "embed", []):
_check_binary_dep(go, e, "embed")
_merge_embed(source, e)
source["deps"] = _dedup_deps(source["deps"])

source["deps"] = _dedup_archives(source["deps"])

x_defs = source["x_defs"]
for k, v in getattr(attr, "x_defs", {}).items():
v = _expand_location(go, attr, v)
Expand All @@ -312,7 +313,9 @@ def _library_to_source(go, attr, library, coverage_instrumented):
fail("source {} has C/C++ extension, but cgo was not enabled (set 'cgo = True')".format(f.path))
if library.resolve:
library.resolve(go, attr, source, _merge_embed)
source["cc_info"] = _collect_cc_infos(source["deps"], source["cdeps"])

source["cc_info"] = _collect_cc_infos(attr_deps + generated_deps, source["cdeps"])

return GoSource(**source)

def _collect_runfiles(go, data, deps):
Expand Down
13 changes: 6 additions & 7 deletions go/private/mode.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -211,24 +211,23 @@ _LINK_PIE_PLATFORMS = {
"freebsd/amd64": None,
}

def link_mode_args(mode):
def link_mode_arg(mode):
# based on buildModeInit in cmd/go/internal/work/init.go
platform = mode.goos + "/" + mode.goarch
args = []
if mode.link == LINKMODE_C_ARCHIVE:
if (platform in _LINK_C_ARCHIVE_PLATFORMS or
mode.goos in _LINK_C_ARCHIVE_GOOS and platform != "linux/ppc64"):
args.append("-shared")
return "-shared"
elif mode.link == LINKMODE_C_SHARED:
if mode.goos in _LINK_C_SHARED_GOOS:
args.append("-shared")
return "-shared"
elif mode.link == LINKMODE_PLUGIN:
if platform in _LINK_PLUGIN_PLATFORMS:
args.append("-dynlink")
return "-dynlink"
elif mode.link == LINKMODE_PIE:
if platform in _LINK_PIE_PLATFORMS:
args.append("-shared")
return args
return "-shared"
return None

def extldflags_from_cc_toolchain(go):
if not go.cgo_tools:
Expand Down
13 changes: 6 additions & 7 deletions go/private/rules/test.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ load(
"GoLibrary",
"GoSource",
"INFERRED_PATH",
"get_archive",
)
load(
"//go/private/rules:binary.bzl",
Expand Down Expand Up @@ -525,7 +524,7 @@ def _recompile_external_deps(go, external_source, internal_archive, library_labe
# Build a map from labels to GoArchiveData.
# If none of the librares embedded in the internal archive are in the
# dependency graph, then nothing needs to be recompiled.
arc_data_list = depset(transitive = [get_archive(dep).transitive for dep in external_source.deps]).to_list()
arc_data_list = depset(transitive = [archive.transitive for archive in external_source.deps]).to_list()
label_to_arc_data = {a.label: a for a in arc_data_list}
if all([l not in label_to_arc_data for l in library_labels]):
return external_source, internal_archive
Expand All @@ -543,7 +542,7 @@ def _recompile_external_deps(go, external_source, internal_archive, library_labe
dep_list = []

# stack is a stack of targets to process. We're done when it's empty.
stack = [get_archive(dep).data.label for dep in external_source.deps]
stack = [archive.data.label for archive in external_source.deps]

# deps_pushed tracks the status of each target.
# DEPS_UNPROCESSED means the target is on the stack, but its dependencies
Expand Down Expand Up @@ -622,10 +621,10 @@ def _recompile_external_deps(go, external_source, internal_archive, library_labe
# Pass internal dependencies that need to be recompiled down to the builder to check if the internal archive
# tries to import any of the dependencies. If there is, that means that there is a dependency cycle.
need_recompile_deps = []
for dep in internal_source.deps:
dep_data = get_archive(dep).data
for archive in internal_source.deps:
dep_data = archive.data
if not need_recompile[dep_data.label]:
internal_deps.append(dep)
internal_deps.append(archive)
else:
need_recompile_deps.append(dep_data.importpath)

Expand Down Expand Up @@ -711,5 +710,5 @@ def _recompile_external_deps(go, external_source, internal_archive, library_labe
# Finally, we need to replace external_source.deps with the recompiled
# archives.
attrs = structs.to_dict(external_source)
attrs["deps"] = [label_to_archive[get_archive(dep).data.label] for dep in external_source.deps]
attrs["deps"] = [label_to_archive[archive.data.label] for archive in external_source.deps]
return GoSource(**attrs), internal_archive
2 changes: 1 addition & 1 deletion go/providers.rst
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ method. In general, only rules_go should need to build or handle these.
+--------------------------------+-----------------------------------------------------------------+
| Map of defines to add to the go link command. |
+--------------------------------+-----------------------------------------------------------------+
| :param:`deps` | :type:`list of Target` |
| :param:`deps` | :type:`list of GoArchive` |
+--------------------------------+-----------------------------------------------------------------+
| The direct dependencies needed by this library. |
+--------------------------------+-----------------------------------------------------------------+
Expand Down

0 comments on commit 7f6622b

Please sign in to comment.