'Run before changing automatically generated contents of the
_sysconfigdata.py, which is used by distutils to figure out what
executables to use while compiling and linking extensions. If we build
extensions with spack those executables should be spack\'s wrappers.
Spack partially covers this by setting environment variables that
are also accounted for by distutils. Currently there is one more known
variable that must be set, which is LDSHARED, so the method saves its
autogenerated value to pass it to the dependent package\'s setup script.'

def _save_distutil_vars(self, prefix):

    self._distutil_vars = {}
    input_filename = None
    for filename in [join_path(lib_dir, 'python{0}'.format(self.version.up_to(2)), self.sysconfigfilename) for lib_dir in [prefix.lib, prefix.lib64]]:
        if os.path.isfile(filename):
            input_filename = filename
            break
    if (not input_filename):
        return
    input_dict = None
    try:
        with open(input_filename) as input_file:
            match = re.search('build_time_vars\\s*=\\s*(?P<dict>{.*})', input_file.read(), flags=re.DOTALL)
            if match:
                input_dict = ast.literal_eval(match.group('dict'))
    except (IOError, SyntaxError):
        pass
    if (not input_dict):
        tty.warn(("Failed    to    find    'build_time_vars'    dictionary    in    file    '%s'.    This    might    cause    the    extensions    that    are    installed    with    distutils    to    call    compilers    directly    avoiding    Spack's    wrappers." % input_filename))
        return
    for var_name in Python._DISTUTIL_VARS_TO_SAVE:
        if (var_name in input_dict):
            self._distutil_vars[var_name] = input_dict[var_name]
        else:
            tty.warn(("Failed    to    find    key    '%s'    in    'build_time_vars'    dictionary    in    file    '%s'.    This    might    cause    the    extensions    that    are    installed    with    distutils    to    call    compilers    directly    avoiding    Spack's    wrappers." % (var_name, input_filename)))
    if (len(self._distutil_vars) > 0):
        output_filename = None
        try:
            output_filename = join_path(spack.store.layout.metadata_path(self.spec), Python._DISTUTIL_CACHE_FILENAME)
            with open(output_filename, 'w') as output_file:
                sjson.dump(self._distutil_vars, output_file)
        except:
            tty.warn("Failed    to    save    metadata    for    distutils.    This    might    cause    the    extensions    that    are    installed    with    distutils    to    call    compilers    directly    avoiding    Spack's    wrappers.")
            self._distutil_vars = {}
            if output_filename:
                force_remove(output_filename)
