From 630e5cc93f9516636d1945054a9b5492f1b8295d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mika=C3=ABl=20Capelle?= Date: Mon, 23 Nov 2020 10:58:17 +0100 Subject: [PATCH] Improve Python module installation. --- python_module_plugin.cmake | 51 +++++++++++++++++++++++++++++++++++--- 1 file changed, 47 insertions(+), 4 deletions(-) diff --git a/python_module_plugin.cmake b/python_module_plugin.cmake index a6721f1..230454a 100644 --- a/python_module_plugin.cmake +++ b/python_module_plugin.cmake @@ -2,19 +2,46 @@ cmake_minimum_required(VERSION 3.16) include(${CMAKE_CURRENT_LIST_DIR}/python.cmake) if(NOT DEFINED install_dir) - set(install_dir bin/plugins) + set(install_dir "bin/plugins/${CMAKE_PROJECT_NAME}") endif() +if(NOT DEFINED res_dir) + set(res_dir res) +endif() + +if(NOT DEFINED lib_dir) + set(lib_dir lib) +endif() macro(do_project) do_python_project() + + # install requirements if there are any + if(EXISTS "${CMAKE_SOURCE_DIR}/plugin-requirements.txt") + add_custom_command( + OUTPUT "${CMAKE_SOURCE_DIR}/${lib_dir}/" + COMMAND ${PYTHON_ROOT}/PCbuild/amd64/python.exe + -I + -m pip + install --force --disable-pip-version-check + --target="${CMAKE_SOURCE_DIR}/${lib_dir}" + -r "${CMAKE_SOURCE_DIR}/plugin-requirements.txt" + WORKING_DIRECTORY ${PYTHON_ROOT}) + add_custom_target(libs ALL DEPENDS "${CMAKE_SOURCE_DIR}/${lib_dir}") + endif() endmacro() macro(do_src) # this copies all the .py files into ${install_dir}/${PROJECT_NAME} + if(EXISTS "${CMAKE_SOURCE_DIR}/__init__.py") + set(src_dir ${CMAKE_SOURCE_DIR}) + else() + set(src_dir ${CMAKE_SOURCE_DIR}/src) + endif() + # resources in the src directory - file(GLOB_RECURSE resources ${CMAKE_SOURCE_DIR}/*.ui ${CMAKE_SOURCE_DIR}/*.qrc) + file(GLOB_RECURSE resources ${src_dir}/*.ui ${src_dir}/*.qrc) foreach(object ${resources}) get_filename_component(ext "${object}" LAST_EXT) @@ -54,11 +81,27 @@ macro(do_src) file(GLOB_RECURSE src_files RELATIVE ${CMAKE_SOURCE_DIR} ${CMAKE_SOURCE_DIR}/*.py) - # # directories that go in bin/plugins/${name} + # directories that go in bin/plugins/${name} install( - DIRECTORY ${CMAKE_SOURCE_DIR} + DIRECTORY "${src_dir}/" DESTINATION ${install_dir} FILES_MATCHING PATTERN "*.py" PATTERN ".git*" EXCLUDE PATTERN "vsbuild" EXCLUDE) + + # copy the resource directory if it exists + if(EXISTS "${CMAKE_SOURCE_DIR}/${res_dir}") + install( + DIRECTORY "${CMAKE_SOURCE_DIR}/${res_dir}" + DESTINATION ${install_dir} + ) + endif() + + if(EXISTS "${CMAKE_SOURCE_DIR}/plugin-requirements.txt") + install( + DIRECTORY "${CMAKE_SOURCE_DIR}/${lib_dir}" + DESTINATION ${install_dir} + ) + endif() + endmacro()