I am writing a C++ library which can be used from both C++ and Python on Mac and Linux. So I have decided to use CMake and SWIG for my project.
As well described in the SWIG 2.0 documentation, combination of SWIG and CMake also works fine on my Mac. http://www.swig.org/Doc2.0/SWIGDocumentation.html#Introduction_build_system
But I have a question about make install.
After typing cmake . and make, _example.so was successfully generated. But make install does not work, because the auto-generated Makefile does not have install target. I would like to know how I can add install target in the Makefile. I would like _example.so to be installed under site-packages directory on each system.
I would very appreciate it if anyone could tell me how to modify the CMake example written in the above link.
find_package(SWIG REQUIRED)
find_package(PythonLibs REQUIRED)
include(${SWIG_USE_FILE})
set(CMAKE_SWIG_FLAGS "")
include_directories(${PYTHON_INCLUDE_DIRS})
set_source_files_properties(target.i PROPERTIES CPLUSPLUS ON)
set_source_files_properties(target.i PROPERTIES SWIG_FLAGS "-includeall")
swig_add_module(target python target.i ${SOURCES})
swig_link_libraries(target ${PYTHON_LIBRARIES})
execute_process(COMMAND python -c "from distutils.sysconfig import get_python_lib; print get_python_lib()" OUTPUT_VARIABLE PYTHON_SITE_PACKAGES OUTPUT_STRIP_TRAILING_WHITESPACE)
install(TARGETS _target DESTINATION ${PYTHON_SITE_PACKAGES})
install(FILES ${CMAKE_BINARY_DIR}/src/target.py DESTINATION ${PYTHON_SITE_PACKAGES})
                        The CMake interface to make install is the CMake command install(). In your example, you could add an installation rule like this:
install(
  TARGETS ${SWIG_MODULE_example_REAL_NAME} 
  # ... add other arguments to install() as necessary
)
Once there are any install() commands in a CMakeList, CMake will generate an install target callable as make install.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With