Browse Source

improve python fetch (#1783)

* improve python fetch

* fix python on windows arm64
Hoildkv 2 years ago
parent
commit
3171ceca48
2 changed files with 89 additions and 22 deletions
  1. 87 0
      packages/p/python/fetch.lua
  2. 2 22
      packages/p/python/xmake.lua

+ 87 - 0
packages/p/python/fetch.lua

@@ -0,0 +1,87 @@
+import("lib.detect.find_path")
+import("lib.detect.find_library")
+import("lib.detect.find_program")
+import("lib.detect.find_programver")
+
+function _find_binary(package, opt)
+    local result = package:find_tool("python3", opt)
+    if not result then
+        result = package:find_tool("python", opt)
+    end
+    if result then
+        -- check if pip, setuptools and wheel are installed
+        local ok = try { function () 
+            os.vrunv(result.program, {"-c", "import pip"})
+            os.vrunv(result.program, {"-c", "import setuptools"})
+            os.vrunv(result.program, {"-c", "import wheel"})
+            return true
+        end}
+        if not ok then
+            return false
+        end
+    end
+end
+
+function _find_library(package, opt)
+
+    -- init search options
+    opt = opt or {}
+    opt.paths = opt.paths or {}
+    table.insert(opt.paths, "$(env PATH)")
+    table.insert(opt.paths, "$(env CONDA_PREFIX)")
+    
+    -- find python
+    local program = find_program("python3", opt)
+    if not program then
+        program = find_program("python", opt)
+    end
+    local version = nil
+    if program then
+        opt.command = function ()
+            local outs, errs = os.iorunv(program, {"--version"})
+            return ((outs or "") .. (errs or "")):trim()
+        end
+        version = find_programver(program, opt)
+    end
+    if not program or not version then
+        return false
+    end
+
+    -- find library and header
+    local exepath = path.directory(program)
+    local link = nil
+    local libpath = nil
+    local includepath = nil
+    if package:is_plat("windows") then
+        link = format("python" .. table.concat(table.slice(version:split("%."), 1, 2), ""))
+        libpath = find_library(link, {exepath}, {suffixes = {"libs"}})
+        linkdirs = {}
+        includepath = find_path("Python.h", {exepath}, {suffixes = {"include"}})
+    else
+        local pyver = table.concat(table.slice(version:split("%."), 1, 2), ".")
+        link = format("python" .. pyver)
+        libpath = find_library(link, {path.directory(exepath)}, {suffixes = {"lib", "lib64", "lib/x86_64-linux-gnu"}})
+        includepath = find_path("Python.h", {path.directory(exepath)}, {suffixes = {"include/python" .. pyver}})
+    end
+
+    -- return
+    if libpath and includepath then
+        local result = {
+            version = version,
+            link = libpath.link,
+            linkdirs = {libpath.linkdir},
+            includedirs = {includepath}
+        }
+        return result
+    end
+end
+
+function main(package, opt)
+    if opt.system then
+        if package:is_binary() then
+            return _find_binary(package, opt)
+        else
+            return _find_library(package, opt)
+        end
+    end
+end

+ 2 - 22
packages/p/python/xmake.lua

@@ -38,7 +38,7 @@ package("python")
         add_versions("3.10.6", "848cb06a5caa85da5c45bd7a9221bb821e33fc2bdcba088c127c58fad44e6343")
         add_versions("3.10.6", "848cb06a5caa85da5c45bd7a9221bb821e33fc2bdcba088c127c58fad44e6343")
     end
     end
 
 
-    if not is_plat(os.host()) then
+    if not is_plat(os.host()) or not is_arch(os.arch()) then
         set_kind("binary")
         set_kind("binary")
     end
     end
 
 
@@ -86,27 +86,7 @@ package("python")
         package:addenv("PATH", "Scripts")
         package:addenv("PATH", "Scripts")
     end)
     end)
 
 
-    on_fetch(function (package, opt)
-        if opt.system and package:is_binary() then
-            local result = package:find_tool("python3", opt)
-            if not result then
-                result = package:find_tool("python", opt)
-            end
-            if result then
-                -- check if pip, setuptools and wheel are installed
-                local ok = try { function () 
-                    os.vrunv(result.program, {"-c", "import pip"})
-                    os.vrunv(result.program, {"-c", "import setuptools"})
-                    os.vrunv(result.program, {"-c", "import wheel"})
-                    return true
-                end}
-                if not ok then
-                    return false
-                end
-            end
-            return result
-        end
-    end)
+    on_fetch("fetch")
 
 
     on_install("@windows|x86", "@windows|x64", "@msys", "@cygwin", function (package)
     on_install("@windows|x86", "@windows|x64", "@msys", "@cygwin", function (package)
         if package:version():ge("3.0") then
         if package:version():ge("3.0") then