build: Use Python venv by default

This commit is contained in:
Gabriel Ferreira
2025-06-18 10:34:37 +02:00
parent 1093479bcf
commit bb7b22838a
2 changed files with 23 additions and 7 deletions

View File

@@ -40,6 +40,27 @@ function(check_deps missing_deps)
)
if(NOT (${return_code} EQUAL 0))
list(APPEND local_missing_deps ${package})
else()
# To make sure CMake import files can be found from venv site packages, we
# manually add them to CMAKE_PREFIX_PATH
execute_process(
COMMAND
${Python3_EXECUTABLE} -c
"import os; import ${package}; print(os.path.abspath(os.path.dirname(${package}.__file__)))"
OUTPUT_VARIABLE venv_site_packages_path
)
# Remove newlines (\n, \r, \r\n)
string(REGEX REPLACE "[\r\n]+$" "" venv_site_packages_path
"${venv_site_packages_path}"
)
if(EXISTS ${venv_site_packages_path})
if(NOT (DEFINED CMAKE_PREFIX_PATH))
set(CMAKE_PREFIX_PATH "" PARENT_SCOPE)
endif()
set(CMAKE_PREFIX_PATH "${CMAKE_PREFIX_PATH};${venv_site_packages_path}"
PARENT_SCOPE
)
endif()
endif()
endforeach()

View File

@@ -628,16 +628,11 @@ macro(process_options)
set(Python3_FOUND FALSE)
set(Python3_INCLUDE_DIRS)
set(Python3_Interpreter_FOUND FALSE)
set(Python3_FIND_VIRTUALENV FIRST)
if(${NS3_PYTHON_BINDINGS})
find_package(Python3 COMPONENTS Interpreter Development)
else()
# If Python was not set yet, use the version found by check_deps
check_deps(python3_deps EXECUTABLES python3)
if(python3_deps)
message(FATAL_ERROR "Python3 was not found")
else()
set(Python3_EXECUTABLE ${PYTHON3})
endif()
find_package(Python3 COMPONENTS Interpreter)
endif()
# Check if both Python interpreter and development libraries were found