示例#1
0
文件: gmakegen.py 项目: spcl/fflib
 def __init__(self, petsc_dir=None, petsc_arch=None, verbose=False):
     if petsc_dir is None:
         petsc_dir = os.environ.get('PETSC_DIR')
         if petsc_dir is None:
             try:
                 petsc_dir = parse_makefile(
                     os.path.join('lib', 'petsc', 'conf',
                                  'petscvariables')).get('PETSC_DIR')
             finally:
                 if petsc_dir is None:
                     raise RuntimeError(
                         'Could not determine PETSC_DIR, please set in environment'
                     )
     if petsc_arch is None:
         petsc_arch = os.environ.get('PETSC_ARCH')
         if petsc_arch is None:
             try:
                 petsc_arch = parse_makefile(
                     os.path.join(petsc_dir, 'lib', 'petsc', 'conf',
                                  'petscvariables')).get('PETSC_ARCH')
             finally:
                 if petsc_arch is None:
                     raise RuntimeError(
                         'Could not determine PETSC_ARCH, please set in environment'
                     )
     self.petsc_dir = petsc_dir
     self.petsc_arch = petsc_arch
     self.read_conf()
     logging.basicConfig(filename=self.arch_path('lib', 'petsc', 'conf',
                                                 'gmake.log'),
                         level=logging.DEBUG)
     self.log = logging.getLogger('gmakegen')
     self.mistakes = Mistakes(debuglogger(self.log), verbose=verbose)
     self.gendeps = []
示例#2
0
 def __init__(self,
              slepc_dir=None,
              petsc_dir=None,
              petsc_arch=None,
              installed_petsc=False,
              verbose=False):
     if slepc_dir is None:
         slepc_dir = os.environ.get('SLEPC_DIR')
         if slepc_dir is None:
             raise RuntimeError(
                 'Could not determine SLEPC_DIR, please set in environment')
     if petsc_dir is None:
         petsc_dir = os.environ.get('PETSC_DIR')
         if petsc_dir is None:
             raise RuntimeError(
                 'Could not determine PETSC_DIR, please set in environment')
     if petsc_arch is None:
         petsc_arch = os.environ.get('PETSC_ARCH')
         if petsc_arch is None:
             raise RuntimeError(
                 'Could not determine PETSC_ARCH, please set in environment'
             )
     self.slepc_dir = slepc_dir
     self.petsc_dir = petsc_dir
     self.petsc_arch = petsc_arch
     self.installed_petsc = installed_petsc
     self.read_conf()
     logging.basicConfig(filename=self.arch_path('conf', 'gmake.log'),
                         level=logging.DEBUG)
     self.log = logging.getLogger('gmakegen')
     self.mistakes = Mistakes(debuglogger(self.log), verbose=verbose)
     self.gendeps = []
示例#3
0
 def __init__(self,
              petsc_dir=None,
              petsc_arch=None,
              pkg_dir=None,
              pkg_name=None,
              pkg_arch=None,
              pkg_pkgs=None,
              verbose=False):
     if petsc_dir is None:
         petsc_dir = os.environ.get('PETSC_DIR')
         if petsc_dir is None:
             try:
                 petsc_dir = parse_makefile(
                     os.path.join('lib', 'petsc', 'conf',
                                  'petscvariables')).get('PETSC_DIR')
             finally:
                 if petsc_dir is None:
                     raise RuntimeError(
                         'Could not determine PETSC_DIR, please set in environment'
                     )
     if petsc_arch is None:
         petsc_arch = os.environ.get('PETSC_ARCH')
         if petsc_arch is None:
             try:
                 petsc_arch = parse_makefile(
                     os.path.join(petsc_dir, 'lib', 'petsc', 'conf',
                                  'petscvariables')).get('PETSC_ARCH')
             finally:
                 if petsc_arch is None:
                     raise RuntimeError(
                         'Could not determine PETSC_ARCH, please set in environment'
                     )
     self.petsc_dir = os.path.normpath(petsc_dir)
     self.petsc_arch = petsc_arch.rstrip(os.sep)
     self.pkg_dir = pkg_dir
     self.pkg_name = pkg_name
     self.pkg_arch = pkg_arch
     if self.pkg_dir is None:
         self.pkg_dir = petsc_dir
         self.pkg_name = 'petsc'
         self.pkg_arch = self.petsc_arch
     if self.pkg_name is None:
         self.pkg_name = os.path.basename(os.path.normpath(self.pkg_dir))
     if self.pkg_arch is None:
         self.pkg_arch = self.petsc_arch
     self.pkg_pkgs = PetscPKGS
     if pkg_pkgs is not None:
         self.pkg_pkgs += list(
             set(pkg_pkgs.split(',')) - set(self.pkg_pkgs))
     self.read_conf()
     try:
         logging.basicConfig(filename=self.pkg_arch_path(
             'lib', self.pkg_name, 'conf', 'gmake.log'),
                             level=logging.DEBUG)
     except IOError:
         # Disable logging if path is not writeable (e.g., prefix install)
         logging.basicConfig(filename='/dev/null', level=logging.DEBUG)
     self.log = logging.getLogger('gmakegen')
     self.mistakes = Mistakes(debuglogger(self.log), verbose=verbose)
     self.gendeps = []
示例#4
0
文件: gmakegen.py 项目: petsc/petsc
 def __init__(self, petsc_dir=None, petsc_arch=None, verbose=False):
     if petsc_dir is None:
         petsc_dir = os.environ.get("PETSC_DIR")
         if petsc_dir is None:
             try:
                 petsc_dir = parse_makefile(os.path.join("lib", "petsc", "conf", "petscvariables")).get("PETSC_DIR")
             finally:
                 if petsc_dir is None:
                     raise RuntimeError("Could not determine PETSC_DIR, please set in environment")
     if petsc_arch is None:
         petsc_arch = os.environ.get("PETSC_ARCH")
         if petsc_arch is None:
             try:
                 petsc_arch = parse_makefile(os.path.join(petsc_dir, "lib", "petsc", "conf", "petscvariables")).get(
                     "PETSC_ARCH"
                 )
             finally:
                 if petsc_arch is None:
                     raise RuntimeError("Could not determine PETSC_ARCH, please set in environment")
     self.petsc_dir = petsc_dir
     self.petsc_arch = petsc_arch
     self.read_conf()
     logging.basicConfig(filename=self.arch_path("lib", "petsc", "conf", "gmake.log"), level=logging.DEBUG)
     self.log = logging.getLogger("gmakegen")
     self.mistakes = Mistakes(debuglogger(self.log), verbose=verbose)
     self.gendeps = []
示例#5
0
 def __init__(self, petsc_dir=None, petsc_arch=None, pkg_dir=None, pkg_name=None, pkg_arch=None, pkg_pkgs=None, verbose=False):
     if petsc_dir is None:
         petsc_dir = os.environ.get('PETSC_DIR')
         if petsc_dir is None:
             try:
                 petsc_dir = parse_makefile(os.path.join('lib','petsc','conf', 'petscvariables')).get('PETSC_DIR')
             finally:
                 if petsc_dir is None:
                     raise RuntimeError('Could not determine PETSC_DIR, please set in environment')
     if petsc_arch is None:
         petsc_arch = os.environ.get('PETSC_ARCH')
         if petsc_arch is None:
             try:
                 petsc_arch = parse_makefile(os.path.join(petsc_dir, 'lib','petsc','conf', 'petscvariables')).get('PETSC_ARCH')
             finally:
                 if petsc_arch is None:
                     raise RuntimeError('Could not determine PETSC_ARCH, please set in environment')
     self.petsc_dir = os.path.normpath(petsc_dir)
     self.petsc_arch = petsc_arch.rstrip(os.sep)
     self.pkg_dir = pkg_dir
     self.pkg_name = pkg_name
     self.pkg_arch = pkg_arch
     if self.pkg_dir is None:
       self.pkg_dir = petsc_dir
       self.pkg_name = 'petsc'
       self.pkg_arch = self.petsc_arch
     if self.pkg_name is None:
       self.pkg_name = os.path.basename(os.path.normpath(self.pkg_dir))
     if self.pkg_arch is None:
       self.pkg_arch = self.petsc_arch
     self.pkg_pkgs = PetscPKGS
     if pkg_pkgs is not None:
       self.pkg_pkgs += list(set(pkg_pkgs.split(','))-set(self.pkg_pkgs))
     self.read_conf()
     try:
         logging.basicConfig(filename=self.pkg_arch_path('lib',self.pkg_name,'conf', 'gmake.log'), level=logging.DEBUG)
     except IOError:
         # Disable logging if path is not writeable (e.g., prefix install)
         logging.basicConfig(filename='/dev/null', level=logging.DEBUG)
     self.log = logging.getLogger('gmakegen')
     self.mistakes = Mistakes(debuglogger(self.log), verbose=verbose)
     self.gendeps = []
示例#6
0
 def __init__(self, slepc_dir=None, petsc_dir=None, petsc_arch=None, installed_petsc=False, verbose=False):
     if slepc_dir is None:
         slepc_dir = os.environ.get('SLEPC_DIR')
         if slepc_dir is None:
             raise RuntimeError('Could not determine SLEPC_DIR, please set in environment')
     if petsc_dir is None:
         petsc_dir = os.environ.get('PETSC_DIR')
         if petsc_dir is None:
             raise RuntimeError('Could not determine PETSC_DIR, please set in environment')
     if petsc_arch is None:
         petsc_arch = os.environ.get('PETSC_ARCH')
         if petsc_arch is None:
             raise RuntimeError('Could not determine PETSC_ARCH, please set in environment')
     self.slepc_dir = slepc_dir
     self.petsc_dir = petsc_dir
     self.petsc_arch = petsc_arch
     self.installed_petsc = installed_petsc
     self.read_conf()
     logging.basicConfig(filename=self.arch_path('conf', 'gmake.log'), level=logging.DEBUG)
     self.log = logging.getLogger('gmakegen')
     self.mistakes = Mistakes(debuglogger(self.log), verbose=verbose)
     self.gendeps = []
 def __init__(self, petsc_dir=None, petsc_arch=None, verbose=False):
     if petsc_dir is None:
         petsc_dir = os.environ.get('PETSC_DIR')
         if petsc_dir is None:
             try:
                 petsc_dir = parse_makefile(os.path.join('conf', 'petscvariables')).get('PETSC_DIR')
             finally:
                 if petsc_dir is None:
                     raise RuntimeError('Could not determine PETSC_DIR, please set in environment')
     if petsc_arch is None:
         petsc_arch = os.environ.get('PETSC_ARCH')
         if petsc_arch is None:
             try:
                 petsc_arch = parse_makefile(os.path.join(petsc_dir, 'conf', 'petscvariables')).get('PETSC_ARCH')
             finally:
                 if petsc_arch is None:
                     raise RuntimeError('Could not determine PETSC_ARCH, please set in environment')
     self.petsc_dir = petsc_dir
     self.petsc_arch = petsc_arch
     self.read_conf()
     logging.basicConfig(filename=self.arch_path('conf', 'gmake.log'), level=logging.DEBUG)
     self.log = logging.getLogger('gmakegen')
     self.mistakes = Mistakes(debuglogger(self.log), verbose=verbose)
     self.gendeps = []
示例#8
0
文件: gmakegen.py 项目: petsc/petsc
class Petsc(object):
    def __init__(self, petsc_dir=None, petsc_arch=None, verbose=False):
        if petsc_dir is None:
            petsc_dir = os.environ.get("PETSC_DIR")
            if petsc_dir is None:
                try:
                    petsc_dir = parse_makefile(os.path.join("lib", "petsc", "conf", "petscvariables")).get("PETSC_DIR")
                finally:
                    if petsc_dir is None:
                        raise RuntimeError("Could not determine PETSC_DIR, please set in environment")
        if petsc_arch is None:
            petsc_arch = os.environ.get("PETSC_ARCH")
            if petsc_arch is None:
                try:
                    petsc_arch = parse_makefile(os.path.join(petsc_dir, "lib", "petsc", "conf", "petscvariables")).get(
                        "PETSC_ARCH"
                    )
                finally:
                    if petsc_arch is None:
                        raise RuntimeError("Could not determine PETSC_ARCH, please set in environment")
        self.petsc_dir = petsc_dir
        self.petsc_arch = petsc_arch
        self.read_conf()
        logging.basicConfig(filename=self.arch_path("lib", "petsc", "conf", "gmake.log"), level=logging.DEBUG)
        self.log = logging.getLogger("gmakegen")
        self.mistakes = Mistakes(debuglogger(self.log), verbose=verbose)
        self.gendeps = []

    def arch_path(self, *args):
        return os.path.join(self.petsc_dir, self.petsc_arch, *args)

    def read_conf(self):
        self.conf = dict()
        for line in open(self.arch_path("include", "petscconf.h")):
            if line.startswith("#define "):
                define = line[len("#define ") :]
                space = define.find(" ")
                key = define[:space]
                val = define[space + 1 :]
                self.conf[key] = val
        self.conf.update(parse_makefile(self.arch_path("lib", "petsc", "conf", "petscvariables")))
        self.have_fortran = int(self.conf.get("PETSC_HAVE_FORTRAN", "0"))

    def inconf(self, key, val):
        if key in ["package", "function", "define"]:
            return self.conf.get(val)
        elif key == "precision":
            return val == self.conf["PETSC_PRECISION"]
        elif key == "scalar":
            return val == self.conf["PETSC_SCALAR"]
        elif key == "language":
            return val == self.conf["PETSC_LANGUAGE"]
        raise RuntimeError("Unknown conf check: %s %s" % (key, val))

    def relpath(self, root, src):
        return os.path.relpath(os.path.join(root, src), self.petsc_dir)

    def get_sources(self, makevars):
        """Return dict {lang: list_of_source_files}"""
        source = dict()
        for lang, sourcelang in LANGS.items():
            source[lang] = [f for f in makevars.get("SOURCE" + sourcelang, "").split() if f.endswith(lang)]
        return source

    def gen_pkg(self, pkg):
        pkgsrcs = dict()
        for lang in LANGS:
            pkgsrcs[lang] = []
        for root, dirs, files in os.walk(os.path.join(self.petsc_dir, "src", pkg)):
            makefile = os.path.join(root, "makefile")
            if not os.path.exists(makefile):
                dirs[:] = []
                continue
            mklines = open(makefile)
            conditions = set(tuple(stripsplit(line)) for line in mklines if line.startswith("#requires"))
            mklines.close()
            if not all(self.inconf(key, val) for key, val in conditions):
                dirs[:] = []
                continue
            makevars = parse_makefile(makefile)
            mdirs = makevars.get("DIRS", "").split()  # Directories specified in the makefile
            self.mistakes.compareDirLists(root, mdirs, dirs)  # diagnostic output to find unused directories
            candidates = set(mdirs).union(AUTODIRS).difference(SKIPDIRS)
            dirs[:] = list(candidates.intersection(dirs))
            allsource = []

            def mkrel(src):
                return self.relpath(root, src)

            source = self.get_sources(makevars)
            for lang, s in source.items():
                pkgsrcs[lang] += map(mkrel, s)
                allsource += s
            self.mistakes.compareSourceLists(root, allsource, files)  # Diagnostic output about unused source files
            self.gendeps.append(self.relpath(root, "makefile"))
        return pkgsrcs

    def gen_gnumake(self, fd):
        def write(stem, srcs):
            for lang in LANGS:
                fd.write("%(stem)s.%(lang)s := %(srcs)s\n" % dict(stem=stem, lang=lang, srcs=" ".join(srcs[lang])))

        for pkg in PKGS:
            srcs = self.gen_pkg(pkg)
            write("srcs-" + pkg, srcs)
        return self.gendeps

    def gen_ninja(self, fd):
        libobjs = []
        for pkg in PKGS:
            srcs = self.gen_pkg(pkg)
            for lang in LANGS:
                for src in srcs[lang]:
                    obj = "$objdir/%s.o" % src
                    fd.write(
                        "build %(obj)s : %(lang)s_COMPILE %(src)s\n"
                        % dict(obj=obj, lang=lang.upper(), src=os.path.join(self.petsc_dir, src))
                    )
                    libobjs.append(obj)
        fd.write("\n")
        fd.write("build $libdir/libpetsc.so : %s_LINK_SHARED %s\n\n" % ("CF"[self.have_fortran], " ".join(libobjs)))
        fd.write("build petsc : phony || $libdir/libpetsc.so\n\n")

    def summary(self):
        self.mistakes.summary()
示例#9
0
class Petsc(object):
    def __init__(self, petsc_dir=None, petsc_arch=None, verbose=False):
        if petsc_dir is None:
            petsc_dir = os.environ.get('PETSC_DIR')
            if petsc_dir is None:
                try:
                    petsc_dir = parse_makefile(
                        os.path.join('lib', 'petsc', 'conf',
                                     'petscvariables')).get('PETSC_DIR')
                finally:
                    if petsc_dir is None:
                        raise RuntimeError(
                            'Could not determine PETSC_DIR, please set in environment'
                        )
        if petsc_arch is None:
            petsc_arch = os.environ.get('PETSC_ARCH')
            if petsc_arch is None:
                try:
                    petsc_arch = parse_makefile(
                        os.path.join(petsc_dir, 'lib', 'petsc', 'conf',
                                     'petscvariables')).get('PETSC_ARCH')
                finally:
                    if petsc_arch is None:
                        raise RuntimeError(
                            'Could not determine PETSC_ARCH, please set in environment'
                        )
        self.petsc_dir = petsc_dir
        self.petsc_arch = petsc_arch
        self.read_conf()
        try:
            logging.basicConfig(filename=self.arch_path(
                'lib', 'petsc', 'conf', 'gmake.log'),
                                level=logging.DEBUG)
        except IOError:
            # Disable logging if path is not writeable (e.g., prefix install)
            logging.basicConfig(filename='/dev/null', level=logging.DEBUG)
        self.log = logging.getLogger('gmakegen')
        self.mistakes = Mistakes(debuglogger(self.log), verbose=verbose)
        self.gendeps = []

    def arch_path(self, *args):
        return os.path.join(self.petsc_dir, self.petsc_arch, *args)

    def read_conf(self):
        self.conf = dict()
        for line in open(self.arch_path('include', 'petscconf.h')):
            if line.startswith('#define '):
                define = line[len('#define '):]
                space = define.find(' ')
                key = define[:space]
                val = define[space + 1:]
                self.conf[key] = val
        self.conf.update(
            parse_makefile(
                self.arch_path('lib', 'petsc', 'conf', 'petscvariables')))
        self.have_fortran = int(self.conf.get('PETSC_HAVE_FORTRAN', '0'))

    def inconf(self, key, val):
        if key in ['package', 'function', 'define']:
            return self.conf.get(val)
        elif key == 'precision':
            return val == self.conf['PETSC_PRECISION']
        elif key == 'scalar':
            return val == self.conf['PETSC_SCALAR']
        elif key == 'language':
            return val == self.conf['PETSC_LANGUAGE']
        raise RuntimeError('Unknown conf check: %s %s' % (key, val))

    def relpath(self, root, src):
        return os.path.relpath(os.path.join(root, src), self.petsc_dir)

    def get_sources(self, makevars):
        """Return dict {lang: list_of_source_files}"""
        source = dict()
        for lang, sourcelang in LANGS.items():
            source[lang] = [
                f for f in makevars.get('SOURCE' + sourcelang, '').split()
                if f.endswith(lang)
            ]
        return source

    def gen_pkg(self, pkg):
        pkgsrcs = dict()
        for lang in LANGS:
            pkgsrcs[lang] = []
        for root, dirs, files in os.walk(
                os.path.join(self.petsc_dir, 'src', pkg)):
            dirs.sort()
            files.sort()
            makefile = os.path.join(root, 'makefile')
            if not os.path.exists(makefile):
                dirs[:] = []
                continue
            mklines = open(makefile)
            conditions = set(
                tuple(stripsplit(line)) for line in mklines
                if line.startswith('#requires'))
            mklines.close()
            if not all(self.inconf(key, val) for key, val in conditions):
                dirs[:] = []
                continue
            makevars = parse_makefile(makefile)
            mdirs = makevars.get(
                'DIRS', '').split()  # Directories specified in the makefile
            self.mistakes.compareDirLists(
                root, mdirs,
                dirs)  # diagnostic output to find unused directories
            candidates = set(mdirs).union(AUTODIRS).difference(SKIPDIRS)
            dirs[:] = list(candidates.intersection(dirs))
            allsource = []

            def mkrel(src):
                return self.relpath(root, src)

            source = self.get_sources(makevars)
            for lang, s in source.items():
                pkgsrcs[lang] += map(mkrel, s)
                allsource += s
            self.mistakes.compareSourceLists(
                root, allsource,
                files)  # Diagnostic output about unused source files
            self.gendeps.append(self.relpath(root, 'makefile'))
        return pkgsrcs

    def gen_gnumake(self, fd):
        def write(stem, srcs):
            for lang in LANGS:
                fd.write('%(stem)s.%(lang)s := %(srcs)s\n' %
                         dict(stem=stem, lang=lang, srcs=' '.join(srcs[lang])))

        for pkg in PKGS:
            srcs = self.gen_pkg(pkg)
            write('srcs-' + pkg, srcs)
        return self.gendeps

    def gen_ninja(self, fd):
        libobjs = []
        for pkg in PKGS:
            srcs = self.gen_pkg(pkg)
            for lang in LANGS:
                for src in srcs[lang]:
                    obj = '$objdir/%s.o' % src
                    fd.write('build %(obj)s : %(lang)s_COMPILE %(src)s\n' %
                             dict(obj=obj,
                                  lang=lang.upper(),
                                  src=os.path.join(self.petsc_dir, src)))
                    libobjs.append(obj)
        fd.write('\n')
        fd.write('build $libdir/libpetsc.so : %s_LINK_SHARED %s\n\n' %
                 ('CF'[self.have_fortran], ' '.join(libobjs)))
        fd.write('build petsc : phony || $libdir/libpetsc.so\n\n')

    def summary(self):
        self.mistakes.summary()
示例#10
0
class Slepc(object):
    def __init__(self, slepc_dir=None, petsc_dir=None, petsc_arch=None, installed_petsc=False, verbose=False):
        if slepc_dir is None:
            slepc_dir = os.environ.get('SLEPC_DIR')
            if slepc_dir is None:
                raise RuntimeError('Could not determine SLEPC_DIR, please set in environment')
        if petsc_dir is None:
            petsc_dir = os.environ.get('PETSC_DIR')
            if petsc_dir is None:
                raise RuntimeError('Could not determine PETSC_DIR, please set in environment')
        if petsc_arch is None:
            petsc_arch = os.environ.get('PETSC_ARCH')
            if petsc_arch is None:
                raise RuntimeError('Could not determine PETSC_ARCH, please set in environment')
        self.slepc_dir = slepc_dir
        self.petsc_dir = petsc_dir
        self.petsc_arch = petsc_arch
        self.installed_petsc = installed_petsc
        self.read_conf()
        logging.basicConfig(filename=self.arch_path('conf', 'gmake.log'), level=logging.DEBUG)
        self.log = logging.getLogger('gmakegen')
        self.mistakes = Mistakes(debuglogger(self.log), verbose=verbose)
        self.gendeps = []

    def petsc_path(self, *args):
        if self.installed_petsc:
            return os.path.join(self.petsc_dir, *args)
        else:
            return os.path.join(self.petsc_dir, self.petsc_arch, *args)

    def arch_path(self, *args):
        return os.path.join(self.slepc_dir, self.petsc_arch, *args)

    def read_conf(self):
        self.conf = dict()
        for line in open(self.petsc_path('include', 'petscconf.h')):
            if line.startswith('#define '):
                define = line[len('#define '):]
                space = define.find(' ')
                key = define[:space]
                val = define[space+1:]
                self.conf[key] = val
        self.conf.update(parse_makefile(self.petsc_path('conf', 'petscvariables')))
        for line in open(self.arch_path('include', 'slepcconf.h')):
            if line.startswith('#define '):
                define = line[len('#define '):]
                space = define.find(' ')
                key = define[:space]
                val = define[space+1:]
                self.conf[key] = val
        self.conf.update(parse_makefile(self.arch_path('conf', 'slepcvariables')))
        self.have_fortran = int(self.conf.get('PETSC_HAVE_FORTRAN', '0'))

    def inconf(self, key, val):
        if key in ['package', 'function', 'define']:
            return self.conf.get(val)
        elif key == 'precision':
            return val == self.conf['PETSC_PRECISION']
        elif key == 'scalar':
            return val == self.conf['PETSC_SCALAR']
        elif key == 'language':
            return val == self.conf['PETSC_LANGUAGE']
        raise RuntimeError('Unknown conf check: %s %s' % (key, val))

    def relpath(self, root, src):
        return os.path.relpath(os.path.join(root, src), self.slepc_dir)

    def get_sources(self, makevars):
        """Return dict {lang: list_of_source_files}"""
        source = dict()
        for lang, sourcelang in LANGS.items():
            source[lang] = [f for f in makevars.get('SOURCE'+sourcelang,'').split() if f.endswith(lang)]
        return source

    def gen_pkg(self, pkg):
        pkgsrcs = dict()
        for lang in LANGS:
            pkgsrcs[lang] = []
        for root, dirs, files in os.walk(os.path.join(self.slepc_dir, 'src', pkg)):
            makefile = os.path.join(root,'makefile')
            if not os.path.exists(makefile):
                dirs[:] = []
                continue
            mklines = open(makefile)
            conditions = set(tuple(stripsplit(line)) for line in mklines if line.startswith('#requires'))
            mklines.close()
            if not all(self.inconf(key, val) for key, val in conditions):
                dirs[:] = []
                continue
            makevars = parse_makefile(makefile)
            mdirs = makevars.get('DIRS','').split() # Directories specified in the makefile
            self.mistakes.compareDirLists(root, mdirs, dirs) # diagnostic output to find unused directories
            candidates = set(mdirs).union(AUTODIRS).difference(SKIPDIRS)
            dirs[:] = list(candidates.intersection(dirs))
            allsource = []
            def mkrel(src):
                return self.relpath(root, src)
            source = self.get_sources(makevars)
            for lang, s in source.items():
                pkgsrcs[lang] += map(mkrel, s)
                allsource += s
            self.mistakes.compareSourceLists(root, allsource, files) # Diagnostic output about unused source files
            self.gendeps.append(self.relpath(root, 'makefile'))
        return pkgsrcs

    def gen_gnumake(self, fd):
        def write(stem, srcs):
            fd.write('%s :=\n' % stem)
            for lang in LANGS:
                fd.write('%(stem)s.%(lang)s := %(srcs)s\n' % dict(stem=stem, lang=lang, srcs=' '.join(srcs[lang])))
                fd.write('%(stem)s += $(%(stem)s.%(lang)s)\n' % dict(stem=stem, lang=lang))
        for pkg in PKGS:
            srcs = self.gen_pkg(pkg)
            write('srcs-' + pkg, srcs)
        return self.gendeps

    def summary(self):
        self.mistakes.summary()
示例#11
0
class Petsc(object):
    def __init__(self, petsc_dir=None, petsc_arch=None, pkg_dir=None, pkg_name=None, pkg_arch=None, pkg_pkgs=None, verbose=False):
        if petsc_dir is None:
            petsc_dir = os.environ.get('PETSC_DIR')
            if petsc_dir is None:
                try:
                    petsc_dir = parse_makefile(os.path.join('lib','petsc','conf', 'petscvariables')).get('PETSC_DIR')
                finally:
                    if petsc_dir is None:
                        raise RuntimeError('Could not determine PETSC_DIR, please set in environment')
        if petsc_arch is None:
            petsc_arch = os.environ.get('PETSC_ARCH')
            if petsc_arch is None:
                try:
                    petsc_arch = parse_makefile(os.path.join(petsc_dir, 'lib','petsc','conf', 'petscvariables')).get('PETSC_ARCH')
                finally:
                    if petsc_arch is None:
                        raise RuntimeError('Could not determine PETSC_ARCH, please set in environment')
        self.petsc_dir = os.path.normpath(petsc_dir)
        self.petsc_arch = petsc_arch.rstrip(os.sep)
        self.pkg_dir = pkg_dir
        self.pkg_name = pkg_name
        self.pkg_arch = pkg_arch
        if self.pkg_dir is None:
          self.pkg_dir = petsc_dir
          self.pkg_name = 'petsc'
          self.pkg_arch = self.petsc_arch
        if self.pkg_name is None:
          self.pkg_name = os.path.basename(os.path.normpath(self.pkg_dir))
        if self.pkg_arch is None:
          self.pkg_arch = self.petsc_arch
        self.pkg_pkgs = PetscPKGS
        if pkg_pkgs is not None:
          self.pkg_pkgs += list(set(pkg_pkgs.split(','))-set(self.pkg_pkgs))
        self.read_conf()
        try:
            logging.basicConfig(filename=self.pkg_arch_path('lib',self.pkg_name,'conf', 'gmake.log'), level=logging.DEBUG)
        except IOError:
            # Disable logging if path is not writeable (e.g., prefix install)
            logging.basicConfig(filename='/dev/null', level=logging.DEBUG)
        self.log = logging.getLogger('gmakegen')
        self.mistakes = Mistakes(debuglogger(self.log), verbose=verbose)
        self.gendeps = []

    def arch_path(self, *args):
        return os.path.join(self.petsc_dir, self.petsc_arch, *args)

    def pkg_arch_path(self, *args):
        return os.path.join(self.pkg_dir, self.pkg_arch, *args)

    def read_conf(self):
        self.conf = dict()
        for line in open(self.arch_path('include', 'petscconf.h')):
            if line.startswith('#define '):
                define = line[len('#define '):]
                space = define.find(' ')
                key = define[:space]
                val = define[space+1:]
                self.conf[key] = val
        self.conf.update(parse_makefile(self.arch_path('lib','petsc','conf', 'petscvariables')))
        # allow parsing package additional configurations (if any)
        if self.pkg_name != 'petsc' :
            f = self.pkg_arch_path('include', self.pkg_name + 'conf.h')
            if os.path.isfile(f):
                for line in open(self.pkg_arch_path('include', self.pkg_name + 'conf.h')):
                    if line.startswith('#define '):
                        define = line[len('#define '):]
                        space = define.find(' ')
                        key = define[:space]
                        val = define[space+1:]
                        self.conf[key] = val
            f = self.pkg_arch_path('lib',self.pkg_name,'conf', self.pkg_name + 'variables')
            if os.path.isfile(f):
                self.conf.update(parse_makefile(self.pkg_arch_path('lib',self.pkg_name,'conf', self.pkg_name + 'variables')))
        self.have_fortran = int(self.conf.get('PETSC_HAVE_FORTRAN', '0'))

    def inconf(self, key, val):
        if key in ['package', 'function', 'define']:
            return self.conf.get(val)
        elif key == 'precision':
            return val == self.conf['PETSC_PRECISION']
        elif key == 'scalar':
            return val == self.conf['PETSC_SCALAR']
        elif key == 'language':
            return val == self.conf['PETSC_LANGUAGE']
        raise RuntimeError('Unknown conf check: %s %s' % (key, val))

    def relpath(self, root, src):
        return os.path.relpath(os.path.join(root, src), self.pkg_dir)

    def get_sources(self, makevars):
        """Return dict {lang: list_of_source_files}"""
        source = dict()
        for lang, sourcelang in LANGS.items():
            source[lang] = [f for f in makevars.get('SOURCE'+sourcelang,'').split() if f.endswith(lang)]
        return source

    def gen_pkg(self, pkg):
        pkgsrcs = dict()
        for lang in LANGS:
            pkgsrcs[lang] = []
        for root, dirs, files in os.walk(os.path.join(self.pkg_dir, 'src', pkg)):
            dirs.sort()
            files.sort()
            makefile = os.path.join(root,'makefile')
            if not os.path.exists(makefile):
                dirs[:] = []
                continue
            mklines = open(makefile)
            conditions = set(tuple(stripsplit(line)) for line in mklines if line.startswith('#requires'))
            mklines.close()
            if not all(self.inconf(key, val) for key, val in conditions):
                dirs[:] = []
                continue
            makevars = parse_makefile(makefile)
            mdirs = makevars.get('DIRS','').split() # Directories specified in the makefile
            self.mistakes.compareDirLists(root, mdirs, dirs) # diagnostic output to find unused directories
            candidates = set(mdirs).union(AUTODIRS).difference(SKIPDIRS)
            dirs[:] = list(candidates.intersection(dirs))
            allsource = []
            def mkrel(src):
                return self.relpath(root, src)
            source = self.get_sources(makevars)
            for lang, s in source.items():
                pkgsrcs[lang] += [mkrel(t) for t in s]
                allsource += s
            self.mistakes.compareSourceLists(root, allsource, files) # Diagnostic output about unused source files
            self.gendeps.append(self.relpath(root, 'makefile'))
        return pkgsrcs

    def gen_gnumake(self, fd):
        def write(stem, srcs):
            for lang in LANGS:
                fd.write('%(stem)s.%(lang)s := %(srcs)s\n' % dict(stem=stem, lang=lang, srcs=' '.join(srcs[lang])))
        for pkg in self.pkg_pkgs:
            srcs = self.gen_pkg(pkg)
            write('srcs-' + pkg, srcs)
        return self.gendeps

    def gen_ninja(self, fd):
        libobjs = []
        for pkg in self.pkg_pkgs:
            srcs = self.gen_pkg(pkg)
            for lang in LANGS:
                for src in srcs[lang]:
                    obj = '$objdir/%s.o' % src
                    fd.write('build %(obj)s : %(lang)s_COMPILE %(src)s\n' % dict(obj=obj, lang=lang.upper(), src=os.path.join(self.pkg_dir,src)))
                    libobjs.append(obj)
        fd.write('\n')
        fd.write('build $libdir/libpetsc.so : %s_LINK_SHARED %s\n\n' % ('CF'[self.have_fortran], ' '.join(libobjs)))
        fd.write('build petsc : phony || $libdir/libpetsc.so\n\n')

    def summary(self):
        self.mistakes.summary()