Chris@87: from __future__ import division, absolute_import, print_function Chris@87: Chris@87: import re Chris@87: import os Chris@87: import sys Chris@87: import types Chris@87: from copy import copy Chris@87: Chris@87: from distutils.ccompiler import * Chris@87: from distutils import ccompiler Chris@87: from distutils.errors import DistutilsExecError, DistutilsModuleError, \ Chris@87: DistutilsPlatformError Chris@87: from distutils.sysconfig import customize_compiler Chris@87: from distutils.version import LooseVersion Chris@87: Chris@87: from numpy.distutils import log Chris@87: from numpy.distutils.exec_command import exec_command Chris@87: from numpy.distutils.misc_util import cyg2win32, is_sequence, mingw32, \ Chris@87: quote_args Chris@87: from numpy.distutils.compat import get_exception Chris@87: Chris@87: Chris@87: def replace_method(klass, method_name, func): Chris@87: if sys.version_info[0] < 3: Chris@87: m = types.MethodType(func, None, klass) Chris@87: else: Chris@87: # Py3k does not have unbound method anymore, MethodType does not work Chris@87: m = lambda self, *args, **kw: func(self, *args, **kw) Chris@87: setattr(klass, method_name, m) Chris@87: Chris@87: # Using customized CCompiler.spawn. Chris@87: def CCompiler_spawn(self, cmd, display=None): Chris@87: """ Chris@87: Execute a command in a sub-process. Chris@87: Chris@87: Parameters Chris@87: ---------- Chris@87: cmd : str Chris@87: The command to execute. Chris@87: display : str or sequence of str, optional Chris@87: The text to add to the log file kept by `numpy.distutils`. Chris@87: If not given, `display` is equal to `cmd`. Chris@87: Chris@87: Returns Chris@87: ------- Chris@87: None Chris@87: Chris@87: Raises Chris@87: ------ Chris@87: DistutilsExecError Chris@87: If the command failed, i.e. the exit status was not 0. Chris@87: Chris@87: """ Chris@87: if display is None: Chris@87: display = cmd Chris@87: if is_sequence(display): Chris@87: display = ' '.join(list(display)) Chris@87: log.info(display) Chris@87: s, o = exec_command(cmd) Chris@87: if s: Chris@87: if is_sequence(cmd): Chris@87: cmd = ' '.join(list(cmd)) Chris@87: try: Chris@87: print(o) Chris@87: except UnicodeError: Chris@87: # When installing through pip, `o` can contain non-ascii chars Chris@87: pass Chris@87: if re.search('Too many open files', o): Chris@87: msg = '\nTry rerunning setup command until build succeeds.' Chris@87: else: Chris@87: msg = '' Chris@87: raise DistutilsExecError('Command "%s" failed with exit status %d%s' % (cmd, s, msg)) Chris@87: Chris@87: replace_method(CCompiler, 'spawn', CCompiler_spawn) Chris@87: Chris@87: def CCompiler_object_filenames(self, source_filenames, strip_dir=0, output_dir=''): Chris@87: """ Chris@87: Return the name of the object files for the given source files. Chris@87: Chris@87: Parameters Chris@87: ---------- Chris@87: source_filenames : list of str Chris@87: The list of paths to source files. Paths can be either relative or Chris@87: absolute, this is handled transparently. Chris@87: strip_dir : bool, optional Chris@87: Whether to strip the directory from the returned paths. If True, Chris@87: the file name prepended by `output_dir` is returned. Default is False. Chris@87: output_dir : str, optional Chris@87: If given, this path is prepended to the returned paths to the Chris@87: object files. Chris@87: Chris@87: Returns Chris@87: ------- Chris@87: obj_names : list of str Chris@87: The list of paths to the object files corresponding to the source Chris@87: files in `source_filenames`. Chris@87: Chris@87: """ Chris@87: if output_dir is None: Chris@87: output_dir = '' Chris@87: obj_names = [] Chris@87: for src_name in source_filenames: Chris@87: base, ext = os.path.splitext(os.path.normpath(src_name)) Chris@87: base = os.path.splitdrive(base)[1] # Chop off the drive Chris@87: base = base[os.path.isabs(base):] # If abs, chop off leading / Chris@87: if base.startswith('..'): Chris@87: # Resolve starting relative path components, middle ones Chris@87: # (if any) have been handled by os.path.normpath above. Chris@87: i = base.rfind('..')+2 Chris@87: d = base[:i] Chris@87: d = os.path.basename(os.path.abspath(d)) Chris@87: base = d + base[i:] Chris@87: if ext not in self.src_extensions: Chris@87: raise UnknownFileError("unknown file type '%s' (from '%s')" % (ext, src_name)) Chris@87: if strip_dir: Chris@87: base = os.path.basename(base) Chris@87: obj_name = os.path.join(output_dir, base + self.obj_extension) Chris@87: obj_names.append(obj_name) Chris@87: return obj_names Chris@87: Chris@87: replace_method(CCompiler, 'object_filenames', CCompiler_object_filenames) Chris@87: Chris@87: def CCompiler_compile(self, sources, output_dir=None, macros=None, Chris@87: include_dirs=None, debug=0, extra_preargs=None, Chris@87: extra_postargs=None, depends=None): Chris@87: """ Chris@87: Compile one or more source files. Chris@87: Chris@87: Please refer to the Python distutils API reference for more details. Chris@87: Chris@87: Parameters Chris@87: ---------- Chris@87: sources : list of str Chris@87: A list of filenames Chris@87: output_dir : str, optional Chris@87: Path to the output directory. Chris@87: macros : list of tuples Chris@87: A list of macro definitions. Chris@87: include_dirs : list of str, optional Chris@87: The directories to add to the default include file search path for Chris@87: this compilation only. Chris@87: debug : bool, optional Chris@87: Whether or not to output debug symbols in or alongside the object Chris@87: file(s). Chris@87: extra_preargs, extra_postargs : ? Chris@87: Extra pre- and post-arguments. Chris@87: depends : list of str, optional Chris@87: A list of file names that all targets depend on. Chris@87: Chris@87: Returns Chris@87: ------- Chris@87: objects : list of str Chris@87: A list of object file names, one per source file `sources`. Chris@87: Chris@87: Raises Chris@87: ------ Chris@87: CompileError Chris@87: If compilation fails. Chris@87: Chris@87: """ Chris@87: # This method is effective only with Python >=2.3 distutils. Chris@87: # Any changes here should be applied also to fcompiler.compile Chris@87: # method to support pre Python 2.3 distutils. Chris@87: if not sources: Chris@87: return [] Chris@87: # FIXME:RELATIVE_IMPORT Chris@87: if sys.version_info[0] < 3: Chris@87: from .fcompiler import FCompiler Chris@87: else: Chris@87: from numpy.distutils.fcompiler import FCompiler Chris@87: if isinstance(self, FCompiler): Chris@87: display = [] Chris@87: for fc in ['f77', 'f90', 'fix']: Chris@87: fcomp = getattr(self, 'compiler_'+fc) Chris@87: if fcomp is None: Chris@87: continue Chris@87: display.append("Fortran %s compiler: %s" % (fc, ' '.join(fcomp))) Chris@87: display = '\n'.join(display) Chris@87: else: Chris@87: ccomp = self.compiler_so Chris@87: display = "C compiler: %s\n" % (' '.join(ccomp),) Chris@87: log.info(display) Chris@87: macros, objects, extra_postargs, pp_opts, build = \ Chris@87: self._setup_compile(output_dir, macros, include_dirs, sources, Chris@87: depends, extra_postargs) Chris@87: cc_args = self._get_cc_args(pp_opts, debug, extra_preargs) Chris@87: display = "compile options: '%s'" % (' '.join(cc_args)) Chris@87: if extra_postargs: Chris@87: display += "\nextra options: '%s'" % (' '.join(extra_postargs)) Chris@87: log.info(display) Chris@87: Chris@87: # build any sources in same order as they were originally specified Chris@87: # especially important for fortran .f90 files using modules Chris@87: if isinstance(self, FCompiler): Chris@87: objects_to_build = list(build.keys()) Chris@87: for obj in objects: Chris@87: if obj in objects_to_build: Chris@87: src, ext = build[obj] Chris@87: if self.compiler_type=='absoft': Chris@87: obj = cyg2win32(obj) Chris@87: src = cyg2win32(src) Chris@87: self._compile(obj, src, ext, cc_args, extra_postargs, pp_opts) Chris@87: else: Chris@87: for obj, (src, ext) in build.items(): Chris@87: self._compile(obj, src, ext, cc_args, extra_postargs, pp_opts) Chris@87: Chris@87: # Return *all* object filenames, not just the ones we just built. Chris@87: return objects Chris@87: Chris@87: replace_method(CCompiler, 'compile', CCompiler_compile) Chris@87: Chris@87: def CCompiler_customize_cmd(self, cmd, ignore=()): Chris@87: """ Chris@87: Customize compiler using distutils command. Chris@87: Chris@87: Parameters Chris@87: ---------- Chris@87: cmd : class instance Chris@87: An instance inheriting from `distutils.cmd.Command`. Chris@87: ignore : sequence of str, optional Chris@87: List of `CCompiler` commands (without ``'set_'``) that should not be Chris@87: altered. Strings that are checked for are: Chris@87: ``('include_dirs', 'define', 'undef', 'libraries', 'library_dirs', Chris@87: 'rpath', 'link_objects')``. Chris@87: Chris@87: Returns Chris@87: ------- Chris@87: None Chris@87: Chris@87: """ Chris@87: log.info('customize %s using %s' % (self.__class__.__name__, Chris@87: cmd.__class__.__name__)) Chris@87: def allow(attr): Chris@87: return getattr(cmd, attr, None) is not None and attr not in ignore Chris@87: Chris@87: if allow('include_dirs'): Chris@87: self.set_include_dirs(cmd.include_dirs) Chris@87: if allow('define'): Chris@87: for (name, value) in cmd.define: Chris@87: self.define_macro(name, value) Chris@87: if allow('undef'): Chris@87: for macro in cmd.undef: Chris@87: self.undefine_macro(macro) Chris@87: if allow('libraries'): Chris@87: self.set_libraries(self.libraries + cmd.libraries) Chris@87: if allow('library_dirs'): Chris@87: self.set_library_dirs(self.library_dirs + cmd.library_dirs) Chris@87: if allow('rpath'): Chris@87: self.set_runtime_library_dirs(cmd.rpath) Chris@87: if allow('link_objects'): Chris@87: self.set_link_objects(cmd.link_objects) Chris@87: Chris@87: replace_method(CCompiler, 'customize_cmd', CCompiler_customize_cmd) Chris@87: Chris@87: def _compiler_to_string(compiler): Chris@87: props = [] Chris@87: mx = 0 Chris@87: keys = list(compiler.executables.keys()) Chris@87: for key in ['version', 'libraries', 'library_dirs', Chris@87: 'object_switch', 'compile_switch', Chris@87: 'include_dirs', 'define', 'undef', 'rpath', 'link_objects']: Chris@87: if key not in keys: Chris@87: keys.append(key) Chris@87: for key in keys: Chris@87: if hasattr(compiler, key): Chris@87: v = getattr(compiler, key) Chris@87: mx = max(mx, len(key)) Chris@87: props.append((key, repr(v))) Chris@87: lines = [] Chris@87: format = '%-' + repr(mx+1) + 's = %s' Chris@87: for prop in props: Chris@87: lines.append(format % prop) Chris@87: return '\n'.join(lines) Chris@87: Chris@87: def CCompiler_show_customization(self): Chris@87: """ Chris@87: Print the compiler customizations to stdout. Chris@87: Chris@87: Parameters Chris@87: ---------- Chris@87: None Chris@87: Chris@87: Returns Chris@87: ------- Chris@87: None Chris@87: Chris@87: Notes Chris@87: ----- Chris@87: Printing is only done if the distutils log threshold is < 2. Chris@87: Chris@87: """ Chris@87: if 0: Chris@87: for attrname in ['include_dirs', 'define', 'undef', Chris@87: 'libraries', 'library_dirs', Chris@87: 'rpath', 'link_objects']: Chris@87: attr = getattr(self, attrname, None) Chris@87: if not attr: Chris@87: continue Chris@87: log.info("compiler '%s' is set to %s" % (attrname, attr)) Chris@87: try: Chris@87: self.get_version() Chris@87: except: Chris@87: pass Chris@87: if log._global_log.threshold<2: Chris@87: print('*'*80) Chris@87: print(self.__class__) Chris@87: print(_compiler_to_string(self)) Chris@87: print('*'*80) Chris@87: Chris@87: replace_method(CCompiler, 'show_customization', CCompiler_show_customization) Chris@87: Chris@87: def CCompiler_customize(self, dist, need_cxx=0): Chris@87: """ Chris@87: Do any platform-specific customization of a compiler instance. Chris@87: Chris@87: This method calls `distutils.sysconfig.customize_compiler` for Chris@87: platform-specific customization, as well as optionally remove a flag Chris@87: to suppress spurious warnings in case C++ code is being compiled. Chris@87: Chris@87: Parameters Chris@87: ---------- Chris@87: dist : object Chris@87: This parameter is not used for anything. Chris@87: need_cxx : bool, optional Chris@87: Whether or not C++ has to be compiled. If so (True), the Chris@87: ``"-Wstrict-prototypes"`` option is removed to prevent spurious Chris@87: warnings. Default is False. Chris@87: Chris@87: Returns Chris@87: ------- Chris@87: None Chris@87: Chris@87: Notes Chris@87: ----- Chris@87: All the default options used by distutils can be extracted with:: Chris@87: Chris@87: from distutils import sysconfig Chris@87: sysconfig.get_config_vars('CC', 'CXX', 'OPT', 'BASECFLAGS', Chris@87: 'CCSHARED', 'LDSHARED', 'SO') Chris@87: Chris@87: """ Chris@87: # See FCompiler.customize for suggested usage. Chris@87: log.info('customize %s' % (self.__class__.__name__)) Chris@87: customize_compiler(self) Chris@87: if need_cxx: Chris@87: # In general, distutils uses -Wstrict-prototypes, but this option is Chris@87: # not valid for C++ code, only for C. Remove it if it's there to Chris@87: # avoid a spurious warning on every compilation. Chris@87: try: Chris@87: self.compiler_so.remove('-Wstrict-prototypes') Chris@87: except (AttributeError, ValueError): Chris@87: pass Chris@87: Chris@87: if hasattr(self, 'compiler') and 'cc' in self.compiler[0]: Chris@87: if not self.compiler_cxx: Chris@87: if self.compiler[0].startswith('gcc'): Chris@87: a, b = 'gcc', 'g++' Chris@87: else: Chris@87: a, b = 'cc', 'c++' Chris@87: self.compiler_cxx = [self.compiler[0].replace(a, b)]\ Chris@87: + self.compiler[1:] Chris@87: else: Chris@87: if hasattr(self, 'compiler'): Chris@87: log.warn("#### %s #######" % (self.compiler,)) Chris@87: log.warn('Missing compiler_cxx fix for '+self.__class__.__name__) Chris@87: return Chris@87: Chris@87: replace_method(CCompiler, 'customize', CCompiler_customize) Chris@87: Chris@87: def simple_version_match(pat=r'[-.\d]+', ignore='', start=''): Chris@87: """ Chris@87: Simple matching of version numbers, for use in CCompiler and FCompiler. Chris@87: Chris@87: Parameters Chris@87: ---------- Chris@87: pat : str, optional Chris@87: A regular expression matching version numbers. Chris@87: Default is ``r'[-.\\d]+'``. Chris@87: ignore : str, optional Chris@87: A regular expression matching patterns to skip. Chris@87: Default is ``''``, in which case nothing is skipped. Chris@87: start : str, optional Chris@87: A regular expression matching the start of where to start looking Chris@87: for version numbers. Chris@87: Default is ``''``, in which case searching is started at the Chris@87: beginning of the version string given to `matcher`. Chris@87: Chris@87: Returns Chris@87: ------- Chris@87: matcher : callable Chris@87: A function that is appropriate to use as the ``.version_match`` Chris@87: attribute of a `CCompiler` class. `matcher` takes a single parameter, Chris@87: a version string. Chris@87: Chris@87: """ Chris@87: def matcher(self, version_string): Chris@87: # version string may appear in the second line, so getting rid Chris@87: # of new lines: Chris@87: version_string = version_string.replace('\n', ' ') Chris@87: pos = 0 Chris@87: if start: Chris@87: m = re.match(start, version_string) Chris@87: if not m: Chris@87: return None Chris@87: pos = m.end() Chris@87: while True: Chris@87: m = re.search(pat, version_string[pos:]) Chris@87: if not m: Chris@87: return None Chris@87: if ignore and re.match(ignore, m.group(0)): Chris@87: pos = m.end() Chris@87: continue Chris@87: break Chris@87: return m.group(0) Chris@87: return matcher Chris@87: Chris@87: def CCompiler_get_version(self, force=False, ok_status=[0]): Chris@87: """ Chris@87: Return compiler version, or None if compiler is not available. Chris@87: Chris@87: Parameters Chris@87: ---------- Chris@87: force : bool, optional Chris@87: If True, force a new determination of the version, even if the Chris@87: compiler already has a version attribute. Default is False. Chris@87: ok_status : list of int, optional Chris@87: The list of status values returned by the version look-up process Chris@87: for which a version string is returned. If the status value is not Chris@87: in `ok_status`, None is returned. Default is ``[0]``. Chris@87: Chris@87: Returns Chris@87: ------- Chris@87: version : str or None Chris@87: Version string, in the format of `distutils.version.LooseVersion`. Chris@87: Chris@87: """ Chris@87: if not force and hasattr(self, 'version'): Chris@87: return self.version Chris@87: self.find_executables() Chris@87: try: Chris@87: version_cmd = self.version_cmd Chris@87: except AttributeError: Chris@87: return None Chris@87: if not version_cmd or not version_cmd[0]: Chris@87: return None Chris@87: try: Chris@87: matcher = self.version_match Chris@87: except AttributeError: Chris@87: try: Chris@87: pat = self.version_pattern Chris@87: except AttributeError: Chris@87: return None Chris@87: def matcher(version_string): Chris@87: m = re.match(pat, version_string) Chris@87: if not m: Chris@87: return None Chris@87: version = m.group('version') Chris@87: return version Chris@87: Chris@87: status, output = exec_command(version_cmd, use_tee=0) Chris@87: Chris@87: version = None Chris@87: if status in ok_status: Chris@87: version = matcher(output) Chris@87: if version: Chris@87: version = LooseVersion(version) Chris@87: self.version = version Chris@87: return version Chris@87: Chris@87: replace_method(CCompiler, 'get_version', CCompiler_get_version) Chris@87: Chris@87: def CCompiler_cxx_compiler(self): Chris@87: """ Chris@87: Return the C++ compiler. Chris@87: Chris@87: Parameters Chris@87: ---------- Chris@87: None Chris@87: Chris@87: Returns Chris@87: ------- Chris@87: cxx : class instance Chris@87: The C++ compiler, as a `CCompiler` instance. Chris@87: Chris@87: """ Chris@87: if self.compiler_type=='msvc': return self Chris@87: cxx = copy(self) Chris@87: cxx.compiler_so = [cxx.compiler_cxx[0]] + cxx.compiler_so[1:] Chris@87: if sys.platform.startswith('aix') and 'ld_so_aix' in cxx.linker_so[0]: Chris@87: # AIX needs the ld_so_aix script included with Python Chris@87: cxx.linker_so = [cxx.linker_so[0], cxx.compiler_cxx[0]] \ Chris@87: + cxx.linker_so[2:] Chris@87: else: Chris@87: cxx.linker_so = [cxx.compiler_cxx[0]] + cxx.linker_so[1:] Chris@87: return cxx Chris@87: Chris@87: replace_method(CCompiler, 'cxx_compiler', CCompiler_cxx_compiler) Chris@87: Chris@87: compiler_class['intel'] = ('intelccompiler', 'IntelCCompiler', Chris@87: "Intel C Compiler for 32-bit applications") Chris@87: compiler_class['intele'] = ('intelccompiler', 'IntelItaniumCCompiler', Chris@87: "Intel C Itanium Compiler for Itanium-based applications") Chris@87: compiler_class['intelem'] = ('intelccompiler', 'IntelEM64TCCompiler', Chris@87: "Intel C Compiler for 64-bit applications") Chris@87: compiler_class['pathcc'] = ('pathccompiler', 'PathScaleCCompiler', Chris@87: "PathScale Compiler for SiCortex-based applications") Chris@87: ccompiler._default_compilers += (('linux.*', 'intel'), Chris@87: ('linux.*', 'intele'), Chris@87: ('linux.*', 'intelem'), Chris@87: ('linux.*', 'pathcc')) Chris@87: Chris@87: if sys.platform == 'win32': Chris@87: compiler_class['mingw32'] = ('mingw32ccompiler', 'Mingw32CCompiler', Chris@87: "Mingw32 port of GNU C Compiler for Win32"\ Chris@87: "(for MSC built Python)") Chris@87: if mingw32(): Chris@87: # On windows platforms, we want to default to mingw32 (gcc) Chris@87: # because msvc can't build blitz stuff. Chris@87: log.info('Setting mingw32 as default compiler for nt.') Chris@87: ccompiler._default_compilers = (('nt', 'mingw32'),) \ Chris@87: + ccompiler._default_compilers Chris@87: Chris@87: Chris@87: _distutils_new_compiler = new_compiler Chris@87: def new_compiler (plat=None, Chris@87: compiler=None, Chris@87: verbose=0, Chris@87: dry_run=0, Chris@87: force=0): Chris@87: # Try first C compilers from numpy.distutils. Chris@87: if plat is None: Chris@87: plat = os.name Chris@87: try: Chris@87: if compiler is None: Chris@87: compiler = get_default_compiler(plat) Chris@87: (module_name, class_name, long_description) = compiler_class[compiler] Chris@87: except KeyError: Chris@87: msg = "don't know how to compile C/C++ code on platform '%s'" % plat Chris@87: if compiler is not None: Chris@87: msg = msg + " with '%s' compiler" % compiler Chris@87: raise DistutilsPlatformError(msg) Chris@87: module_name = "numpy.distutils." + module_name Chris@87: try: Chris@87: __import__ (module_name) Chris@87: except ImportError: Chris@87: msg = str(get_exception()) Chris@87: log.info('%s in numpy.distutils; trying from distutils', Chris@87: str(msg)) Chris@87: module_name = module_name[6:] Chris@87: try: Chris@87: __import__(module_name) Chris@87: except ImportError: Chris@87: msg = str(get_exception()) Chris@87: raise DistutilsModuleError("can't compile C/C++ code: unable to load module '%s'" % \ Chris@87: module_name) Chris@87: try: Chris@87: module = sys.modules[module_name] Chris@87: klass = vars(module)[class_name] Chris@87: except KeyError: Chris@87: raise DistutilsModuleError(("can't compile C/C++ code: unable to find class '%s' " + Chris@87: "in module '%s'") % (class_name, module_name)) Chris@87: compiler = klass(None, dry_run, force) Chris@87: log.debug('new_compiler returns %s' % (klass)) Chris@87: return compiler Chris@87: Chris@87: ccompiler.new_compiler = new_compiler Chris@87: Chris@87: _distutils_gen_lib_options = gen_lib_options Chris@87: def gen_lib_options(compiler, library_dirs, runtime_library_dirs, libraries): Chris@87: library_dirs = quote_args(library_dirs) Chris@87: runtime_library_dirs = quote_args(runtime_library_dirs) Chris@87: r = _distutils_gen_lib_options(compiler, library_dirs, Chris@87: runtime_library_dirs, libraries) Chris@87: lib_opts = [] Chris@87: for i in r: Chris@87: if is_sequence(i): Chris@87: lib_opts.extend(list(i)) Chris@87: else: Chris@87: lib_opts.append(i) Chris@87: return lib_opts Chris@87: ccompiler.gen_lib_options = gen_lib_options Chris@87: Chris@87: # Also fix up the various compiler modules, which do Chris@87: # from distutils.ccompiler import gen_lib_options Chris@87: # Don't bother with mwerks, as we don't support Classic Mac. Chris@87: for _cc in ['msvc', 'bcpp', 'cygwinc', 'emxc', 'unixc']: Chris@87: _m = sys.modules.get('distutils.'+_cc+'compiler') Chris@87: if _m is not None: Chris@87: setattr(_m, 'gen_lib_options', gen_lib_options) Chris@87: Chris@87: _distutils_gen_preprocess_options = gen_preprocess_options Chris@87: def gen_preprocess_options (macros, include_dirs): Chris@87: include_dirs = quote_args(include_dirs) Chris@87: return _distutils_gen_preprocess_options(macros, include_dirs) Chris@87: ccompiler.gen_preprocess_options = gen_preprocess_options Chris@87: Chris@87: ##Fix distutils.util.split_quoted: Chris@87: # NOTE: I removed this fix in revision 4481 (see ticket #619), but it appears Chris@87: # that removing this fix causes f2py problems on Windows XP (see ticket #723). Chris@87: # Specifically, on WinXP when gfortran is installed in a directory path, which Chris@87: # contains spaces, then f2py is unable to find it. Chris@87: import re Chris@87: import string Chris@87: _wordchars_re = re.compile(r'[^\\\'\"%s ]*' % string.whitespace) Chris@87: _squote_re = re.compile(r"'(?:[^'\\]|\\.)*'") Chris@87: _dquote_re = re.compile(r'"(?:[^"\\]|\\.)*"') Chris@87: _has_white_re = re.compile(r'\s') Chris@87: def split_quoted(s): Chris@87: s = s.strip() Chris@87: words = [] Chris@87: pos = 0 Chris@87: Chris@87: while s: Chris@87: m = _wordchars_re.match(s, pos) Chris@87: end = m.end() Chris@87: if end == len(s): Chris@87: words.append(s[:end]) Chris@87: break Chris@87: Chris@87: if s[end] in string.whitespace: # unescaped, unquoted whitespace: now Chris@87: words.append(s[:end]) # we definitely have a word delimiter Chris@87: s = s[end:].lstrip() Chris@87: pos = 0 Chris@87: Chris@87: elif s[end] == '\\': # preserve whatever is being escaped; Chris@87: # will become part of the current word Chris@87: s = s[:end] + s[end+1:] Chris@87: pos = end+1 Chris@87: Chris@87: else: Chris@87: if s[end] == "'": # slurp singly-quoted string Chris@87: m = _squote_re.match(s, end) Chris@87: elif s[end] == '"': # slurp doubly-quoted string Chris@87: m = _dquote_re.match(s, end) Chris@87: else: Chris@87: raise RuntimeError("this can't happen (bad char '%c')" % s[end]) Chris@87: Chris@87: if m is None: Chris@87: raise ValueError("bad string (mismatched %s quotes?)" % s[end]) Chris@87: Chris@87: (beg, end) = m.span() Chris@87: if _has_white_re.search(s[beg+1:end-1]): Chris@87: s = s[:beg] + s[beg+1:end-1] + s[end:] Chris@87: pos = m.end() - 2 Chris@87: else: Chris@87: # Keeping quotes when a quoted word does not contain Chris@87: # white-space. XXX: send a patch to distutils Chris@87: pos = m.end() Chris@87: Chris@87: if pos >= len(s): Chris@87: words.append(s) Chris@87: break Chris@87: Chris@87: return words Chris@87: ccompiler.split_quoted = split_quoted Chris@87: ##Fix distutils.util.split_quoted: