common_compiler_flags.py 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. import os
  2. import subprocess
  3. import sys
  4. def using_clang(env):
  5. return "clang" in os.path.basename(env["CC"])
  6. def is_vanilla_clang(env):
  7. if not using_clang(env):
  8. return False
  9. try:
  10. version = subprocess.check_output([env.subst(env["CXX"]), "--version"]).strip().decode("utf-8")
  11. except (subprocess.CalledProcessError, OSError):
  12. print("Couldn't parse CXX environment variable to infer compiler version.")
  13. return False
  14. return not version.startswith("Apple")
  15. def exists(env):
  16. return True
  17. def generate(env):
  18. # Require C++17
  19. if env.get("is_msvc", False):
  20. env.Append(CXXFLAGS=["/std:c++17"])
  21. else:
  22. env.Append(CXXFLAGS=["-std=c++17"])
  23. # Disable exception handling. Godot doesn't use exceptions anywhere, and this
  24. # saves around 20% of binary size and very significant build time.
  25. if env["disable_exceptions"]:
  26. if env.get("is_msvc", False):
  27. env.Append(CPPDEFINES=[("_HAS_EXCEPTIONS", 0)])
  28. else:
  29. env.Append(CXXFLAGS=["-fno-exceptions"])
  30. elif env.get("is_msvc", False):
  31. env.Append(CXXFLAGS=["/EHsc"])
  32. # Set optimize and debug_symbols flags.
  33. # "custom" means do nothing and let users set their own optimization flags.
  34. if env.get("is_msvc", False):
  35. if env["debug_symbols"]:
  36. env.Append(CCFLAGS=["/Zi", "/FS"])
  37. env.Append(LINKFLAGS=["/DEBUG:FULL"])
  38. if env["optimize"] == "speed":
  39. env.Append(CCFLAGS=["/O2"])
  40. env.Append(LINKFLAGS=["/OPT:REF"])
  41. elif env["optimize"] == "speed_trace":
  42. env.Append(CCFLAGS=["/O2"])
  43. env.Append(LINKFLAGS=["/OPT:REF", "/OPT:NOICF"])
  44. elif env["optimize"] == "size":
  45. env.Append(CCFLAGS=["/O1"])
  46. env.Append(LINKFLAGS=["/OPT:REF"])
  47. elif env["optimize"] == "debug" or env["optimize"] == "none":
  48. env.Append(CCFLAGS=["/Od"])
  49. else:
  50. if env["debug_symbols"]:
  51. # Adding dwarf-4 explicitly makes stacktraces work with clang builds,
  52. # otherwise addr2line doesn't understand them.
  53. env.Append(CCFLAGS=["-gdwarf-4"])
  54. if env.dev_build:
  55. env.Append(CCFLAGS=["-g3"])
  56. else:
  57. env.Append(CCFLAGS=["-g2"])
  58. else:
  59. if using_clang(env) and not is_vanilla_clang(env):
  60. # Apple Clang, its linker doesn't like -s.
  61. env.Append(LINKFLAGS=["-Wl,-S", "-Wl,-x", "-Wl,-dead_strip"])
  62. else:
  63. env.Append(LINKFLAGS=["-s"])
  64. if env["optimize"] == "speed":
  65. env.Append(CCFLAGS=["-O3"])
  66. # `-O2` is friendlier to debuggers than `-O3`, leading to better crash backtraces.
  67. elif env["optimize"] == "speed_trace":
  68. env.Append(CCFLAGS=["-O2"])
  69. elif env["optimize"] == "size":
  70. env.Append(CCFLAGS=["-Os"])
  71. elif env["optimize"] == "debug":
  72. env.Append(CCFLAGS=["-Og"])
  73. elif env["optimize"] == "none":
  74. env.Append(CCFLAGS=["-O0"])