From 404aa2a5a0355db1bbe546be138d3ea0477738cb Mon Sep 17 00:00:00 2001 From: Dan Liew Date: Tue, 8 Mar 2016 20:29:20 +0000 Subject: [PATCH] Move ``mk_gparams_register_modules_internal()`` from ``mk_util.py`` to ``mk_genfile_common.py`` and adapt ``mk_util.py`` and ``mk_gparams_register_modules_cpp.py`` to use the code at its new location. The interface has been changed slightly so that ``mk_gparams_register_modules_internal()`` now returns the path to the generated file. The motivation behind this so that clients of the function know the path to the generated file. Whilst I'm here reindent ``mk_gparams_register_modules_cpp.py`` and the relevant code in ``mk_util.py``. Also remove duplicated code that is now available in ``mk_genfile_common.py``. The purpose of this change is to have Python code common to the Python and CMake build systems separate from Python code that is only used for the Python build system. --- scripts/mk_genfile_common.py | 61 ++++++++++++++++++++++ scripts/mk_gparams_register_modules_cpp.py | 46 +++++++--------- scripts/mk_util.py | 59 +++------------------ 3 files changed, 86 insertions(+), 80 deletions(-) diff --git a/scripts/mk_genfile_common.py b/scripts/mk_genfile_common.py index e0b9bce48..71db2518a 100644 --- a/scripts/mk_genfile_common.py +++ b/scripts/mk_genfile_common.py @@ -157,3 +157,64 @@ def mk_def_file_internal(defname, dll_name, export_header_files): num = num + 1 api.close() fout.close() + +############################################################################### +# Functions for generating ``gparams_register_modules.cpp`` +############################################################################### +def mk_gparams_register_modules_internal(component_src_dirs, path): + """ + Generate a ``gparams_register_modules.cpp`` file in the directory ``path``. + Returns the path to the generated file. + + This file implements the procedure + + ``` + void gparams_register_modules() + ``` + + This procedure is invoked by gparams::init() + """ + assert isinstance(component_src_dirs, list) + assert check_dir_exists(path) + cmds = [] + mod_cmds = [] + mod_descrs = [] + fullname = os.path.join(path, 'gparams_register_modules.cpp') + fout = open(fullname, 'w') + fout.write('// Automatically generated file.\n') + fout.write('#include"gparams.h"\n') + reg_pat = re.compile('[ \t]*REG_PARAMS\(\'([^\']*)\'\)') + reg_mod_pat = re.compile('[ \t]*REG_MODULE_PARAMS\(\'([^\']*)\', *\'([^\']*)\'\)') + reg_mod_descr_pat = re.compile('[ \t]*REG_MODULE_DESCRIPTION\(\'([^\']*)\', *\'([^\']*)\'\)') + for component_src_dir in component_src_dirs: + h_files = filter(lambda f: f.endswith('.h') or f.endswith('.hpp'), os.listdir(component_src_dir)) + for h_file in h_files: + added_include = False + fin = open(os.path.join(component_src_dir, h_file), 'r') + for line in fin: + m = reg_pat.match(line) + if m: + if not added_include: + added_include = True + fout.write('#include"%s"\n' % h_file) + cmds.append((m.group(1))) + m = reg_mod_pat.match(line) + if m: + if not added_include: + added_include = True + fout.write('#include"%s"\n' % h_file) + mod_cmds.append((m.group(1), m.group(2))) + m = reg_mod_descr_pat.match(line) + if m: + mod_descrs.append((m.group(1), m.group(2))) + fin.close() + fout.write('void gparams_register_modules() {\n') + for code in cmds: + fout.write('{ param_descrs d; %s(d); gparams::register_global(d); }\n' % code) + for (mod, code) in mod_cmds: + fout.write('{ param_descrs * d = alloc(param_descrs); %s(*d); gparams::register_module("%s", d); }\n' % (code, mod)) + for (mod, descr) in mod_descrs: + fout.write('gparams::register_module_descr("%s", "%s");\n' % (mod, descr)) + fout.write('}\n') + fout.close() + return fullname diff --git a/scripts/mk_gparams_register_modules_cpp.py b/scripts/mk_gparams_register_modules_cpp.py index 1c7e221ba..cf6e8da96 100755 --- a/scripts/mk_gparams_register_modules_cpp.py +++ b/scripts/mk_gparams_register_modules_cpp.py @@ -6,42 +6,34 @@ and generates a ``gparams_register_modules.cpp`` file in the destination directory that defines a function ``void gparams_register_modules()``. """ -import mk_util +import mk_genfile_common import argparse import logging import os import sys -def check_dir(path): - if not os.path.exists(path): - logging.error('"{}" does not exist'.format(path)) - return 1 - - if not os.path.isdir(path): - logging.error('"{}" is not a directory'.format(path)) - return 1 - - return 0 - - def main(args): - logging.basicConfig(level=logging.INFO) - parser = argparse.ArgumentParser(description=__doc__) - parser.add_argument("destination_dir", help="destination directory") - parser.add_argument("source_dirs", nargs="+", - help="One or more source directories to search") - pargs = parser.parse_args(args) + logging.basicConfig(level=logging.INFO) + parser = argparse.ArgumentParser(description=__doc__) + parser.add_argument("destination_dir", help="destination directory") + parser.add_argument("source_dirs", nargs="+", + help="One or more source directories to search") + pargs = parser.parse_args(args) - if check_dir(pargs.destination_dir) != 0: - return 1 + if not mk_genfile_common.check_dir_exists(pargs.destination_dir): + return 1 - for source_dir in pargs.source_dirs: - if check_dir(source_dir) != 0: - return 1 + for source_dir in pargs.source_dirs: + if not mk_genfile_common.check_dir_exists(source_dir): + return 1 - mk_util.mk_gparams_register_modules_internal(pargs.source_dirs, pargs.destination_dir) - return 0 + output = mk_genfile_common.mk_gparams_register_modules_internal( + pargs.source_dirs, + pargs.destination_dir + ) + logging.info('Generated "{}"'.format(output)) + return 0 if __name__ == '__main__': - sys.exit(main(sys.argv[1:])) + sys.exit(main(sys.argv[1:])) diff --git a/scripts/mk_util.py b/scripts/mk_util.py index f9d639daf..260625ec4 100644 --- a/scripts/mk_util.py +++ b/scripts/mk_util.py @@ -2839,61 +2839,14 @@ def mk_all_mem_initializer_cpps(): cnames.append(c.name) mk_mem_initializer_cpp(cnames, c.src_dir) -# Generate an ``gparams_register_modules.cpp`` at path. -# This file implements the procedure -# void gparams_register_modules() -# This procedure is invoked by gparams::init() def mk_gparams_register_modules(cnames, path): - component_src_dirs = [] - for cname in cnames: - c = get_component(cname) - component_src_dirs.append(c.src_dir) - mk_gparams_register_modules_internal(component_src_dirs, path) - -def mk_gparams_register_modules_internal(component_src_dirs, path): - cmds = [] - mod_cmds = [] - mod_descrs = [] - fullname = os.path.join(path, 'gparams_register_modules.cpp') - fout = open(fullname, 'w') - fout.write('// Automatically generated file.\n') - fout.write('#include"gparams.h"\n') - reg_pat = re.compile('[ \t]*REG_PARAMS\(\'([^\']*)\'\)') - reg_mod_pat = re.compile('[ \t]*REG_MODULE_PARAMS\(\'([^\']*)\', *\'([^\']*)\'\)') - reg_mod_descr_pat = re.compile('[ \t]*REG_MODULE_DESCRIPTION\(\'([^\']*)\', *\'([^\']*)\'\)') - for component_src_dir in component_src_dirs: - h_files = filter(lambda f: f.endswith('.h') or f.endswith('.hpp'), os.listdir(component_src_dir)) - for h_file in h_files: - added_include = False - fin = open(os.path.join(component_src_dir, h_file), 'r') - for line in fin: - m = reg_pat.match(line) - if m: - if not added_include: - added_include = True - fout.write('#include"%s"\n' % h_file) - cmds.append((m.group(1))) - m = reg_mod_pat.match(line) - if m: - if not added_include: - added_include = True - fout.write('#include"%s"\n' % h_file) - mod_cmds.append((m.group(1), m.group(2))) - m = reg_mod_descr_pat.match(line) - if m: - mod_descrs.append((m.group(1), m.group(2))) - fin.close() - fout.write('void gparams_register_modules() {\n') - for code in cmds: - fout.write('{ param_descrs d; %s(d); gparams::register_global(d); }\n' % code) - for (mod, code) in mod_cmds: - fout.write('{ param_descrs * d = alloc(param_descrs); %s(*d); gparams::register_module("%s", d); }\n' % (code, mod)) - for (mod, descr) in mod_descrs: - fout.write('gparams::register_module_descr("%s", "%s");\n' % (mod, descr)) - fout.write('}\n') - fout.close() + component_src_dirs = [] + for cname in cnames: + c = get_component(cname) + component_src_dirs.append(c.src_dir) + generated_file = mk_genfile_common.mk_gparams_register_modules_internal(component_src_dirs, path) if VERBOSE: - print("Generated '%s'" % fullname) + print("Generated '{}'".format(generated_file)) def mk_all_gparams_register_modules(): if not ONLY_MAKEFILES: