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 collections import defaultdict 9 10AUTODIRS = set('ftn-auto ftn-custom f90-custom'.split()) # Automatically recurse into these, if they exist 11SKIPDIRS = set('benchmarks'.split()) # Skip these during the build 12NOWARNDIRS = set('tests tutorials'.split()) # Do not warn about mismatch in these 13 14def pathsplit(path): 15 """Recursively split a path, returns a tuple""" 16 stem, basename = os.path.split(path) 17 if stem == '': 18 return (basename,) 19 if stem == path: # fixed point, likely '/' 20 return (path,) 21 return pathsplit(stem) + (basename,) 22 23class Mistakes(object): 24 def __init__(self, log, verbose=False): 25 self.mistakes = [] 26 self.verbose = verbose 27 self.log = log 28 29 def compareDirLists(self,root, mdirs, dirs): 30 if NOWARNDIRS.intersection(pathsplit(root)): 31 return 32 smdirs = set(mdirs) 33 sdirs = set(dirs).difference(AUTODIRS) 34 if not smdirs.issubset(sdirs): 35 self.mistakes.append('Makefile contains directory not on filesystem: %s: %r' % (root, sorted(smdirs - sdirs))) 36 if not self.verbose: return 37 if smdirs != sdirs: 38 from sys import stderr 39 stderr.write('Directory mismatch at %s:\n\t%s: %r\n\t%s: %r\n\t%s: %r\n' 40 % (root, 41 'in makefile ',sorted(smdirs), 42 'on filesystem ',sorted(sdirs), 43 'symmetric diff',sorted(smdirs.symmetric_difference(sdirs)))) 44 45 def compareSourceLists(self, root, msources, files): 46 if NOWARNDIRS.intersection(pathsplit(root)): 47 return 48 smsources = set(msources) 49 ssources = set(f for f in files if os.path.splitext(f)[1] in ['.c', '.cxx', '.cc', '.cu', '.cpp', '.F', '.F90']) 50 if not smsources.issubset(ssources): 51 self.mistakes.append('Makefile contains file not on filesystem: %s: %r' % (root, sorted(smsources - ssources))) 52 if not self.verbose: return 53 if smsources != ssources: 54 from sys import stderr 55 stderr.write('Source mismatch at %s:\n\t%s: %r\n\t%s: %r\n\t%s: %r\n' 56 % (root, 57 'in makefile ',sorted(smsources), 58 'on filesystem ',sorted(ssources), 59 'symmetric diff',sorted(smsources.symmetric_difference(ssources)))) 60 61 def summary(self): 62 for m in self.mistakes: 63 self.log.write(m + '\n') 64 if self.mistakes: 65 raise RuntimeError('PETSc makefiles contain mistakes or files are missing on filesystem.\n%s\nPossible reasons:\n\t1. Files were deleted locally, try "hg revert filename" or "git checkout filename".\n\t2. Files were deleted from repository, but were not removed from makefile. Send mail to petsc-maint@mcs.anl.gov.\n\t3. Someone forgot to "add" new files to the repository. Send mail to petsc-maint@mcs.anl.gov.' % ('\n'.join(self.mistakes))) 66 67def stripsplit(line): 68 return line[len('#requires'):].replace("'","").split() 69 70PetscPKGS = 'sys vec mat dm ksp snes ts tao'.split() 71LANGS = dict(c='C', cxx='CXX', cpp='CPP', cu='CU', F='F', F90='F90', 72 hip='HIP.CPP', sycl='SYCL.CXX') 73 74class debuglogger(object): 75 def __init__(self, log): 76 self._log = log 77 78 def write(self, string): 79 self._log.debug(string) 80 81class Petsc(object): 82 def __init__(self, petsc_dir=None, petsc_arch=None, pkg_dir=None, pkg_name=None, pkg_arch=None, pkg_pkgs=None, verbose=False): 83 if petsc_dir is None: 84 petsc_dir = os.environ.get('PETSC_DIR') 85 if petsc_dir is None: 86 try: 87 petsc_dir = parse_makefile(os.path.join('lib','petsc','conf', 'petscvariables')).get('PETSC_DIR') 88 finally: 89 if petsc_dir is None: 90 raise RuntimeError('Could not determine PETSC_DIR, please set in environment') 91 if petsc_arch is None: 92 petsc_arch = os.environ.get('PETSC_ARCH') 93 if petsc_arch is None: 94 try: 95 petsc_arch = parse_makefile(os.path.join(petsc_dir, 'lib','petsc','conf', 'petscvariables')).get('PETSC_ARCH') 96 finally: 97 if petsc_arch is None: 98 raise RuntimeError('Could not determine PETSC_ARCH, please set in environment') 99 self.petsc_dir = os.path.normpath(petsc_dir) 100 self.petsc_arch = petsc_arch.rstrip(os.sep) 101 self.pkg_dir = pkg_dir 102 self.pkg_name = pkg_name 103 self.pkg_arch = pkg_arch 104 if self.pkg_dir is None: 105 self.pkg_dir = petsc_dir 106 self.pkg_name = 'petsc' 107 self.pkg_arch = self.petsc_arch 108 if self.pkg_name is None: 109 self.pkg_name = os.path.basename(os.path.normpath(self.pkg_dir)) 110 if self.pkg_arch is None: 111 self.pkg_arch = self.petsc_arch 112 self.pkg_pkgs = PetscPKGS 113 if pkg_pkgs is not None: 114 self.pkg_pkgs += list(set(pkg_pkgs.split(','))-set(self.pkg_pkgs)) 115 self.read_conf() 116 try: 117 logging.basicConfig(filename=self.pkg_arch_path('lib',self.pkg_name,'conf', 'gmake.log'), level=logging.DEBUG) 118 except IOError: 119 # Disable logging if path is not writeable (e.g., prefix install) 120 logging.basicConfig(filename='/dev/null', level=logging.DEBUG) 121 self.log = logging.getLogger('gmakegen') 122 self.mistakes = Mistakes(debuglogger(self.log), verbose=verbose) 123 self.gendeps = [] 124 125 def arch_path(self, *args): 126 return os.path.join(self.petsc_dir, self.petsc_arch, *args) 127 128 def pkg_arch_path(self, *args): 129 return os.path.join(self.pkg_dir, self.pkg_arch, *args) 130 131 def read_conf(self): 132 self.conf = dict() 133 with open(self.arch_path('include', 'petscconf.h')) as petscconf_h: 134 for line in petscconf_h: 135 if line.startswith('#define '): 136 define = line[len('#define '):] 137 space = define.find(' ') 138 key = define[:space] 139 val = define[space+1:] 140 self.conf[key] = val 141 self.conf.update(parse_makefile(self.arch_path('lib','petsc','conf', 'petscvariables'))) 142 # allow parsing package additional configurations (if any) 143 if self.pkg_name != 'petsc' : 144 f = self.pkg_arch_path('include', self.pkg_name + 'conf.h') 145 if os.path.isfile(f): 146 with open(self.pkg_arch_path('include', self.pkg_name + 'conf.h')) as pkg_conf_h: 147 for line in pkg_conf_h: 148 if line.startswith('#define '): 149 define = line[len('#define '):] 150 space = define.find(' ') 151 key = define[:space] 152 val = define[space+1:] 153 self.conf[key] = val 154 f = self.pkg_arch_path('lib',self.pkg_name,'conf', self.pkg_name + 'variables') 155 if os.path.isfile(f): 156 self.conf.update(parse_makefile(self.pkg_arch_path('lib',self.pkg_name,'conf', self.pkg_name + 'variables'))) 157 self.have_fortran = int(self.conf.get('PETSC_HAVE_FORTRAN', '0')) 158 159 def inconf(self, key, val): 160 if key in ['package', 'function', 'define']: 161 return self.conf.get(val) 162 elif key == 'precision': 163 return val == self.conf['PETSC_PRECISION'] 164 elif key == 'scalar': 165 return val == self.conf['PETSC_SCALAR'] 166 elif key == 'language': 167 return val == self.conf['PETSC_LANGUAGE'] 168 raise RuntimeError('Unknown conf check: %s %s' % (key, val)) 169 170 def relpath(self, root, src): 171 return os.path.relpath(os.path.join(root, src), self.pkg_dir) 172 173 def get_sources(self, makevars): 174 """Return dict {lang: list_of_source_files}""" 175 source = dict() 176 for lang, sourcelang in LANGS.items(): 177 source[lang] = [f for f in makevars.get('SOURCE'+sourcelang,'').split() if f.endswith(lang)] 178 return source 179 180 def gen_pkg(self, pkg): 181 pkgsrcs = dict() 182 for lang in LANGS: 183 pkgsrcs[lang] = [] 184 for root, dirs, files in os.walk(os.path.join(self.pkg_dir, 'src', pkg)): 185 dirs.sort() 186 files.sort() 187 makefile = os.path.join(root,'makefile') 188 if not os.path.exists(makefile): 189 dirs[:] = [] 190 continue 191 with open(makefile) as mklines: 192 conditions = set(tuple(stripsplit(line)) for line in mklines if line.startswith('#requires')) 193 if not all(self.inconf(key, val) for key, val in conditions): 194 dirs[:] = [] 195 continue 196 makevars = parse_makefile(makefile) 197 mdirs = makevars.get('DIRS','').split() # Directories specified in the makefile 198 self.mistakes.compareDirLists(root, mdirs, dirs) # diagnostic output to find unused directories 199 candidates = set(mdirs).union(AUTODIRS).difference(SKIPDIRS) 200 dirs[:] = list(candidates.intersection(dirs)) 201 allsource = [] 202 def mkrel(src): 203 return self.relpath(root, src) 204 source = self.get_sources(makevars) 205 for lang, s in source.items(): 206 pkgsrcs[lang] += [mkrel(t) for t in s] 207 allsource += s 208 self.mistakes.compareSourceLists(root, allsource, files) # Diagnostic output about unused source files 209 self.gendeps.append(self.relpath(root, 'makefile')) 210 return pkgsrcs 211 212 def gen_gnumake(self, fd): 213 def write(stem, srcs): 214 for lang in LANGS: 215 fd.write('%(stem)s.%(lang)s := %(srcs)s\n' % dict(stem=stem, lang=lang, srcs=' '.join(srcs[lang]))) 216 for pkg in self.pkg_pkgs: 217 srcs = self.gen_pkg(pkg) 218 write('srcs-' + pkg, srcs) 219 return self.gendeps 220 221 def gen_ninja(self, fd): 222 libobjs = [] 223 for pkg in self.pkg_pkgs: 224 srcs = self.gen_pkg(pkg) 225 for lang in LANGS: 226 for src in srcs[lang]: 227 obj = '$objdir/%s.o' % src 228 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))) 229 libobjs.append(obj) 230 fd.write('\n') 231 fd.write('build $libdir/libpetsc.so : %s_LINK_SHARED %s\n\n' % ('CF'[self.have_fortran], ' '.join(libobjs))) 232 fd.write('build petsc : phony || $libdir/libpetsc.so\n\n') 233 234 def summary(self): 235 self.mistakes.summary() 236 237def WriteGnuMake(petsc): 238 arch_files = petsc.pkg_arch_path('lib',petsc.pkg_name,'conf', 'files') 239 with open(arch_files, 'w') as fd: 240 gendeps = petsc.gen_gnumake(fd) 241 fd.write('\n') 242 fd.write('# Dependency to regenerate this file\n') 243 fd.write('%s : %s %s\n' % (os.path.relpath(arch_files, petsc.pkg_dir), 244 os.path.relpath(__file__, os.path.realpath(petsc.pkg_dir)), 245 ' '.join(gendeps))) 246 fd.write('\n') 247 fd.write('# Dummy dependencies in case makefiles are removed\n') 248 fd.write(''.join([dep + ':\n' for dep in gendeps])) 249 250def WriteNinja(petsc): 251 conf = dict() 252 parse_makefile(os.path.join(petsc.petsc_dir, 'lib', 'petsc','conf', 'variables'), conf) 253 parse_makefile(petsc.arch_path('lib','petsc','conf', 'petscvariables'), conf) 254 build_ninja = petsc.arch_path('build.ninja') 255 with open(build_ninja, 'w') as fd: 256 fd.write('objdir = obj-ninja\n') 257 fd.write('libdir = lib\n') 258 fd.write('c_compile = %(PCC)s\n' % conf) 259 fd.write('c_flags = %(PETSC_CC_INCLUDES)s %(PCC_FLAGS)s %(CCPPFLAGS)s\n' % conf) 260 fd.write('c_link = %(PCC_LINKER)s\n' % conf) 261 fd.write('c_link_flags = %(PCC_LINKER_FLAGS)s\n' % conf) 262 if petsc.have_fortran: 263 fd.write('f_compile = %(FC)s\n' % conf) 264 fd.write('f_flags = %(PETSC_FC_INCLUDES)s %(FC_FLAGS)s %(FCPPFLAGS)s\n' % conf) 265 fd.write('f_link = %(FC_LINKER)s\n' % conf) 266 fd.write('f_link_flags = %(FC_LINKER_FLAGS)s\n' % conf) 267 fd.write('petsc_external_lib = %(PETSC_EXTERNAL_LIB_BASIC)s\n' % conf) 268 fd.write('python = %(PYTHON)s\n' % conf) 269 fd.write('\n') 270 fd.write('rule C_COMPILE\n' 271 ' command = $c_compile -MMD -MF $out.d $c_flags -c $in -o $out\n' 272 ' description = CC $out\n' 273 ' depfile = $out.d\n' 274 # ' deps = gcc\n') # 'gcc' is default, 'msvc' only recognized by newer versions of ninja 275 '\n') 276 fd.write('rule C_LINK_SHARED\n' 277 ' command = $c_link $c_link_flags -shared -o $out $in $petsc_external_lib\n' 278 ' description = CLINK_SHARED $out\n' 279 '\n') 280 if petsc.have_fortran: 281 fd.write('rule F_COMPILE\n' 282 ' command = $f_compile -MMD -MF $out.d $f_flags -c $in -o $out\n' 283 ' description = FC $out\n' 284 ' depfile = $out.d\n' 285 '\n') 286 fd.write('rule F_LINK_SHARED\n' 287 ' command = $f_link $f_link_flags -shared -o $out $in $petsc_external_lib\n' 288 ' description = FLINK_SHARED $out\n' 289 '\n') 290 fd.write('rule GEN_NINJA\n' 291 ' command = $python $in --output=ninja\n' 292 ' generator = 1\n' 293 '\n') 294 petsc.gen_ninja(fd) 295 fd.write('\n') 296 fd.write('build %s : GEN_NINJA | %s %s %s %s\n' % (build_ninja, 297 os.path.abspath(__file__), 298 os.path.join(petsc.petsc_dir, 'lib','petsc','conf', 'variables'), 299 petsc.arch_path('lib','petsc','conf', 'petscvariables'), 300 ' '.join(os.path.join(petsc.pkg_dir, dep) for dep in petsc.gendeps))) 301 302def main(petsc_dir=None, petsc_arch=None, pkg_dir=None, pkg_name=None, pkg_arch=None, pkg_pkgs=None, output=None, verbose=False): 303 if output is None: 304 output = 'gnumake' 305 writer = dict(gnumake=WriteGnuMake, ninja=WriteNinja) 306 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, verbose=verbose) 307 writer[output](petsc) 308 petsc.summary() 309 310if __name__ == '__main__': 311 import optparse 312 parser = optparse.OptionParser() 313 parser.add_option('--verbose', help='Show mismatches between makefiles and the filesystem', action='store_true', default=False) 314 parser.add_option('--petsc-arch', help='Set PETSC_ARCH different from environment', default=os.environ.get('PETSC_ARCH')) 315 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) 316 parser.add_option('--pkg-name', help='Set the name of the package you want to generate the makefile rules for', default=None) 317 parser.add_option('--pkg-arch', help='Set the package arch name you want to generate the makefile rules for', default=None) 318 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) 319 parser.add_option('--output', help='Location to write output file', default=None) 320 opts, extra_args = parser.parse_args() 321 if extra_args: 322 import sys 323 sys.stderr.write('Unknown arguments: %s\n' % ' '.join(extra_args)) 324 exit(1) 325 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, verbose=opts.verbose) 326