Newer
Older
from numpy.distutils.misc_util import Configuration
Jean-Baptiste Keck
committed
Jean-Matthieu Etancelin
committed
import os
Jean-Baptiste Keck
committed
import re
Jean-Baptiste Keck
committed
import fnmatch
# tools to deal with fortran dependencies.
import sys
def parseCMakeVar(var, sep=';'):
"""
Post-process cmake list-like variables.
a = parseCMakeVar("var1;var2 ; var3;")
Jean-Baptiste Keck
committed
if var != "":
# split and remove empty strings
res = list(var.split(sep))
return list(filter(len, map(str.strip, res)))
Jean-Baptiste Keck
committed
else:
Jean-Baptiste Keck
committed
def parseCMakeDefines(var):
"""
Post-process cmake variables, corresponding
to pre-processor definitions.
Example::
res = parseCMakeDefines("-DVAR=VAL1:-DVAR2=VAL2:-DVAR3=VAL3")
# --> res = [('VAR', 'VAL1'), ('VAR2', 'VAL2'), ('VAR3', 'VAL3')]
"""
Jean-Baptiste Keck
committed
defines = parseCMakeVar(var)
Jean-Baptiste Keck
committed
return None
EXT Jean-Matthieu Etancelin
committed
# regex to match compiler defines like -DMACRO_NAME or
# -DMACRO_NAME = MACRO_VALUE
p = re.compile(r'\s*(?:-D)?\s*(\w+)(?:\s*=\s*(\w+))?\s*')
Jean-Baptiste Keck
committed
res = list()
for d in defines:
m = p.match(d)
if m:
res.append(m.group(1,2))
else:
print("\tWarning: Could not extract cmake define from '", d, "'.")
Jean-Baptiste Keck
committed
return res
hysop_link_libraries = parseCMakeVar("@HYSOP_LINK_LIBRARIES@")
# deal with macosx framework ...
extra_flags = []
filename = os.path.basename(lib)
hysop_link_libraries.pop(index)
filename = filename.split('.')[0]
extra_flags.append('-framework ' + filename)
hysop_link_libraries += extra_flags
def create_fortran_extension(name, pyf_file=None, src_dirs=None, sources=None,
libdir=None, libs=None, debug_mode=0):
"""Create a new f2py module from fortran files
Parameters
----------
name: string
python module name
pyf_file: string, optional
f2py interface file (absolute path)
src_dirs: list, optional
all directories containing fortran sources (absolute path)
that must be taken into account. Warning: dir. scan is not
recursive.
sources: list, optional
all fortran files that must be taken into account
libs: list of strings, optional
names of all libraries to be linked with to build f2py module
--> for -l option
libdir: list of strings, optional
list of directories, absolute path to libraries listed in libs
--> for -L... option
debug_mode: boolean, optional
if true enable f2py debug mode, default=False
Notes
-----
* either sources or src_dirs is required but both
can be used simultaneously.
* only .f90 and .f95 files are taken into account.
if sources is None:
sources = []
assert src_dirs is not None
for sdir in src_dirs:
sources += glob.glob(os.path.join(sdir, '*.f95'))
sources += glob.glob(os.path.join(sdir, '*.f90'))
EXT Jean-Matthieu Etancelin
committed
# Reorder source list with fortran modules
# dependencies. It seems that this is not taken into
# account in f2py or distutils.
if pyf_file is not None:
sources.append(pyf_file)
sources = sort_f90.sort(sources)
# --- set f2py options ---
f2py_options = ['--no-lower', '--no-wrap-functions']
options = []
if debug_mode == 1:
options.append(('F2PY_REPORT_ON_ARRAY_COPY', '1'))
if os.uname()[0] == 'Linux':
options.append(('F2PY_REPORT_ATEXIT', '1'))
inc_dir = parseCMakeVar("@FORTRAN_INCLUDE_DIRS@")
inc_dir += parseCMakeVar("@MPI_Fortran_INCLUDE_PATH@")
inc_dir += parseCMakeVar('@CMAKE_Fortran_MODULE_DIRECTORY@')
fortran_flags = parseCMakeVar('@Fortran_FLAGS@', sep=' ')
# we trust cmake for external libraries and
# add them to linker, without using libraries option
extra_link_args = hysop_link_libraries
EXT Jean-Matthieu Etancelin
committed
ext_fort = Extension(name=name,
sources=sources,
f2py_options=f2py_options,
include_dirs=inc_dir,
define_macros=options,
library_dirs=libdir,
libraries=libs,
extra_f90_compile_args=fortran_flags,
extra_link_args=extra_link_args
# ------------ Set list of packages required to build the module -------------
with_test = "@WITH_TESTS@" == "ON"
with_opencl = "@WITH_OPENCL@" == "ON"
exclude=[]
if not with_test:
exclude.append('*tests*')
Jean-Baptiste Keck
committed
if not with_opencl:
exclude.append('*opencl*')
packages = find_packages(exclude=exclude, where="@CMAKE_SOURCE_DIR@")
Jean-Matthieu Etancelin
committed
DISTUTILS_DEBUG = 1
# ------------ Extensions (f2py, cython, ...) setup ------------
ext_modules = []
# Check if fortran interface is enabled
fortran_root = \
'@CMAKE_SOURCE_DIR@/hysop'
hysop_libdir = ['@CMAKE_BINARY_DIR@/src']
hysoplib = ['@HYSOP_LIBRARY_NAME@']
f2py_options = ['--no-lower']
# -- list of directories which contains fortran sources --
# path must be relative to hysop package directory
subdirs = ['common_f', ]
withfftw = "@WITH_FFTW@"
subdirs.append(os.path.join('numerics', 'fftw_f'))
fftwdirs = parseCMakeVar('@FFTWLIB@')
hysop_libdir.extend(fftwdirs)
withscales = '@WITH_SCALES@'
subdirs.append('scales_f')
#fortran_src.add('f2py/scales2py.f90')
# -- other fortran sources --
withextras = '@WITH_EXTRAS@'
subdirs.append(os.path.join('numerics', 'extras_f'))
# -- set full path to fortran sources --
fortran_src = list(fortran_src)
fortran_src[i] = os.path.join(fortran_root, fortran_src[i])
precision_file = os.path.join('@GENERATED_FORTRAN_FILES_DIR@',
EXT Jean-Matthieu Etancelin
committed
'precision.f90')
# === Draft for future implementation of fortran interface ===
# -- f2py signature file --
f2hysop = "@F_2_HYSOP@"
pyf_file = os.path.join('@GENERATED_FORTRAN_FILES_DIR@', f2hysop+'.pyf')
# change from relative path in subdirs to absolute path in num_dirs
num_dirs = []
for sd in subdirs:
num_dirs.append(os.path.join(fortran_root, sd))
EXT Jean-Matthieu Etancelin
committed
hysop_libdir = [ld.strip() for ld in hysop_libdir]
# hysop_libdir = ' '.join([ '-L{}'.format(ld) if ld[1]!='L' else hysop_libdir])
# create python interface to fortran sources
# For the moment, it includes the 'old' interface
# to scales and fftw (in sources) and the new
# interface, in src_dirs
ext[f2hysop] = create_fortran_extension(
name='hysop.'+f2hysop,
sources=fortran_src,
libdir=hysop_libdir,
libs=hysoplib,
pyf_file=pyf_file,
src_dirs=num_dirs)
for ex in ext:
ext_modules.append(ext[ex])
Jean-Baptiste Keck
committed
authors = 'G.H Cottet, J.M Etancelin, J.B Keck, C.Mimeau, F.Pérignon, C. Picard'
name='@PACKAGE_NAME@',
version='@HYSOP_VERSION@',
description=descr,
author=authors,
author_email='hysop-members@lists.forge.imag.fr',
url='https://gricad-gitlab.univ-grenoble-alpes.fr/particle_methods/hysop',
license='Apache Licence, Version 2.0 (APLv2)',
package_dir={'': '@CMAKE_SOURCE_DIR@'},
ext_modules=ext_modules,
packages=packages,
data_files=data_files,
setup(**config.todict())