Add Python setup to CMakeLists.txt

This commit is contained in:
Dan Paulat 2025-05-28 22:21:48 -05:00
parent ffbe3aedad
commit ea2c2e8f58
4 changed files with 44 additions and 4 deletions

View file

@ -6,7 +6,7 @@
-DCMAKE_PROJECT_TOP_LEVEL_INCLUDES="%script_dir%\..\..\external\cmake-conan\conan_provider.cmake" ^
-DCONAN_HOST_PROFILE=%conan_profile% ^
-DCONAN_BUILD_PROFILE=%conan_profile% ^
-DSCWX_VENV_PATH=%venv_path%
-DSCWX_VIRTUAL_ENV=%venv_path%
@if defined build_type (
set cmake_args=%cmake_args% ^

View file

@ -17,3 +17,39 @@ macro(scwx_output_dirs_setup)
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY_MINSIZEREL ${CMAKE_CURRENT_BINARY_DIR}/MinSizeRel/lib)
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY_DEBUG ${CMAKE_CURRENT_BINARY_DIR}/Debug/lib)
endmacro()
macro(scwx_python_setup)
set(SCWX_VIRTUAL_ENV "" CACHE STRING "Python Virtual Environment")
# Use a Python Virtual Environment
if (SCWX_VIRTUAL_ENV)
set(ENV{VIRTUAL_ENV} "${SCWX_VIRTUAL_ENV}")
if (WIN32)
set(Python3_EXECUTABLE "$ENV{VIRTUAL_ENV}/Scripts/python.exe")
else()
set(Python3_EXECUTABLE "$ENV{VIRTUAL_ENV}/bin/python")
endif()
message(STATUS "Using virtual environment: $ENV{VIRTUAL_ENV}")
else()
message(STATUS "Python virtual environment undefined")
endif()
# Find Python
find_package(Python3 REQUIRED COMPONENTS Interpreter)
# Verify we're using the right Python
message(STATUS "Python executable: ${Python3_EXECUTABLE}")
message(STATUS "Python version: ${Python3_VERSION}")
# Only if we are in an application defined virtual environment
if (SCWX_VIRTUAL_ENV)
# Setup pip
set(PIP_ARGS install --upgrade -r "${CMAKE_SOURCE_DIR}/requirements.txt")
# Install requirements
execute_process(COMMAND ${Python3_EXECUTABLE} -m pip ${PIP_ARGS}
RESULT_VARIABLE PIP_RESULT)
endif()
endmacro()