Skip to content
Permalink
Browse files

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 facebookarchive/caffe2#1514

Reviewed By: dzhulgakov

Differential Revision: D6412405

Pulled By: bddppq

fbshipit-source-id: 9ff5b752ae3806bfac94085942f82d89c304c887
  • Loading branch information...
bddppq authored and facebook-github-bot committed Nov 27, 2017
1 parent 6e2e623 commit 73f6715f4725a0723d8171d3131e09ac7abf0666
Showing with 2 additions and 1 deletion.
  1. +2 −0 caffe2/CMakeLists.txt
  2. +0 −1 cmake/Dependencies.cmake
@@ -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()
@@ -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)

0 comments on commit 73f6715

Please sign in to comment.
You can’t perform that action at this time.