2020-03-31 06:32:19 +00:00
|
|
|
#!/usr/bin/env python3
|
2014-07-14 19:32:44 +00:00
|
|
|
#
|
2020-03-09 06:18:30 +00:00
|
|
|
# Copyright 2020, Data61, CSIRO (ABN 41 687 119 230)
|
2014-07-14 19:32:44 +00:00
|
|
|
#
|
2020-03-09 06:18:30 +00:00
|
|
|
# SPDX-License-Identifier: BSD-2-Clause
|
2014-07-14 19:32:44 +00:00
|
|
|
#
|
|
|
|
|
|
|
|
#
|
|
|
|
# Generate the file "umm_types.txt", required for generating theory files used
|
|
|
|
# in Isabelle sessions "CSpec" and beyond.
|
|
|
|
#
|
|
|
|
|
|
|
|
import subprocess
|
|
|
|
import argparse
|
|
|
|
import os
|
|
|
|
import sys
|
|
|
|
import tempfile
|
|
|
|
import shutil
|
|
|
|
|
|
|
|
# Create a temporary directory
|
2020-03-18 09:25:55 +00:00
|
|
|
|
|
|
|
|
2014-07-14 19:32:44 +00:00
|
|
|
class TempDir(object):
|
|
|
|
def __enter__(self, cleanup=True):
|
|
|
|
self.filename = tempfile.mkdtemp()
|
|
|
|
self.cleanup = cleanup
|
|
|
|
return self.filename
|
|
|
|
|
|
|
|
def __exit__(self, type, value, traceback):
|
|
|
|
if self.cleanup:
|
|
|
|
shutil.rmtree(self.filename)
|
|
|
|
return False
|
|
|
|
|
2020-03-18 09:25:55 +00:00
|
|
|
|
2014-07-14 19:32:44 +00:00
|
|
|
parser = argparse.ArgumentParser(
|
2020-03-18 09:25:55 +00:00
|
|
|
description="Generate a 'umm_types.txt' file from the C parser, required by the bitfield generator.")
|
2014-07-14 19:32:44 +00:00
|
|
|
parser.add_argument('input', metavar='INPUT', type=str,
|
2020-03-18 09:25:55 +00:00
|
|
|
help="C file to parse")
|
2014-07-14 19:32:44 +00:00
|
|
|
parser.add_argument('output', metavar='OUTPUT', type=str,
|
2020-03-18 09:25:55 +00:00
|
|
|
help="output filename")
|
2014-07-14 19:32:44 +00:00
|
|
|
parser.add_argument('--root', metavar='ROOT', type=str,
|
2020-03-18 09:25:55 +00:00
|
|
|
help="add Isabelle ROOT or ROOTS file path", action='append')
|
2014-07-14 19:32:44 +00:00
|
|
|
args = parser.parse_args()
|
|
|
|
|
|
|
|
if "ISABELLE_PROCESS" not in os.environ or "ISABELLE_TOOL" not in os.environ:
|
2020-03-31 06:32:19 +00:00
|
|
|
print("Run this from within 'isabelle env'.")
|
2014-07-14 19:32:44 +00:00
|
|
|
sys.exit(1)
|
|
|
|
|
|
|
|
THY_DATA = """
|
|
|
|
theory UmmTypesFile
|
2018-06-24 13:50:58 +00:00
|
|
|
imports CParser.CTranslation
|
2014-07-14 19:32:44 +00:00
|
|
|
begin
|
|
|
|
declare [[allow_underscore_idents = true]]
|
2018-06-24 13:50:58 +00:00
|
|
|
external_file "%(input)s"
|
2014-07-14 19:32:44 +00:00
|
|
|
setup {* IsarInstall.gen_umm_types_file "%(input)s" "%(output)s" *}
|
|
|
|
end
|
|
|
|
"""
|
|
|
|
|
|
|
|
ROOT_DATA = """
|
|
|
|
session UmmTypes = CParser +
|
|
|
|
theories UmmTypesFile
|
|
|
|
"""
|
|
|
|
|
|
|
|
# Create a new theory file and ROOT file in a temporary directory.
|
|
|
|
with TempDir() as tmp_dir:
|
|
|
|
filenames = {
|
2020-03-18 09:25:55 +00:00
|
|
|
"input": os.path.abspath(args.input),
|
|
|
|
"output": os.path.abspath(args.output),
|
|
|
|
}
|
2014-07-14 19:32:44 +00:00
|
|
|
with open(os.path.join(tmp_dir, "UmmTypesFile.thy"), "w") as f:
|
|
|
|
f.write(THY_DATA % filenames)
|
|
|
|
with open(os.path.join(tmp_dir, "ROOT"), "w") as f:
|
|
|
|
f.write(ROOT_DATA % filenames)
|
|
|
|
|
|
|
|
# Run Isabelle over it.
|
|
|
|
def interleave(a, l):
|
|
|
|
result = []
|
|
|
|
for x in l:
|
|
|
|
result.append(a)
|
|
|
|
result.append(x)
|
|
|
|
return result
|
2020-03-31 06:32:19 +00:00
|
|
|
print("\nGenerating umm_types data file...\n")
|
2014-07-14 19:32:44 +00:00
|
|
|
subprocess.check_call([
|
2020-03-18 09:25:55 +00:00
|
|
|
os.environ["ISABELLE_TOOL"], "build", "-c"]
|
|
|
|
+ interleave("-d", [os.path.abspath(x) for x in args.root])
|
|
|
|
+ ["-d", ".", "-v", "-b", "UmmTypes"],
|
|
|
|
cwd=tmp_dir)
|