Do not link against libpython when building python bindings

Summary:
our cmake build used to link against libpython.so with its absolute path (instead of -LSOME_LIB_PATH -lpython), so at runtime loader will think it needs the libpython.so at that specific path, and so load in an additional libpython.so, which causes the python binding built with one python installation not reusable by another (maybe on same machine or sometimes even not on same machine). The solution is quite simple, which is we don't link against libpython, leave all the python related symbols unresolved at build time, they will be resolved at runtime when imported into python.
Closes https://github.com/caffe2/caffe2/pull/1514

Reviewed By: dzhulgakov

Differential Revision: D6412405

Pulled By: bddppq

fbshipit-source-id: 9ff5b752ae3806bfac94085942f82d89c304c887
This commit is contained in:
Junjie Bai
2017-11-27 08:45:07 -08:00
committed by Facebook Github Bot
parent 6e2e623fa9
commit 73f6715f47
2 changed files with 2 additions and 1 deletions

View File

@ -197,6 +197,7 @@ if (BUILD_PYTHON)
set_target_properties(caffe2_pybind11_state PROPERTIES PREFIX "")
if (APPLE)
set_target_properties(caffe2_pybind11_state PROPERTIES SUFFIX ".so")
set_target_properties(caffe2_pybind11_state PROPERTIES LINK_FLAGS "-undefined dynamic_lookup")
elseif (MSVC)
set_target_properties(caffe2_pybind11_state PROPERTIES SUFFIX ".pyd")
endif()
@ -215,6 +216,7 @@ if (BUILD_PYTHON)
set_target_properties(caffe2_pybind11_state_gpu PROPERTIES PREFIX "")
if (APPLE)
set_target_properties(caffe2_pybind11_state_gpu PROPERTIES SUFFIX ".so")
set_target_properties(caffe2_pybind11_state_gpu PROPERTIES LINK_FLAGS "-undefined dynamic_lookup")
elseif (MSVC)
set_target_properties(caffe2_pybind11_state_gpu PROPERTIES SUFFIX ".pyd")
endif()

View File

@ -257,7 +257,6 @@ if(BUILD_PYTHON)
set(USE_OBSERVERS ON)
if(PYTHONINTERP_FOUND AND PYTHONLIBS_FOUND AND NUMPY_FOUND)
caffe2_include_directories(${PYTHON_INCLUDE_DIRS} ${NUMPY_INCLUDE_DIR})
list(APPEND Caffe2_PYTHON_DEPENDENCY_LIBS ${PYTHON_LIBRARIES})
else()
message(WARNING "Python dependencies not met. Not compiling with python. Suppress this warning with -DBUILD_PYTHON=OFF")
set(BUILD_PYTHON OFF)