toolchain.lua 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462
  1. --
  2. -- Copyright (c) 2012-2023 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. "StaticRuntime",
  120. "NoPCH",
  121. "NoRTTI",
  122. "NoExceptions",
  123. "NoEditAndContinue",
  124. "NoFramePointer",
  125. "Symbols",
  126. }
  127. defines {
  128. "__STDC_CONSTANT_MACROS",
  129. "__STDC_FORMAT_MACROS",
  130. "__STDC_LIMIT_MACROS",
  131. }
  132. configuration { "debug" }
  133. targetsuffix "-debug"
  134. configuration { "development" }
  135. targetsuffix "-development"
  136. configuration { "release" }
  137. targetsuffix "-release"
  138. configuration { "development or release" }
  139. flags {
  140. "NoBufferSecurityCheck",
  141. "OptimizeSpeed",
  142. }
  143. configuration { "debug or development", "linux-*" }
  144. linkoptions {
  145. "-rdynamic"
  146. }
  147. configuration { "debug or development", "wasm" }
  148. linkoptions {
  149. "-gsource-map" -- See: https://emscripten.org/docs/porting/exceptions.html?highlight=gsource%20map#stack-traces
  150. }
  151. configuration { "linux-gcc or android-arm" }
  152. buildoptions {
  153. "-Werror=return-type",
  154. }
  155. configuration { "x32", "linux-gcc" }
  156. targetdir (build_dir .. "linux32" .. "/bin")
  157. objdir (build_dir .. "linux32" .. "/obj")
  158. libdirs {
  159. lib_dir .. "../build/linux32/bin",
  160. }
  161. configuration { "x64", "linux-gcc" }
  162. targetdir (build_dir .. "linux64" .. "/bin")
  163. objdir (build_dir .. "linux64" .. "/obj")
  164. libdirs {
  165. lib_dir .. "../build/linux64/bin",
  166. }
  167. configuration { "x64", "linux-clang" }
  168. targetdir (build_dir .. "linux64_clang" .. "/bin")
  169. objdir (build_dir .. "linux64_clang" .. "/obj")
  170. libdirs {
  171. lib_dir .. "../build/linux64_clang/bin",
  172. }
  173. configuration { "x32", "vs*" }
  174. targetdir (build_dir .. "windows32" .. "/bin")
  175. objdir (build_dir .. "windows32" .. "/obj")
  176. libdirs {
  177. lib_dir .. "../build/windows32/bin",
  178. }
  179. configuration { "x64", "vs*" }
  180. targetdir (build_dir .. "windows64" .. "/bin")
  181. objdir (build_dir .. "windows64" .. "/obj")
  182. libdirs {
  183. lib_dir .. "../build/windows64/bin",
  184. }
  185. configuration { "linux-gcc* or linux-clang" }
  186. buildoptions {
  187. "-Wall",
  188. "-Wextra",
  189. "-Wundef",
  190. "-msse2",
  191. }
  192. buildoptions_cpp {
  193. "-std=c++14",
  194. }
  195. links {
  196. "dl",
  197. }
  198. linkoptions {
  199. "-Wl,-rpath=\\$$ORIGIN",
  200. "-Wl,--no-as-needed",
  201. "-no-pie",
  202. }
  203. configuration { "android-*" }
  204. targetprefix("lib")
  205. flags {
  206. "NoImportLib"
  207. }
  208. includedirs {
  209. "$(ANDROID_NDK_ROOT)/sources/android/native_app_glue",
  210. }
  211. linkoptions {
  212. "-nostdlib",
  213. }
  214. links {
  215. "c",
  216. "dl",
  217. "m",
  218. "android",
  219. "log",
  220. "c++_shared",
  221. "gcc",
  222. }
  223. buildoptions {
  224. "-fPIC",
  225. "-no-canonical-prefixes",
  226. "-Wa,--noexecstack",
  227. "-fstack-protector-strong",
  228. "-ffunction-sections",
  229. "-Wunused-value",
  230. "-Wundef",
  231. }
  232. linkoptions {
  233. "-no-canonical-prefixes",
  234. "-Wl,--no-undefined",
  235. "-Wl,-z,noexecstack",
  236. "-Wl,-z,relro",
  237. "-Wl,-z,now",
  238. }
  239. configuration { "android-arm" }
  240. targetdir (build_dir .. "android-arm/bin")
  241. objdir (build_dir .. "android-arm/obj")
  242. libdirs {
  243. lib_dir .. "../build/android-arm/bin",
  244. "$(ANDROID_NDK_ROOT)/sources/cxx-stl/llvm-libc++/libs/armeabi-v7a",
  245. }
  246. includedirs {
  247. "$(ANDROID_NDK_ROOT)/sysroot/usr/include/arm-linux-androideabi", -- <asm/...>
  248. }
  249. buildoptions {
  250. "--target=armv7-none-linux-androideabi$(ANDROID_NDK_ABI)",
  251. "--gcc-toolchain=$(ANDROID_NDK_ROOT)/toolchains/llvm/prebuilt/linux-x86_64",
  252. "--sysroot=$(ANDROID_NDK_ROOT)/toolchains/llvm/prebuilt/linux-x86_64/sysroot",
  253. "-mthumb",
  254. "-march=armv7-a",
  255. "-mfloat-abi=softfp",
  256. "-mfpu=neon",
  257. "-Wunused-value",
  258. "-Wundef",
  259. }
  260. linkoptions {
  261. "--target=armv7-none-linux-androideabi$(ANDROID_NDK_ABI)",
  262. "--gcc-toolchain=$(ANDROID_NDK_ROOT)/toolchains/llvm/prebuilt/linux-x86_64",
  263. "--sysroot=$(ANDROID_NDK_ROOT)/toolchains/llvm/prebuilt/linux-x86_64/sysroot",
  264. "$(ANDROID_NDK_ROOT)/platforms/android-$(ANDROID_NDK_ABI)/arch-arm/usr/lib/crtbegin_so.o",
  265. "$(ANDROID_NDK_ROOT)/platforms/android-$(ANDROID_NDK_ABI)/arch-arm/usr/lib/crtend_so.o",
  266. "-march=armv7-a",
  267. "-Wl,--fix-cortex-a8",
  268. }
  269. configuration { "android-arm64" }
  270. targetdir (build_dir .. "android-arm64/bin")
  271. objdir (build_dir .. "android-arm64/obj")
  272. libdirs {
  273. "$(ANDROID_NDK_ROOT)/sources/cxx-stl/llvm-libc++/libs/arm64-v8a",
  274. }
  275. includedirs {
  276. "$(ANDROID_NDK_ROOT)/sysroot/usr/include/aarch64-linux-android",
  277. }
  278. buildoptions {
  279. "--target=aarch64-none-linux-androideabi$(ANDROID_NDK_ABI)",
  280. "--gcc-toolchain=$(ANDROID_NDK_ROOT)/toolchains/llvm/prebuilt/linux-x86_64",
  281. "--sysroot=$(ANDROID_NDK_ROOT)/toolchains/llvm/prebuilt/linux-x86_64/sysroot",
  282. "-march=armv8-a",
  283. "-Wunused-value",
  284. "-Wundef",
  285. }
  286. linkoptions {
  287. "--target=aarch64-none-linux-androideabi$(ANDROID_NDK_ABI)",
  288. "--gcc-toolchain=$(ANDROID_NDK_ROOT)/toolchains/llvm/prebuilt/linux-x86_64",
  289. "--sysroot=$(ANDROID_NDK_ROOT)/toolchains/llvm/prebuilt/linux-x86_64/sysroot",
  290. "$(ANDROID_NDK_ROOT)/platforms/android-$(ANDROID_NDK_ABI)/arch-arm64/usr/lib/crtbegin_so.o",
  291. "$(ANDROID_NDK_ROOT)/platforms/android-$(ANDROID_NDK_ABI)/arch-arm64/usr/lib/crtend_so.o",
  292. "-march=armv8-a",
  293. }
  294. configuration { "mingw-*" }
  295. defines { "WIN32" }
  296. includedirs { path.join(CROWN_DIR, "3rdparty/bx/include/compat/mingw") }
  297. buildoptions {
  298. "-fdata-sections",
  299. "-ffunction-sections",
  300. "-msse2",
  301. "-Wunused-value",
  302. "-Wundef",
  303. }
  304. buildoptions_cpp {
  305. "-std=c++14",
  306. }
  307. linkoptions {
  308. "-Wl,--gc-sections",
  309. "-static",
  310. "-static-libgcc",
  311. "-static-libstdc++",
  312. }
  313. configuration { "x32", "mingw-gcc" }
  314. targetdir (path.join(build_dir, "mingw32/bin"))
  315. objdir (path.join(build_dir, "mingw32/obj"))
  316. libdirs {
  317. lib_dir .. "../build/mingw32/bin",
  318. }
  319. buildoptions {
  320. "-m32",
  321. "-mstackrealign",
  322. }
  323. configuration { "x64", "mingw-gcc" }
  324. targetdir (path.join(build_dir, "mingw64/bin"))
  325. objdir (path.join(build_dir, "mingw64/obj"))
  326. libdirs {
  327. lib_dir .. "../build/mingw64/bin",
  328. }
  329. buildoptions { "-m64" }
  330. configuration { "wasm" }
  331. targetdir (path.join(build_dir, "wasm/bin"))
  332. objdir (path.join(build_dir, "wasm/obj"))
  333. libdirs { path.join(lib_dir, "lib/wasm") }
  334. buildoptions {
  335. "-Wunused-value",
  336. "-Wundef",
  337. "-pthread",
  338. }
  339. configuration { "vs*" }
  340. includedirs { CROWN_DIR .. "3rdparty/bx/include/compat/msvc" }
  341. defines {
  342. "WIN32",
  343. "_WIN32",
  344. "_HAS_EXCEPTIONS=0",
  345. "_HAS_ITERATOR_DEBUGGING=0",
  346. "_SCL_SECURE=0",
  347. "_SECURE_SCL=0",
  348. "_SCL_SECURE_NO_WARNINGS",
  349. "_CRT_SECURE_NO_WARNINGS",
  350. "_CRT_SECURE_NO_DEPRECATE",
  351. "NOMINMAX",
  352. }
  353. buildoptions {
  354. "/Ob2", -- The Inline Function Expansion
  355. "/we4715", -- Not all control paths return a value
  356. }
  357. linkoptions {
  358. "/ignore:4199", -- LNK4199: /DELAYLOAD:*.dll ignored; no imports found from *.dll
  359. "/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
  360. "/ignore:4099", -- LNK4099: The linker was unable to find your .pdb file.
  361. }
  362. configuration { "x32", "vs*" }
  363. targetdir (build_dir .. "windows32" .. "/bin")
  364. objdir (build_dir .. "windows32" .. "/obj")
  365. configuration { "x64", "vs*" }
  366. defines {
  367. "_WIN64",
  368. }
  369. targetdir (build_dir .. "windows64" .. "/bin")
  370. objdir (build_dir .. "windows64" .. "/obj")
  371. configuration {} -- reset configuration
  372. end
  373. function strip()
  374. configuration { "android-arm", "release"}
  375. postbuildcommands {
  376. "$(SILENT) echo Stripping symbols",
  377. "$(SILENT) $(ANDROID_NDK_ROOT)/toolchains/llvm/prebuilt/linux-x86_64/bin/arm-linux-androideabi-strip -s \"$(TARGET)\""
  378. }
  379. configuration { "android-arm64", "release"}
  380. postbuildcommands {
  381. "$(SILENT) echo Stripping symbols",
  382. "$(SILENT) $(ANDROID_NDK_ROOT)/toolchains/llvm/prebuilt/linux-x86_64/bin/aarch64-linux-android-strip -s \"$(TARGET)\""
  383. }
  384. configuration { "linux-*", "release" }
  385. postbuildcommands {
  386. "$(SILENT) echo Stripping symbols",
  387. "$(SILENT) strip -s \"$(TARGET)\""
  388. }
  389. configuration { "mingw*", "Release" }
  390. postbuildcommands {
  391. "$(SILENT) echo Stripping symbols.",
  392. "$(SILENT) $(MINGW)/bin/strip -s \"$(TARGET)\""
  393. }
  394. configuration {} -- reset configuration
  395. end