2
0

fetch.lua 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. import("lib.detect.find_path")
  2. import("lib.detect.find_library")
  3. import("lib.detect.find_program")
  4. import("lib.detect.find_programver")
  5. function _find_binary(package, opt)
  6. local result = package:find_tool("python3", opt)
  7. if not result then
  8. result = package:find_tool("python", opt)
  9. end
  10. if result then
  11. -- check if pip, setuptools and wheel are installed
  12. local ok = try { function()
  13. os.vrunv(result.program, { "-c", "import pip" })
  14. os.vrunv(result.program, { "-c", "import setuptools" })
  15. os.vrunv(result.program, { "-c", "import wheel" })
  16. return true
  17. end }
  18. if not ok then
  19. return false
  20. end
  21. end
  22. end
  23. function _find_library(package, opt)
  24. -- init search options
  25. opt = opt or {}
  26. opt.paths = opt.paths or {}
  27. table.insert(opt.paths, "$(env PATH)")
  28. table.insert(opt.paths, "$(env CONDA_PREFIX)")
  29. -- find python
  30. local program = find_program("python3", opt)
  31. if not program then
  32. program = find_program("python", opt)
  33. end
  34. local version = nil
  35. if program then
  36. opt.command = function()
  37. local outs, errs = os.iorunv(program, { "--version" })
  38. return ((outs or "") .. (errs or "")):trim()
  39. end
  40. version = find_programver(program, opt)
  41. end
  42. if not program or not version then
  43. return false
  44. end
  45. -- find library and header
  46. local exepath = path.directory(program)
  47. local link = nil
  48. local libpath = nil
  49. local includepath = nil
  50. if package:is_plat("windows") then
  51. link = format("python" .. table.concat(table.slice(version:split("%."), 1, 2), ""))
  52. local out = try { function () return os.iorunv(program, { "-c", "import sys; print(sys.base_prefix, end='')" }) end }
  53. libpath = find_library(link, { exepath, out }, { suffixes = { "libs" } })
  54. linkdirs = {}
  55. includepath = find_path("Python.h", { exepath, out }, { suffixes = { "include" } })
  56. else
  57. local pyver = table.concat(table.slice(version:split("%."), 1, 2), ".")
  58. link = format("python" .. pyver)
  59. local out = try { function () return os.iorunv(program, { "-c", "import sys; print(sys.base_prefix, end='')" }) end }
  60. libpath = find_library(link, { path.directory(exepath), out }, { suffixes = { "lib", "lib64", "lib/x86_64-linux-gnu" } })
  61. includepath = find_path("Python.h", { path.directory(exepath), out }, { suffixes = { "include/python" .. pyver } })
  62. end
  63. if not includepath then
  64. return
  65. end
  66. local result = {
  67. version = version,
  68. includedirs = includepath
  69. }
  70. if package:config("headeronly") then
  71. return result
  72. end
  73. if libpath then
  74. result.links = libpath.link
  75. result.linkdirs = libpath.linkdir
  76. return result
  77. end
  78. end
  79. function main(package, opt)
  80. if opt.system then
  81. if package:is_binary() then
  82. return _find_binary(package, opt)
  83. else
  84. return _find_library(package, opt)
  85. end
  86. end
  87. end