How to put swig wrappers in a reachable location to be tested by python tests?
<p>I got a simple test example like this one:</p>

<p><a href="http://i.stack.imgur.com/4GHE0.png" rel="nofollow"><img src="http://i.stack.imgur.com/4GHE0.png" alt="enter image description here"></a></p>

<p>And my CMakeLists.txt looks like this:</p>

<pre><code>cmake_minimum_required(VERSION 3.7)

FIND_PACKAGE(SWIG REQUIRED)
INCLUDE(${SWIG_USE_FILE})

FIND_PACKAGE(PythonLibs)
INCLUDE_DIRECTORIES(${PYTHON_INCLUDE_PATH})

INCLUDE_DIRECTORIES(${CMAKE_CURRENT_SOURCE_DIR})

SET(CMAKE_SWIG_FLAGS "")

SET_SOURCE_FILES_PROPERTIES(example.i PROPERTIES CPLUSPLUS ON)
set(SWIG_MODULE_example_EXTRA_DEPS example.cxx example.h)
SWIG_ADD_MODULE(example python example.i example.cxx)
SWIG_LINK_LIBRARIES(example ${PYTHON_LIBRARIES})
</code></pre>

<p>After generating the ST files and building the wrapper I'll get this output in the build directory:</p>

<p><a href="http://i.stack.imgur.com/NIPIL.png" rel="nofollow"><img src="http://i.stack.imgur.com/NIPIL.png" alt="enter image description here"></a></p>

<p>Problem here is my test <code>runme.py</code> isn't able to load those libraries. So, my question is, how can I place the <code>example.py</code> and <code>example.pyd</code> (only those ones) to be in a reachable place to be tested properly? I've tried adding this line to the CMakeLists.txt <code>set(CMAKE_SWIG_OUTDIR ${CMAKE_CURRENT_BINARY_DIR}/..)</code> and the result was placing the .py and the generated wrappers alone:</p>

<p><a href="http://i.stack.imgur.com/KrrcL.png" rel="nofollow"><img src="http://i.stack.imgur.com/KrrcL.png" alt="enter image description here"></a></p>

<p>And the .pyd remaining in the build folder:</p>

<p><a href="http://i.stack.imgur.com/9R3yO.png" rel="nofollow"><img src="http://i.stack.imgur.com/9R3yO.png" alt="enter image description here"></a></p>

<p>So, how can I put the wrappers in a reachable place to be tested by my python test files?</p>

<p>I use C++ for unit testing and Python for testing against reference implementations or integration testing. </p>

<p>The approach I use is to add a custom target, which copies the libraries as well as any extra generated files to my source directory, where the python test files are located. I have configured my <code>.gitignore</code> file such that they are never added by mistake to my repository.</p>

<pre><code>add_custom_target(copy ALL)

# Some library my Swig wrapped library depends on (say gl)
add_custom_command(TARGET copy
  PRE_BUILD
  COMMAND ${CMAKE_COMMAND} -E copy $&lt;TARGET_FILE:gl&gt; ${CMAKE_CURRENT_SOURCE_DIR}
  DEPENDS $&lt;TARGET_FILE:gl&gt;)

# The Swig library, say swig_example (Note the _ in the target name)
add_custom_command(TARGET copy
  PRE_BUILD
  COMMAND ${CMAKE_COMMAND} -E copy_if_different $&lt;TARGET_FILE:_swig_example&gt; ${CMAKE_CURRENT_SOURCE_DIR}
  DEPENDS $&lt;TARGET_FILE:_swig_example&gt;)

# The extra .py files generated by SWIG
add_custom_command(TARGET copy
  PRE_BUILD
  COMMAND ${CMAKE_COMMAND} -E copy_if_different ${swig_extra_generated_files} ${CMAKE_CURRENT_SOURCE_DIR}
  DEPENDS $&lt;TARGET_FILE:_swig_example&gt;)

enable_testing()

add_test(
  NAME    "${CMAKE_CURRENT_SOURCE_DIR}/some_test.py"
  COMMAND ${PYTHON_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/some_test.py
  WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR})
</code></pre>

<p>I believe this is a very neat setup. Alternatively, you could start adding paths in your Python test scripts, but it is often desired to support an arbitrary build location.</p>

python cmake swig