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