summaryrefslogtreecommitdiff
path: root/tools
diff options
context:
space:
mode:
authorElliott Sales de Andrade <quantum.analyst@gmail.com>2016-08-30 02:52:12 -0400
committerElliott Sales de Andrade <quantum.analyst@gmail.com>2016-08-30 04:17:47 -0400
commit859c5e28df90851838aacc4b9ad49d3630e4992a (patch)
tree8a2a553a03a11ad32dd25cae94a7939e58a5e381 /tools
parentaec1e88c21224691edbe9c2fbba712c267eb917d (diff)
downloadmeson-859c5e28df90851838aacc4b9ad49d3630e4992a.tar.gz
Use context manager in scripts.
Diffstat (limited to 'tools')
-rwxr-xr-xtools/ac_converter.py107
-rwxr-xr-xtools/cmake2meson.py55
2 files changed, 97 insertions, 65 deletions
diff --git a/tools/ac_converter.py b/tools/ac_converter.py
index 571481e1d..e88f2e2bb 100755
--- a/tools/ac_converter.py
+++ b/tools/ac_converter.py
@@ -25,28 +25,6 @@ that are unrelated to configure checks.
import sys
-print('''cc = meson.get_compiler('c')
-cdata = configuration_data()''')
-
-print('check_headers = [')
-
-for line in open(sys.argv[1]):
- line = line.strip()
- if line.startswith('#mesondefine') and \
- line.endswith('_H'):
- token = line.split()[1]
- tarr = token.split('_')[1:-1]
- tarr = [x.lower() for x in tarr]
- hname = '/'.join(tarr) + '.h'
- print(" ['%s', '%s']," % (token, hname))
-print(']\n')
-
-print('''foreach h : check_headers
- if cc.has_header(h.get(1))
- cdata.set(h.get(0), 1)
- endif
-endforeach
-''')
# Add stuff here as it is encountered.
function_data = \
@@ -242,18 +220,71 @@ function_data = \
'HAVE_PTHREAD_SET_NAME_NP': ('pthread_set_name_np', 'pthread.h'),
}
-print('check_functions = [')
+headers = []
+functions = []
+sizes = []
+with open(sys.argv[1]) as f:
+ for line in f:
+ line = line.strip()
+ arr = line.split()
+
+ # Check for headers.
+ if line.startswith('#mesondefine') and line.endswith('_H'):
+ token = line.split()[1]
+ tarr = token.split('_')[1:-1]
+ tarr = [x.lower() for x in tarr]
+ hname = '/'.join(tarr) + '.h'
+ headers.append((token, hname))
+
+ # Check for functions.
+ try:
+ token = arr[1]
+ if token in function_data:
+ fdata = function_data[token]
+ functions.append((token, fdata[0], fdata[1]))
+ elif token.startswith('HAVE_') and not token.endswith('_H'):
+ functions.append((token, ))
+ except Exception:
+ pass
+
+ # Check for sizeof tests.
+ if len(arr) != 2:
+ continue
+ elem = arr[1]
+ if elem.startswith('SIZEOF_'):
+ typename = elem.split('_', 1)[1] \
+ .replace('_P', '*') \
+ .replace('_', ' ') \
+ .lower() \
+ .replace('size t', 'size_t')
+ sizes.append((elem, typename))
-for line in open(sys.argv[1]):
- try:
- token = line.split()[1]
- if token in function_data:
- fdata = function_data[token]
- print(" ['%s', '%s', '#include<%s>']," % (token, fdata[0], fdata[1]))
- elif token.startswith('HAVE_') and not token.endswith('_H'):
- print('# check token', token)
- except Exception:
- pass
+print('''cc = meson.get_compiler('c')
+cdata = configuration_data()''')
+
+# Convert header checks.
+
+print('check_headers = [')
+for token, hname in headers:
+ print(" ['%s', '%s']," % (token, hname))
+print(']\n')
+
+print('''foreach h : check_headers
+ if cc.has_header(h.get(1))
+ cdata.set(h.get(0), 1)
+ endif
+endforeach
+''')
+
+# Convert function checks.
+
+print('check_functions = [')
+for token in functions:
+ if len(func) == 3:
+ token, fdata0, fdata1 = token
+ print(" ['%s', '%s', '#include<%s>']," % (token, fdata0, fdata1))
+ else:
+ print('# check token', token)
print(']\n')
print('''foreach f : check_functions
@@ -265,14 +296,8 @@ endforeach
# Convert sizeof checks.
-for line in open(sys.argv[1]):
- arr = line.strip().split()
- if len(arr) != 2:
- continue
- elem = arr[1]
- if elem.startswith('SIZEOF_'):
- typename = elem.split('_', 1)[1].replace('_P', '*').replace('_', ' ').lower().replace('size t', 'size_t')
- print("cdata.set('%s', cc.sizeof('%s'))" % (elem, typename))
+for elem, typename in size:
+ print("cdata.set('%s', cc.sizeof('%s'))" % (elem, typename))
print('''
configure_file(input : 'config.h.meson',
diff --git a/tools/cmake2meson.py b/tools/cmake2meson.py
index 098a6e0c8..7465d4513 100755
--- a/tools/cmake2meson.py
+++ b/tools/cmake2meson.py
@@ -252,39 +252,46 @@ class Converter:
subdir = self.cmake_root
cfile = os.path.join(subdir, 'CMakeLists.txt')
try:
- cmakecode = open(cfile).read()
+ with open(cfile) as f:
+ cmakecode = f.read()
except FileNotFoundError:
print('\nWarning: No CMakeLists.txt in', subdir, '\n')
return
p = Parser(cmakecode)
- outfile = open(os.path.join(subdir, 'meson.build'), 'w')
- for t in p.parse():
- if t.name == 'add_subdirectory':
- #print('\nRecursing to subdir', os.path.join(self.cmake_root, t.args[0].value), '\n')
- self.convert(os.path.join(subdir, t.args[0].value))
- #print('\nReturning to', self.cmake_root, '\n')
- self.write_entry(outfile, t)
+ with open(os.path.join(subdir, 'meson.build'), 'w') as outfile:
+ for t in p.parse():
+ if t.name == 'add_subdirectory':
+ # print('\nRecursing to subdir',
+ # os.path.join(self.cmake_root, t.args[0].value),
+ # '\n')
+ self.convert(os.path.join(subdir, t.args[0].value))
+ # print('\nReturning to', self.cmake_root, '\n')
+ self.write_entry(outfile, t)
if subdir == self.cmake_root and len(self.options) > 0:
self.write_options()
def write_options(self):
- optfile = open(os.path.join(self.cmake_root, 'meson_options.txt'), 'w')
- for o in self.options:
- (optname, description, default) = o
- if default is None:
- defaultstr = ''
- else:
- if default == 'OFF':
- typestr = ' type : boolean,'
- default = 'false'
- elif default == 'ON':
- default = 'true'
- typestr = ' type : boolean,'
+ filename = os.path.join(self.cmake_root, 'meson_options.txt')
+ with open(filename, 'w') as optfile:
+ for o in self.options:
+ (optname, description, default) = o
+ if default is None:
+ defaultstr = ''
else:
- typestr = ' type : string,'
- defaultstr = ' value : %s,' % default
- line = "option(%s,%s%s description : '%s')\n" % (optname, typestr, defaultstr, description)
- optfile.write(line)
+ if default == 'OFF':
+ typestr = ' type : boolean,'
+ default = 'false'
+ elif default == 'ON':
+ default = 'true'
+ typestr = ' type : boolean,'
+ else:
+ typestr = ' type : string,'
+ defaultstr = ' value : %s,' % default
+ line = "option(%s,%s%s description : '%s')\n" % (optname,
+ typestr,
+ defaultstr,
+ description)
+ optfile.write(line)
if __name__ == '__main__':
if len(sys.argv) != 2: