From 181ba887401778a909e027a5f518aff00dafdec1 Mon Sep 17 00:00:00 2001 From: Sindre Nistad Date: Mon, 8 Jan 2024 12:02:09 +0100 Subject: [PATCH] chore: Only install dependencies once --- cmake/python-venv.cmake | 48 ++++++++++++++++++++++------------------- 1 file changed, 26 insertions(+), 22 deletions(-) diff --git a/cmake/python-venv.cmake b/cmake/python-venv.cmake index ec250387..d3222db9 100644 --- a/cmake/python-venv.cmake +++ b/cmake/python-venv.cmake @@ -1,3 +1,4 @@ +set(VENV_INITIALIZED ON) if (NOT EXISTS ${CMAKE_BINARY_DIR}/venv) message(STATUS "Creating a new venv in ${CMAKE_BINARY_DIR}/venv") find_package(Python3 ${Python3_VERSION} EXACT REQUIRED COMPONENTS Interpreter) @@ -6,25 +7,27 @@ if (NOT EXISTS ${CMAKE_BINARY_DIR}/venv) COMMAND_ERROR_IS_FATAL ANY ) unset(${Python3_EXECUTABLE}) + set(VENV_INITIALIZED OFF) endif () -message(STATUS "Installing necessary dependencies in ${CMAKE_BINARY_DIR}/venv") set(Python3_EXECUTABLE "${CMAKE_BINARY_DIR}/venv/bin/python") find_package(Python3 REQUIRED COMPONENTS Interpreter) -message(STATUS ${Python3_EXECUTABLE}) -# Ensure pip ins up do date -execute_process( - COMMAND ${Python3_EXECUTABLE} -m pip install --upgrade pip - COMMAND_ERROR_IS_FATAL ANY -) -# For very old version of pip, we may need to update it twice (e.g. Python 3.6) -execute_process( - COMMAND ${Python3_EXECUTABLE} -m pip install --upgrade pip - COMMAND_ERROR_IS_FATAL ANY -) -execute_process( - # Ensure toml / tomllib is installed - COMMAND ${Python3_EXECUTABLE} -c " +if (NOT VENV_INITIALIZED) + message(STATUS "Installing necessary dependencies in ${CMAKE_BINARY_DIR}/venv") + + # Ensure pip ins up do date + execute_process( + COMMAND ${Python3_EXECUTABLE} -m pip install --upgrade pip + COMMAND_ERROR_IS_FATAL ANY + ) + # For very old version of pip, we may need to update it twice (e.g. Python 3.6) + execute_process( + COMMAND ${Python3_EXECUTABLE} -m pip install --upgrade pip + COMMAND_ERROR_IS_FATAL ANY + ) + execute_process( + # Ensure toml / tomllib is installed + COMMAND ${Python3_EXECUTABLE} -c " try: # Newer versions of Python come with tomllib preinstalled import tomllib @@ -46,10 +49,11 @@ except ImportError: check=True, ) " - COMMAND_ERROR_IS_FATAL ANY -) -execute_process( - COMMAND ${CMAKE_COMMAND} -E copy ${CMAKE_SOURCE_DIR}/pyproject.toml pyproject.toml - COMMAND ${Python3_EXECUTABLE} ${CMAKE_SOURCE_DIR}/bin/install-build-requirements.py - COMMAND_ERROR_IS_FATAL ANY -) + COMMAND_ERROR_IS_FATAL ANY + ) + execute_process( + COMMAND ${CMAKE_COMMAND} -E copy ${CMAKE_SOURCE_DIR}/pyproject.toml pyproject.toml + COMMAND ${Python3_EXECUTABLE} ${CMAKE_SOURCE_DIR}/bin/install-build-requirements.py + COMMAND_ERROR_IS_FATAL ANY + ) +endif ()