From 81bcf48dc545b2749fa30068afbb649dc982e33e Mon Sep 17 00:00:00 2001 From: Craig Warren Date: Wed, 5 Jan 2022 16:35:49 +0000 Subject: [PATCH] Overhauled and steamlined setup.py --- setup.py | 158 +++++++++++++++++++++---------------------------------- 1 file changed, 60 insertions(+), 98 deletions(-) diff --git a/setup.py b/setup.py index 8232da91..d1b9c9ca 100644 --- a/setup.py +++ b/setup.py @@ -16,17 +16,6 @@ # You should have received a copy of the GNU General Public License # along with gprMax. If not, see . -try: - from setuptools import Extension, setup -except ImportError: - from distutils.core import setup - from distutils.extension import Extension - -try: - import numpy as np -except ImportError: - raise ImportError('gprMax requires the NumPy package.') - import glob import os import re @@ -35,24 +24,29 @@ import subprocess import sys from pathlib import Path +import numpy as np +from Cython.Build import cythonize from jinja2 import Environment, FileSystemLoader +from setuptools import Extension, find_packages, setup -MIN_PYTHON_VERSION = (3, 7) # Check Python version +MIN_PYTHON_VERSION = (3, 7) if sys.version_info[:2] < MIN_PYTHON_VERSION: - sys.exit('\nExited: Requires Python 3.7 or newer!\n') + sys.exit('\nExited: Requires Python {MIN_PYTHON_VERSION[0]}.{MIN_PYTHON_VERSION[1]} or newer!\n') +# Importing gprMax _version__.py before building can cause issues. +with open('gprMax/_version.py', 'r') as fd: + version = re.search(r'^__version__\s*=\s*[\'"]([^\'"]*)[\'"]', + fd.read(), re.MULTILINE).group(1) def build_dispersive_material_templates(): - """Function to generate Cython .pyx files for dispersive media update. + """Function to generate Cython .pyx file for dispersive media update. Jinja2 templates are used to render the various dispersive update functions. """ - env = Environment( - loader=FileSystemLoader('gprMax/templates'), - ) + env = Environment(loader = FileSystemLoader('gprMax/templates'), ) template = env.get_template('fields_updates_dispersive_template') @@ -109,20 +103,8 @@ def build_dispersive_material_templates(): f.write(r) # Generate Cython file for dispersive materials update functions -build_dispersive_material_templates() - -# Importing _version__.py before building can cause issues. -with open('gprMax/_version.py', 'r') as fd: - version = re.search(r'^__version__\s*=\s*[\'"]([^\'"]*)[\'"]', - fd.read(), re.MULTILINE).group(1) - -# Parse package name from init file. Importing __init__.py / gprMax will break -# as gprMax depends on compiled .pyx files. -with open('gprMax/__init__.py', 'r') as fd: - packagename = re.search(r'^__name__\s*=\s*[\'"]([^\'"]*)[\'"]', - fd.read(), re.MULTILINE).group(1) - -packages = [packagename, 'tests', 'tools', 'user_libs'] +if not os.path.isfile('gprMax/cython/fields_updates_dispersive.pyx'): + build_dispersive_material_templates() # Process 'build' command line argument if 'build' in sys.argv: @@ -131,25 +113,16 @@ if 'build' in sys.argv: sys.argv.append('build_ext') sys.argv.append('--inplace') -# Process '--no-cython' command line argument - either Cythonize or just compile -# the .c files -if '--no-cython' in sys.argv: - USE_CYTHON = False - sys.argv.remove('--no-cython') -else: - USE_CYTHON = True - # Build a list of all the files that need to be Cythonized looking in gprMax # directory cythonfiles = [] -for root, dirs, files in os.walk(os.path.join(os.getcwd(), packagename), topdown=True): +for root, dirs, files in os.walk(os.path.join(os.getcwd(), 'gprMax'), topdown=True): for file in files: if file.endswith('.pyx'): cythonfiles.append(os.path.relpath(os.path.join(root, file))) -# Process 'cleanall' command line argument - cleanup Cython files +# Process 'cleanall' command line argument if 'cleanall' in sys.argv: - USE_CYTHON = False for file in cythonfiles: filebase = os.path.splitext(file)[0] # Remove Cython C files @@ -185,7 +158,7 @@ else: if sys.platform == 'win32': compile_args = ['/O2', '/openmp', '/w'] # No static linking as no static version of OpenMP library; /w disables warnings linker_args = [] - libraries=[] + libraries = [] # Compiler options - macOS - needs gcc (usually via HomeBrew) because the # default compiler LLVM (clang) does not @@ -203,7 +176,7 @@ else: os.environ['CC'] = gccpath[-1].split(os.sep)[-1] rpath = '/opt/homebrew/opt/gcc/lib/gcc/' + gccpath[-1].split(os.sep)[-1][-1] + '/' else: - raise('Cannot find gcc 4-10 in /opt/homebrew/bin. gprMax requires gcc to be installed - easily done through the Homebrew package manager (http://brew.sh). Note: gcc with OpenMP support is required.') + raise('Cannot find gcc in /opt/homebrew/bin. gprMax requires gcc to be installed - easily done through the Homebrew package manager (http://brew.sh). Note: gcc with OpenMP support is required.') else: gccpath = glob.glob('/usr/local/bin/gcc-[4-9]*') gccpath += glob.glob('/usr/local/bin/gcc-[10-11]*') @@ -221,7 +194,9 @@ else: except: pass os.environ['MIN_SUPPORTED_MACOSX_DEPLOYMENT_TARGET'] = MIN_MACOS_VERSION - compile_args = ['-O3', '-w', '-fopenmp', '-march=native', '-mmacosx-version-min=' + MIN_MACOS_VERSION] # Sometimes worth testing with '-fstrict-aliasing', '-fno-common' + # Sometimes worth testing with '-fstrict-aliasing', '-fno-common' + compile_args = ['-O3', '-w', '-fopenmp', '-march=native', + '-mmacosx-version-min=' + MIN_MACOS_VERSION] linker_args = ['-fopenmp', '-mmacosx-version-min=' + MIN_MACOS_VERSION] libraries=['gomp'] @@ -229,18 +204,14 @@ else: elif sys.platform == 'linux': compile_args = ['-O3', '-w', '-fopenmp', '-march=native'] linker_args = ['-fopenmp'] - libraries=[] + libraries = [] # Build list of all the extensions - Cython source files extensions = [] for file in cythonfiles: tmp = os.path.splitext(file) - if USE_CYTHON: - fileext = tmp[1] - else: - fileext = '.c' extension = Extension(tmp[0].replace(os.sep, '.'), - [tmp[0] + fileext], + [tmp[0] + tmp[1]], language='c', include_dirs=[np.get_include()], extra_compile_args=compile_args, @@ -249,57 +220,48 @@ else: extensions.append(extension) # Cythonize - build .c files - if USE_CYTHON: - from Cython.Build import cythonize - extensions = cythonize(extensions, - compiler_directives={ - 'boundscheck': False, - 'wraparound': False, - 'initializedcheck': False, - 'embedsignature': True, - 'language_level': 3 - }, - nthreads=None, - annotate=False) + extensions = cythonize(extensions, + compiler_directives={'boundscheck': False, + 'wraparound': False, + 'initializedcheck': False, + 'embedsignature': True, + 'language_level': 3}, + nthreads=None, + annotate=False) # Parse long_description from README.rst file. with open('README.rst','r') as fd: long_description = fd.read() - setup(name=packagename, - version=version, - author='Craig Warren, Antonis Giannopoulos, and John Hartley', - url='http://www.gprmax.com', - description='Electromagnetic Modelling Software based on the Finite-Difference Time-Domain (FDTD) method', - long_description=long_description, - long_description_content_type="text/x-rst", - license='GPLv3+', - classifiers=[ - 'Environment :: Console', - 'License :: OSI Approved :: GNU General Public License v3 or later (GPLv3+)', - 'Operating System :: MacOS', - 'Operating System :: Microsoft :: Windows', - 'Operating System :: POSIX :: Linux', - 'Programming Language :: Cython', - 'Programming Language :: Python :: 3', - 'Topic :: Scientific/Engineering' - ], - #requirements - python_requires='>' + str(MIN_PYTHON_VERSION[0]) + '.' + str(MIN_PYTHON_VERSION[1]), - install_requires=[ - 'colorama', - 'cython', - 'h5py', - 'jinja2', - 'jupyter', - 'matplotlib', - 'numpy', - 'psutil', - 'scipy', - 'terminaltables', - 'tqdm', - ], + setup(name='gprMax', + version=version, + author='Craig Warren, Antonis Giannopoulos, and John Hartley', + url='http://www.gprmax.com', + description='Electromagnetic Modelling Software based on the Finite-Difference Time-Domain (FDTD) method', + long_description=long_description, + long_description_content_type="text/x-rst", + license='GPLv3+', + python_requires='>' + str(MIN_PYTHON_VERSION[0]) + '.' + str(MIN_PYTHON_VERSION[1]), + install_requires=['colorama', + 'cython', + 'h5py', + 'jinja2', + 'matplotlib', + 'numpy', + 'psutil', + 'scipy', + 'terminaltables', + 'tqdm'], ext_modules=extensions, - packages=packages, + packages=find_packages(), include_package_data=True, - include_dirs=[np.get_include()]) + include_dirs=[np.get_include()], + zip_safe=False, + classifiers=['Environment :: Console', + 'License :: OSI Approved :: GNU General Public License v3 or later (GPLv3+)', + 'Operating System :: MacOS', + 'Operating System :: Microsoft :: Windows', + 'Operating System :: POSIX :: Linux', + 'Programming Language :: Cython', + 'Programming Language :: Python :: 3', + 'Topic :: Scientific/Engineering'])