toolchain.lua 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435
  1. --
  2. -- Copyright (c) 2012-2025 Daniele Bartolini et al.
  3. -- SPDX-License-Identifier: MIT
  4. --
  5. -- Based on 3rdparty/bx/scripts/toolchain.lua by Branimir Karadzic.
  6. local android = {};
  7. local function androidToolchainRoot()
  8. if android.toolchainRoot == nil then
  9. local hostTags = {
  10. windows = "windows-x86_64",
  11. linux = "linux-x86_64",
  12. macosx = "darwin-x86_64"
  13. }
  14. android.toolchainRoot = "$(ANDROID_NDK_ROOT)/toolchains/llvm/prebuilt/" .. hostTags[os.get()]
  15. end
  16. return android.toolchainRoot;
  17. end
  18. function toolchain(build_dir, lib_dir)
  19. newoption
  20. {
  21. trigger = "compiler",
  22. value = "COMPILER",
  23. description = "Choose compiler",
  24. allowed =
  25. {
  26. { "android-arm", "Android - ARM" },
  27. { "android-arm64", "Android - ARM64" },
  28. { "linux-gcc", "Linux (GCC compiler)" },
  29. { "linux-clang", "Linux (Clang compiler)" },
  30. { "mingw-gcc", "MinGW (GCC compiler)" },
  31. { "wasm", "emscripten/wasm" },
  32. }
  33. }
  34. newoption {
  35. trigger = "with-32bit-compiler",
  36. description = "Use 32-bit compiler instead 64-bit.",
  37. }
  38. if (_ACTION == nil) then return end
  39. if _ACTION == "clean" then
  40. os.rmdir(BUILD_DIR)
  41. end
  42. local compiler32bit = false
  43. if _OPTIONS["with-32bit-compiler"] then
  44. compiler32bit = true
  45. end
  46. if _ACTION == "gmake" then
  47. if nil == _OPTIONS["compiler"] then
  48. print("Choose a compiler!")
  49. os.exit(1)
  50. end
  51. if "android-arm" == _OPTIONS["compiler"] then
  52. if not os.getenv("ANDROID_NDK_ABI") then
  53. print("Set ANDROID_NDK_ABI environment variable.")
  54. end
  55. if not os.getenv("ANDROID_NDK_ROOT") then
  56. print("Set ANDROID_NDK_ROOT environment variable.")
  57. end
  58. premake.gcc.cc = androidToolchainRoot() .. "/bin/clang"
  59. premake.gcc.cxx = androidToolchainRoot() .. "/bin/clang++"
  60. premake.gcc.ar = androidToolchainRoot() .. "/bin/llvm-ar"
  61. premake.gcc.llvm = true
  62. location(build_dir .. "projects/android-arm")
  63. elseif "android-arm64" == _OPTIONS["compiler"] then
  64. if not os.getenv("ANDROID_NDK_ABI") then
  65. print("Set ANDROID_NDK_ABI environment variable.")
  66. end
  67. if not os.getenv("ANDROID_NDK_ROOT") then
  68. print("Set ANDROID_NDK_ROOT environment variable.")
  69. end
  70. premake.gcc.cc = androidToolchainRoot() .. "/bin/clang"
  71. premake.gcc.cxx = androidToolchainRoot() .. "/bin/clang++"
  72. premake.gcc.ar = androidToolchainRoot() .. "/bin/llvm-ar"
  73. premake.gcc.llvm = true
  74. location(build_dir .. "projects/android-arm64")
  75. elseif "linux-gcc" == _OPTIONS["compiler"] then
  76. if not os.is("linux") then
  77. print("Action not valid in current OS.")
  78. end
  79. location(build_dir .. "projects/linux")
  80. elseif "linux-clang" == _OPTIONS["compiler"] then
  81. if not os.is("linux") then
  82. print("Action not valid in current OS.")
  83. end
  84. premake.gcc.cc = "clang"
  85. premake.gcc.cxx = "clang++"
  86. premake.gcc.ar = "ar"
  87. location(build_dir .. "projects/linux-clang")
  88. elseif "mingw-gcc" == _OPTIONS["compiler"] then
  89. if not os.getenv("MINGW") then
  90. print("Set MINGW environment variable.")
  91. os.exit(1)
  92. end
  93. local mingwToolchain = "x86_64-w64-mingw32"
  94. if compiler32bit then
  95. mingwToolchain = "i686-w64-mingw32"
  96. end
  97. premake.gcc.cc = "$(MINGW)/bin/" .. mingwToolchain .. "-gcc"
  98. premake.gcc.cxx = "$(MINGW)/bin/" .. mingwToolchain .. "-g++"
  99. premake.gcc.ar = "$(MINGW)/bin/ar"
  100. premake.valac.cc = premake.gcc.cc
  101. location(build_dir .. "projects/mingw")
  102. elseif "wasm" == _OPTIONS["compiler"] then
  103. if not os.getenv("EMSCRIPTEN") then
  104. print("Set EMSCRIPTEN environment variable.")
  105. os.exit(1)
  106. end
  107. premake.gcc.cc = "$(EMSCRIPTEN)/emcc"
  108. premake.gcc.cxx = "$(EMSCRIPTEN)/em++"
  109. premake.gcc.ar = "$(EMSCRIPTEN)/emar"
  110. premake.gcc.llvm = true
  111. location(build_dir .. "projects/wasm")
  112. end
  113. elseif _ACTION == "vs2019"
  114. or _ACTION == "vs2022"
  115. then
  116. if not os.is("windows") then
  117. print("Action not valid in current OS.")
  118. end
  119. local winKitVer = os.getenv("WindowsSDKVersion")
  120. if not winKitVer then
  121. print("Run vcvarsall.bat as part of your Visual Studio installation to set the environment variable 'WindowsSDKVersion'.")
  122. os.exit(1)
  123. end
  124. winKitVer = string.gsub(winKitVer, "\\", "")
  125. local action = premake.action.current()
  126. action.vstudio.windowsTargetPlatformVersion = winKitVer
  127. action.vstudio.windowsTargetPlatformMinVersion = winKitVer
  128. location(build_dir .. "projects/" .. _ACTION)
  129. end
  130. flags {
  131. "Cpp20",
  132. "StaticRuntime",
  133. "NoPCH",
  134. "NoRTTI",
  135. "NoExceptions",
  136. "NoEditAndContinue",
  137. "NoFramePointer",
  138. "Symbols",
  139. }
  140. defines {
  141. "__STDC_CONSTANT_MACROS",
  142. "__STDC_FORMAT_MACROS",
  143. "__STDC_LIMIT_MACROS",
  144. }
  145. configuration { "debug" }
  146. targetsuffix "-debug"
  147. configuration { "development" }
  148. targetsuffix "-development"
  149. configuration { "release" }
  150. targetsuffix "-release"
  151. configuration { "development or release" }
  152. flags {
  153. "NoBufferSecurityCheck",
  154. "OptimizeSpeed",
  155. }
  156. configuration { "debug or development", "linux-*" }
  157. linkoptions {
  158. "-rdynamic"
  159. }
  160. configuration { "debug or development", "wasm" }
  161. linkoptions {
  162. "-gsource-map" -- See: https://emscripten.org/docs/porting/exceptions.html?highlight=gsource%20map#stack-traces
  163. }
  164. configuration { "linux-gcc or android-arm" }
  165. buildoptions {
  166. "-Werror=return-type",
  167. }
  168. configuration { "x32", "linux-gcc" }
  169. targetdir (build_dir .. "linux32" .. "/bin")
  170. objdir (build_dir .. "linux32" .. "/obj")
  171. libdirs {
  172. lib_dir .. "../build/linux32/bin",
  173. }
  174. configuration { "x64", "linux-gcc" }
  175. targetdir (build_dir .. "linux64" .. "/bin")
  176. objdir (build_dir .. "linux64" .. "/obj")
  177. libdirs {
  178. lib_dir .. "../build/linux64/bin",
  179. }
  180. configuration { "x64", "linux-clang" }
  181. targetdir (build_dir .. "linux64_clang" .. "/bin")
  182. objdir (build_dir .. "linux64_clang" .. "/obj")
  183. libdirs {
  184. lib_dir .. "../build/linux64_clang/bin",
  185. }
  186. configuration { "x32", "vs*" }
  187. targetdir (build_dir .. "windows32" .. "/bin")
  188. objdir (build_dir .. "windows32" .. "/obj")
  189. libdirs {
  190. lib_dir .. "../build/windows32/bin",
  191. }
  192. configuration { "x64", "vs*" }
  193. targetdir (build_dir .. "windows64" .. "/bin")
  194. objdir (build_dir .. "windows64" .. "/obj")
  195. libdirs {
  196. lib_dir .. "../build/windows64/bin",
  197. }
  198. configuration { "linux-gcc* or linux-clang" }
  199. buildoptions {
  200. "-Wall",
  201. "-Wextra",
  202. "-Wundef",
  203. "-msse2",
  204. }
  205. links {
  206. "dl",
  207. }
  208. linkoptions {
  209. "-Wl,-rpath=\\$$ORIGIN",
  210. "-Wl,--no-as-needed",
  211. "-no-pie",
  212. }
  213. configuration { "android-*" }
  214. targetprefix("lib")
  215. flags {
  216. "NoImportLib"
  217. }
  218. links {
  219. "c",
  220. "dl",
  221. "m",
  222. "android",
  223. "log",
  224. "c++_shared",
  225. }
  226. buildoptions {
  227. "--gcc-toolchain=" .. androidToolchainRoot(),
  228. "--sysroot=" .. androidToolchainRoot() .. "/sysroot",
  229. --"-DANDROID",
  230. "-fPIC",
  231. "-no-canonical-prefixes",
  232. "-Wa,--noexecstack",
  233. "-fstack-protector-strong",
  234. "-ffunction-sections",
  235. "-Wunused-value",
  236. "-Wundef",
  237. }
  238. linkoptions {
  239. "--gcc-toolchain=" .. androidToolchainRoot(),
  240. "--sysroot=" .. androidToolchainRoot() .. "/sysroot",
  241. "-no-canonical-prefixes",
  242. "-Wl,--no-undefined",
  243. "-Wl,-z,noexecstack",
  244. "-Wl,-z,relro",
  245. "-Wl,-z,now",
  246. }
  247. configuration { "android-arm" }
  248. targetdir (build_dir .. "android-arm/bin")
  249. objdir (build_dir .. "android-arm/obj")
  250. buildoptions {
  251. "--target=armv7-none-linux-android" .. "$(ANDROID_NDK_ABI)",
  252. "-mthumb",
  253. "-march=armv7-a",
  254. "-mfloat-abi=softfp",
  255. "-mfpu=neon",
  256. }
  257. linkoptions {
  258. "--target=armv7-none-linux-android" .. "$(ANDROID_NDK_ABI)",
  259. "-march=armv7-a",
  260. }
  261. configuration { "android-arm64" }
  262. targetdir (build_dir .. "android-arm64/bin")
  263. objdir (build_dir .. "android-arm64/obj")
  264. buildoptions {
  265. "--target=aarch64-none-linux-android" .. "$(ANDROID_NDK_ABI)",
  266. }
  267. linkoptions {
  268. "--target=aarch64-none-linux-android" .. "$(ANDROID_NDK_ABI)",
  269. }
  270. configuration { "mingw-*" }
  271. defines { "WIN32" }
  272. includedirs { path.join(CROWN_DIR, "3rdparty/bx/include/compat/mingw") }
  273. buildoptions {
  274. "-fdata-sections",
  275. "-ffunction-sections",
  276. "-msse2",
  277. "-Wunused-value",
  278. "-Wundef",
  279. }
  280. linkoptions {
  281. "-Wl,--gc-sections",
  282. "-static",
  283. "-static-libgcc",
  284. "-static-libstdc++",
  285. }
  286. configuration { "x32", "mingw-gcc" }
  287. targetdir (path.join(build_dir, "mingw32/bin"))
  288. objdir (path.join(build_dir, "mingw32/obj"))
  289. libdirs {
  290. lib_dir .. "../build/mingw32/bin",
  291. }
  292. buildoptions {
  293. "-m32",
  294. "-mstackrealign",
  295. }
  296. configuration { "x64", "mingw-gcc" }
  297. targetdir (path.join(build_dir, "mingw64/bin"))
  298. objdir (path.join(build_dir, "mingw64/obj"))
  299. libdirs {
  300. lib_dir .. "../build/mingw64/bin",
  301. }
  302. buildoptions { "-m64" }
  303. configuration { "wasm" }
  304. targetdir (path.join(build_dir, "wasm/bin"))
  305. objdir (path.join(build_dir, "wasm/obj"))
  306. libdirs { path.join(lib_dir, "lib/wasm") }
  307. buildoptions {
  308. "-Wunused-value",
  309. "-Wundef",
  310. "-pthread",
  311. }
  312. configuration { "vs*" }
  313. includedirs { CROWN_DIR .. "3rdparty/bx/include/compat/msvc" }
  314. defines {
  315. "WIN32",
  316. "_WIN32",
  317. "_HAS_EXCEPTIONS=0",
  318. "_HAS_ITERATOR_DEBUGGING=0",
  319. "_SCL_SECURE=0",
  320. "_SECURE_SCL=0",
  321. "_SCL_SECURE_NO_WARNINGS",
  322. "_CRT_SECURE_NO_WARNINGS",
  323. "_CRT_SECURE_NO_DEPRECATE",
  324. "NOMINMAX",
  325. }
  326. buildoptions {
  327. "/Ob2", -- The Inline Function Expansion
  328. "/we4715", -- Not all control paths return a value
  329. "/Zc:__cplusplus", -- https://learn.microsoft.com/en-us/cpp/build/reference/zc-cplusplus?view=msvc-170
  330. "/Zc:preprocessor", -- Enable preprocessor conformance mode.
  331. }
  332. linkoptions {
  333. "/ignore:4199", -- LNK4199: /DELAYLOAD:*.dll ignored; no imports found from *.dll
  334. "/ignore:4221", -- LNK4221: This object file does not define any previously undefined public symbols, so it will not be used by any link operation that consumes this library
  335. "/ignore:4099", -- LNK4099: The linker was unable to find your .pdb file.
  336. }
  337. configuration { "x32", "vs*" }
  338. targetdir (build_dir .. "windows32" .. "/bin")
  339. objdir (build_dir .. "windows32" .. "/obj")
  340. configuration { "x64", "vs*" }
  341. defines {
  342. "_WIN64",
  343. }
  344. targetdir (build_dir .. "windows64" .. "/bin")
  345. objdir (build_dir .. "windows64" .. "/obj")
  346. configuration {} -- reset configuration
  347. end
  348. function strip()
  349. configuration { "android-*", "release"}
  350. postbuildcommands {
  351. "$(SILENT) echo Stripping symbols",
  352. "$(SILENT) " .. androidToolchainRoot() .. "/bin/llvm-strip -s \"$(TARGET)\""
  353. }
  354. configuration { "linux-*", "release" }
  355. postbuildcommands {
  356. "$(SILENT) echo Stripping symbols",
  357. "$(SILENT) strip -s \"$(TARGET)\""
  358. }
  359. configuration { "mingw*", "Release" }
  360. postbuildcommands {
  361. "$(SILENT) echo Stripping symbols.",
  362. "$(SILENT) $(MINGW)/bin/strip -s \"$(TARGET)\""
  363. }
  364. configuration {} -- reset configuration
  365. end