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

Unable to build simple Go program that makes use of C bindings #25959

Closed
javgh opened this issue May 21, 2017 · 5 comments
Closed

Unable to build simple Go program that makes use of C bindings #25959

javgh opened this issue May 21, 2017 · 5 comments

Comments

@javgh
Copy link

javgh commented May 21, 2017

Issue description

Using go + stdenv.cc I am unable to build a simple Go program that makes use of C bindings. The program is a testcase from the Go compiler repository ( https://raw.githubusercontent.com/golang/go/master/misc/cgo/errors/issue14669.go ) . The build fails with an error message like this:

# command-line-arguments
cgo-dwarf-inference:2:8: error: enumerator value for '__cgo_enum__0' is not an integer constant
cgo-dwarf-inference:5:8: error: enumerator value for '__cgo_enum__2' is not an integer constant
cgo-dwarf-inference:8:2: error: initializer element is not constant
cgo-dwarf-inference:8:2: note: (near initialization for '__cgodebug_data[0]')
cgo-dwarf-inference:10:2: error: initializer element is not constant
cgo-dwarf-inference:10:2: note: (near initialization for '__cgodebug_data[2]')

Steps to reproduce

nix-shell --pure -p stdenv.cc go wget
wget https://raw.githubusercontent.com/golang/go/master/misc/cgo/errors/issue14669.go
go build issue14669.go

warning: GOPATH set to GOROOT (/nix/store/56c4drb35ww6475r8w43i6706mzyma6p-go-1.8.1/share/go) has no effect
# command-line-arguments
cgo-dwarf-inference:2:8: error: enumerator value for '__cgo_enum__0' is not an integer constant
cgo-dwarf-inference:5:8: error: enumerator value for '__cgo_enum__2' is not an integer constant
cgo-dwarf-inference:8:2: error: initializer element is not constant
cgo-dwarf-inference:8:2: note: (near initialization for '__cgodebug_data[0]')
cgo-dwarf-inference:10:2: error: initializer element is not constant
cgo-dwarf-inference:10:2: note: (near initialization for '__cgodebug_data[2]')

Another example is geth, the go ethereum client, in version 1.6.1:

nix-shell --pure -p stdenv.cc go wget
wget https://github.com/ethereum/go-ethereum/archive/v1.6.1.tar.gz
tar xf v1.6.1.tar.gz
cd go-ethereum-1.6.1
make

[ ... some output omitted ... ]
github.com/ethereum/go-ethereum/vendor/github.com/naoina/toml
github.com/ethereum/go-ethereum/p2p/nat
github.com/ethereum/go-ethereum/vendor/github.com/gizak/termui
# github.com/ethereum/go-ethereum/vendor/github.com/karalabe/hid
cgo-dwarf-inference:2:8: error: enumerator value for '__cgo_enum__0' is not an integer constant
cgo-dwarf-inference:10:8: error: enumerator value for '__cgo_enum__7' is not an integer constant
cgo-dwarf-inference:18:2: error: initializer element is not constant
cgo-dwarf-inference:18:2: note: (near initialization for '__cgodebug_data[0]')
cgo-dwarf-inference:25:2: error: initializer element is not constant
cgo-dwarf-inference:25:2: note: (near initialization for '__cgodebug_data[7]')
github.com/ethereum/go-ethereum/internal/jsre
github.com/ethereum/go-ethereum/console
github.com/ethereum/go-ethereum/crypto
[ ... some output omitted ... ]
github.com/ethereum/go-ethereum/miner
github.com/ethereum/go-ethereum/light
github.com/ethereum/go-ethereum/eth/gasprice
util.go:44: exit status 2
exit status 1
make: *** [Makefile:15: geth] Error 1

Switching to clang instead seems to be a workaround for the problem:

nix-shell --pure -p clang go wget
wget https://raw.githubusercontent.com/golang/go/master/misc/cgo/errors/issue14669.go
go build issue14669.go

This last command builds successfully.

Technical details

  • System: 17.09.git.bb37fc9 (Hummingbird)
  • Nix version: nix-env (Nix) 1.11.9
  • Nixpkgs version: 17.09.git.bb37fc90c7
  • Sandboxing enabled: false
@matthewbauer
Copy link
Member

I think this has to do with how Go handles GOROOT. It expects to be able to write to $GOROOT even though it's a read-only path. Could you try the buildCommand in go-ethereum:

https://github.com/NixOS/nixpkgs/blob/master/pkgs/applications/altcoins/go-ethereum.nix#L18-L21

javgh added a commit to javgh/nixpkgs that referenced this issue May 22, 2017
Uses clang as a workaround for NixOS#25959.
@javgh
Copy link
Author

javgh commented May 22, 2017

Aw, I was wondering how to get rid of that warning. The warning is gone, but it doesn't seem to have helped:

nix-shell --pure -p stdenv.cc go wget which
export GOROOT=$(mktemp -d --suffix=-goroot)
which go
  /nix/store/56c4drb35ww6475r8w43i6706mzyma6p-go-1.8.1/bin/go
ln -sv /nix/store/56c4drb35ww6475r8w43i6706mzyma6p-go-1.8.1/share/go/* $GOROOT
ln -svf /nix/store/56c4drb35ww6475r8w43i6706mzyma6p-go-1.8.1/bin $GOROOT 
wget https://raw.githubusercontent.com/golang/go/master/misc/cgo/errors/issue14669.go
go build issue14669.go

# command-line-arguments
cgo-dwarf-inference:2:8: error: enumerator value for '__cgo_enum__0' is not an integer constant
cgo-dwarf-inference:5:8: error: enumerator value for '__cgo_enum__2' is not an integer constant
cgo-dwarf-inference:8:2: error: initializer element is not constant
cgo-dwarf-inference:8:2: note: (near initialization for '__cgodebug_data[0]')
cgo-dwarf-inference:10:2: error: initializer element is not constant
cgo-dwarf-inference:10:2: note: (near initialization for '__cgodebug_data[2]')

I originally ran into this issue when I tried to upgrade that linked Nix expression. Version 1.4.7 of go-ethereum compiles just fine, but 1.6.1 doesn't, because now one of the dependencies makes use of some kind of C bindings.

While playing around with it I also switched over to using buildGoPackage in the Nix expression, but that also didn't help. Switching to clang helped in the end. That version can be found here: https://github.com/javgh/nixpkgs/blob/go-ethereum/pkgs/applications/altcoins/go-ethereum.nix

But that's just a workaround and I would assume that other Go packages will at some point also run into this problem when they just use stdenv.cc or buildGoPackage.

@hhomar
Copy link
Contributor

hhomar commented Jul 22, 2017

I ran into this problem adding seccomp support to the docker package. It's caused by the -O2 optimization flag which is being added by the fortify hardening flag.

A possible fix would be to add hardeningDisable = [ "fortify" ]; to the buildGoPackage.

@prusnak
Copy link
Member

prusnak commented Oct 27, 2019

I can't no longer reproduce this on my system so I guess the issue is fixed?

nixos-20.03pre198214.4cd2cb43fb3

I created a PR to get rid of the workarounds: #72093

prusnak added a commit to prusnak/nixpkgs that referenced this issue Oct 27, 2019
it seems that the issue NixOS#25959 is not present anymore
@javgh
Copy link
Author

javgh commented Oct 28, 2019

I can confirm that I also cannot reproduce this anymore on my system. I am closing this - thanks!

@javgh javgh closed this as completed Oct 28, 2019
dtzWill pushed a commit to dtzWill/nixpkgs that referenced this issue Jan 27, 2020
it seems that the issue NixOS#25959 is not present anymore

(cherry picked from commit afd38ea)
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

No branches or pull requests

4 participants