methods.py 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791
  1. import os
  2. import re
  3. import glob
  4. import subprocess
  5. def add_source_files(self, sources, files, warn_duplicates=True):
  6. # Convert string to list of absolute paths (including expanding wildcard)
  7. if isinstance(files, (str, bytes)):
  8. # Keep SCons project-absolute path as they are (no wildcard support)
  9. if files.startswith("#"):
  10. if "*" in files:
  11. print("ERROR: Wildcards can't be expanded in SCons project-absolute path: '{}'".format(files))
  12. return
  13. files = [files]
  14. else:
  15. dir_path = self.Dir(".").abspath
  16. files = sorted(glob.glob(dir_path + "/" + files))
  17. # Add each path as compiled Object following environment (self) configuration
  18. for path in files:
  19. obj = self.Object(path)
  20. if obj in sources:
  21. if warn_duplicates:
  22. print('WARNING: Object "{}" already included in environment sources.'.format(obj))
  23. else:
  24. continue
  25. sources.append(obj)
  26. def disable_warnings(self):
  27. # 'self' is the environment
  28. if self.msvc:
  29. # We have to remove existing warning level defines before appending /w,
  30. # otherwise we get: "warning D9025 : overriding '/W3' with '/w'"
  31. warn_flags = ["/Wall", "/W4", "/W3", "/W2", "/W1", "/WX"]
  32. self.Append(CCFLAGS=["/w"])
  33. self.Append(CFLAGS=["/w"])
  34. self.Append(CXXFLAGS=["/w"])
  35. self["CCFLAGS"] = [x for x in self["CCFLAGS"] if not x in warn_flags]
  36. self["CFLAGS"] = [x for x in self["CFLAGS"] if not x in warn_flags]
  37. self["CXXFLAGS"] = [x for x in self["CXXFLAGS"] if not x in warn_flags]
  38. else:
  39. self.Append(CCFLAGS=["-w"])
  40. self.Append(CFLAGS=["-w"])
  41. self.Append(CXXFLAGS=["-w"])
  42. def add_module_version_string(self, s):
  43. self.module_version_string += "." + s
  44. def update_version(module_version_string=""):
  45. build_name = "custom_build"
  46. if os.getenv("BUILD_NAME") != None:
  47. build_name = os.getenv("BUILD_NAME")
  48. print("Using custom build name: " + build_name)
  49. import version
  50. # NOTE: It is safe to generate this file here, since this is still executed serially
  51. f = open("core/version_generated.gen.h", "w")
  52. f.write("/* THIS FILE IS GENERATED DO NOT EDIT */\n")
  53. f.write("#ifndef VERSION_GENERATED_GEN_H\n")
  54. f.write("#define VERSION_GENERATED_GEN_H\n")
  55. f.write('#define VERSION_SHORT_NAME "' + str(version.short_name) + '"\n')
  56. f.write('#define VERSION_NAME "' + str(version.name) + '"\n')
  57. f.write("#define VERSION_MAJOR " + str(version.major) + "\n")
  58. f.write("#define VERSION_MINOR " + str(version.minor) + "\n")
  59. f.write("#define VERSION_PATCH " + str(version.patch) + "\n")
  60. f.write('#define VERSION_STATUS "' + str(version.status) + '"\n')
  61. f.write('#define VERSION_BUILD "' + str(build_name) + '"\n')
  62. f.write('#define VERSION_MODULE_CONFIG "' + str(version.module_config) + module_version_string + '"\n')
  63. f.write("#define VERSION_YEAR " + str(version.year) + "\n")
  64. f.write('#define VERSION_WEBSITE "' + str(version.website) + '"\n')
  65. f.write("#endif // VERSION_GENERATED_GEN_H\n")
  66. f.close()
  67. # NOTE: It is safe to generate this file here, since this is still executed serially
  68. fhash = open("core/version_hash.gen.h", "w")
  69. fhash.write("/* THIS FILE IS GENERATED DO NOT EDIT */\n")
  70. fhash.write("#ifndef VERSION_HASH_GEN_H\n")
  71. fhash.write("#define VERSION_HASH_GEN_H\n")
  72. githash = ""
  73. gitfolder = ".git"
  74. if os.path.isfile(".git"):
  75. module_folder = open(".git", "r").readline().strip()
  76. if module_folder.startswith("gitdir: "):
  77. gitfolder = module_folder[8:]
  78. if os.path.isfile(os.path.join(gitfolder, "HEAD")):
  79. head = open(os.path.join(gitfolder, "HEAD"), "r").readline().strip()
  80. if head.startswith("ref: "):
  81. head = os.path.join(gitfolder, head[5:])
  82. if os.path.isfile(head):
  83. githash = open(head, "r").readline().strip()
  84. else:
  85. githash = head
  86. fhash.write('#define VERSION_HASH "' + githash + '"\n')
  87. fhash.write("#endif // VERSION_HASH_GEN_H\n")
  88. fhash.close()
  89. def parse_cg_file(fname, uniforms, sizes, conditionals):
  90. fs = open(fname, "r")
  91. line = fs.readline()
  92. while line:
  93. if re.match(r"^\s*uniform", line):
  94. res = re.match(r"uniform ([\d\w]*) ([\d\w]*)")
  95. type = res.groups(1)
  96. name = res.groups(2)
  97. uniforms.append(name)
  98. if type.find("texobj") != -1:
  99. sizes.append(1)
  100. else:
  101. t = re.match(r"float(\d)x(\d)", type)
  102. if t:
  103. sizes.append(int(t.groups(1)) * int(t.groups(2)))
  104. else:
  105. t = re.match(r"float(\d)", type)
  106. sizes.append(int(t.groups(1)))
  107. if line.find("[branch]") != -1:
  108. conditionals.append(name)
  109. line = fs.readline()
  110. fs.close()
  111. def detect_modules(at_path):
  112. module_list = {} # name : path
  113. modules_glob = os.path.join(at_path, "*")
  114. files = glob.glob(modules_glob)
  115. files.sort() # so register_module_types does not change that often, and also plugins are registered in alphabetic order
  116. for x in files:
  117. if not is_module(x):
  118. continue
  119. name = os.path.basename(x)
  120. path = x.replace("\\", "/") # win32
  121. module_list[name] = path
  122. return module_list
  123. def is_module(path):
  124. return os.path.isdir(path) and os.path.exists(os.path.join(path, "SCsub"))
  125. def write_modules(module_list):
  126. includes_cpp = ""
  127. preregister_cpp = ""
  128. register_cpp = ""
  129. unregister_cpp = ""
  130. for name, path in module_list.items():
  131. try:
  132. with open(os.path.join(path, "register_types.h")):
  133. includes_cpp += '#include "' + path + '/register_types.h"\n'
  134. preregister_cpp += "#ifdef MODULE_" + name.upper() + "_ENABLED\n"
  135. preregister_cpp += "#ifdef MODULE_" + name.upper() + "_HAS_PREREGISTER\n"
  136. preregister_cpp += "\tpreregister_" + name + "_types();\n"
  137. preregister_cpp += "#endif\n"
  138. preregister_cpp += "#endif\n"
  139. register_cpp += "#ifdef MODULE_" + name.upper() + "_ENABLED\n"
  140. register_cpp += "\tregister_" + name + "_types();\n"
  141. register_cpp += "#endif\n"
  142. unregister_cpp += "#ifdef MODULE_" + name.upper() + "_ENABLED\n"
  143. unregister_cpp += "\tunregister_" + name + "_types();\n"
  144. unregister_cpp += "#endif\n"
  145. except IOError:
  146. pass
  147. modules_cpp = """// register_module_types.gen.cpp
  148. /* THIS FILE IS GENERATED DO NOT EDIT */
  149. #include "register_module_types.h"
  150. #include "modules/modules_enabled.gen.h"
  151. %s
  152. void preregister_module_types() {
  153. %s
  154. }
  155. void register_module_types() {
  156. %s
  157. }
  158. void unregister_module_types() {
  159. %s
  160. }
  161. """ % (
  162. includes_cpp,
  163. preregister_cpp,
  164. register_cpp,
  165. unregister_cpp,
  166. )
  167. # NOTE: It is safe to generate this file here, since this is still executed serially
  168. with open("modules/register_module_types.gen.cpp", "w") as f:
  169. f.write(modules_cpp)
  170. def convert_custom_modules_path(path):
  171. if not path:
  172. return path
  173. err_msg = "Build option 'custom_modules' must %s"
  174. if not os.path.isdir(path):
  175. raise ValueError(err_msg % "point to an existing directory.")
  176. if os.path.realpath(path) == os.path.realpath("modules"):
  177. raise ValueError(err_msg % "be a directory other than built-in `modules` directory.")
  178. if is_module(path):
  179. raise ValueError(err_msg % "point to a directory with modules, not a single module.")
  180. return os.path.realpath(os.path.expanduser(path))
  181. def disable_module(self):
  182. self.disabled_modules.append(self.current_module)
  183. def use_windows_spawn_fix(self, platform=None):
  184. if os.name != "nt":
  185. return # not needed, only for windows
  186. # On Windows, due to the limited command line length, when creating a static library
  187. # from a very high number of objects SCons will invoke "ar" once per object file;
  188. # that makes object files with same names to be overwritten so the last wins and
  189. # the library looses symbols defined by overwritten objects.
  190. # By enabling quick append instead of the default mode (replacing), libraries will
  191. # got built correctly regardless the invocation strategy.
  192. # Furthermore, since SCons will rebuild the library from scratch when an object file
  193. # changes, no multiple versions of the same object file will be present.
  194. self.Replace(ARFLAGS="q")
  195. def mySubProcess(cmdline, env):
  196. startupinfo = subprocess.STARTUPINFO()
  197. startupinfo.dwFlags |= subprocess.STARTF_USESHOWWINDOW
  198. proc = subprocess.Popen(
  199. cmdline,
  200. stdin=subprocess.PIPE,
  201. stdout=subprocess.PIPE,
  202. stderr=subprocess.PIPE,
  203. startupinfo=startupinfo,
  204. shell=False,
  205. env=env,
  206. )
  207. _, err = proc.communicate()
  208. rv = proc.wait()
  209. if rv:
  210. print("=====")
  211. print(err)
  212. print("=====")
  213. return rv
  214. def mySpawn(sh, escape, cmd, args, env):
  215. newargs = " ".join(args[1:])
  216. cmdline = cmd + " " + newargs
  217. rv = 0
  218. env = {str(key): str(value) for key, value in iter(env.items())}
  219. if len(cmdline) > 32000 and cmd.endswith("ar"):
  220. cmdline = cmd + " " + args[1] + " " + args[2] + " "
  221. for i in range(3, len(args)):
  222. rv = mySubProcess(cmdline + args[i], env)
  223. if rv:
  224. break
  225. else:
  226. rv = mySubProcess(cmdline, env)
  227. return rv
  228. self["SPAWN"] = mySpawn
  229. def save_active_platforms(apnames, ap):
  230. for x in ap:
  231. names = ["logo"]
  232. if os.path.isfile(x + "/run_icon.png"):
  233. names.append("run_icon")
  234. for name in names:
  235. pngf = open(x + "/" + name + ".png", "rb")
  236. b = pngf.read(1)
  237. str = " /* AUTOGENERATED FILE, DO NOT EDIT */ \n"
  238. str += " static const unsigned char _" + x[9:] + "_" + name + "[]={"
  239. while len(b) == 1:
  240. str += hex(ord(b))
  241. b = pngf.read(1)
  242. if len(b) == 1:
  243. str += ","
  244. str += "};\n"
  245. pngf.close()
  246. # NOTE: It is safe to generate this file here, since this is still executed serially
  247. wf = x + "/" + name + ".gen.h"
  248. with open(wf, "w") as pngw:
  249. pngw.write(str)
  250. def no_verbose(sys, env):
  251. colors = {}
  252. # Colors are disabled in non-TTY environments such as pipes. This means
  253. # that if output is redirected to a file, it will not contain color codes
  254. if sys.stdout.isatty():
  255. colors["cyan"] = "\033[96m"
  256. colors["purple"] = "\033[95m"
  257. colors["blue"] = "\033[94m"
  258. colors["green"] = "\033[92m"
  259. colors["yellow"] = "\033[93m"
  260. colors["red"] = "\033[91m"
  261. colors["end"] = "\033[0m"
  262. else:
  263. colors["cyan"] = ""
  264. colors["purple"] = ""
  265. colors["blue"] = ""
  266. colors["green"] = ""
  267. colors["yellow"] = ""
  268. colors["red"] = ""
  269. colors["end"] = ""
  270. compile_source_message = "{}Compiling {}==> {}$SOURCE{}".format(
  271. colors["blue"], colors["purple"], colors["yellow"], colors["end"]
  272. )
  273. java_compile_source_message = "{}Compiling {}==> {}$SOURCE{}".format(
  274. colors["blue"], colors["purple"], colors["yellow"], colors["end"]
  275. )
  276. compile_shared_source_message = "{}Compiling shared {}==> {}$SOURCE{}".format(
  277. colors["blue"], colors["purple"], colors["yellow"], colors["end"]
  278. )
  279. link_program_message = "{}Linking Program {}==> {}$TARGET{}".format(
  280. colors["red"], colors["purple"], colors["yellow"], colors["end"]
  281. )
  282. link_library_message = "{}Linking Static Library {}==> {}$TARGET{}".format(
  283. colors["red"], colors["purple"], colors["yellow"], colors["end"]
  284. )
  285. ranlib_library_message = "{}Ranlib Library {}==> {}$TARGET{}".format(
  286. colors["red"], colors["purple"], colors["yellow"], colors["end"]
  287. )
  288. link_shared_library_message = "{}Linking Shared Library {}==> {}$TARGET{}".format(
  289. colors["red"], colors["purple"], colors["yellow"], colors["end"]
  290. )
  291. java_library_message = "{}Creating Java Archive {}==> {}$TARGET{}".format(
  292. colors["red"], colors["purple"], colors["yellow"], colors["end"]
  293. )
  294. env.Append(CXXCOMSTR=[compile_source_message])
  295. env.Append(CCCOMSTR=[compile_source_message])
  296. env.Append(SHCCCOMSTR=[compile_shared_source_message])
  297. env.Append(SHCXXCOMSTR=[compile_shared_source_message])
  298. env.Append(ARCOMSTR=[link_library_message])
  299. env.Append(RANLIBCOMSTR=[ranlib_library_message])
  300. env.Append(SHLINKCOMSTR=[link_shared_library_message])
  301. env.Append(LINKCOMSTR=[link_program_message])
  302. env.Append(JARCOMSTR=[java_library_message])
  303. env.Append(JAVACCOMSTR=[java_compile_source_message])
  304. def detect_visual_c_compiler_version(tools_env):
  305. # tools_env is the variable scons uses to call tools that execute tasks, SCons's env['ENV'] that executes tasks...
  306. # (see the SCons documentation for more information on what it does)...
  307. # in order for this function to be well encapsulated i choose to force it to receive SCons's TOOLS env (env['ENV']
  308. # and not scons setup environment (env)... so make sure you call the right environment on it or it will fail to detect
  309. # the proper vc version that will be called
  310. # There is no flag to give to visual c compilers to set the architecture, ie scons bits argument (32,64,ARM etc)
  311. # There are many different cl.exe files that are run, and each one compiles & links to a different architecture
  312. # As far as I know, the only way to figure out what compiler will be run when Scons calls cl.exe via Program()
  313. # is to check the PATH variable and figure out which one will be called first. Code below does that and returns:
  314. # the following string values:
  315. # "" Compiler not detected
  316. # "amd64" Native 64 bit compiler
  317. # "amd64_x86" 64 bit Cross Compiler for 32 bit
  318. # "x86" Native 32 bit compiler
  319. # "x86_amd64" 32 bit Cross Compiler for 64 bit
  320. # There are other architectures, but Godot does not support them currently, so this function does not detect arm/amd64_arm
  321. # and similar architectures/compilers
  322. # Set chosen compiler to "not detected"
  323. vc_chosen_compiler_index = -1
  324. vc_chosen_compiler_str = ""
  325. # Start with Pre VS 2017 checks which uses VCINSTALLDIR:
  326. if "VCINSTALLDIR" in tools_env:
  327. # print("Checking VCINSTALLDIR")
  328. # find() works with -1 so big ifs below are needed... the simplest solution, in fact
  329. # First test if amd64 and amd64_x86 compilers are present in the path
  330. vc_amd64_compiler_detection_index = tools_env["PATH"].find(tools_env["VCINSTALLDIR"] + "BIN\\amd64;")
  331. if vc_amd64_compiler_detection_index > -1:
  332. vc_chosen_compiler_index = vc_amd64_compiler_detection_index
  333. vc_chosen_compiler_str = "amd64"
  334. vc_amd64_x86_compiler_detection_index = tools_env["PATH"].find(tools_env["VCINSTALLDIR"] + "BIN\\amd64_x86;")
  335. if vc_amd64_x86_compiler_detection_index > -1 and (
  336. vc_chosen_compiler_index == -1 or vc_chosen_compiler_index > vc_amd64_x86_compiler_detection_index
  337. ):
  338. vc_chosen_compiler_index = vc_amd64_x86_compiler_detection_index
  339. vc_chosen_compiler_str = "amd64_x86"
  340. # Now check the 32 bit compilers
  341. vc_x86_compiler_detection_index = tools_env["PATH"].find(tools_env["VCINSTALLDIR"] + "BIN;")
  342. if vc_x86_compiler_detection_index > -1 and (
  343. vc_chosen_compiler_index == -1 or vc_chosen_compiler_index > vc_x86_compiler_detection_index
  344. ):
  345. vc_chosen_compiler_index = vc_x86_compiler_detection_index
  346. vc_chosen_compiler_str = "x86"
  347. vc_x86_amd64_compiler_detection_index = tools_env["PATH"].find(tools_env["VCINSTALLDIR"] + "BIN\\x86_amd64;")
  348. if vc_x86_amd64_compiler_detection_index > -1 and (
  349. vc_chosen_compiler_index == -1 or vc_chosen_compiler_index > vc_x86_amd64_compiler_detection_index
  350. ):
  351. vc_chosen_compiler_index = vc_x86_amd64_compiler_detection_index
  352. vc_chosen_compiler_str = "x86_amd64"
  353. # and for VS 2017 and newer we check VCTOOLSINSTALLDIR:
  354. if "VCTOOLSINSTALLDIR" in tools_env:
  355. # Newer versions have a different path available
  356. vc_amd64_compiler_detection_index = (
  357. tools_env["PATH"].upper().find(tools_env["VCTOOLSINSTALLDIR"].upper() + "BIN\\HOSTX64\\X64;")
  358. )
  359. if vc_amd64_compiler_detection_index > -1:
  360. vc_chosen_compiler_index = vc_amd64_compiler_detection_index
  361. vc_chosen_compiler_str = "amd64"
  362. vc_amd64_x86_compiler_detection_index = (
  363. tools_env["PATH"].upper().find(tools_env["VCTOOLSINSTALLDIR"].upper() + "BIN\\HOSTX64\\X86;")
  364. )
  365. if vc_amd64_x86_compiler_detection_index > -1 and (
  366. vc_chosen_compiler_index == -1 or vc_chosen_compiler_index > vc_amd64_x86_compiler_detection_index
  367. ):
  368. vc_chosen_compiler_index = vc_amd64_x86_compiler_detection_index
  369. vc_chosen_compiler_str = "amd64_x86"
  370. vc_x86_compiler_detection_index = (
  371. tools_env["PATH"].upper().find(tools_env["VCTOOLSINSTALLDIR"].upper() + "BIN\\HOSTX86\\X86;")
  372. )
  373. if vc_x86_compiler_detection_index > -1 and (
  374. vc_chosen_compiler_index == -1 or vc_chosen_compiler_index > vc_x86_compiler_detection_index
  375. ):
  376. vc_chosen_compiler_index = vc_x86_compiler_detection_index
  377. vc_chosen_compiler_str = "x86"
  378. vc_x86_amd64_compiler_detection_index = (
  379. tools_env["PATH"].upper().find(tools_env["VCTOOLSINSTALLDIR"].upper() + "BIN\\HOSTX86\\X64;")
  380. )
  381. if vc_x86_amd64_compiler_detection_index > -1 and (
  382. vc_chosen_compiler_index == -1 or vc_chosen_compiler_index > vc_x86_amd64_compiler_detection_index
  383. ):
  384. vc_chosen_compiler_index = vc_x86_amd64_compiler_detection_index
  385. vc_chosen_compiler_str = "x86_amd64"
  386. return vc_chosen_compiler_str
  387. def find_visual_c_batch_file(env):
  388. from SCons.Tool.MSCommon.vc import get_default_version, get_host_target, find_batch_file
  389. version = get_default_version(env)
  390. (host_platform, target_platform, _) = get_host_target(env)
  391. return find_batch_file(env, version, host_platform, target_platform)[0]
  392. def generate_cpp_hint_file(filename):
  393. if os.path.isfile(filename):
  394. # Don't overwrite an existing hint file since the user may have customized it.
  395. pass
  396. else:
  397. try:
  398. with open(filename, "w") as fd:
  399. fd.write("#define GDCLASS(m_class, m_inherits)\n")
  400. except IOError:
  401. print("Could not write cpp.hint file.")
  402. def generate_vs_project(env, num_jobs):
  403. batch_file = find_visual_c_batch_file(env)
  404. if batch_file:
  405. def build_commandline(commands):
  406. common_build_prefix = [
  407. 'cmd /V /C set "plat=$(PlatformTarget)"',
  408. '(if "$(PlatformTarget)"=="x64" (set "plat=x86_amd64"))',
  409. 'set "tools=yes"',
  410. '(if "$(Configuration)"=="release" (set "tools=no"))',
  411. 'call "' + batch_file + '" !plat!',
  412. ]
  413. result = " ^& ".join(common_build_prefix + [commands])
  414. return result
  415. env.AddToVSProject(env.core_sources)
  416. env.AddToVSProject(env.main_sources)
  417. env.AddToVSProject(env.modules_sources)
  418. env.AddToVSProject(env.scene_sources)
  419. env.AddToVSProject(env.servers_sources)
  420. env.AddToVSProject(env.editor_sources)
  421. # windows allows us to have spaces in paths, so we need
  422. # to double quote off the directory. However, the path ends
  423. # in a backslash, so we need to remove this, lest it escape the
  424. # last double quote off, confusing MSBuild
  425. env["MSVSBUILDCOM"] = build_commandline(
  426. "scons --directory=\"$(ProjectDir.TrimEnd('\\'))\" platform=windows progress=no target=$(Configuration) tools=!tools! -j"
  427. + str(num_jobs)
  428. )
  429. env["MSVSREBUILDCOM"] = build_commandline(
  430. "scons --directory=\"$(ProjectDir.TrimEnd('\\'))\" platform=windows progress=no target=$(Configuration) tools=!tools! vsproj=yes -j"
  431. + str(num_jobs)
  432. )
  433. env["MSVSCLEANCOM"] = build_commandline(
  434. "scons --directory=\"$(ProjectDir.TrimEnd('\\'))\" --clean platform=windows progress=no target=$(Configuration) tools=!tools! -j"
  435. + str(num_jobs)
  436. )
  437. # This version information (Win32, x64, Debug, Release, Release_Debug seems to be
  438. # required for Visual Studio to understand that it needs to generate an NMAKE
  439. # project. Do not modify without knowing what you are doing.
  440. debug_variants = ["debug|Win32"] + ["debug|x64"]
  441. release_variants = ["release|Win32"] + ["release|x64"]
  442. release_debug_variants = ["release_debug|Win32"] + ["release_debug|x64"]
  443. variants = debug_variants + release_variants + release_debug_variants
  444. debug_targets = ["bin\\godot.windows.tools.32.exe"] + ["bin\\godot.windows.tools.64.exe"]
  445. release_targets = ["bin\\godot.windows.opt.32.exe"] + ["bin\\godot.windows.opt.64.exe"]
  446. release_debug_targets = ["bin\\godot.windows.opt.tools.32.exe"] + ["bin\\godot.windows.opt.tools.64.exe"]
  447. targets = debug_targets + release_targets + release_debug_targets
  448. if not env.get("MSVS"):
  449. env["MSVS"]["PROJECTSUFFIX"] = ".vcxproj"
  450. env["MSVS"]["SOLUTIONSUFFIX"] = ".sln"
  451. env.MSVSProject(
  452. target=["#godot" + env["MSVSPROJECTSUFFIX"]],
  453. incs=env.vs_incs,
  454. srcs=env.vs_srcs,
  455. runfile=targets,
  456. buildtarget=targets,
  457. auto_build_solution=1,
  458. variant=variants,
  459. )
  460. else:
  461. print("Could not locate Visual Studio batch file to set up the build environment. Not generating VS project.")
  462. def precious_program(env, program, sources, **args):
  463. program = env.ProgramOriginal(program, sources, **args)
  464. env.Precious(program)
  465. return program
  466. def add_shared_library(env, name, sources, **args):
  467. library = env.SharedLibrary(name, sources, **args)
  468. env.NoCache(library)
  469. return library
  470. def add_library(env, name, sources, **args):
  471. library = env.Library(name, sources, **args)
  472. env.NoCache(library)
  473. return library
  474. def add_program(env, name, sources, **args):
  475. program = env.Program(name, sources, **args)
  476. env.NoCache(program)
  477. return program
  478. def CommandNoCache(env, target, sources, command, **args):
  479. result = env.Command(target, sources, command, **args)
  480. env.NoCache(result)
  481. return result
  482. def detect_darwin_sdk_path(platform, env):
  483. sdk_name = ""
  484. if platform == "osx":
  485. sdk_name = "macosx"
  486. var_name = "MACOS_SDK_PATH"
  487. elif platform == "iphone":
  488. sdk_name = "iphoneos"
  489. var_name = "IPHONESDK"
  490. elif platform == "iphonesimulator":
  491. sdk_name = "iphonesimulator"
  492. var_name = "IPHONESDK"
  493. else:
  494. raise Exception("Invalid platform argument passed to detect_darwin_sdk_path")
  495. if not env[var_name]:
  496. try:
  497. sdk_path = subprocess.check_output(["xcrun", "--sdk", sdk_name, "--show-sdk-path"]).strip().decode("utf-8")
  498. if sdk_path:
  499. env[var_name] = sdk_path
  500. except (subprocess.CalledProcessError, OSError):
  501. print("Failed to find SDK path while running xcrun --sdk {} --show-sdk-path.".format(sdk_name))
  502. raise
  503. def is_vanilla_clang(env):
  504. if not using_clang(env):
  505. return False
  506. try:
  507. version = subprocess.check_output([env.subst(env["CXX"]), "--version"]).strip().decode("utf-8")
  508. except (subprocess.CalledProcessError, OSError):
  509. print("Couldn't parse CXX environment variable to infer compiler version.")
  510. return False
  511. return not version.startswith("Apple")
  512. def get_compiler_version(env):
  513. """
  514. Returns an array of version numbers as ints: [major, minor, patch].
  515. The return array should have at least two values (major, minor).
  516. """
  517. if not env.msvc:
  518. # Not using -dumpversion as some GCC distros only return major, and
  519. # Clang used to return hardcoded 4.2.1: # https://reviews.llvm.org/D56803
  520. try:
  521. version = subprocess.check_output([env.subst(env["CXX"]), "--version"]).strip().decode("utf-8")
  522. except (subprocess.CalledProcessError, OSError):
  523. print("Couldn't parse CXX environment variable to infer compiler version.")
  524. return None
  525. else: # TODO: Implement for MSVC
  526. return None
  527. match = re.search("[0-9]+\.[0-9.]+", version)
  528. if match is not None:
  529. return list(map(int, match.group().split(".")))
  530. else:
  531. return None
  532. def using_gcc(env):
  533. return "gcc" in os.path.basename(env["CC"])
  534. def using_clang(env):
  535. return "clang" in os.path.basename(env["CC"])
  536. def show_progress(env):
  537. import sys
  538. from SCons.Script import Progress, Command, AlwaysBuild
  539. screen = sys.stdout
  540. # Progress reporting is not available in non-TTY environments since it
  541. # messes with the output (for example, when writing to a file)
  542. show_progress = env["progress"] and sys.stdout.isatty()
  543. node_count = 0
  544. node_count_max = 0
  545. node_count_interval = 1
  546. node_count_fname = str(env.Dir("#")) + "/.scons_node_count"
  547. import time, math
  548. class cache_progress:
  549. # The default is 1 GB cache and 12 hours half life
  550. def __init__(self, path=None, limit=1073741824, half_life=43200):
  551. self.path = path
  552. self.limit = limit
  553. self.exponent_scale = math.log(2) / half_life
  554. if env["verbose"] and path != None:
  555. screen.write(
  556. "Current cache limit is {} (used: {})\n".format(
  557. self.convert_size(limit), self.convert_size(self.get_size(path))
  558. )
  559. )
  560. self.delete(self.file_list())
  561. def __call__(self, node, *args, **kw):
  562. nonlocal node_count, node_count_max, node_count_interval, node_count_fname, show_progress
  563. if show_progress:
  564. # Print the progress percentage
  565. node_count += node_count_interval
  566. if node_count_max > 0 and node_count <= node_count_max:
  567. screen.write("\r[%3d%%] " % (node_count * 100 / node_count_max))
  568. screen.flush()
  569. elif node_count_max > 0 and node_count > node_count_max:
  570. screen.write("\r[100%] ")
  571. screen.flush()
  572. else:
  573. screen.write("\r[Initial build] ")
  574. screen.flush()
  575. def delete(self, files):
  576. if len(files) == 0:
  577. return
  578. if env["verbose"]:
  579. # Utter something
  580. screen.write("\rPurging %d %s from cache...\n" % (len(files), len(files) > 1 and "files" or "file"))
  581. [os.remove(f) for f in files]
  582. def file_list(self):
  583. if self.path is None:
  584. # Nothing to do
  585. return []
  586. # Gather a list of (filename, (size, atime)) within the
  587. # cache directory
  588. file_stat = [(x, os.stat(x)[6:8]) for x in glob.glob(os.path.join(self.path, "*", "*"))]
  589. if file_stat == []:
  590. # Nothing to do
  591. return []
  592. # Weight the cache files by size (assumed to be roughly
  593. # proportional to the recompilation time) times an exponential
  594. # decay since the ctime, and return a list with the entries
  595. # (filename, size, weight).
  596. current_time = time.time()
  597. file_stat = [(x[0], x[1][0], (current_time - x[1][1])) for x in file_stat]
  598. # Sort by the most recently accessed files (most sensible to keep) first
  599. file_stat.sort(key=lambda x: x[2])
  600. # Search for the first entry where the storage limit is
  601. # reached
  602. sum, mark = 0, None
  603. for i, x in enumerate(file_stat):
  604. sum += x[1]
  605. if sum > self.limit:
  606. mark = i
  607. break
  608. if mark is None:
  609. return []
  610. else:
  611. return [x[0] for x in file_stat[mark:]]
  612. def convert_size(self, size_bytes):
  613. if size_bytes == 0:
  614. return "0 bytes"
  615. size_name = ("bytes", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB")
  616. i = int(math.floor(math.log(size_bytes, 1024)))
  617. p = math.pow(1024, i)
  618. s = round(size_bytes / p, 2)
  619. return "%s %s" % (int(s) if i == 0 else s, size_name[i])
  620. def get_size(self, start_path="."):
  621. total_size = 0
  622. for dirpath, dirnames, filenames in os.walk(start_path):
  623. for f in filenames:
  624. fp = os.path.join(dirpath, f)
  625. total_size += os.path.getsize(fp)
  626. return total_size
  627. def progress_finish(target, source, env):
  628. nonlocal node_count, progressor
  629. with open(node_count_fname, "w") as f:
  630. f.write("%d\n" % node_count)
  631. progressor.delete(progressor.file_list())
  632. try:
  633. with open(node_count_fname) as f:
  634. node_count_max = int(f.readline())
  635. except:
  636. pass
  637. cache_directory = os.environ.get("SCONS_CACHE")
  638. # Simple cache pruning, attached to SCons' progress callback. Trim the
  639. # cache directory to a size not larger than cache_limit.
  640. cache_limit = float(os.getenv("SCONS_CACHE_LIMIT", 1024)) * 1024 * 1024
  641. progressor = cache_progress(cache_directory, cache_limit)
  642. Progress(progressor, interval=node_count_interval)
  643. progress_finish_command = Command("progress_finish", [], progress_finish)
  644. AlwaysBuild(progress_finish_command)
  645. def dump(env):
  646. # Dumps latest build information for debugging purposes and external tools.
  647. from json import dump
  648. def non_serializable(obj):
  649. return "<<non-serializable: %s>>" % (type(obj).__qualname__)
  650. with open(".scons_env.json", "w") as f:
  651. dump(env.Dictionary(), f, indent=4, default=non_serializable)