Skip to content

Commit

Permalink
Support a separate namespace for the protobuf library (tensorflow#3741)
Browse files Browse the repository at this point in the history
* Added protobuf renaming script

* Post-process protobuf generated source files to use new namespace

* Removed backup files

* Updated protoc scripts for version renaming

* Added more disclaimers to rename_protobuf
  • Loading branch information
petewarden authored and Vijay Vasudevan committed Aug 15, 2016
1 parent f0cff57 commit 5b6dbfe
Show file tree
Hide file tree
Showing 5 changed files with 173 additions and 16 deletions.
12 changes: 0 additions & 12 deletions tensorflow/contrib/makefile/build_all_ios.sh
Original file line number Diff line number Diff line change
Expand Up @@ -42,18 +42,6 @@ rm -rf tensorflow/contrib/makefile/downloads
# Pull down the required versions of the frameworks we need.
tensorflow/contrib/makefile/download_dependencies.sh

# TODO(petewarden) - Some new code in Eigen triggers a clang bug, so work
# around it by patching the source.
sed -e 's#static uint32x4_t p4ui_CONJ_XOR = vld1q_u32( conj_XOR_DATA );#static uint32x4_t p4ui_CONJ_XOR; // = vld1q_u32( conj_XOR_DATA ); - Removed by script#' \
-i '' \
tensorflow/contrib/makefile/downloads/eigen-latest/eigen/src/Core/arch/NEON/Complex.h
sed -e 's#static uint32x2_t p2ui_CONJ_XOR = vld1_u32( conj_XOR_DATA );#static uint32x2_t p2ui_CONJ_XOR;// = vld1_u32( conj_XOR_DATA ); - Removed by scripts#' \
-i '' \
tensorflow/contrib/makefile/downloads/eigen-latest/eigen/src/Core/arch/NEON/Complex.h
sed -e 's#static uint64x2_t p2ul_CONJ_XOR = vld1q_u64( p2ul_conj_XOR_DATA );#static uint64x2_t p2ul_CONJ_XOR;// = vld1q_u64( p2ul_conj_XOR_DATA ); - Removed by script#' \
-i '' \
tensorflow/contrib/makefile/downloads/eigen-latest/eigen/src/Core/arch/NEON/Complex.h

# Compile protobuf for the target iOS device architectures.
tensorflow/contrib/makefile/compile_ios_protobuf.sh ${JOBS_COUNT}

Expand Down
20 changes: 16 additions & 4 deletions tensorflow/contrib/makefile/download_dependencies.sh
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,26 @@ curl "https://bitbucket.org/eigen/eigen/get/${EIGEN_HASH}.tar.gz" \
-o /tmp/eigen-${EIGEN_HASH}.tar.gz
tar xzf /tmp/eigen-${EIGEN_HASH}.tar.gz -C ${DOWNLOADS_DIR}

git clone https://github.com/google/re2.git ${DOWNLOADS_DIR}/re2
git clone https://github.com/google/gemmlowp.git ${DOWNLOADS_DIR}/gemmlowp
git clone https://github.com/google/protobuf.git ${DOWNLOADS_DIR}/protobuf

# Link to the downloaded Eigen library from a permanent directory name, since
# the downloaded name changes with every version.
cd ${DOWNLOADS_DIR}
rm -rf eigen-latest
ln -s eigen-eigen-${EIGEN_HASH} eigen-latest

# TODO(petewarden) - Some new code in Eigen triggers a clang bug with iOS arm64,
# so work around it by patching the source.
sed -e 's#static uint32x4_t p4ui_CONJ_XOR = vld1q_u32( conj_XOR_DATA );#static uint32x4_t p4ui_CONJ_XOR; // = vld1q_u32( conj_XOR_DATA ); - Removed by script#' \
-i '' \
eigen-latest/eigen/src/Core/arch/NEON/Complex.h
sed -e 's#static uint32x2_t p2ui_CONJ_XOR = vld1_u32( conj_XOR_DATA );#static uint32x2_t p2ui_CONJ_XOR;// = vld1_u32( conj_XOR_DATA ); - Removed by scripts#' \
-i '' \
eigen-latest/eigen/src/Core/arch/NEON/Complex.h
sed -e 's#static uint64x2_t p2ul_CONJ_XOR = vld1q_u64( p2ul_conj_XOR_DATA );#static uint64x2_t p2ul_CONJ_XOR;// = vld1q_u64( p2ul_conj_XOR_DATA ); - Removed by script#' \
-i '' \
eigen-latest/eigen/src/Core/arch/NEON/Complex.h

git clone https://github.com/google/re2.git re2
git clone https://github.com/google/gemmlowp.git gemmlowp
git clone https://github.com/google/protobuf.git protobuf

echo "download_dependencies.sh completed successfully."
89 changes: 89 additions & 0 deletions tensorflow/contrib/makefile/rename_protobuf.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
#!/bin/bash

# Copyright 2015 The TensorFlow Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ==============================================================================

# This script modifies the downloaded protobuf library and the TensorFlow source
# to put all protobuf-related symbols into the google::protobuf3 namespace
# instead of the default google::protobuf. This is necessary to work around
# linking issues for applications that use protobuf v2 already, and want to
# adopt TensorFlow, since otherwise the two libraries have duplicate function
# symbols that clash. It also renames all the include paths to google/protobuf3
# throughout the protobuf and TensorFlow code.
# This is a massive hack, and if possible it's recommended that you switch your
# whole application to protobuf v3 so there's no mismatch with TensorFlow. There
# are also no guarantees that this will continue to work with future versions of
# protobuf or TensorFlow, or that it's a bulletproof solution for every
# application.
#
# To use this script, run the following sequence:
# tensorflow/contrib/makefile/download_dependencies.sh
# tensorflow/contrib/makefile/rename_protobuf.sh
#
# You can then build the source as normal. For example on iOS:
# tensorflow/contrib/makefile/compile_ios_protobuf.sh
# tensorflow/contrib/makefile/compile_ios_tensorflow.sh
#
# Note that this script modifies the source code in-place, so once it's been run
# it's no longer suitable for further manual modifications, since the difference
# with the top of tree will already be large.

mv tensorflow/contrib/makefile/downloads/protobuf/src/google/protobuf \
tensorflow/contrib/makefile/downloads/protobuf//src/google/protobuf3

# Rename protobuf #includes to use protobuf3.
find . \
-type f \
\( -name "*.cc" -or -name "*.h" \) \
-exec sed -i '' \
's%#include \([<"]\)google/protobuf/%#include \1google/protobuf3/%' {} \;
find . \
-type f \
-name "*.proto" \
-exec sed -i '' \
's%import \(.*\)\([<"]\)google/protobuf/%import \1\2google/protobuf3/%' {} \;

# Rename the namespace mentions.
find . \
-type f \
\( -name "*.cc" -or -name "*.h" \) \
-exec sed -i '' \
's%namespace protobuf\([^3]\)%namespace protobuf3\1%' {} \;
find . \
-type f \
\( -name "*.cc" -or -name "*.h" \) \
-exec sed -i '' \
's%protobuf::%protobuf3::%g' {} \;
sed -i '' 's%::google::protobuf;%google::protobuf3;%' \
tensorflow/core/platform/default/protobuf.h

# Fix up a couple of special build scripts that look for particular files.
sed -i '' 's%src/google/protobuf/message.cc%src/google/protobuf3/message.cc%' \
tensorflow/contrib/makefile/downloads/protobuf/configure.ac
sed -i '' 's%src/google/protobuf/stubs/common.h%src/google/protobuf3/stubs/common.h%' \
tensorflow/contrib/makefile/downloads/protobuf/autogen.sh

# Update the locations within the protobuf makefile.
sed -i '' 's%google/protobuf/%google/protobuf3/%g' \
tensorflow/contrib/makefile/downloads/protobuf/src/Makefile.am

# Make sure protoc can find the new google/protobuf3 paths by putting them at
# the root directory.
cp -r tensorflow/contrib/makefile/downloads/protobuf/src/google .

# Update the protobuf commands used in the makefile.
sed -i '' 's%$(PROTOC) $(PROTOCFLAGS) $< --cpp_out $(PROTOGENDIR)%tensorflow/contrib/makefile/rename_protoc.sh $(PROTOC) $(PROTOCFLAGS) $< --cpp_out $(PROTOGENDIR)%' tensorflow/contrib/makefile/Makefile
sed -i '' 's%$(PROTOC) $(PROTOCFLAGS) $< --cpp_out $(HOST_GENDIR)%tensorflow/contrib/makefile/rename_protoc.sh $(PROTOC) $(PROTOCFLAGS) $< --cpp_out $(HOST_GENDIR)%' tensorflow/contrib/makefile/Makefile
sed -i '' 's%$(PROTO_TEXT) \\%tensorflow/contrib/makefile/rename_prototext.sh $(PROTO_TEXT) \\%' tensorflow/contrib/makefile/Makefile
34 changes: 34 additions & 0 deletions tensorflow/contrib/makefile/rename_protoc.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#!/bin/bash -e -x

# Copyright 2015 The TensorFlow Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ==============================================================================

PROTO_C_COMMAND=$1
shift
${PROTO_C_COMMAND} $*

# Assumes that the order is always <some flags> *.protofile --cpp_out dir
PROTO_LAST_THREE_ARGS=(${@: -3})
PROTO_FILE=${PROTO_LAST_THREE_ARGS[0]}
CC_FILE=${PROTO_FILE%.proto}.pb.cc
H_FILE=${PROTO_FILE%.proto}.pb.h
GEN_DIR=${PROTO_LAST_THREE_ARGS[2]}
GEN_CC=${GEN_DIR}/${CC_FILE}
GEN_H=${GEN_DIR}/${H_FILE}

sed -i '' 's%protobuf::%protobuf3::%g' ${GEN_CC}
sed -i '' 's%protobuf::%protobuf3::%g' ${GEN_H}
sed -i '' 's%google_2fprotobuf3_2f%google_2fprotobuf_2f%g' ${GEN_CC}
sed -i '' 's%google_2fprotobuf3_2f%google_2fprotobuf_2f%g' ${GEN_H}
34 changes: 34 additions & 0 deletions tensorflow/contrib/makefile/rename_prototext.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#!/bin/bash -e -x

# Copyright 2015 The TensorFlow Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ==============================================================================

PROTO_TEXT_COMMAND=$1
shift
${PROTO_TEXT_COMMAND} $*

# Assumes a fixed order for the arguments.
PROTO_LAST_FOUR_ARGS=(${@: -4})
PROTO_FILE=${PROTO_LAST_FOUR_ARGS[3]}
CC_FILE=${PROTO_FILE%.proto}.pb_text.cc
H_FILE=${PROTO_FILE%.proto}.pb_text.h
GEN_DIR=${PROTO_LAST_FOUR_ARGS[0]}
GEN_CC=${GEN_DIR}/${CC_FILE#tensorflow/core}
GEN_H=${GEN_DIR}/${H_FILE#tensorflow/core}

sed -i '' 's%protobuf::%protobuf3::%g' ${GEN_CC}
sed -i '' 's%protobuf::%protobuf3::%g' ${GEN_H}
sed -i '' 's%google_2fprotobuf3_2f%google_2fprotobuf_2f%g' ${GEN_CC}
sed -i '' 's%google_2fprotobuf3_2f%google_2fprotobuf_2f%g' ${GEN_H}

0 comments on commit 5b6dbfe

Please sign in to comment.