xmake.lua 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478
  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)-b2-nodocs.tar.gz")
  6. add_urls("https://github.com/boostorg/boost/releases/download/boost-$(version)/boost-$(version).tar.gz")
  7. add_urls("https://github.com/xmake-mirror/boost/releases/download/boost-$(version).tar.bz2", {alias = "mirror", version = function (version)
  8. return version .. "/boost_" .. (version:gsub("%.", "_"))
  9. end})
  10. add_versions("1.85.0", "f4a7d3f81b8a0f65067b769ea84135fd7b72896f4f59c7f405086c8c0dc61434")
  11. add_versions("1.84.0", "4d27e9efed0f6f152dc28db6430b9d3dfb40c0345da7342eaa5a987dde57bd95")
  12. add_versions("1.83.0", "0c6049764e80aa32754acd7d4f179fd5551d8172a83b71532ae093e7384e98da")
  13. add_versions("1.82.0", "b62bd839ea6c28265af9a1f68393eda37fab3611425d3b28882d8e424535ec9d")
  14. add_versions("1.81.0", "121da556b718fd7bd700b5f2e734f8004f1cfa78b7d30145471c526ba75a151c")
  15. add_versions("mirror:1.80.0", "1e19565d82e43bc59209a168f5ac899d3ba471d55c7610c677d4ccf2c9c500c0")
  16. add_versions("mirror:1.79.0", "475d589d51a7f8b3ba2ba4eda022b170e562ca3b760ee922c146b6c65856ef39")
  17. add_versions("mirror:1.78.0", "8681f175d4bdb26c52222665793eef08490d7758529330f98d3b29dd0735bccc")
  18. add_versions("mirror:1.77.0", "fc9f85fc030e233142908241af7a846e60630aa7388de9a5fafb1f3a26840854")
  19. add_versions("mirror:1.76.0", "f0397ba6e982c4450f27bf32a2a83292aba035b827a5623a14636ea583318c41")
  20. add_versions("mirror:1.75.0", "953db31e016db7bb207f11432bef7df100516eeb746843fa0486a222e3fd49cb")
  21. add_versions("mirror:1.74.0", "83bfc1507731a0906e387fc28b7ef5417d591429e51e788417fe9ff025e116b1")
  22. add_versions("mirror:1.73.0", "4eb3b8d442b426dc35346235c8733b5ae35ba431690e38c6a8263dce9fcbb402")
  23. add_versions("mirror:1.72.0", "59c9b274bc451cf91a9ba1dd2c7fdcaf5d60b1b3aa83f2c9fa143417cc660722")
  24. add_versions("mirror:1.70.0", "430ae8354789de4fd19ee52f3b1f739e1fba576f0aded0897c3c2bc00fb38778")
  25. if is_plat("mingw") and is_subhost("msys") then
  26. add_extsources("pacman::boost")
  27. elseif is_plat("linux") then
  28. add_extsources("pacman::boost", "apt::libboost-all-dev")
  29. elseif is_plat("macosx") then
  30. add_extsources("brew::boost")
  31. end
  32. add_patches("1.75.0", path.join(os.scriptdir(), "patches", "1.75.0", "warning.patch"), "43ff97d338c78b5c3596877eed1adc39d59a000cf651d0bcc678cf6cd6d4ae2e")
  33. if is_plat("linux") then
  34. add_deps("bzip2", "zlib")
  35. add_syslinks("pthread", "dl")
  36. end
  37. add_configs("pyver", {description = "python version x.y, etc. 3.10", default = "3.10"})
  38. local libnames = {"atomic",
  39. "charconv",
  40. "chrono",
  41. "cobalt",
  42. "container",
  43. "context",
  44. "contract",
  45. "coroutine",
  46. "date_time",
  47. "exception",
  48. "fiber",
  49. "filesystem",
  50. "graph",
  51. "graph_parallel",
  52. "headers",
  53. "iostreams",
  54. "json",
  55. "locale",
  56. "log",
  57. "math",
  58. "mpi",
  59. "nowide",
  60. "program_options",
  61. "python",
  62. "random",
  63. "regex",
  64. "serialization",
  65. "stacktrace",
  66. "system",
  67. "test",
  68. "thread",
  69. "timer",
  70. "type_erasure",
  71. "url",
  72. "wave"}
  73. add_configs("all", { description = "Enable all library modules support.", default = false, type = "boolean"})
  74. add_configs("multi", { description = "Enable multi-thread support.", default = true, type = "boolean"})
  75. for _, libname in ipairs(libnames) do
  76. add_configs(libname, { description = "Enable " .. libname .. " library.", default = (libname == "filesystem"), type = "boolean"})
  77. end
  78. add_configs("zstd", {description = "enable zstd for iostreams", default = false, type = "boolean"})
  79. add_configs("lzma", {description = "enable lzma for iostreams", default = false, type = "boolean"})
  80. on_load(function (package)
  81. local function get_linkname(package, libname)
  82. local linkname
  83. if package:is_plat("windows") then
  84. linkname = (package:config("shared") and "boost_" or "libboost_") .. libname
  85. else
  86. linkname = "boost_" .. libname
  87. end
  88. if libname == "python" or libname == "numpy" then
  89. linkname = linkname .. package:config("pyver"):gsub("%p+", "")
  90. end
  91. if package:config("multi") then
  92. linkname = linkname .. "-mt"
  93. end
  94. if package:is_plat("windows") then
  95. local vs_runtime = package:config("vs_runtime")
  96. if package:config("shared") then
  97. if package:debug() then
  98. linkname = linkname .. "-gd"
  99. end
  100. elseif package:config("asan") or vs_runtime == "MTd" then
  101. linkname = linkname .. "-sgd"
  102. elseif vs_runtime == "MT" then
  103. linkname = linkname .. "-s"
  104. elseif package:config("asan") or vs_runtime == "MDd" then
  105. linkname = linkname .. "-gd"
  106. end
  107. else
  108. if package:debug() then
  109. linkname = linkname .. "-d"
  110. end
  111. end
  112. return linkname
  113. end
  114. -- we need the fixed link order
  115. local sublibs = {log = {"log_setup", "log"},
  116. python = {"python", "numpy"},
  117. stacktrace = {"stacktrace_backtrace", "stacktrace_basic"}}
  118. for _, libname in ipairs(libnames) do
  119. local libs = sublibs[libname]
  120. if libs then
  121. for _, lib in ipairs(libs) do
  122. package:add("links", get_linkname(package, lib))
  123. end
  124. else
  125. package:add("links", get_linkname(package, libname))
  126. end
  127. end
  128. -- disable auto-link all libs
  129. if package:is_plat("windows") then
  130. package:add("defines", "BOOST_ALL_NO_LIB")
  131. end
  132. if package:config("python") then
  133. if not package:config("shared") then
  134. package:add("defines", "BOOST_PYTHON_STATIC_LIB")
  135. end
  136. package:add("deps", "python " .. package:config("pyver") .. ".x", {configs = {headeronly = true}})
  137. end
  138. if package:is_plat("linux") then
  139. if package:config("zstd") then
  140. package:add("deps", "zstd")
  141. end
  142. if package:config("lzma") then
  143. package:add("deps", "xz")
  144. end
  145. end
  146. end)
  147. on_install("macosx", "linux", "windows", "bsd", "mingw", "cross", function (package)
  148. import("core.base.option")
  149. local function get_compiler(package, toolchain)
  150. local cxx = package:build_getenv("cxx")
  151. if package:is_plat("macosx") then
  152. -- we uses ld/clang++ for link stdc++ for shared libraries
  153. -- and we need `xcrun -sdk macosx clang++` to make b2 to get `-isysroot` automatically
  154. local cc = package:build_getenv("ld")
  155. if cc and cc:find("clang", 1, true) and cc:find("Xcode", 1, true) then
  156. cc = "xcrun -sdk macosx clang++"
  157. end
  158. return format("using darwin : : %s ;", cc)
  159. elseif package:is_plat("windows") then
  160. local vs_toolset = toolchain:config("vs_toolset")
  161. local msvc_ver = ""
  162. local win_toolset = "msvc"
  163. if toolchain:name() == "clang-cl" then
  164. win_toolset = "clang-win"
  165. cxx = cxx:gsub("(clang%-cl)$", "%1.exe", 1)
  166. msvc_ver = ""
  167. elseif vs_toolset then
  168. local i = vs_toolset:find("%.")
  169. msvc_ver = i and vs_toolset:sub(1, i + 1)
  170. end
  171. -- Specifying a version will disable b2 from forcing tools
  172. -- from the latest installed msvc version.
  173. return format("using %s : %s : \"%s\" ;", win_toolset, msvc_ver, cxx:gsub("\\", "\\\\"))
  174. else
  175. cxx = cxx:gsub("gcc$", "g++")
  176. cxx = cxx:gsub("gcc%-", "g++-")
  177. cxx = cxx:gsub("clang$", "clang++")
  178. cxx = cxx:gsub("clang%-", "clang++-")
  179. if cxx and cxx:find("clang", 1, true) then
  180. return format("using clang : : \"%s\" ;", cxx:gsub("\\", "/"))
  181. else
  182. return format("using gcc : : \"%s\" ;", cxx:gsub("\\", "/"))
  183. end
  184. end
  185. end
  186. -- get host toolchain
  187. import("core.tool.toolchain")
  188. local host_toolchain
  189. if package:is_plat("windows") then
  190. host_toolchain = toolchain.load("msvc", {plat = "windows", arch = os.arch()})
  191. if not host_toolchain:check() then
  192. host_toolchain = toolchain.load("clang-cl", {plat = "windows", arch = os.arch()})
  193. end
  194. assert(host_toolchain:check(), "host msvc or clang-cl not found!")
  195. end
  196. -- force boost to compile with the desired compiler
  197. local file = io.open("user-config.jam", "w")
  198. if file then
  199. file:write(get_compiler(package, host_toolchain))
  200. file:close()
  201. end
  202. local bootstrap_argv =
  203. {
  204. "--prefix=" .. package:installdir(),
  205. "--libdir=" .. package:installdir("lib"),
  206. "--without-icu"
  207. }
  208. if package:has_tool("cxx", "clang", "clangxx") then
  209. table.insert(bootstrap_argv, "--with-toolset=clang")
  210. end
  211. if package:is_plat("windows") then
  212. -- for bootstrap.bat, all other arguments are useless
  213. bootstrap_argv = { "msvc" }
  214. os.vrunv("bootstrap.bat", bootstrap_argv, {envs = host_toolchain:runenvs()})
  215. elseif package:is_plat("mingw") and is_host("windows") then
  216. bootstrap_argv = { "gcc" }
  217. os.vrunv("bootstrap.bat", bootstrap_argv)
  218. -- todo looking for better solution to fix the confict between user-config.jam and project-config.jam
  219. io.replace("project-config.jam", "using[^\n]+", "")
  220. else
  221. os.vrunv("./bootstrap.sh", bootstrap_argv)
  222. end
  223. -- get build toolchain
  224. local build_toolchain
  225. local build_toolset
  226. local runenvs
  227. if package:is_plat("windows") then
  228. if package:has_tool("cxx", "clang_cl") then
  229. build_toolset = "clang-win"
  230. build_toolchain = package:toolchain("clang-cl")
  231. elseif package:has_tool("cxx", "clang") then
  232. build_toolset = "clang-win"
  233. build_toolchain = package:toolchain("clang") or package:toolchain("llvm")
  234. elseif package:has_tool("cxx", "cl") then
  235. build_toolset = "msvc"
  236. build_toolchain = package:toolchain("msvc")
  237. end
  238. if build_toolchain then
  239. runenvs = build_toolchain:runenvs()
  240. end
  241. end
  242. local function config_deppath(file, depname, rule)
  243. local dep = package:dep(depname)
  244. local info = dep:fetch({external = false})
  245. if info then
  246. local usingstr = format("\nusing %s : : <include>\"%s\" <search>\"%s\" ;",rule, info.includedirs[1], info.linkdirs[1])
  247. file:write(usingstr)
  248. end
  249. end
  250. local file = io.open("user-config.jam", "w")
  251. if file then
  252. file:write(get_compiler(package, build_toolchain))
  253. if package:config("lzma") then
  254. config_deppath(file, "xz", "lzma")
  255. end
  256. if package:config("zstd") then
  257. config_deppath(file, "zstd", "zstd")
  258. end
  259. file:close()
  260. end
  261. os.vrun("./b2 headers")
  262. local njobs = option.get("jobs") or tostring(os.default_njob())
  263. local argv =
  264. {
  265. "--prefix=" .. package:installdir(),
  266. "--libdir=" .. package:installdir("lib"),
  267. "-d2",
  268. "-j" .. njobs,
  269. "--hash",
  270. "-q", -- quit on first error
  271. "--layout=tagged-1.66", -- prevent -x64 suffix in case cmake can't find it
  272. "--user-config=user-config.jam",
  273. "install",
  274. "threading=" .. (package:config("multi") and "multi" or "single"),
  275. "debug-symbols=" .. (package:debug() and "on" or "off"),
  276. "link=" .. (package:config("shared") and "shared" or "static"),
  277. "variant=" .. (package:is_debug() and "debug" or "release"),
  278. "runtime-debugging=" .. (package:is_debug() and "on" or "off")
  279. }
  280. if not package:config("lzma") then
  281. table.insert(argv, "-sNO_LZMA=1")
  282. end
  283. if not package:config("zstd") then
  284. table.insert(argv, "-sNO_ZSTD=1")
  285. end
  286. if package:config("lto") then
  287. table.insert(argv, "lto=on")
  288. end
  289. if package:is_arch("aarch64", "arm+.*") then
  290. table.insert(argv, "architecture=arm")
  291. end
  292. if package:is_arch(".+64.*") then
  293. table.insert(argv, "address-model=64")
  294. else
  295. table.insert(argv, "address-model=32")
  296. end
  297. local cxxflags = {}
  298. local linkflags = {}
  299. table.join2(cxxflags, table.wrap(package:config("cxflags")))
  300. table.join2(cxxflags, table.wrap(package:config("cxxflags")))
  301. if package:is_plat("windows") then
  302. local vs_runtime = package:config("vs_runtime")
  303. if package:config("shared") then
  304. table.insert(argv, "runtime-link=shared")
  305. elseif vs_runtime and vs_runtime:startswith("MT") then
  306. table.insert(argv, "runtime-link=static")
  307. else
  308. table.insert(argv, "runtime-link=shared")
  309. end
  310. table.insert(argv, "toolset=" .. build_toolset)
  311. table.insert(cxxflags, "-std:c++14")
  312. elseif package:is_plat("mingw") then
  313. table.insert(argv, "toolset=gcc")
  314. elseif package:is_plat("macosx") then
  315. table.insert(argv, "toolset=darwin")
  316. -- fix macosx arm64 build issue https://github.com/microsoft/vcpkg/pull/18529
  317. table.insert(cxxflags, "-std=c++14")
  318. table.insert(cxxflags, "-arch")
  319. table.insert(cxxflags, package:arch())
  320. local xcode = package:toolchain("xcode") or import("core.tool.toolchain").load("xcode", {plat = package:plat(), arch = package:arch()})
  321. if xcode:check() then
  322. local xcode_dir = xcode:config("xcode")
  323. local xcode_sdkver = xcode:config("xcode_sdkver")
  324. local target_minver = xcode:config("target_minver")
  325. if xcode_dir and xcode_sdkver then
  326. local xcode_sdkdir = xcode_dir .. "/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX" .. xcode_sdkver .. ".sdk"
  327. table.insert(cxxflags, "-isysroot")
  328. table.insert(cxxflags, xcode_sdkdir)
  329. end
  330. if target_minver then
  331. table.insert(cxxflags, "-mmacosx-version-min=" .. target_minver)
  332. end
  333. end
  334. else
  335. table.insert(cxxflags, "-std=c++14")
  336. if package:config("pic") ~= false then
  337. table.insert(cxxflags, "-fPIC")
  338. end
  339. end
  340. if package.has_runtime and package:has_runtime("c++_shared", "c++_static") then
  341. table.insert(cxxflags, "-stdlib=libc++")
  342. table.insert(linkflags, "-stdlib=libc++")
  343. if package:has_runtime("c++_static") then
  344. table.insert(linkflags, "-static-libstdc++")
  345. end
  346. end
  347. if package:config("asan") then
  348. table.insert(cxxflags, "-fsanitize=address")
  349. table.insert(linkflags, "-fsanitize=address")
  350. end
  351. if cxxflags then
  352. table.insert(argv, "cxxflags=" .. table.concat(cxxflags, " "))
  353. end
  354. if linkflags then
  355. table.insert(argv, "linkflags=" .. table.concat(linkflags, " "))
  356. end
  357. for _, libname in ipairs(libnames) do
  358. if package:config("all") or package:config(libname) then
  359. table.insert(argv, "--with-" .. libname)
  360. end
  361. end
  362. if package:is_plat("linux") then
  363. table.insert(argv, "pch=off")
  364. end
  365. if package:is_plat("windows") and package:version():le("1.85.0") then
  366. local vs_toolset = build_toolchain:config("vs_toolset")
  367. if vs_toolset then
  368. local vs_toolset_ver = import("core.base.semver").new(vs_toolset)
  369. local minor = vs_toolset_ver:minor()
  370. if minor and minor >= 40 then
  371. io.replace("tools/build/src/engine/config_toolset.bat", "vc143", "vc144", {plain = true})
  372. io.replace("tools/build/src/engine/build.bat", "vc143", "vc144", {plain = true})
  373. io.replace("tools/build/src/engine/guess_toolset.bat", "vc143", "vc144", {plain = true})
  374. io.replace("tools/build/src/tools/intel-win.jam", "14.3", "14.4", {plain = true})
  375. io.replace("tools/build/src/tools/msvc.jam", "14.3", "14.4", {plain = true})
  376. end
  377. end
  378. end
  379. local ok = os.execv("./b2", argv, {envs = runenvs, try = true, stdout = "boost-log.txt"})
  380. if ok ~= 0 then
  381. raise("boost build failed, please check log in " .. path.join(os.curdir(), "boost-log.txt"))
  382. end
  383. end)
  384. on_test(function (package)
  385. assert(package:check_cxxsnippets({test = [[
  386. #include <boost/algorithm/string.hpp>
  387. #include <string>
  388. #include <vector>
  389. static void test() {
  390. std::string str("a,b");
  391. std::vector<std::string> vec;
  392. boost::algorithm::split(vec, str, boost::algorithm::is_any_of(","));
  393. }
  394. ]]}, {configs = {languages = "c++14"}}))
  395. assert(package:check_cxxsnippets({test = [[
  396. #include <boost/unordered_map.hpp>
  397. static void test() {
  398. boost::unordered_map<std::string, int> map;
  399. map["2"] = 2;
  400. }
  401. ]]}, {configs = {languages = "c++14"}}))
  402. if package:config("date_time") then
  403. assert(package:check_cxxsnippets({test = [[
  404. #include <boost/date_time/gregorian/gregorian.hpp>
  405. static void test() {
  406. boost::gregorian::date d(2010, 1, 30);
  407. }
  408. ]]}, {configs = {languages = "c++14"}}))
  409. end
  410. if package:config("filesystem") then
  411. assert(package:check_cxxsnippets({test = [[
  412. #include <boost/filesystem.hpp>
  413. #include <iostream>
  414. static void test() {
  415. boost::filesystem::path path("/path/to/directory");
  416. if (boost::filesystem::exists(path)) {
  417. std::cout << "Directory exists" << std::endl;
  418. } else {
  419. std::cout << "Directory does not exist" << std::endl;
  420. }
  421. }
  422. ]]}, {configs = {languages = "c++14"}}))
  423. end
  424. if package:config("iostreams") then
  425. if package:config("zstd") then
  426. assert(package:check_cxxsnippets({test = [[
  427. #include <boost/iostreams/filter/zstd.hpp>
  428. #include <boost/iostreams/filtering_stream.hpp>
  429. static void test() {
  430. boost::iostreams::filtering_ostream out;
  431. out.push(boost::iostreams::zstd_compressor());
  432. }
  433. ]]}, {configs = {languages = "c++14"}}))
  434. end
  435. if package:config("lzma") then
  436. assert(package:check_cxxsnippets({test = [[
  437. #include <boost/iostreams/filter/lzma.hpp>
  438. #include <boost/iostreams/filtering_stream.hpp>
  439. static void test() {
  440. boost::iostreams::filtering_ostream out;
  441. out.push(boost::iostreams::lzma_compressor());
  442. }
  443. ]]}, {configs = {languages = "c++14"}}))
  444. end
  445. end
  446. end)