xref: /petsc/config/gmakegen.py (revision 0cd39abacf2394cd13758a5e89aeeb1f22ef1336)
1#!/usr/bin/env python
2
3import os
4from distutils.sysconfig import parse_makefile
5import sys
6import logging
7sys.path.insert(0, os.path.abspath(os.path.dirname(__file__)))
8from cmakegen import Mistakes, stripsplit, AUTODIRS, SKIPDIRS
9from cmakegen import defaultdict # collections.defaultdict, with fallback for python-2.4
10
11PKGS = 'sys vec mat dm ksp snes ts tao'.split()
12#LANGS = dict(c='C', cxx='CXX', cu='CU', F='F')
13LANGS = dict(c='C', cxx='CXX', cu='CU', F='F',F90='F90')
14
15try:
16    all([True, True])
17except NameError:               # needs python-2.5
18    def all(iterable):
19        for i in iterable:
20            if not i:
21                return False
22        return True
23
24try:
25    os.path.relpath             # needs python-2.6
26except AttributeError:
27    def _relpath(path, start=os.path.curdir):
28        """Return a relative version of a path"""
29
30        from os.path import curdir, abspath, commonprefix, sep, pardir, join
31        if not path:
32            raise ValueError("no path specified")
33
34        start_list = [x for x in abspath(start).split(sep) if x]
35        path_list = [x for x in abspath(path).split(sep) if x]
36
37        # Work out how much of the filepath is shared by start and path.
38        i = len(commonprefix([start_list, path_list]))
39
40        rel_list = [pardir] * (len(start_list)-i) + path_list[i:]
41        if not rel_list:
42            return curdir
43        return join(*rel_list)
44    os.path.relpath = _relpath
45
46class debuglogger(object):
47    def __init__(self, log):
48        self._log = log
49
50    def write(self, string):
51        self._log.debug(string)
52
53class Petsc(object):
54    def __init__(self, petsc_dir=None, petsc_arch=None, verbose=False):
55        if petsc_dir is None:
56            petsc_dir = os.environ.get('PETSC_DIR')
57            if petsc_dir is None:
58                try:
59                    petsc_dir = parse_makefile(os.path.join('lib','petsc','conf', 'petscvariables')).get('PETSC_DIR')
60                finally:
61                    if petsc_dir is None:
62                        raise RuntimeError('Could not determine PETSC_DIR, please set in environment')
63        if petsc_arch is None:
64            petsc_arch = os.environ.get('PETSC_ARCH')
65            if petsc_arch is None:
66                try:
67                    petsc_arch = parse_makefile(os.path.join(petsc_dir, 'lib','petsc','conf', 'petscvariables')).get('PETSC_ARCH')
68                finally:
69                    if petsc_arch is None:
70                        raise RuntimeError('Could not determine PETSC_ARCH, please set in environment')
71        self.petsc_dir = petsc_dir
72        self.petsc_arch = petsc_arch
73        self.read_conf()
74        logging.basicConfig(filename=self.arch_path('lib','petsc','conf', 'gmake.log'), level=logging.DEBUG)
75        self.log = logging.getLogger('gmakegen')
76        self.mistakes = Mistakes(debuglogger(self.log), verbose=verbose)
77        self.gendeps = []
78
79    def arch_path(self, *args):
80        return os.path.join(self.petsc_dir, self.petsc_arch, *args)
81
82    def read_conf(self):
83        self.conf = dict()
84        for line in open(self.arch_path('include', 'petscconf.h')):
85            if line.startswith('#define '):
86                define = line[len('#define '):]
87                space = define.find(' ')
88                key = define[:space]
89                val = define[space+1:]
90                self.conf[key] = val
91        self.conf.update(parse_makefile(self.arch_path('lib','petsc','conf', 'petscvariables')))
92        self.have_fortran = int(self.conf.get('PETSC_HAVE_FORTRAN', '0'))
93
94    def inconf(self, key, val):
95        if key in ['package', 'function', 'define']:
96            return self.conf.get(val)
97        elif key == 'precision':
98            return val == self.conf['PETSC_PRECISION']
99        elif key == 'scalar':
100            return val == self.conf['PETSC_SCALAR']
101        elif key == 'language':
102            return val == self.conf['PETSC_LANGUAGE']
103        raise RuntimeError('Unknown conf check: %s %s' % (key, val))
104
105    def relpath(self, root, src):
106        return os.path.relpath(os.path.join(root, src), self.petsc_dir)
107
108    def get_sources(self, makevars):
109        """Return dict {lang: list_of_source_files}"""
110        source = dict()
111        for lang, sourcelang in LANGS.items():
112            source[lang] = [f for f in makevars.get('SOURCE'+sourcelang,'').split() if f.endswith(lang)]
113        return source
114
115    def gen_pkg(self, pkg):
116        pkgsrcs = dict()
117        for lang in LANGS:
118            pkgsrcs[lang] = []
119        for root, dirs, files in os.walk(os.path.join(self.petsc_dir, 'src', pkg)):
120            makefile = os.path.join(root,'makefile')
121            if not os.path.exists(makefile):
122                dirs[:] = []
123                continue
124            mklines = open(makefile)
125            conditions = set(tuple(stripsplit(line)) for line in mklines if line.startswith('#requires'))
126            mklines.close()
127            if not all(self.inconf(key, val) for key, val in conditions):
128                dirs[:] = []
129                continue
130            makevars = parse_makefile(makefile)
131            mdirs = makevars.get('DIRS','').split() # Directories specified in the makefile
132            self.mistakes.compareDirLists(root, mdirs, dirs) # diagnostic output to find unused directories
133            candidates = set(mdirs).union(AUTODIRS).difference(SKIPDIRS)
134            dirs[:] = list(candidates.intersection(dirs))
135            allsource = []
136            def mkrel(src):
137                return self.relpath(root, src)
138            source = self.get_sources(makevars)
139            for lang, s in source.items():
140                pkgsrcs[lang] += map(mkrel, s)
141                allsource += s
142            self.mistakes.compareSourceLists(root, allsource, files) # Diagnostic output about unused source files
143            self.gendeps.append(self.relpath(root, 'makefile'))
144        return pkgsrcs
145
146    def gen_gnumake(self, fd,prefix='srcs-'):
147        def write(stem, srcs):
148            fd.write('%s :=\n' % stem)
149            for lang in LANGS:
150                fd.write('%(stem)s.%(lang)s := %(srcs)s\n' % dict(stem=stem, lang=lang, srcs=' '.join(srcs[lang])))
151                fd.write('%(stem)s += $(%(stem)s.%(lang)s)\n' % dict(stem=stem, lang=lang))
152        for pkg in PKGS:
153            srcs = self.gen_pkg(pkg)
154            write(prefix + pkg, srcs)
155        return self.gendeps
156
157    def gen_ninja(self, fd):
158        libobjs = []
159        for pkg in PKGS:
160            srcs = self.gen_pkg(pkg)
161            for lang in LANGS:
162                for src in srcs[lang]:
163                    obj = '$objdir/%s.o' % src
164                    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)))
165                    libobjs.append(obj)
166        fd.write('\n')
167        fd.write('build $libdir/libpetsc.so : %s_LINK_SHARED %s\n\n' % ('CF'[self.have_fortran], ' '.join(libobjs)))
168        fd.write('build petsc : phony || $libdir/libpetsc.so\n\n')
169
170    def summary(self):
171        self.mistakes.summary()
172
173def WriteGnuMake(petsc):
174    arch_files = petsc.arch_path('lib','petsc','conf', 'files')
175    fd = open(arch_files, 'w')
176    gendeps = petsc.gen_gnumake(fd)
177    fd.write('\n')
178    fd.write('# Dependency to regenerate this file\n')
179    fd.write('%s : %s %s\n' % (os.path.relpath(arch_files, petsc.petsc_dir),
180                               os.path.relpath(__file__, os.path.realpath(petsc.petsc_dir)),
181                               ' '.join(gendeps)))
182    fd.write('\n')
183    fd.write('# Dummy dependencies in case makefiles are removed\n')
184    fd.write(''.join([dep + ':\n' for dep in gendeps]))
185    fd.close()
186
187def WriteNinja(petsc):
188    conf = dict()
189    parse_makefile(os.path.join(petsc.petsc_dir, 'lib', 'petsc','conf', 'variables'), conf)
190    parse_makefile(petsc.arch_path('lib','petsc','conf', 'petscvariables'), conf)
191    build_ninja = petsc.arch_path('build.ninja')
192    fd = open(build_ninja, 'w')
193    fd.write('objdir = obj-ninja\n')
194    fd.write('libdir = lib\n')
195    fd.write('c_compile = %(PCC)s\n' % conf)
196    fd.write('c_flags = %(PETSC_CC_INCLUDES)s %(PCC_FLAGS)s %(CCPPFLAGS)s\n' % conf)
197    fd.write('c_link = %(PCC_LINKER)s\n' % conf)
198    fd.write('c_link_flags = %(PCC_LINKER_FLAGS)s\n' % conf)
199    if petsc.have_fortran:
200        fd.write('f_compile = %(FC)s\n' % conf)
201        fd.write('f_flags = %(PETSC_FC_INCLUDES)s %(FC_FLAGS)s %(FCPPFLAGS)s\n' % conf)
202        fd.write('f_link = %(FC_LINKER)s\n' % conf)
203        fd.write('f_link_flags = %(FC_LINKER_FLAGS)s\n' % conf)
204    fd.write('petsc_external_lib = %(PETSC_EXTERNAL_LIB_BASIC)s\n' % conf)
205    fd.write('python = %(PYTHON)s\n' % conf)
206    fd.write('\n')
207    fd.write('rule C_COMPILE\n'
208             '  command = $c_compile -MMD -MF $out.d $c_flags -c $in -o $out\n'
209             '  description = CC $out\n'
210             '  depfile = $out.d\n'
211             # '  deps = gcc\n') # 'gcc' is default, 'msvc' only recognized by newer versions of ninja
212             '\n')
213    fd.write('rule C_LINK_SHARED\n'
214             '  command = $c_link $c_link_flags -shared -o $out $in $petsc_external_lib\n'
215             '  description = CLINK_SHARED $out\n'
216             '\n')
217    if petsc.have_fortran:
218        fd.write('rule F_COMPILE\n'
219                 '  command = $f_compile -MMD -MF $out.d $f_flags -c $in -o $out\n'
220                 '  description = FC $out\n'
221                 '  depfile = $out.d\n'
222                 '\n')
223        fd.write('rule F_LINK_SHARED\n'
224                 '  command = $f_link $f_link_flags -shared -o $out $in $petsc_external_lib\n'
225                 '  description = FLINK_SHARED $out\n'
226                 '\n')
227    fd.write('rule GEN_NINJA\n'
228             '  command = $python $in --output=ninja\n'
229             '  generator = 1\n'
230             '\n')
231    petsc.gen_ninja(fd)
232    fd.write('\n')
233    fd.write('build %s : GEN_NINJA | %s %s %s %s\n' % (build_ninja,
234                                                       os.path.abspath(__file__),
235                                                       os.path.join(petsc.petsc_dir, 'lib','petsc','conf', 'variables'),
236                                                       petsc.arch_path('lib','petsc','conf', 'petscvariables'),
237                                                       ' '.join(os.path.join(petsc.petsc_dir, dep) for dep in petsc.gendeps)))
238
239def main(petsc_dir=None, petsc_arch=None, output=None, verbose=False):
240    if output is None:
241        output = 'gnumake'
242    writer = dict(gnumake=WriteGnuMake, ninja=WriteNinja)
243    petsc = Petsc(petsc_dir=petsc_dir, petsc_arch=petsc_arch, verbose=verbose)
244    writer[output](petsc)
245    petsc.summary()
246
247if __name__ == '__main__':
248    import optparse
249    parser = optparse.OptionParser()
250    parser.add_option('--verbose', help='Show mismatches between makefiles and the filesystem', action='store_true', default=False)
251    parser.add_option('--petsc-arch', help='Set PETSC_ARCH different from environment', default=os.environ.get('PETSC_ARCH'))
252    parser.add_option('--output', help='Location to write output file', default=None)
253    opts, extra_args = parser.parse_args()
254    if extra_args:
255        import sys
256        sys.stderr.write('Unknown arguments: %s\n' % ' '.join(extra_args))
257        exit(1)
258    main(petsc_arch=opts.petsc_arch, output=opts.output, verbose=opts.verbose)
259