xref: /petsc/config/gmakegen.py (revision a3f1d042deeee8d591d0e166df91c7782e45ac59)
1#!/usr/bin/env python3
2
3import os
4import sys
5import logging
6sys.path.insert(0, os.path.abspath(os.path.dirname(__file__)))
7from collections import defaultdict
8
9AUTODIRS = set('ftn-auto ftn-custom f90-custom ftn-auto-interfaces'.split()) # Automatically recurse into these, if they exist
10SKIPDIRS = set('benchmarks build mex-scripts tests tutorials'.split())       # Skip these during the build
11
12def pathsplit(pkg_dir, path):
13    """Recursively split a path, returns a tuple"""
14    stem, basename = os.path.split(path)
15    if stem == '' or stem == pkg_dir:
16        return (basename,)
17    if stem == path: # fixed point, likely '/'
18        return (None,)
19    return pathsplit(pkg_dir, stem) + (basename,)
20
21def getlangext(name):
22    """Returns everything after the first . in the filename, including the ."""
23    file = os.path.basename(name)
24    loc = file.find('.')
25    if loc > -1: return file[loc:]
26    else: return ''
27
28def getlangsplit(name):
29    """Returns everything before the first . in the filename, excluding the ."""
30    file = os.path.basename(name)
31    loc = file.find('.')
32    if loc > -1: return os.path.join(os.path.dirname(name),file[:loc])
33    raise RuntimeError("No . in filename")
34
35def stripsplit(line):
36  return line[len('#requires'):].replace("'","").split()
37
38def parse_makefile(fn, out=None):
39    if out is None:
40        out = {}
41    with open(fn) as f:
42        for l in f:
43            if "=" in l:
44                a,b = l.split("=", 1)
45                out[a.strip()] = b.strip()
46    return out
47
48PetscPKGS = 'sys vec mat dm ksp snes ts tao'.split()
49# the key is actually the language suffix, it won't work for suffixes such as 'kokkos.cxx' so use an _ and replace the _ as needed with .
50LANGS = dict(kokkos_cxx='KOKKOS', hip_cpp='HIP', sycl_cxx='SYCL', raja_cxx='RAJA', c='C', cxx='CXX', cpp='CPP', cu='CU', F='F', F90='F90')
51
52class debuglogger(object):
53    def __init__(self, log):
54        self._log = log
55
56    def write(self, string):
57        self._log.debug(string)
58
59class Petsc(object):
60    def __init__(self, petsc_dir=None, petsc_arch=None, pkg_dir=None, pkg_name=None, pkg_arch=None, pkg_pkgs=None):
61        if petsc_dir is None:
62            petsc_dir = os.environ.get('PETSC_DIR')
63            if petsc_dir is None:
64                try:
65                    petsc_dir = parse_makefile(os.path.join('lib','petsc','conf', 'petscvariables')).get('PETSC_DIR')
66                finally:
67                    if petsc_dir is None:
68                        raise RuntimeError('Could not determine PETSC_DIR, please set in environment')
69        if petsc_arch is None:
70            petsc_arch = os.environ.get('PETSC_ARCH')
71            if petsc_arch is None:
72                try:
73                    petsc_arch = parse_makefile(os.path.join(petsc_dir, 'lib','petsc','conf', 'petscvariables')).get('PETSC_ARCH')
74                finally:
75                    if petsc_arch is None:
76                        raise RuntimeError('Could not determine PETSC_ARCH, please set in environment')
77        self.petsc_dir = os.path.normpath(petsc_dir)
78        self.petsc_arch = petsc_arch.rstrip(os.sep)
79        self.pkg_dir = pkg_dir
80        self.pkg_name = pkg_name
81        self.pkg_arch = pkg_arch
82        if self.pkg_dir is None:
83          self.pkg_dir = petsc_dir
84          self.pkg_name = 'petsc'
85          self.pkg_arch = self.petsc_arch
86        if self.pkg_name is None:
87          self.pkg_name = os.path.basename(os.path.normpath(self.pkg_dir))
88        if self.pkg_arch is None:
89          self.pkg_arch = self.petsc_arch
90        self.pkg_pkgs = PetscPKGS
91        if pkg_pkgs is not None:
92          if pkg_pkgs.find(',') > 0: npkgs = set(pkg_pkgs.split(','))
93          else: npkgs = set(pkg_pkgs.split(' '))
94          self.pkg_pkgs += list(npkgs - set(self.pkg_pkgs))
95        self.read_conf()
96        try:
97            logging.basicConfig(filename=self.pkg_arch_path('lib',self.pkg_name,'conf', 'gmake.log'), level=logging.DEBUG)
98        except IOError:
99            # Disable logging if path is not writeable (e.g., prefix install)
100            logging.basicConfig(filename='/dev/null', level=logging.DEBUG)
101        self.log = logging.getLogger('gmakegen')
102        self.gendeps = []
103
104    def arch_path(self, *args):
105        return os.path.join(self.petsc_dir, self.petsc_arch, *args)
106
107    def pkg_arch_path(self, *args):
108        return os.path.join(self.pkg_dir, self.pkg_arch, *args)
109
110    def read_conf(self):
111        self.conf = dict()
112        with open(self.arch_path('include', 'petscconf.h')) as petscconf_h:
113            for line in petscconf_h:
114                if line.startswith('#define '):
115                    define = line[len('#define '):]
116                    space = define.find(' ')
117                    key = define[:space]
118                    val = define[space+1:]
119                    self.conf[key] = val
120        self.conf.update(parse_makefile(self.arch_path('lib','petsc','conf', 'petscvariables')))
121        # allow parsing package additional configurations (if any)
122        if self.pkg_name != 'petsc' :
123            f = self.pkg_arch_path('include', self.pkg_name + 'conf.h')
124            if os.path.isfile(f):
125                with open(self.pkg_arch_path('include', self.pkg_name + 'conf.h')) as pkg_conf_h:
126                    for line in pkg_conf_h:
127                        if line.startswith('#define '):
128                            define = line[len('#define '):]
129                            space = define.find(' ')
130                            key = define[:space]
131                            val = define[space+1:]
132                            self.conf[key] = val
133            f = self.pkg_arch_path('lib',self.pkg_name,'conf', self.pkg_name + 'variables')
134            if os.path.isfile(f):
135                self.conf.update(parse_makefile(self.pkg_arch_path('lib',self.pkg_name,'conf', self.pkg_name + 'variables')))
136        self.have_fortran = int(self.conf.get('PETSC_USE_FORTRAN_BINDINGS', '0'))
137
138    def inconf(self, key, val):
139        if key in ['package', 'function', 'define']:
140            return self.conf.get(val)
141        elif key == 'precision':
142            return val == self.conf['PETSC_PRECISION']
143        elif key == 'scalar':
144            return val == self.conf['PETSC_SCALAR']
145        elif key == 'language':
146            return val == self.conf['PETSC_LANGUAGE']
147        raise RuntimeError('Unknown conf check: %s %s' % (key, val))
148
149    def relpath(self, root, src):
150        return os.path.relpath(os.path.join(root, src), self.pkg_dir)
151
152    def get_sources_from_files(self, files):
153        """Return dict {lang: list_of_source_files}"""
154        source = dict()
155        for lang, sourcelang in LANGS.items():
156            source[lang] = [f for f in files if f.endswith('.'+lang.replace('_','.'))]
157            files = [f for f in files if not f.endswith('.'+lang.replace('_','.'))]
158        return source
159
160    def gen_pkg(self, pkg):
161        from itertools import chain
162        pkgsrcs = dict()
163        for lang in LANGS:
164            pkgsrcs[lang] = []
165        for root, dirs, files in chain.from_iterable(os.walk(path) for path in [os.path.join(self.pkg_dir, 'src', pkg),os.path.join(self.pkg_dir, self.pkg_arch, 'src', pkg)]):
166            if SKIPDIRS.intersection(pathsplit(self.pkg_dir, root)): continue
167            dirs.sort()
168            dirs[:] = list(set(dirs).difference(SKIPDIRS))
169            files.sort()
170            makefile = os.path.join(root,'makefile')
171            if os.path.isfile(makefile):
172              with open(makefile) as mklines:
173                conditions = set(tuple(stripsplit(line)) for line in mklines if line.startswith('#requires'))
174              if not all(self.inconf(key, val) for key, val in conditions):
175                dirs[:] = []
176                continue
177            allsource = []
178            def mkrel(src):
179                return self.relpath(root, src)
180            if files:
181              source = self.get_sources_from_files(files)
182              for lang, s in source.items():
183                  pkgsrcs[lang] += [mkrel(t) for t in s]
184              if os.path.isfile(makefile): self.gendeps.append(self.relpath(root, 'makefile'))
185        return pkgsrcs
186
187    def gen_gnumake(self, fd):
188        def write(stem, srcs):
189            for lang in LANGS:
190                fd.write('%(stem)s.%(lang)s := %(srcs)s\n' % dict(stem=stem, lang=lang.replace('_','.'), srcs=' '.join(sorted(srcs[lang]))))
191        for pkg in self.pkg_pkgs:
192            srcs = self.gen_pkg(pkg)
193            write('srcs-' + pkg, srcs)
194        return self.gendeps
195
196    def gen_ninja(self, fd):
197        libobjs = []
198        for pkg in self.pkg_pkgs:
199            srcs = self.gen_pkg(pkg)
200            for lang in LANGS:
201                for src in srcs[lang]:
202                    obj = '$objdir/%s.o' % src
203                    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)))
204                    libobjs.append(obj)
205        fd.write('\n')
206        fd.write('build $libdir/libpetsc.so : %s_LINK_SHARED %s\n\n' % ('CF'[self.have_fortran], ' '.join(libobjs)))
207        fd.write('build petsc : phony || $libdir/libpetsc.so\n\n')
208
209def WriteGnuMake(petsc):
210    arch_files = petsc.pkg_arch_path('lib',petsc.pkg_name,'conf', 'files')
211    with open(arch_files, 'w') as fd:
212        gendeps = petsc.gen_gnumake(fd)
213        fd.write('\n')
214        fd.write('# Dependency to regenerate this file\n')
215        fd.write('%s : %s %s\n' % (os.path.relpath(arch_files, petsc.pkg_dir),
216                                   os.path.relpath(__file__, os.path.realpath(petsc.pkg_dir)),
217                                   ' '.join(gendeps)))
218        fd.write('\n')
219        fd.write('# Dummy dependencies in case makefiles are removed\n')
220        fd.write(''.join([dep + ':\n' for dep in gendeps]))
221
222def WriteNinja(petsc):
223    conf = dict()
224    parse_makefile(os.path.join(petsc.petsc_dir, 'lib', 'petsc','conf', 'variables'), conf)
225    parse_makefile(petsc.arch_path('lib','petsc','conf', 'petscvariables'), conf)
226    build_ninja = petsc.arch_path('build.ninja')
227    with open(build_ninja, 'w') as fd:
228        fd.write('objdir = obj-ninja\n')
229        fd.write('libdir = lib\n')
230        fd.write('c_compile = %(PCC)s\n' % conf)
231        fd.write('c_flags = %(PETSC_CC_INCLUDES)s %(PCC_FLAGS)s %(CCPPFLAGS)s\n' % conf)
232        fd.write('c_link = %(PCC_LINKER)s\n' % conf)
233        fd.write('c_link_flags = %(PCC_LINKER_FLAGS)s\n' % conf)
234        if petsc.have_fortran:
235            fd.write('f_compile = %(FC)s\n' % conf)
236            fd.write('f_flags = %(PETSC_FC_INCLUDES)s %(FC_FLAGS)s %(FCPPFLAGS)s\n' % conf)
237            fd.write('f_link = %(FC_LINKER)s\n' % conf)
238            fd.write('f_link_flags = %(FC_LINKER_FLAGS)s\n' % conf)
239        fd.write('petsc_external_lib = %(PETSC_EXTERNAL_LIB_BASIC)s\n' % conf)
240        fd.write('python = %(PYTHON)s\n' % conf)
241        fd.write('\n')
242        fd.write('rule C_COMPILE\n'
243                 '  command = $c_compile -MMD -MF $out.d $c_flags -c $in -o $out\n'
244                 '  description = CC $out\n'
245                 '  depfile = $out.d\n'
246                 # '  deps = gcc\n') # 'gcc' is default, 'msvc' only recognized by newer versions of ninja
247                 '\n')
248        fd.write('rule C_LINK_SHARED\n'
249                 '  command = $c_link $c_link_flags -shared -o $out $in $petsc_external_lib\n'
250                 '  description = CLINK_SHARED $out\n'
251                 '\n')
252        if petsc.have_fortran:
253            fd.write('rule F_COMPILE\n'
254                     '  command = $f_compile -MMD -MF $out.d $f_flags -c $in -o $out\n'
255                     '  description = FC $out\n'
256                     '  depfile = $out.d\n'
257                     '\n')
258            fd.write('rule F_LINK_SHARED\n'
259                     '  command = $f_link $f_link_flags -shared -o $out $in $petsc_external_lib\n'
260                     '  description = FLINK_SHARED $out\n'
261                     '\n')
262        fd.write('rule GEN_NINJA\n'
263                 '  command = $python $in --output=ninja\n'
264                 '  generator = 1\n'
265                 '\n')
266        petsc.gen_ninja(fd)
267        fd.write('\n')
268        fd.write('build %s : GEN_NINJA | %s %s %s %s\n' % (build_ninja,
269                                                           os.path.abspath(__file__),
270                                                           os.path.join(petsc.petsc_dir, 'lib','petsc','conf', 'variables'),
271                                                           petsc.arch_path('lib','petsc','conf', 'petscvariables'),
272                                                       ' '.join(os.path.join(petsc.pkg_dir, dep) for dep in petsc.gendeps)))
273
274def main(petsc_dir=None, petsc_arch=None, pkg_dir=None, pkg_name=None, pkg_arch=None, pkg_pkgs=None, output=None):
275    if output is None:
276        output = 'gnumake'
277    writer = dict(gnumake=WriteGnuMake, ninja=WriteNinja)
278    petsc = Petsc(petsc_dir=petsc_dir, petsc_arch=petsc_arch, pkg_dir=pkg_dir, pkg_name=pkg_name, pkg_arch=pkg_arch, pkg_pkgs=pkg_pkgs)
279    writer[output](petsc)
280
281if __name__ == '__main__':
282    import optparse
283    parser = optparse.OptionParser()
284    parser.add_option('--petsc-arch', help='Set PETSC_ARCH different from environment', default=os.environ.get('PETSC_ARCH'))
285    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)
286    parser.add_option('--pkg-name', help='Set the name of the package you want to generate the makefile rules for', default=None)
287    parser.add_option('--pkg-arch', help='Set the package arch name you want to generate the makefile rules for', default=None)
288    parser.add_option('--pkg-pkgs', help='Set the package folders (comma separated list, different from the usual sys,vec,mat etc) you want to generate the makefile rules for', default=None)
289    parser.add_option('--output', help='Location to write output file', default=None)
290    opts, extra_args = parser.parse_args()
291    if extra_args:
292        import sys
293        sys.stderr.write('Unknown arguments: %s\n' % ' '.join(extra_args))
294        exit(1)
295    main(petsc_arch=opts.petsc_arch, pkg_dir=opts.pkg_dir, pkg_name=opts.pkg_name, pkg_arch=opts.pkg_arch, pkg_pkgs=opts.pkg_pkgs, output=opts.output)
296