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