Embedded python package (zip) for Linux (#871)

Signed-off-by: Paulchen-Panther <Paulchen-Panter@protonmail.com>
This commit is contained in:
Paulchen Panther 2020-07-12 09:26:28 +02:00 committed by GitHub
parent c32a4df587
commit 8c188d71d9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 76 additions and 51 deletions

View File

@ -1,32 +1,32 @@
macro(DeployUnix TARGET) macro(DeployUnix TARGET)
set(TARGET_FILE ${CMAKE_BINARY_DIR}/bin/${TARGET})
set(SYSTEM_LIBS_SKIP
"libc"
"libdl"
"libexpat"
"libfontconfig"
"libfreetype"
"libgcc_s"
"libgcrypt"
"libGL"
"libGLdispatch"
"libglib-2"
"libGLX"
"libgpg-error"
"libm"
"libpthread"
"librt"
"libstdc++"
"libudev"
"libusb-1"
"libutil"
"libX11"
"libz"
)
if(EXISTS ${TARGET_FILE}) if(EXISTS ${TARGET_FILE})
message(STATUS "Collecting Dependencies for target file: ${TARGET_FILE}")
include(GetPrerequisites) include(GetPrerequisites)
set(SYSTEM_LIBS_SKIP
"libc"
"libdl"
"libexpat"
"libfontconfig"
"libfreetype"
"libgcc_s"
"libgcrypt"
"libGL"
"libGLdispatch"
"libglib-2"
"libGLX"
"libgpg-error"
"libm"
"libpthread"
"librt"
"libstdc++"
"libudev"
"libusb-1"
"libutil"
"libX11"
"libz"
)
if (APPLE) if (APPLE)
set(OPENSSL_ROOT_DIR /usr/local/opt/openssl) set(OPENSSL_ROOT_DIR /usr/local/opt/openssl)
endif(APPLE) endif(APPLE)
@ -133,17 +133,19 @@ macro(DeployUnix TARGET)
) )
endforeach() endforeach()
# Detect the Python version and modules directory
if (NOT CMAKE_VERSION VERSION_LESS "3.12") if (NOT CMAKE_VERSION VERSION_LESS "3.12")
# Detect the Python modules directory set(PYTHON_VERSION_MAJOR_MINOR "${Python3_VERSION_MAJOR}${Python3_VERSION_MINOR}")
execute_process( execute_process(
COMMAND ${Python3_EXECUTABLE} -c "from distutils.sysconfig import get_python_lib; print(get_python_lib(standard_lib=True))" COMMAND ${Python3_EXECUTABLE} -c "from distutils.sysconfig import get_python_lib; print(get_python_lib(standard_lib=True))"
OUTPUT_VARIABLE PYTHON_MODULES_DIR OUTPUT_VARIABLE PYTHON_MODULES_DIR
OUTPUT_STRIP_TRAILING_WHITESPACE OUTPUT_STRIP_TRAILING_WHITESPACE
) )
else() else()
# Detect the Python modules directory set(PYTHON_VERSION_MAJOR_MINOR "${PYTHON_VERSION_MAJOR}${PYTHON_VERSION_MINOR}")
execute_process( execute_process(
COMMAND ${PYTHON_EXECUTABLE} -c "from distutils.sysconfig import get_python_lib; print(get_python_lib(standard_lib=True))" COMMAND ${PYTHON_EXECUTABLE} -c "from distutils.sysconfig import get_python_lib; print(get_python_lib(standard_lib=True))"
OUTPUT_VARIABLE PYTHON_MODULES_DIR OUTPUT_VARIABLE PYTHON_MODULES_DIR
@ -152,19 +154,43 @@ macro(DeployUnix TARGET)
endif() endif()
# Copy Python modules to 'share/hyperion/lib/python' # Pack Python modules to pythonXX.zip or copy to 'share/hyperion/lib/python'
if (PYTHON_MODULES_DIR) if (PYTHON_MODULES_DIR)
install( # Since version 3.3.2 CMake has the functionality to generate a zip file built-in.
DIRECTORY ${PYTHON_MODULES_DIR}/ if (NOT CMAKE_VERSION VERSION_LESS "3.3.2")
DESTINATION "share/hyperion/lib/python"
COMPONENT "Hyperion" file(GLOB PYTHON_MODULE_FILES RELATIVE "${PYTHON_MODULES_DIR}" "${PYTHON_MODULES_DIR}/*")
) set(PYTHON_ZIP "python${PYTHON_VERSION_MAJOR_MINOR}.zip")
execute_process(
COMMAND "${CMAKE_COMMAND}" "-E" "tar" "cf" "${CMAKE_BINARY_DIR}/${PYTHON_ZIP}" "--format=zip" ${PYTHON_MODULE_FILES}
WORKING_DIRECTORY "${PYTHON_MODULES_DIR}"
OUTPUT_QUIET
)
install(
FILES "${CMAKE_BINARY_DIR}/${PYTHON_ZIP}"
DESTINATION "share/hyperion/bin"
COMPONENT "Hyperion"
)
else()
install(
DIRECTORY ${PYTHON_MODULES_DIR}/
DESTINATION "share/hyperion/lib/python"
COMPONENT "Hyperion"
)
endif()
endif(PYTHON_MODULES_DIR) endif(PYTHON_MODULES_DIR)
else() else()
# Run CMake after target was built to run get_prerequisites on ${TARGET_FILE} # Run CMake after target was built to run get_prerequisites on ${TARGET_FILE}
add_custom_command( add_custom_command(
TARGET ${TARGET} POST_BUILD TARGET ${TARGET} POST_BUILD
COMMAND ${CMAKE_COMMAND} COMMAND "${CMAKE_COMMAND}" "-DTARGET_FILE=$<TARGET_FILE:${TARGET}>"
ARGS ${CMAKE_SOURCE_DIR} ARGS ${CMAKE_SOURCE_DIR}
WORKING_DIRECTORY ${CMAKE_BINARY_DIR} WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
VERBATIM VERBATIM
@ -190,11 +216,11 @@ macro(DeployWindows TARGET)
execute_process( execute_process(
COMMAND "${CMAKE_COMMAND}" -E COMMAND "${CMAKE_COMMAND}" -E
env "PATH=${COMPILER_PATH};${QT_BIN_DIR}" "${WINDEPLOYQT_EXECUTABLE}" env "PATH=${COMPILER_PATH};${QT_BIN_DIR}" "${WINDEPLOYQT_EXECUTABLE}"
--dry-run --dry-run
${WINDEPLOYQT_PARAMS} ${WINDEPLOYQT_PARAMS}
--list mapping --list mapping
"${TARGET_FILE}" "${TARGET_FILE}"
OUTPUT_VARIABLE DEPS OUTPUT_VARIABLE DEPS
OUTPUT_STRIP_TRAILING_WHITESPACE OUTPUT_STRIP_TRAILING_WHITESPACE
) )

View File

@ -17,29 +17,28 @@
#ifdef _WIN32 #ifdef _WIN32
#include <stdexcept> #include <stdexcept>
#define STRINGIFY2(x) #x
#define STRINGIFY(x) STRINGIFY2(x)
#endif #endif
#define STRINGIFY2(x) #x
#define STRINGIFY(x) STRINGIFY2(x)
PythonInit::PythonInit() PythonInit::PythonInit()
{ {
// register modules // register modules
EffectModule::registerHyperionExtensionModule(); EffectModule::registerHyperionExtensionModule();
// set Python module path when exists // set Python module path when exists
wchar_t *pythonPath = nullptr; QString py_patch = QDir::cleanPath(qApp->applicationDirPath() + "/../lib/python");
#ifdef _WIN32 QString py_file = QDir::cleanPath(qApp->applicationDirPath() + "/python" + STRINGIFY(PYTHON_VERSION_MAJOR_MINOR) + ".zip");
pythonPath = Py_DecodeLocale((QDir::cleanPath(qApp->applicationDirPath()) + "/python" + STRINGIFY(PYTHON_VERSION_MAJOR_MINOR) + ".zip").toLatin1().data(), nullptr);
if(QFile(QString::fromWCharArray(pythonPath)).exists())
#else
pythonPath = Py_DecodeLocale((QDir::cleanPath(qApp->applicationDirPath() + "/../lib/python")).toLatin1().data(), nullptr);
if(QDir(QString::fromWCharArray(pythonPath)).exists())
#endif
if (QFile(py_file).exists() || QDir(py_patch).exists())
{ {
Py_NoSiteFlag++; Py_NoSiteFlag++;
Py_SetPath(pythonPath); if (QFile(py_file).exists())
PyMem_RawFree(pythonPath); Py_SetPath(Py_DecodeLocale(py_file.toLatin1().data(), nullptr));
else if (QDir(py_patch).exists())
Py_SetPath(Py_DecodeLocale(py_patch.toLatin1().data(), nullptr));
} }
// init Python // init Python