1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305
|
#!/usr/bin/env python
# This file generates $SLEPC_DIR/CMakeLists.txt by parsing the makefiles
# throughout the source tree, reading their constraints and included
# sources, and encoding the rules through CMake conditionals. When CMake
# runs, it will use the conditionals written to
#
# $SLEPC_DIR/$PETSC_ARCH/lib/slepc/conf/SLEPcBuildInternal.cmake
#
# after a successful configure.
#
# The generated CMakeLists.txt is independent of PETSC_ARCH.
#
# This script supports one option:
# --verbose : Show mismatches between makefiles and the filesystem
from __future__ import print_function
import os
from collections import deque
# compatibility code for python-2.4 from http://code.activestate.com/recipes/523034-emulate-collectionsdefaultdict/
try:
from collections import defaultdict
except:
class defaultdict(dict):
def __init__(self, default_factory=None, *a, **kw):
if (default_factory is not None and
not hasattr(default_factory, '__call__')):
raise TypeError('first argument must be callable')
dict.__init__(self, *a, **kw)
self.default_factory = default_factory
def __getitem__(self, key):
try:
return dict.__getitem__(self, key)
except KeyError:
return self.__missing__(key)
def __missing__(self, key):
if self.default_factory is None:
raise KeyError(key)
self[key] = value = self.default_factory()
return value
def __reduce__(self):
if self.default_factory is None:
args = tuple()
else:
args = self.default_factory,
return type(self), args, None, None, self.items()
def copy(self):
return self.__copy__()
def __copy__(self):
return type(self)(self.default_factory, self)
def __deepcopy__(self, memo):
import copy
return type(self)(self.default_factory,
copy.deepcopy(self.items()))
def __repr__(self):
return 'defaultdict(%s, %s)' % (self.default_factory,
dict.__repr__(self))
class StdoutLogger(object):
def write(self,str):
print(str)
def cmakeconditional(key,val):
def unexpected():
raise RuntimeError('Unexpected')
if key in ['package', 'function', 'define']:
return val
if key == 'precision':
if val == 'double':
return 'PETSC_USE_REAL_DOUBLE'
elif val == 'single':
return 'PETSC_USE_REAL_SINGLE'
raise RuntimeError('Unexpected precision: %r'%val)
if key == 'scalar':
if val == 'real':
return 'NOT PETSC_USE_COMPLEX'
if val == 'complex':
return 'PETSC_USE_COMPLEX'
raise RuntimeError('Unexpected scalar: %r'%val)
if key == 'language':
if val == 'CXXONLY':
return 'PETSC_CLANGUAGE_Cxx'
if val == 'CONLY':
return 'PETSC_CLANGUAGE_C'
raise RuntimeError('Unexpected language: %r'%val)
raise RuntimeError('Unhandled case: %r=%r'%(key,val))
AUTODIRS = set('ftn-auto ftn-custom f90-custom'.split()) # Automatically recurse into these, if they exist
SKIPDIRS = set('benchmarks'.split()) # Skip these during the build
NOWARNDIRS = set('tests tutorials'.split()) # Do not warn about mismatch in these
def pathsplit(path):
"""Recursively split a path, returns a tuple"""
stem, basename = os.path.split(path)
if stem == '':
return (basename,)
if stem == path: # fixed point, likely '/'
return (path,)
return pathsplit(stem) + (basename,)
class Mistakes(object):
def __init__(self, log, verbose=False):
self.mistakes = []
self.verbose = verbose
self.log = log
def compareDirLists(self,root, mdirs, dirs):
if NOWARNDIRS.intersection(pathsplit(root)):
return
smdirs = set(mdirs)
sdirs = set(dirs).difference(AUTODIRS)
if not smdirs.issubset(sdirs):
self.mistakes.append('Makefile contains directory not on filesystem: %s: %r' % (root, sorted(smdirs - sdirs)))
if not self.verbose: return
if smdirs != sdirs:
from sys import stderr
stderr.write('Directory mismatch at %s:\n\t%s: %r\n\t%s: %r\n\t%s: %r\n'
% (root,
'in makefile ',sorted(smdirs),
'on filesystem ',sorted(sdirs),
'symmetric diff',sorted(smdirs.symmetric_difference(sdirs))))
def compareSourceLists(self, root, msources, files):
if NOWARNDIRS.intersection(pathsplit(root)):
return
smsources = set(msources)
ssources = set(f for f in files if os.path.splitext(f)[1] in ['.c', '.cxx', '.cc', '.cu', '.cpp', '.F'])
if not smsources.issubset(ssources):
self.mistakes.append('Makefile contains file not on filesystem: %s: %r' % (root, sorted(smsources - ssources)))
if not self.verbose: return
if smsources != ssources:
from sys import stderr
stderr.write('Source mismatch at %s:\n\t%s: %r\n\t%s: %r\n\t%s: %r\n'
% (root,
'in makefile ',sorted(smsources),
'on filesystem ',sorted(ssources),
'symmetric diff',sorted(smsources.symmetric_difference(ssources))))
def summary(self):
for m in self.mistakes:
self.log.write(m + '\n')
if self.mistakes:
raise RuntimeError('SLEPc 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 slepc-maint@upv.es.\n\t3. Someone forgot to "add" new files to the repository. Send mail to slepc-maint@upv.es.' % ('\n'.join(self.mistakes)))
def stripsplit(line):
return line[len('#requires'):].replace("'","").split()
def pkgsources(pkg, mistakes):
'''
Walks the source tree associated with 'pkg', analyzes the conditional written into the makefiles,
and returns a list of sources associated with each unique conditional (as a dictionary).
'''
from distutils.sysconfig import parse_makefile
allconditions = defaultdict(set)
sources = defaultdict(deque)
for root,dirs,files in os.walk(os.path.join('src',pkg)):
conditions = allconditions[os.path.dirname(root)].copy()
makefile = os.path.join(root,'makefile')
if not os.path.exists(makefile):
continue
makevars = parse_makefile(makefile)
mdirs = makevars.get('DIRS','').split() # Directories specified in the makefile
mistakes.compareDirLists(root,mdirs,dirs) # diagnostic output to find unused directories
candidates = set(mdirs).union(AUTODIRS).difference(SKIPDIRS)
dirs[:] = list(candidates.intersection(dirs))
lines = open(makefile)
conditions.update(set(tuple(stripsplit(line)) for line in lines if line.startswith('#requires')))
lines.close()
def relpath(filename):
return os.path.join(root,filename)
sourcecu = makevars.get('SOURCECU','').split()
sourcec = makevars.get('SOURCEC','').split()
sourcecxx = makevars.get('SOURCECXX','').split()
sourcef = makevars.get('SOURCEF','').split()
mistakes.compareSourceLists(root,sourcec+sourcecxx+sourcef+sourcecu, files) # Diagnostic output about unused source files
sources[repr(sorted(conditions))].extend(relpath(f) for f in sourcec + sourcecxx + sourcef + sourcecu)
allconditions[root] = conditions
return sources
def writeRoot(f,petscdir,petscprefixdir):
f.write(r'''cmake_minimum_required (VERSION 2.6.2)
project (SLEPc C)
set (PETSc_SOURCE_DIR %s)
set (PETSc_BINARY_DIR %s)
include (${PETSc_BINARY_DIR}/lib/petsc/conf/PETScBuildInternal.cmake)
include (${PETSC_CMAKE_ARCH}/lib/slepc/conf/SLEPcBuildInternal.cmake)
if (PETSC_HAVE_FORTRAN)
enable_language (Fortran)
endif ()
if (PETSC_CLANGUAGE_Cxx OR PETSC_HAVE_CXX)
enable_language (CXX)
endif ()
if (APPLE)
SET(CMAKE_C_ARCHIVE_FINISH "<CMAKE_RANLIB> -c <TARGET> ")
SET(CMAKE_CXX_ARCHIVE_FINISH "<CMAKE_RANLIB> -c <TARGET> ")
SET(CMAKE_Fortran_ARCHIVE_FINISH "<CMAKE_RANLIB> -c <TARGET> ")
endif ()
if (PETSC_HAVE_CUDA)
find_package (CUDA REQUIRED)
set (CUDA_PROPAGATE_HOST_FLAGS OFF)
set (CUDA_NVCC_FLAGS ${CUDA_NVCC_FLAGS} --compiler-options ${PETSC_CUDA_HOST_FLAGS})
endif ()
include_directories ("${PETSc_SOURCE_DIR}/include" "${PETSc_BINARY_DIR}/include")
include_directories ("${SLEPc_SOURCE_DIR}/include" "${SLEPc_BINARY_DIR}/include")
set (CMAKE_ARCHIVE_OUTPUT_DIRECTORY "${SLEPc_BINARY_DIR}/lib" CACHE PATH "Output directory for SLEPc archives")
set (CMAKE_LIBRARY_OUTPUT_DIRECTORY "${SLEPc_BINARY_DIR}/lib" CACHE PATH "Output directory for SLEPc libraries")
set (CMAKE_Fortran_MODULE_DIRECTORY "${SLEPc_BINARY_DIR}/include" CACHE PATH "Output directory for fortran *.mod files")
mark_as_advanced (CMAKE_ARCHIVE_OUTPUT_DIRECTORY CMAKE_LIBRARY_OUTPUT_DIRECTORY CMAKE_Fortran_MODULE_DIRECTORY)
set (CMAKE_INSTALL_RPATH "${CMAKE_INSTALL_PREFIX}/lib")
set (CMAKE_INSTALL_RPATH_USE_LINK_PATH TRUE)
################### The following describes the build ####################
''' % (petscdir,petscprefixdir))
def writePackage(f,pkg,pkgdeps,mistakes):
for conds, srcs in pkgsources(pkg,mistakes).items():
conds = eval(conds)
def body(indentlevel):
indent = ' '*(indentlevel+2)
lfindent = '\n'+indent
return ' '*indentlevel + 'list (APPEND SLEPC' + pkg.upper() + '_SRCS' + lfindent + lfindent.join(srcs) + lfindent + ')\n'
if conds:
f.write('if (%s)\n%sendif ()\n' % (' AND '.join(cmakeconditional(*c) for c in conds), body(2)))
else:
f.write(body(0))
f.write('''
if (NOT PETSC_USE_SINGLE_LIBRARY)
if (PETSC_HAVE_CUDA)
cuda_add_library (slepc%(pkg)s ${SLEPC%(PKG)s_SRCS})
else ()
add_library (slepc%(pkg)s ${SLEPC%(PKG)s_SRCS})
endif ()
target_link_libraries (slepc%(pkg)s %(pkgdeps)s ${PETSC_LIB} ${SLEPC_PACKAGE_LIBS} ${PETSC_PACKAGE_LIBS})
if (PETSC_WIN32FE)
set_target_properties (slepc%(pkg)s PROPERTIES RULE_LAUNCH_COMPILE "${PETSC_WIN32FE}")
set_target_properties (slepc%(pkg)s PROPERTIES RULE_LAUNCH_LINK "${PETSC_WIN32FE}")
endif ()
endif ()
''' % dict(pkg=pkg, PKG=pkg.upper(), pkgdeps=' '.join('slepc%s'%p for p in pkgdeps)))
def main(slepcdir,petscdir,petscprefixdir,log=StdoutLogger(), verbose=False):
import tempfile, shutil
written = False # We delete the temporary file if it wasn't finished, otherwise rename (atomic)
mistakes = Mistakes(log=log, verbose=verbose)
fd,tmplists = tempfile.mkstemp(prefix='CMakeLists.txt.',dir=slepcdir,text=True)
try:
f = os.fdopen(fd,'w')
writeRoot(f,petscdir,petscprefixdir)
f.write('include_directories (${PETSC_PACKAGE_INCLUDES} ${SLEPC_PACKAGE_INCLUDES})\n')
pkglist = [('sys' , ''),
('eps' , 'sys'),
('svd' , 'eps sys'),
('pep' , 'eps sys'),
('nep' , 'pep eps sys'),
('mfn' , 'sys'),
('lme' , 'sys')]
for pkg,deps in pkglist:
writePackage(f,pkg,deps.split(),mistakes)
f.write ('''
if (PETSC_USE_SINGLE_LIBRARY)
if (PETSC_HAVE_CUDA)
cuda_add_library (slepc %(allsrc)s)
else ()
add_library (slepc %(allsrc)s)
endif ()
target_link_libraries (slepc ${PETSC_LIB} ${SLEPC_PACKAGE_LIBS} ${PETSC_PACKAGE_LIBS})
if (PETSC_WIN32FE)
set_target_properties (slepc PROPERTIES RULE_LAUNCH_COMPILE "${PETSC_WIN32FE}")
set_target_properties (slepc PROPERTIES RULE_LAUNCH_LINK "${PETSC_WIN32FE}")
endif ()
endif ()
''' % dict(allsrc=' '.join([r'${SLEPC' + pkg.upper() + r'_SRCS}' for pkg,deps in pkglist]),))
f.write('''
if (PETSC_CLANGUAGE_Cxx)
foreach (file IN LISTS %s)
if (file MATCHES "^.*\\\\.c$")
set_source_files_properties(${file} PROPERTIES LANGUAGE CXX)
endif ()
endforeach ()
endif()''' % ('\n '.join([r'SLEPC' + pkg.upper() + r'_SRCS' for (pkg,_) in pkglist])))
written = True
finally:
f.close()
if written:
shutil.move(tmplists,os.path.join(slepcdir,'CMakeLists.txt'))
else:
os.remove(tmplists)
mistakes.summary()
if __name__ == "__main__":
import optparse
parser = optparse.OptionParser()
parser.add_option('--verbose', help='Show mismatches between makefiles and the filesystem', dest='verbose', action='store_true', default=False)
(opts, extra_args) = parser.parse_args()
main(slepcdir=os.environ['SLEPC_DIR'],petscdir=os.environ['PETSC_DIR'],petscprefixdir=os.sep.join([os.environ['PETSC_DIR'],os.environ['PETSC_ARCH']]), verbose=opts.verbose)
|