Explorar el Código

improve python (#307)

* fix ssl for python

* improve python
ruki hace 4 años
padre
commit
38084e30c9
Se han modificado 3 ficheros con 94 adiciones y 21 borrados
  1. 2 2
      .github/workflows/cross_musl.yml
  2. 48 16
      packages/p/python/xmake.lua
  3. 44 3
      packages/p/python2/xmake.lua

+ 2 - 2
.github/workflows/cross_musl.yml

@@ -21,8 +21,8 @@ jobs:
 
       - name: Installation
         run: |
-          wget https://musl.cc/${{ matrix.cross }}-cross.tgz
-          tar -xvf ${{ matrix.cross }}-cross.tgz
+          wget https://github.com/xmake-mirror/musl.cc/releases/download/20210202/${{ matrix.cross }}-cross.linux.tgz
+          tar -xvf ${{ matrix.cross }}-cross.linux.tgz
 
       - name: Tests
         run: |

+ 48 - 16
packages/p/python/xmake.lua

@@ -40,6 +40,7 @@ package("python")
     end
 
     if is_host("linux") then
+        add_deps("libffi", "zlib", {host = true})
         add_syslinks("util", "pthread", "dl")
     end
 
@@ -57,15 +58,6 @@ package("python")
 
     on_load("@macosx", "@linux", function (package)
 
-        -- check system libs
-        if is_host("linux") then
-            for link, lib in pairs({z = "zlib", ffi = "libffi"}) do
-                local result = find_package("pkg_config::" .. lib, {plat = os.host(), arch = os.arch()}) or
-                               find_package("system::" .. link, {plat = os.host(), arch = os.arch()})
-                assert(result, ("%s-dev not found, please use your system package manager to install it."):format(lib))
-            end
-        end
-
         -- set includedirs
         local version = package:version()
         local pyver = ("python%d.%d"):format(version:major(), version:minor())
@@ -105,19 +97,32 @@ package("python")
         table.insert(configs, "--datarootdir=" .. package:installdir("share"))
 
         -- add openssl libs path for detecting
-        local openssl_dir = package:dep("openssl"):installdir()
-        if package:version():ge("3.0") then
-            table.insert(configs, "--with-openssl=" .. openssl_dir)
-        else
-            io.gsub("setup.py", "/usr/local/ssl", openssl_dir)
+        local openssl_dir
+        local openssl = package:dep("openssl"):fetch()
+        if openssl then
+            for _, linkdir in ipairs(openssl.linkdirs) do
+                if path.filename(linkdir) == "lib" then
+                    openssl_dir = path.directory(linkdir)
+                    if openssl_dir then
+                        break
+                    end
+                end
+            end
+        end
+        if openssl_dir then
+            if package:version():ge("3.0") then
+                table.insert(configs, "--with-openssl=" .. openssl_dir)
+            else
+                io.gsub("setup.py", "/usr/local/ssl", openssl_dir)
+            end
         end
 
         -- allow python modules to use ctypes.find_library to find xmake's stuff
-        if is_host("macosx") then
+        if package:is_plat("macosx") then
             io.gsub("Lib/ctypes/macholib/dyld.py", "DEFAULT_LIBRARY_FALLBACK = %[", format("DEFAULT_LIBRARY_FALLBACK = [ '%s/lib',", package:installdir()))
         end
 
-        -- add flags
+        -- add flags for macOS
         local cflags = {}
         local ldflags = {}
         if package:is_plat("macosx") then
@@ -182,6 +187,33 @@ package("python")
             table.insert(configs, "LDFLAGS=" .. table.concat(ldflags, " "))
         end
 
+        -- add zlib to fix `No module named 'zlib'`
+        local linkdirs = {}
+        local includedirs = {}
+        if package:is_plat("linux") then
+            local zlib = package:dep("zlib"):fetch({external = false})
+            if zlib then
+                table.join2(linkdirs, zlib.linkdirs)
+                table.join2(includedirs, zlib.includedirs)
+            end
+            -- add libffi to fix `No module named '_ctypes'`
+            local libffi = package:dep("libffi"):fetch({external = false})
+            if libffi then
+                table.join2(linkdirs, libffi.linkdirs)
+                table.join2(includedirs, libffi.includedirs)
+            end
+        end
+        if #linkdirs > 0 and #includedirs > 0 then
+            io.replace("setup.py", "    def detect_modules(self):", format([[    def detect_modules(self):
+        linkdirs = ['%s']
+        includedirs = ['%s']
+        for includedir in includedirs:
+            add_dir_to_list(self.compiler.include_dirs, includedir)
+        for linkdir in linkdirs:
+            add_dir_to_list(self.compiler.library_dirs, linkdir)
+]], table.concat(linkdirs, "', '"), table.concat(includedirs, "', '")), {plain = true})
+        end
+
         -- unset these so that installing pip and setuptools puts them where we want
         -- and not into some other Python the user has installed.
         import("package.tools.autoconf").configure(package, configs, {envs = {PYTHONHOME = "", PYTHONPATH = ""}})

+ 44 - 3
packages/p/python2/xmake.lua

@@ -33,6 +33,7 @@ package("python2")
     end
 
     if is_host("linux") then
+        add_deps("libffi", "zlib", {host = true})
         add_syslinks("util", "pthread", "dl")
     end
 
@@ -79,11 +80,24 @@ package("python2")
         table.insert(configs, "--datarootdir=" .. package:installdir("share"))
 
         -- add openssl libs path for detecting
-        local openssl_dir = package:dep("openssl"):installdir()
-        io.gsub("setup.py", "/usr/local/ssl", openssl_dir)
+        local openssl_dir
+        local openssl = package:dep("openssl"):fetch()
+        if openssl then
+            for _, linkdir in ipairs(openssl.linkdirs) do
+                if path.filename(linkdir) == "lib" then
+                    openssl_dir = path.directory(linkdir)
+                    if openssl_dir then
+                        break
+                    end
+                end
+            end
+        end
+        if openssl_dir then
+            io.gsub("setup.py", "/usr/local/ssl", openssl_dir)
+        end
 
         -- allow python modules to use ctypes.find_library to find xmake's stuff
-        if is_host("macosx") then
+        if package:is_plat("macosx") then
             io.gsub("Lib/ctypes/macholib/dyld.py", "DEFAULT_LIBRARY_FALLBACK = %[", format("DEFAULT_LIBRARY_FALLBACK = [ '%s/lib',", package:installdir()))
         end
 
@@ -152,6 +166,33 @@ package("python2")
             table.insert(configs, "LDFLAGS=" .. table.concat(ldflags, " "))
         end
 
+        -- add zlib to fix `No module named 'zlib'`
+        local linkdirs = {}
+        local includedirs = {}
+        if package:is_plat("linux") then
+            local zlib = package:dep("zlib"):fetch({external = false})
+            if zlib then
+                table.join2(linkdirs, zlib.linkdirs)
+                table.join2(includedirs, zlib.includedirs)
+            end
+            -- add libffi to fix `No module named '_ctypes'`
+            local libffi = package:dep("libffi"):fetch({external = false})
+            if libffi then
+                table.join2(linkdirs, libffi.linkdirs)
+                table.join2(includedirs, libffi.includedirs)
+            end
+        end
+        if #linkdirs > 0 and #includedirs > 0 then
+            io.replace("setup.py", "    def detect_modules(self):", format([[    def detect_modules(self):
+        linkdirs = ['%s']
+        includedirs = ['%s']
+        for includedir in includedirs:
+            add_dir_to_list(self.compiler.include_dirs, includedir)
+        for linkdir in linkdirs:
+            add_dir_to_list(self.compiler.library_dirs, linkdir)
+]], table.concat(linkdirs, "', '"), table.concat(includedirs, "', '")), {plain = true})
+        end
+
         -- unset these so that installing pip and setuptools puts them where we want
         -- and not into some other Python the user has installed.
         import("package.tools.autoconf").configure(package, configs, {envs = {PYTHONHOME = "", PYTHONPATH = ""}})