9

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.

2 Answers 2

10
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})
3
  • How does this work if the CMAKE_INSTALL_PREFIX is set to a local directory? The PYTHON_SITE_PACKAGES will point to a system folder and not use the prefix... Commented Oct 18, 2013 at 0:25
  • 1
    I found the answer in the libftdi python CMakeLists.txt file. I don't understand it well enough to respond with any updates yet but for anyone also interested check out developer.intra2net.com/git/?p=libftdi;a=blob;f=python/… Commented Oct 18, 2013 at 1:21
  • This behaviour is as expected in the Python world. The library gets installed into the environment that a naive call to python would use. This lets you install into the currently activated virtualenv or what have you. Commented Jul 11, 2016 at 18:44
8

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.

2
  • Thanks a lot. I will add an answer to show the whole commands. Commented Feb 10, 2013 at 10:12
  • Wondered how to generate that variable name dynamically… Despite my expectation, ${SWIG_MODULE_${PROJECT_NAME}_REAL_NAME} seems to work!
    – Melebius
    Commented Sep 20, 2016 at 10:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.