fetch.lua 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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. libpath = find_library(link, {exepath}, {suffixes = {"libs"}})
  53. linkdirs = {}
  54. includepath = find_path("Python.h", {exepath}, {suffixes = {"include"}})
  55. else
  56. local pyver = table.concat(table.slice(version:split("%."), 1, 2), ".")
  57. link = format("python" .. pyver)
  58. libpath = find_library(link, {path.directory(exepath)}, {suffixes = {"lib", "lib64", "lib/x86_64-linux-gnu"}})
  59. includepath = find_path("Python.h", {path.directory(exepath)}, {suffixes = {"include/python" .. pyver}})
  60. end
  61. if libpath and includepath then
  62. local result = {
  63. version = version,
  64. links = libpath.link,
  65. linkdirs = libpath.linkdir,
  66. includedirs = includepath
  67. }
  68. return result
  69. end
  70. end
  71. function main(package, opt)
  72. if opt.system then
  73. if package:is_binary() then
  74. return _find_binary(package, opt)
  75. else
  76. return _find_library(package, opt)
  77. end
  78. end
  79. end