toolchain.lua 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  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 windowsPlatform = string.gsub(os.getenv("WindowsSDKVersion") or "8.1", "\\", "")
  84. local action = premake.action.current()
  85. action.vstudio.windowsTargetPlatformVersion = windowsPlatform
  86. action.vstudio.windowsTargetPlatformMinVersion = windowsPlatform
  87. location(build_dir .. "projects/" .. _ACTION)
  88. end
  89. flags {
  90. "StaticRuntime",
  91. "NoPCH",
  92. "NoRTTI",
  93. "NoExceptions",
  94. "NoEditAndContinue",
  95. "NoFramePointer",
  96. "Symbols",
  97. }
  98. defines {
  99. "__STDC_CONSTANT_MACROS",
  100. "__STDC_FORMAT_MACROS",
  101. "__STDC_LIMIT_MACROS",
  102. }
  103. configuration { "debug" }
  104. targetsuffix "-debug"
  105. configuration { "development" }
  106. targetsuffix "-development"
  107. configuration { "release" }
  108. targetsuffix "-release"
  109. configuration { "development or release" }
  110. flags {
  111. "NoBufferSecurityCheck",
  112. "OptimizeSpeed",
  113. }
  114. configuration { "debug or development", "linux-*" }
  115. linkoptions {
  116. "-rdynamic"
  117. }
  118. configuration { "linux-gcc or android-arm" }
  119. buildoptions {
  120. "-Werror=return-type",
  121. }
  122. configuration { "x32", "linux-gcc" }
  123. targetdir (build_dir .. "linux32" .. "/bin")
  124. objdir (build_dir .. "linux32" .. "/obj")
  125. libdirs {
  126. lib_dir .. "../build/linux32/bin",
  127. }
  128. configuration { "x64", "linux-gcc" }
  129. targetdir (build_dir .. "linux64" .. "/bin")
  130. objdir (build_dir .. "linux64" .. "/obj")
  131. libdirs {
  132. lib_dir .. "../build/linux64/bin",
  133. }
  134. configuration { "x64", "linux-clang" }
  135. targetdir (build_dir .. "linux64_clang" .. "/bin")
  136. objdir (build_dir .. "linux64_clang" .. "/obj")
  137. libdirs {
  138. lib_dir .. "../build/linux64_clang/bin",
  139. }
  140. configuration { "x32", "vs*" }
  141. targetdir (build_dir .. "windows32" .. "/bin")
  142. objdir (build_dir .. "windows32" .. "/obj")
  143. libdirs {
  144. lib_dir .. "../build/windows32/bin",
  145. }
  146. configuration { "x64", "vs*" }
  147. targetdir (build_dir .. "windows64" .. "/bin")
  148. objdir (build_dir .. "windows64" .. "/obj")
  149. libdirs {
  150. lib_dir .. "../build/windows64/bin",
  151. }
  152. configuration { "linux-gcc* or linux-clang" }
  153. buildoptions {
  154. "-Wall",
  155. "-Wextra",
  156. "-Wundef",
  157. "-msse2",
  158. }
  159. buildoptions_cpp {
  160. "-std=c++14",
  161. }
  162. links {
  163. "dl",
  164. }
  165. linkoptions {
  166. "-Wl,-rpath=\\$$ORIGIN",
  167. "-Wl,--no-as-needed",
  168. "-no-pie",
  169. }
  170. configuration { "android-*" }
  171. targetprefix("lib")
  172. flags {
  173. "NoImportLib"
  174. }
  175. includedirs {
  176. "$(ANDROID_NDK_ROOT)/sources/android/native_app_glue",
  177. }
  178. linkoptions {
  179. "-nostdlib",
  180. }
  181. links {
  182. "c",
  183. "dl",
  184. "m",
  185. "android",
  186. "log",
  187. "c++_shared",
  188. "gcc",
  189. }
  190. buildoptions {
  191. "-fPIC",
  192. "-no-canonical-prefixes",
  193. "-Wa,--noexecstack",
  194. "-fstack-protector-strong",
  195. "-ffunction-sections",
  196. "-Wunused-value",
  197. "-Wundef",
  198. }
  199. linkoptions {
  200. "-no-canonical-prefixes",
  201. "-Wl,--no-undefined",
  202. "-Wl,-z,noexecstack",
  203. "-Wl,-z,relro",
  204. "-Wl,-z,now",
  205. }
  206. configuration { "android-arm" }
  207. targetdir (build_dir .. "android-arm/bin")
  208. objdir (build_dir .. "android-arm/obj")
  209. libdirs {
  210. lib_dir .. "../build/android-arm/bin",
  211. "$(ANDROID_NDK_ROOT)/sources/cxx-stl/llvm-libc++/libs/armeabi-v7a",
  212. }
  213. includedirs {
  214. "$(ANDROID_NDK_ROOT)/sysroot/usr/include/arm-linux-androideabi", -- <asm/...>
  215. }
  216. buildoptions {
  217. "--target=armv7-none-linux-androideabi$(ANDROID_NDK_ABI)",
  218. "--gcc-toolchain=$(ANDROID_NDK_ROOT)/toolchains/llvm/prebuilt/linux-x86_64",
  219. "--sysroot=$(ANDROID_NDK_ROOT)/toolchains/llvm/prebuilt/linux-x86_64/sysroot",
  220. "-mthumb",
  221. "-march=armv7-a",
  222. "-mfloat-abi=softfp",
  223. "-mfpu=neon",
  224. "-Wunused-value",
  225. "-Wundef",
  226. }
  227. linkoptions {
  228. "--target=armv7-none-linux-androideabi$(ANDROID_NDK_ABI)",
  229. "--gcc-toolchain=$(ANDROID_NDK_ROOT)/toolchains/llvm/prebuilt/linux-x86_64",
  230. "--sysroot=$(ANDROID_NDK_ROOT)/toolchains/llvm/prebuilt/linux-x86_64/sysroot",
  231. "$(ANDROID_NDK_ROOT)/platforms/android-$(ANDROID_NDK_ABI)/arch-arm/usr/lib/crtbegin_so.o",
  232. "$(ANDROID_NDK_ROOT)/platforms/android-$(ANDROID_NDK_ABI)/arch-arm/usr/lib/crtend_so.o",
  233. "-march=armv7-a",
  234. "-Wl,--fix-cortex-a8",
  235. }
  236. configuration { "android-arm64" }
  237. targetdir (build_dir .. "android-arm64/bin")
  238. objdir (build_dir .. "android-arm64/obj")
  239. libdirs {
  240. "$(ANDROID_NDK_ROOT)/sources/cxx-stl/llvm-libc++/libs/arm64-v8a",
  241. }
  242. includedirs {
  243. "$(ANDROID_NDK_ROOT)/sysroot/usr/include/aarch64-linux-android",
  244. }
  245. buildoptions {
  246. "--target=aarch64-none-linux-androideabi$(ANDROID_NDK_ABI)",
  247. "--gcc-toolchain=$(ANDROID_NDK_ROOT)/toolchains/llvm/prebuilt/linux-x86_64",
  248. "--sysroot=$(ANDROID_NDK_ROOT)/toolchains/llvm/prebuilt/linux-x86_64/sysroot",
  249. "-march=armv8-a",
  250. "-Wunused-value",
  251. "-Wundef",
  252. }
  253. linkoptions {
  254. "--target=aarch64-none-linux-androideabi$(ANDROID_NDK_ABI)",
  255. "--gcc-toolchain=$(ANDROID_NDK_ROOT)/toolchains/llvm/prebuilt/linux-x86_64",
  256. "--sysroot=$(ANDROID_NDK_ROOT)/toolchains/llvm/prebuilt/linux-x86_64/sysroot",
  257. "$(ANDROID_NDK_ROOT)/platforms/android-$(ANDROID_NDK_ABI)/arch-arm64/usr/lib/crtbegin_so.o",
  258. "$(ANDROID_NDK_ROOT)/platforms/android-$(ANDROID_NDK_ABI)/arch-arm64/usr/lib/crtend_so.o",
  259. "-march=armv8-a",
  260. }
  261. configuration { "mingw-*" }
  262. defines { "WIN32" }
  263. includedirs { path.join(CROWN_DIR, "3rdparty/bx/include/compat/mingw") }
  264. buildoptions {
  265. "-fdata-sections",
  266. "-ffunction-sections",
  267. "-msse2",
  268. "-Wunused-value",
  269. "-Wundef",
  270. }
  271. buildoptions_cpp {
  272. "-std=c++14",
  273. }
  274. linkoptions {
  275. "-Wl,--gc-sections",
  276. "-static",
  277. "-static-libgcc",
  278. "-static-libstdc++",
  279. }
  280. configuration { "x32", "mingw-gcc" }
  281. targetdir (path.join(build_dir, "mingw32/bin"))
  282. objdir (path.join(build_dir, "mingw32/obj"))
  283. libdirs {
  284. lib_dir .. "../build/mingw32/bin",
  285. }
  286. buildoptions {
  287. "-m32",
  288. "-mstackrealign",
  289. }
  290. configuration { "x64", "mingw-gcc" }
  291. targetdir (path.join(build_dir, "mingw64/bin"))
  292. objdir (path.join(build_dir, "mingw64/obj"))
  293. libdirs {
  294. lib_dir .. "../build/mingw64/bin",
  295. }
  296. buildoptions { "-m64" }
  297. configuration { "vs*" }
  298. includedirs { CROWN_DIR .. "3rdparty/bx/include/compat/msvc" }
  299. defines {
  300. "WIN32",
  301. "_WIN32",
  302. "_HAS_EXCEPTIONS=0",
  303. "_HAS_ITERATOR_DEBUGGING=0",
  304. "_SCL_SECURE=0",
  305. "_SECURE_SCL=0",
  306. "_SCL_SECURE_NO_WARNINGS",
  307. "_CRT_SECURE_NO_WARNINGS",
  308. "_CRT_SECURE_NO_DEPRECATE",
  309. "NOMINMAX",
  310. }
  311. buildoptions {
  312. "/Ob2", -- The Inline Function Expansion
  313. "/we4715", -- Not all control paths return a value
  314. }
  315. linkoptions {
  316. "/ignore:4199", -- LNK4199: /DELAYLOAD:*.dll ignored; no imports found from *.dll
  317. "/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
  318. "/ignore:4099", -- LNK4099: The linker was unable to find your .pdb file.
  319. }
  320. configuration { "x32", "vs*" }
  321. targetdir (build_dir .. "windows32" .. "/bin")
  322. objdir (build_dir .. "windows32" .. "/obj")
  323. configuration { "x64", "vs*" }
  324. defines {
  325. "_WIN64",
  326. }
  327. targetdir (build_dir .. "windows64" .. "/bin")
  328. objdir (build_dir .. "windows64" .. "/obj")
  329. configuration {} -- reset configuration
  330. end
  331. function strip()
  332. configuration { "android-arm", "release"}
  333. postbuildcommands {
  334. "$(SILENT) echo Stripping symbols",
  335. "$(SILENT) $(ANDROID_NDK_ROOT)/toolchains/llvm/prebuilt/linux-x86_64/bin/arm-linux-androideabi-strip -s \"$(TARGET)\""
  336. }
  337. configuration { "android-arm64", "release"}
  338. postbuildcommands {
  339. "$(SILENT) echo Stripping symbols",
  340. "$(SILENT) $(ANDROID_NDK_ROOT)/toolchains/llvm/prebuilt/linux-x86_64/bin/aarch64-linux-android-strip -s \"$(TARGET)\""
  341. }
  342. configuration { "linux-*", "release" }
  343. postbuildcommands {
  344. "$(SILENT) echo Stripping symbols",
  345. "$(SILENT) strip -s \"$(TARGET)\""
  346. }
  347. configuration { "mingw*", "Release" }
  348. postbuildcommands {
  349. "$(SILENT) echo Stripping symbols.",
  350. "$(SILENT) $(MINGW)/bin/strip -s \"$(TARGET)\""
  351. }
  352. configuration {} -- reset configuration
  353. end