From 9558dc14fb88bcee3997cba6f42e0b5377861edb Mon Sep 17 00:00:00 2001 From: Dan Liew Date: Fri, 26 Feb 2016 18:24:57 +0000 Subject: [PATCH] Refactor ``exec_pyg_scripts()`` so that it is usable externally via a new ``pyg2hpp.py`` script. This will allow other build systems to take ``pyg`` files and generate the corresponding ``hpp`` files. --- scripts/mk_util.py | 14 +++++++------- scripts/pyg2hpp.py | 42 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 49 insertions(+), 7 deletions(-) create mode 100755 scripts/pyg2hpp.py diff --git a/scripts/mk_util.py b/scripts/mk_util.py index a02a9b40f..dbc83353f 100644 --- a/scripts/mk_util.py +++ b/scripts/mk_util.py @@ -2488,10 +2488,10 @@ DOUBLE = 2 STRING = 3 SYMBOL = 4 UINT_MAX = 4294967295 -CURR_PYG = None +CURRENT_PYG_HPP_DEST_DIR = None -def get_curr_pyg(): - return CURR_PYG +def get_current_pyg_hpp_dest_dir(): + return CURRENT_PYG_HPP_DEST_DIR TYPE2CPK = { UINT : 'CPK_UINT', BOOL : 'CPK_BOOL', DOUBLE : 'CPK_DOUBLE', STRING : 'CPK_STRING', SYMBOL : 'CPK_SYMBOL' } TYPE2CTYPE = { UINT : 'unsigned', BOOL : 'bool', DOUBLE : 'double', STRING : 'char const *', SYMBOL : 'symbol' } @@ -2524,8 +2524,8 @@ def to_c_method(s): return s.replace('.', '_') def def_module_params(module_name, export, params, class_name=None, description=None): - pyg = get_curr_pyg() - dirname = os.path.split(get_curr_pyg())[0] + dirname = get_current_pyg_hpp_dest_dir() + assert(os.path.exists(dirname)) if class_name is None: class_name = '%s_params' % module_name hpp = os.path.join(dirname, '%s.hpp' % class_name) @@ -2590,12 +2590,12 @@ def _execfile(file, globals=globals(), locals=locals()): # Execute python auxiliary scripts that generate extra code for Z3. def exec_pyg_scripts(): - global CURR_PYG + global CURRENT_PYG_HPP_DEST_DIR for root, dirs, files in os.walk('src'): for f in files: if f.endswith('.pyg'): script = os.path.join(root, f) - CURR_PYG = script + CURRENT_PYG_HPP_DEST_DIR = root _execfile(script, PYG_GLOBALS) # TODO: delete after src/ast/pattern/expr_pattern_match diff --git a/scripts/pyg2hpp.py b/scripts/pyg2hpp.py new file mode 100755 index 000000000..f1ada2b84 --- /dev/null +++ b/scripts/pyg2hpp.py @@ -0,0 +1,42 @@ +#!/usr/bin/env python +""" +Reads a pyg file and emits the corresponding +C++ header file into the specified destination +directory. +""" +import mk_util +import argparse +import logging +import os +import sys + +def main(args): + logging.basicConfig(level=logging.INFO) + parser = argparse.ArgumentParser(description=__doc__) + parser.add_argument("pyg_file", help="pyg file") + parser.add_argument("destination_dir", help="destination directory") + pargs = parser.parse_args(args) + + if not os.path.exists(pargs.pyg_file): + logging.error('"{}" does not exist'.format(pargs.pyg_file)) + return 1 + + if not os.path.exists(pargs.destination_dir): + logging.error('"{}" does not exist'.format(pargs.destination_dir)) + return 1 + + if not os.path.isdir(pargs.destination_dir): + logging.error('"{}" is not a directory'.format(pargs.destination_dir)) + return 1 + + pyg_full_path = os.path.abspath(pargs.pyg_file) + destination_dir_full_path = os.path.abspath(pargs.destination_dir) + logging.info('Using {}'.format(pyg_full_path)) + # Use the existing infrastructure to do this + mk_util.CURRENT_PYG_HPP_DEST_DIR = destination_dir_full_path + mk_util._execfile(pyg_full_path, mk_util.PYG_GLOBALS) + return 0 + +if __name__ == '__main__': + sys.exit(main(sys.argv[1:])) +