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

go-sqlite3 new_go_repository fails to build #274

Closed
gsf opened this issue Feb 22, 2017 · 4 comments
Closed

go-sqlite3 new_go_repository fails to build #274

gsf opened this issue Feb 22, 2017 · 4 comments

Comments

@gsf
Copy link

gsf commented Feb 22, 2017

Expected this to work as of #166 but getting undefined pthread references when go-sqlite3 is pointed at the v1.2.0 release commit. Here's the block in the WORKSPACE:

new_go_repository(
  name       = 'com_github_mattn_go_sqlite3',
  importpath = 'github.com/mattn/go-sqlite3',
  commit     = 'ca5e3819723d8eeaf170ad510e7da1d6d2e94a08',
)

And here's the error output:

# bazel build --verbose_failures @com_github_mattn_go_sqlite3//:go_default_library                             
INFO: Found 1 target...
ERROR: /root/.cache/bazel/_bazel_root/8cd13b75700216788cbc2043831c737d/external/com_github_mattn_go_sqlite3/BUILD:5:1: Linking of rule '@com_github_mattn_go_sqlite3//:cgo_default_library.cgo.dir/gen/_cgo_.o' failed: linux-sandbox failed: error executing command
  (cd /root/.cache/bazel/_bazel_root/8cd13b75700216788cbc2043831c737d/bazel-sandbox/680b43be-f2a1-4dc6-abff-a2622f463272-0/execroot/app && \
  exec env - \
  /root/.cache/bazel/_bazel_root/8cd13b75700216788cbc2043831c737d/execroot/app/_bin/linux-sandbox @/root/.cache/bazel/_bazel_root/8cd13b75700216788cbc2043831c737d/bazel-sandbox/680b43be-f2a1-4dc6-abff-a2622f463272-0/linux-sandbox.params -- /usr/bin/gcc -o bazel-out/local-fastbuild/bin/external/com_github_mattn_go_sqlite3/cgo_default_library.cgo.dir/gen/_cgo_.o -Wl,-no-as-needed -B/usr/bin -B/usr/bin -pass-exit-codes '-Wl,--build-id=md5' '-Wl,--hash-style=gnu' -Wl,-S -Wl,@bazel-out/local-fastbuild/bin/external/com_github_mattn_go_sqlite3/cgo_default_library.cgo.dir/gen/_cgo_.o-2.params).
bazel-out/local-fastbuild/bin/external/com_github_mattn_go_sqlite3/cgo_default_library.cgo.dir/gen/lib_cgo_lib.lo(sqlite3-binding.pic.o): In function `pthreadMutexAlloc':
sqlite3-binding.c:(.text+0x4162): undefined reference to `pthread_mutexattr_init'
sqlite3-binding.c:(.text+0x4173): undefined reference to `pthread_mutexattr_settype'
sqlite3-binding.c:(.text+0x4192): undefined reference to `pthread_mutexattr_destroy'
bazel-out/local-fastbuild/bin/external/com_github_mattn_go_sqlite3/cgo_default_library.cgo.dir/gen/lib_cgo_lib.lo(sqlite3-binding.pic.o): In function `pthreadMutexTry':
sqlite3-binding.c:(.text+0x425b): undefined reference to `pthread_mutex_trylock'
bazel-out/local-fastbuild/bin/external/com_github_mattn_go_sqlite3/cgo_default_library.cgo.dir/gen/lib_cgo_lib.lo(sqlite3-binding.pic.o): In function `sqlite3ThreadCreate':
sqlite3-binding.c:(.text+0x85c4): undefined reference to `pthread_create'
bazel-out/local-fastbuild/bin/external/com_github_mattn_go_sqlite3/cgo_default_library.cgo.dir/gen/lib_cgo_lib.lo(sqlite3-binding.pic.o): In function `sqlite3ThreadJoin':
sqlite3-binding.c:(.text+0x8659): undefined reference to `pthread_join'
collect2: error: ld returned 1 exit status
Target @com_github_mattn_go_sqlite3//:go_default_library failed to build
INFO: Elapsed time: 0.183s, Critical Path: 0.09s
@pmbethe09
Copy link
Contributor

pmbethe09 commented Feb 22, 2017 via email

@jayconrod
Copy link
Contributor

I was able to reproduce this with Bazel 0.4.4 and rules_go both at HEAD and 0.4.0. I'm building from Goobuntu 14.04. @pmbethe09, are you building on a different platform?

I worked around this by checking out go-sqlite3 directly, generating a BUILD file with gazelle, then manually adding "-pthread" to "clinkopts" of ":cgo_default_library".

It looks like Gazelle generates clinkopts by taking go/build.Package.CgoLDFLAGS. I didn't see "-pthread" anywhere in the "#cgo LDFLAGS" comments of that library. Maybe it should be in there? I don't know how Gazelle would infer this on its own.

@achew22
Copy link
Member

achew22 commented Feb 22, 2017

I was able to fix this in my local rules_go by doing this. I didn't have the time to investigate upstreaming it yet but this seems like a good time to point out that it works when that flag is added.

@jayconrod
Copy link
Contributor

I looked into this a bit further. "go build" always adds "-mthreads" (for Windows) or "-pthread" (everything else) to the command line if Cgo is enabled. There is no need for Cgo or Gazelle to infer this flag. We should just add these to the default link options, as @achew22 mentioned.

I'll put together a PR to fix this.

jayconrod pushed a commit to jayconrod/rules_go that referenced this issue Feb 27, 2017
This matches behavior of "go build", which compiles and links C code
with -pthread (or -mthreads on Windows).

Fixes bazel-contrib#274
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