summaryrefslogtreecommitdiff
path: root/command/build_ext.py
diff options
context:
space:
mode:
Diffstat (limited to 'command/build_ext.py')
-rw-r--r--command/build_ext.py459
1 files changed, 270 insertions, 189 deletions
diff --git a/command/build_ext.py b/command/build_ext.py
index 729f383c6..6febe9124 100644
--- a/command/build_ext.py
+++ b/command/build_ext.py
@@ -2,25 +2,27 @@
"""
import os
-import string
import sys
from glob import glob
from distutils.dep_util import newer_group
from distutils.command.build_ext import build_ext as old_build_ext
-from distutils.errors import DistutilsFileError, DistutilsSetupError
+from distutils.errors import DistutilsFileError, DistutilsSetupError,\
+ DistutilsError
from distutils.file_util import copy_file
from numpy.distutils import log
from numpy.distutils.exec_command import exec_command
from numpy.distutils.system_info import combine_paths
from numpy.distutils.misc_util import filter_sources, has_f_sources, \
- has_cxx_sources, get_ext_source_files, all_strings, \
+ has_cxx_sources, get_ext_source_files, \
get_numpy_include_dirs, is_sequence
+from numpy.distutils.command.config_compiler import show_fortran_compilers
-
-def ext_language(ext):
- return getattr(ext, 'language', 'c')
+try:
+ set
+except NameError:
+ from sets import Set as set
class build_ext (old_build_ext):
@@ -31,6 +33,11 @@ class build_ext (old_build_ext):
"specify the Fortran compiler type"),
]
+ help_options = old_build_ext.help_options + [
+ ('help-fcompiler',None, "list available Fortran compilers",
+ show_fortran_compilers),
+ ]
+
def initialize_options(self):
old_build_ext.initialize_options(self)
self.fcompiler = None
@@ -40,44 +47,6 @@ class build_ext (old_build_ext):
old_build_ext.finalize_options(self)
if incl_dirs is not None:
self.include_dirs.extend(self.distribution.include_dirs or [])
- self.set_undefined_options('config_fc',
- ('fcompiler', 'fcompiler'))
- self._fcompiler = None
-
- def initialize_fcompiler(self, build_clib):
- # Determine if Fortran compiler is needed.
- requiref77 = requiref90 = False
- if build_clib:
- lang = build_clib.languages()
- requiref77 = 'f77' in lang
- requiref90 = 'f90' in lang
- else:
- for ext in self.extensions:
- language = ext_language(ext)
- if language == 'f77':
- requiref77 = True
- elif language == 'f90':
- requiref90 = True
- elif has_f_sources(ext.sources):
- # because we don't know any better, assume F77
- requiref77 = True
-
- if not (requiref77 or requiref90):
- return
-
- if requiref90:
- self.fcompiler.need_f90()
- if requiref77:
- self.fcompiler.need_f77()
-
- fc = self.fcompiler.fortran(requiref90)
- if fc.get_version():
- fc.customize_cmd(self)
- fc.show_customization()
- else:
- self.warn('fcompiler=%s is not available.' % (
- fc.compiler_type,))
- self._fcompiler = fc
def run(self):
if not self.extensions:
@@ -86,10 +55,6 @@ class build_ext (old_build_ext):
# Make sure that extension sources are complete.
self.run_command('build_src')
- # Not including C libraries to the list of
- # extension libraries automatically to prevent
- # bogus linking commands. Extensions must
- # explicitly specify the C libraries that they use.
if self.distribution.has_c_libraries():
self.run_command('build_clib')
build_clib = self.get_finalized_command('build_clib')
@@ -97,26 +62,148 @@ class build_ext (old_build_ext):
else:
build_clib = None
- # Determine if C++ compiler is needed.
- need_cxx_compiler = False
- for ext in self.extensions:
- if has_cxx_sources(ext.sources):
- need_cxx_compiler = True
- break
- if getattr(ext,'language','c') == 'c++':
- need_cxx_compiler = True
- break
+ # Not including C libraries to the list of
+ # extension libraries automatically to prevent
+ # bogus linking commands. Extensions must
+ # explicitly specify the C libraries that they use.
from distutils.ccompiler import new_compiler
- self.compiler = new_compiler(compiler=self.compiler,
+ from numpy.distutils.fcompiler import new_fcompiler
+
+ compiler_type = self.compiler
+ # Initialize C compiler:
+ self.compiler = new_compiler(compiler=compiler_type,
verbose=self.verbose,
dry_run=self.dry_run,
force=self.force)
- self.compiler.customize(self.distribution,need_cxx=need_cxx_compiler)
+ self.compiler.customize(self.distribution)
self.compiler.customize_cmd(self)
self.compiler.show_customization()
- self.initialize_fcompiler(build_clib)
+ # Create mapping of libraries built by build_clib:
+ clibs = {}
+ if build_clib is not None:
+ for libname,build_info in build_clib.libraries or []:
+ if clibs.has_key(libname):
+ log.warn('library %r defined more than once,'\
+ ' overwriting build_info %r with %r.' \
+ % (libname, clibs[libname], build_info))
+ clibs[libname] = build_info
+ # .. and distribution libraries:
+ for libname,build_info in self.distribution.libraries or []:
+ if clibs.has_key(libname):
+ # build_clib libraries have a precedence before distribution ones
+ continue
+ clibs[libname] = build_info
+
+ # Determine if C++/Fortran 77/Fortran 90 compilers are needed.
+ # Update extension libraries, library_dirs, and macros.
+ all_languages = set()
+ for ext in self.extensions:
+ ext_languages = set()
+ c_libs = []
+ c_lib_dirs = []
+ macros = []
+ for libname in ext.libraries:
+ if clibs.has_key(libname):
+ binfo = clibs[libname]
+ c_libs += binfo.get('libraries',[])
+ c_lib_dirs += binfo.get('library_dirs',[])
+ for m in binfo.get('macros',[]):
+ if m not in macros:
+ macros.append(m)
+ for l in clibs.get(libname,{}).get('source_languages',[]):
+ ext_languages.add(l)
+ if c_libs:
+ new_c_libs = ext.libraries + c_libs
+ log.info('updating extension %r libraries from %r to %r'
+ % (ext.name, ext.libraries, new_c_libs))
+ ext.libraries = new_c_libs
+ ext.library_dirs = ext.library_dirs + c_lib_dirs
+ if macros:
+ log.info('extending extension %r defined_macros with %r'
+ % (ext.name, macros))
+ ext.define_macros = ext.define_macros + macros
+
+ # determine extension languages
+ if has_f_sources(ext.sources):
+ ext_languages.add('f77')
+ if has_cxx_sources(ext.sources):
+ ext_languages.add('c++')
+ l = ext.language or self.compiler.detect_language(ext.sources)
+ if l:
+ ext_languages.add(l)
+ # reset language attribute for choosing proper linker
+ if 'c++' in ext_languages:
+ ext_language = 'c++'
+ elif 'f90' in ext_languages:
+ ext_language = 'f90'
+ elif 'f77' in ext_languages:
+ ext_language = 'f77'
+ else:
+ ext_language = 'c' # default
+ if l and l != ext_language and ext.language:
+ log.warn('resetting extension %r language from %r to %r.' %
+ (ext.name,l,ext_language))
+ ext.language = ext_language
+ # global language
+ all_languages.update(ext_languages)
+
+ need_f90_compiler = 'f90' in all_languages
+ need_f77_compiler = 'f77' in all_languages
+ need_cxx_compiler = 'c++' in all_languages
+
+ # Initialize C++ compiler:
+ if need_cxx_compiler:
+ self._cxx_compiler = new_compiler(compiler=compiler_type,
+ verbose=self.verbose,
+ dry_run=self.dry_run,
+ force=self.force)
+ compiler = self._cxx_compiler
+ compiler.customize(self.distribution,need_cxx=need_cxx_compiler)
+ compiler.customize_cmd(self)
+ compiler.show_customization()
+ self._cxx_compiler = compiler.cxx_compiler()
+ else:
+ self._cxx_compiler = None
+
+ # Initialize Fortran 77 compiler:
+ if need_f77_compiler:
+ self._f77_compiler = new_fcompiler(compiler=self.fcompiler,
+ verbose=self.verbose,
+ dry_run=self.dry_run,
+ force=self.force,
+ requiref90=False)
+ fcompiler = self._f77_compiler
+ if fcompiler.get_version():
+ fcompiler.customize(self.distribution)
+ fcompiler.customize_cmd(self)
+ fcompiler.show_customization()
+ else:
+ self.warn('f77_compiler=%s is not available.' %
+ (fcompiler.compiler_type))
+ self._f77_compiler = None
+ else:
+ self._f77_compiler = None
+
+ # Initialize Fortran 90 compiler:
+ if need_f90_compiler:
+ self._f90_compiler = new_fcompiler(compiler=self.fcompiler,
+ verbose=self.verbose,
+ dry_run=self.dry_run,
+ force=self.force,
+ requiref90=True)
+ fcompiler = self._f90_compiler
+ if fcompiler.get_version():
+ fcompiler.customize(self.distribution)
+ fcompiler.customize_cmd(self)
+ fcompiler.show_customization()
+ else:
+ self.warn('f90_compiler=%s is not available.' %
+ (fcompiler.compiler_type))
+ self._f90_compiler = None
+ else:
+ self._f90_compiler = None
# Build extensions
self.build_extensions()
@@ -125,68 +212,13 @@ class build_ext (old_build_ext):
# Do nothing. Swig sources have beed handled in build_src command.
return sources
- def get_fortran_objects(self, ext, f_sources, fmodule_sources,
- macros, include_dirs):
- if not f_sources and not fmodule_sources:
- return None, []
-
- fcompiler = self._fcompiler
-
- extra_postargs = []
- module_dirs = ext.module_dirs[:]
-
- macros = []
-
- if fmodule_sources:
- module_build_dir = os.path.join(
- self.build_temp,os.path.dirname(
- self.get_ext_filename(fullname)))
-
- self.mkpath(module_build_dir)
- if fcompiler.module_dir_switch is None:
- existing_modules = glob('*.mod')
- extra_postargs += fcompiler.module_options(\
- module_dirs,module_build_dir)
-
- f_objects = []
- if fmodule_sources:
- log.info("compiling Fortran 90 module sources")
- f_objects = fcompiler.compile(fmodule_sources,
- output_dir=self.build_temp,
- macros=macros,
- include_dirs=include_dirs,
- debug=self.debug,
- extra_postargs=extra_postargs,
- depends=ext.depends)
-
- if fmodule_sources and fcompiler.module_dir_switch is None:
- for f in glob('*.mod'):
- if f in existing_modules:
- continue
- try:
- self.move_file(f, module_build_dir)
- except DistutilsFileError: # already exists in destination
- os.remove(f)
-
- if f_sources:
- log.info("compiling Fortran sources")
- f_objects += fcompiler.compile(f_sources,
- output_dir=self.build_temp,
- macros=macros,
- include_dirs=include_dirs,
- debug=self.debug,
- extra_postargs=extra_postargs,
- depends=ext.depends)
-
- return fcompiler, f_objects
-
def build_extension(self, ext):
sources = ext.sources
if sources is None or not is_sequence(sources):
- raise DistutilsSetupError, \
- ("in 'ext_modules' option (extension '%s'), " +
- "'sources' must be present and must be " +
- "a list of source filenames") % ext.name
+ raise DistutilsSetupError(
+ ("in 'ext_modules' option (extension '%s'), " +
+ "'sources' must be present and must be " +
+ "a list of source filenames") % ext.name)
sources = list(sources)
if not sources:
@@ -194,10 +226,9 @@ class build_ext (old_build_ext):
fullname = self.get_ext_fullname(ext.name)
if self.inplace:
- modpath = string.split(fullname, '.')
- package = string.join(modpath[0:-1], '.')
+ modpath = fullname.split('.')
+ package = '.'.join(modpath[0:-1])
base = modpath[-1]
-
build_py = self.get_finalized_command('build_py')
package_dir = build_py.get_package_dir(package)
ext_filename = os.path.join(package_dir,
@@ -218,17 +249,11 @@ class build_ext (old_build_ext):
for undef in ext.undef_macros:
macros.append((undef,))
- clib_libraries = []
- clib_library_dirs = []
- if self.distribution.libraries:
- for libname,build_info in self.distribution.libraries:
- if libname in ext.libraries:
- macros.extend(build_info.get('macros',[]))
- clib_libraries.extend(build_info.get('libraries',[]))
- clib_library_dirs.extend(build_info.get('library_dirs',[]))
-
c_sources, cxx_sources, f_sources, fmodule_sources = \
filter_sources(ext.sources)
+
+
+
if self.compiler.compiler_type=='msvc':
if cxx_sources:
# Needed to compile kiva.agg._agg extension.
@@ -238,6 +263,28 @@ class build_ext (old_build_ext):
c_sources += cxx_sources
cxx_sources = []
+ # Set Fortran/C++ compilers for compilation and linking.
+ if ext.language=='f90':
+ fcompiler = self._f90_compiler
+ elif ext.language=='f77':
+ fcompiler = self._f77_compiler
+ else: # in case ext.language is c++, for instance
+ fcompiler = self._f90_compiler or self._f77_compiler
+ cxx_compiler = self._cxx_compiler
+
+ # check for the availability of required compilers
+ if cxx_sources and cxx_compiler is None:
+ raise DistutilsError, "extension %r has C++ sources" \
+ "but no C++ compiler found" % (ext.name)
+ if (f_sources or fmodule_sources) and fcompiler is None:
+ raise DistutilsError, "extension %r has Fortran sources " \
+ "but no Fortran compiler found" % (ext.name)
+ if ext.language in ['f77','f90'] and fcompiler is None:
+ self.warn("extension %r has Fortran libraries " \
+ "but no Fortran linker found, using default linker" % (ext.name))
+ if ext.language=='c++' and cxx_compiler is None:
+ self.warn("extension %r has C++ libraries " \
+ "but no C++ linker found, using default linker" % (ext.name))
kws = {'depends':ext.depends}
output_dir = self.build_temp
@@ -254,11 +301,9 @@ class build_ext (old_build_ext):
debug=self.debug,
extra_postargs=extra_args,
**kws)
+
if cxx_sources:
log.info("compiling C++ sources")
-
- cxx_compiler = self.compiler.cxx_compiler()
-
c_objects += cxx_compiler.compile(cxx_sources,
output_dir=output_dir,
macros=macros,
@@ -267,80 +312,117 @@ class build_ext (old_build_ext):
extra_postargs=extra_args,
**kws)
- fcompiler, f_objects = self.get_fortran_objects(ext,
- f_sources,
- fmodule_sources,
- macros, include_dirs)
+ extra_postargs = []
+ f_objects = []
+ if fmodule_sources:
+ log.info("compiling Fortran 90 module sources")
+ module_dirs = ext.module_dirs[:]
+ module_build_dir = os.path.join(
+ self.build_temp,os.path.dirname(
+ self.get_ext_filename(fullname)))
+
+ self.mkpath(module_build_dir)
+ if fcompiler.module_dir_switch is None:
+ existing_modules = glob('*.mod')
+ extra_postargs += fcompiler.module_options(
+ module_dirs,module_build_dir)
+ f_objects += fcompiler.compile(fmodule_sources,
+ output_dir=self.build_temp,
+ macros=macros,
+ include_dirs=include_dirs,
+ debug=self.debug,
+ extra_postargs=extra_postargs,
+ depends=ext.depends)
+
+ if fcompiler.module_dir_switch is None:
+ for f in glob('*.mod'):
+ if f in existing_modules:
+ continue
+ t = os.path.join(module_build_dir, f)
+ if os.path.abspath(f)==os.path.abspath(t):
+ continue
+ if os.path.isfile(t):
+ os.remove(t)
+ try:
+ self.move_file(f, module_build_dir)
+ except DistutilsFileError:
+ log.warn('failed to move %r to %r' %
+ (f, module_build_dir))
+ if f_sources:
+ log.info("compiling Fortran sources")
+ f_objects += fcompiler.compile(f_sources,
+ output_dir=self.build_temp,
+ macros=macros,
+ include_dirs=include_dirs,
+ debug=self.debug,
+ extra_postargs=extra_postargs,
+ depends=ext.depends)
objects = c_objects + f_objects
if ext.extra_objects:
objects.extend(ext.extra_objects)
extra_args = ext.extra_link_args or []
+ libraries = self.get_libraries(ext)[:]
+ library_dirs = ext.library_dirs[:]
linker = self.compiler.link_shared_object
-
- use_fortran_linker = getattr(ext,'language','c') in ['f77','f90']
- c_libraries = []
- c_library_dirs = []
- if not use_fortran_linker and self.distribution.has_c_libraries():
- build_clib = self.get_finalized_command('build_clib')
- f_libs = []
- for (lib_name, build_info) in build_clib.libraries:
- if has_f_sources(build_info.get('sources',[])):
- f_libs.append(lib_name)
- if lib_name in ext.libraries:
- # XXX: how to determine if c_libraries contain
- # fortran compiled sources?
- c_libraries.extend(build_info.get('libraries',[]))
- c_library_dirs.extend(build_info.get('library_dirs',[]))
- for l in ext.libraries:
- if l in f_libs:
- use_fortran_linker = True
- fcompiler = self.fcompiler.fortran()
- break
-
- if use_fortran_linker and not fcompiler:
- fcompiler = self.fcompiler.fortran()
-
# Always use system linker when using MSVC compiler.
- if self.compiler.compiler_type=='msvc' and use_fortran_linker:
- self._libs_with_msvc_and_fortran(c_libraries, c_library_dirs)
- use_fortran_linker = False
-
- if use_fortran_linker:
- if cxx_sources:
- # XXX: Which linker should be used, Fortran or C++?
- log.warn('mixing Fortran and C++ is untested')
+ if self.compiler.compiler_type=='msvc':
+ # expand libraries with fcompiler libraries as we are
+ # not using fcompiler linker
+ self._libs_with_msvc_and_fortran(fcompiler, libraries, library_dirs)
+ elif ext.language in ['f77','f90'] and fcompiler is not None:
linker = fcompiler.link_shared_object
- language = ext.language or fcompiler.detect_language(f_sources)
- else:
- linker = self.compiler.link_shared_object
- if sys.version[:3]>='2.3':
- language = ext.language or self.compiler.detect_language(sources)
- else:
- language = ext.language
- if cxx_sources:
- linker = self.compiler.cxx_compiler().link_shared_object
+ if ext.language=='c++' and cxx_compiler is not None:
+ linker = cxx_compiler.link_shared_object
if sys.version[:3]>='2.3':
- kws = {'target_lang':language}
+ kws = {'target_lang':ext.language}
else:
kws = {}
linker(objects, ext_filename,
- libraries=self.get_libraries(ext) + c_libraries + clib_libraries,
- library_dirs=ext.library_dirs+c_library_dirs+clib_library_dirs,
+ libraries=libraries,
+ library_dirs=library_dirs,
runtime_library_dirs=ext.runtime_library_dirs,
extra_postargs=extra_args,
export_symbols=self.get_export_symbols(ext),
debug=self.debug,
build_temp=self.build_temp,**kws)
- def _libs_with_msvc_and_fortran(self, c_libraries, c_library_dirs):
+ def _libs_with_msvc_and_fortran(self, fcompiler, c_libraries,
+ c_library_dirs):
+ if fcompiler is None: return
+
+ for libname in c_libraries:
+ if libname.startswith('msvc'): continue
+ fileexists = False
+ for libdir in c_library_dirs or []:
+ libfile = os.path.join(libdir,'%s.lib' % (libname))
+ if os.path.isfile(libfile):
+ fileexists = True
+ break
+ if fileexists: continue
+ # make g77-compiled static libs available to MSVC
+ fileexists = False
+ for libdir in c_library_dirs:
+ libfile = os.path.join(libdir,'lib%s.a' % (libname))
+ if os.path.isfile(libfile):
+ # copy libname.a file to name.lib so that MSVC linker
+ # can find it
+ libfile2 = os.path.join(self.build_temp, libname + '.lib')
+ copy_file(libfile, libfile2)
+ if self.build_temp not in c_library_dirs:
+ c_library_dirs.append(self.build_temp)
+ fileexists = True
+ break
+ if fileexists: continue
+ log.warn('could not find library %r in directories %s'
+ % (libname, c_library_dirs))
+
# Always use system linker when using MSVC compiler.
f_lib_dirs = []
- fcompiler = self.fcompiler.fortran()
for dir in fcompiler.library_dirs:
# correct path when compiling in Cygwin but with normal Win
# Python
@@ -352,17 +434,16 @@ class build_ext (old_build_ext):
c_library_dirs.extend(f_lib_dirs)
# make g77-compiled static libs available to MSVC
- lib_added = False
for lib in fcompiler.libraries:
- if not lib.startswith('msvcr'):
+ if not lib.startswith('msvc'):
c_libraries.append(lib)
p = combine_paths(f_lib_dirs, 'lib' + lib + '.a')
if p:
dst_name = os.path.join(self.build_temp, lib + '.lib')
- copy_file(p[0], dst_name)
- if not lib_added:
+ if not os.path.isfile(dst_name):
+ copy_file(p[0], dst_name)
+ if self.build_temp not in c_library_dirs:
c_library_dirs.append(self.build_temp)
- lib_added = True
def get_source_files (self):
self.check_extensions_list(self.extensions)