toolchain.lua 11 KB

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