From 8a35f744c7b285d5087107debc22e4274c8f39d8 Mon Sep 17 00:00:00 2001 From: Dan Liew Date: Tue, 8 Mar 2016 19:08:47 +0000 Subject: [PATCH] Move ``mk_def_file_internal()`` out of ``mk_util.py`` into ``mk_genfile_common.py`` and adapt ``mk_util.py`` and ``mk_def_file.py`` to use the code at its new location. Whilst I'm here also reindent ``mk_def_file.py`` and make it use some of the code in ``mk_genfile_common.py`` to avoid code duplication. 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_def_file.py | 34 +++++++++++++++++----------------- scripts/mk_genfile_common.py | 33 +++++++++++++++++++++++++++++++++ scripts/mk_util.py | 23 +---------------------- 3 files changed, 51 insertions(+), 39 deletions(-) diff --git a/scripts/mk_def_file.py b/scripts/mk_def_file.py index 2b9b5a071..07418a975 100755 --- a/scripts/mk_def_file.py +++ b/scripts/mk_def_file.py @@ -6,31 +6,31 @@ exported symbols of a dll. This file can be passed to the ``/DEF`` to the linker used by MSVC. """ -import mk_util +import mk_genfile_common import argparse import logging import os import sys def main(args): - logging.basicConfig(level=logging.INFO) - parser = argparse.ArgumentParser(description=__doc__) - parser.add_argument("output_file", help="output def file path") - parser.add_argument("dllname", help="dllname to use in def file") - parser.add_argument("api_files", nargs="+") - pargs = parser.parse_args(args) + logging.basicConfig(level=logging.INFO) + parser = argparse.ArgumentParser(description=__doc__) + parser.add_argument("output_file", help="output def file path") + parser.add_argument("dllname", help="dllname to use in def file") + parser.add_argument("api_files", nargs="+") + pargs = parser.parse_args(args) - for api_file in pargs.api_files: - if not os.path.exists(api_file): - logging.error('"{}" does not exist'.format(api_file)) - return 1 + if not mk_genfile_common.check_files_exist(pargs.api_files): + logging.error('One or more api files do not exist') + return 1 - mk_util.mk_def_file_internal( - pargs.output_file, - pargs.dllname, - pargs.api_files) - return 0 + mk_genfile_common.mk_def_file_internal( + pargs.output_file, + pargs.dllname, + pargs.api_files) + logging.info('Generated "{}"'.format(pargs.output_file)) + return 0 if __name__ == '__main__': - sys.exit(main(sys.argv[1:])) + sys.exit(main(sys.argv[1:])) diff --git a/scripts/mk_genfile_common.py b/scripts/mk_genfile_common.py index abe10a0d1..e0b9bce48 100644 --- a/scripts/mk_genfile_common.py +++ b/scripts/mk_genfile_common.py @@ -124,3 +124,36 @@ def mk_z3consts_py_internal(api_files, output_dir): api.close() z3consts.close() return z3consts_output_path + +############################################################################### +# Functions for generating a "module definition file" for MSVC +############################################################################### + +def mk_def_file_internal(defname, dll_name, export_header_files): + """ + Writes to a module definition file to a file named ``defname``. + + ``dll_name`` is the name of the dll (without the ``.dll`` suffix). + ``export_header_file`` is a list of header files to scan for symbols + to include in the module definition file. + """ + assert isinstance(export_header_files, list) + pat1 = re.compile(".*Z3_API.*") + fout = open(defname, 'w') + fout.write('LIBRARY "%s"\nEXPORTS\n' % dll_name) + num = 1 + for export_header_file in export_header_files: + api = open(export_header_file, 'r') + for line in api: + m = pat1.match(line) + if m: + words = re.split('\W+', line) + i = 0 + for w in words: + if w == 'Z3_API': + f = words[i+1] + fout.write('\t%s @%s\n' % (f, num)) + i = i + 1 + num = num + 1 + api.close() + fout.close() diff --git a/scripts/mk_util.py b/scripts/mk_util.py index da3c6eef9..f9d639daf 100644 --- a/scripts/mk_util.py +++ b/scripts/mk_util.py @@ -2913,28 +2913,7 @@ def mk_def_file(c): dot_h_c = c.find_file(dot_h, c.name) api = os.path.join(dot_h_c.src_dir, dot_h) export_header_files.append(api) - mk_def_file_internal(defname, dll_name, export_header_files) - -def mk_def_file_internal(defname, dll_name, export_header_files): - pat1 = re.compile(".*Z3_API.*") - fout = open(defname, 'w') - fout.write('LIBRARY "%s"\nEXPORTS\n' % dll_name) - num = 1 - for export_header_file in export_header_files: - api = open(export_header_file, 'r') - for line in api: - m = pat1.match(line) - if m: - words = re.split('\W+', line) - i = 0 - for w in words: - if w == 'Z3_API': - f = words[i+1] - fout.write('\t%s @%s\n' % (f, num)) - i = i + 1 - num = num + 1 - api.close() - fout.close() + mk_genfile_common.mk_def_file_internal(defname, dll_name, export_header_files) if VERBOSE: print("Generated '%s'" % defname)