mirror of
https://github.com/Z3Prover/z3
synced 2025-04-22 16:45:31 +00:00
auto gen for z3consts.py
Signed-off-by: Leonardo de Moura <leonardo@microsoft.com>
This commit is contained in:
parent
a9a46ec145
commit
d40c62d8aa
4 changed files with 96 additions and 344 deletions
|
@ -62,12 +62,15 @@ add_lib('portfolio', ['smtlogic_tactics', 'ufbv_tactic', 'fpa', 'aig', 'muz_qe',
|
|||
add_lib('api', ['portfolio', 'user_plugin'])
|
||||
add_exe('shell', ['api', 'sat', 'extra_cmds'], exe_name='z3')
|
||||
add_exe('test', ['api', 'fuzzing'], exe_name='test-z3')
|
||||
add_dll('api_dll', ['api', 'sat', 'extra_cmds'], 'api/dll', dll_name='z3', export_files=['z3_api.h', 'z3_poly.h'])
|
||||
API_files = ['z3_api.h', 'z3_poly.h']
|
||||
add_dll('api_dll', ['api', 'sat', 'extra_cmds'], 'api/dll', dll_name='z3', export_files=API_files)
|
||||
add_dot_net_dll('dotnet', ['api_dll'], 'bindings/dotnet/Microsoft.Z3', dll_name='Microsoft.Z3', assembly_info_dir='Properties')
|
||||
add_dot_net_dll('dotnetV3', ['api_dll'], 'bindings/dotnet/Microsoft.Z3V3', dll_name='Microsoft.Z3V3')
|
||||
set_python_dir('bindings/python')
|
||||
|
||||
mk_auto_src()
|
||||
update_version(4, 2, 0, 0)
|
||||
mk_auto_src()
|
||||
mk_bindings(API_files)
|
||||
|
||||
mk_makefile()
|
||||
|
||||
|
|
|
@ -23,6 +23,7 @@ DEBUG_MODE=False
|
|||
SHOW_CPPS = True
|
||||
VS_X64 = False
|
||||
ONLY_MAKEFILES = False
|
||||
PYTHON_DIR=None
|
||||
|
||||
if os.name == 'nt':
|
||||
IS_WINDOW=True
|
||||
|
@ -112,6 +113,15 @@ def set_build_dir(d):
|
|||
BUILD_DIR = d
|
||||
REV_BUILD_DIR = reverse_path(d)
|
||||
|
||||
def set_python_dir(p):
|
||||
global SRC_DIR, PYTHON_DIR
|
||||
full = '%s/%s' % (SRC_DIR, p)
|
||||
if not os.path.exists(full):
|
||||
raise MKException("Python bindings directory '%s' does not exist" % full)
|
||||
PYTHON_DIR = full
|
||||
if VERBOSE:
|
||||
print "Python bindinds directory was detected."
|
||||
|
||||
_UNIQ_ID = 0
|
||||
|
||||
def mk_fresh_name(prefix):
|
||||
|
@ -668,6 +678,87 @@ def mk_def_files():
|
|||
for c in _Components:
|
||||
if c.require_def_file():
|
||||
mk_def_file(c)
|
||||
|
||||
def mk_bindings(api_files):
|
||||
mk_z3consts_py(api_files)
|
||||
|
||||
# Extract enumeration types from API files, and add python definitions.
|
||||
def mk_z3consts_py(api_files):
|
||||
if PYTHON_DIR == None:
|
||||
raise MKException("You must invoke set_python_dir(path):")
|
||||
|
||||
blank_pat = re.compile("^ *$")
|
||||
comment_pat = re.compile("^ *//.*$")
|
||||
typedef_pat = re.compile("typedef enum *")
|
||||
typedef2_pat = re.compile("typedef enum { *")
|
||||
openbrace_pat = re.compile("{ *")
|
||||
closebrace_pat = re.compile("}.*;")
|
||||
|
||||
z3consts = open('%s/z3consts.py' % PYTHON_DIR, 'w')
|
||||
z3consts.write('# Automatically generated file\n\n')
|
||||
|
||||
api_dll = _Name2Component['api_dll']
|
||||
|
||||
for api_file in api_files:
|
||||
api_file_c = api_dll.find_file(api_file, api_dll.name)
|
||||
api_file = '%s/%s' % (api_file_c.src_dir, api_file)
|
||||
api = open(api_file, 'r')
|
||||
|
||||
SEARCHING = 0
|
||||
FOUND_ENUM = 1
|
||||
IN_ENUM = 2
|
||||
|
||||
mode = SEARCHING
|
||||
decls = {}
|
||||
idx = 0
|
||||
|
||||
linenum = 1
|
||||
for line in api:
|
||||
m1 = blank_pat.match(line)
|
||||
m2 = comment_pat.match(line)
|
||||
if m1 or m2:
|
||||
# skip blank lines and comments
|
||||
linenum = linenum + 1
|
||||
elif mode == SEARCHING:
|
||||
m = typedef_pat.match(line)
|
||||
if m:
|
||||
mode = FOUND_ENUM
|
||||
m = typedef2_pat.match(line)
|
||||
if m:
|
||||
mode = IN_ENUM
|
||||
decls = {}
|
||||
idx = 0
|
||||
elif mode == FOUND_ENUM:
|
||||
m = openbrace_pat.match(line)
|
||||
if m:
|
||||
mode = IN_ENUM
|
||||
decls = {}
|
||||
idx = 0
|
||||
else:
|
||||
assert False, "Invalid %s, line: %s" % (api_file, linenum)
|
||||
else:
|
||||
assert mode == IN_ENUM
|
||||
words = re.split('[^\-a-zA-Z0-9_]+', line)
|
||||
m = closebrace_pat.match(line)
|
||||
if m:
|
||||
name = words[1]
|
||||
z3consts.write('# enum %s\n' % name)
|
||||
for k, i in decls.iteritems():
|
||||
z3consts.write('%s = %s\n' % (k, i))
|
||||
z3consts.write('\n')
|
||||
mode = SEARCHING
|
||||
else:
|
||||
if words[2] != '':
|
||||
if len(words[2]) > 1 and words[2][1] == 'x':
|
||||
idx = int(words[2], 16)
|
||||
else:
|
||||
idx = int(words[2])
|
||||
decls[words[1]] = idx
|
||||
idx = idx + 1
|
||||
linenum = linenum + 1
|
||||
if VERBOSE:
|
||||
print "Generated '%s'" % ('%s/z3consts.py' % PYTHON_DIR)
|
||||
|
||||
|
||||
def get_component(name):
|
||||
return _Name2Component[name]
|
||||
|
|
|
@ -8,7 +8,6 @@
|
|||
import re
|
||||
import sys
|
||||
import os
|
||||
from sets import Set
|
||||
|
||||
API_FILES = []
|
||||
|
||||
|
@ -97,106 +96,6 @@ def mk_z3consts_donet():
|
|||
z3consts.write('}\n');
|
||||
|
||||
|
||||
# Extract enumeration types from z3_api.h, and add python definitions.
|
||||
def mk_z3consts_py():
|
||||
blank_pat = re.compile("^ *$")
|
||||
comment_pat = re.compile("^ *//.*$")
|
||||
typedef_pat = re.compile("typedef enum *")
|
||||
typedef2_pat = re.compile("typedef enum { *")
|
||||
openbrace_pat = re.compile("{ *")
|
||||
closebrace_pat = re.compile("}.*;")
|
||||
|
||||
z3consts = open('python%sz3consts.py' % (os.sep), 'w')
|
||||
z3consts.write('# Automatically generated file, generator: update_api.py\n\n')
|
||||
|
||||
for api_file in API_FILES:
|
||||
api = open(api_file, 'r')
|
||||
|
||||
SEARCHING = 0
|
||||
FOUND_ENUM = 1
|
||||
IN_ENUM = 2
|
||||
|
||||
mode = SEARCHING
|
||||
decls = {}
|
||||
idx = 0
|
||||
|
||||
linenum = 1
|
||||
for line in api:
|
||||
m1 = blank_pat.match(line)
|
||||
m2 = comment_pat.match(line)
|
||||
if m1 or m2:
|
||||
# skip blank lines and comments
|
||||
linenum = linenum + 1
|
||||
elif mode == SEARCHING:
|
||||
m = typedef_pat.match(line)
|
||||
if m:
|
||||
mode = FOUND_ENUM
|
||||
m = typedef2_pat.match(line)
|
||||
if m:
|
||||
mode = IN_ENUM
|
||||
decls = {}
|
||||
idx = 0
|
||||
elif mode == FOUND_ENUM:
|
||||
m = openbrace_pat.match(line)
|
||||
if m:
|
||||
mode = IN_ENUM
|
||||
decls = {}
|
||||
idx = 0
|
||||
else:
|
||||
assert False, "Invalid %s, line: %s" % (api_file, linenum)
|
||||
else:
|
||||
assert mode == IN_ENUM
|
||||
words = re.split('[^\-a-zA-Z0-9_]+', line)
|
||||
m = closebrace_pat.match(line)
|
||||
if m:
|
||||
name = words[1]
|
||||
z3consts.write('# enum %s\n' % name)
|
||||
for k, i in decls.iteritems():
|
||||
z3consts.write('%s = %s\n' % (k, i))
|
||||
z3consts.write('\n')
|
||||
mode = SEARCHING
|
||||
else:
|
||||
if words[2] != '':
|
||||
if len(words[2]) > 1 and words[2][1] == 'x':
|
||||
idx = int(words[2], 16)
|
||||
else:
|
||||
idx = int(words[2])
|
||||
decls[words[1]] = idx
|
||||
idx = idx + 1
|
||||
linenum = linenum + 1
|
||||
|
||||
# Extract tactic and probe definitions from lib\install_tactics.cpp, and
|
||||
# Add python function definitions for them.
|
||||
def mk_z3tactics_py():
|
||||
tactic_pat = re.compile("^[ \t]*ADD_TACTIC_CMD")
|
||||
probe_pat = re.compile("^[ \t]*ADD_PROBE")
|
||||
|
||||
cppfile = open('lib%sinstall_tactics.cpp' % os.sep, 'r')
|
||||
z3tactics = open('python%sz3tactics.py' % os.sep, 'w')
|
||||
|
||||
z3tactics.write('# Automatically generated file, generator: update_api.py\n')
|
||||
z3tactics.write('import z3core\n')
|
||||
z3tactics.write('import z3\n\n')
|
||||
|
||||
for line in cppfile:
|
||||
m1 = tactic_pat.match(line)
|
||||
m2 = probe_pat.match(line)
|
||||
if m1:
|
||||
words = re.split('[^\-a-zA-Z0-9_]+', line)
|
||||
tactic = words[2]
|
||||
py_tactic = tactic.replace('-', '_')
|
||||
z3tactics.write('def %s_tactic(ctx=None):\n' % py_tactic)
|
||||
z3tactics.write(' ctx = z3._get_ctx(ctx)\n')
|
||||
z3tactics.write(' return z3.Tactic(z3core.Z3_mk_tactic(ctx.ref(), \'%s\'), ctx)\n\n' % tactic)
|
||||
elif m2:
|
||||
words = re.split('[^\-a-zA-Z0-9_]+', line)
|
||||
probe = words[2]
|
||||
py_probe = probe.replace('-', '_')
|
||||
z3tactics.write('def %s_probe(ctx=None):\n' % py_probe)
|
||||
z3tactics.write(' ctx = z3._get_ctx(ctx)\n')
|
||||
z3tactics.write(' return z3.Probe(z3core.Z3_mk_probe(ctx.ref(), \'%s\'), ctx)\n\n' % probe)
|
||||
|
||||
|
||||
#
|
||||
# Generate logging support and bindings
|
||||
#
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue