SConstruct 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. #!/usr/bin/env python
  2. import os
  3. import sys
  4. # Try to detect the host platform automatically.
  5. # This is used if no `platform` argument is passed
  6. if sys.platform.startswith("linux"):
  7. host_platform = "linux"
  8. elif sys.platform == "darwin":
  9. host_platform = "osx"
  10. elif sys.platform == "win32" or sys.platform == "msys":
  11. host_platform = "windows"
  12. else:
  13. raise ValueError("Could not detect platform automatically, please specify with " "platform=<platform>")
  14. env = Environment(ENV=os.environ)
  15. opts = Variables([], ARGUMENTS)
  16. # Define our options
  17. opts.Add(EnumVariable("target", "Compilation target", "debug", ["d", "debug", "r", "release"]))
  18. opts.Add(EnumVariable("platform", "Compilation platform", host_platform, ["", "windows", "x11", "linux", "osx"]))
  19. opts.Add(
  20. EnumVariable("p", "Compilation target, alias for 'platform'", host_platform, ["", "windows", "x11", "linux", "osx"])
  21. )
  22. opts.Add(EnumVariable("bits", "Target platform bits", "64", ("32", "64")))
  23. opts.Add(BoolVariable("use_llvm", "Use the LLVM / Clang compiler", "no"))
  24. opts.Add(PathVariable("target_path", "The path where the lib is installed.", "demo/bin/", PathVariable.PathAccept))
  25. opts.Add(PathVariable("target_name", "The library name.", "libgdexample", PathVariable.PathAccept))
  26. # Local dependency paths, adapt them to your setup
  27. godot_headers_path = "../godot-headers-temp/"
  28. cpp_bindings_path = "../"
  29. cpp_library = "libgodot-cpp"
  30. # only support 64 at this time..
  31. bits = 64
  32. # Updates the environment with the option variables.
  33. opts.Update(env)
  34. # Generates help for the -h scons option.
  35. Help(opts.GenerateHelpText(env))
  36. # This makes sure to keep the session environment variables on Windows.
  37. # This way, you can run SCons in a Visual Studio 2017 prompt and it will find
  38. # all the required tools
  39. if host_platform == "windows" and env["platform"] != "android":
  40. if env["bits"] == "64":
  41. env = Environment(TARGET_ARCH="amd64")
  42. elif env["bits"] == "32":
  43. env = Environment(TARGET_ARCH="x86")
  44. opts.Update(env)
  45. # Process some arguments
  46. if env["use_llvm"]:
  47. env["CC"] = "clang"
  48. env["CXX"] = "clang++"
  49. if env["p"] != "":
  50. env["platform"] = env["p"]
  51. if env["platform"] == "":
  52. print("No valid target platform selected.")
  53. quit()
  54. # For the reference:
  55. # - CCFLAGS are compilation flags shared between C and C++
  56. # - CFLAGS are for C-specific compilation flags
  57. # - CXXFLAGS are for C++-specific compilation flags
  58. # - CPPFLAGS are for pre-processor flags
  59. # - CPPDEFINES are for pre-processor defines
  60. # - LINKFLAGS are for linking flags
  61. if env["target"] == "debug":
  62. env.Append(CPPDEFINES=["DEBUG_ENABLED", "DEBUG_METHODS_ENABLED"])
  63. # Check our platform specifics
  64. if env["platform"] == "osx":
  65. env["target_path"] += "osx/"
  66. cpp_library += ".osx"
  67. env.Append(CCFLAGS=["-arch", "x86_64"])
  68. env.Append(CXXFLAGS=["-std=c++17"])
  69. env.Append(LINKFLAGS=["-arch", "x86_64"])
  70. if env["target"] in ("debug", "d"):
  71. env.Append(CCFLAGS=["-g", "-O2"])
  72. else:
  73. env.Append(CCFLAGS=["-g", "-O3"])
  74. elif env["platform"] in ("x11", "linux"):
  75. env["target_path"] += "x11/"
  76. cpp_library += ".linux"
  77. env.Append(CCFLAGS=["-fPIC"])
  78. env.Append(CXXFLAGS=["-std=c++17"])
  79. if env["target"] in ("debug", "d"):
  80. env.Append(CCFLAGS=["-g3", "-Og"])
  81. else:
  82. env.Append(CCFLAGS=["-g", "-O3"])
  83. elif env["platform"] == "windows":
  84. env["target_path"] += "win64/"
  85. cpp_library += ".windows"
  86. # This makes sure to keep the session environment variables on windows,
  87. # that way you can run scons in a vs 2017 prompt and it will find all the required tools
  88. env.Append(ENV=os.environ)
  89. env.Append(CPPDEFINES=["WIN32", "_WIN32", "_WINDOWS", "_CRT_SECURE_NO_WARNINGS"])
  90. env.Append(CCFLAGS=["-W3", "-GR"])
  91. env.Append(CXXFLAGS=["-std:c++17"])
  92. if env["target"] in ("debug", "d"):
  93. env.Append(CPPDEFINES=["_DEBUG"])
  94. env.Append(CCFLAGS=["-EHsc", "-MDd", "-ZI", "-FS"])
  95. env.Append(LINKFLAGS=["-DEBUG"])
  96. else:
  97. env.Append(CPPDEFINES=["NDEBUG"])
  98. env.Append(CCFLAGS=["-O2", "-EHsc", "-MD"])
  99. if not(env["use_llvm"]):
  100. env.Append(CPPDEFINES=["TYPED_METHOD_BIND"])
  101. if env["target"] in ("debug", "d"):
  102. cpp_library += ".debug"
  103. else:
  104. cpp_library += ".release"
  105. cpp_library += "." + str(bits)
  106. # make sure our binding library is properly includes
  107. env.Append(CPPPATH=[".", godot_headers_path, cpp_bindings_path + "include/", cpp_bindings_path + "gen/include/"])
  108. env.Append(LIBPATH=[cpp_bindings_path + "bin/"])
  109. env.Append(LIBS=[cpp_library])
  110. # tweak this if you want to use different folders, or more folders, to store your source code in.
  111. env.Append(CPPPATH=["src/"])
  112. sources = Glob("src/*.cpp")
  113. library = env.SharedLibrary(target=env["target_path"] + env["target_name"], source=sources)
  114. Default(library)