blob: a9749eae9d408fa3a63bf06620fc635dec302af4 [file] [log] [blame]
Brad Bishop1a4b7ee2018-12-16 17:11:34 -08001#!/usr/bin/env python3
2
3import os
4import sys
5
6def bail(msg):
7 print(msg, file=sys.stderr)
8 sys.exit(1)
9
10_MARKER = '@@'
11def transform_line(line):
12 # Substitute any special markers of this form:
13 # @@ENV@@
14 # with the value of ENV, split into meson array syntax.
15 start = line.find(_MARKER)
16 if start == -1:
17 return line
18
19 end = line.rfind(_MARKER)
20 if end == start:
21 return line
22
23 # Lookup value of the env var.
24 var = line[start+len(_MARKER):end]
25 try:
26 val = os.environ[var]
27 except KeyError:
28 bail('cannot generate meson.cross; env var %s not set' % var)
29
30 # Transform into meson array.
31 val = ["'%s'" % x for x in val.split()]
32 val = ', '.join(val)
33 val = '[%s]' % val
34
35 before = line[:start]
36 after = line[end+len(_MARKER):]
37
38 return '%s%s%s' % (before, val, after)
39
40# Make sure this is really an SDK extraction environment.
41try:
42 sysroot = os.environ['OECORE_NATIVE_SYSROOT']
43except KeyError:
44 bail('OECORE_NATIVE_SYSROOT env var must be set')
45
46cross_file = os.path.join(sysroot, 'usr/share/meson/meson.cross')
47tmp_cross_file = '%s.tmp' % cross_file
48
49# Read through and transform the current meson.cross.
50lines = []
51with open(cross_file, 'r') as f:
52 for line in f:
53 lines.append(transform_line(line))
54
55# Write the transformed result to a tmp file and atomically rename it. In case
56# we crash during the file write, we don't want an invalid meson.cross file.
57with open(tmp_cross_file, 'w') as f:
58 for line in lines:
59 f.write(line)
60 f.flush()
61 os.fdatasync(f.fileno())
62os.rename(tmp_cross_file, cross_file)