xmake.lua 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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_syslinks("util", "pthread", "dl")
  39. end
  40. on_load("@windows", "@msys", "@cygwin", function (package)
  41. -- set includedirs
  42. package:add("includedirs", "include")
  43. -- set python environments
  44. local PYTHONPATH = package:installdir("Lib", "site-packages")
  45. package:addenv("PYTHONPATH", PYTHONPATH)
  46. package:addenv("PATH", "bin")
  47. package:addenv("PATH", "Scripts")
  48. end)
  49. on_load("@macosx", "@linux", function (package)
  50. -- set includedirs
  51. local version = package:version()
  52. local pyver = ("python%d.%d"):format(version:major(), version:minor())
  53. if version:ge("3.0") and version:le("3.8") then
  54. package:add("includedirs", path.join("include", pyver .. "m"))
  55. else
  56. package:add("includedirs", path.join("include", pyver))
  57. end
  58. -- set python environments
  59. local PYTHONPATH = package:installdir("lib", pyver, "site-packages")
  60. package:addenv("PYTHONPATH", PYTHONPATH)
  61. package:addenv("PATH", "bin")
  62. package:addenv("PATH", "Scripts")
  63. end)
  64. on_install("@windows", "@msys", "@cygwin", function (package)
  65. if package:version():ge("3.0") then
  66. os.cp("python.exe", path.join(package:installdir("bin"), "python3.exe"))
  67. else
  68. os.cp("python.exe", path.join(package:installdir("bin"), "python2.exe"))
  69. end
  70. os.mv("*.exe", package:installdir("bin"))
  71. os.mv("*.dll", package:installdir("bin"))
  72. os.mv("Lib", package:installdir())
  73. os.mv("libs/*", package:installdir("lib"))
  74. os.cp("*|libs", package:installdir())
  75. local python = path.join(package:installdir("bin"), "python.exe")
  76. os.vrunv(python, {"-m", "pip", "install", "wheel"})
  77. end)
  78. on_install("@macosx", "@linux", function (package)
  79. -- init configs
  80. local configs = {"--enable-ipv6", "--with-ensurepip", "--enable-optimizations"}
  81. table.insert(configs, "--datadir=" .. package:installdir("share"))
  82. table.insert(configs, "--datarootdir=" .. package:installdir("share"))
  83. -- add openssl libs path for detecting
  84. local openssl_dir = package:dep("openssl"):installdir()
  85. if package:version():ge("3.0") then
  86. table.insert(configs, "--with-openssl=" .. openssl_dir)
  87. else
  88. io.gsub("setup.py", "/usr/local/ssl", openssl_dir)
  89. end
  90. -- allow python modules to use ctypes.find_library to find xmake's stuff
  91. if is_host("macosx") then
  92. io.gsub("Lib/ctypes/macholib/dyld.py", "DEFAULT_LIBRARY_FALLBACK = %[", format("DEFAULT_LIBRARY_FALLBACK = [ '%s/lib',", package:installdir()))
  93. end
  94. -- add flags
  95. local cflags = {}
  96. local ldflags = {}
  97. if package:is_plat("macosx") then
  98. local xcode_dir = get_config("xcode")
  99. local xcode_sdkver = get_config("xcode_sdkver") or get_config("xcode_sdkver_macosx")
  100. if not xcode_dir or not xcode_sdkver then
  101. -- maybe on cross platform, we need find xcode envs manually
  102. local xcode = import("detect.sdks.find_xcode")(nil, {force = true, plat = package:plat(), arch = package:arch()})
  103. if xcode then
  104. xcode_dir = xcode.sdkdir
  105. xcode_sdkver = xcode.sdkver
  106. end
  107. end
  108. if xcode_dir and xcode_sdkver then
  109. -- help Python's build system (setuptools/pip) to build things on SDK-based systems
  110. -- the setup.py looks at "-isysroot" to get the sysroot (and not at --sysroot)
  111. local xcode_sdkdir = xcode_dir .. "/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX" .. xcode_sdkver .. ".sdk"
  112. table.insert(cflags, "-isysroot " .. xcode_sdkdir)
  113. table.insert(cflags, "-I" .. path.join(xcode_sdkdir, "/usr/include"))
  114. table.insert(ldflags, "-isysroot " .. xcode_sdkdir)
  115. -- for the Xlib.h, Python needs this header dir with the system Tk
  116. -- yep, this needs the absolute path where zlib needed a path relative to the SDK.
  117. table.insert(cflags, "-I" .. path.join(xcode_sdkdir, "/System/Library/Frameworks/Tk.framework/Versions/8.5/Headers"))
  118. end
  119. -- avoid linking to libgcc https://mail.python.org/pipermail/python-dev/2012-February/116205.html
  120. local target_minver = get_config("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 target_minver then
  128. table.insert(configs, "MACOSX_DEPLOYMENT_TARGET=" .. target_minver)
  129. end
  130. end
  131. if #cflags > 0 then
  132. table.insert(configs, "CFLAGS=" .. table.concat(cflags, " "))
  133. end
  134. if #ldflags > 0 then
  135. table.insert(configs, "LDFLAGS=" .. table.concat(ldflags, " "))
  136. end
  137. -- unset these so that installing pip and setuptools puts them where we want
  138. -- and not into some other Python the user has installed.
  139. import("package.tools.autoconf").configure(package, configs, {envs = {PYTHONHOME = "", PYTHONPATH = ""}})
  140. os.vrunv("make", {"install", "-j4", "PYTHONAPPSDIR=" .. package:installdir()})
  141. if package:version():ge("3.0") then
  142. os.cp(path.join(package:installdir("bin"), "python3"), path.join(package:installdir("bin"), "python"))
  143. os.cp(path.join(package:installdir("bin"), "python3-config"), path.join(package:installdir("bin"), "python-config"))
  144. end
  145. -- install wheel
  146. local python = path.join(package:installdir("bin"), "python")
  147. os.vrunv(python, {"-m", "pip", "install", "wheel"})
  148. end)
  149. on_test(function (package)
  150. os.vrun("python --version")
  151. os.vrun("python -c \"import pip\"")
  152. os.vrun("python -c \"import setuptools\"")
  153. os.vrun("python -c \"import wheel\"")
  154. if package:kind() ~= "binary" then
  155. assert(package:has_cfuncs("PyModule_New", {includes = "Python.h"}))
  156. end
  157. if is_host("windows") and package:version():ge("3.8.0") then
  158. os.vrun("py -3 -c \"import sys\"")
  159. end
  160. end)