[graphics/krita] /: Python: add functions for converting paths to native form

L. E. Segovia null at kde.org
Sat Sep 11 08:16:11 BST 2021


Git commit 3c00ed4a13c277477fa2e4f6d3512b4de9fc8061 by L. E. Segovia, on behalf of Dmitry Kazakov.
Committed on 10/09/2021 at 12:58.
Pushed by dkazakov into branch 'master'.

Python: add functions for converting paths to native form

file(TO_NATIVE_PATH ...) doesn't work on MinGW, that is a known
bug from 2010, so we need to use something else. CMake 3.20.0
introduces cmake_path(CONVERT ...), but we still need to support
older versions.

There is also a problem of expanding lists like ${KRITA_PYTHONPATH}
into the environment variable form, when the list has more than one
element. The list should be expanded like "path1;path2;path3", but
CMake auto-expands that into a space-separated list, like
"path1 path2 path3", which breakes compilation (obviously).

The patch addresses these two problems by introducing two reusable
functions:

krita_to_native_path() --- converts a list of cmake-style paths into
a native form. It will also shortcut into cmake_path(CONVERT ...),
when newer cmake is found (and allowed by cmake_minimum_required).

krita_to_native_environment_path_list() --- takes a list of cmake-style
paths, converts the paths into native form and joins them with $<SEMICOLON>
or $<COLON> depending on the OS.

CCMAIL: kimageshop at kde.org

M  +5    -3    3rdparty/CMakeLists.txt
M  +12   -12   3rdparty/ext_pyqt/CMakeLists.txt
M  +8    -5    3rdparty/ext_sip/CMakeLists.txt
A  +60   -0    cmake/kde_macro/KritaToNativePath.cmake

https://invent.kde.org/graphics/krita/commit/3c00ed4a13c277477fa2e4f6d3512b4de9fc8061

diff --git a/3rdparty/CMakeLists.txt b/3rdparty/CMakeLists.txt
index 13d3aa048f..2a20b92352 100644
--- a/3rdparty/CMakeLists.txt
+++ b/3rdparty/CMakeLists.txt
@@ -32,6 +32,9 @@ endif (CMAKE_SOURCE_DIR STREQUAL CMAKE_BINARY_DIR)
 # Tools must be obtained to work with:
 include (ExternalProject)
 
+LIST (APPEND CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}/../cmake/kde_macro")
+include (KritaToNativePath)
+
 # allow specification of a directory with pre-downloaded
 # requirements
 if(NOT IS_DIRECTORY ${EXTERNALS_DOWNLOAD_DIR})
@@ -238,9 +241,8 @@ endfunction()
 
 function(DumpSitePackages PYTHONPATH)
     if (WIN32)
-        string(REPLACE "//" "/" ___krita_pythonpath "${${PYTHONPATH}}")
-        string(REPLACE "/" "\\" __krita_pythonpath "${___krita_pythonpath}")
-        string(TOLOWER "${__krita_pythonpath}" _krita_pythonpath)
+        krita_to_native_path("${${PYTHONPATH}}" _krita_pythonpath)
+        string(TOLOWER "${_krita_pythonpath}" _krita_pythonpath)
     else()
         set(_krita_pythonpath ${${PYTHONPATH}})
     endif()
diff --git a/3rdparty/ext_pyqt/CMakeLists.txt b/3rdparty/ext_pyqt/CMakeLists.txt
index 4bf88aedde..00fbb8ee22 100644
--- a/3rdparty/ext_pyqt/CMakeLists.txt
+++ b/3rdparty/ext_pyqt/CMakeLists.txt
@@ -121,11 +121,11 @@ elseif(WIN32)
         set(_compiler -c msvc)
         set(_make_command jom)
     endif()
-    string(REPLACE "//" "/" __pyqt_prefix ${PREFIX_ext_pyqt})
-    string(REPLACE "/" "\\" _pyqt_prefix ${__pyqt_prefix})
-    string(REPLACE "//" "/" ___krita_pythonpath "${KRITA_PYTHONPATH}")
-    string(REPLACE "/" "\\" __krita_pythonpath "${___krita_pythonpath}")
-    string(TOLOWER ${__krita_pythonpath} _krita_pythonpath)
+
+    krita_to_native_path("${PREFIX_ext_pyqt}" _pyqt_prefix)
+    krita_to_native_path("${KRITA_PYTHONPATH}" _krita_pythonpath)
+    string(TOLOWER ${_krita_pythonpath} _krita_pythonpath)
+    krita_to_native_environment_path_list("${_krita_pythonpath}" _krita_pythonpath)
 
     ExternalProject_Add(ext_pyqtbuilder
         DOWNLOAD_DIR ${EXTERNALS_DOWNLOAD_DIR}
@@ -137,14 +137,14 @@ elseif(WIN32)
         CONFIGURE_COMMAND ""
 
         BUILD_COMMAND ${CMAKE_COMMAND} -E env 
-            "PYTHONPATH=${_krita_pythonpath}"
+            PYTHONPATH=${_krita_pythonpath}
             ${Python_EXECUTABLE} <SOURCE_DIR>/setup.py build ${_compiler} -j ${SUBMAKE_JOBS}
 
         # HACK: Create lib\site-packages before installing
         # See e.g. https://access.redhat.com/errata/RHBA-2020:4437
         INSTALL_COMMAND ${CMAKE_COMMAND} -E make_directory ${_pyqt_prefix}\\lib\\site-packages
             COMMAND ${CMAKE_COMMAND} -E env 
-                "PYTHONPATH=${_krita_pythonpath}"
+                PYTHONPATH=${_krita_pythonpath}
                 ${Python_EXECUTABLE} <SOURCE_DIR>/setup.py install --skip-build --prefix=${_pyqt_prefix} --optimize=1 --install-scripts=${_pyqt_prefix}\\bin
 
         BUILD_IN_SOURCE 1
@@ -158,19 +158,19 @@ elseif(WIN32)
         URL_HASH SHA256=2a69597e0dd11caabe75fae133feca66387819fc9bc050f547e5551bce97e5be
 
         CONFIGURE_COMMAND ${CMAKE_COMMAND} -E env 
-            "PYTHONPATH=${_krita_pythonpath}"
+            PYTHONPATH=${_krita_pythonpath}
             ${PREFIX_ext_pyqt}/bin/sip-build --confirm-license --no-designer-plugin --no-qml-plugin --no-dbus-python --no-tools --no-make --target-dir ${PREFIX_ext_pyqt}\\lib\\site-packages --jobs ${SUBMAKE_JOBS}
 
         BUILD_COMMAND ${CMAKE_COMMAND} -E chdir <SOURCE_DIR>/build
             ${CMAKE_COMMAND} -E env 
-            "PYTHONPATH=${_krita_pythonpath}"
+            PYTHONPATH=${_krita_pythonpath}
             ${_make_command} -j${SUBMAKE_JOBS}
 
         # force single-threaded installation to avoid
         # race conditions in post-install .py script
         INSTALL_COMMAND ${CMAKE_COMMAND} -E chdir <SOURCE_DIR>/build
             ${CMAKE_COMMAND} -E env 
-            "PYTHONPATH=${_krita_pythonpath}"
+            PYTHONPATH=${_krita_pythonpath}
             ${_make_command} -j1 install
 
         BUILD_IN_SOURCE 1
@@ -190,7 +190,7 @@ elseif(WIN32)
         CONFIGURE_COMMAND ""
 
         BUILD_COMMAND ${CMAKE_COMMAND} -E env 
-            "PYTHONPATH=${_krita_pythonpath}"
+            PYTHONPATH=${_krita_pythonpath}
             ${Python_EXECUTABLE} <SOURCE_DIR>/setup.py build ${_compiler} -j ${SUBMAKE_JOBS}
 
         # Here we need a flat-egg installation, otherwise the egg is deployed
@@ -198,7 +198,7 @@ elseif(WIN32)
         # So we set as root our 3rdparty layout, and a sufficiently dud prefix.
         # See https://github.com/msys2/MINGW-packages/blob/8a4066591cf1e023971526c55bf2d5fe0c7aa84a/mingw-w64-pyqt5-sip/PKGBUILD
         INSTALL_COMMAND ${CMAKE_COMMAND} -E env 
-            "PYTHONPATH=${_krita_pythonpath}"
+            PYTHONPATH=${_krita_pythonpath}
             ${Python_EXECUTABLE} <SOURCE_DIR>/setup.py install --skip-build --prefix "." --root=${_pyqt_prefix} --optimize=1 --install-scripts=${_pyqt_prefix}\\bin
 
         BUILD_IN_SOURCE 1
diff --git a/3rdparty/ext_sip/CMakeLists.txt b/3rdparty/ext_sip/CMakeLists.txt
index f569e13e09..52ee2a580d 100644
--- a/3rdparty/ext_sip/CMakeLists.txt
+++ b/3rdparty/ext_sip/CMakeLists.txt
@@ -63,8 +63,11 @@ elseif (WIN32)
         set(_compiler -c msvc)
     endif()
 
-    string(REPLACE "/" "\\" _sip_prefix ${PREFIX_ext_sip})
-    string(REPLACE "/" "\\" _krita_pythonpath ${KRITA_PYTHONPATH})
+    krita_to_native_path("${PREFIX_ext_sip}" _sip_prefix)
+    krita_to_native_path("${KRITA_PYTHONPATH}" _krita_pythonpath)
+    string(TOLOWER ${_krita_pythonpath} _krita_pythonpath)
+    krita_to_native_environment_path_list("${_krita_pythonpath}" _krita_pythonpath)
+
     ExternalProject_Add( ext_sip
         DOWNLOAD_DIR ${EXTERNALS_DOWNLOAD_DIR}
         URL https://pypi.python.org/packages/source/s/sip/sip-6.1.0.tar.gz
@@ -75,15 +78,15 @@ elseif (WIN32)
         CONFIGURE_COMMAND ""
 
         BUILD_COMMAND ${CMAKE_COMMAND} -E env 
-            "PYTHONPATH=${_krita_pythonpath}"
+            PYTHONPATH=${_krita_pythonpath}
             ${Python_EXECUTABLE} <SOURCE_DIR>/setup.py build -j ${SUBMAKE_JOBS} ${_compiler}
 
         # HACK: Create lib\site-packages before installing
         # See e.g. https://access.redhat.com/errata/RHBA-2020:4437
         INSTALL_COMMAND ${CMAKE_COMMAND} -E make_directory ${_sip_prefix}\\lib\\site-packages
             COMMAND ${CMAKE_COMMAND} -E env 
-                "PYTHONPATH=${_krita_pythonpath}"
-                ${Python_EXECUTABLE} <SOURCE_DIR>/setup.py install --skip-build --prefix ${_sip_prefix} --optimize=1 --install-scripts=${_sip_prefix}\\bin
+                PYTHONPATH=${_krita_pythonpath}
+                ${Python_EXECUTABLE} <SOURCE_DIR>/setup.py install --skip-build --prefix=${_sip_prefix} --optimize=1 --install-scripts=${_sip_prefix}\\bin
 
         BUILD_IN_SOURCE 1
 
diff --git a/cmake/kde_macro/KritaToNativePath.cmake b/cmake/kde_macro/KritaToNativePath.cmake
new file mode 100644
index 0000000000..6d5a286930
--- /dev/null
+++ b/cmake/kde_macro/KritaToNativePath.cmake
@@ -0,0 +1,60 @@
+# SPDX-FileCopyrightText: 2021 Dmitry Kazakov <dimula73 at gmail.com>
+#
+# SPDX-License-Identifier: BSD-3-Clause
+#
+
+# krita_to_native_path(<path_list> <output_var>)
+#
+# Converts path separators of the list <path_list> into the native format
+# and stores the result in <output_var>
+#
+# Usage:
+#
+# krita_to_native_path("${KRITA_PYTHONPATH}" _krita_pythonpath)
+
+function (krita_to_native_path path_list output_var)
+    if(WIN32)
+        if(${CMAKE_VERSION} VERSION_LESS "3.20.0")
+            foreach (_path ${path_list})
+                string(REPLACE "//" "/" _path "${_path}")
+                string(REPLACE "/" "\\" _path "${_path}")
+                list(APPEND _output_var "${_path}")
+            endforeach()
+
+            set (${output_var} ${_output_var} PARENT_SCOPE)
+        else()
+            cmake_path(CONVERT "${path_list}" TO_NATIVE_PATH_LIST _output_var NORMALIZE)
+            set (${output_var} ${_output_var} PARENT_SCOPE)
+        endif()
+    else()
+        set (${output_var} ${path_list} PARENT_SCOPE)
+    endif()
+endfunction()
+
+
+# krita_to_native_environment_path_list(<path_list> <output_var>)
+#
+# Converts the list of paths in <path_list> into the native format
+# and then joins them into a string suitable to be assigned to
+# PATH (or PATH-like) variable. On Windows the string is joined
+# using ';' separator. On other platforms with ':' separator.
+#
+# To avoid conversion back to a list object, the function adds
+# separators in a form of generators: $<SEMICOLON> or $<COLON>
+#
+# Usage:
+#
+# krita_to_native_environment_path_list("${KRITA_PYTHONPATH}" _krita_pythonpath)
+
+
+function (krita_to_native_environment_path_list path_list output_var)
+    krita_to_native_path("${path_list}" _output_var)
+
+if(WIN32)
+    list(JOIN _output_var $<SEMICOLON> _output_var)
+else()
+    list(JOIN _output_var $<COLON> _output_var)
+endif()
+
+    set (${output_var} ${_output_var} PARENT_SCOPE)
+endfunction()



More information about the kimageshop mailing list