From d6031a17b189779fcdf7d4113e279ec7fddda18b Mon Sep 17 00:00:00 2001 From: Derek Murray Date: Mon, 19 Sep 2016 16:07:02 -0700 Subject: [PATCH] Revised CMake rules for SWIG wrapper generation. (#4472) Instead of generating a separate `python_deps.so` and `_pywrap_tensorflow.so`, we now generate a single `_pywrap_tensorflow.so` containing the entire runtime, like the Bazel build. This simplifies the linkage on other platforms. --- tensorflow/contrib/cmake/setup.py | 1 - tensorflow/contrib/cmake/tf_python.cmake | 103 ++++++++++------------- 2 files changed, 44 insertions(+), 60 deletions(-) diff --git a/tensorflow/contrib/cmake/setup.py b/tensorflow/contrib/cmake/setup.py index 953c2aa97c7..b8a30adc9d9 100644 --- a/tensorflow/contrib/cmake/setup.py +++ b/tensorflow/contrib/cmake/setup.py @@ -177,7 +177,6 @@ setup( include_package_data=True, package_data={ 'tensorflow': ['python/_pywrap_tensorflow.so', - 'python/libpython_deps.so', ] + matches, }, zip_safe=False, diff --git a/tensorflow/contrib/cmake/tf_python.cmake b/tensorflow/contrib/cmake/tf_python.cmake index fc3579aa903..67f45082f72 100644 --- a/tensorflow/contrib/cmake/tf_python.cmake +++ b/tensorflow/contrib/cmake/tf_python.cmake @@ -12,11 +12,7 @@ # Resolve installed dependencies ######################################################## -# 1. Resolve the installed version of SWIG. -FIND_PACKAGE(SWIG REQUIRED) -INCLUDE(${SWIG_USE_FILE}) - -# 2. Resolve the installed version of Python (for Python.h and python). +# 1. Resolve the installed version of Python (for Python.h and python). # TODO(mrry): Parameterize the build script to enable Python 3 building. include(FindPythonInterp) if(NOT PYTHON_INCLUDE_DIR) @@ -32,7 +28,7 @@ if(NOT PYTHON_INCLUDE_DIR) endif(NOT PYTHON_INCLUDE_DIR) FIND_PACKAGE(PythonLibs) -# 3. Resolve the installed version of NumPy (for numpy/arrayobject.h). +# 2. Resolve the installed version of NumPy (for numpy/arrayobject.h). if(NOT NUMPY_INCLUDE_DIR) set(NUMPY_NOT_FOUND false) exec_program("${PYTHON_EXECUTABLE}" @@ -45,7 +41,7 @@ if(NOT NUMPY_INCLUDE_DIR) endif(${NUMPY_NOT_FOUND}) endif(NOT NUMPY_INCLUDE_DIR) -# 4. Resolve the installed version of zlib (for libz.so). +# 3. Resolve the installed version of zlib (for libz.so). find_package(ZLIB REQUIRED) @@ -292,12 +288,30 @@ add_dependencies(tf_python_ops tf_python_op_gen_main) # Build the SWIG-wrapped library for the TensorFlow runtime. ############################################################ -# python_deps is a shared library containing all of the TensorFlow +find_package(SWIG REQUIRED) +# Generate the C++ and Python source code for the SWIG wrapper. +add_custom_command( + OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/tf_python/tensorflow/python/pywrap_tensorflow.py" + "${CMAKE_CURRENT_BINARY_DIR}/pywrap_tensorflow.cc" + DEPENDS tf_python_touchup_modules + COMMAND ${SWIG_EXECUTABLE} + ARGS -python -c++ + -I${tensorflow_source_dir} + -I${CMAKE_CURRENT_BINARY_DIR} + -module pywrap_tensorflow + -outdir ${CMAKE_CURRENT_BINARY_DIR}/tf_python/tensorflow/python + -o ${CMAKE_CURRENT_BINARY_DIR}/pywrap_tensorflow.cc + -globals '' + ${tensorflow_source_dir}/tensorflow/python/tensorflow.i + COMMENT "Running SWIG to generate Python wrappers" + VERBATIM ) + +# pywrap_tensorflow is a shared library containing all of the TensorFlow # runtime and the standard ops and kernels. These are installed into # tf_python/tensorflow/python/. # TODO(mrry): Refactor this to expose a framework library that # facilitates `tf.load_op_library()`. -add_library(python_deps SHARED +add_library(pywrap_tensorflow SHARED "${tensorflow_source_dir}/tensorflow/python/client/tf_session_helper.h" "${tensorflow_source_dir}/tensorflow/python/client/tf_session_helper.cc" "${tensorflow_source_dir}/tensorflow/python/framework/cpp_shape_inference.h" @@ -318,6 +332,7 @@ add_library(python_deps SHARED "${tensorflow_source_dir}/tensorflow/c/checkpoint_reader.h" "${tensorflow_source_dir}/tensorflow/c/tf_status_helper.cc" "${tensorflow_source_dir}/tensorflow/c/tf_status_helper.h" + "${CMAKE_CURRENT_BINARY_DIR}/pywrap_tensorflow.cc" $ $ $ @@ -326,7 +341,7 @@ add_library(python_deps SHARED $ $ ) -target_link_libraries(python_deps +target_link_libraries(pywrap_tensorflow ${CMAKE_THREAD_LIBS_INIT} tf_protos_cc ${GRPC_LIBRARIES} @@ -341,7 +356,7 @@ target_link_libraries(python_deps ${ZLIB_LIBRARIES} ${CMAKE_DL_LIBS} ) -target_include_directories(python_deps PUBLIC +target_include_directories(pywrap_tensorflow PUBLIC ${tensorflow_source_dir} ${CMAKE_CURRENT_BINARY_DIR} ${eigen_INCLUDE_DIRS} @@ -349,62 +364,32 @@ target_include_directories(python_deps PUBLIC ${NUMPY_INCLUDE_DIR} ) # C++11 -target_compile_features(python_deps PRIVATE +target_compile_features(pywrap_tensorflow PRIVATE cxx_rvalue_references ) -set_target_properties(python_deps PROPERTIES - LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/tf_python/tensorflow/python) -# _pywrap_tensorflow is the target that generates the SWIG bindings -# and compiles them as a shared library that depends on python_deps. -set(CMAKE_SWIG_FLAGS "") -set(CMAKE_SWIG_OUTDIR ${CMAKE_CURRENT_BINARY_DIR}/tf_python/tensorflow/python) -SET_SOURCE_FILES_PROPERTIES("${tensorflow_source_dir}/tensorflow/python/tensorflow.i" - PROPERTIES CPLUSPLUS ON -) -SET_PROPERTY(SOURCE "${tensorflow_source_dir}/tensorflow/python/tensorflow.i" - PROPERTY SWIG_FLAGS "-I\"${tensorflow_source_dir}\"" "-module" "pywrap_tensorflow" -) -SWIG_ADD_MODULE(pywrap_tensorflow python - "${tensorflow_source_dir}/tensorflow/python/tensorflow.i" -) -SWIG_LINK_LIBRARIES(pywrap_tensorflow - python_deps - ${PROTOBUF_LIBRARY} - ${CMAKE_DL_LIBS} -) -target_include_directories(_pywrap_tensorflow PUBLIC - ${tensorflow_source_dir} - ${CMAKE_CURRENT_BINARY_DIR} - ${eigen_INCLUDE_DIRS} - ${PYTHON_INCLUDE_DIR} - ${NUMPY_INCLUDE_DIR} -) -add_dependencies(_pywrap_tensorflow - eigen - tf_core_direct_session - tf_core_distributed_runtime - tf_core_framework - python_deps - tf_python_copy_scripts_to_destination - tf_python_ops - tf_python_touchup_modules -) -# C++11 -target_compile_features(_pywrap_tensorflow PRIVATE - cxx_rvalue_references -) -set_target_properties(_pywrap_tensorflow PROPERTIES - LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/tf_python/tensorflow/python) +############################################################ +# Build a PIP package containing the TensorFlow runtime. +############################################################ add_custom_target(tf_python_copy_pip_files) -add_dependencies(tf_python_copy_pip_files _pywrap_tensorflow tf_python_copy_scripts_to_destination tf_python_touchup_modules) +add_dependencies(tf_python_copy_pip_files + pywrap_tensorflow + tf_python_copy_scripts_to_destination + tf_python_touchup_modules + tf_python_ops) add_custom_command(TARGET tf_python_copy_pip_files POST_BUILD - COMMAND ${CMAKE_COMMAND} -E copy ${tensorflow_source_dir}/tensorflow/contrib/cmake/setup.py ${CMAKE_CURRENT_BINARY_DIR}/tf_python/) + COMMAND ${CMAKE_COMMAND} -E copy ${tensorflow_source_dir}/tensorflow/contrib/cmake/setup.py + ${CMAKE_CURRENT_BINARY_DIR}/tf_python/) add_custom_command(TARGET tf_python_copy_pip_files POST_BUILD - COMMAND ${CMAKE_COMMAND} -E copy ${tensorflow_source_dir}/tensorflow/tools/pip_package/README ${CMAKE_CURRENT_BINARY_DIR}/tf_python/) + COMMAND ${CMAKE_COMMAND} -E copy ${CMAKE_CURRENT_BINARY_DIR}/libpywrap_tensorflow.so + ${CMAKE_CURRENT_BINARY_DIR}/tf_python/tensorflow/python/_pywrap_tensorflow.so) add_custom_command(TARGET tf_python_copy_pip_files POST_BUILD - COMMAND ${CMAKE_COMMAND} -E copy ${tensorflow_source_dir}/tensorflow/tools/pip_package/MANIFEST.in ${CMAKE_CURRENT_BINARY_DIR}/tf_python/) + COMMAND ${CMAKE_COMMAND} -E copy ${tensorflow_source_dir}/tensorflow/tools/pip_package/README + ${CMAKE_CURRENT_BINARY_DIR}/tf_python/) +add_custom_command(TARGET tf_python_copy_pip_files POST_BUILD + COMMAND ${CMAKE_COMMAND} -E copy ${tensorflow_source_dir}/tensorflow/tools/pip_package/MANIFEST.in + ${CMAKE_CURRENT_BINARY_DIR}/tf_python/) add_custom_command(TARGET tf_python_copy_pip_files POST_BUILD COMMAND ${PYTHON_EXECUTABLE} ${CMAKE_CURRENT_BINARY_DIR}/tf_python/setup.py bdist_wheel WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/tf_python)