toolchain.lua 12 KB

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