'Set the DEVELOPER_DIR environment for the Xcode toolchain.
On macOS, not all buildsystems support querying CC and CXX for the
compilers to use and instead query the Xcode toolchain for what
compiler to run. This side-steps the spack wrappers. In order to inject
spack into this setup, we need to copy (a subset of) Xcode.app and
replace the compiler executables with symlinks to the spack wrapper.
Currently, the stage is used to store the Xcode.app copies. We then set
the \'DEVELOPER_DIR\' environment variables to cause the xcrun and
related tools to use this Xcode.app.'

def setup_custom_environment(self, pkg, env):

    super(Clang, self).setup_custom_environment(pkg, env)
    if ((not self.is_apple) or (not pkg.use_xcode)):
        return
    xcode_select = Executable('xcode-select')
    real_root = xcode_select('--print-path', output=str).strip()
    real_root = os.path.dirname(os.path.dirname(real_root))
    developer_root = os.path.join(spack.stage_path, 'xcode-select', self.name, str(self.version))
    xcode_link = os.path.join(developer_root, 'Xcode.app')
    if (not os.path.exists(developer_root)):
        tty.warn(('Copying    Xcode    from    %s    to    %s    in    order    to    add    spack    wrappers    to    it.    Please    do    not    interrupt.' % (real_root, developer_root)))
        copytree(real_root, developer_root, symlinks=True, ignore=ignore_patterns('AppleTV*.platform', 'Watch*.platform', 'iPhone*.platform', 'Documentation', 'swift*'))
        real_dirs = ['Toolchains/XcodeDefault.xctoolchain/usr/bin', 'usr/bin']
        bins = ['c++', 'c89', 'c99', 'cc', 'clang', 'clang++', 'cpp']
        for real_dir in real_dirs:
            dev_dir = os.path.join(developer_root, 'Contents', 'Developer', real_dir)
            for fname in os.listdir(dev_dir):
                if (fname in bins):
                    os.unlink(os.path.join(dev_dir, fname))
                    os.symlink(os.path.join(spack.build_env_path, 'cc'), os.path.join(dev_dir, fname))
        os.symlink(developer_root, xcode_link)
    env.set('DEVELOPER_DIR', xcode_link)
