xmake.lua 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. package("python")
  2. set_homepage("https://www.python.org/")
  3. set_description("The python programming language.")
  4. if is_host("windows") then
  5. if is_arch("x86", "i386") or os.arch() == "x86" then
  6. add_urls("https://github.com/xmake-mirror/python-windows/releases/download/$(version)/python-$(version).win32.zip")
  7. add_versions("2.7.18", "95e21c87c9f38fa8068e014fc3683c3bc2c827f64875e620b9ecd3c75976a79c")
  8. add_versions("3.7.9", "55c8a408a11e598964f5d581589cf7f8c622e3cad048dce331ee5a61e5a6f57f")
  9. add_versions("3.8.10", "f520d2880578df076e3df53bf9e147b81b5328db02d8d873670a651fa076be50")
  10. add_versions("3.9.5", "ce0bfe8ced874d8d74a6cf6a98f13f5afee27cffbaf2d1ee0f09d3a027fab299")
  11. add_versions("3.9.6", "2918246384dfb233bd8f8c2bcf6aa3688e6834e84ab204f7c962147c468f8d12")
  12. add_versions("3.9.10", "e2c8e6b792748289ac27ef8462478022c96e24c99c4c3eb97d3afe510d9db646")
  13. add_versions("3.9.13", "c60ec0da0adf3a31623073d4fa085da62747085a9f23f4348fe43dfe94ea447b")
  14. else
  15. add_urls("https://github.com/xmake-mirror/python-windows/releases/download/$(version)/python-$(version).win64.zip")
  16. add_versions("2.7.18", "6680835ed5b818e2c041c7033bea47ace17f6f3b73b0d6efb6ded8598a266754")
  17. add_versions("3.7.9", "d0d879c934b463d46161f933db53a676790d72f24e92143f629ee5629ae286bc")
  18. add_versions("3.8.10", "acf35048274404dd415e190bf5b928fae3b03d8bb5dfbfa504f9a183361468bd")
  19. add_versions("3.9.5", "3265059edac21bf4c46fac13553a5d78417e7aa209eceeffd0250aa1dd8d6fdf")
  20. add_versions("3.9.6", "57ccd1b1b5fbc62882bd2a6f47df6e830ba39af741acf0a1d2f161eef4e87f2e")
  21. add_versions("3.9.10", "4cee67e2a529fe363e34f0da57f8e5c3fc036913dc838b17389b2319ead0927e")
  22. add_versions("3.9.13", "6774fdd872fc55b028becc81b7d79bdcb96c5e0eb1483cfcd38224b921c94d7d")
  23. end
  24. else
  25. set_urls("https://www.python.org/ftp/python/$(version)/Python-$(version).tgz",
  26. "https://github.com/xmake-mirror/cpython/releases/download/v$(version)/Python-$(version).tgz")
  27. add_versions("2.7.18", "da3080e3b488f648a3d7a4560ddee895284c3380b11d6de75edb986526b9a814")
  28. add_versions("3.7.9", "39b018bc7d8a165e59aa827d9ae45c45901739b0bbb13721e4f973f3521c166a")
  29. add_versions("3.8.10", "b37ac74d2cbad2590e7cd0dd2b3826c29afe89a734090a87bf8c03c45066cb65")
  30. add_versions("3.9.5", "e0fbd5b6e1ee242524430dee3c91baf4cbbaba4a72dd1674b90fda87b713c7ab")
  31. add_versions("3.9.6", "d0a35182e19e416fc8eae25a3dcd4d02d4997333e4ad1f2eee6010aadc3fe866")
  32. add_versions("3.9.10", "1aa9c0702edbae8f6a2c95f70a49da8420aaa76b7889d3419c186bfc8c0e571e")
  33. add_versions("3.9.13", "829b0d26072a44689a6b0810f5b4a3933ee2a0b8a4bfc99d7c5893ffd4f97c44")
  34. end
  35. if not is_plat(os.host()) then
  36. set_kind("binary")
  37. end
  38. if is_host("macosx", "linux") then
  39. add_deps("openssl", "ca-certificates", {host = true})
  40. end
  41. if is_host("linux") then
  42. add_deps("libffi", "zlib", {host = true})
  43. add_syslinks("util", "pthread", "dl")
  44. end
  45. on_load("@windows", "@msys", "@cygwin", function (package)
  46. -- set includedirs
  47. package:add("includedirs", "include")
  48. -- set python environments
  49. local PYTHONPATH = package:installdir("Lib", "site-packages")
  50. package:addenv("PYTHONPATH", PYTHONPATH)
  51. package:addenv("PATH", "bin")
  52. package:addenv("PATH", "Scripts")
  53. end)
  54. on_load("@macosx", "@linux", function (package)
  55. -- set includedirs
  56. local version = package:version()
  57. local pyver = ("python%d.%d"):format(version:major(), version:minor())
  58. if version:ge("3.0") and version:le("3.8") then
  59. package:add("includedirs", path.join("include", pyver .. "m"))
  60. else
  61. package:add("includedirs", path.join("include", pyver))
  62. end
  63. -- set python environments
  64. local PYTHONPATH = package:installdir("lib", pyver, "site-packages")
  65. package:addenv("PYTHONPATH", PYTHONPATH)
  66. package:addenv("PATH", "bin")
  67. package:addenv("PATH", "Scripts")
  68. end)
  69. on_fetch(function (package, opt)
  70. if opt.system and package:is_binary() then
  71. local result = package:find_tool("python3", opt)
  72. if not result then
  73. result = package:find_tool("python", opt)
  74. end
  75. return result
  76. end
  77. end)
  78. on_install("@windows", "@msys", "@cygwin", function (package)
  79. if package:version():ge("3.0") then
  80. os.cp("python.exe", path.join(package:installdir("bin"), "python3.exe"))
  81. else
  82. os.cp("python.exe", path.join(package:installdir("bin"), "python2.exe"))
  83. end
  84. os.cp("*.exe", package:installdir("bin"))
  85. os.cp("*.dll", package:installdir("bin"))
  86. os.cp("Lib", package:installdir())
  87. os.cp("libs/*", package:installdir("lib"))
  88. os.cp("*", package:installdir())
  89. local python = path.join(package:installdir("bin"), "python.exe")
  90. if package:version():eq("3.9.10") then
  91. -- https://github.com/xmake-io/xmake-repo/issues/1013
  92. os.vrunv(python, {"-m", "pip", "install", "--upgrade", "--force-reinstall", "pip==21.3.1"})
  93. else
  94. os.vrunv(python, {"-m", "pip", "install", "--upgrade", "--force-reinstall", "pip"})
  95. end
  96. os.vrunv(python, {"-m", "pip", "install", "wheel"})
  97. end)
  98. on_install("@macosx", "@linux", function (package)
  99. -- init configs
  100. local configs = {"--enable-ipv6", "--with-ensurepip", "--enable-optimizations"}
  101. table.insert(configs, "--datadir=" .. package:installdir("share"))
  102. table.insert(configs, "--datarootdir=" .. package:installdir("share"))
  103. table.insert(configs, "--enable-shared=" .. (package:config("shared") and "yes" or "no"))
  104. -- add openssl libs path for detecting
  105. local openssl_dir
  106. local openssl = package:dep("openssl"):fetch()
  107. if openssl then
  108. for _, linkdir in ipairs(openssl.linkdirs) do
  109. if path.filename(linkdir) == "lib" then
  110. openssl_dir = path.directory(linkdir)
  111. if openssl_dir then
  112. break
  113. end
  114. end
  115. end
  116. end
  117. if openssl_dir then
  118. if package:version():ge("3.0") then
  119. table.insert(configs, "--with-openssl=" .. openssl_dir)
  120. else
  121. io.gsub("setup.py", "/usr/local/ssl", openssl_dir)
  122. end
  123. end
  124. -- allow python modules to use ctypes.find_library to find xmake's stuff
  125. if package:is_plat("macosx") then
  126. io.gsub("Lib/ctypes/macholib/dyld.py", "DEFAULT_LIBRARY_FALLBACK = %[", format("DEFAULT_LIBRARY_FALLBACK = [ '%s/lib',", package:installdir()))
  127. end
  128. -- add flags for macOS
  129. local cppflags = {}
  130. local ldflags = {}
  131. if package:is_plat("macosx") then
  132. -- get xcode information
  133. import("core.tool.toolchain")
  134. local xcode_dir
  135. local xcode_sdkver
  136. local target_minver
  137. local xcode = toolchain.load("xcode", {plat = package:plat(), arch = package:arch()})
  138. if xcode and xcode.config and xcode:check() then
  139. xcode_dir = xcode:config("xcode")
  140. xcode_sdkver = xcode:config("xcode_sdkver")
  141. target_minver = xcode:config("target_minver")
  142. end
  143. xcode_dir = xcode_dir or get_config("xcode")
  144. xcode_sdkver = xcode_sdkver or get_config("xcode_sdkver")
  145. target_minver = target_minver or get_config("target_minver")
  146. -- TODO will be deprecated after xmake v2.5.1
  147. xcode_sdkver = xcode_sdkver or get_config("xcode_sdkver_macosx")
  148. if not xcode_dir or not xcode_sdkver then
  149. -- maybe on cross platform, we need find xcode envs manually
  150. local xcode = import("detect.sdks.find_xcode")(nil, {force = true, plat = package:plat(), arch = package:arch()})
  151. if xcode then
  152. xcode_dir = xcode.sdkdir
  153. xcode_sdkver = xcode.sdkver
  154. end
  155. end
  156. -- TODO will be deprecated after xmake v2.5.1
  157. target_minver = target_minver or get_config("target_minver_macosx")
  158. if not target_minver then
  159. local macos_ver = macos.version()
  160. if macos_ver then
  161. target_minver = macos_ver:major() .. "." .. macos_ver:minor()
  162. end
  163. end
  164. if xcode_dir and xcode_sdkver then
  165. -- help Python's build system (setuptools/pip) to build things on SDK-based systems
  166. -- the setup.py looks at "-isysroot" to get the sysroot (and not at --sysroot)
  167. local xcode_sdkdir = xcode_dir .. "/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX" .. xcode_sdkver .. ".sdk"
  168. table.insert(cppflags, "-isysroot " .. xcode_sdkdir)
  169. table.insert(cppflags, "-I" .. path.join(xcode_sdkdir, "/usr/include"))
  170. table.insert(ldflags, "-isysroot " .. xcode_sdkdir)
  171. -- for the Xlib.h, Python needs this header dir with the system Tk
  172. -- yep, this needs the absolute path where zlib needed a path relative to the SDK.
  173. table.insert(cppflags, "-I" .. path.join(xcode_sdkdir, "/System/Library/Frameworks/Tk.framework/Versions/8.5/Headers"))
  174. end
  175. -- avoid linking to libgcc https://mail.python.org/pipermail/python-dev/2012-February/116205.html
  176. if target_minver then
  177. table.insert(configs, "MACOSX_DEPLOYMENT_TARGET=" .. target_minver)
  178. end
  179. end
  180. -- add pic
  181. if package:is_plat("linux") and package:config("pic") ~= false then
  182. table.insert(cppflags, "-fPIC")
  183. end
  184. -- add external path for zlib and libffi
  185. for _, libname in ipairs({"zlib", "libffi"}) do
  186. local lib = package:dep(libname)
  187. if lib and not lib:is_system() then
  188. local fetchinfo = lib:fetch({external = false})
  189. if fetchinfo then
  190. for _, includedir in ipairs(fetchinfo.includedirs or fetchinfo.sysincludedirs) do
  191. table.insert(cppflags, "-I" .. includedir)
  192. end
  193. for _, linkdir in ipairs(fetchinfo.linkdirs) do
  194. table.insert(ldflags, "-L" .. linkdir)
  195. end
  196. end
  197. end
  198. end
  199. if #cppflags > 0 then
  200. table.insert(configs, "CPPFLAGS=" .. table.concat(cppflags, " "))
  201. end
  202. if #ldflags > 0 then
  203. table.insert(configs, "LDFLAGS=" .. table.concat(ldflags, " "))
  204. end
  205. -- unset these so that installing pip and setuptools puts them where we want
  206. -- and not into some other Python the user has installed.
  207. import("package.tools.autoconf").configure(package, configs, {envs = {PYTHONHOME = "", PYTHONPATH = ""}})
  208. os.vrunv("make", {"install", "-j4", "PYTHONAPPSDIR=" .. package:installdir()})
  209. if package:version():ge("3.0") then
  210. os.cp(path.join(package:installdir("bin"), "python3"), path.join(package:installdir("bin"), "python"))
  211. os.cp(path.join(package:installdir("bin"), "python3-config"), path.join(package:installdir("bin"), "python-config"))
  212. end
  213. -- install wheel
  214. local python = path.join(package:installdir("bin"), "python")
  215. local version = package:version()
  216. local pyver = ("python%d.%d"):format(version:major(), version:minor())
  217. local envs = {
  218. PATH = package:installdir("bin"),
  219. PYTHONPATH = package:installdir("lib", pyver, "site-packages"),
  220. LD_LIBRARY_PATH = package:installdir("lib")
  221. }
  222. os.vrunv(python, {"-m", "pip", "install", "--upgrade", "--force-reinstall", "pip"}, {envs = envs})
  223. os.vrunv(python, {"-m", "pip", "install", "wheel"}, {envs = envs})
  224. end)
  225. on_test(function (package)
  226. os.vrun("python --version")
  227. os.vrun("python -c \"import pip\"")
  228. os.vrun("python -c \"import setuptools\"")
  229. os.vrun("python -c \"import wheel\"")
  230. if package:kind() ~= "binary" then
  231. assert(package:has_cfuncs("PyModule_New", {includes = "Python.h"}))
  232. end
  233. end)