Rate This Document
Findability
Accuracy
Completeness
Readability

Failed to Compile the gRPC Dependency When Compiling TensorFlow 1.15.5

Symptom

When compiling TensorFlow 1.15.5, an error is reported stating "external/grpc/src/core/lib/gpr/log_linux.cc:43:13: error: ambiguating new declaration of 'long int gettid()'."

Key Process and Cause Analysis

Key error information: When compiling the gRPC dependency, an error is reported indicating ambiguous function declaration for the gettid function.

1
2
external/grpc/src/core/lib/gpr/log_linux.cc:43:13: error: ambiguating new declaration of 'long int gettid()'
   43 | static long gettid(void) { return syscall(__NR_gettid); }

Root cause analysis: The gettid function defined by the gRPC dependency conflicts with the glibc system library definition.

Conclusion and Solution

  1. Download the gRPC dependency.
    1
    wget https://github.com/grpc/grpc/archive/4566c2a29ebec0835643b972eb99f4306c4234a3.tar.gz  --no-check-certificate
    
  2. Decompress the gRPC source package and go to the decompressed directory.
    1
    2
    tar -zxvf 4566c2a29ebec0835643b972eb99f4306c4234a3.tar.gz
    cd grpc-4566c2a29ebec0835643b972eb99f4306c4234a3
    
  3. Change gettid in the following files to gettid_sys:
    • src/core/lib/gpr/log_posix.cc: lines 34 and 89
    • src/core/lib/gpr/log_linux.cc: lines 43 and 73
    • src/core/lib/iomgr/ev_epollex_linux.cc: lines 1106 and 1126

    You can run the following command to change them:

    grep -rl 'gettid' src/ | xargs sed -i 's/\bgettid/gettid_sys/g'

    Before:

    After:

  4. Exit the decompressed directory and compress the gRPC package.
    1
    2
    cd ..
    tar -zcvf grpc-4566c2a29ebec0835643b972eb99f4306c4234a3.tar.gz grpc-4566c2a29ebec0835643b972eb99f4306c4234a3
    
  5. Calculate the SHA256 value of gRPC.
    1
    sha256sum grpc-4566c2a29ebec0835643b972eb99f4306c4234a3.tar.gz
    
  6. Create a local Bazel repository and copy the modified gRPC code to the local repository, which is /home/bazel_local in the following example.
    1
    2
    mkdir /home/bazel_local
    cp grpc-4566c2a29ebec0835643b972eb99f4306c4234a3.tar.gz /home/bazel_local/
    
  7. Change the gRPC download method.
    1. Go to the TensorFlow directory.
      1
      cd path/to/tensorflow/tensorflow-1.15.5
      
    2. Open the tensorflow/workspace.bzl file.
      1
      vi tensorflow/workspace.bzl
      
    3. Press i to enter the insert mode and modify the tensorflow/workspace.bzl file. Replace the SHA256 value of the new gRPC package in line 514 and add the Bazel local repository download path below line 517.

      Before:

          511     # WARNING: make sure ncteisen@ and vpai@ are cc-ed on any CL to change the below rule
          512     tf_http_archive(
          513         name = "grpc",
          514         sha256 = "67a6c26db56f345f7cee846e681db2c23f919eba46dd639b09462d1b6203d28c",
          515         strip_prefix = "grpc-4566c2a29ebec0835643b972eb99f4306c4234a3",
          516         system_build_file = clean_dep("//third_party/systemlibs:grpc.BUILD"),
          517         urls = [
          518             "https://storage.googleapis.com/mirror.tensorflow.org/github.com/grpc/grpc/archive/4566c2a29ebec0835643b972eb99f4306c4234a3.tar.gz",
          519             "https://github.com/grpc/grpc/archive/4566c2a29ebec0835643b972eb99f4306c4234a3.tar.gz",
          520         ],
          521     )

      After:

          511     # WARNING: make sure ncteisen@ and vpai@ are cc-ed on any CL to change the below rule
          512     tf_http_archive(
          513         name = "grpc",
          514         sha256 = "f15ad57d5cf1ef6e4552ddcfc4b5c45ba098d520eff2f113669efae1ff5e892d",
          515         strip_prefix = "grpc-4566c2a29ebec0835643b972eb99f4306c4234a3",
          516         system_build_file = clean_dep("//third_party/systemlibs:grpc.BUILD"),
          517         urls = [
          518             "file:///home/bazel_local/grpc-4566c2a29ebec0835643b972eb99f4306c4234a3.tar.gz",
          519             "https://storage.googleapis.com/mirror.tensorflow.org/github.com/grpc/grpc/archive/4566c2a29ebec0835643b972eb99f4306c4234a3.tar.gz",
          520             "https://github.com/grpc/grpc/archive/4566c2a29ebec0835643b972eb99f4306c4234a3.tar.gz",
          521         ],
          522     )

      Set the SHA256 and local Bazel repository path as required.

    4. Press Esc, type :wq!, and press Enter to save the file and exit.
  8. Recompile TensorFlow.
    1
    bazel build --config=v1 --cxxopt="-D_GLIBCXX_USE_CXX11_ABI=0"  //tensorflow/tools/pip_package:build_pip_package