platform_methods.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. import os
  2. import sys
  3. import json
  4. import platform
  5. import uuid
  6. import functools
  7. import subprocess
  8. # NOTE: The multiprocessing module is not compatible with SCons due to conflict on cPickle
  9. JSON_SERIALIZABLE_TYPES = (bool, int, float, str)
  10. def run_in_subprocess(builder_function):
  11. @functools.wraps(builder_function)
  12. def wrapper(target, source, env):
  13. # Convert SCons Node instances to absolute paths
  14. target = [node.srcnode().abspath for node in target]
  15. source = [node.srcnode().abspath for node in source]
  16. # Short circuit on non-Windows platforms, no need to run in subprocess
  17. if sys.platform not in ("win32", "cygwin"):
  18. return builder_function(target, source, env)
  19. # Identify module
  20. module_name = builder_function.__module__
  21. function_name = builder_function.__name__
  22. module_path = sys.modules[module_name].__file__
  23. if module_path.endswith(".pyc") or module_path.endswith(".pyo"):
  24. module_path = module_path[:-1]
  25. # Subprocess environment
  26. subprocess_env = os.environ.copy()
  27. subprocess_env["PYTHONPATH"] = os.pathsep.join([os.getcwd()] + sys.path)
  28. # Keep only JSON serializable environment items
  29. filtered_env = dict((key, value) for key, value in env.items() if isinstance(value, JSON_SERIALIZABLE_TYPES))
  30. # Save parameters
  31. args = (target, source, filtered_env)
  32. data = dict(fn=function_name, args=args)
  33. json_path = os.path.join(os.environ["TMP"], uuid.uuid4().hex + ".json")
  34. with open(json_path, "wt") as json_file:
  35. json.dump(data, json_file, indent=2)
  36. json_file_size = os.stat(json_path).st_size
  37. if env["verbose"]:
  38. print(
  39. "Executing builder function in subprocess: "
  40. "module_path=%r, parameter_file=%r, parameter_file_size=%r, target=%r, source=%r"
  41. % (module_path, json_path, json_file_size, target, source)
  42. )
  43. try:
  44. exit_code = subprocess.call([sys.executable, module_path, json_path], env=subprocess_env)
  45. finally:
  46. try:
  47. os.remove(json_path)
  48. except OSError as e:
  49. # Do not fail the entire build if it cannot delete a temporary file
  50. print(
  51. "WARNING: Could not delete temporary file: path=%r; [%s] %s" % (json_path, e.__class__.__name__, e)
  52. )
  53. # Must succeed
  54. if exit_code:
  55. raise RuntimeError(
  56. "Failed to run builder function in subprocess: module_path=%r; data=%r" % (module_path, data)
  57. )
  58. return wrapper
  59. def subprocess_main(namespace):
  60. with open(sys.argv[1]) as json_file:
  61. data = json.load(json_file)
  62. fn = namespace[data["fn"]]
  63. fn(*data["args"])
  64. # CPU architecture options.
  65. architectures = ["x86_32", "x86_64", "arm32", "arm64", "rv64", "ppc32", "ppc64", "wasm32"]
  66. architecture_aliases = {
  67. "x86": "x86_32",
  68. "x64": "x86_64",
  69. "amd64": "x86_64",
  70. "armv7": "arm32",
  71. "armv8": "arm64",
  72. "arm64v8": "arm64",
  73. "aarch64": "arm64",
  74. "rv": "rv64",
  75. "riscv": "rv64",
  76. "riscv64": "rv64",
  77. "ppcle": "ppc32",
  78. "ppc": "ppc32",
  79. "ppc64le": "ppc64",
  80. }
  81. def detect_arch():
  82. host_machine = platform.machine().lower()
  83. if host_machine in architectures:
  84. return host_machine
  85. elif host_machine in architecture_aliases.keys():
  86. return architecture_aliases[host_machine]
  87. elif "86" in host_machine:
  88. # Catches x86, i386, i486, i586, i686, etc.
  89. return "x86_32"
  90. else:
  91. print("Unsupported CPU architecture: " + host_machine)
  92. print("Falling back to x86_64.")
  93. return "x86_64"