xmake.lua 22 KB

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