2
0

xmake.lua 9.9 KB

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