title: Build a python package with setup.py in CMake
body: <p>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).</p>
  <hr />
  <p>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 &amp; install it with CMake.</p>
  <p>I followed <a href="https://stackoverflow.com/questions/13298504/using-cmake-with-setup-py">Using CMake with setup.py</a> but it only works with one <code>CMakeLists.txt</code> alongside the <code>setup.py</code> and the python folder and without executing cmake from a build directory.</p>
  <p>With this layout :</p>
  <pre><code>Project/
  --build/
  --lib/
  ----python/
  ------folder1/
  ------folder2/
  ------data/
  ------...
  ------__init__.py
  ----setup.py
  ----CMakeLists.txt
  --CMakeLists.txt
  </code></pre>
  <p>and with <code>CMakeLists.txt</code>:</p>
  <pre><code>cmake_minimum_required(VERSION 2.8.8 FATAL_ERROR)
  add_subdirectory(lib)
  (..)
  </code></pre>
  <p>and with <code>lib/CMakeLists.txt</code>:</p>
  <pre><code>find_program(PYTHON &quot;python&quot;)
  
  if (PYTHON)
      set(SETUP_PY_IN &quot;${CMAKE_CURRENT_SOURCE_DIR}/setup.py&quot;)
      set(SETUP_PY    &quot;${CMAKE_CURRENT_BINARY_DIR}/setup.py&quot;)
      set(DEPS        &quot;${CMAKE_CURRENT_SOURCE_DIR}/python/__init__.py&quot;)
      set(OUTPUT      &quot;${CMAKE_CURRENT_BINARY_DIR}/build&quot;)
  
      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 &quot;execute_process(COMMAND ${PYTHON} ${SETUP_PY} install)&quot;)
  endif()
  </code></pre>
  <p>and with <code>setup.py</code>:</p>
  <pre><code>from setuptools import setup, find_packages
  
  setup(name=&quot;python&quot;,
      version=&quot;xx&quot;,
      author=&quot;xx&quot;,
      packages = find_packages(),
      package_data = {'': ['*.txt']},
      description=&quot;Python lib for xx&quot;)
  </code></pre>
  <p>When I run <code>CMake</code> from build directory and then <code>make</code>, the target is built but with nothing. It is as if no packages were found. The installation installs the python package without <code>.py</code> files.</p>