xmake.lua 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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()) or not is_arch(os.arch()) then
  25. set_kind("binary")
  26. end
  27. if is_host("macosx", "linux", "bsd") then
  28. add_deps("openssl", "ca-certificates", {host = true})
  29. end
  30. if is_host("linux", "bsd") 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", "@bsd", 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|x86_64", "@linux", "@bsd", function (package)
  63. -- init configs
  64. local configs = {"--enable-ipv6", "--with-ensurepip", "--enable-optimizations"}
  65. table.insert(configs, "--libdir=" .. package:installdir("lib"))
  66. table.insert(configs, "--datadir=" .. package:installdir("share"))
  67. table.insert(configs, "--datarootdir=" .. package:installdir("share"))
  68. table.insert(configs, "--enable-shared=" .. (package:config("shared") and "yes" or "no"))
  69. if package:is_plat("linux") and package:config("pic") ~= false then
  70. table.insert(configs, "--with-pic")
  71. end
  72. -- add compiler settings
  73. if package:has_tool("cxx", "gcc", "g++") then
  74. table.insert(configs, "CXX=g++")
  75. elseif package:has_tool("cxx", "clang", "clang++") then
  76. table.insert(configs, "CXX=clang++")
  77. end
  78. -- add openssl libs path for detecting
  79. local openssl_dir
  80. local openssl = package:dep("openssl"):fetch()
  81. if openssl then
  82. for _, linkdir in ipairs(openssl.linkdirs) do
  83. if path.filename(linkdir) == "lib" then
  84. openssl_dir = path.directory(linkdir)
  85. if openssl_dir then
  86. break
  87. end
  88. end
  89. end
  90. end
  91. if openssl_dir then
  92. io.gsub("setup.py", "/usr/local/ssl", openssl_dir)
  93. end
  94. -- allow python modules to use ctypes.find_library to find xmake's stuff
  95. if package:is_plat("macosx") then
  96. io.gsub("Lib/ctypes/macholib/dyld.py", "DEFAULT_LIBRARY_FALLBACK = %[", format("DEFAULT_LIBRARY_FALLBACK = [ '%s/lib',", package:installdir()))
  97. end
  98. -- add flags
  99. local cflags = {}
  100. local ldflags = {}
  101. if package:is_plat("macosx") then
  102. -- get xcode information
  103. import("core.tool.toolchain")
  104. local xcode_dir
  105. local xcode_sdkver
  106. local target_minver
  107. local xcode = toolchain.load("xcode", {plat = package:plat(), arch = package:arch()})
  108. if xcode and xcode.config and xcode:check() then
  109. xcode_dir = xcode:config("xcode")
  110. xcode_sdkver = xcode:config("xcode_sdkver")
  111. target_minver = xcode:config("target_minver")
  112. end
  113. xcode_dir = xcode_dir or get_config("xcode")
  114. xcode_sdkver = xcode_sdkver or get_config("xcode_sdkver")
  115. target_minver = target_minver or get_config("target_minver")
  116. if xcode_dir and xcode_sdkver then
  117. -- help Python's build system (setuptools/pip) to build things on SDK-based systems
  118. -- the setup.py looks at "-isysroot" to get the sysroot (and not at --sysroot)
  119. local xcode_sdkdir = xcode_dir .. "/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX" .. xcode_sdkver .. ".sdk"
  120. table.insert(cflags, "-isysroot " .. xcode_sdkdir)
  121. table.insert(cflags, "-I" .. path.join(xcode_sdkdir, "/usr/include"))
  122. table.insert(ldflags, "-isysroot " .. xcode_sdkdir)
  123. -- for the Xlib.h, Python needs this header dir with the system Tk
  124. -- yep, this needs the absolute path where zlib needed a path relative to the SDK.
  125. table.insert(cflags, "-I" .. path.join(xcode_sdkdir, "/System/Library/Frameworks/Tk.framework/Versions/8.5/Headers"))
  126. end
  127. -- avoid linking to libgcc https://mail.python.org/pipermail/python-dev/2012-February/116205.html
  128. if target_minver then
  129. table.insert(configs, "MACOSX_DEPLOYMENT_TARGET=" .. target_minver)
  130. end
  131. end
  132. if #cflags > 0 then
  133. table.insert(configs, "CFLAGS=" .. table.concat(cflags, " "))
  134. end
  135. if #ldflags > 0 then
  136. table.insert(configs, "LDFLAGS=" .. table.concat(ldflags, " "))
  137. end
  138. -- add zlib to fix `No module named 'zlib'`
  139. local linkdirs = {}
  140. local includedirs = {}
  141. if package:is_plat("linux") then
  142. local zlib = package:dep("zlib"):fetch({external = false})
  143. if zlib then
  144. table.join2(linkdirs, zlib.linkdirs)
  145. table.join2(includedirs, zlib.includedirs)
  146. end
  147. -- add libffi to fix `No module named '_ctypes'`
  148. local libffi = package:dep("libffi"):fetch({external = false})
  149. if libffi then
  150. table.join2(linkdirs, libffi.linkdirs)
  151. table.join2(includedirs, libffi.includedirs)
  152. end
  153. end
  154. if #linkdirs > 0 and #includedirs > 0 then
  155. io.replace("setup.py", " def detect_modules(self):", format([[ def detect_modules(self):
  156. linkdirs = ['%s']
  157. includedirs = ['%s']
  158. for includedir in includedirs:
  159. add_dir_to_list(self.compiler.include_dirs, includedir)
  160. for linkdir in linkdirs:
  161. add_dir_to_list(self.compiler.library_dirs, linkdir)
  162. ]], table.concat(linkdirs, "', '"), table.concat(includedirs, "', '")), {plain = true})
  163. end
  164. -- unset these so that installing pip and setuptools puts them where we want
  165. -- and not into some other Python the user has installed.
  166. import("package.tools.autoconf").configure(package, configs, {envs = {PYTHONHOME = "", PYTHONPATH = "", LD_LIBRARY_PATH = package:installdir("lib")}})
  167. os.vrunv("make", {"-j4", "PYTHONAPPSDIR=" .. package:installdir()})
  168. os.vrunv("make", {"install", "-j4", "PYTHONAPPSDIR=" .. package:installdir()})
  169. -- install wheel
  170. local python = path.join(package:installdir("bin"), "python")
  171. os.vrunv(python, {"-m", "pip", "install", "wheel"}, {envs = {LD_LIBRARY_PATH = package:installdir("lib")}})
  172. end)
  173. on_test(function (package)
  174. os.vrun("python2 --version")
  175. os.vrun("python2 -c \"import pip\"")
  176. os.vrun("python2 -c \"import setuptools\"")
  177. os.vrun("python2 -c \"import wheel\"")
  178. if package:kind() ~= "binary" then
  179. assert(package:has_cfuncs("PyModule_New", {includes = "Python.h"}))
  180. end
  181. end)