1# -------------------------------------------------------------------- 2 3import re 4import os 5import sys 6import glob 7import copy 8import warnings 9 10try: 11 from cStringIO import StringIO 12except ImportError: 13 from io import StringIO 14 15try: 16 import setuptools 17except ImportError: 18 setuptools = None 19 20if setuptools: 21 from setuptools import setup as _setup 22 from setuptools import Extension as _Extension 23 from setuptools import Command 24else: 25 from distutils.core import setup as _setup 26 from distutils.core import Extension as _Extension 27 from distutils.core import Command 28 29def import_command(cmd): 30 try: 31 from importlib import import_module 32 except ImportError: 33 def import_module(n): 34 return __import__(n, fromlist=[None]) 35 try: 36 if not setuptools: raise ImportError 37 mod = import_module('setuptools.command.' + cmd) 38 return getattr(mod, cmd) 39 except ImportError: 40 mod = import_module('distutils.command.' + cmd) 41 return getattr(mod, cmd) 42 43_config = import_command('config') 44_build = import_command('build') 45_build_ext = import_command('build_ext') 46_install = import_command('install') 47 48from distutils import log 49from distutils import sysconfig 50from distutils.util import execute 51from distutils.util import split_quoted 52from distutils.errors import DistutilsError 53 54try: 55 from setuptools import modified 56except ImportError: 57 try: 58 from setuptools import dep_util as modified 59 except ImportError: 60 from distutils import dep_util as modified 61 62try: 63 from packaging.version import Version 64except ImportError: 65 try: 66 from setuptools.extern.packaging.version import Version 67 except ImportError: 68 from distutils.version import StrictVersion as Version 69 70# -------------------------------------------------------------------- 71 72# Cython 73 74CYTHON = '3.0.0' 75 76def cython_req(): 77 return CYTHON 78 79def cython_chk(VERSION, verbose=True): 80 # 81 def warn(message): 82 if not verbose: return 83 ruler, ws, nl = "*"*80, " " ,"\n" 84 pyexe = sys.executable 85 advise = "$ %s -m pip install --upgrade cython" % pyexe 86 def printer(*s): sys.stderr.write(" ".join(s)+"\n") 87 printer(ruler, nl) 88 printer(ws, message, nl) 89 printer(ws, ws, advise, nl) 90 printer(ruler) 91 # 92 try: 93 import Cython 94 except ImportError: 95 warn("You need Cython to generate C source files.") 96 return False 97 # 98 CYTHON_VERSION = Cython.__version__ 99 m = re.match(r"(\d+\.\d+(?:\.\d+)?).*", CYTHON_VERSION) 100 if not m: 101 warn("Cannot parse Cython version string {0!r}" 102 .format(CYTHON_VERSION)) 103 return False 104 REQUIRED = Version(VERSION) 105 PROVIDED = Version(m.groups()[0]) 106 if PROVIDED < REQUIRED: 107 warn("You need Cython >= {0} (you have version {1})" 108 .format(VERSION, CYTHON_VERSION)) 109 return False 110 # 111 if verbose: 112 log.info("using Cython %s" % CYTHON_VERSION) 113 return True 114 115def cython_run( 116 source, target=None, 117 depends=(), includes=(), 118 workdir=None, force=False, 119 VERSION="0.0", 120): 121 if target is None: 122 target = os.path.splitext(source)[0]+'.c' 123 cwd = os.getcwd() 124 try: 125 if workdir: 126 os.chdir(workdir) 127 alldeps = [source] 128 for dep in depends: 129 alldeps += glob.glob(dep) 130 if not (force or modified.newer_group(alldeps, target)): 131 log.debug("skipping '%s' -> '%s' (up-to-date)", 132 source, target) 133 return 134 finally: 135 os.chdir(cwd) 136 require = 'Cython >= %s' % VERSION 137 if setuptools and not cython_chk(VERSION, verbose=False): 138 if sys.modules.get('Cython'): 139 removed = getattr(sys.modules['Cython'], '__version__', '') 140 log.info("removing Cython %s from sys.modules" % removed) 141 pkgname = re.compile(r'cython(\.|$)', re.IGNORECASE) 142 for modname in list(sys.modules.keys()): 143 if pkgname.match(modname): 144 del sys.modules[modname] 145 try: 146 install_setup_requires = setuptools._install_setup_requires 147 with warnings.catch_warnings(): 148 if hasattr(setuptools, 'SetuptoolsDeprecationWarning'): 149 category = setuptools.SetuptoolsDeprecationWarning 150 warnings.simplefilter('ignore', category) 151 log.info("fetching build requirement '%s'" % require) 152 install_setup_requires(dict(setup_requires=[require])) 153 except Exception: 154 log.info("failed to fetch build requirement '%s'" % require) 155 if not cython_chk(VERSION): 156 raise DistutilsError("unsatisfied build requirement '%s'" % require) 157 # 158 log.info("cythonizing '%s' -> '%s'", source, target) 159 from cythonize import cythonize 160 args = [] 161 if workdir: 162 args += ['--working', workdir] 163 args += [source] 164 if target: 165 args += ['--output-file', target] 166 err = cythonize(args) 167 if err: 168 raise DistutilsError( 169 "Cython failure: '%s' -> '%s'" % (source, target) 170 ) 171 172 173# -------------------------------------------------------------------- 174 175def fix_config_vars(names, values): 176 values = list(values) 177 if 'CONDA_BUILD' in os.environ: 178 return values 179 if sys.platform == 'darwin': 180 if 'ARCHFLAGS' in os.environ: 181 ARCHFLAGS = os.environ['ARCHFLAGS'] 182 for i, flag in enumerate(list(values)): 183 flag, count = re.subn(r'-arch\s+\w+', ' ', str(flag)) 184 if count and ARCHFLAGS: 185 flag = flag + ' ' + ARCHFLAGS 186 values[i] = flag 187 if 'SDKROOT' in os.environ: 188 SDKROOT = os.environ['SDKROOT'] 189 for i, flag in enumerate(list(values)): 190 flag, count = re.subn(r'-isysroot [^ \t]*', ' ', str(flag)) 191 if count and SDKROOT: 192 flag = flag + ' ' + '-isysroot ' + SDKROOT 193 values[i] = flag 194 return values 195 196def get_config_vars(*names): 197 # Core Python configuration 198 values = sysconfig.get_config_vars(*names) 199 # Do any distutils flags fixup right now 200 values = fix_config_vars(names, values) 201 return values 202 203from distutils.unixccompiler import UnixCCompiler 204rpath_option_orig = UnixCCompiler.runtime_library_dir_option 205def rpath_option(compiler, dir): 206 option = rpath_option_orig(compiler, dir) 207 if sys.platform[:5] == 'linux': 208 if option.startswith('-R'): 209 option = option.replace('-R', '-Wl,-rpath,', 1) 210 elif option.startswith('-Wl,-R'): 211 option = option.replace('-Wl,-R', '-Wl,-rpath,', 1) 212 return option 213UnixCCompiler.runtime_library_dir_option = rpath_option 214 215# -------------------------------------------------------------------- 216 217class PetscConfig: 218 219 def __init__(self, petsc_dir, petsc_arch, dest_dir=None): 220 if dest_dir is None: 221 dest_dir = os.environ.get('DESTDIR') 222 self.configdict = { } 223 if not petsc_dir: 224 raise DistutilsError("PETSc not found") 225 if not os.path.isdir(petsc_dir): 226 raise DistutilsError("invalid PETSC_DIR: %s" % petsc_dir) 227 self.version = self._get_petsc_version(petsc_dir) 228 self.configdict = self._get_petsc_config(petsc_dir, petsc_arch) 229 self.PETSC_DIR = self['PETSC_DIR'] 230 self.PETSC_ARCH = self['PETSC_ARCH'] 231 self.DESTDIR = dest_dir 232 language_map = {'CONLY':'c', 'CXXONLY':'c++'} 233 self.language = language_map[self['PETSC_LANGUAGE']] 234 235 def __getitem__(self, item): 236 return self.configdict[item] 237 238 def get(self, item, default=None): 239 return self.configdict.get(item, default) 240 241 def configure(self, extension, compiler=None): 242 self.configure_extension(extension) 243 if compiler is not None: 244 self.configure_compiler(compiler) 245 246 def _get_petsc_version(self, petsc_dir): 247 import re 248 version_re = { 249 'major' : re.compile(r"#define\s+PETSC_VERSION_MAJOR\s+(\d+)"), 250 'minor' : re.compile(r"#define\s+PETSC_VERSION_MINOR\s+(\d+)"), 251 'micro' : re.compile(r"#define\s+PETSC_VERSION_SUBMINOR\s+(\d+)"), 252 'release': re.compile(r"#define\s+PETSC_VERSION_RELEASE\s+(-*\d+)"), 253 } 254 petscversion_h = os.path.join(petsc_dir, 'include', 'petscversion.h') 255 with open(petscversion_h, 'rt') as f: data = f.read() 256 major = int(version_re['major'].search(data).groups()[0]) 257 minor = int(version_re['minor'].search(data).groups()[0]) 258 micro = int(version_re['micro'].search(data).groups()[0]) 259 release = int(version_re['release'].search(data).groups()[0]) 260 return (major, minor, micro), (release == 1) 261 262 def _get_petsc_config(self, petsc_dir, petsc_arch): 263 from os.path import join, isdir, exists 264 PETSC_DIR = petsc_dir 265 PETSC_ARCH = petsc_arch 266 # 267 confdir = join('lib', 'petsc', 'conf') 268 if not (PETSC_ARCH and isdir(join(PETSC_DIR, PETSC_ARCH))): 269 petscvars = join(PETSC_DIR, confdir, 'petscvariables') 270 PETSC_ARCH = makefile(open(petscvars, 'rt')).get('PETSC_ARCH') 271 if not (PETSC_ARCH and isdir(join(PETSC_DIR, PETSC_ARCH))): 272 PETSC_ARCH = '' 273 # 274 variables = join(PETSC_DIR, confdir, 'variables') 275 if not exists(variables): 276 variables = join(PETSC_DIR, PETSC_ARCH, confdir, 'variables') 277 petscvariables = join(PETSC_DIR, PETSC_ARCH, confdir, 'petscvariables') 278 # 279 with open(variables) as f: 280 contents = f.read() 281 with open(petscvariables) as f: 282 contents += f.read() 283 # 284 confstr = 'PETSC_DIR = %s\n' % PETSC_DIR 285 confstr += 'PETSC_ARCH = %s\n' % PETSC_ARCH 286 confstr += contents 287 confdict = makefile(StringIO(confstr)) 288 return confdict 289 290 def _configure_ext(self, ext, dct, append=False): 291 extdict = ext.__dict__ 292 for key, values in dct.items(): 293 if key in extdict: 294 for value in values: 295 if value not in extdict[key]: 296 if not append: 297 extdict[key].insert(0, value) 298 else: 299 extdict[key].append(value) 300 301 def configure_extension(self, extension): 302 # includes and libraries 303 # paths in PETSc config files point to final installation location, but 304 # we might be building against PETSc in staging location (DESTDIR) when 305 # DESTDIR is set, so append DESTDIR (if nonempty) to those paths 306 petsc_inc = flaglist(prepend_to_flags(self.DESTDIR, self['PETSC_CC_INCLUDES'])) 307 lib_flags = prepend_to_flags(self.DESTDIR, '-L%s %s' % \ 308 (self['PETSC_LIB_DIR'], self['PETSC_LIB_BASIC'])) 309 petsc_lib = flaglist(lib_flags) 310 # runtime_library_dirs is not supported on Windows 311 if sys.platform != 'win32': 312 # if DESTDIR is set, then we're building against PETSc in a staging 313 # directory, but rpath needs to point to final install directory. 314 rpath = [strip_prefix(self.DESTDIR, self['PETSC_LIB_DIR'])] 315 if sys.modules.get('petsc') is not None: 316 if sys.platform == 'darwin': 317 rpath = ['@loader_path/../../petsc/lib'] 318 else: 319 rpath = ['$ORIGIN/../../petsc/lib'] 320 petsc_lib['runtime_library_dirs'].extend(rpath) 321 # Link in extra libraries on static builds 322 if self['BUILDSHAREDLIB'] != 'yes': 323 petsc_ext_lib = split_quoted(self['PETSC_EXTERNAL_LIB_BASIC']) 324 petsc_lib['extra_link_args'].extend(petsc_ext_lib) 325 self._configure_ext(extension, petsc_inc, append=True) 326 self._configure_ext(extension, petsc_lib) 327 328 def configure_compiler(self, compiler): 329 if compiler.compiler_type != 'unix': return 330 getenv = os.environ.get 331 # distutils C/C++ compiler 332 (cc, cflags, ccshared, cxx) = get_config_vars( 333 'CC', 'CFLAGS', 'CCSHARED', 'CXX') 334 ccshared = getenv('CCSHARED', ccshared or '') 335 cflags = getenv('CFLAGS', cflags or '') 336 cflags = cflags.replace('-Wstrict-prototypes', '') 337 # distutils linker 338 (ldflags, ldshared, so_ext) = get_config_vars( 339 'LDFLAGS', 'LDSHARED', 'SO') 340 ld = cc 341 ldshared = getenv('LDSHARED', ldshared) 342 ldflags = getenv('LDFLAGS', cflags + ' ' + (ldflags or '')) 343 ldcmd = split_quoted(ld) + split_quoted(ldflags) 344 ldshared = [flg for flg in split_quoted(ldshared) if flg not in ldcmd and (flg.find('/lib/spack/env')<0)] 345 ldshared = str.join(' ', ldshared) 346 # 347 def get_flags(cmd): 348 if not cmd: return '' 349 cmd = split_quoted(cmd) 350 if os.path.basename(cmd[0]) == 'xcrun': 351 del cmd[0] 352 while True: 353 if cmd[0] == '-sdk': 354 del cmd[0:2] 355 continue 356 if cmd[0] == '-log': 357 del cmd[0] 358 continue 359 break 360 return ' '.join(cmd[1:]) 361 # PETSc C compiler 362 PCC = self['PCC'] 363 PCC_FLAGS = get_flags(cc) + ' ' + self['PCC_FLAGS'] 364 PCC_FLAGS = PCC_FLAGS.replace('-fvisibility=hidden', '') 365 PCC = getenv('PCC', PCC) + ' ' + getenv('PCCFLAGS', PCC_FLAGS) 366 PCC_SHARED = str.join(' ', (PCC, ccshared, cflags)) 367 # PETSc C++ compiler 368 PCXX = PCC if self.language == 'c++' else self.get('CXX', cxx) 369 # PETSc linker 370 PLD = self['PCC_LINKER'] 371 PLD_FLAGS = get_flags(ld) + ' ' + self['PCC_LINKER_FLAGS'] 372 PLD_FLAGS = PLD_FLAGS.replace('-fvisibility=hidden', '') 373 PLD = getenv('PLD', PLD) + ' ' + getenv('PLDFLAGS', PLD_FLAGS) 374 PLD_SHARED = str.join(' ', (PLD, ldshared, ldflags)) 375 # 376 compiler.set_executables( 377 compiler = PCC, 378 compiler_cxx = PCXX, 379 linker_exe = PLD, 380 compiler_so = PCC_SHARED, 381 linker_so = PLD_SHARED, 382 ) 383 compiler.shared_lib_extension = so_ext 384 385 def log_info(self): 386 PETSC_DIR = self['PETSC_DIR'] 387 PETSC_ARCH = self['PETSC_ARCH'] 388 version = ".".join([str(i) for i in self.version[0]]) 389 release = ("development", "release")[self.version[1]] 390 version_info = version + ' ' + release 391 integer_size = '%s-bit' % self['PETSC_INDEX_SIZE'] 392 scalar_type = self['PETSC_SCALAR'] 393 precision = self['PETSC_PRECISION'] 394 language = self['PETSC_LANGUAGE'] 395 compiler = self['PCC'] 396 linker = self['PCC_LINKER'] 397 log.info('PETSC_DIR: %s' % PETSC_DIR ) 398 log.info('PETSC_ARCH: %s' % PETSC_ARCH ) 399 log.info('version: %s' % version_info) 400 log.info('integer-size: %s' % integer_size) 401 log.info('scalar-type: %s' % scalar_type) 402 log.info('precision: %s' % precision) 403 log.info('language: %s' % language) 404 log.info('compiler: %s' % compiler) 405 log.info('linker: %s' % linker) 406 407# -------------------------------------------------------------------- 408 409class Extension(_Extension): 410 pass 411 412# -------------------------------------------------------------------- 413 414cmd_petsc_opts = [ 415 ('petsc-dir=', None, 416 "define PETSC_DIR, overriding environmental variables"), 417 ('petsc-arch=', None, 418 "define PETSC_ARCH, overriding environmental variables"), 419 ] 420 421 422class config(_config): 423 424 Configure = PetscConfig 425 426 user_options = _config.user_options + cmd_petsc_opts 427 428 def initialize_options(self): 429 _config.initialize_options(self) 430 self.petsc_dir = None 431 self.petsc_arch = None 432 433 def get_config_arch(self, arch): 434 return config.Configure(self.petsc_dir, arch) 435 436 def run(self): 437 _config.run(self) 438 self.petsc_dir = config.get_petsc_dir(self.petsc_dir) 439 if self.petsc_dir is None: return 440 petsc_arch = config.get_petsc_arch(self.petsc_dir, self.petsc_arch) 441 log.info('-' * 70) 442 log.info('PETSC_DIR: %s' % self.petsc_dir) 443 arch_list = petsc_arch 444 if not arch_list : 445 arch_list = [ None ] 446 for arch in arch_list: 447 conf = self.get_config_arch(arch) 448 archname = conf.PETSC_ARCH or conf['PETSC_ARCH'] 449 scalar_type = conf['PETSC_SCALAR'] 450 precision = conf['PETSC_PRECISION'] 451 language = conf['PETSC_LANGUAGE'] 452 compiler = conf['PCC'] 453 linker = conf['PCC_LINKER'] 454 log.info('-'*70) 455 log.info('PETSC_ARCH: %s' % archname) 456 log.info(' * scalar-type: %s' % scalar_type) 457 log.info(' * precision: %s' % precision) 458 log.info(' * language: %s' % language) 459 log.info(' * compiler: %s' % compiler) 460 log.info(' * linker: %s' % linker) 461 log.info('-' * 70) 462 463 #@staticmethod 464 def get_petsc_dir(petsc_dir): 465 if not petsc_dir: return None 466 petsc_dir = os.path.expandvars(petsc_dir) 467 if not petsc_dir or '$PETSC_DIR' in petsc_dir: 468 try: 469 import petsc 470 petsc_dir = petsc.get_petsc_dir() 471 except ImportError: 472 log.warn("PETSC_DIR not specified") 473 return None 474 petsc_dir = os.path.expanduser(petsc_dir) 475 petsc_dir = os.path.abspath(petsc_dir) 476 return config.chk_petsc_dir(petsc_dir) 477 get_petsc_dir = staticmethod(get_petsc_dir) 478 479 #@staticmethod 480 def chk_petsc_dir(petsc_dir): 481 if not os.path.isdir(petsc_dir): 482 log.error('invalid PETSC_DIR: %s (ignored)' % petsc_dir) 483 return None 484 return petsc_dir 485 chk_petsc_dir = staticmethod(chk_petsc_dir) 486 487 #@staticmethod 488 def get_petsc_arch(petsc_dir, petsc_arch): 489 if not petsc_dir: return None 490 petsc_arch = os.path.expandvars(petsc_arch) 491 if (not petsc_arch or '$PETSC_ARCH' in petsc_arch): 492 petsc_arch = '' 493 petsc_conf = os.path.join(petsc_dir, 'lib', 'petsc', 'conf') 494 if os.path.isdir(petsc_conf): 495 petscvariables = os.path.join(petsc_conf, 'petscvariables') 496 if os.path.exists(petscvariables): 497 conf = makefile(open(petscvariables, 'rt')) 498 petsc_arch = conf.get('PETSC_ARCH', '') 499 petsc_arch = petsc_arch.split(os.pathsep) 500 petsc_arch = unique(petsc_arch) 501 petsc_arch = [arch for arch in petsc_arch if arch] 502 return config.chk_petsc_arch(petsc_dir, petsc_arch) 503 get_petsc_arch = staticmethod(get_petsc_arch) 504 505 #@staticmethod 506 def chk_petsc_arch(petsc_dir, petsc_arch): 507 valid_archs = [] 508 for arch in petsc_arch: 509 arch_path = os.path.join(petsc_dir, arch) 510 if os.path.isdir(arch_path): 511 valid_archs.append(arch) 512 else: 513 log.warn("invalid PETSC_ARCH: %s (ignored)" % arch) 514 return valid_archs 515 chk_petsc_arch = staticmethod(chk_petsc_arch) 516 517 518class build(_build): 519 520 user_options = _build.user_options 521 user_options += [( 522 'inplace', 523 'i', 524 "ignore build-lib and put compiled extensions into the source " 525 "directory alongside your pure Python modules", 526 )] 527 user_options += cmd_petsc_opts 528 529 boolean_options = _build.boolean_options 530 boolean_options += ['inplace'] 531 532 def initialize_options(self): 533 _build.initialize_options(self) 534 self.inplace = None 535 self.petsc_dir = None 536 self.petsc_arch = None 537 538 def finalize_options(self): 539 _build.finalize_options(self) 540 if self.inplace is None: 541 self.inplace = False 542 self.set_undefined_options('config', 543 ('petsc_dir', 'petsc_dir'), 544 ('petsc_arch', 'petsc_arch')) 545 self.petsc_dir = config.get_petsc_dir(self.petsc_dir) 546 self.petsc_arch = config.get_petsc_arch(self.petsc_dir, 547 self.petsc_arch) 548 549 sub_commands = \ 550 [('build_src', lambda *args: True)] + \ 551 _build.sub_commands 552 553 554class build_src(Command): 555 description = "build C sources from Cython files" 556 557 user_options = [ 558 ('force', 'f', 559 "forcibly build everything (ignore file timestamps)"), 560 ] 561 562 boolean_options = ['force'] 563 564 def initialize_options(self): 565 self.force = False 566 567 def finalize_options(self): 568 self.set_undefined_options('build', 569 ('force', 'force'), 570 ) 571 572 def run(self): 573 sources = getattr(self, 'sources', []) 574 for source in sources: 575 cython_run( 576 force=self.force, 577 VERSION=cython_req(), 578 **source 579 ) 580 581 582class build_ext(_build_ext): 583 584 user_options = _build_ext.user_options + cmd_petsc_opts 585 586 def initialize_options(self): 587 _build_ext.initialize_options(self) 588 self.inplace = None 589 self.petsc_dir = None 590 self.petsc_arch = None 591 self._outputs = [] 592 593 def finalize_options(self): 594 _build_ext.finalize_options(self) 595 self.set_undefined_options('build', ('inplace', 'inplace')) 596 self.set_undefined_options('build', 597 ('petsc_dir', 'petsc_dir'), 598 ('petsc_arch', 'petsc_arch')) 599 600 def _copy_ext(self, ext): 601 extclass = ext.__class__ 602 fullname = self.get_ext_fullname(ext.name) 603 modpath = str.split(fullname, '.') 604 pkgpath = os.path.join('', *modpath[0:-1]) 605 name = modpath[-1] 606 sources = list(ext.sources) 607 newext = extclass(name, sources) 608 newext.__dict__.update(copy.deepcopy(ext.__dict__)) 609 newext.name = name 610 return pkgpath, newext 611 612 def _build_ext_arch(self, ext, pkgpath, arch): 613 build_temp = self.build_temp 614 build_lib = self.build_lib 615 try: 616 self.build_temp = os.path.join(build_temp, arch) 617 self.build_lib = os.path.join(build_lib, pkgpath, arch) 618 _build_ext.build_extension(self, ext) 619 finally: 620 self.build_temp = build_temp 621 self.build_lib = build_lib 622 623 def get_config_arch(self, arch): 624 return config.Configure(self.petsc_dir, arch) 625 626 def build_extension(self, ext): 627 if not isinstance(ext, Extension): 628 return _build_ext.build_extension(self, ext) 629 petsc_arch = self.petsc_arch 630 if not petsc_arch: 631 petsc_arch = [ None ] 632 for arch in petsc_arch: 633 config = self.get_config_arch(arch) 634 ARCH = arch or config['PETSC_ARCH'] 635 if ARCH not in self.PETSC_ARCH_LIST: 636 self.PETSC_ARCH_LIST.append(ARCH) 637 self.DESTDIR = config.DESTDIR 638 ext.language = config.language 639 config.log_info() 640 pkgpath, newext = self._copy_ext(ext) 641 config.configure(newext, self.compiler) 642 self._build_ext_arch(newext, pkgpath, ARCH) 643 644 def run(self): 645 self.build_sources() 646 _build_ext.run(self) 647 648 def build_sources(self): 649 if 'build_src' in self.distribution.cmdclass: 650 self.run_command('build_src') 651 652 def build_extensions(self, *args, **kargs): 653 self.PETSC_ARCH_LIST = [] 654 _build_ext.build_extensions(self, *args,**kargs) 655 if not self.PETSC_ARCH_LIST: return 656 self.build_configuration(self.PETSC_ARCH_LIST) 657 658 def build_configuration(self, arch_list): 659 # 660 template, variables = self.get_config_data(arch_list) 661 config_data = template % variables 662 # 663 build_lib = self.build_lib 664 dist_name = self.distribution.get_name() 665 config_file = os.path.join(build_lib, dist_name, 'lib', 666 dist_name.replace('4py', '') + '.cfg') 667 # 668 def write_file(filename, data): 669 with open(filename, 'w') as fh: 670 fh.write(config_data) 671 execute(write_file, (config_file, config_data), 672 msg='writing %s' % config_file, 673 verbose=self.verbose, dry_run=self.dry_run) 674 675 def get_config_data(self, arch_list): 676 DESTDIR = self.DESTDIR 677 template = "\n".join([ 678 "PETSC_DIR = %(PETSC_DIR)s", 679 "PETSC_ARCH = %(PETSC_ARCH)s", 680 ]) + "\n" 681 variables = { 682 'PETSC_DIR' : strip_prefix(DESTDIR, self.petsc_dir), 683 'PETSC_ARCH' : os.path.pathsep.join(arch_list), 684 } 685 return template, variables 686 687 def copy_extensions_to_source(self): 688 build_py = self.get_finalized_command('build_py') 689 for ext in self.extensions: 690 inp_file, reg_file = self._get_inplace_equivalent(build_py, ext) 691 692 arch_list = [''] 693 if isinstance(ext, Extension) and self.petsc_arch: 694 arch_list = self.petsc_arch[:] 695 696 file_pairs = [] 697 inp_head, inp_tail = os.path.split(inp_file) 698 reg_head, reg_tail = os.path.split(reg_file) 699 for arch in arch_list: 700 inp_file = os.path.join(inp_head, arch, inp_tail) 701 reg_file = os.path.join(reg_head, arch, reg_tail) 702 file_pairs.append((inp_file, reg_file)) 703 704 for inp_file, reg_file in file_pairs: 705 if os.path.exists(reg_file) or not ext.optional: 706 dest_dir, _ = os.path.split(inp_file) 707 self.mkpath(dest_dir) 708 self.copy_file(reg_file, inp_file, level=self.verbose) 709 710 def get_outputs(self): 711 self.check_extensions_list(self.extensions) 712 outputs = [] 713 for ext in self.extensions: 714 fullname = self.get_ext_fullname(ext.name) 715 filename = self.get_ext_filename(fullname) 716 if isinstance(ext, Extension) and self.petsc_arch: 717 head, tail = os.path.split(filename) 718 for arch in self.petsc_arch: 719 outfile = os.path.join(self.build_lib, head, arch, tail) 720 outputs.append(outfile) 721 else: 722 outfile = os.path.join(self.build_lib, filename) 723 outputs.append(outfile) 724 outputs = list(set(outputs)) 725 return outputs 726 727 728class install(_install): 729 730 def initialize_options(self): 731 with warnings.catch_warnings(): 732 if setuptools: 733 if hasattr(setuptools, 'SetuptoolsDeprecationWarning'): 734 category = setuptools.SetuptoolsDeprecationWarning 735 warnings.simplefilter('ignore', category) 736 _install.initialize_options(self) 737 self.old_and_unmanageable = True 738 739 740cmdclass_list = [ 741 config, 742 build, 743 build_src, 744 build_ext, 745 install, 746] 747 748# -------------------------------------------------------------------- 749 750def setup(**attrs): 751 cmdclass = attrs.setdefault('cmdclass', {}) 752 for cmd in cmdclass_list: 753 cmdclass.setdefault(cmd.__name__, cmd) 754 build_src.sources = attrs.pop('cython_sources', None) 755 use_setup_requires = False # handle Cython requirement ourselves 756 if setuptools and build_src.sources and use_setup_requires: 757 version = cython_req() 758 if not cython_chk(version, verbose=False): 759 reqs = attrs.setdefault('setup_requires', []) 760 reqs += ['Cython>='+version] 761 return _setup(**attrs) 762 763# -------------------------------------------------------------------- 764 765if setuptools: 766 try: 767 from setuptools.command import egg_info as mod_egg_info 768 _FileList = mod_egg_info.FileList 769 class FileList(_FileList): 770 def process_template_line(self, line): 771 level = log.set_threshold(log.ERROR) 772 try: 773 _FileList.process_template_line(self, line) 774 finally: 775 log.set_threshold(level) 776 mod_egg_info.FileList = FileList 777 except (ImportError, AttributeError): 778 pass 779 780# -------------------------------------------------------------------- 781 782def append(seq, item): 783 if item not in seq: 784 seq.append(item) 785 786def append_dict(conf, dct): 787 for key, values in dct.items(): 788 if key in conf: 789 for value in values: 790 if value not in conf[key]: 791 conf[key].append(value) 792def unique(seq): 793 res = [] 794 for item in seq: 795 if item not in res: 796 res.append(item) 797 return res 798 799def flaglist(flags): 800 801 conf = { 802 'define_macros' : [], 803 'undef_macros' : [], 804 'include_dirs' : [], 805 806 'libraries' : [], 807 'library_dirs' : [], 808 'runtime_library_dirs': [], 809 810 'extra_compile_args' : [], 811 'extra_link_args' : [], 812 } 813 814 if type(flags) is str: 815 flags = flags.split() 816 817 switch = '-Wl,' 818 newflags = [] 819 linkopts = [] 820 for f in flags: 821 if f.startswith(switch): 822 if len(f) > 4: 823 append(linkopts, f[4:]) 824 else: 825 append(newflags, f) 826 if linkopts: 827 newflags.append(switch + ','.join(linkopts)) 828 flags = newflags 829 830 append_next_word = None 831 832 for word in flags: 833 834 if append_next_word is not None: 835 append(append_next_word, word) 836 append_next_word = None 837 continue 838 839 switch, value = word[0:2], word[2:] 840 841 if switch == "-I": 842 append(conf['include_dirs'], value) 843 elif switch == "-D": 844 try: 845 idx = value.index("=") 846 macro = (value[:idx], value[idx+1:]) 847 except ValueError: 848 macro = (value, None) 849 append(conf['define_macros'], macro) 850 elif switch == "-U": 851 append(conf['undef_macros'], value) 852 elif switch == "-l": 853 append(conf['libraries'], value) 854 elif switch == "-L": 855 append(conf['library_dirs'], value) 856 elif switch == "-R": 857 append(conf['runtime_library_dirs'], value) 858 elif word.startswith("-Wl"): 859 linkopts = word.split(',') 860 append_dict(conf, flaglist(linkopts[1:])) 861 elif word == "-rpath": 862 append_next_word = conf['runtime_library_dirs'] 863 elif word == "-Xlinker": 864 append_next_word = conf['extra_link_args'] 865 else: 866 #log.warn("unrecognized flag '%s'" % word) 867 pass 868 return conf 869 870def prepend_to_flags(path, flags): 871 """Prepend a path to compiler flags with absolute paths""" 872 if not path: 873 return flags 874 def append_path(m): 875 switch = m.group(1) 876 open_quote = m.group(4) 877 old_path = m.group(5) 878 close_quote = m.group(6) 879 if os.path.isabs(old_path): 880 moded_path = os.path.normpath(path + os.path.sep + old_path) 881 return switch + open_quote + moded_path + close_quote 882 return m.group(0) 883 return re.sub(r'((^|\s+)(-I|-L))(\s*["\']?)(\S+)(["\']?)', 884 append_path, flags) 885 886def strip_prefix(prefix, string): 887 if not prefix: 888 return string 889 return re.sub(r'^' + prefix, '', string) 890 891# -------------------------------------------------------------------- 892 893from distutils.text_file import TextFile 894 895# Regexes needed for parsing Makefile-like syntaxes 896import re as _re 897_variable_rx = _re.compile(r"([a-zA-Z][a-zA-Z0-9_]+)\s*=\s*(.*)") 898_findvar1_rx = _re.compile(r"\$\(([A-Za-z][A-Za-z0-9_]*)\)") 899_findvar2_rx = _re.compile(r"\${([A-Za-z][A-Za-z0-9_]*)}") 900 901def makefile(fileobj, dct=None): 902 """Parse a Makefile-style file. 903 904 A dictionary containing name/value pairs is returned. If an 905 optional dictionary is passed in as the second argument, it is 906 used instead of a new dictionary. 907 """ 908 fp = TextFile(file=fileobj, 909 strip_comments=1, 910 skip_blanks=1, 911 join_lines=1) 912 913 if dct is None: 914 dct = {} 915 done = {} 916 notdone = {} 917 918 while 1: 919 line = fp.readline() 920 if line is None: # eof 921 break 922 m = _variable_rx.match(line) 923 if m: 924 n, v = m.group(1, 2) 925 v = str.strip(v) 926 if "$" in v: 927 notdone[n] = v 928 else: 929 try: v = int(v) 930 except ValueError: pass 931 done[n] = v 932 try: del notdone[n] 933 except KeyError: pass 934 fp.close() 935 936 # do variable interpolation here 937 while notdone: 938 for name in list(notdone.keys()): 939 value = notdone[name] 940 m = _findvar1_rx.search(value) or _findvar2_rx.search(value) 941 if m: 942 n = m.group(1) 943 found = True 944 if n in done: 945 item = str(done[n]) 946 elif n in notdone: 947 # get it on a subsequent round 948 found = False 949 else: 950 done[n] = item = "" 951 if found: 952 after = value[m.end():] 953 value = value[:m.start()] + item + after 954 if "$" in after: 955 notdone[name] = value 956 else: 957 try: value = int(value) 958 except ValueError: 959 done[name] = str.strip(value) 960 else: 961 done[name] = value 962 del notdone[name] 963 else: 964 # bogus variable reference; 965 # just drop it since we can't deal 966 del notdone[name] 967 # save the results in the global dictionary 968 dct.update(done) 969 return dct 970 971# -------------------------------------------------------------------- 972