Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CMake - setting up python virtualenv

I would like for my project which is being build with portions in cmake - to have cmake set up a virtualenv for my python code.

Is this usually done or should I just assume that my python environment is going to be a global one?

like image 673
MKaras Avatar asked Jun 12 '15 11:06

MKaras


1 Answers

The linked gist works

cmake_minimum_required(VERSION 3.6)

project(CmakeVirtualenv)

enable_testing()

# Find Python and Virtualenv. We don't actually use the output of the
# find_package, but it'll give nicer errors.
find_package(PythonInterp 2.7 REQUIRED)
find_program(VIRTUALENV virtualenv)
if(NOT VIRTUALENV)
    message(FATAL_ERROR "Could not find `virtualenv` in PATH")
endif()
set(VIRTUALENV ${VIRTUALENV} -p python2.7)

# Generate the virtualenv and ensure it's up to date.
add_custom_command(
    OUTPUT venv
    COMMAND ${VIRTUALENV} venv
)
add_custom_command(
    OUTPUT venv.stamp
    DEPENDS venv requirements.txt
    COMMAND ${CMAKE_COMMAND} -E copy ${CMAKE_CURRENT_SOURCE_DIR}/requirements.txt requirements.txt
    COMMAND ./venv/bin/pip install -r requirements.txt --upgrade
)

# Build command line to run py.test.
set(PYTEST
    ${CMAKE_CURRENT_BINARY_DIR}/venv/bin/python2
    ${CMAKE_CURRENT_BINARY_DIR}/venv/bin/py.test
)


add_custom_target(Tests ALL
    DEPENDS venv.stamp
    SOURCES requirements.txt
)

add_test(NAME run_tests
    COMMAND ${PYTEST} ${CMAKE_CURRENT_SOURCE_DIR}/test_sample.py
)

Note that you might need to use ${CMAKE_CURRENT_BINARY_PATH/venv} or similar if you use WORKING_DIRECTORY to override the current working directory.

like image 123
sehe Avatar answered Sep 18 '22 08:09

sehe