xmake.lua 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  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-releases/raw/$(version)/python-$(version).win32.tar.gz",
  7. "https://gitlab.com/xmake-mirror/python-releases/-/raw/$(version)/python-$(version).win32.tar.gz")
  8. add_versions("2.7.15", "4a7be2b440b74776662daaeb6bb6c5574bb6d0f4ddc0ad03ce63571ab2353303")
  9. add_versions("2.7.18", "9efaf273aa2e7d23fa22efa2936619ec91cf9ee189f707e375f9063fadeabcd6")
  10. add_versions("3.7.0", "6f6dfd3df4b15157a12d06685a6dda450478ca118aa8832f0033093b9ca6329f")
  11. add_versions("3.8.1", "f4fe3eeec4ee50260382a8221b1bebf919b6635a499341abe128986ae76f17e3")
  12. add_versions("3.8.5", "9d1b901a508b3a6745aa225596d98a1aaa39cf8e9b9f79b5ded7192d4503a5aa")
  13. else
  14. add_urls("https://github.com/xmake-mirror/python-releases/raw/$(version)/python-$(version).win64.tar.gz",
  15. "https://gitlab.com/xmake-mirror/python-releases/-/raw/$(version)/python-$(version).win64.tar.gz")
  16. add_versions("2.7.15", "c81c4604b4176ff26be8d37cf48a2582e71a5e8f475b531c2e5d032a39511acb")
  17. add_versions("2.7.18", "0e1adec089c4358b4ff1cd392c8bd7c975e0bf7c279aee91e7aaa04c00fb2c10")
  18. add_versions("3.7.0", "8acd395e64d09b6b33ef78e199ffa48a8fd48f32d4d90d575e72448939a0e4c5")
  19. add_versions("3.8.1", "9b7666a3d39a5b8405b0706fc042390b7ecfd0f75b948c7d2be012598b11163e")
  20. add_versions("3.8.5", "585f71093dd1303140f2e97700581456fe38e3ec47922bcb4ad3c76ee8ee2433")
  21. end
  22. else
  23. set_urls("https://www.python.org/ftp/python/$(version)/Python-$(version).tgz",
  24. "https://github.com/xmake-mirror/cpython/releases/download/v$(version)/Python-$(version).tgz")
  25. add_versions("2.7.15", "18617d1f15a380a919d517630a9cd85ce17ea602f9bbdc58ddc672df4b0239db")
  26. add_versions("2.7.18", "da3080e3b488f648a3d7a4560ddee895284c3380b11d6de75edb986526b9a814")
  27. add_versions("3.7.0", "85bb9feb6863e04fb1700b018d9d42d1caac178559ffa453d7e6a436e259fd0d")
  28. add_versions("3.8.1", "c7cfa39a43b994621b245e029769e9126caa2a93571cee2e743b213cceac35fb")
  29. add_versions("3.8.5", "015115023c382eb6ab83d512762fe3c5502fa0c6c52ffebc4831c4e1a06ffc49")
  30. end
  31. if not is_plat(os.host()) then
  32. set_kind("binary")
  33. end
  34. if is_host("macosx", "linux") then
  35. add_deps("openssl", {host = true})
  36. end
  37. if is_host("linux") then
  38. add_deps("libffi", "zlib", {host = true})
  39. add_syslinks("util", "pthread", "dl")
  40. end
  41. on_load("@windows", "@msys", "@cygwin", function (package)
  42. -- set includedirs
  43. package:add("includedirs", "include")
  44. -- set python environments
  45. local PYTHONPATH = package:installdir("Lib", "site-packages")
  46. package:addenv("PYTHONPATH", PYTHONPATH)
  47. package:addenv("PATH", "bin")
  48. package:addenv("PATH", "Scripts")
  49. end)
  50. on_load("@macosx", "@linux", function (package)
  51. -- set includedirs
  52. local version = package:version()
  53. local pyver = ("python%d.%d"):format(version:major(), version:minor())
  54. if version:ge("3.0") and version:le("3.8") then
  55. package:add("includedirs", path.join("include", pyver .. "m"))
  56. else
  57. package:add("includedirs", path.join("include", pyver))
  58. end
  59. -- set python environments
  60. local PYTHONPATH = package:installdir("lib", pyver, "site-packages")
  61. package:addenv("PYTHONPATH", PYTHONPATH)
  62. package:addenv("PATH", "bin")
  63. package:addenv("PATH", "Scripts")
  64. end)
  65. on_install("@windows", "@msys", "@cygwin", function (package)
  66. if package:version():ge("3.0") then
  67. os.cp("python.exe", path.join(package:installdir("bin"), "python3.exe"))
  68. else
  69. os.cp("python.exe", path.join(package:installdir("bin"), "python2.exe"))
  70. end
  71. os.mv("*.exe", package:installdir("bin"))
  72. os.mv("*.dll", package:installdir("bin"))
  73. os.mv("Lib", package:installdir())
  74. os.cp("libs/*", package:installdir("lib"))
  75. os.cp("*", package:installdir())
  76. local python = path.join(package:installdir("bin"), "python.exe")
  77. os.vrunv(python, {"-m", "pip", "install", "wheel"})
  78. end)
  79. on_install("@macosx", "@linux", function (package)
  80. -- init configs
  81. local configs = {"--enable-ipv6", "--with-ensurepip", "--enable-optimizations"}
  82. table.insert(configs, "--datadir=" .. package:installdir("share"))
  83. table.insert(configs, "--datarootdir=" .. package:installdir("share"))
  84. -- add openssl libs path for detecting
  85. local openssl_dir
  86. local openssl = package:dep("openssl"):fetch()
  87. if openssl then
  88. for _, linkdir in ipairs(openssl.linkdirs) do
  89. if path.filename(linkdir) == "lib" then
  90. openssl_dir = path.directory(linkdir)
  91. if openssl_dir then
  92. break
  93. end
  94. end
  95. end
  96. end
  97. if openssl_dir then
  98. if package:version():ge("3.0") then
  99. table.insert(configs, "--with-openssl=" .. openssl_dir)
  100. else
  101. io.gsub("setup.py", "/usr/local/ssl", openssl_dir)
  102. end
  103. end
  104. -- allow python modules to use ctypes.find_library to find xmake's stuff
  105. if package:is_plat("macosx") then
  106. io.gsub("Lib/ctypes/macholib/dyld.py", "DEFAULT_LIBRARY_FALLBACK = %[", format("DEFAULT_LIBRARY_FALLBACK = [ '%s/lib',", package:installdir()))
  107. end
  108. -- add flags for macOS
  109. local cflags = {}
  110. local ldflags = {}
  111. if package:is_plat("macosx") then
  112. -- get xcode information
  113. import("core.tool.toolchain")
  114. local xcode_dir
  115. local xcode_sdkver
  116. local target_minver
  117. local xcode = toolchain.load("xcode", {plat = package:plat(), arch = package:arch()})
  118. if xcode and xcode.config and xcode:check() then
  119. xcode_dir = xcode:config("xcode")
  120. xcode_sdkver = xcode:config("xcode_sdkver")
  121. target_minver = xcode:config("target_minver")
  122. end
  123. xcode_dir = xcode_dir or get_config("xcode")
  124. xcode_sdkver = xcode_sdkver or get_config("xcode_sdkver")
  125. target_minver = target_minver or get_config("target_minver")
  126. -- TODO will be deprecated after xmake v2.5.1
  127. xcode_sdkver = xcode_sdkver or get_config("xcode_sdkver_macosx")
  128. if not xcode_dir or not xcode_sdkver then
  129. -- maybe on cross platform, we need find xcode envs manually
  130. local xcode = import("detect.sdks.find_xcode")(nil, {force = true, plat = package:plat(), arch = package:arch()})
  131. if xcode then
  132. xcode_dir = xcode.sdkdir
  133. xcode_sdkver = xcode.sdkver
  134. end
  135. end
  136. -- TODO will be deprecated after xmake v2.5.1
  137. target_minver = target_minver or get_config("target_minver_macosx")
  138. if not target_minver then
  139. local macos_ver = macos.version()
  140. if macos_ver then
  141. target_minver = macos_ver:major() .. "." .. macos_ver:minor()
  142. end
  143. end
  144. if xcode_dir and xcode_sdkver then
  145. -- help Python's build system (setuptools/pip) to build things on SDK-based systems
  146. -- the setup.py looks at "-isysroot" to get the sysroot (and not at --sysroot)
  147. local xcode_sdkdir = xcode_dir .. "/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX" .. xcode_sdkver .. ".sdk"
  148. table.insert(cflags, "-isysroot " .. xcode_sdkdir)
  149. table.insert(cflags, "-I" .. path.join(xcode_sdkdir, "/usr/include"))
  150. table.insert(ldflags, "-isysroot " .. xcode_sdkdir)
  151. -- for the Xlib.h, Python needs this header dir with the system Tk
  152. -- yep, this needs the absolute path where zlib needed a path relative to the SDK.
  153. table.insert(cflags, "-I" .. path.join(xcode_sdkdir, "/System/Library/Frameworks/Tk.framework/Versions/8.5/Headers"))
  154. end
  155. -- avoid linking to libgcc https://mail.python.org/pipermail/python-dev/2012-February/116205.html
  156. if target_minver then
  157. table.insert(configs, "MACOSX_DEPLOYMENT_TARGET=" .. target_minver)
  158. end
  159. end
  160. if #cflags > 0 then
  161. table.insert(configs, "CFLAGS=" .. table.concat(cflags, " "))
  162. end
  163. if #ldflags > 0 then
  164. table.insert(configs, "LDFLAGS=" .. table.concat(ldflags, " "))
  165. end
  166. -- add zlib to fix `No module named 'zlib'`
  167. local linkdirs = {}
  168. local includedirs = {}
  169. if package:is_plat("linux") then
  170. local zlib = package:dep("zlib"):fetch({external = false})
  171. if zlib then
  172. table.join2(linkdirs, zlib.linkdirs)
  173. table.join2(includedirs, zlib.includedirs)
  174. end
  175. -- add libffi to fix `No module named '_ctypes'`
  176. local libffi = package:dep("libffi"):fetch({external = false})
  177. if libffi then
  178. table.join2(linkdirs, libffi.linkdirs)
  179. table.join2(includedirs, libffi.includedirs)
  180. end
  181. end
  182. if #linkdirs > 0 and #includedirs > 0 then
  183. io.replace("setup.py", " def detect_modules(self):", format([[ def detect_modules(self):
  184. linkdirs = ['%s']
  185. includedirs = ['%s']
  186. for includedir in includedirs:
  187. add_dir_to_list(self.compiler.include_dirs, includedir)
  188. for linkdir in linkdirs:
  189. add_dir_to_list(self.compiler.library_dirs, linkdir)
  190. ]], table.concat(linkdirs, "', '"), table.concat(includedirs, "', '")), {plain = true})
  191. end
  192. -- unset these so that installing pip and setuptools puts them where we want
  193. -- and not into some other Python the user has installed.
  194. import("package.tools.autoconf").configure(package, configs, {envs = {PYTHONHOME = "", PYTHONPATH = ""}})
  195. os.vrunv("make", {"install", "-j4", "PYTHONAPPSDIR=" .. package:installdir()})
  196. if package:version():ge("3.0") then
  197. os.cp(path.join(package:installdir("bin"), "python3"), path.join(package:installdir("bin"), "python"))
  198. os.cp(path.join(package:installdir("bin"), "python3-config"), path.join(package:installdir("bin"), "python-config"))
  199. end
  200. -- install wheel
  201. local python = path.join(package:installdir("bin"), "python")
  202. os.vrunv(python, {"-m", "pip", "install", "wheel"})
  203. end)
  204. on_test(function (package)
  205. os.vrun("python --version")
  206. os.vrun("python -c \"import pip\"")
  207. os.vrun("python -c \"import setuptools\"")
  208. os.vrun("python -c \"import wheel\"")
  209. if package:kind() ~= "binary" then
  210. assert(package:has_cfuncs("PyModule_New", {includes = "Python.h"}))
  211. end
  212. if is_host("windows") and package:version():ge("3.8.0") and winos.version():gt("win8") then
  213. os.vrun("py -3 -c \"import sys\"")
  214. end
  215. end)