#!/usr/bin/env python
#
# Copyright (C) 2005-2009 Anders Logg.
# Licensed under the GNU LGPL Version 2.1.
#
# Recompile all ffc forms (use when FFC has been updated)
# This script should be run from the top level directory.

import os

# Forms that need special options
special = {"CahnHilliard2D.ufl": "-fsplit",
           "CahnHilliard3D.ufl": "-fsplit"}

# Compile all form files
cwd = os.getcwd()
for root, dirs, files in os.walk(cwd):

    # Check for .ufl files
    formfiles = [f for f in files if len(f) > 4 and f[-4:] == ".ufl"]
    if len(formfiles) == 0:
        continue

    # Compile files
    os.chdir(root)
    print "Compiling forms in %s..." % root
    for f in formfiles:
        if f in special:
            options = special[f]
        else:
            options = ""
        command = "ffc -d -O -l dolfin %s %s >> compile.log" % (options, f)
        print "  " + command
        ret = os.system(command)
        if not ret == 0:
            raise RuntimeError, "Unable to compile form: %s/%s" % (root, f)
