targets.py 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. import os
  2. import sys
  3. from SCons.Script import ARGUMENTS
  4. from SCons.Variables import *
  5. from SCons.Variables.BoolVariable import _text2bool
  6. def get_cmdline_bool(option, default):
  7. """We use `ARGUMENTS.get()` to check if options were manually overridden on the command line,
  8. and SCons' _text2bool helper to convert them to booleans, otherwise they're handled as strings.
  9. """
  10. cmdline_val = ARGUMENTS.get(option)
  11. if cmdline_val is not None:
  12. return _text2bool(cmdline_val)
  13. else:
  14. return default
  15. def options(opts):
  16. opts.Add(
  17. EnumVariable(
  18. "optimize",
  19. "The desired optimization flags",
  20. "speed_trace",
  21. ("none", "custom", "debug", "speed", "speed_trace", "size"),
  22. )
  23. )
  24. opts.Add(BoolVariable("debug_symbols", "Build with debugging symbols", True))
  25. opts.Add(BoolVariable("dev_build", "Developer build with dev-only debugging code (DEV_ENABLED)", False))
  26. def exists(env):
  27. return True
  28. def generate(env):
  29. env.dev_build = env["dev_build"]
  30. env.debug_features = env["target"] in ["editor", "template_debug"]
  31. env.editor_build = env["target"] == "editor"
  32. if env.editor_build:
  33. env.AppendUnique(CPPDEFINES=["TOOLS_ENABLED"])
  34. if env.debug_features:
  35. env.AppendUnique(CPPDEFINES=["DEBUG_ENABLED", "DEBUG_METHODS_ENABLED"])
  36. if env.dev_build:
  37. opt_level = "none"
  38. env.AppendUnique(CPPDEFINES=["DEV_ENABLED"])
  39. elif env.debug_features:
  40. opt_level = "speed_trace"
  41. else: # Release
  42. opt_level = "speed"
  43. env["optimize"] = ARGUMENTS.get("optimize", opt_level)
  44. env["debug_symbols"] = get_cmdline_bool("debug_symbols", env.dev_build)
  45. if "is_msvc" in env and env["is_msvc"]:
  46. if env["debug_symbols"]:
  47. env.Append(CCFLAGS=["/Zi", "/FS"])
  48. env.Append(LINKFLAGS=["/DEBUG:FULL"])
  49. if env["optimize"] == "speed" or env["optimize"] == "speed_trace":
  50. env.Append(CCFLAGS=["/O2"])
  51. env.Append(LINKFLAGS=["/OPT:REF"])
  52. elif env["optimize"] == "size":
  53. env.Append(CCFLAGS=["/O1"])
  54. env.Append(LINKFLAGS=["/OPT:REF"])
  55. elif env["optimize"] == "debug" or env["optimize"] == "none":
  56. env.Append(CCFLAGS=["/Od"])
  57. else:
  58. if env["debug_symbols"]:
  59. if env.dev_build:
  60. env.Append(CCFLAGS=["-g3"])
  61. else:
  62. env.Append(CCFLAGS=["-g2"])
  63. if env["optimize"] == "speed":
  64. env.Append(CCFLAGS=["-O3"])
  65. # `-O2` is friendlier to debuggers than `-O3`, leading to better crash backtraces.
  66. elif env["optimize"] == "speed_trace":
  67. env.Append(CCFLAGS=["-O2"])
  68. elif env["optimize"] == "size":
  69. env.Append(CCFLAGS=["-Os"])
  70. elif env["optimize"] == "debug":
  71. env.Append(CCFLAGS=["-Og"])
  72. elif env["optimize"] == "none":
  73. env.Append(CCFLAGS=["-O0"])