1import os
2import pipes
3import shlex
4import sys
5
6if not 'go' in config.root.llvm_bindings:
7    config.unsupported = True
8
9def find_executable(executable, path=None):
10    if path is None:
11        path = os.environ['PATH']
12    paths = path.split(os.pathsep)
13    base, ext = os.path.splitext(executable)
14
15    if (sys.platform == 'win32' or os.name == 'os2') and (ext != '.exe'):
16        executable = executable + '.exe'
17
18    if not os.path.isfile(executable):
19        for p in paths:
20            f = os.path.join(p, executable)
21            if os.path.isfile(f):
22                return f
23        return None
24    else:
25        return executable
26
27# Resolve certain symlinks in the first word of compiler.
28#
29# This is a Go-specific hack. cgo and other Go tools check $CC and $CXX for the
30# substring 'clang' to determine if the compiler is Clang. This won't work if
31# $CC is cc and cc is a symlink pointing to clang, as it is on Darwin.
32#
33# Go tools also have problems with ccache, so we disable it.
34def fixup_compiler_path(compiler):
35    args = shlex.split(compiler)
36    if args[0].endswith('ccache'):
37        args = args[1:]
38
39    path = find_executable(args[0])
40
41    try:
42        if path.endswith('/cc') and os.readlink(path) == 'clang':
43            args[0] = path[:len(path)-2] + 'clang'
44    except (AttributeError, OSError):
45        pass
46
47    try:
48        if path.endswith('/c++') and os.readlink(path) == 'clang++':
49            args[0] = path[:len(path)-3] + 'clang++'
50    except (AttributeError, OSError):
51        pass
52
53    return ' '.join([pipes.quote(arg) for arg in args])
54
55config.environment['CC'] = fixup_compiler_path(config.host_cc)
56config.environment['CXX'] = fixup_compiler_path(config.host_cxx)
57config.environment['CGO_LDFLAGS'] = config.host_ldflags
58