From 86f7c332528c78236e093e5e47070519b8d9f795 Mon Sep 17 00:00:00 2001
From: Dominic Kempf <dominic.r.kempf@gmail.com>
Date: Wed, 2 Sep 2015 15:41:57 +0200
Subject: [PATCH] Add some compiler infrastructure

---
 python/dune/perftool/compile.py | 62 +++++++++++++++++++++++++++++++++
 python/dune/perftool/options.py | 21 +++++++++++
 python/setup.py                 |  7 +++-
 3 files changed, 89 insertions(+), 1 deletion(-)
 create mode 100644 python/dune/perftool/compile.py
 create mode 100644 python/dune/perftool/options.py

diff --git a/python/dune/perftool/compile.py b/python/dune/perftool/compile.py
new file mode 100644
index 00000000..8bee29ee
--- /dev/null
+++ b/python/dune/perftool/compile.py
@@ -0,0 +1,62 @@
+"""
+The methods to run the parts of the form compiler
+
+Should also contain the entrypoint methods.
+"""
+
+def read_ufl(uflfile=None):
+    assert uflfile
+    
+    from ufl.algorithms.formfiles import read_ufl_file, interpret_ufl_namespace
+    from ufl.algorithms import compute_form_data
+
+    uflcode = read_ufl_file(uflfile)
+    namespace = {}
+    uflcode = "from dune.perftool.runtime_ufl import *\n" + uflcode
+    exec uflcode in namespace
+    data = interpret_ufl_namespace(namespace)
+    formdata = compute_form_data(data.forms[0])
+    return formdata
+
+
+def write_driver(formdata=None, filename=None):
+    assert formdata
+    assert filename
+    
+    # The driver module uses a global variable for ease of use
+    import dune.perftool.driver
+    dune.perftool.driver._formdata = formdata
+
+    # The vtkoutput is the generating method that triggers all others.
+    # Alternatively, one could use the `solve` method.
+    from dune.perftool.driver import vtkoutput
+    vtkoutput()
+
+    from dune.perftool.generation import cache_preambles, cache_includes
+    includes = cache_includes()
+
+    # Get all preambles for the driver and sort them.
+    driver_content = [i[1] for i in sorted(cache_preambles(), key=lambda x : x[0])]
+
+    # And flatten out those, that conained nested lists
+    def flatjoin(l):
+        if isinstance(l, str):
+            return "\n" + l
+        return "".join(flatjoin(i) for i in l)
+
+    from cgen import LiteralBlock, FunctionDeclaration    
+    driver = LiteralBlock(flatjoin(driver_content))
+
+    # Write the file.
+    f = open(filename, 'w')
+    f.write("\n".join(cache_includes()))
+    f.write("\nvoid driver(int argc, char** argv)\n")
+    f.write("\n".join(driver.generate()))
+
+
+def write_localoperator():
+    pass
+
+
+def compile_form():
+    pass    
diff --git a/python/dune/perftool/options.py b/python/dune/perftool/options.py
new file mode 100644
index 00000000..125d112f
--- /dev/null
+++ b/python/dune/perftool/options.py
@@ -0,0 +1,21 @@
+""" A module that manages the command line options to the form compiler executable """
+
+from pytools import memoize
+
+@memoize
+def get_form_compiler_arguments():
+    from argparse import ArgumentParser
+    parser = ArgumentParser(description="Compile UFL files to PDELab C++ code", epilog="Please report bugs to dominic.kempf@iwr.uni-heidelberg.de")
+    parser.add_argument("uflfile", type=str, nargs=1, help="the UFL file to compile")
+#     parser.add_argument("--numerical-jacobian", action="store_true", help="use numerical jacobians (only makes sense, if uflpdelab for some reason fails to generate analytic jacobians)")
+#     parser.add_argument('--version', action='version', version='%(prog)s 0.0')
+#     parser.add_argument("--operator-file", type=str, help="The filename for the generated local operator header")
+#     parser.add_argument("--driver-file", type=str, help="The filename for the generated driver header")
+#     parser.add_argument("--param-class-file", type=str, help="The filename for the generated parameter class header")
+#     parser.add_argument("--export-trafo-graphs", action="store_true", help="export expression graphs after the application of each single transformation")
+#     parser.add_argument("--parameter-tree", action="store_true", help="have the generated local operate take a Dune::ParameterTree as constructor argument")
+#     parser.add_argument("--inifile", type=str, help="The inifile that this program's driver should be hardcoded to. Omit to use a commandline argument ini file")
+    return vars(parser.parse_args)
+
+def get_option(key):
+    return get_form_compiler_arguments()(key)
\ No newline at end of file
diff --git a/python/setup.py b/python/setup.py
index cb01adab..4a6b0729 100644
--- a/python/setup.py
+++ b/python/setup.py
@@ -32,4 +32,9 @@ setup(name='dune.perftool',
       packages=['dune.perftool'],
       install_requires=[],
       tests_require=['pytest'],
-      cmdclass={'test': PyTest})
+      cmdclass={'test': PyTest},
+      entry_points = {
+        "console_scripts": [
+            "ufl2pdelab = dune.perftool.compiler:compile_form",
+        ]
+    })
-- 
GitLab