2
# This Source Code Form is subject to the terms of the Mozilla Public
3
# License, v. 2.0. If a copy of the MPL was not distributed with this
4
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
7
# This script find the version of libstdc++ and prints it as single number
8
# with 8 bits per element. For example, GLIBCXX_3.4.10 becomes
9
# 3 << 16 | 4 << 8 | 10 = 197642. This format is easy to use
10
# in the C preprocessor.
12
# We find out both the host and target versions. Since the output
13
# will be used from shell, we just print the two assignments and evaluate
21
re_for_ld = re.compile('.*\((.*)\).*')
23
def parse_readelf_line(x):
24
"""Return the version from a readelf line that looks like:
25
0x00ec: Rev: 1 Flags: none Index: 8 Cnt: 2 Name: GLIBCXX_3.4.6
27
return x.split(':')[-1].split('_')[-1].strip()
30
"""Parse a line from the output of ld -t. The output of gold is just
31
the full path, gnu ld prints "-lstdc++ (path)".
33
t = re_for_ld.match(x)
35
return t.groups()[0].strip()
39
"""Covert the string '1.2.3' into the list [1,2,3]
41
return [int(x) for x in v.split('.')]
44
"""Compare versions in the form 'a.b.c'
46
for (i, j) in zip(split_ver(a), split_ver(b)):
52
"""Encode the version as a single number.
55
return t[0] << 16 | t[1] << 8 | t[2]
58
"""Given the value of environment variable CXX or HOST_CXX, find the
59
version of the libstdc++ it uses.
62
args += ['-shared', '-Wl,-t']
63
p = subprocess.Popen(args, stderr=subprocess.STDOUT, stdout=subprocess.PIPE)
64
candidates = [x for x in p.stdout if 'libstdc++.so' in x]
65
assert len(candidates) == 1
66
libstdcxx = parse_ld_line(candidates[-1])
68
p = subprocess.Popen(['readelf', '-V', libstdcxx], stdout=subprocess.PIPE)
69
versions = [parse_readelf_line(x)
70
for x in p.stdout.readlines() if 'Name: GLIBCXX' in x]
71
last_version = sorted(versions, cmp = cmp_ver)[-1]
72
return encode_ver(last_version)
74
if __name__ == '__main__':
75
if os.uname()[0] == 'Darwin':
76
sdk_dir = os.environ['MACOS_SDK_DIR']
77
if 'MacOSX10.5.sdk' in sdk_dir:
81
target_ver = encode_ver('3.4.9')
82
host_ver = encode_ver('3.4.9')
84
cxx_env = os.environ['CXX']
85
target_ver = find_version(cxx_env)
86
host_cxx_env = os.environ.get('HOST_CXX', cxx_env)
87
host_ver = find_version(host_cxx_env)
89
print 'MOZ_LIBSTDCXX_TARGET_VERSION=%s' % target_ver
90
print 'MOZ_LIBSTDCXX_HOST_VERSION=%s' % host_ver