def makefile(args):
    if args.CC:
        CC = args.CC
    elif args.petsc_dir:
        CC = '$(PCC)'
    else:
        CC = 'mpicc'
    m = [('HPGMG_ARCH = %s' % args.arch), ('HPGMG_CC = %s' % CC), ('HPGMG_CFLAGS = %s' % (args.CFLAGS if args.CFLAGS else ('$(PCC_FLAGS) ' if args.petsc_dir else ''))), ('HPGMG_CPPFLAGS = %s' % (('$(CCPPFLAGS) ' if args.petsc_dir else '') + args.CPPFLAGS)), ('HPGMG_LDFLAGS = %s' % args.LDFLAGS), ('HPGMG_LDLIBS = %s' % args.LDLIBS), ('PETSC_DIR = %s' % args.petsc_dir), ('PETSC_ARCH = %s' % args.petsc_arch), ('PYTHON = %s' % sys.executable), ('SRCDIR = %s' % os.path.abspath(os.path.dirname(__name__)))]
    if args.with_hpm:
        m.append('CONFIG_HPM = y')
        hpm_lib = args.with_hpm
        try:
            hpm_lib = int(hpm_lib)
        except:
            pass
        if (not isinstance(hpm_lib, str)):
            hpm_lib = '/soft/perftools/hpctw/lib/libmpihpm.a /bgsys/drivers/ppcfloor/bgpm/lib/libbgpm.a'
        for p in hpm_lib.split():
            assert os.path.exists(p), ("HPM path '%s' not found" % p)
        m.append(('HPGMG_LDLIBS += ' + hpm_lib))
        m.append('HPGMG_CPPFLAGS += -DUSE_HPM=1')
    if args.fv:
        m.append('CONFIG_FV = y')
    if (args.fe and args.petsc_dir):
        m.append('CONFIG_FE = y')
    m.append(('CONFIG_FV_CPPFLAGS = ' + hpgmg_fv_cflags(args)))
    if args.petsc_dir:
        found = False
        for variables_path in [os.path.join('lib', 'petsc', 'conf', 'variables'), os.path.join('lib', 'petsc-conf', 'variables'), os.path.join('conf', 'variables')]:
            if os.path.exists(os.path.join(args.petsc_dir, variables_path)):
                m.append(('include $(PETSC_DIR)/' + variables_path))
                found = True
        if (not found):
            raise RuntimeError(('Could not find PETSc variables file in PETSC_DIR=%s' % (args.petsc_dir,)))
    m.append('include $(SRCDIR)/base.mk\n')
    return '\n'.join(m)
def makefile(args):
    if args.CC:
        CC = args.CC
    elif args.petsc_dir:
        CC = '$(PCC)'
    else:
        CC = 'mpicc'
    m = [('HPGMG_ARCH = %s' % args.arch), ('HPGMG_CC = %s' % CC), ('HPGMG_CFLAGS = %s' % (args.CFLAGS if args.CFLAGS else ('$(PCC_FLAGS) ' if args.petsc_dir else ))), ('HPGMG_CPPFLAGS = %s' % (('$(CCPPFLAGS) ' if args.petsc_dir else ) + args.CPPFLAGS)), ('HPGMG_LDFLAGS = %s' % args.LDFLAGS), ('HPGMG_LDLIBS = %s' % args.LDLIBS), ('PETSC_DIR = %s' % args.petsc_dir), ('PETSC_ARCH = %s' % args.petsc_arch), ('PYTHON = %s' % sys.executable), ('SRCDIR = %s' % os.path.abspath(os.path.dirname(__name__)))]
    if args.with_hpm:
        m.append('CONFIG_HPM = y')
        hpm_lib = args.with_hpm
        try:
            hpm_lib = int(hpm_lib)
        except:
            pass
        if (not isinstance(hpm_lib, str)):
            hpm_lib = '/soft/perftools/hpctw/lib/libmpihpm.a /bgsys/drivers/ppcfloor/bgpm/lib/libbgpm.a'
        for p in hpm_lib.split():
            assert os.path.exists(p), ("HPM path '%s' not found" % p)
        m.append(('HPGMG_LDLIBS += ' + hpm_lib))
        m.append('HPGMG_CPPFLAGS += -DUSE_HPM=1')
    if args.fv:
        m.append('CONFIG_FV = y')
    if (args.fe and args.petsc_dir):
        m.append('CONFIG_FE = y')
    m.append(('CONFIG_FV_CPPFLAGS = ' + hpgmg_fv_cflags(args)))
    if args.petsc_dir:
        found = False
        for variables_path in [os.path.join('lib', 'petsc', 'conf', 'variables'), os.path.join('lib', 'petsc-conf', 'variables'), os.path.join('conf', 'variables')]:
            if os.path.exists(os.path.join(args.petsc_dir, variables_path)):
                m.append(('include $(PETSC_DIR)/' + variables_path))
                found = True
        if (not found):
            raise RuntimeError(('Could not find PETSc variables file in PETSC_DIR=%s' % (args.petsc_dir,)))
    m.append('include $(SRCDIR)/base.mk\n')
    return '\n'.join(m)