Skip to content

stdlib: Do not pass -static to cgo #3593

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

Merged
merged 3 commits into from
Jun 23, 2023
Merged

Conversation

zecke
Copy link
Contributor

@zecke zecke commented Jun 19, 2023

What type of PR is this?

Bug fix

What does this PR do? Why is it needed?

For our C/C++ we have used --features=fully_static_link to produce static binaries. Prior to Go 1.20 this setting did not leak into the compiled Go binary (when using cgo). This PR restores the behavior we have seen with Go 1.19. For a go_binary we can individually decide whether or not to link it statically.

Which issues(s) does this PR fix?

Fixes #3590

Other notes for review

zecke added 2 commits June 19, 2023 11:33
The CGO_LDFLAGS we pass get embedded as the cgo_ldflag directive. Once
we pass -static, any cgo/net/os.user usage will lead to linking
statically. Avoid this.

Fixes: bazel-contrib#3590
Control whether or not to link statically through go.mode.static
and not through the C/C++ compiler's ldflags.

This only matters when Go's linker decides to use external linker
for linking the final binary. This started to happen with Go 1.20
as the builder passes `-fdebug-prefix-map=` when building cgo code.

In return the Go compiler emits the preferlinkext hint.
@fmeum
Copy link
Member

fmeum commented Jun 19, 2023

@linzhp Could you test at Uber?

@zecke zecke changed the title Freyth 3590 stdlib: Do not pass -static to cgo Jun 19, 2023
Copy link
Contributor

@linzhp linzhp left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tests passed

@fmeum
Copy link
Member

fmeum commented Jun 22, 2023

@zecke Would it still be possible to build a fully static Go binary using CGo, but not the CGo part of the stdlib, with this PR? That's my only remaining concern.

@zecke
Copy link
Contributor Author

zecke commented Jun 23, 2023

@zecke Would it still be possible to build a fully static Go binary using CGo, but not the CGo part of the stdlib, with this PR? That's my only remaining concern.

That one is interesting. Given --features=fully_static_link and a simple bit of Cgo, this links statically all the time with Go 1.19 and later (regardless of static="off"). So far I have only patched rules_go to keep the Go1.19 behavior on Go1.20. I am happy to patch this too but wonder whether or not to do this in a separate MR.

@fmeum
Copy link
Member

fmeum commented Jun 23, 2023

Thanks for the confirmation. I agree that this can be done as a separate PR.

@fmeum fmeum enabled auto-merge (squash) June 23, 2023 08:58
@fmeum fmeum disabled auto-merge June 23, 2023 08:58
@fmeum fmeum enabled auto-merge (squash) June 23, 2023 08:59
@fmeum fmeum merged commit b766077 into bazel-contrib:master Jun 23, 2023
xhebox pushed a commit to xhebox/rules_go that referenced this pull request Jul 7, 2023
* stdlib: Do not pass -static to cgo

The CGO_LDFLAGS we pass get embedded as the cgo_ldflag directive. Once
we pass -static, any cgo/net/os.user usage will lead to linking
statically. Avoid this.

Fixes: bazel-contrib#3590

* link: Control static linking through go.mode.static

Control whether or not to link statically through go.mode.static
and not through the C/C++ compiler's ldflags.

This only matters when Go's linker decides to use external linker
for linking the final binary. This started to happen with Go 1.20
as the builder passes `-fdebug-prefix-map=` when building cgo code.

In return the Go compiler emits the preferlinkext hint.
renovate bot referenced this pull request in cgrindel/bazel-starlib Jul 10, 2023
[![Mend
Renovate](https://app.renovatebot.com/images/banner.svg)](https://renovatebot.com)

This PR contains the following updates:

| Package | Type | Update | Change |
|---|---|---|---|
| [io_bazel_rules_go](https://togithub.com/bazelbuild/rules_go) |
http_archive | minor | `v0.40.1` -> `v0.41.0` |

---

### Release Notes

<details>
<summary>bazelbuild/rules_go (io_bazel_rules_go)</summary>

###
[`v0.41.0`](https://togithub.com/bazelbuild/rules_go/releases/tag/v0.41.0)

[Compare
Source](https://togithub.com/bazelbuild/rules_go/compare/v0.40.1...v0.41.0)

#### What's Changed

Starting this release, rules_go no longer ship with `@go_googleapis`
repo. Together with Gazelle v0.32.0, it means that all Go code importing
generated code from Google APIs will depend on
`@org_golang_google_genproto`, which is resolved by Go modules. For
proto files importing Google APIs proto, people needs to:

1.  Add an `http_archive` rule to download Google APIs, e.g.,

<!---->

    http_archive(
        name = "googleapis",
sha256 =
"9d1a930e767c93c825398b8f8692eca3fe353b9aaadedfbcf1fca2282c85df88",
strip_prefix = "googleapis-64926d52febbf298cb82a8f472ade4a3969ba922",
        urls = [

"https://github.com/googleapis/googleapis/archive/64926d52febbf298cb82a8f472ade4a3969ba922.zip",
        ],
    )

2. Resolve the proto manually. If Gazelle is being used, directives like
this needs to be added to a parent directory of the proto files:

<!---->

### gazelle:resolve proto proto google/rpc/status.proto
@&#8203;googleapis//google/rpc:status_proto
### gazelle:resolve proto go google/rpc/status.proto
@&#8203;org_golang_google_genproto//googleapis/rpc/status
### gazelle:resolve proto google/longrunning/operations.proto
@&#8203;googleapis//google/longrunning:operations_proto
### gazelle:resolve proto go google/longrunning/operations.proto
@&#8203;org_golang_google_genproto//googleapis/longrunning

Other changes included in this release

- detect_sdk_version: Support 1.21's new VERSION file format by
[@&#8203;evanj](https://togithub.com/evanj) in
[https://github.com/bazelbuild/rules_go/pull/3600](https://togithub.com/bazelbuild/rules_go/pull/3600)
- stdlib: Do not pass -static to cgo by
[@&#8203;zecke](https://togithub.com/zecke) in
[https://github.com/bazelbuild/rules_go/pull/3593](https://togithub.com/bazelbuild/rules_go/pull/3593)
- Expand stdlib packages when queried by
[@&#8203;JamyDev](https://togithub.com/JamyDev) in
[https://github.com/bazelbuild/rules_go/pull/3606](https://togithub.com/bazelbuild/rules_go/pull/3606)
- Fix stdlib file outputs pointing to processwrapper by
[@&#8203;JamyDev](https://togithub.com/JamyDev) in
[https://github.com/bazelbuild/rules_go/pull/3608](https://togithub.com/bazelbuild/rules_go/pull/3608)
- Fix `go_sdk` extension failure for unnamed root module by
[@&#8203;fmeum](https://togithub.com/fmeum) in
[https://github.com/bazelbuild/rules_go/pull/3612](https://togithub.com/bazelbuild/rules_go/pull/3612)
- Adding Uber's hermetic cc toolchain for tests by
[@&#8203;linzhp](https://togithub.com/linzhp) in
[https://github.com/bazelbuild/rules_go/pull/3609](https://togithub.com/bazelbuild/rules_go/pull/3609)
- Implement functionality to skip tests via TESTBRIDGE_TEST_ONLY env by
[@&#8203;yushan26](https://togithub.com/yushan26) in
[https://github.com/bazelbuild/rules_go/pull/3618](https://togithub.com/bazelbuild/rules_go/pull/3618)

**Full Changelog**:
bazel-contrib/rules_go@v0.40.1...v0.41.0

#### `WORKSPACE` code

load("@&#8203;bazel_tools//tools/build_defs/repo:http.bzl",
"http_archive")

    http_archive(
        name = "io_bazel_rules_go",
sha256 =
"278b7ff5a826f3dc10f04feaf0b70d48b68748ccd512d7f98bf442077f043fe3",
        urls = [

"https://mirror.bazel.build/github.com/bazelbuild/rules_go/releases/download/v0.41.0/rules_go-v0.41.0.zip",

"https://github.com/bazelbuild/rules_go/releases/download/v0.41.0/rules_go-v0.41.0.zip",
        ],
    )

load("@&#8203;io_bazel_rules_go//go:deps.bzl", "go_register_toolchains",
"go_rules_dependencies")

    go_rules_dependencies()

    go_register_toolchains(version = "1.20.5")

</details>

---

### Configuration

📅 **Schedule**: Branch creation - At any time (no schedule defined),
Automerge - At any time (no schedule defined).

🚦 **Automerge**: Enabled.

♻ **Rebasing**: Whenever PR is behind base branch, or you tick the
rebase/retry checkbox.

🔕 **Ignore**: Close this PR and you won't be reminded about this update
again.

---

- [ ] <!-- rebase-check -->If you want to rebase/retry this PR, check
this box

---

This PR has been generated by [Mend
Renovate](https://www.mend.io/free-developer-tools/renovate/). View
repository job log
[here](https://developer.mend.io/github/cgrindel/bazel-starlib).

<!--renovate-debug:eyJjcmVhdGVkSW5WZXIiOiIzNi41LjMiLCJ1cGRhdGVkSW5WZXIiOiIzNi41LjMiLCJ0YXJnZXRCcmFuY2giOiJtYWluIn0=-->

Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
renovate bot referenced this pull request in kreempuff/rules_unreal_engine Jul 11, 2023
[![Mend
Renovate](https://app.renovatebot.com/images/banner.svg)](https://renovatebot.com)

This PR contains the following updates:

| Package | Type | Update | Change |
|---|---|---|---|
| [io_bazel_rules_go](https://togithub.com/bazelbuild/rules_go) |
http_archive | minor | `v0.40.1` -> `v0.41.0` |

---

### ⚠ Dependency Lookup Warnings ⚠

Warnings were logged while processing this repo. Please check the
Dependency Dashboard for more information.

---

### Release Notes

<details>
<summary>bazelbuild/rules_go (io_bazel_rules_go)</summary>

###
[`v0.41.0`](https://togithub.com/bazelbuild/rules_go/releases/tag/v0.41.0)

[Compare
Source](https://togithub.com/bazelbuild/rules_go/compare/v0.40.1...v0.41.0)

##### What's Changed

Starting this release, rules_go no longer ship with `@go_googleapis`
repo. Together with Gazelle v0.32.0, it means that all Go code importing
generated code from Google APIs will depend on
`@org_golang_google_genproto`, which is resolved by Go modules. For
proto files importing Google APIs proto, people needs to:

1.  Add an `http_archive` rule to download Google APIs, e.g.,

<!---->

    http_archive(
        name = "googleapis",
sha256 =
"9d1a930e767c93c825398b8f8692eca3fe353b9aaadedfbcf1fca2282c85df88",
strip_prefix = "googleapis-64926d52febbf298cb82a8f472ade4a3969ba922",
        urls = [

"https://github.com/googleapis/googleapis/archive/64926d52febbf298cb82a8f472ade4a3969ba922.zip",
        ],
    )

2. Resolve the proto manually. If Gazelle is being used, directives like
this needs to be added to a parent directory of the proto files:

<!---->

##### gazelle:resolve proto proto google/rpc/status.proto
@&#8203;googleapis//google/rpc:status_proto
##### gazelle:resolve proto go google/rpc/status.proto
@&#8203;org_golang_google_genproto//googleapis/rpc/status
##### gazelle:resolve proto google/longrunning/operations.proto
@&#8203;googleapis//google/longrunning:operations_proto
##### gazelle:resolve proto go google/longrunning/operations.proto
@&#8203;org_golang_google_genproto//googleapis/longrunning

Other changes included in this release

- detect_sdk_version: Support 1.21's new VERSION file format by
[@&#8203;evanj](https://togithub.com/evanj) in
[https://github.com/bazelbuild/rules_go/pull/3600](https://togithub.com/bazelbuild/rules_go/pull/3600)
- stdlib: Do not pass -static to cgo by
[@&#8203;zecke](https://togithub.com/zecke) in
[https://github.com/bazelbuild/rules_go/pull/3593](https://togithub.com/bazelbuild/rules_go/pull/3593)
- Expand stdlib packages when queried by
[@&#8203;JamyDev](https://togithub.com/JamyDev) in
[https://github.com/bazelbuild/rules_go/pull/3606](https://togithub.com/bazelbuild/rules_go/pull/3606)
- Fix stdlib file outputs pointing to processwrapper by
[@&#8203;JamyDev](https://togithub.com/JamyDev) in
[https://github.com/bazelbuild/rules_go/pull/3608](https://togithub.com/bazelbuild/rules_go/pull/3608)
- Fix `go_sdk` extension failure for unnamed root module by
[@&#8203;fmeum](https://togithub.com/fmeum) in
[https://github.com/bazelbuild/rules_go/pull/3612](https://togithub.com/bazelbuild/rules_go/pull/3612)
- Adding Uber's hermetic cc toolchain for tests by
[@&#8203;linzhp](https://togithub.com/linzhp) in
[https://github.com/bazelbuild/rules_go/pull/3609](https://togithub.com/bazelbuild/rules_go/pull/3609)
- Implement functionality to skip tests via TESTBRIDGE_TEST_ONLY env by
[@&#8203;yushan26](https://togithub.com/yushan26) in
[https://github.com/bazelbuild/rules_go/pull/3618](https://togithub.com/bazelbuild/rules_go/pull/3618)

**Full Changelog**:
bazel-contrib/rules_go@v0.40.1...v0.41.0

##### `WORKSPACE` code

load("@&#8203;bazel_tools//tools/build_defs/repo:http.bzl",
"http_archive")

    http_archive(
        name = "io_bazel_rules_go",
sha256 =
"278b7ff5a826f3dc10f04feaf0b70d48b68748ccd512d7f98bf442077f043fe3",
        urls = [

"https://mirror.bazel.build/github.com/bazelbuild/rules_go/releases/download/v0.41.0/rules_go-v0.41.0.zip",

"https://github.com/bazelbuild/rules_go/releases/download/v0.41.0/rules_go-v0.41.0.zip",
        ],
    )

load("@&#8203;io_bazel_rules_go//go:deps.bzl", "go_register_toolchains",
"go_rules_dependencies")

    go_rules_dependencies()

    go_register_toolchains(version = "1.20.5")

</details>

---

### Configuration

📅 **Schedule**: Branch creation - At any time (no schedule defined),
Automerge - At any time (no schedule defined).

🚦 **Automerge**: Enabled.

♻ **Rebasing**: Whenever PR becomes conflicted, or you tick the
rebase/retry checkbox.

🔕 **Ignore**: Close this PR and you won't be reminded about this update
again.

---

- [ ] <!-- rebase-check -->If you want to rebase/retry this PR, check
this box

---

This PR has been generated by [Mend
Renovate](https://www.mend.io/free-developer-tools/renovate/). View
repository job log
[here](https://developer.mend.io/github/kreempuff/rules_unreal_engine).

<!--renovate-debug:eyJjcmVhdGVkSW5WZXIiOiIzNi41LjMiLCJ1cGRhdGVkSW5WZXIiOiIzNi41LjMiLCJ0YXJnZXRCcmFuY2giOiJtYWluIn0=-->
renovate bot referenced this pull request in cgrindel/rules_swift_package_manager Jul 11, 2023
[![Mend
Renovate](https://app.renovatebot.com/images/banner.svg)](https://renovatebot.com)

This PR contains the following updates:

| Package | Type | Update | Change |
|---|---|---|---|
| [io_bazel_rules_go](https://togithub.com/bazelbuild/rules_go) |
http_archive | minor | `v0.40.1` -> `v0.41.0` |

---

### Release Notes

<details>
<summary>bazelbuild/rules_go (io_bazel_rules_go)</summary>

###
[`v0.41.0`](https://togithub.com/bazelbuild/rules_go/releases/tag/v0.41.0)

[Compare
Source](https://togithub.com/bazelbuild/rules_go/compare/v0.40.1...v0.41.0)

#### What's Changed

Starting this release, rules_go no longer ship with `@go_googleapis`
repo. Together with Gazelle v0.32.0, it means that all Go code importing
generated code from Google APIs will depend on
`@org_golang_google_genproto`, which is resolved by Go modules. For
proto files importing Google APIs proto, people needs to:

1.  Add an `http_archive` rule to download Google APIs, e.g.,

<!---->

    http_archive(
        name = "googleapis",
sha256 =
"9d1a930e767c93c825398b8f8692eca3fe353b9aaadedfbcf1fca2282c85df88",
strip_prefix = "googleapis-64926d52febbf298cb82a8f472ade4a3969ba922",
        urls = [

"https://github.com/googleapis/googleapis/archive/64926d52febbf298cb82a8f472ade4a3969ba922.zip",
        ],
    )

2. Resolve the proto manually. If Gazelle is being used, directives like
this needs to be added to a parent directory of the proto files:

<!---->

### gazelle:resolve proto proto google/rpc/status.proto
@&#8203;googleapis//google/rpc:status_proto
### gazelle:resolve proto go google/rpc/status.proto
@&#8203;org_golang_google_genproto//googleapis/rpc/status
### gazelle:resolve proto google/longrunning/operations.proto
@&#8203;googleapis//google/longrunning:operations_proto
### gazelle:resolve proto go google/longrunning/operations.proto
@&#8203;org_golang_google_genproto//googleapis/longrunning

Other changes included in this release

- detect_sdk_version: Support 1.21's new VERSION file format by
[@&#8203;evanj](https://togithub.com/evanj) in
[https://github.com/bazelbuild/rules_go/pull/3600](https://togithub.com/bazelbuild/rules_go/pull/3600)
- stdlib: Do not pass -static to cgo by
[@&#8203;zecke](https://togithub.com/zecke) in
[https://github.com/bazelbuild/rules_go/pull/3593](https://togithub.com/bazelbuild/rules_go/pull/3593)
- Expand stdlib packages when queried by
[@&#8203;JamyDev](https://togithub.com/JamyDev) in
[https://github.com/bazelbuild/rules_go/pull/3606](https://togithub.com/bazelbuild/rules_go/pull/3606)
- Fix stdlib file outputs pointing to processwrapper by
[@&#8203;JamyDev](https://togithub.com/JamyDev) in
[https://github.com/bazelbuild/rules_go/pull/3608](https://togithub.com/bazelbuild/rules_go/pull/3608)
- Fix `go_sdk` extension failure for unnamed root module by
[@&#8203;fmeum](https://togithub.com/fmeum) in
[https://github.com/bazelbuild/rules_go/pull/3612](https://togithub.com/bazelbuild/rules_go/pull/3612)
- Adding Uber's hermetic cc toolchain for tests by
[@&#8203;linzhp](https://togithub.com/linzhp) in
[https://github.com/bazelbuild/rules_go/pull/3609](https://togithub.com/bazelbuild/rules_go/pull/3609)
- Implement functionality to skip tests via TESTBRIDGE_TEST_ONLY env by
[@&#8203;yushan26](https://togithub.com/yushan26) in
[https://github.com/bazelbuild/rules_go/pull/3618](https://togithub.com/bazelbuild/rules_go/pull/3618)

**Full Changelog**:
bazel-contrib/rules_go@v0.40.1...v0.41.0

#### `WORKSPACE` code

load("@&#8203;bazel_tools//tools/build_defs/repo:http.bzl",
"http_archive")

    http_archive(
        name = "io_bazel_rules_go",
sha256 =
"278b7ff5a826f3dc10f04feaf0b70d48b68748ccd512d7f98bf442077f043fe3",
        urls = [

"https://mirror.bazel.build/github.com/bazelbuild/rules_go/releases/download/v0.41.0/rules_go-v0.41.0.zip",

"https://github.com/bazelbuild/rules_go/releases/download/v0.41.0/rules_go-v0.41.0.zip",
        ],
    )

load("@&#8203;io_bazel_rules_go//go:deps.bzl", "go_register_toolchains",
"go_rules_dependencies")

    go_rules_dependencies()

    go_register_toolchains(version = "1.20.5")

</details>

---

### Configuration

📅 **Schedule**: Branch creation - At any time (no schedule defined),
Automerge - At any time (no schedule defined).

🚦 **Automerge**: Enabled.

♻ **Rebasing**: Whenever PR is behind base branch, or you tick the
rebase/retry checkbox.

🔕 **Ignore**: Close this PR and you won't be reminded about this update
again.

---

- [ ] <!-- rebase-check -->If you want to rebase/retry this PR, check
this box

---

This PR has been generated by [Mend
Renovate](https://www.mend.io/free-developer-tools/renovate/). View
repository job log
[here](https://developer.mend.io/github/cgrindel/rules_swift_package_manager).

<!--renovate-debug:eyJjcmVhdGVkSW5WZXIiOiIzNi41LjMiLCJ1cGRhdGVkSW5WZXIiOiIzNi41LjMiLCJ0YXJnZXRCcmFuY2giOiJtYWluIn0=-->

Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

go1.20: Suddenly linking static binaries
3 participants