godot/platform_methods.py
Viktor Ferenczi c5bd0c37ce Running builder (content generator) functions in subprocesses on Windows
- Refactored all builder (make_*) functions into separate Python modules along to the build tree
- Introduced utility function to wrap all invocations on Windows, but does not change it elsewhere
- Introduced stub to use the builders module as a stand alone script and invoke a selected function

There is a problem with file handles related to writing generated content (*.gen.h and *.gen.cpp)
on Windows, which randomly causes a SHARING VIOLATION error to the compiler resulting in flaky
builds. Running all such content generators in a new subprocess instead of directly inside the
build script works around the issue.

Yes, I tried the multiprocessing module. It did not work due to conflict with SCons on cPickle.
Suggested workaround did not fully work either.

Using the run_in_subprocess wrapper on osx and x11 platforms as well for consistency. In case of
running a cross-compilation on Windows they would still be used, but likely it will not happen
in practice. What counts is that the build itself is running on which platform, not the target
platform.

Some generated files are written directly in an SConstruct or SCsub file, before the parallel build starts. They don't need to be written in a subprocess, apparently, so I left them as is.
2018-07-27 21:37:55 +02:00

67 lines
2.2 KiB
Python

import os
import sys
import json
import uuid
import functools
import subprocess
# NOTE: The multiprocessing module is not compatible with SCons due to conflict on cPickle
def run_in_subprocess(builder_function):
@functools.wraps(builder_function)
def wrapper(target, source, env):
# Convert SCons Node instances to absolute paths
target = [node.srcnode().abspath for node in target]
source = [node.srcnode().abspath for node in source]
# Short circuit on non-Windows platforms
if os.name != 'nt':
return builder_function(target, source, None)
# Identify module
module_name = builder_function.__module__
function_name = builder_function.__name__
module_path = sys.modules[module_name].__file__
if module_path.endswith('.pyc') or module_path.endswith('.pyo'):
module_path = module_path[:-1]
# Subprocess environment
subprocess_env = os.environ.copy()
subprocess_env['PYTHONPATH'] = os.pathsep.join([os.getcwd()] + sys.path)
# Save parameters
args = (target, source, None)
data = dict(fn=function_name, args=args)
json_path = os.path.join(os.environ['TMP'], uuid.uuid4().hex + '.json')
with open(json_path, 'wt') as json_file:
json.dump(data, json_file, indent=2)
try:
print('Executing builder function in subprocess: module_path=%r; data=%r' % (module_path, data))
exit_code = subprocess.call([sys.executable, module_path, json_path], env=subprocess_env)
finally:
try:
os.remove(json_path)
except (OSError, IOError) as e:
# Do not fail the entire build if it cannot delete a temporary file
print('WARNING: Could not delete temporary file: path=%r; [%s] %s' %
(json_path, e.__class__.__name__, e))
# Must succeed
if exit_code:
raise RuntimeError(
'Failed to run builder function in subprocess: module_path=%r; data=%r' % (module_path, data))
return wrapper
def subprocess_main(namespace):
with open(sys.argv[1]) as json_file:
data = json.load(json_file)
fn = namespace[data['fn']]
fn(*data['args'])