Guest User

Untitled

a guest
Feb 10th, 2019
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 17.25 KB | None | 0 0
  1. CheckVersion("0.5")
  2.  
  3. Import("configure.lua")
  4. Import("other/sdl/sdl.lua")
  5. Import("other/freetype/freetype.lua")
  6.  
  7. --- Setup Config -------
  8. config = NewConfig()
  9. config:Add(OptCCompiler("compiler"))
  10. config:Add(OptTestCompileC("stackprotector", "int main(){return 0;}", "-fstack-protector -fstack-protector-all"))
  11. config:Add(OptTestCompileC("minmacosxsdk", "int main(){return 0;}", "-mmacosx-version-min=10.7 -isysroot /Developer/SDKs/MacOSX10.7.sdk"))
  12. config:Add(OptTestCompileC("buildwithoutsseflag", "#include <immintrin.h>\nint main(){_mm_pause();return 0;}", ""))
  13. config:Add(OptLibrary("zlib", "zlib.h", false))
  14. config:Add(SDL.OptFind("sdl", true))
  15. config:Add(FreeType.OptFind("freetype", true))
  16. config:Finalize("config.lua")
  17.  
  18. generated_src_dir = "build/src"
  19. generated_icon_dir = "build/icons"
  20. builddir = "build/%(arch)s/%(conf)s"
  21. content_src_dir = "datasrc/"
  22.  
  23. -- data compiler
  24. function Python(name)
  25. if family == "windows" then
  26. -- Python is usually registered for .py files in Windows
  27. return str_replace(name, "/", "\\")
  28. end
  29. return "python " .. name
  30. end
  31.  
  32. function CHash(output, ...)
  33. local inputs = TableFlatten({...})
  34.  
  35. output = PathJoin(generated_src_dir, Path(output))
  36.  
  37. -- compile all the files
  38. local cmd = Python("scripts/cmd5.py") .. " "
  39. for index, inname in ipairs(inputs) do
  40. cmd = cmd .. Path(inname) .. " "
  41. end
  42.  
  43. cmd = cmd .. " > " .. output
  44.  
  45. AddJob(output, "cmd5 " .. output, cmd)
  46. for index, inname in ipairs(inputs) do
  47. AddDependency(output, inname)
  48. end
  49. AddDependency(output, "scripts/cmd5.py")
  50. return output
  51. end
  52.  
  53. function ResCompile(scriptfile, compiler)
  54. scriptfile = Path(scriptfile)
  55. local output = nil
  56. if compiler == "cl" then
  57. output = PathJoin(generated_icon_dir, PathBase(PathFilename(scriptfile)) .. ".res")
  58. AddJob(output, "rc " .. scriptfile, "rc /fo " .. output .. " " .. scriptfile)
  59. elseif compiler == "gcc" or compiler == "clang" then
  60. output = PathJoin(generated_icon_dir, PathBase(PathFilename(scriptfile)) .. ".coff")
  61. AddJob(output, "windres " .. scriptfile, "windres -i " .. scriptfile .. " -o " .. output)
  62. end
  63. AddDependency(output, scriptfile)
  64. return output
  65. end
  66.  
  67. function ContentCompile(action, output)
  68. output = PathJoin(generated_src_dir, Path(output))
  69. AddJob(
  70. output,
  71. action .. " > " .. output,
  72. Python("datasrc/compile.py") .. " " .. action .. " > " .. output
  73. )
  74. AddDependency(output, "datasrc/compile.py")
  75. AddDependency("datasrc/compile.py", "datasrc/content.py", "datasrc/network.py", "datasrc/datatypes.py")
  76. return output
  77. end
  78.  
  79.  
  80. function GenerateCommonSettings(settings, conf, arch, compiler)
  81. if compiler == "gcc" or compiler == "clang" then
  82. settings.cc.flags:Add("-Wall", "-fno-exceptions")
  83. end
  84.  
  85. -- Compile zlib if needed
  86. local zlib = nil
  87. if config.zlib.value == 1 then
  88. settings.link.libs:Add("z")
  89. if config.zlib.include_path then
  90. settings.cc.includes:Add(config.zlib.include_path)
  91. end
  92. else
  93. settings.cc.includes:Add("src/engine/external/zlib")
  94. zlib = Compile(settings, Collect("src/engine/external/zlib/*.c"))
  95. end
  96.  
  97. local md5 = Compile(settings, Collect("src/engine/external/md5/*.c"))
  98. local wavpack = Compile(settings, Collect("src/engine/external/wavpack/*.c"))
  99. local png = Compile(settings, Collect("src/engine/external/pnglite/*.c"))
  100. local json = Compile(settings, Collect("src/engine/external/json-parser/*.c"))
  101.  
  102. -- globally available libs
  103. libs = {zlib=zlib, wavpack=wavpack, png=png, md5=md5, json=json}
  104. end
  105.  
  106. function GenerateMacOSXSettings(settings, conf, arch, compiler)
  107. if arch == "x86" then
  108. settings.cc.flags:Add("-arch i386")
  109. settings.link.flags:Add("-arch i386")
  110. elseif arch == "x86_64" then
  111. settings.cc.flags:Add("-arch x86_64")
  112. settings.link.flags:Add("-arch x86_64")
  113. elseif arch == "ppc" then
  114. settings.cc.flags:Add("-arch ppc")
  115. settings.link.flags:Add("-arch ppc")
  116. elseif arch == "ppc64" then
  117. settings.cc.flags:Add("-arch ppc64")
  118. settings.link.flags:Add("-arch ppc64")
  119. else
  120. print("Unknown Architecture '" .. arch .. "'. Supported: x86, x86_64, ppc, ppc64")
  121. os.exit(1)
  122. end
  123.  
  124. -- c++ stdlib needed
  125. settings.cc.flags:Add("--stdlib=libc++")
  126. settings.link.flags:Add("--stdlib=libc++")
  127. -- this also needs the macOS min SDK version to be at least 10.7
  128.  
  129. settings.cc.flags:Add("-mmacosx-version-min=10.7")
  130. settings.link.flags:Add("-mmacosx-version-min=10.7")
  131.  
  132. if config.minmacosxsdk.value == 1 then
  133. settings.cc.flags:Add("-isysroot /Developer/SDKs/MacOSX10.7.sdk")
  134. settings.link.flags:Add("-isysroot /Developer/SDKs/MacOSX10.7.sdk")
  135. end
  136.  
  137. settings.link.frameworks:Add("Carbon")
  138. settings.link.frameworks:Add("AppKit")
  139.  
  140. GenerateCommonSettings(settings, conf, arch, compiler)
  141.  
  142. -- Build server launcher before adding game stuff
  143. local serverlaunch = Link(settings, "serverlaunch", Compile(settings, "src/osxlaunch/server.m"))
  144.  
  145. -- Master server, version server and tools
  146. BuildEngineCommon(settings)
  147. BuildMasterserver(settings)
  148. BuildVersionserver(settings)
  149. BuildTools(settings)
  150.  
  151. -- Add requirements for Server & Client
  152. BuildGameCommon(settings)
  153.  
  154. -- Server
  155. settings.link.frameworks:Add("Cocoa")
  156. local server_exe = BuildServer(settings)
  157. AddDependency(server_exe, serverlaunch)
  158.  
  159. -- Client
  160. settings.link.frameworks:Add("OpenGL")
  161. settings.link.frameworks:Add("AGL")
  162. -- FIXME: the SDL config is applied in BuildClient too but is needed here before so the launcher will compile
  163. config.sdl:Apply(settings)
  164. settings.link.extrafiles:Merge(Compile(settings, "src/osxlaunch/client.m"))
  165. BuildClient(settings)
  166.  
  167. -- Content
  168. BuildContent(settings, arch, conf)
  169. end
  170.  
  171. function GenerateLinuxSettings(settings, conf, arch, compiler)
  172. if arch == "x86" then
  173. if config.buildwithoutsseflag.value == 0 then
  174. settings.cc.flags:Add("-msse2") -- for the _mm_pause call
  175. end
  176. settings.cc.flags:Add("-m32")
  177. settings.link.flags:Add("-m32")
  178. elseif arch == "x86_64" then
  179. settings.cc.flags:Add("-m64")
  180. settings.link.flags:Add("-m64")
  181. elseif arch == "armv7l" then
  182. -- arm 32 bit
  183. else
  184. print("Unknown Architecture '" .. arch .. "'. Supported: x86, x86_64")
  185. os.exit(1)
  186. end
  187. settings.link.libs:Add("pthread")
  188.  
  189. GenerateCommonSettings(settings, conf, arch, compiler)
  190.  
  191. -- Master server, version server and tools
  192. BuildEngineCommon(settings)
  193. BuildTools(settings)
  194. BuildMasterserver(settings)
  195. BuildVersionserver(settings)
  196.  
  197. -- Add requirements for Server & Client
  198. BuildGameCommon(settings)
  199.  
  200. -- Server
  201. BuildServer(settings)
  202.  
  203. -- Client
  204. settings.link.libs:Add("X11")
  205. settings.link.libs:Add("GL")
  206. settings.link.libs:Add("GLU")
  207. BuildClient(settings)
  208.  
  209. -- Content
  210. BuildContent(settings, arch, conf)
  211. end
  212.  
  213. function GenerateSolarisSettings(settings, conf, arch, compiler)
  214. settings.link.libs:Add("socket")
  215. settings.link.libs:Add("nsl")
  216.  
  217. GenerateLinuxSettings(settings, conf, arch, compiler)
  218. end
  219.  
  220. function GenerateWindowsSettings(settings, conf, target_arch, compiler)
  221. if compiler == "cl" then
  222. if (target_arch == "x86" and arch ~= "ia32") or
  223. (target_arch == "x86_64" and arch ~= "ia64" and arch ~= "amd64") then
  224. print("Cross compiling is unsupported on Windows.")
  225. os.exit(1)
  226. end
  227. settings.cc.flags:Add("/wd4244", "/wd4577")
  228. elseif compiler == "gcc" or config.compiler.driver == "clang" then
  229. if target_arch ~= "x86" and target_arch ~= "x86_64" then
  230. print("Unknown Architecture '" .. arch .. "'. Supported: x86, x86_64")
  231. os.exit(1)
  232. end
  233.  
  234. -- disable visibility attribute support for gcc on windows
  235. settings.cc.defines:Add("NO_VIZ")
  236. settings.cc.defines:Add("_WIN32_WINNT=0x0501")
  237. end
  238.  
  239. local icons = SharedIcons(compiler)
  240.  
  241. -- Required libs
  242. settings.link.libs:Add("gdi32")
  243. settings.link.libs:Add("user32")
  244. settings.link.libs:Add("ws2_32")
  245. settings.link.libs:Add("ole32")
  246. settings.link.libs:Add("shell32")
  247. settings.link.libs:Add("advapi32")
  248.  
  249. GenerateCommonSettings(settings, conf, target_arch, compiler)
  250.  
  251. -- Master server, version server and tools
  252. BuildEngineCommon(settings)
  253. BuildMasterserver(settings)
  254. BuildVersionserver(settings)
  255. BuildTools(settings)
  256.  
  257. -- Add requirements for Server & Client
  258. BuildGameCommon(settings)
  259.  
  260. -- Server
  261. local server_settings = settings:Copy()
  262. server_settings.link.extrafiles:Add(icons.server)
  263. BuildServer(server_settings)
  264.  
  265. -- Client
  266. settings.link.extrafiles:Add(icons.client)
  267. settings.link.libs:Add("opengl32")
  268. settings.link.libs:Add("glu32")
  269. settings.link.libs:Add("winmm")
  270. BuildClient(settings)
  271.  
  272. -- Content
  273. BuildContent(settings, target_arch, conf)
  274. end
  275.  
  276. function SharedCommonFiles()
  277. -- Shared game files, generate only once
  278.  
  279. if not shared_common_files then
  280. local network_source = ContentCompile("network_source", "generated/protocol.cpp")
  281. local network_header = ContentCompile("network_header", "generated/protocol.h")
  282. AddDependency(network_source, network_header, "src/engine/shared/protocol.h")
  283.  
  284. local nethash = CHash("generated/nethash.cpp", "src/engine/shared/protocol.h", "src/game/tuning.h", "src/game/gamecore.cpp", network_header)
  285. shared_common_files = {network_source, nethash}
  286. end
  287.  
  288. return shared_common_files
  289. end
  290.  
  291. function SharedServerFiles()
  292. -- Shared server files, generate only once
  293.  
  294. if not shared_server_files then
  295. local server_content_source = ContentCompile("server_content_source", "generated/server_data.cpp")
  296. local server_content_header = ContentCompile("server_content_header", "generated/server_data.h")
  297. AddDependency(server_content_source, server_content_header)
  298. shared_server_files = {server_content_source}
  299. end
  300.  
  301. return shared_server_files
  302. end
  303.  
  304. function SharedClientFiles()
  305. -- Shared client files, generate only once
  306.  
  307. if not shared_client_files then
  308. local client_content_source = ContentCompile("client_content_source", "generated/client_data.cpp")
  309. local client_content_header = ContentCompile("client_content_header", "generated/client_data.h")
  310. AddDependency(client_content_source, client_content_header)
  311. shared_client_files = {client_content_source}
  312. end
  313.  
  314. return shared_client_files
  315. end
  316.  
  317. shared_icons = {}
  318. function SharedIcons(compiler)
  319. if not shared_icons[compiler] then
  320. local server_icon = ResCompile("other/icons/teeworlds_srv_" .. compiler .. ".rc", compiler)
  321. local client_icon = ResCompile("other/icons/teeworlds_" .. compiler .. ".rc", compiler)
  322. shared_icons[compiler] = {server=server_icon, client=client_icon}
  323. end
  324. return shared_icons[compiler]
  325. end
  326.  
  327. function BuildEngineCommon(settings)
  328. settings.link.extrafiles:Merge(Compile(settings, Collect("src/engine/shared/*.cpp", "src/base/*.c")))
  329. end
  330.  
  331. function BuildGameCommon(settings)
  332. settings.link.extrafiles:Merge(Compile(settings, Collect("src/game/*.cpp"), SharedCommonFiles()))
  333. end
  334.  
  335.  
  336. function BuildClient(settings, family, platform)
  337. config.sdl:Apply(settings)
  338. config.freetype:Apply(settings)
  339.  
  340. local client = Compile(settings, Collect("src/engine/client/*.cpp"))
  341.  
  342. local game_client = Compile(settings, CollectRecursive("src/game/client/*.cpp"), SharedClientFiles())
  343. local game_editor = Compile(settings, Collect("src/game/editor/*.cpp"))
  344.  
  345. Link(settings, "teeworlds", libs["zlib"], libs["md5"], libs["wavpack"], libs["png"], libs["json"], client, game_client, game_editor)
  346. end
  347.  
  348. function BuildServer(settings, family, platform)
  349. local server = Compile(settings, Collect("src/engine/server/*.cpp"))
  350.  
  351. local game_server = Compile(settings, CollectRecursive("src/game/server/*.cpp"), SharedServerFiles())
  352.  
  353. return Link(settings, "teeworlds_srv", libs["zlib"], libs["md5"], server, game_server)
  354. end
  355.  
  356. function BuildTools(settings)
  357. local tools = {}
  358. for i,v in ipairs(Collect("src/tools/*.cpp", "src/tools/*.c")) do
  359. local toolname = PathFilename(PathBase(v))
  360. tools[i] = Link(settings, toolname, Compile(settings, v), libs["zlib"], libs["md5"], libs["wavpack"], libs["png"])
  361. end
  362. PseudoTarget(settings.link.Output(settings, "pseudo_tools") .. settings.link.extension, tools)
  363. end
  364.  
  365. function BuildMasterserver(settings)
  366. return Link(settings, "mastersrv", Compile(settings, Collect("src/mastersrv/*.cpp")), libs["zlib"], libs["md5"])
  367. end
  368.  
  369. function BuildVersionserver(settings)
  370. return Link(settings, "versionsrv", Compile(settings, Collect("src/versionsrv/*.cpp")), libs["zlib"], libs["md5"])
  371. end
  372.  
  373. function BuildContent(settings, arch, conf)
  374. local content = {}
  375. table.insert(content, CopyToDir(settings.link.Output(settings, "data"), CollectRecursive(content_src_dir .. "*.png", content_src_dir .. "*.wv", content_src_dir .. "*.ttf", content_src_dir .. "*.txt", content_src_dir .. "*.map", content_src_dir .. "*.rules", content_src_dir .. "*.json")))
  376. if family == "windows" then
  377. if arch == "x86_64" then
  378. _arch = "64"
  379. else
  380. _arch = "32"
  381. end
  382. -- dependencies
  383. dl = Python("scripts/download.py")
  384. AddJob("other/sdl/include/SDL.h", "Downloading SDL2", dl .. " sdl")
  385. AddJob("other/freetype/include/ft2build.h", "Downloading freetype", dl .. " freetype")
  386. table.insert(content, CopyFile(settings.link.Output(settings, "") .. "/SDL2.dll", "other/sdl/windows/lib" .. _arch .. "/SDL2.dll"))
  387. table.insert(content, CopyFile(settings.link.Output(settings, "") .. "/freetype.dll", "other/freetype/windows/lib" .. _arch .. "/freetype.dll"))
  388. AddDependency(settings.link.Output(settings, "") .. "/SDL2.dll", "other/sdl/include/SDL.h")
  389. AddDependency(settings.link.Output(settings, "") .. "/freetype.dll", "other/freetype/include/ft2build.h")
  390. end
  391. PseudoTarget(settings.link.Output(settings, "content") .. settings.link.extension, content)
  392. end
  393.  
  394. -- create all targets for specified configuration & architecture
  395. function GenerateSettings(conf, arch, builddir, compiler)
  396. local settings = NewSettings()
  397.  
  398. -- Set compiler if explicitly requested
  399. if compiler == "gcc" then
  400. SetDriversGCC(settings)
  401. elseif compiler == "clang" then
  402. SetDriversClang(settings)
  403. elseif compiler == "cl" then
  404. SetDriversCL(settings)
  405. else
  406. -- apply compiler settings
  407. config.compiler:Apply(settings)
  408. compiler = config.compiler.driver
  409. end
  410.  
  411. if conf == "debug" then
  412. settings.debug = 1
  413. settings.optimize = 0
  414. settings.cc.defines:Add("CONF_DEBUG")
  415. else
  416. settings.debug = 0
  417. settings.optimize = 1
  418. settings.cc.defines:Add("CONF_RELEASE")
  419. end
  420.  
  421. -- Generate object files in {builddir}/objs/
  422. settings.cc.Output = function (settings_, input)
  423. -- strip
  424. input = input:gsub("^src/", "")
  425. input = input:gsub("^" .. generated_src_dir .. "/", "")
  426. return PathJoin(PathJoin(builddir, "objs"), PathBase(input))
  427. end
  428.  
  429. -- Build output files in {builddir}
  430. settings.link.Output = function (settings_, input)
  431. return PathJoin(builddir, PathBase(input) .. settings_.config_ext)
  432. end
  433.  
  434. settings.cc.includes:Add("src")
  435. settings.cc.includes:Add("src/engine/external/pnglite")
  436. settings.cc.includes:Add("src/engine/external/wavpack")
  437. settings.cc.includes:Add(generated_src_dir)
  438.  
  439. if family == "windows" then
  440. GenerateWindowsSettings(settings, conf, arch, compiler)
  441. elseif family == "unix" then
  442. if platform == "macosx" then
  443. GenerateMacOSXSettings(settings, conf, arch, compiler)
  444. elseif platform == "solaris" then
  445. GenerateSolarisSettings(settings, conf, arch, compiler)
  446. else -- Linux, BSD
  447. GenerateLinuxSettings(settings, conf, arch, compiler)
  448. end
  449. end
  450.  
  451. return settings
  452. end
  453.  
  454. -- String formatting with named parameters, by RiciLake http://lua-users.org/wiki/StringInterpolation
  455. function interp(s, tab)
  456. return (s:gsub('%%%((%a%w*)%)([-0-9%.]*[cdeEfgGiouxXsq])',
  457. function(k, fmt)
  458. return tab[k] and ("%"..fmt):format(tab[k]) or '%('..k..')'..fmt
  459. end))
  460. end
  461.  
  462. function CopyToDir(dst, ...)
  463. local output = {}
  464. for filename in TableWalk({...}) do
  465. table.insert(output, CopyFile(PathJoin(dst, string.sub(filename, string.len(content_src_dir)+1)), filename))
  466. end
  467. return output
  468. end
  469.  
  470. function split(str, sep)
  471. local vals = {}
  472. str:gsub("([^,]+)", function(val) table.insert(vals, val) end)
  473. return vals
  474. end
  475.  
  476. -- Supported archtitectures: x86, amd64, ppc, ppc64
  477. if ScriptArgs['arch'] then
  478. archs = split(ScriptArgs['arch'])
  479. else
  480. if arch == "ia32" then
  481. archs = {"x86"}
  482. elseif arch == "ia64" or arch == "amd64" then
  483. archs = {"x86_64"}
  484. else
  485. archs = {arch}
  486. end
  487. end
  488.  
  489. if ScriptArgs['conf'] then
  490. confs = split(ScriptArgs['conf'])
  491. else
  492. confs = {"debug"}
  493. end
  494.  
  495. if ScriptArgs['compiler'] then
  496. compiler = ScriptArgs['compiler']
  497. else
  498. compiler = nil
  499. end
  500.  
  501. if ScriptArgs['builddir'] then
  502. builddir = ScriptArgs['builddir']
  503. end
  504.  
  505. targets = {client="teeworlds", server="teeworlds_srv",
  506. versionserver="versionsrv", masterserver="mastersrv",
  507. tools="pseudo_tools", content="content"}
  508.  
  509. subtargets = {}
  510. for t, cur_target in pairs(targets) do
  511. subtargets[cur_target] = {}
  512. end
  513. for a, cur_arch in ipairs(archs) do
  514. for c, cur_conf in ipairs(confs) do
  515. cur_builddir = interp(builddir, {platform=family, arch=cur_arch, target=cur_target, conf=cur_conf, compiler=compiler})
  516. local settings = GenerateSettings(cur_conf, cur_arch, cur_builddir, compiler)
  517. for t, cur_target in pairs(targets) do
  518. table.insert(subtargets[cur_target], PathJoin(cur_builddir, cur_target .. settings.link.extension))
  519. end
  520. end
  521. end
  522.  
  523. for cur_name, cur_target in pairs(targets) do
  524. -- Supertarget for all configurations and architectures of that target
  525. PseudoTarget(cur_name, subtargets[cur_target])
  526. end
  527.  
  528. PseudoTarget("game", "client", "server", "content")
  529. DefaultTarget("game")
Advertisement
Add Comment
Please, Sign In to add comment