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

[internal] go: refactor link step into separate rule #13022

Merged
merged 1 commit into from
Sep 27, 2021
Merged
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
2 changes: 2 additions & 0 deletions src/python/pants/backend/experimental/go/register.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
go_mod,
go_pkg,
import_analysis,
link,
sdk,
)

Expand All @@ -31,6 +32,7 @@ def rules():
*import_analysis.rules(),
*go_mod.rules(),
*go_pkg.rules(),
*link.rules(),
*sdk.rules(),
*tailor.rules(),
*target_type_rules.rules(),
Expand Down
26 changes: 9 additions & 17 deletions src/python/pants/backend/go/goals/package_binary.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
is_third_party_package_target,
)
from pants.backend.go.util_rules.import_analysis import GatheredImports, GatherImportsRequest
from pants.backend.go.util_rules.sdk import GoSdkProcess
from pants.backend.go.util_rules.link import LinkedGoBinary, LinkGoBinaryRequest
from pants.build_graph.address import Address, AddressInput
from pants.core.goals.package import (
BuiltPackage,
Expand All @@ -21,7 +21,6 @@
)
from pants.engine.fs import AddPrefix, Digest, MergeDigests
from pants.engine.internals.selectors import Get, MultiGet
from pants.engine.process import ProcessResult
from pants.engine.rules import collect_rules, rule
from pants.engine.target import TransitiveTargets, TransitiveTargetsRequest, WrappedTarget
from pants.engine.unions import UnionRule
Expand Down Expand Up @@ -79,27 +78,20 @@ async def package_go_binary(
)

output_filename = PurePath(field_set.output_path.value_or_default(file_ending=None))
result = await Get(
ProcessResult,
GoSdkProcess(

binary = await Get(
LinkedGoBinary,
LinkGoBinaryRequest(
input_digest=input_digest,
command=(
"tool",
"link",
"-importcfg",
"./importcfg",
"-o",
f"./{output_filename.name}",
"-buildmode=exe", # seen in `go build -x` output
"./__pkg__.a",
),
archives=("./__pkg__.a",),
import_config_path="./importcfg",
output_filename=f"./{output_filename.name}",
description="Link Go binary.",
output_files=(f"./{output_filename.name}",),
),
)

renamed_output_digest = await Get(
Digest, AddPrefix(result.output_digest, str(output_filename.parent))
Digest, AddPrefix(binary.output_digest, str(output_filename.parent))
)

artifact = BuiltPackageArtifact(relpath=str(output_filename))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
go_mod,
go_pkg,
import_analysis,
link,
sdk,
)
from pants.build_graph.address import Address
Expand All @@ -40,6 +41,7 @@ def rule_runner() -> RuleRunner:
*build_go_pkg.rules(),
*go_pkg.rules(),
*go_mod.rules(),
*link.rules(),
*target_type_rules.rules(),
*external_module.rules(),
*sdk.rules(),
Expand Down
59 changes: 59 additions & 0 deletions src/python/pants/backend/go/util_rules/link.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
# Copyright 2021 Pants project contributors (see CONTRIBUTORS.md).
# Licensed under the Apache License, Version 2.0 (see LICENSE).
from __future__ import annotations

from dataclasses import dataclass

from pants.backend.go.util_rules.sdk import GoSdkProcess
from pants.engine.fs import Digest
from pants.engine.process import ProcessResult
from pants.engine.rules import Get, collect_rules, rule


@dataclass(frozen=True)
class LinkGoBinaryRequest:
"""Link a Go binary from package archives and an import configuration."""

input_digest: Digest
archives: tuple[str, ...]
import_config_path: str
output_filename: str
description: str


@dataclass(frozen=True)
class LinkedGoBinary:
"""A linked Go binary stored in a `Digest`."""

output_digest: Digest
output_filename: str


@rule
async def link_go_binary(request: LinkGoBinaryRequest) -> LinkedGoBinary:
result = await Get(
ProcessResult,
GoSdkProcess(
input_digest=request.input_digest,
command=(
"tool",
"link",
"-importcfg",
request.import_config_path,
"-o",
request.output_filename,
"-buildmode=exe", # seen in `go build -x` output
*request.archives,
),
description="Link Go binary.",
output_files=(request.output_filename,),
),
)

return LinkedGoBinary(
output_digest=result.output_digest, output_filename=request.output_filename
)


def rules():
return collect_rules()