[go] cgo: Check presence of a C compiler

766 views
Skip to first unread message

Andrey Bokhanko (Gerrit)

unread,
Mar 12, 2021, 11:25:08 AM3/12/21
to goph...@pubsubhelper.golang.org, golang-co...@googlegroups.com

Andrey Bokhanko has uploaded this change for review.

View Change

cgo: Check presence of a C compiler

Currently we print a cryptic message is a C compiler doesn't exist.
This patch adds more graceful handling.

Fixes #44271

Change-Id: I44f16ef6eb2853fee22fa1d996e41ec6c9ee82f1
---
M src/cmd/cgo/main.go
1 file changed, 10 insertions(+), 0 deletions(-)

diff --git a/src/cmd/cgo/main.go b/src/cmd/cgo/main.go
index 5767c54..c7755b5 100644
--- a/src/cmd/cgo/main.go
+++ b/src/cmd/cgo/main.go
@@ -20,6 +20,7 @@
"io"
"io/ioutil"
"os"
+ "os/exec"
"path/filepath"
"reflect"
"runtime"
@@ -302,6 +303,15 @@

p := newPackage(args[:i])

+ // We need a C compiler to be available. Check this.
+ gccName := p.gccBaseCmd()[0]
+ _, err := exec.LookPath(gccName)
+ if err != nil {
+ fatalf("C compiler '%s' can't be found. Check that you have a C compiler "+
+ "installed and added to your PATH.", gccName)
+ os.Exit(2)
+ }
+
// Record CGO_LDFLAGS from the environment for external linking.
if ldflags := os.Getenv("CGO_LDFLAGS"); ldflags != "" {
args, err := splitQuoted(ldflags)

To view, visit change 301249. To unsubscribe, or for help writing mail filters, visit settings.

Gerrit-Project: go
Gerrit-Branch: master
Gerrit-Change-Id: I44f16ef6eb2853fee22fa1d996e41ec6c9ee82f1
Gerrit-Change-Number: 301249
Gerrit-PatchSet: 1
Gerrit-Owner: Andrey Bokhanko <andreyb...@gmail.com>
Gerrit-MessageType: newchange

Andrey Bokhanko (Gerrit)

unread,
Mar 12, 2021, 11:36:27 AM3/12/21
to goph...@pubsubhelper.golang.org, golang-co...@googlegroups.com

Andrey Bokhanko uploaded patch set #2 to this change.

View Change

cgo: Check presence of a C compiler

Currently we print a cryptic message is a C compiler doesn't exist.
This patch adds more graceful handling.

Fixes #44271

Change-Id: I44f16ef6eb2853fee22fa1d996e41ec6c9ee82f1
---
M src/cmd/cgo/main.go
1 file changed, 10 insertions(+), 0 deletions(-)

To view, visit change 301249. To unsubscribe, or for help writing mail filters, visit settings.

Gerrit-Project: go
Gerrit-Branch: master
Gerrit-Change-Id: I44f16ef6eb2853fee22fa1d996e41ec6c9ee82f1
Gerrit-Change-Number: 301249
Gerrit-PatchSet: 2
Gerrit-Owner: Andrey Bokhanko <andreyb...@gmail.com>
Gerrit-MessageType: newpatchset

David Chase (Gerrit)

unread,
Mar 12, 2021, 12:20:55 PM3/12/21
to Andrey Bokhanko, goph...@pubsubhelper.golang.org, Ian Lance Taylor, golang-co...@googlegroups.com

Attention is currently required from: Andrey Bokhanko, Ian Lance Taylor.

View Change

2 comments:

    • C compiler \"%s\" can't be found. Check that you have a C compiler "+

    • 			"installed and added to your PATH.

    • Definitely an improvement. I think I would add mention of cgo so that when cgo is invoked behind the curtains (for example, in the case of race detection), it provides a clue to reduce the size of a user's problem space.

      So, maybe:
      "C compiler \"%s\" can't be found for cgo command. Check that you have a C compiler "+

    • "installed and added to your PATH."
    • Ian, or you, may have a better way to phrase this.

To view, visit change 301249. To unsubscribe, or for help writing mail filters, visit settings.

Gerrit-Project: go
Gerrit-Branch: master
Gerrit-Change-Id: I44f16ef6eb2853fee22fa1d996e41ec6c9ee82f1
Gerrit-Change-Number: 301249
Gerrit-PatchSet: 2
Gerrit-Owner: Andrey Bokhanko <andreyb...@gmail.com>
Gerrit-Reviewer: Ian Lance Taylor <ia...@golang.org>
Gerrit-CC: David Chase <drc...@google.com>
Gerrit-Attention: Andrey Bokhanko <andreyb...@gmail.com>
Gerrit-Attention: Ian Lance Taylor <ia...@golang.org>
Gerrit-Comment-Date: Fri, 12 Mar 2021 17:20:45 +0000
Gerrit-HasComments: Yes
Gerrit-Has-Labels: No
Gerrit-MessageType: comment

Andrey Bokhanko (Gerrit)

unread,
Mar 15, 2021, 3:48:24 AM3/15/21
to goph...@pubsubhelper.golang.org, Ian Lance Taylor, David Chase, golang-co...@googlegroups.com

Attention is currently required from: David Chase, Ian Lance Taylor.

View Change

1 comment:

  • File src/cmd/cgo/main.go:

    • Patch Set #2, Line 310:

      C compiler \"%s\" can't be found. Check that you have a C compiler "+
      "installed and added to your PATH.

    • Definitely an improvement. […]

      Thanks!

      As for mentioning cgo, fatalf already prints "cgo:" prefix, so on my Linux box the message looks like this:

      cgo: C compiler "gcc" can't be found. Check that you have a C compiler installed and added to your PATH.

To view, visit change 301249. To unsubscribe, or for help writing mail filters, visit settings.

Gerrit-Project: go
Gerrit-Branch: master
Gerrit-Change-Id: I44f16ef6eb2853fee22fa1d996e41ec6c9ee82f1
Gerrit-Change-Number: 301249
Gerrit-PatchSet: 2
Gerrit-Owner: Andrey Bokhanko <andreyb...@gmail.com>
Gerrit-Reviewer: Ian Lance Taylor <ia...@golang.org>
Gerrit-CC: David Chase <drc...@google.com>
Gerrit-Attention: David Chase <drc...@google.com>
Gerrit-Attention: Ian Lance Taylor <ia...@golang.org>
Gerrit-Comment-Date: Mon, 15 Mar 2021 07:48:16 +0000
Gerrit-HasComments: Yes
Gerrit-Has-Labels: No
Comment-In-Reply-To: David Chase <drc...@google.com>
Gerrit-MessageType: comment

David Chase (Gerrit)

unread,
Mar 15, 2021, 11:00:53 AM3/15/21
to Andrey Bokhanko, goph...@pubsubhelper.golang.org, Ian Lance Taylor, golang-co...@googlegroups.com

Attention is currently required from: Andrey Bokhanko, Ian Lance Taylor.

Patch set 2:Code-Review +2

View Change

1 comment:

  • File src/cmd/cgo/main.go:

    • Patch Set #2, Line 310:

      C compiler \"%s\" can't be found. Check that you have a C compiler "+
      "installed and added to your PATH.

    • Thanks! […]

      Great!
      I'd like to wait a hair longer for Ian, but for now, LGTM.
      Actually I think we have to anyway, might need a second "trust" vote, not sure.

To view, visit change 301249. To unsubscribe, or for help writing mail filters, visit settings.

Gerrit-Project: go
Gerrit-Branch: master
Gerrit-Change-Id: I44f16ef6eb2853fee22fa1d996e41ec6c9ee82f1
Gerrit-Change-Number: 301249
Gerrit-PatchSet: 2
Gerrit-Owner: Andrey Bokhanko <andreyb...@gmail.com>
Gerrit-Reviewer: David Chase <drc...@google.com>
Gerrit-Reviewer: Ian Lance Taylor <ia...@golang.org>
Gerrit-Attention: Andrey Bokhanko <andreyb...@gmail.com>
Gerrit-Attention: Ian Lance Taylor <ia...@golang.org>
Gerrit-Comment-Date: Mon, 15 Mar 2021 15:00:47 +0000
Gerrit-HasComments: Yes
Gerrit-Has-Labels: Yes
Comment-In-Reply-To: Andrey Bokhanko <andreyb...@gmail.com>

Ian Lance Taylor (Gerrit)

unread,
Mar 15, 2021, 5:28:16 PM3/15/21
to Andrey Bokhanko, goph...@pubsubhelper.golang.org, David Chase, golang-co...@googlegroups.com

Attention is currently required from: Andrey Bokhanko.

View Change

3 comments:

  • Commit Message:

  • File src/cmd/cgo/main.go:

    • Patch Set #2, Line 310: fatalf("C compiler \"%s\" can't be found. Check that you have a C compiler "+

      Use %q instead of \"%s\".

      Also no need for a line break here, there is no line length limit.

    • Patch Set #2, Line 311: "installed and added to your PATH.", gccName)

      I think a message like
      fatalf("C compiler %q not found: %v", gccName, err)
      will suffice. The error returned by exec.LookPath will help.

To view, visit change 301249. To unsubscribe, or for help writing mail filters, visit settings.

Gerrit-Project: go
Gerrit-Branch: master
Gerrit-Change-Id: I44f16ef6eb2853fee22fa1d996e41ec6c9ee82f1
Gerrit-Change-Number: 301249
Gerrit-PatchSet: 2
Gerrit-Owner: Andrey Bokhanko <andreyb...@gmail.com>
Gerrit-Reviewer: David Chase <drc...@google.com>
Gerrit-Reviewer: Ian Lance Taylor <ia...@golang.org>
Gerrit-Attention: Andrey Bokhanko <andreyb...@gmail.com>
Gerrit-Comment-Date: Mon, 15 Mar 2021 21:28:11 +0000
Gerrit-HasComments: Yes
Gerrit-Has-Labels: No
Gerrit-MessageType: comment

Andrey Bokhanko (Gerrit)

unread,
Mar 16, 2021, 2:46:04 AM3/16/21
to goph...@pubsubhelper.golang.org, golang-co...@googlegroups.com

Attention is currently required from: Andrey Bokhanko.

Andrey Bokhanko uploaded patch set #3 to this change.

View Change

cgo: Check presence of a C compiler

Currently we print a cryptic message if a C compiler doesn't exist.

This patch adds more graceful handling.

Fixes #44271

Change-Id: I44f16ef6eb2853fee22fa1d996e41ec6c9ee82f1
---
M src/cmd/cgo/main.go
1 file changed, 9 insertions(+), 0 deletions(-)

To view, visit change 301249. To unsubscribe, or for help writing mail filters, visit settings.

Gerrit-Project: go
Gerrit-Branch: master
Gerrit-Change-Id: I44f16ef6eb2853fee22fa1d996e41ec6c9ee82f1
Gerrit-Change-Number: 301249
Gerrit-PatchSet: 3
Gerrit-Owner: Andrey Bokhanko <andreyb...@gmail.com>
Gerrit-Reviewer: David Chase <drc...@google.com>
Gerrit-Reviewer: Ian Lance Taylor <ia...@golang.org>
Gerrit-Attention: Andrey Bokhanko <andreyb...@gmail.com>
Gerrit-MessageType: newpatchset

Andrey Bokhanko (Gerrit)

unread,
Mar 16, 2021, 2:48:14 AM3/16/21
to goph...@pubsubhelper.golang.org, David Chase, Ian Lance Taylor, golang-co...@googlegroups.com

Attention is currently required from: Ian Lance Taylor.

View Change

2 comments:

  • Patchset:

    • Patch Set #2:

      Ian, thanks for the review! Fixed you comment; please take a look.

  • File src/cmd/cgo/main.go:

    • Patch Set #2, Line 311: "installed and added to your PATH.", gccName)

      I think a message like […]

      Done as you suggested. Now the message looks like so (on my Linux box):

      $ export CC=clang
      $ go build ./issue44271.go
      # runtime/cgo
      cgo: C compiler "clang" not found: exec: "clang": executable file not found in $PATH

To view, visit change 301249. To unsubscribe, or for help writing mail filters, visit settings.

Gerrit-Project: go
Gerrit-Branch: master
Gerrit-Change-Id: I44f16ef6eb2853fee22fa1d996e41ec6c9ee82f1
Gerrit-Change-Number: 301249
Gerrit-PatchSet: 2
Gerrit-Owner: Andrey Bokhanko <andreyb...@gmail.com>
Gerrit-Reviewer: David Chase <drc...@google.com>
Gerrit-Reviewer: Ian Lance Taylor <ia...@golang.org>
Gerrit-Attention: Ian Lance Taylor <ia...@golang.org>
Gerrit-Comment-Date: Tue, 16 Mar 2021 06:48:08 +0000
Gerrit-HasComments: Yes
Gerrit-Has-Labels: No
Comment-In-Reply-To: Ian Lance Taylor <ia...@golang.org>
Gerrit-MessageType: comment

Andrey Bokhanko (Gerrit)

unread,
Mar 16, 2021, 2:49:19 AM3/16/21
to goph...@pubsubhelper.golang.org, David Chase, Ian Lance Taylor, golang-co...@googlegroups.com

Attention is currently required from: Ian Lance Taylor.

View Change

1 comment:

  • File src/cmd/cgo/main.go:

    • Patch Set #2, Line 310: fatalf("C compiler \"%s\" can't be found. Check that you have a C compiler "+

      Use %q instead of \"%s\". […]

      Fixed

To view, visit change 301249. To unsubscribe, or for help writing mail filters, visit settings.

Gerrit-Project: go
Gerrit-Branch: master
Gerrit-Change-Id: I44f16ef6eb2853fee22fa1d996e41ec6c9ee82f1
Gerrit-Change-Number: 301249
Gerrit-PatchSet: 3
Gerrit-Owner: Andrey Bokhanko <andreyb...@gmail.com>
Gerrit-Reviewer: David Chase <drc...@google.com>
Gerrit-Reviewer: Ian Lance Taylor <ia...@golang.org>
Gerrit-Attention: Ian Lance Taylor <ia...@golang.org>
Gerrit-Comment-Date: Tue, 16 Mar 2021 06:49:14 +0000

Ian Lance Taylor (Gerrit)

unread,
Mar 16, 2021, 8:25:30 PM3/16/21
to Andrey Bokhanko, goph...@pubsubhelper.golang.org, David Chase, golang-co...@googlegroups.com

Attention is currently required from: Andrey Bokhanko.

View Change

1 comment:

  • Commit Message:

To view, visit change 301249. To unsubscribe, or for help writing mail filters, visit settings.

Gerrit-Project: go
Gerrit-Branch: master
Gerrit-Change-Id: I44f16ef6eb2853fee22fa1d996e41ec6c9ee82f1
Gerrit-Change-Number: 301249
Gerrit-PatchSet: 3
Gerrit-Owner: Andrey Bokhanko <andreyb...@gmail.com>
Gerrit-Reviewer: David Chase <drc...@google.com>
Gerrit-Reviewer: Ian Lance Taylor <ia...@golang.org>
Gerrit-Attention: Andrey Bokhanko <andreyb...@gmail.com>
Gerrit-Comment-Date: Wed, 17 Mar 2021 00:25:25 +0000

Andrey Bokhanko (Gerrit)

unread,
Mar 17, 2021, 3:52:13 AM3/17/21
to goph...@pubsubhelper.golang.org, golang-co...@googlegroups.com

Attention is currently required from: Andrey Bokhanko.

Andrey Bokhanko uploaded patch set #4 to this change.

View Change

cgo: check whether C compiler exists

Currently we print a cryptic message if a C compiler doesn't exist.
This patch adds more graceful handling.

Fixes #44271

Change-Id: I44f16ef6eb2853fee22fa1d996e41ec6c9ee82f1
---
M src/cmd/cgo/main.go
1 file changed, 9 insertions(+), 0 deletions(-)

To view, visit change 301249. To unsubscribe, or for help writing mail filters, visit settings.

Gerrit-Project: go
Gerrit-Branch: master
Gerrit-Change-Id: I44f16ef6eb2853fee22fa1d996e41ec6c9ee82f1
Gerrit-Change-Number: 301249
Gerrit-PatchSet: 4
Gerrit-Owner: Andrey Bokhanko <andreyb...@gmail.com>
Gerrit-Reviewer: David Chase <drc...@google.com>
Gerrit-Reviewer: Ian Lance Taylor <ia...@golang.org>
Gerrit-Attention: Andrey Bokhanko <andreyb...@gmail.com>
Gerrit-MessageType: newpatchset

Andrey Bokhanko (Gerrit)

unread,
Mar 17, 2021, 3:53:08 AM3/17/21
to goph...@pubsubhelper.golang.org, David Chase, Ian Lance Taylor, golang-co...@googlegroups.com

Attention is currently required from: Ian Lance Taylor.

View Change

1 comment:

  • Commit Message:

To view, visit change 301249. To unsubscribe, or for help writing mail filters, visit settings.

Gerrit-Project: go
Gerrit-Branch: master
Gerrit-Change-Id: I44f16ef6eb2853fee22fa1d996e41ec6c9ee82f1
Gerrit-Change-Number: 301249
Gerrit-PatchSet: 4
Gerrit-Owner: Andrey Bokhanko <andreyb...@gmail.com>
Gerrit-Reviewer: David Chase <drc...@google.com>
Gerrit-Reviewer: Ian Lance Taylor <ia...@golang.org>
Gerrit-Attention: Ian Lance Taylor <ia...@golang.org>
Gerrit-Comment-Date: Wed, 17 Mar 2021 07:53:01 +0000

Andrey Bokhanko (Gerrit)

unread,
Mar 17, 2021, 3:53:54 AM3/17/21
to goph...@pubsubhelper.golang.org, golang-co...@googlegroups.com

Attention is currently required from: Ian Lance Taylor.

Andrey Bokhanko uploaded patch set #5 to this change.

View Change

cmd/cgo: check whether C compiler exists


Currently we print a cryptic message if a C compiler doesn't exist.
This patch adds more graceful handling.

Fixes #44271

Change-Id: I44f16ef6eb2853fee22fa1d996e41ec6c9ee82f1
---
M src/cmd/cgo/main.go
1 file changed, 9 insertions(+), 0 deletions(-)

To view, visit change 301249. To unsubscribe, or for help writing mail filters, visit settings.

Gerrit-Project: go
Gerrit-Branch: master
Gerrit-Change-Id: I44f16ef6eb2853fee22fa1d996e41ec6c9ee82f1
Gerrit-Change-Number: 301249
Gerrit-PatchSet: 5
Gerrit-Owner: Andrey Bokhanko <andreyb...@gmail.com>
Gerrit-Reviewer: David Chase <drc...@google.com>
Gerrit-Reviewer: Ian Lance Taylor <ia...@golang.org>
Gerrit-Attention: Ian Lance Taylor <ia...@golang.org>
Gerrit-MessageType: newpatchset

Ian Lance Taylor (Gerrit)

unread,
Mar 17, 2021, 3:53:34 PM3/17/21
to Andrey Bokhanko, goph...@pubsubhelper.golang.org, David Chase, golang-co...@googlegroups.com

Attention is currently required from: Andrey Bokhanko.

Patch set 5:Run-TryBot +1

View Change

    To view, visit change 301249. To unsubscribe, or for help writing mail filters, visit settings.

    Gerrit-Project: go
    Gerrit-Branch: master
    Gerrit-Change-Id: I44f16ef6eb2853fee22fa1d996e41ec6c9ee82f1
    Gerrit-Change-Number: 301249
    Gerrit-PatchSet: 5
    Gerrit-Owner: Andrey Bokhanko <andreyb...@gmail.com>
    Gerrit-Reviewer: David Chase <drc...@google.com>
    Gerrit-Reviewer: Ian Lance Taylor <ia...@golang.org>
    Gerrit-Attention: Andrey Bokhanko <andreyb...@gmail.com>
    Gerrit-Comment-Date: Wed, 17 Mar 2021 19:53:28 +0000
    Gerrit-HasComments: No
    Gerrit-Has-Labels: Yes
    Gerrit-MessageType: comment

    Ian Lance Taylor (Gerrit)

    unread,
    Mar 17, 2021, 4:29:13 PM3/17/21
    to Andrey Bokhanko, goph...@pubsubhelper.golang.org, Go Bot, David Chase, golang-co...@googlegroups.com

    Attention is currently required from: Andrey Bokhanko.

    Patch set 5:Code-Review +2

    View Change

    1 comment:

    To view, visit change 301249. To unsubscribe, or for help writing mail filters, visit settings.

    Gerrit-Project: go
    Gerrit-Branch: master
    Gerrit-Change-Id: I44f16ef6eb2853fee22fa1d996e41ec6c9ee82f1
    Gerrit-Change-Number: 301249
    Gerrit-PatchSet: 5
    Gerrit-Owner: Andrey Bokhanko <andreyb...@gmail.com>
    Gerrit-Reviewer: David Chase <drc...@google.com>
    Gerrit-Reviewer: Go Bot <go...@golang.org>
    Gerrit-Reviewer: Ian Lance Taylor <ia...@golang.org>
    Gerrit-Attention: Andrey Bokhanko <andreyb...@gmail.com>
    Gerrit-Comment-Date: Wed, 17 Mar 2021 20:29:08 +0000
    Gerrit-HasComments: Yes
    Gerrit-Has-Labels: Yes
    Gerrit-MessageType: comment

    Ian Lance Taylor (Gerrit)

    unread,
    Mar 17, 2021, 4:29:27 PM3/17/21
    to Andrey Bokhanko, goph...@pubsubhelper.golang.org, golang-...@googlegroups.com, Go Bot, David Chase, golang-co...@googlegroups.com

    Ian Lance Taylor submitted this change.

    View Change

    Approvals: Ian Lance Taylor: Looks good to me, approved; Run TryBots David Chase: Looks good to me, approved Go Bot: TryBots succeeded
    cmd/cgo: check whether C compiler exists

    Currently we print a cryptic message if a C compiler doesn't exist.
    This patch adds more graceful handling.

    Fixes #44271

    Change-Id: I44f16ef6eb2853fee22fa1d996e41ec6c9ee82f1
    Reviewed-on: https://go-review.googlesource.com/c/go/+/301249
    Run-TryBot: Ian Lance Taylor <ia...@golang.org>
    TryBot-Result: Go Bot <go...@golang.org>
    Reviewed-by: Ian Lance Taylor <ia...@golang.org>
    Reviewed-by: David Chase <drc...@google.com>

    ---
    M src/cmd/cgo/main.go
    1 file changed, 9 insertions(+), 0 deletions(-)

    diff --git a/src/cmd/cgo/main.go b/src/cmd/cgo/main.go
    index 5767c54..77ac5e0 100644

    --- a/src/cmd/cgo/main.go
    +++ b/src/cmd/cgo/main.go
    @@ -20,6 +20,7 @@
    "io"
    "io/ioutil"
    "os"
    + "os/exec"
    "path/filepath"
    "reflect"
    "runtime"
    @@ -302,6 +303,14 @@


    p := newPackage(args[:i])

    + // We need a C compiler to be available. Check this.
    + gccName := p.gccBaseCmd()[0]
    + _, err := exec.LookPath(gccName)
    + if err != nil {
    +		fatalf("C compiler %q not found: %v", gccName, err)

    + os.Exit(2)
    + }
    +
    // Record CGO_LDFLAGS from the environment for external linking.
    if ldflags := os.Getenv("CGO_LDFLAGS"); ldflags != "" {
    args, err := splitQuoted(ldflags)

    To view, visit change 301249. To unsubscribe, or for help writing mail filters, visit settings.

    Gerrit-Project: go
    Gerrit-Branch: master
    Gerrit-Change-Id: I44f16ef6eb2853fee22fa1d996e41ec6c9ee82f1
    Gerrit-Change-Number: 301249
    Gerrit-PatchSet: 6
    Gerrit-Owner: Andrey Bokhanko <andreyb...@gmail.com>
    Gerrit-Reviewer: David Chase <drc...@google.com>
    Gerrit-Reviewer: Go Bot <go...@golang.org>
    Gerrit-Reviewer: Ian Lance Taylor <ia...@golang.org>
    Gerrit-MessageType: merged
    Reply all
    Reply to author
    Forward
    0 new messages