Skip to content

Commit c8c90a4

Browse files
authored
Merge pull request #246 from FloopCZ/system-protobuf
Add support for matching Protobuf install
2 parents 84f40ed + fb33ad4 commit c8c90a4

File tree

8 files changed

+58
-19
lines changed

8 files changed

+58
-19
lines changed

.dockerignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
tensorflow_cc/build/

Dockerfiles/install-archlinux.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ done
1515
# install requirements
1616
pacman -Syu --noconfirm --needed \
1717
base-devel \
18+
gcc11 \
1819
cmake \
1920
git \
2021
python \

Dockerfiles/install-common.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ chmod go+rX tensorflow_cc/tensorflow_cc/build
1919
cd tensorflow_cc/tensorflow_cc/build
2020

2121
# build and install
22-
cmake -DLOCAL_RAM_RESOURCES=2048 -DLOCAL_CPU_RESOURCES=2 ..
22+
cmake -DLOCAL_RAM_RESOURCES=2048 -DLOCAL_CPU_RESOURCES=2 -DINSTALL_PROTOBUF=ON ..
2323
make
2424
rm -rf /home/tensorflow_cc/.cache
2525
rm -rf /root/.cache

README.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,12 @@ sudo pacman -S cuda cudnn nvidia
6767
```
6868

6969
**Warning:** Newer versions of TensorFlow sometimes fail to build with the latest version of Bazel. You may wish
70-
to install an older version of Bazel (e.g., 3.7.2).
70+
to install an older version of Bazel (e.g., 5.1.1).
71+
72+
**Warning:** If your program uses Protobuf and you encounter linkage or other problems, you can
73+
try `-DINSTALL_PROTOBUF=ON` option to install a Protobuf version matching the version bundled with TensorFlow.
74+
Our Dockerfiles are already built with the right version of Protobuf, so you may want to try
75+
your program in the Dockerfile first.
7176

7277
#### 2) Clone this repository
7378
```

tensorflow_cc/CMakeLists.txt

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
cmake_minimum_required(VERSION 3.3 FATAL_ERROR)
1+
cmake_minimum_required(VERSION 3.7 FATAL_ERROR)
22
cmake_policy(SET CMP0048 NEW) # Enable version parameter in project().
33
file(STRINGS "${CMAKE_CURRENT_SOURCE_DIR}/PROJECT_VERSION" version)
44
project(
@@ -11,6 +11,7 @@ option(REQUIRE_CUDA "Make sure to find and use CUDA (implies ALLOW_CUDA)." OFF)
1111
set(LOCAL_RAM_RESOURCES 4096 CACHE STRING "The amount of local RAM resources passed to bazel scheduler (e.g., 4096).")
1212
set(LOCAL_CPU_RESOURCES HOST_CPUS CACHE STRING "The amount of local CPU cores passed to bazel scheduler (e.g., 2).")
1313
set(TENSORFLOW_TAG "v${version}" CACHE STRING "The tensorflow release tag to be checked out (default v${version}).")
14+
option(INSTALL_PROTOBUF "Install protobuf compatible with tensorflow version." OFF)
1415
set(CMAKE_CXX_STANDARD 14 CACHE STRING "The C++ standard for building and linking the library (e.g., 14).")
1516

1617
# -------------
@@ -31,6 +32,9 @@ configure_file("cmake/build_tensorflow.sh.in" "build_tensorflow.sh" @ONLY)
3132
# ----------------------------------------------
3233

3334
include(TensorflowBase)
35+
if(INSTALL_PROTOBUF)
36+
include(ProtobufExternal)
37+
endif()
3438

3539
# ------------------------------
3640
# Define Tensorflow_CC Interface
@@ -39,6 +43,10 @@ include(TensorflowBase)
3943
add_library(tensorflow_cc INTERFACE)
4044
target_compile_features(tensorflow_cc INTERFACE "cxx_std_${CMAKE_CXX_STANDARD}")
4145

46+
if(INSTALL_PROTOBUF)
47+
add_dependencies(tensorflow_cc protobuf-external)
48+
endif()
49+
4250
# The include folders are sometimes contained under bazel-bin/bin/ and sometimes just bazel-bin.
4351
target_include_directories(
4452
tensorflow_cc INTERFACE
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# Find the proper protobuf archive url.
2+
file(DOWNLOAD
3+
"https://raw.githubusercontent.com/tensorflow/tensorflow/${TENSORFLOW_TAG}/tensorflow/workspace2.bzl"
4+
"${CMAKE_CURRENT_BINARY_DIR}/tmp/workspace2.bzl"
5+
)
6+
file(READ
7+
"${CMAKE_CURRENT_BINARY_DIR}/tmp/workspace2.bzl"
8+
workspace2_str
9+
)
10+
string(REGEX MATCH
11+
"https://github.com/protocolbuffers/protobuf/archive/v[.0-9]+.zip"
12+
PROTOBUF_ARCHIVE
13+
"${workspace2_str}"
14+
)
15+
message("Will build Protobuf from '${PROTOBUF_ARCHIVE}'.")
16+
17+
ExternalProject_Add(
18+
protobuf-external
19+
PREFIX protobuf
20+
URL "${PROTOBUF_ARCHIVE}"
21+
CMAKE_CACHE_ARGS
22+
"-DCMAKE_BUILD_TYPE:STRING=${CMAKE_BUILD_TYPE}"
23+
"-Dprotobuf_BUILD_TESTS:BOOL=OFF"
24+
"-Dprotobuf_BUILD_EXAMPLES:BOOL=OFF"
25+
"-DCMAKE_CXX_COMPILER:STRING=${CMAKE_CXX_COMPILER}"
26+
SOURCE_SUBDIR cmake
27+
INSTALL_COMMAND ""
28+
)
29+
30+
install(SCRIPT "${CMAKE_CURRENT_BINARY_DIR}/protobuf/src/protobuf-external-build/cmake_install.cmake")
31+

tensorflow_cc/cmake/TensorflowBase.cmake

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ ExternalProject_Add(
1111
BUILD_IN_SOURCE 1
1212
UPDATE_COMMAND ""
1313
CONFIGURE_COMMAND ""
14-
BUILD_COMMAND "${CMAKE_CURRENT_BINARY_DIR}/build_tensorflow.sh"
14+
BUILD_COMMAND echo "*" > "${CMAKE_CURRENT_BINARY_DIR}/tensorflow/.bazelversion"
15+
COMMAND "${CMAKE_CURRENT_BINARY_DIR}/build_tensorflow.sh"
1516
INSTALL_COMMAND "${CMAKE_CURRENT_SOURCE_DIR}/cmake/copy_links.sh" bazel-bin
1617
# The include folders are sometimes contained under bazel-bin/bin/ and sometimes just bazel-bin.
1718
# Later, we include both so let's make sure they both exist so that CMake does not complain.

tensorflow_cc/cmake/build_tensorflow.sh.in

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -64,25 +64,17 @@ if [ "$cuda_allowed" == true ] && [ "$cuda_available" == true ]; then
6464
export TF_CUDNN_VERSION="$(find /opt /usr -name 'libcudnn.so.*' -path '*/cuda*' | tail -n1 | sed -r 's/^.*\.so\.//')"
6565

6666
# choose the right version of CUDA compiler
67-
if [ -z "$GCC_HOST_COMPILER_PATH" ]; then
68-
if hash gcc-11 2>/dev/null && version_gt 11.1 `gcc-11 -dumpversion`; then
69-
export GCC_HOST_COMPILER_PATH=${GCC_HOST_COMPILER_PATH:-"/usr/bin/gcc-11"}
67+
if [ -z "${GCC_HOST_COMPILER_PATH}" ]; then
68+
if hash gcc-11 2>/dev/null && version_gt 11.4 `gcc-11 -dumpversion`; then
69+
export GCC_HOST_COMPILER_PATH=${GCC_HOST_COMPILER_PATH:-`which gcc-11`}
7070
elif hash gcc-10 2>/dev/null && version_gt 10.3 `gcc-10 -dumpversion`; then
71-
export GCC_HOST_COMPILER_PATH=${GCC_HOST_COMPILER_PATH:-"/usr/bin/gcc-10"}
71+
export GCC_HOST_COMPILER_PATH=${GCC_HOST_COMPILER_PATH:-`which gcc-10`}
7272
elif hash gcc-9 2>/dev/null && version_gt 9.4 `gcc-9 -dumpversion`; then
73-
export GCC_HOST_COMPILER_PATH=${GCC_HOST_COMPILER_PATH:-"/usr/bin/gcc-9"}
73+
export GCC_HOST_COMPILER_PATH=${GCC_HOST_COMPILER_PATH:-`which gcc-9`}
7474
elif hash gcc-8 2>/dev/null && version_gt 8.5 `gcc-8 -dumpversion`; then
75-
export GCC_HOST_COMPILER_PATH=${GCC_HOST_COMPILER_PATH:-"/usr/bin/gcc-8"}
76-
elif hash gcc-7 2>/dev/null && version_gt 7.5 `gcc-7 -dumpversion`; then
77-
export GCC_HOST_COMPILER_PATH=${GCC_HOST_COMPILER_PATH:-"/usr/bin/gcc-7"}
78-
elif hash gcc-6 2>/dev/null && version_gt 6.4 `gcc-6 -dumpversion`; then
79-
export GCC_HOST_COMPILER_PATH=${GCC_HOST_COMPILER_PATH:-"/usr/bin/gcc-6"}
80-
elif hash gcc-5 2>/dev/null && version_gt 5.5 `gcc-5 -dumpversion`; then
81-
export GCC_HOST_COMPILER_PATH=${GCC_HOST_COMPILER_PATH:-"/usr/bin/gcc-5"}
82-
elif hash gcc-4 2>/dev/null && version_gt 4.9 `gcc-4 -dumpversion`; then
83-
export GCC_HOST_COMPILER_PATH=${GCC_HOST_COMPILER_PATH:-"/usr/bin/gcc-4"}
75+
export GCC_HOST_COMPILER_PATH=${GCC_HOST_COMPILER_PATH:-`which gcc-8`}
8476
else
85-
export GCC_HOST_COMPILER_PATH=${GCC_HOST_COMPILER_PATH:-"/usr/bin/gcc"}
77+
export GCC_HOST_COMPILER_PATH=${GCC_HOST_COMPILER_PATH:-`which gcc`}
8678
fi
8779
fi
8880

0 commit comments

Comments
 (0)