Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Build a python package with setup.py in CMake

Tags:

EDIT: The question is a bit too long. Here is my real question: How can I build and install a python package with setuptools (setup.py) inside CMake? The detail of my code is shown below (but with an out-of-source build method, the method with the source is working).


I have a project where I need to distribute my own python package. I made a setup.py script but I would like to build & install it with CMake.

I followed Using CMake with setup.py but it only works with one CMakeLists.txt alongside the setup.py and the python folder and without executing cmake from a build directory.

With this layout :

Project/ --build/ --lib/ ----python/ ------folder1/ ------folder2/ ------data/ ------... ------__init__.py ----setup.py ----CMakeLists.txt --CMakeLists.txt 

and with CMakeLists.txt:

cmake_minimum_required(VERSION 2.8.8 FATAL_ERROR) add_subdirectory(lib) (..) 

and with lib/CMakeLists.txt:

find_program(PYTHON "python")  if (PYTHON)     set(SETUP_PY_IN "${CMAKE_CURRENT_SOURCE_DIR}/setup.py")     set(SETUP_PY    "${CMAKE_CURRENT_BINARY_DIR}/setup.py")     set(DEPS        "${CMAKE_CURRENT_SOURCE_DIR}/python/__init__.py")     set(OUTPUT      "${CMAKE_CURRENT_BINARY_DIR}/build")      configure_file(${SETUP_PY_IN} ${SETUP_PY})      add_custom_command(OUTPUT ${OUTPUT}                        COMMAND ${PYTHON}                        ARGS setup.py build                        DEPENDS ${DEPS})      add_custom_target(target ALL DEPENDS ${OUTPUT})      install(CODE "execute_process(COMMAND ${PYTHON} ${SETUP_PY} install)") endif() 

and with setup.py:

from setuptools import setup, find_packages  setup(name="python",     version="xx",     author="xx",     packages = find_packages(),     package_data = {'': ['*.txt']},     description="Python lib for xx") 

When I run CMake from build directory and then make, the target is built but with nothing. It is as if no packages were found. The installation installs the python package without .py files.

like image 395
Samuel Girardin Avatar asked Mar 24 '15 12:03

Samuel Girardin


People also ask

Can CMake build Python?

CMake, the cross-platform build system generator, is now easily installable in Python distributions! This makes creation of cross-platform C/C++ CPython extension modules accessible to many more developers.

How do I set up a build on CMake?

Run the cmake executable or the cmake-gui to configure the project and then build it with your chosen build tool. Run the install step by using the install option of the cmake command (introduced in 3.15, older versions of CMake must use make install ) from the command line, or build the INSTALL target from an IDE.

What is setup py in Python?

The setup.py file may be the most significant file that should be placed at the root of the Python project directory. It primarily serves two purposes: It includes choices and metadata about the program, such as the package name, version, author, license, minimal dependencies, entry points, data files, and so on.

What is Cmakelist?

CMake is a meta build system that uses scripts called CMakeLists to generate build files for a specific environment (for example, makefiles on Unix machines). When you create a new CMake project in CLion, a CMakeLists. txt file is automatically generated under the project root.


2 Answers

setuptools doesn't know about the out of source build and therefore doesn't find any python source files (because you do not copy them to the binary dir, only the setup.py file seems to exist there). In order to fix this, you would have to copy the python source tree into the CMAKE_CURRENT_BINARY_DIR.

like image 171
languitar Avatar answered Sep 20 '22 19:09

languitar


https://bloerg.net/2012/11/10/cmake-and-distutils.html suggests setting package_dir to ${CMAKE_CURRENT_SOURCE_DIR} in setup.py.

like image 41
Tudor Bosman Avatar answered Sep 23 '22 19:09

Tudor Bosman