From fed59953aad40d89730ebfc6f33b17116c42abcf Mon Sep 17 00:00:00 2001 From: Eyal Soha Date: Fri, 20 Mar 2020 18:22:49 -0700 Subject: Statically link libpthread for static c++ binaries. The posix_server works fine when run in locally or in docker but fails in the kokoro GCP build environment. Linking libpthread statically fixes it. PiperOrigin-RevId: 302139082 --- tools/bazeldefs/defs.bzl | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) (limited to 'tools/bazeldefs') diff --git a/tools/bazeldefs/defs.bzl b/tools/bazeldefs/defs.bzl index 64171ad8d..0a74370a6 100644 --- a/tools/bazeldefs/defs.bzl +++ b/tools/bazeldefs/defs.bzl @@ -65,10 +65,17 @@ def cc_binary(name, static = False, **kwargs): **kwargs: the rest of the args. """ if static: - if "linkopts" in kwargs: - kwargs["linkopts"] += ["-static", "-lstdc++"] - else: - kwargs["linkopts"] = ["-static", "-lstdc++"] + # How to statically link a c++ program that uses threads, like for gRPC: + # https://gcc.gnu.org/legacy-ml/gcc-help/2010-05/msg00029.html + if "linkopts" not in kwargs: + kwargs["linkopts"] = [] + kwargs["linkopts"] += [ + "-static", + "-lstdc++", + "-Wl,--whole-archive", + "-lpthread", + "-Wl,--no-whole-archive", + ] _cc_binary( name = name, **kwargs -- cgit v1.2.3