[CMake] Python test scripts

Michael Hertling mhertling at online.de
Sat Oct 23 22:35:45 EDT 2010


On 10/24/2010 01:26 AM, Bill Spotz wrote:
> Hi,
> 
> Is there a standard way to make tests for python extension modules?
> 
> Currently, I copy test scripts from the source directory to the binary directory.  I would like to augment this by making the first line of my script
> 
>   #! ${PYTHON_EXECUTABLE}
> 
> but the only copy facility I see that supports this substitution is
> 
>   CONFIGURE_FILE(...)
> 
> The problem with this is that the file gets copied over during configuration, and it would be much preferable to have it copied during the build phase.  Is there a copy facility that performs substitutions that executes during the build phase?

You might use a CMake script which is executed at build time:

CMAKE_MINIMUM_REQUIRED(VERSION 2.8 FATAL_ERROR)
PROJECT(PYTHONTEST C)
SET(PYTHON_EXECUTABLE "path/to/python")
FILE(WRITE ${CMAKE_BINARY_DIR}/pythontest.py.in "#! @INTERPRETER@\n")
FILE(WRITE ${CMAKE_BINARY_DIR}/pythontest.cmake "
CONFIGURE_FILE(${CMAKE_BINARY_DIR}/pythontest.py.in
               ${CMAKE_BINARY_DIR}/pythontest.py)\n")
ADD_CUSTOM_COMMAND(OUTPUT ${CMAKE_BINARY_DIR}/pythontest.py
    COMMAND ${CMAKE_COMMAND} -D INTERPRETER=${PYTHON_EXECUTABLE}
                             -P pythontest.cmake
    DEPENDS ${CMAKE_BINARY_DIR}/pythontest.py.in)
FILE(WRITE ${CMAKE_BINARY_DIR}/main.c "int main(void){return 0;}\n")
ADD_EXECUTABLE(main main.c pythontest.py)

So, CONFIGURE_FILE() is delayed until the custom command's invocation.
Anyway, the variables' values that are subject to substitution, i.e.
INTERPRETER in the example, must be provided at configuration time.

Regards,

Michael


More information about the CMake mailing list