Skip to content

Commit

Permalink
Python packaging and initialization (medInria#1123)
Browse files Browse the repository at this point in the history
* adapt to PYNCPP changes

* switch to lowercase for pyncpp

* Fix handling of install path of embedded Python

* more Python fixes

* Minor correction to find OpenSSL automatically on macOS
  • Loading branch information
fcollot authored and mathildemerle committed May 17, 2023
1 parent 0bde0f1 commit b62b07b
Show file tree
Hide file tree
Showing 8 changed files with 125 additions and 25 deletions.
8 changes: 6 additions & 2 deletions packaging/apple/mac_packager.sh.in
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,10 @@
cd @medInria_BINARY_DIR@/bin

\rm -fr TmpInstall
mkdir TmpInstall
cp -r medInria.app TmpInstall
mkdir -p TmpInstall/medInria.app/Contents/Resources
cp -r medInria.app/Contents/MacOS TmpInstall/medInria.app/Contents
cp medInria.app/Contents/Info.plist TmpInstall/medInria.app/Contents
cp medInria.app/Contents/Resources/medInria.icns TmpInstall/medInria.app/Contents/Resources

cd TmpInstall

Expand All @@ -31,6 +33,8 @@ done

@dtk_DIR@/bin/dtkDeploy medInria.app $injectDirs &>/dev/null

@CMAKE_COMMAND@ -DCMAKE_INSTALL_PREFIX:STRING=medInria.app/Contents/Resources -DCMAKE_INSTALL_COMPONENT:STRING=Python -P @pyncpp_DIR@/cmake_install.cmake

#Run fancy packaging apple script

\cp -f @medInria_SOURCE_DIR@/utils/osx_packaging/BaseMedinriaPackage.sparseimage.gz @PROJECT_BINARY_DIR@/MedinriaPackage.sparseimage.gz
Expand Down
5 changes: 5 additions & 0 deletions packaging/windows/WindowsPackaging.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,11 @@ list(APPEND
${RPI_DIR}/bin/Release
)

set(CPACK_INSTALL_CMAKE_PROJECTS
${pyncpp_DIR} pyncpp Python "/"
${CPACK_INSTALL_CMAKE_PROJECTS}
)

install(CODE "
file(GLOB_RECURSE itk_files LIST_DIRECTORIES true \"${ITK_DIR}/bin/*.dll\")
Expand Down
4 changes: 2 additions & 2 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ find_package(dtk REQUIRED)
include_directories(${dtk_INCLUDE_DIRS})

if(USE_Python)
find_package(PYNCPP REQUIRED COMPONENTS Qt5)
find_package(pyncpp REQUIRED COMPONENTS CPP_API Qt5)
endif()

## #############################################################################
Expand Down Expand Up @@ -166,7 +166,7 @@ if(NOT ${CMAKE_BUILD_TYPE} STREQUAL "Debug")
add_definitions(-DQT_NO_DEBUG)
endif()

add_compile_definitions("USE_PYTHON=$<BOOL:USE_PYTHON>")
add_compile_definitions(USE_PYTHON)

## #############################################################################
## Windows specificity
Expand Down
32 changes: 32 additions & 0 deletions src/app/medInria/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,38 @@ target_link_libraries(${TARGET_NAME}
medPacs
)

## #############################################################################
## Python
## #############################################################################

if(USE_Python)
target_link_libraries(${TARGET_NAME} ${pyncpp_CPP_API_LIBRARY})

if(WIN32)
set(python_home ".")
else()
if(APPLE)
set(python_home "../Resources/${pyncpp_PYTHON_INSTALL_DESTINATION}")
else()
set(python_home "../${pyncpp_PYTHON_INSTALL_DESTINATION}")
endif()

get_filename_component(python_home_base "${python_home}" DIRECTORY)
get_filename_component(python_dir "${python_home}" NAME)

add_custom_command(TARGET ${TARGET_NAME} POST_BUILD
COMMAND ${CMAKE_COMMAND} -E make_directory "$<TARGET_FILE_DIR:${TARGET_NAME}>/${python_home_base}"
)

add_custom_command(TARGET ${TARGET_NAME} POST_BUILD
COMMAND ${CMAKE_COMMAND} -E create_symlink "${pyncpp_PYTHON_DIR}" "${python_dir}"
WORKING_DIRECTORY "$<TARGET_FILE_DIR:${TARGET_NAME}>/${python_home_base}"
)
endif()

target_compile_definitions(${TARGET_NAME} PUBLIC PYTHON_HOME="${python_home}")
endif()

## #############################################################################
## install
## #############################################################################
Expand Down
30 changes: 30 additions & 0 deletions src/app/medInria/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@
#include <QtPlatformHeaders/QWindowsWindowFunctions>
#endif

#if(USE_PYTHON)
#include <pyncpp.h>
#endif

#include <medMainWindow.h>
#include <medApplication.h>
#include <medSplashScreen.h>
Expand Down Expand Up @@ -184,6 +188,24 @@ int main(int argc,char* argv[])

medDataManager::instance()->setDatabaseLocation();

#if(USE_PYTHON)
pyncpp::Manager pythonManager;
QDir pythonHome = qApp->applicationDirPath();
QString pythonErrorMessage;

if (!pythonHome.cd(PYTHON_HOME))
{
pythonErrorMessage = "The embedded Python could not be found ";
}
else
{
if(!pythonManager.initialize(qUtf8Printable(pythonHome.absolutePath())))
{
pythonErrorMessage = "Initialization of the embedded Python failed.";
}
}
#endif

medPluginManager::instance()->setVerboseLoading(true);
medPluginManager::instance()->initialize();

Expand Down Expand Up @@ -234,6 +256,14 @@ int main(int argc,char* argv[])
if (show_splash)
splash.finish(mainwindow);

#if(USE_PYTHON)
if(!pythonErrorMessage.isEmpty())
{
QMessageBox::warning(mainwindow, "Python", pythonErrorMessage);
qWarning() << pythonErrorMessage;
}
#endif

if (medPluginManager::instance()->plugins().isEmpty()) {
QMessageBox::warning(mainwindow,
QObject::tr("No plugin loaded"),
Expand Down
12 changes: 11 additions & 1 deletion superbuild/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,16 @@ find_package(Qt5 REQUIRED COMPONENTS

find_package(Boost REQUIRED)

if(USE_Python AND UNIX)
if(APPLE)
set(default_root_dir "/usr/local/opt/openssl@1.1")
else()
set(default_root_dir)
endif()
set(OPENSSL_ROOT_DIR ${default_root_dir} CACHE PATH "Root directory of OpenSSL.")
find_package(OpenSSL COMPONENTS SSL)
endif()

## #############################################################################
## Add exteral projects
## #############################################################################
Expand Down Expand Up @@ -75,7 +85,7 @@ endif()

if (USE_Python)
list(APPEND external_projects
PYNCPP
pyncpp
)
endif()

Expand Down
12 changes: 8 additions & 4 deletions superbuild/projects_modules/medInria.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ list(APPEND ${ep}_dependencies
QtDCM
RPI
LogDemons
pyncpp
)

if (USE_DTKIMAGING)
Expand Down Expand Up @@ -112,7 +113,7 @@ endif()

if (USE_Python)
list(APPEND cmake_cache_args
-DPYNCPP_DIR:PATH=${PYNCPP_DIR}
-Dpyncpp_DIR:PATH=${pyncpp_DIR}
)
endif()

Expand Down Expand Up @@ -152,6 +153,7 @@ if (WIN32)
file(TO_NATIVE_PATH ${QtDCM_DIR} DCM_BIN_BASE)
file(TO_NATIVE_PATH ${_qt5Core_install_prefix} QT5_BIN_BASE)
file(TO_NATIVE_PATH ${medInria_BINARY_DIR} MED_BIN_BASE)
file(TO_NATIVE_PATH ${pyncpp_DIR} PYNCPP_BIN_BASE)

set(CONFIG_MODE $<$<CONFIG:debug>:Debug>$<$<CONFIG:release>:Release>$<$<CONFIG:MinSizeRel>:MinSizeRel>$<$<CONFIG:RelWithDebInfo>:RelWithDebInfo>)

Expand All @@ -161,10 +163,12 @@ if (WIN32)
POST_BUILD
COMMAND for %%I in ( ${ITK_BIN_BASE}\\bin\\${CONFIG_MODE}\\*.dll ) do (if EXIST ${MED_BIN_BASE}\\%%~nxI (del /S ${MED_BIN_BASE}\\%%~nxI & mklink /H ${MED_BIN_BASE}\\%%~nxI %%~fI) else mklink /H ${MED_BIN_BASE}\\%%~nxI %%~fI)
COMMAND for %%I in ( ${VTK_BIN_BASE}\\bin\\${CONFIG_MODE}\\*.dll ) do (if EXIST ${MED_BIN_BASE}\\%%~nxI (del /S ${MED_BIN_BASE}\\%%~nxI & mklink /H ${MED_BIN_BASE}\\%%~nxI %%~fI) else mklink /H ${MED_BIN_BASE}\\%%~nxI %%~fI)
COMMAND for %%I in ( ${DTK_BIN_BASE}\\bin\\${CONFIG_MODE}\\*.dll ) do (if EXIST ${MED_BIN_BASE}\\%%~nxI (del /S ${MED_BIN_BASE}\\%%~nxI & mklink /H ${MED_BIN_BASE}\\%%~nxI %%~fI) else mklink /H ${MED_BIN_BASE}\\%%~nxI %%~fI)
COMMAND for %%I in ( ${DCM_BIN_BASE}\\bin\\${CONFIG_MODE}\\*.dll ) do (if EXIST ${MED_BIN_BASE}\\%%~nxI (del /S ${MED_BIN_BASE}\\%%~nxI & mklink /H ${MED_BIN_BASE}\\%%~nxI %%~fI) else mklink /H ${MED_BIN_BASE}\\%%~nxI %%~fI)
COMMAND for %%I in ( ${DTK_BIN_BASE}\\bin\\${CONFIG_MODE}\\*.dll ) do (if EXIST ${MED_BIN_BASE}\\%%~nxI (del /S ${MED_BIN_BASE}\\%%~nxI & mklink /H ${MED_BIN_BASE}\\%%~nxI %%~fI) else mklink /H ${MED_BIN_BASE}\\%%~nxI %%~fI)
COMMAND for %%I in ( ${DCM_BIN_BASE}\\bin\\${CONFIG_MODE}\\*.dll ) do (if EXIST ${MED_BIN_BASE}\\%%~nxI (del /S ${MED_BIN_BASE}\\%%~nxI & mklink /H ${MED_BIN_BASE}\\%%~nxI %%~fI) else mklink /H ${MED_BIN_BASE}\\%%~nxI %%~fI)
COMMAND for %%I in ( ${QT5_BIN_BASE}\\bin\\*.dll ) do (if EXIST ${MED_BIN_BASE}\\%%~nxI (del /S ${MED_BIN_BASE}\\%%~nxI & mklink /H ${MED_BIN_BASE}\\%%~nxI %%~fI) else mklink /H ${MED_BIN_BASE}\\%%~nxI %%~fI)
)
COMMAND for %%I in ( ${PYTHON_BIN_BASE}\\bin\\*.dll ) do (if EXIST ${MED_BIN_BASE}\\%%~nxI (del /S ${MED_BIN_BASE}\\%%~nxI & mklink /H ${MED_BIN_BASE}\\%%~nxI %%~fI) else mklink /H ${MED_BIN_BASE}\\%%~nxI %%~fI)
COMMAND for %%I in ( ${PYTHON_BIN_BASE}\\bin\\DLLs ${PYTHON_BIN_BASE}\\bin\\Lib ) do (if EXIST ${MED_BIN_BASE}\\%%~nxI (del /S ${MED_BIN_BASE}\\%%~nxI & mklink /d /H ${MED_BIN_BASE}\\%%~nxI %%~fI) else mklink /d /H ${MED_BIN_BASE}\\%%~nxI %%~fI)
)
endif()


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,25 +11,23 @@
#
################################################################################

function(PYNCPP_project)
set(PYTHON_VERSION_MAJOR 3 CACHE STRING "Python major version")
set(PYTHON_VERSION_MINOR 10 CACHE STRING "Python minor version")
set(PYTHON_VERSION_PATCH 10 CACHE STRING "Python patch version")

set(ep PYNCPP)
function(pyncpp_project)

set(ep pyncpp)

EP_Initialisation(${ep}
USE_SYSTEM OFF
BUILD_SHARED_LIBS ON
REQUIRED_FOR_PLUGINS OFF
REQUIRED_FOR_PLUGINS ON
)

if(USE_SYSTEM_${ep})
find_package(PYNCPP
REQUIRED COMPONENTS Qt5
)
else()
epComputPath(${ep})
if(NOT USE_SYSTEM_${ep})

set(source_dir ${EP_PATH_SOURCE}/${ep})
set(binary_dir ${build_path})
epComputPath(${ep})

set(project_args
GIT_REPOSITORY ${GITHUB_PREFIX}LIRYC-IHU/pyncpp.git
Expand All @@ -38,28 +36,45 @@ function(PYNCPP_project)
GIT_PROGRESS True
)

set(cmake_args
-D "PYNCPP_PYTHON_VERSION_MAJOR:STRING=${PYTHON_VERSION_MAJOR}"
-D "PYNCPP_PYTHON_VERSION_MINOR:STRING=${PYTHON_VERSION_MINOR}"
-D "PYNCPP_PYTHON_VERSION_PATCH:STRING=${PYTHON_VERSION_PATCH}"
)

if(UNIX)
list(APPEND cmake_args
-D Qt5_DIR:PATH=${Qt5_DIR}
-D OPENSSL_ROOT_DIR:PATH=${OPENSSL_ROOT_DIR}
)
if(APPLE)
list(APPEND cmake_args
-D CMAKE_MACOSX_RPATH:BOOL=OFF
)
endif()
endif()

## #####################################################################
## Add external project
## #####################################################################

ExternalProject_Add(${ep}
PREFIX ${EP_PATH_SOURCE}
SOURCE_DIR ${source_dir}
BINARY_DIR ${binary_dir}
SOURCE_DIR ${EP_PATH_SOURCE}/${ep}
BINARY_DIR ${build_path}
TMP_DIR ${tmp_path}
STAMP_DIR ${stamp_path}
DEPENDS ${${ep}_dependencies}
CMAKE_ARGS ${cmake_args}
INSTALL_COMMAND ""
"${project_args}"
)

ExternalProject_Get_Property(${ep} binary_dir)

## #####################################################################
## Export variables
## #####################################################################

set(${ep}_DIR ${binary_dir} PARENT_SCOPE)
set(${ep}_DIR ${build_path} PARENT_SCOPE)

endif()

Expand Down

0 comments on commit b62b07b

Please sign in to comment.