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,5 +1,8 @@
macro(DeployUnix TARGET) macro(DeployUnix TARGET)
set(TARGET_FILE ${CMAKE_BINARY_DIR}/bin/${TARGET}) if(EXISTS ${TARGET_FILE})
message(STATUS "Collecting Dependencies for target file: ${TARGET_FILE}")
include(GetPrerequisites)
set(SYSTEM_LIBS_SKIP set(SYSTEM_LIBS_SKIP
"libc" "libc"
"libdl" "libdl"
@ -24,9 +27,6 @@ macro(DeployUnix TARGET)
"libz" "libz"
) )
if(EXISTS ${TARGET_FILE})
include(GetPrerequisites)
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)
# Since version 3.3.2 CMake has the functionality to generate a zip file built-in.
if (NOT CMAKE_VERSION VERSION_LESS "3.3.2")
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( install(
DIRECTORY ${PYTHON_MODULES_DIR}/ DIRECTORY ${PYTHON_MODULES_DIR}/
DESTINATION "share/hyperion/lib/python" DESTINATION "share/hyperion/lib/python"
COMPONENT "Hyperion" 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

View File

@ -17,9 +17,11 @@
#ifdef _WIN32 #ifdef _WIN32
#include <stdexcept> #include <stdexcept>
#endif
#define STRINGIFY2(x) #x #define STRINGIFY2(x) #x
#define STRINGIFY(x) STRINGIFY2(x) #define STRINGIFY(x) STRINGIFY2(x)
#endif
PythonInit::PythonInit() PythonInit::PythonInit()
{ {
@ -27,19 +29,16 @@ PythonInit::PythonInit()
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