cmake(python): Add BUILD_VENV option

This commit is contained in:
Mizux Seiha
2022-10-27 10:56:20 +02:00
parent 9b0825d58c
commit 0dd8a8756e
2 changed files with 14 additions and 6 deletions

View File

@@ -88,6 +88,10 @@ if(BUILD_SHARED_LIBS AND MSVC)
set(CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS ON)
endif()
# Disable CTest targets
set_property(GLOBAL PROPERTY CTEST_TARGETS_ADDED 1)
include(CTest)
# By default only build the C++ library.
option(BUILD_CXX "Build C++ library" ON)
message(STATUS "Build C++ library: ${BUILD_CXX}")
@@ -286,6 +290,10 @@ if(BUILD_PYTHON)
"NOT BUILD_DEPS" ON)
message(STATUS "Python: Build pybind11: ${BUILD_pybind11}")
CMAKE_DEPENDENT_OPTION(BUILD_VENV "Create Python venv in BINARY_DIR/python/venv" OFF
"NOT BUILD_TESTING" ON)
message(STATUS "Python: Create venv: ${BUILD_VENV}")
option(VENV_USE_SYSTEM_SITE_PACKAGES "Python venv can use system site packages" OFF)
message(STATUS "Python: Allow venv to use system site packages: ${VENV_USE_SYSTEM_SITE_PACKAGES}")
@@ -297,10 +305,6 @@ endif()
add_subdirectory(cmake/dependencies dependencies)
list(APPEND CMAKE_PREFIX_PATH ${CMAKE_CURRENT_BINARY_DIR}/dependencies/install)
# Disable CTest targets
set_property(GLOBAL PROPERTY CTEST_TARGETS_ADDED 1)
include(CTest)
# Basic type
include(CMakePushCheckState)
cmake_push_check_state(RESET)

View File

@@ -179,7 +179,7 @@ add_custom_target(Py${PROJECT_NAME}_proto
###################
## Python Test ##
###################
if(BUILD_TESTING)
if(BUILD_VENV)
search_python_module(NAME virtualenv PACKAGE virtualenv)
# venv not working on github runners
# search_python_internal_module(NAME venv)
@@ -192,7 +192,9 @@ if(BUILD_TESTING)
else()
set(VENV_Python3_EXECUTABLE ${VENV_DIR}/bin/python)
endif()
endif()
if(BUILD_TESTING)
# add_python_test()
# CMake function to generate and build python test.
# Parameters:
@@ -371,7 +373,7 @@ configure_file(
@ONLY)
install(SCRIPT ${PROJECT_BINARY_DIR}/python/python-install.cmake)
if(BUILD_TESTING)
if(BUILD_VENV)
# make a virtualenv to install our python package in it
add_custom_command(TARGET python_package POST_BUILD
# Clean previous install otherwise pip install may do nothing
@@ -390,7 +392,9 @@ if(BUILD_TESTING)
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
COMMENT "Create venv and install ${PYTHON_PROJECT}"
VERBATIM)
endif()
if(BUILD_TESTING)
configure_file(
${PROJECT_SOURCE_DIR}/ortools/init/python/version_test.py.in
${PROJECT_BINARY_DIR}/python/version_test.py