install.lua 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. import("core.tool.toolchain")
  2. import("core.base.option")
  3. function _get_compiler(package, toolchain)
  4. local cxx = package:build_getenv("cxx")
  5. if package:is_plat("macosx") then
  6. -- we uses ld/clang++ for link stdc++ for shared libraries
  7. -- and we need `xcrun -sdk macosx clang++` to make b2 to get `-isysroot` automatically
  8. local cc = package:build_getenv("ld")
  9. if cc and cc:find("clang", 1, true) and cc:find("Xcode", 1, true) then
  10. cc = "xcrun -sdk macosx clang++"
  11. end
  12. return format("using darwin : : %s ;", cc)
  13. elseif package:is_plat("windows") then
  14. local vs_toolset = toolchain:config("vs_toolset")
  15. local msvc_ver = ""
  16. local win_toolset = "msvc"
  17. if toolchain:name() == "clang-cl" then
  18. win_toolset = "clang-win"
  19. cxx = cxx:gsub("(clang%-cl)$", "%1.exe", 1)
  20. msvc_ver = ""
  21. elseif vs_toolset then
  22. local i = vs_toolset:find("%.")
  23. msvc_ver = i and vs_toolset:sub(1, i + 1)
  24. end
  25. -- Specifying a version will disable b2 from forcing tools
  26. -- from the latest installed msvc version.
  27. return format("using %s : %s : \"%s\" ;", win_toolset, msvc_ver, cxx:gsub("\\", "\\\\"))
  28. else
  29. cxx = cxx:gsub("gcc$", "g++")
  30. cxx = cxx:gsub("gcc%-", "g++-")
  31. cxx = cxx:gsub("clang$", "clang++")
  32. cxx = cxx:gsub("clang%-", "clang++-")
  33. if cxx and cxx:find("clang", 1, true) then
  34. return format("using clang : : \"%s\" ;", cxx:gsub("\\", "/"))
  35. else
  36. return format("using gcc : : \"%s\" ;", cxx:gsub("\\", "/"))
  37. end
  38. end
  39. end
  40. function _config_deppath(package, file, depname, rule)
  41. local dep = package:dep(depname)
  42. local info = dep:fetch({external = false})
  43. if info then
  44. local includedirs = table.wrap(info.sysincludedirs or info.includedirs)
  45. for i, dir in ipairs(includedirs) do
  46. includedirs[i] = path.unix(dir)
  47. end
  48. local linkdirs = table.wrap(info.linkdirs)
  49. for i, dir in ipairs(linkdirs) do
  50. linkdirs[i] = path.unix(dir)
  51. end
  52. local links = table.wrap(info.links)
  53. local usingstr = format("\nusing %s : %s : <include>%s <search>%s <name>%s ;",
  54. rule, dep:version(),
  55. table.concat(includedirs, ";"),
  56. table.concat(linkdirs, ";"),
  57. table.concat(links, ";"))
  58. file:write(usingstr)
  59. end
  60. end
  61. function main(package)
  62. import("libs", {rootdir = package:scriptdir()})
  63. -- get host toolchain
  64. local host_toolchain
  65. if package:is_plat("windows") then
  66. host_toolchain = toolchain.load("msvc", {plat = "windows", arch = os.arch()})
  67. if not host_toolchain:check() then
  68. host_toolchain = toolchain.load("clang-cl", {plat = "windows", arch = os.arch()})
  69. end
  70. assert(host_toolchain:check(), "host msvc or clang-cl not found!")
  71. end
  72. -- force boost to compile with the desired compiler
  73. local file = io.open("user-config.jam", "w")
  74. if file then
  75. file:write(_get_compiler(package, host_toolchain))
  76. file:close()
  77. end
  78. local bootstrap_argv =
  79. {
  80. "--prefix=" .. package:installdir(),
  81. "--libdir=" .. package:installdir("lib"),
  82. "--without-icu"
  83. }
  84. if package:has_tool("cxx", "clang", "clangxx") then
  85. table.insert(bootstrap_argv, "--with-toolset=clang")
  86. end
  87. if package:is_plat("windows") then
  88. -- for bootstrap.bat, all other arguments are useless
  89. bootstrap_argv = { "msvc" }
  90. os.vrunv("bootstrap.bat", bootstrap_argv, {envs = host_toolchain:runenvs()})
  91. elseif package:is_plat("mingw") and is_host("windows") then
  92. bootstrap_argv = { "gcc" }
  93. os.vrunv("bootstrap.bat", bootstrap_argv)
  94. -- todo looking for better solution to fix the confict between user-config.jam and project-config.jam
  95. io.replace("project-config.jam", "using[^\n]+", "")
  96. else
  97. os.vrunv("./bootstrap.sh", bootstrap_argv)
  98. end
  99. -- get build toolchain
  100. local build_toolchain
  101. local build_toolset
  102. local runenvs
  103. if package:is_plat("windows") then
  104. if package:has_tool("cxx", "clang_cl") then
  105. build_toolset = "clang-win"
  106. build_toolchain = package:toolchain("clang-cl")
  107. elseif package:has_tool("cxx", "clang") then
  108. build_toolset = "clang-win"
  109. build_toolchain = package:toolchain("clang") or package:toolchain("llvm")
  110. elseif package:has_tool("cxx", "cl") then
  111. build_toolset = "msvc"
  112. build_toolchain = package:toolchain("msvc")
  113. end
  114. if build_toolchain then
  115. runenvs = build_toolchain:runenvs()
  116. end
  117. end
  118. local file = io.open("user-config.jam", "w")
  119. if file then
  120. file:write(_get_compiler(package, build_toolchain))
  121. if package:config("lzma") then
  122. _config_deppath(package, file, "xz", "lzma")
  123. end
  124. if package:config("zstd") then
  125. _config_deppath(package, file, "zstd", "zstd")
  126. end
  127. if package:config("zlib") then
  128. _config_deppath(package, file, "zlib", "zlib")
  129. end
  130. if package:config("bzip2") then
  131. _config_deppath(package, file, "bzip2", "bzip2")
  132. end
  133. file:close()
  134. end
  135. os.vrun("./b2 headers")
  136. local njobs = option.get("jobs") or tostring(os.default_njob())
  137. local argv =
  138. {
  139. "--prefix=" .. package:installdir(),
  140. "--libdir=" .. package:installdir("lib"),
  141. "-d2",
  142. "-j" .. njobs,
  143. "--hash",
  144. "-q", -- quit on first error
  145. "--layout=tagged-1.66", -- prevent -x64 suffix in case cmake can't find it
  146. "--user-config=user-config.jam",
  147. "install",
  148. "threading=" .. (package:config("multi") and "multi" or "single"),
  149. "debug-symbols=" .. (package:debug() and "on" or "off"),
  150. "link=" .. (package:config("shared") and "shared" or "static"),
  151. "variant=" .. (package:is_debug() and "debug" or "release"),
  152. "runtime-debugging=" .. (package:is_debug() and "on" or "off")
  153. }
  154. local cxxflags = {}
  155. if package:config("lzma") then
  156. if package:is_plat("windows") and not package:dep("xz"):config("shared") then
  157. table.insert(cxxflags, "-DLZMA_API_STATIC")
  158. end
  159. else
  160. table.insert(argv, "-sNO_LZMA=1")
  161. end
  162. if not package:config("zstd") then
  163. table.insert(argv, "-sNO_ZSTD=1")
  164. end
  165. if not package:config("zlib") then
  166. table.insert(argv, "-sNO_ZLIB=1")
  167. end
  168. if not package:config("bzip2") then
  169. table.insert(argv, "-sNO_BZIP2=1")
  170. end
  171. if package:config("lto") then
  172. table.insert(argv, "lto=on")
  173. end
  174. if package:is_arch("aarch64", "arm+.*") then
  175. table.insert(argv, "architecture=arm")
  176. end
  177. if package:is_arch(".+64.*") then
  178. table.insert(argv, "address-model=64")
  179. else
  180. table.insert(argv, "address-model=32")
  181. end
  182. local linkflags = {}
  183. table.join2(cxxflags, table.wrap(package:config("cxflags")))
  184. table.join2(cxxflags, table.wrap(package:config("cxxflags")))
  185. if package:is_plat("windows") then
  186. if package:config("shared") then
  187. table.insert(argv, "runtime-link=shared")
  188. elseif package:has_runtime("MT", "MTd") then
  189. table.insert(argv, "runtime-link=static")
  190. else
  191. table.insert(argv, "runtime-link=shared")
  192. end
  193. table.insert(argv, "toolset=" .. build_toolset)
  194. table.insert(cxxflags, "-std:c++14")
  195. elseif package:is_plat("mingw") then
  196. table.insert(argv, "toolset=gcc")
  197. elseif package:is_plat("macosx") then
  198. table.insert(argv, "toolset=darwin")
  199. -- fix macosx arm64 build issue https://github.com/microsoft/vcpkg/pull/18529
  200. table.insert(cxxflags, "-std=c++14")
  201. table.insert(cxxflags, "-arch")
  202. table.insert(cxxflags, package:arch())
  203. local xcode = package:toolchain("xcode") or import("core.tool.toolchain").load("xcode", {plat = package:plat(), arch = package:arch()})
  204. if xcode:check() then
  205. local xcode_dir = xcode:config("xcode")
  206. local xcode_sdkver = xcode:config("xcode_sdkver")
  207. local target_minver = xcode:config("target_minver")
  208. if xcode_dir and xcode_sdkver then
  209. local xcode_sdkdir = xcode_dir .. "/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX" .. xcode_sdkver .. ".sdk"
  210. table.insert(cxxflags, "-isysroot")
  211. table.insert(cxxflags, xcode_sdkdir)
  212. end
  213. if target_minver then
  214. table.insert(cxxflags, "-mmacosx-version-min=" .. target_minver)
  215. end
  216. end
  217. else
  218. table.insert(cxxflags, "-std=c++14")
  219. if package:config("pic") ~= false then
  220. table.insert(cxxflags, "-fPIC")
  221. end
  222. end
  223. if package.has_runtime and package:has_runtime("c++_shared", "c++_static") then
  224. table.insert(cxxflags, "-stdlib=libc++")
  225. table.insert(linkflags, "-stdlib=libc++")
  226. if package:has_runtime("c++_static") then
  227. table.insert(linkflags, "-static-libstdc++")
  228. end
  229. end
  230. if package:config("asan") then
  231. table.insert(cxxflags, "-fsanitize=address")
  232. table.insert(linkflags, "-fsanitize=address")
  233. end
  234. if cxxflags then
  235. table.insert(argv, "cxxflags=" .. table.concat(cxxflags, " "))
  236. end
  237. if linkflags then
  238. table.insert(argv, "linkflags=" .. table.concat(linkflags, " "))
  239. end
  240. libs.for_each(function (libname)
  241. if package:config("all") or package:config(libname) then
  242. table.insert(argv, "--with-" .. libname)
  243. end
  244. end)
  245. if package:is_plat("linux") then
  246. table.insert(argv, "pch=off")
  247. end
  248. local ok = os.execv("./b2", argv, {envs = runenvs, try = true, stdout = "boost-log.txt"})
  249. if ok ~= 0 then
  250. raise("boost build failed, please check log in " .. path.join(os.curdir(), "boost-log.txt"))
  251. end
  252. end