xmake.lua 8.0 KB

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