SConstruct 5.5 KB

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