xmake.lua 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  1. package("boost")
  2. set_homepage("https://www.boost.org/")
  3. set_description("Collection of portable C++ source libraries.")
  4. set_license("BSL-1.0")
  5. add_urls("https://github.com/boostorg/boost/releases/download/boost-$(version)/boost-$(version).tar.gz")
  6. add_urls("https://github.com/xmake-mirror/boost/releases/download/boost-$(version).tar.bz2", {alias = "mirror", version = function (version)
  7. return version .. "/boost_" .. (version:gsub("%.", "_"))
  8. end})
  9. add_versions("1.84.0", "4d27e9efed0f6f152dc28db6430b9d3dfb40c0345da7342eaa5a987dde57bd95")
  10. add_versions("1.83.0", "0c6049764e80aa32754acd7d4f179fd5551d8172a83b71532ae093e7384e98da")
  11. add_versions("1.82.0", "b62bd839ea6c28265af9a1f68393eda37fab3611425d3b28882d8e424535ec9d")
  12. add_versions("1.81.0", "121da556b718fd7bd700b5f2e734f8004f1cfa78b7d30145471c526ba75a151c")
  13. add_versions("mirror:1.80.0", "1e19565d82e43bc59209a168f5ac899d3ba471d55c7610c677d4ccf2c9c500c0")
  14. add_versions("mirror:1.79.0", "475d589d51a7f8b3ba2ba4eda022b170e562ca3b760ee922c146b6c65856ef39")
  15. add_versions("mirror:1.78.0", "8681f175d4bdb26c52222665793eef08490d7758529330f98d3b29dd0735bccc")
  16. add_versions("mirror:1.77.0", "fc9f85fc030e233142908241af7a846e60630aa7388de9a5fafb1f3a26840854")
  17. add_versions("mirror:1.76.0", "f0397ba6e982c4450f27bf32a2a83292aba035b827a5623a14636ea583318c41")
  18. add_versions("mirror:1.75.0", "953db31e016db7bb207f11432bef7df100516eeb746843fa0486a222e3fd49cb")
  19. add_versions("mirror:1.74.0", "83bfc1507731a0906e387fc28b7ef5417d591429e51e788417fe9ff025e116b1")
  20. add_versions("mirror:1.73.0", "4eb3b8d442b426dc35346235c8733b5ae35ba431690e38c6a8263dce9fcbb402")
  21. add_versions("mirror:1.72.0", "59c9b274bc451cf91a9ba1dd2c7fdcaf5d60b1b3aa83f2c9fa143417cc660722")
  22. add_versions("mirror:1.70.0", "430ae8354789de4fd19ee52f3b1f739e1fba576f0aded0897c3c2bc00fb38778")
  23. if is_plat("mingw") and is_subhost("msys") then
  24. add_extsources("pacman::boost")
  25. elseif is_plat("linux") then
  26. add_extsources("pacman::boost", "apt::libboost-all-dev")
  27. elseif is_plat("macosx") then
  28. add_extsources("brew::boost")
  29. end
  30. add_patches("1.75.0", path.join(os.scriptdir(), "patches", "1.75.0", "warning.patch"), "43ff97d338c78b5c3596877eed1adc39d59a000cf651d0bcc678cf6cd6d4ae2e")
  31. if is_plat("linux") then
  32. add_deps("bzip2", "zlib")
  33. add_syslinks("pthread", "dl")
  34. end
  35. add_configs("pyver", {description = "python version x.y, etc. 3.10", default = "3.10"})
  36. local libnames = {"fiber",
  37. "coroutine",
  38. "context",
  39. "regex",
  40. "system",
  41. "container",
  42. "exception",
  43. "timer",
  44. "atomic",
  45. "graph",
  46. "serialization",
  47. "random",
  48. "wave",
  49. "date_time",
  50. "locale",
  51. "iostreams",
  52. "program_options",
  53. "test",
  54. "chrono",
  55. "contract",
  56. "graph_parallel",
  57. "json",
  58. "log",
  59. "thread",
  60. "filesystem",
  61. "math",
  62. "mpi",
  63. "nowide",
  64. "python",
  65. "stacktrace",
  66. "type_erasure"}
  67. add_configs("all", { description = "Enable all library modules support.", default = false, type = "boolean"})
  68. add_configs("multi", { description = "Enable multi-thread support.", default = true, type = "boolean"})
  69. for _, libname in ipairs(libnames) do
  70. add_configs(libname, { description = "Enable " .. libname .. " library.", default = (libname == "filesystem"), type = "boolean"})
  71. end
  72. on_load(function (package)
  73. function get_linkname(package, libname)
  74. local linkname
  75. if package:is_plat("windows") then
  76. linkname = (package:config("shared") and "boost_" or "libboost_") .. libname
  77. else
  78. linkname = "boost_" .. libname
  79. end
  80. if libname == "python" or libname == "numpy" then
  81. linkname = linkname .. package:config("pyver"):gsub("%p+", "")
  82. end
  83. if package:config("multi") then
  84. linkname = linkname .. "-mt"
  85. end
  86. if package:is_plat("windows") then
  87. local vs_runtime = package:config("vs_runtime")
  88. if package:config("shared") then
  89. if package:debug() then
  90. linkname = linkname .. "-gd"
  91. end
  92. elseif vs_runtime == "MT" then
  93. linkname = linkname .. "-s"
  94. elseif vs_runtime == "MTd" then
  95. linkname = linkname .. "-sgd"
  96. elseif vs_runtime == "MDd" then
  97. linkname = linkname .. "-gd"
  98. end
  99. else
  100. if package:debug() then
  101. linkname = linkname .. "-d"
  102. end
  103. end
  104. return linkname
  105. end
  106. -- we need the fixed link order
  107. local sublibs = {log = {"log_setup", "log"},
  108. python = {"python", "numpy"},
  109. stacktrace = {"stacktrace_backtrace", "stacktrace_basic"}}
  110. for _, libname in ipairs(libnames) do
  111. local libs = sublibs[libname]
  112. if libs then
  113. for _, lib in ipairs(libs) do
  114. package:add("links", get_linkname(package, lib))
  115. end
  116. else
  117. package:add("links", get_linkname(package, libname))
  118. end
  119. end
  120. -- disable auto-link all libs
  121. if package:is_plat("windows") then
  122. package:add("defines", "BOOST_ALL_NO_LIB")
  123. end
  124. if package:config("python") then
  125. if not package:config("shared") then
  126. package:add("defines", "BOOST_PYTHON_STATIC_LIB")
  127. end
  128. package:add("deps", "python " .. package:config("pyver") .. ".x", {configs = {headeronly = true}})
  129. end
  130. end)
  131. on_install("macosx", "linux", "windows", "bsd", "mingw", "cross", function (package)
  132. import("core.base.option")
  133. function get_compiler(package, toolchain)
  134. local cxx = package:build_getenv("cxx")
  135. if package:is_plat("macosx") then
  136. -- we uses ld/clang++ for link stdc++ for shared libraries
  137. -- and we need `xcrun -sdk macosx clang++` to make b2 to get `-isysroot` automatically
  138. local cc = package:build_getenv("ld")
  139. if cc and cc:find("clang", 1, true) and cc:find("Xcode", 1, true) then
  140. cc = "xcrun -sdk macosx clang++"
  141. end
  142. return format("using darwin : : %s ;", cc)
  143. elseif package:is_plat("windows") then
  144. local vs_toolset = toolchain:config("vs_toolset")
  145. local msvc_ver = ""
  146. local win_toolset = "msvc"
  147. if toolchain:name() == "clang-cl" then
  148. win_toolset = "clang-win"
  149. cxx = cxx:gsub("(clang%-cl)$", "%1.exe", 1)
  150. msvc_ver = ""
  151. elseif vs_toolset then
  152. local i = vs_toolset:find("%.")
  153. msvc_ver = i and vs_toolset:sub(1, i + 1)
  154. end
  155. -- Specifying a version will disable b2 from forcing tools
  156. -- from the latest installed msvc version.
  157. return format("using %s : %s : \"%s\" ;", win_toolset, msvc_ver, cxx:gsub("\\", "\\\\"))
  158. else
  159. cxx = cxx:gsub("gcc$", "g++")
  160. cxx = cxx:gsub("clang$", "clang++")
  161. return format("using gcc : : %s ;", cxx:gsub("\\", "/"))
  162. end
  163. end
  164. -- get host toolchain
  165. import("core.tool.toolchain")
  166. local host_toolchain
  167. if package:is_plat("windows") then
  168. host_toolchain = toolchain.load("msvc", {plat = "windows", arch = os.arch()})
  169. if not host_toolchain:check() then
  170. host_toolchain = toolchain.load("clang-cl", {plat = "windows", arch = os.arch()})
  171. end
  172. assert(host_toolchain:check(), "host msvc or clang-cl not found!")
  173. end
  174. -- force boost to compile with the desired compiler
  175. local file = io.open("user-config.jam", "w")
  176. if file then
  177. file:write(get_compiler(package, host_toolchain))
  178. file:close()
  179. end
  180. local bootstrap_argv =
  181. {
  182. "--prefix=" .. package:installdir(),
  183. "--libdir=" .. package:installdir("lib"),
  184. "--without-icu"
  185. }
  186. if package:is_plat("windows") then
  187. -- for bootstrap.bat, all other arguments are useless
  188. bootstrap_argv = { "msvc" }
  189. os.vrunv("bootstrap.bat", bootstrap_argv, {envs = host_toolchain:runenvs()})
  190. elseif package:is_plat("mingw") and is_host("windows") then
  191. bootstrap_argv = { "gcc" }
  192. os.vrunv("bootstrap.bat", bootstrap_argv)
  193. -- todo looking for better solution to fix the confict between user-config.jam and project-config.jam
  194. io.replace("project-config.jam", "using[^\n]+", "")
  195. else
  196. os.vrunv("./bootstrap.sh", bootstrap_argv)
  197. end
  198. -- get build toolchain
  199. local build_toolchain
  200. local build_toolset
  201. local runenvs
  202. if package:is_plat("windows") then
  203. build_toolchain = package:toolchain("clang-cl") or package:toolchain("msvc") or
  204. toolchain.load("msvc", {plat = package:plat(), arch = package:arch()})
  205. assert(build_toolchain:check(), "build toolchain not found!")
  206. build_toolset = build_toolchain:name() == "clang-cl" and "clang-win" or "msvc"
  207. runenvs = build_toolchain:runenvs()
  208. end
  209. local file = io.open("user-config.jam", "w")
  210. if file then
  211. file:write(get_compiler(package, build_toolchain))
  212. file:close()
  213. end
  214. os.vrun("./b2 headers")
  215. local njobs = option.get("jobs") or tostring(os.default_njob())
  216. local argv =
  217. {
  218. "--prefix=" .. package:installdir(),
  219. "--libdir=" .. package:installdir("lib"),
  220. "-d2",
  221. "-j" .. njobs,
  222. "--hash",
  223. "-q", -- quit on first error
  224. "--layout=tagged-1.66", -- prevent -x64 suffix in case cmake can't find it
  225. "--user-config=user-config.jam",
  226. "-sNO_LZMA=1",
  227. "-sNO_ZSTD=1",
  228. "install",
  229. "threading=" .. (package:config("multi") and "multi" or "single"),
  230. "debug-symbols=" .. (package:debug() and "on" or "off"),
  231. "link=" .. (package:config("shared") and "shared" or "static"),
  232. "variant=" .. (package:is_debug() and "debug" or "release"),
  233. "runtime-debugging=" .. (package:is_debug() and "on" or "off")
  234. }
  235. if package:config("lto") then
  236. table.insert(argv, "lto=on")
  237. end
  238. if package:is_arch("aarch64", "arm+.*") then
  239. table.insert(argv, "architecture=arm")
  240. end
  241. if package:is_arch(".+64.*") then
  242. table.insert(argv, "address-model=64")
  243. else
  244. table.insert(argv, "address-model=32")
  245. end
  246. local cxxflags
  247. if package:is_plat("windows") then
  248. local vs_runtime = package:config("vs_runtime")
  249. if package:config("shared") then
  250. table.insert(argv, "runtime-link=shared")
  251. elseif vs_runtime and vs_runtime:startswith("MT") then
  252. table.insert(argv, "runtime-link=static")
  253. else
  254. table.insert(argv, "runtime-link=shared")
  255. end
  256. table.insert(argv, "toolset=" .. build_toolset)
  257. cxxflags = "-std:c++14"
  258. elseif package:is_plat("mingw") then
  259. table.insert(argv, "toolset=gcc")
  260. elseif package:is_plat("macosx") then
  261. table.insert(argv, "toolset=darwin")
  262. -- fix macosx arm64 build issue https://github.com/microsoft/vcpkg/pull/18529
  263. cxxflags = "-std=c++14 -arch " .. package:arch()
  264. local xcode = package:toolchain("xcode") or import("core.tool.toolchain").load("xcode", {plat = package:plat(), arch = package:arch()})
  265. if xcode:check() then
  266. local xcode_dir = xcode:config("xcode")
  267. local xcode_sdkver = xcode:config("xcode_sdkver")
  268. local target_minver = xcode:config("target_minver")
  269. if xcode_dir and xcode_sdkver then
  270. local xcode_sdkdir = xcode_dir .. "/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX" .. xcode_sdkver .. ".sdk"
  271. cxxflags = cxxflags .. " -isysroot " .. xcode_sdkdir
  272. end
  273. if target_minver then
  274. cxxflags = cxxflags .. " -mmacosx-version-min=" .. target_minver
  275. end
  276. end
  277. else
  278. cxxflags = "-std=c++14"
  279. if package:config("pic") ~= false then
  280. cxxflags = cxxflags .. " -fPIC"
  281. end
  282. end
  283. if cxxflags then
  284. table.insert(argv, "cxxflags=" .. cxxflags)
  285. end
  286. for _, libname in ipairs(libnames) do
  287. if package:config("all") or package:config(libname) then
  288. table.insert(argv, "--with-" .. libname)
  289. end
  290. end
  291. if package:is_plat("linux") then
  292. table.insert(argv, "pch=off")
  293. end
  294. local ok = os.execv("./b2", argv, {envs = runenvs, try = true, stdout = "boost-log.txt"})
  295. if ok ~= 0 then
  296. raise("boost build failed, please check log in " .. path.join(os.curdir(), "boost-log.txt"))
  297. end
  298. end)
  299. on_test(function (package)
  300. assert(package:check_cxxsnippets({test = [[
  301. #include <boost/algorithm/string.hpp>
  302. #include <string>
  303. #include <vector>
  304. static void test() {
  305. std::string str("a,b");
  306. std::vector<std::string> vec;
  307. boost::algorithm::split(vec, str, boost::algorithm::is_any_of(","));
  308. }
  309. ]]}, {configs = {languages = "c++14"}}))
  310. if package:config("date_time") then
  311. assert(package:check_cxxsnippets({test = [[
  312. #include <boost/date_time/gregorian/gregorian.hpp>
  313. static void test() {
  314. boost::gregorian::date d(2010, 1, 30);
  315. }
  316. ]]}, {configs = {languages = "c++14"}}))
  317. end
  318. end)