Skip to content

Commit

Permalink
Ability to create Windows python venv with cmake
Browse files Browse the repository at this point in the history
  • Loading branch information
dougmassay committed Nov 9, 2024
1 parent f9f3c48 commit 5eac56a
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 5 deletions.
34 changes: 31 additions & 3 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,22 @@ cmake_minimum_required( VERSION 3.18 )

project( Sigil )

# Change this URL if the location of the CI Windows Qt6 download ever changes
set (WINQTURL "https://github.com/dougmassay/win-qtwebkit-5.212/releases/download/v5.212-1")

set( RECENT_CMAKE_VER "3.18" )
if ( NOT DEFINED TRY_NEWER_FINDPYTHON3 )
set ( TRY_NEWER_FINDPYTHON3 0 )
endif()

if ( NOT DEFINED USE_VIRT_PY )
set ( USE_VERT_PY 0 )
endif()

if ( NOT DEFINED DOWNLOAD_QT )
set ( DOWNLOAD_QT 0 )
endif()

# Set Inno minimum Windows version
if ( NOT DEFINED BUILD_FOR_WIN7 )
# Windows 10 (1809)
Expand Down Expand Up @@ -121,11 +132,28 @@ if (${USE_NEWER_FINDPYTHON3})
find_package(Python3 3.9 COMPONENTS Interpreter Development)
endif()
if ( WIN32 )
if ( DOWNLOAD_QT )
include(windownloadqt.cmake)
set(CMAKE_PREFIX_PATH "${CMAKE_CURRENT_BINARY_DIR}/Qt${QTVER}/lib/cmake")
set(Qt6_Dir "${CMAKE_CURRENT_BINARY_DIR}/Qt${QTVER}/lib/cmake/Qt6")
if(NOT EXISTS "${Qt6_Dir}")
message(FATAL_ERROR "Qt6 download/configure unsuccessful!")
endif()
endif()
if ( USE_VIRT_PY )
# Must be run in an environment with the venv activated
set ( Python3_FIND_VIRTUAL ONLY )
include(winvirtpy.cmake)
# Set hints so virtual Python environement is found first
set(Python3_FIND_VIRTUAL ONLY)
set(Python3_FIND_REGISTRY NEVER)
set(Python3_FIND_STRATEGY LOCATION)
# Trick cmake's findPython3 module into thinking the virtual python is activated
set(VIRT_PATH "${CMAKE_CURRENT_BINARY_DIR}/sigilpy")
set(ENV{VIRTUAL_ENV} "${VIRT_PATH}")
if(NOT EXISTS "${CMAKE_CURRENT_BINARY_DIR}/sigilpy/Scripts/python.exe")
message(FATAL_ERROR "Virtual Python setup unsuccessful!")
endif()
endif()
find_package(Python3 3.9 COMPONENTS Interpreter Development)
find_package(Python3 3.9 REQUIRED COMPONENTS Interpreter Development)
endif()
if( UNIX AND NOT APPLE )
if ( USE_VIRT_PY )
Expand Down
13 changes: 13 additions & 0 deletions src/Resource_Files/python_pkg/winreqs.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
six==1.16.0
html5lib==1.1
regex==2024.5.15
css-parser==1.0.10
cssselect==1.2.0
urllib3==2.2.2
certifi==2024.6.2
dulwich==0.22.1
chardet==5.2.0
pillow==10.3.0
lxml==5.2.2
shiboken6@https://github.com/dougmassay/win-qtwebkit-5.212/releases/download/v5.212-1/shiboken6-6.7.3-6.7.3-cp311-cp311-win_amd64.whl
PySide6@https://github.com/dougmassay/win-qtwebkit-5.212/releases/download/v5.212-1/PySide6-6.7.3-6.7.3-cp311-cp311-win_amd64.whl
4 changes: 2 additions & 2 deletions src/qt6sigil.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -425,11 +425,11 @@ elseif (MSVC)
# Sigil doesn't need QtUiTools or QtOpenGlWidgets to function,
# but Pyside6 needs them to use the QUiLoader feature.
add_custom_command( TARGET ${TARGET_FOR_COPY} POST_BUILD COMMAND
windeployqt.exe --release --no-translations --no-compiler-runtime --dir ${MAIN_PACKAGE_DIR}
${QT_INSTALL_BINS}/windeployqt.exe --release --no-translations --no-compiler-runtime --dir ${MAIN_PACKAGE_DIR}
--libdir ${MAIN_PACKAGE_DIR} -openglwidgets ${MAIN_PACKAGE_DIR}/${PROJECT_NAME}${CMAKE_EXECUTABLE_SUFFIX} )
else()
add_custom_command( TARGET ${TARGET_FOR_COPY} POST_BUILD COMMAND
windeployqt.exe --release --no-translations --no-compiler-runtime --dir ${MAIN_PACKAGE_DIR}
${QT_INSTALL_BINS}/windeployqt.exe --release --no-translations --no-compiler-runtime --dir ${MAIN_PACKAGE_DIR}
--libdir ${MAIN_PACKAGE_DIR} ${MAIN_PACKAGE_DIR}/${PROJECT_NAME}${CMAKE_EXECUTABLE_SUFFIX} )
endif()

Expand Down
12 changes: 12 additions & 0 deletions windownloadqt.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
if(NOT DEFINED QTVER)
message(FATAL_ERROR "-DQTVER must be used if downloading custom Qt6")
endif()

if(NOT EXISTS "${CMAKE_CURRENT_BINARY_DIR}/Qt${QTVER}")
message(STATUS "Downloading Custom Qt6 from developer's github...")
set(QTURL "${WINQTURL}/Qt${QTVER}ci_x64_VS2022.7z")
file(DOWNLOAD "${QTURL}" "${CMAKE_CURRENT_BINARY_DIR}/qt6.7z")
file(ARCHIVE_EXTRACT INPUT "${CMAKE_CURRENT_BINARY_DIR}/qt6.7z" )
else()
message(STATUS "Custom Qt6 has already been downloaded to ${CMAKE_CURRENT_BINARY_DIR}. Will try to use it.")
endif()
16 changes: 16 additions & 0 deletions winvirtpy.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
if(NOT EXISTS "${CMAKE_CURRENT_BINARY_DIR}/sigilpy")
find_package(Python3 3.9 COMPONENTS Interpreter)
set(REQUIREMENTS "${PROJECT_SOURCE_DIR}/src/Resource_Files/python_pkg/winreqs.txt")
# Create venv with system python
message(STATUS "Creating virtual python environment in ${CMAKE_CURRENT_BINARY_DIR}/sigilpy")
execute_process(COMMAND "${Python3_EXECUTABLE}" -m venv "${CMAKE_CURRENT_BINARY_DIR}/sigilpy")
# Pip install -r requirements.txt into venv
message(STATUS "Downloading/installing required python modules into venv using pip requirements file: ${REQUIREMENTS}")
execute_process(COMMAND "${CMAKE_CURRENT_BINARY_DIR}/sigilpy/Scripts/python.exe" -m pip install -r "${REQUIREMENTS}")
# Unset system python so venv python can be found
unset(Python3_EXECUTABLE)
else()
# Try to reuse venv if already present
message(STATUS "${CMAKE_CURRENT_BINARY_DIR}/sigilpy already exists. Will try to use it.")
set (Python3_EXECUTABLE "${CMAKE_CURRENT_BINARY_DIR}/sigilpy/Scripts/python.exe")
endif()

0 comments on commit 5eac56a

Please sign in to comment.