mirror of
https://github.com/ModOrganizer2/cmake_common.git
synced 2026-07-27 14:06:46 -07:00
Many fixes for Python plugins.
This commit is contained in:
+186
-59
@@ -4,6 +4,7 @@ include(${CMAKE_CURRENT_LIST_DIR}/helpers/PyQt5TranslationMacros.cmake)
|
||||
include(${CMAKE_CURRENT_LIST_DIR}/mo2_utils.cmake)
|
||||
|
||||
#! mo2_python_translations : create translations for a python target
|
||||
#
|
||||
function(mo2_python_translations MO2_TARGET)
|
||||
find_package(Qt5LinguistTools)
|
||||
|
||||
@@ -27,6 +28,118 @@ function(mo2_python_translations MO2_TARGET)
|
||||
|
||||
endfunction()
|
||||
|
||||
#! mo2_python_uifiles : create .py files from .ui files for a python target
|
||||
#
|
||||
# \param:INPLACE if specified, .py files are generated next to the .ui files, useful
|
||||
# for Python modules, otherwise files are generated in the binary directory
|
||||
# \param:FILES list of .ui files to generate .py files from
|
||||
#
|
||||
function(mo2_python_uifiles MO2_TARGET)
|
||||
cmake_parse_arguments(MO2 "INPLACE" "" "FILES" ${ARGN})
|
||||
|
||||
if (NOT MO2_FILES)
|
||||
return()
|
||||
endif()
|
||||
|
||||
message(DEBUG "generating .py from ui files: ${MO2_FILES}")
|
||||
|
||||
set(pyui_files "")
|
||||
foreach (UI_FILE ${MO2_FILES})
|
||||
get_filename_component(name "${UI_FILE}" NAME_WLE)
|
||||
if (${MO2_INPLACE})
|
||||
get_filename_component(folder "${UI_FILE}" DIRECTORY)
|
||||
else()
|
||||
set(folder "${CMAKE_CURRENT_BINARY_DIR}")
|
||||
endif()
|
||||
|
||||
set(output "${folder}/${name}.py")
|
||||
add_custom_command(
|
||||
OUTPUT "${output}"
|
||||
COMMAND ${PYTHON_ROOT}/PCbuild/amd64/python.exe
|
||||
-I
|
||||
-m PyQt5.uic.pyuic
|
||||
-o "${output}"
|
||||
"${UI_FILE}"
|
||||
WORKING_DIRECTORY ${PYTHON_ROOT}
|
||||
DEPENDS "${UI_FILE}"
|
||||
)
|
||||
|
||||
list(APPEND pyui_files "${output}")
|
||||
endforeach()
|
||||
|
||||
add_custom_target("${MO2_TARGET}_uic" DEPENDS ${pyui_files})
|
||||
add_dependencies(${MO2_TARGET} "${MO2_TARGET}_uic")
|
||||
|
||||
endfunction()
|
||||
|
||||
#! mo2_python_rcfiles : create .py files from .qrc files for a python target
|
||||
#
|
||||
# \param:INPLACE if specified, .py files are generated next to the .qrc files, useful
|
||||
# for Python modules, otherwise files are generated in the binary directory
|
||||
# \param:FILES list of .qrc files to generate .py files from
|
||||
#
|
||||
function(mo2_python_rcfiles MO2_TARGET)
|
||||
cmake_parse_arguments(MO2 "" "INPLACE" "FILES" ${ARGN})
|
||||
|
||||
if (NOT MO2_FILES)
|
||||
return()
|
||||
endif()
|
||||
|
||||
message(DEBUG "generating .py from qrc files: ${MO2_FILES}")
|
||||
|
||||
set(pyrc_files "")
|
||||
foreach (RC_FILE ${MO2_FILES})
|
||||
get_filename_component(name "${RC_FILE}" NAME_WLE)
|
||||
get_filename_component(folder "${RC_FILE}" DIRECTORY)
|
||||
if (${MO2_INPLACE})
|
||||
get_filename_component(folder "${UI_FILE}" DIRECTORY)
|
||||
else()
|
||||
set(folder "${CMAKE_CURRENT_BINARY_DIR}")
|
||||
endif()
|
||||
|
||||
|
||||
set(output "${folder}/${name}.py")
|
||||
add_custom_command(
|
||||
OUTPUT "${output}"
|
||||
COMMAND ${PYTHON_ROOT}/PCbuild/amd64/python.exe
|
||||
-I
|
||||
-m PyQt5.pyrcc_main
|
||||
-o "${output}"
|
||||
"${RC_FILE}"
|
||||
WORKING_DIRECTORY ${PYTHON_ROOT}
|
||||
DEPENDS "${RC_FILE}"
|
||||
)
|
||||
|
||||
list(APPEND pyrc_files "${output}")
|
||||
endforeach()
|
||||
|
||||
add_custom_target("${MO2_TARGET}_qrc" DEPENDS ${pyrc_files})
|
||||
add_dependencies(${MO2_TARGET} "${MO2_TARGET}_qrc")
|
||||
|
||||
endfunction()
|
||||
|
||||
#! mo2_python_requirements : install requirements for a python target
|
||||
#
|
||||
function(mo2_python_requirements MO2_TARGET)
|
||||
cmake_parse_arguments(MO2 "" "LIBDIR" "")
|
||||
|
||||
add_custom_command(
|
||||
OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/pip.log"
|
||||
COMMAND ${PYTHON_ROOT}/PCbuild/amd64/python.exe
|
||||
-I
|
||||
-m pip
|
||||
install --force --upgrade --disable-pip-version-check
|
||||
--target="${lib_dir}"
|
||||
--log="${CMAKE_CURRENT_BINARY_DIR}/pip.log"
|
||||
-r "${PROJECT_SOURCE_DIR}/plugin-requirements.txt"
|
||||
WORKING_DIRECTORY ${PYTHON_ROOT}
|
||||
DEPENDS "${PROJECT_SOURCE_DIR}/plugin-requirements.txt"
|
||||
)
|
||||
add_custom_target("${MO2_TARGET}_libs"
|
||||
DEPENDS "${CMAKE_CURRENT_BINARY_DIR}/pip.log")
|
||||
add_dependencies(${MO2_TARGET} "${MO2_TARGET}_libs")
|
||||
endfunction()
|
||||
|
||||
function(mo2_configure_python_module MO2_TARGET)
|
||||
cmake_parse_arguments(MO2 "" "LIBDIR;RESDIR" "" ${ARGN})
|
||||
|
||||
@@ -38,66 +151,16 @@ function(mo2_configure_python_module MO2_TARGET)
|
||||
|
||||
# install requirements if there are any
|
||||
if(EXISTS "${PROJECT_SOURCE_DIR}/plugin-requirements.txt")
|
||||
add_custom_command(
|
||||
OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/pip.log"
|
||||
COMMAND ${PYTHON_ROOT}/PCbuild/amd64/python.exe
|
||||
-I
|
||||
-m pip
|
||||
install --force --upgrade --disable-pip-version-check
|
||||
--target="${lib_dir}"
|
||||
--log="${CMAKE_CURRENT_BINARY_DIR}/pip.log"
|
||||
-r "${PROJECT_SOURCE_DIR}/plugin-requirements.txt"
|
||||
WORKING_DIRECTORY ${PYTHON_ROOT}
|
||||
DEPENDS "${PROJECT_SOURCE_DIR}/plugin-requirements.txt"
|
||||
)
|
||||
add_custom_target("${MO2_TARGET}_libs"
|
||||
DEPENDS "${CMAKE_CURRENT_BINARY_DIR}/pip.log")
|
||||
|
||||
add_dependencies(${MO2_TARGET} "${MO2_TARGET}_libs")
|
||||
mo2_python_requirements(${MO2_TARGET} LIBDIR "${lib_dir}")
|
||||
endif()
|
||||
|
||||
# resources in the src directory
|
||||
file(GLOB_RECURSE resources
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/*.ui ${CMAKE_CURRENT_SOURCE_DIR}/*.qrc)
|
||||
# ui files
|
||||
file(GLOB_RECURSE ui_files ${CMAKE_CURRENT_SOURCE_DIR}/*.ui)
|
||||
mo2_python_uifiles(${MO2_TARGET} INPLACE FILES ${ui_files})
|
||||
|
||||
foreach(object ${resources})
|
||||
get_filename_component(ext "${object}" LAST_EXT)
|
||||
|
||||
if("${ext}" STREQUAL ".ui")
|
||||
# process .ui files and copy the resulting .py in data
|
||||
get_filename_component(name "${object}" NAME_WLE)
|
||||
get_filename_component(folder "${object}" DIRECTORY)
|
||||
set(output "${folder}/${name}.py")
|
||||
|
||||
execute_process(
|
||||
COMMAND ${PYTHON_ROOT}/PCbuild/amd64/python.exe
|
||||
-I
|
||||
-m PyQt5.uic.pyuic
|
||||
-o "${output}"
|
||||
"${object}"
|
||||
WORKING_DIRECTORY ${PYTHON_ROOT})
|
||||
|
||||
list(APPEND src_files "${output}")
|
||||
elseif("${ext}" STREQUAL ".qrc")
|
||||
# process .qrc files and copy the resulting .py in data
|
||||
get_filename_component(name "${object}" NAME_WLE)
|
||||
get_filename_component(folder "${object}" DIRECTORY)
|
||||
set(output "${folder}/${name}.py")
|
||||
|
||||
execute_process(
|
||||
COMMAND ${PYTHON_ROOT}/PCbuild/amd64/python.exe
|
||||
-I
|
||||
-m PyQt5.pyrcc_main
|
||||
-o "${output}"
|
||||
"${object}"
|
||||
WORKING_DIRECTORY ${PYTHON_ROOT})
|
||||
|
||||
list(APPEND src_files "${output}")
|
||||
endif()
|
||||
endforeach()
|
||||
|
||||
file(GLOB_RECURSE src_files
|
||||
RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} ${CMAKE_CURRENT_SOURCE_DIR}/*.py)
|
||||
# qrc file
|
||||
file(GLOB_RECURSE qrc_files ${CMAKE_CURRENT_SOURCE_DIR}/*.qrc)
|
||||
mo2_python_rcfiles(${MO2_TARGET} INPLACE FILES ${qrc_files})
|
||||
|
||||
set(install_dir "${MO2_INSTALL_PATH}/bin/plugins/${MO2_TARGET}")
|
||||
|
||||
@@ -126,16 +189,80 @@ function(mo2_configure_python_module MO2_TARGET)
|
||||
|
||||
endfunction()
|
||||
|
||||
function(mo2_configure_python_simple MO2_TARGET)
|
||||
|
||||
# this copies all the .py files that are directly in src/ into
|
||||
# ${install_dir}/
|
||||
#
|
||||
# any folder that contains at least one .py file (recursive) is copied in
|
||||
# bin/plugins/data
|
||||
#
|
||||
|
||||
# ui files
|
||||
file(GLOB ui_files ${CMAKE_CURRENT_SOURCE_DIR}/*.ui)
|
||||
mo2_python_uifiles(${MO2_TARGET} FILES ${ui_files})
|
||||
|
||||
# qrc file
|
||||
file(GLOB qrc_files ${CMAKE_CURRENT_SOURCE_DIR}/*.qrc)
|
||||
mo2_python_rcfiles(${MO2_TARGET} FILES ${qrc_files})
|
||||
|
||||
# .py files directly in the directory
|
||||
file(GLOB py_files ${CMAKE_CURRENT_SOURCE_DIR}/*.py)
|
||||
|
||||
# .json files directly in the directory
|
||||
file(GLOB json_files ${CMAKE_CURRENT_SOURCE_DIR}/*.json)
|
||||
|
||||
# subfolder with Python files
|
||||
set(data_dirs "")
|
||||
file(GLOB everything ${CMAKE_CURRENT_SOURCE_DIR}/*)
|
||||
foreach(object ${everything})
|
||||
if(IS_DIRECTORY "${object}")
|
||||
# only copy interesting directories
|
||||
file(GLOB_RECURSE dir_content "${dir}/*.py")
|
||||
if (dir_content)
|
||||
list(APPEND data_dirs "${object}")
|
||||
endif()
|
||||
endif()
|
||||
endforeach()
|
||||
|
||||
set(install_dir "${MO2_INSTALL_PATH}/bin/plugins")
|
||||
|
||||
# .py files directly in src/ go to plugins/
|
||||
install(FILES ${py_files} DESTINATION ${install_dir})
|
||||
|
||||
# folders with Python files go into plugins/data
|
||||
install(
|
||||
DIRECTORY ${data_dirs}
|
||||
DESTINATION "${install_dir}/data"
|
||||
FILES_MATCHING PATTERN "*.py")
|
||||
|
||||
# JSON file go in plugins/data
|
||||
install(FILES ${json_files} DESTINATION "${install_dir}/data")
|
||||
|
||||
# generated files go in plugins/data
|
||||
install(
|
||||
DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/"
|
||||
DESTINATION "${install_dir}/data"
|
||||
FILES_MATCHING
|
||||
PATTERN "*.py"
|
||||
PATTERN "CMakeFiles" EXCLUDE
|
||||
PATTERN "x64" EXCLUDE)
|
||||
|
||||
endfunction()
|
||||
|
||||
#! mo2_configure_python : configure a MO2 python target
|
||||
#
|
||||
# \param:MODULE indicates if this is a Python module plugin or a file plugin
|
||||
#
|
||||
function(mo2_configure_python MO2_TARGET)
|
||||
cmake_parse_arguments(MO2 "MODULE" "TRANSLATIONS;LIB;RES" "" ${ARGN})
|
||||
cmake_parse_arguments(MO2 "MODULE;SIMPLE" "TRANSLATIONS;LIB;RES" "" ${ARGN})
|
||||
|
||||
mo2_set_if_not_defined(MO2_TRANSLATIONS ON)
|
||||
|
||||
if ((${MO2_MODULE} AND ${MO2_SIMPLE}) OR (NOT(${MO2_MODULE}) AND NOT(${MO2_SIMPLE})))
|
||||
message(FATAL_ERROR "mo2_configure_python should be called with either SIMPLE or MODULE")
|
||||
endif()
|
||||
|
||||
if(${MO2_TRANSLATIONS})
|
||||
mo2_python_translations(${MO2_TARGET})
|
||||
endif()
|
||||
@@ -150,7 +277,7 @@ function(mo2_configure_python MO2_TARGET)
|
||||
if (${MO2_MODULE})
|
||||
mo2_configure_python_module(${MO2_TARGET} ${ARGN})
|
||||
else()
|
||||
mo2_configure_python_file(${MO2_TARGET} ${ARGN})
|
||||
mo2_configure_python_simple(${MO2_TARGET} ${ARGN})
|
||||
endif()
|
||||
|
||||
endfunction()
|
||||
|
||||
Reference in New Issue
Block a user