# Path: clastgen/tools.py
# def clang_version():
#     """Get a string that describes the version of the clang compiler 
#     """
#     llvm_home = os.environ['LLVM_HOME']
#     binary = os.path.join(llvm_home, 'bin', 'clang')
#     res = subprocess.check_output([binary, '--version']).splitlines()[0]
#     if sys.version_info.major >= 3:
#         return res.decode('utf-8')
#     return res
# 
# def llvm_config(arg):
#     """Run the llvm_config tool 
#     """
#     llvm_home = os.environ['LLVM_HOME']
#     llvm_config = os.path.join(llvm_home, 'bin', 'llvm-config')
#     res = subprocess.check_output([llvm_config, arg]).split()
#     if sys.version_info.major >= 3:
#         return [p.decode('utf-8') for p in res]
#     return res


import glud
import ccsyspath
import os
import sys
import subprocess
import io
import json
    import jinja2
    import argparse
from glud import *
from clastgen.tools import clang_version, llvm_config
from clastgen.utils import *
from clastgen.clangext import *
from clastgen.context import *
from clastgen.filters import *
from clastgen.intermediate import *
from clastgen.templates import *
from clastgen.pagination import *
    from jinja2 import Environment, StrictUndefined
        if not os.path.exists(outfolder):
            os.makedirs(outfolder)
        outfile = os.path.join(outfolder, fname)
        return open(outfile, 'w')

    with output_dir('intermediate.json') as fh:
        fh.write(json.dumps(intermediate, indent=4))

    with output_dir('00_autogen_enums.cpp') as fh:
        fh.write(render_result(template='enum_module.j2', model=intermediate)) 

    with output_dir('autogen_dyntypednode.cpp') as fh:
        fh.write(render_result(template='dyntyped_node_template.j2', model=intermediate)) 

    # in cases where optimisation is required, clast is so slow (and memory hungry)
    # to build, that you really want to divide the build process into smaller pieces
    pages = list(pagination(intermediate['classes'], chunksize=50))
    with output_dir('autogen_classes.cpp') as fh:
        fh.write(render_result(template='allclass_template.j2', model=intermediate, pagecnt=len(pages))) 

    for page in pages:
        with output_dir('%02d_autogen_classes.cpp' % (page.idx+1)) as fh:
            fh.write(render_result(template='class_module.j2', model=intermediate, page=page)) 

if __name__ == "__main__":
    if 'LLVM_HOME' not in os.environ:
        print('LLVM_HOME is undefined, giving up on code generation')
        sys.exit(1)



# Path: clastgen/tools.py
# def clang_version():
#     """Get a string that describes the version of the clang compiler 
#     """
#     llvm_home = os.environ['LLVM_HOME']
#     binary = os.path.join(llvm_home, 'bin', 'clang')
#     res = subprocess.check_output([binary, '--version']).splitlines()[0]
#     if sys.version_info.major >= 3:
#         return res.decode('utf-8')
#     return res
# 
# def llvm_config(arg):
#     """Run the llvm_config tool 
#     """
#     llvm_home = os.environ['LLVM_HOME']
#     llvm_config = os.path.join(llvm_home, 'bin', 'llvm-config')
#     res = subprocess.check_output([llvm_config, arg]).split()
#     if sys.version_info.major >= 3:
#         return [p.decode('utf-8') for p in res]
#     return res


# Path: codegen.py
import glud
import ccsyspath
import os
import sys
import subprocess
import io
import json
    import jinja2
    import argparse
from glud import *
from clastgen.tools import clang_version, llvm_config
from clastgen.utils import *
from clastgen.clangext import *
from clastgen.context import *
from clastgen.filters import *
from clastgen.intermediate import *
from clastgen.templates import *
from clastgen.pagination import *
    from jinja2 import Environment, StrictUndefined

        if not os.path.exists(outfolder):
            os.makedirs(outfolder)
        outfile = os.path.join(outfolder, fname)
        return open(outfile, 'w')

    with output_dir('intermediate.json') as fh:
        fh.write(json.dumps(intermediate, indent=4))

    with output_dir('00_autogen_enums.cpp') as fh:
        fh.write(render_result(template='enum_module.j2', model=intermediate)) 

    with output_dir('autogen_dyntypednode.cpp') as fh:
        fh.write(render_result(template='dyntyped_node_template.j2', model=intermediate)) 

    # in cases where optimisation is required, clast is so slow (and memory hungry)
    # to build, that you really want to divide the build process into smaller pieces
    pages = list(pagination(intermediate['classes'], chunksize=50))
    with output_dir('autogen_classes.cpp') as fh:
        fh.write(render_result(template='allclass_template.j2', model=intermediate, pagecnt=len(pages))) 

    for page in pages:
        with output_dir('%02d_autogen_classes.cpp' % (page.idx+1)) as fh:
            fh.write(render_result(template='class_module.j2', model=intermediate, page=page)) 

if __name__ == "__main__":
    if 'LLVM_HOME' not in os.environ:
        print('LLVM_HOME is undefined, giving up on code generation')
        sys.exit(1)



    default_output_dir = llvm_config('--version')[0].replace('.', '')