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