Skip to content

Commit

Permalink
Simplify setup.py by using pybind11's build_ext
Browse files Browse the repository at this point in the history
  • Loading branch information
rmjarvis committed Nov 25, 2024
1 parent 7194440 commit a4d3be5
Showing 1 changed file with 12 additions and 294 deletions.
306 changes: 12 additions & 294 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,47 +2,12 @@
import shutil
import urllib.request as urllib2
import tarfile
import glob

try:
from setuptools import setup, Extension, find_packages
from setuptools.command.build_ext import build_ext
from setuptools.command.install_scripts import install_scripts
from setuptools.command.easy_install import easy_install
import setuptools
print("Using setuptools version",setuptools.__version__)
except ImportError:
print('Unable to import setuptools. Using distutils instead.')
from distutils.core import setup, Extension
from distutils.command.build_ext import build_ext
from distutils.command.install_scripts import install_scripts
easy_install = object # Prevent error when using as base class
import distutils
# cf. http://stackoverflow.com/questions/1612733/including-non-python-files-with-setup-py
from distutils.command.install import INSTALL_SCHEMES
for scheme in INSTALL_SCHEMES.values():
scheme['data'] = scheme['purelib']
# cf. http://stackoverflow.com/questions/37350816/whats-distutils-equivalent-of-setuptools-find-packages-python
from distutils.util import convert_path
def find_packages(base_path='.'):
base_path = convert_path(base_path)
found = []
for root, dirs, files in os.walk(base_path, followlinks=True):
dirs[:] = [d for d in dirs if d[0] != '.' and d not in ('ez_setup', '__pycache__')]
relpath = os.path.relpath(root, base_path)
parent = relpath.replace(os.sep, '.').lstrip('.')
if relpath != '.' and parent not in found:
# foo.bar package but no foo package, skip
continue
for dir in dirs:
if os.path.isfile(os.path.join(root, dir, '__init__.py')):
package = '.'.join((parent, dir)) if parent else dir
found.append(package)
return found
print("Using distutils version",distutils.__version__)

from distutils.command.install_headers import install_headers
from pybind11.setup_helpers import Pybind11Extension
from setuptools import setup, find_packages
import setuptools
print("Using setuptools version",setuptools.__version__)

from pybind11.setup_helpers import Pybind11Extension, build_ext

try:
from sysconfig import get_config_vars
Expand All @@ -56,44 +21,9 @@ def find_packages(base_path='.'):
scripts = [ os.path.join('scripts',f) for f in scripts ]
shared_data = glob.glob('share/*')

undef_macros = []

packages = find_packages()
print('packages = ',packages)

# If we build with debug, also undefine NDEBUG flag
if "--debug" in sys.argv:
undef_macros+=['NDEBUG']

copt = {
'gcc' : ['-O2','-std=c++14','-fvisibility=hidden','-fopenmp'],
'icc' : ['-O2','-vec-report0','-std=c++14','-openmp'],
'clang' : ['-O2','-std=c++14',
'-Wno-shorten-64-to-32','-fvisibility=hidden','-stdlib=libc++'],
'clang w/ OpenMP' : ['-O2','-std=c++14','-fopenmp',
'-Wno-shorten-64-to-32','-fvisibility=hidden','-stdlib=libc++'],
'unknown' : [],
}
lopt = {
'gcc' : ['-fopenmp'],
'gcc w/ GPU' : ['-fopenmp','-foffload=nvptx-none', '-foffload=-lm'],
'icc' : ['-openmp'],
'clang' : ['-stdlib=libc++'],
'clang w/ OpenMP' : ['-stdlib=libc++','-fopenmp'],
'clang w/ Intel OpenMP' : ['-stdlib=libc++','-liomp5'],
'clang w/ manual OpenMP' : ['-stdlib=libc++','-lomp'],
'clang w/ GPU' : ['-fopenmp','-fopenmp-targets=nvptx64-nvidia-cuda',
'-Wno-openmp-mapping','-Wno-unknown-cuda-version'],
'nvc++' : ['-mp=gpu'],
'unknown' : [],
}

if "--debug" in sys.argv:
copt['gcc'].append('-g')
copt['icc'].append('-g')
copt['clang'].append('-g')
copt['clang w/ OpenMP'].append('-g')

# Check for Eigen in some likely places
def find_eigen_dir(output=False):

Expand Down Expand Up @@ -246,204 +176,6 @@ def find_pybind_path():

return pybind_path

def get_compiler(cc):
"""Try to figure out which kind of compiler this really is.
In particular, try to distinguish between clang and gcc, either of which may
be called cc or gcc.
"""
cmd = [cc,'--version']
import subprocess
p = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
lines = p.stdout.readlines()
print('compiler version information: ')
for line in lines:
print(line.strip())
line = lines[0].decode(encoding='UTF-8')
if line.startswith('Configured'):
line = lines[1].decode(encoding='UTF-8')

if "clang" in line:
# clang 3.7 is the first with openmp support. So check the version number.
# It can show up in one of two places depending on whether this is Apple clang or
# regular clang.
import re
if 'LLVM' in line:
match = re.search(r'LLVM [0-9]+(\.[0-9]+)+', line)
match_num = 1
else:
match = re.search(r'[0-9]+(\.[0-9]+)+', line)
match_num = 0
if match:
version = match.group(0).split()[match_num]
print('clang version = ',version)
# Get the version up to the first decimal
# e.g. for 3.4.1 we only keep 3.4
vnum = version[0:version.find('.')+2]
if vnum >= '3.7':
return 'clang w/ OpenMP'
return 'clang'
elif 'gcc' in line:
return 'gcc'
elif 'GCC' in line:
return 'gcc'
elif 'clang' in cc:
return 'clang'
elif 'gcc' in cc or 'g++' in cc:
return 'gcc'
elif 'icc' in cc or 'icpc' in cc:
return 'icc'
else:
# OK, the main thing we need to know is what openmp flag we need for this compiler,
# so let's just try the various options and see what works. Don't try icc, since
# the -openmp flag there gets treated as '-o penmp' by gcc and clang, which is bad.
# Plus, icc should be detected correctly by the above procedure anyway.
for cc_type in ['gcc', 'clang']:
if try_cc(cc, cc_type):
return cc_type
# I guess none of them worked. Now we really do have to bail.
return 'unknown'

def try_cc(cc, cc_type):
"""
If cc --version is not helpful, the last resort is to try each compiler type and see
if it works.
"""
cpp_code = """
#include <iostream>
#include <vector>
#ifdef _OPENMP
#include "omp.h"
#endif
int get_max_threads() {
#ifdef _OPENMP
return omp_get_max_threads();
#else
return 1;
#endif
}
int main() {
int n = 500;
std::vector<double> x(n,0.);
#ifdef _OPENMP
#pragma omp parallel for schedule(static)
#endif
for (int i=0; i<n; ++i) x[i] = 2*i+1;
double sum = 0.;
for (int i=0; i<n; ++i) sum += x[i];
// Sum should be n^2 = 250000
std::cout<<get_max_threads()<<" "<<sum<<std::endl;
return 0;
}
"""
import tempfile
cpp_file = tempfile.NamedTemporaryFile(delete=False, suffix='.cpp')
cpp_bytes = cpp_code.encode()
cpp_file.write(cpp_bytes)
cpp_file.close()

# Just get a named temporary file to write to:
o_file = tempfile.NamedTemporaryFile(delete=False, suffix='.os')
o_file.close()

# Try compiling with the given flags
import subprocess
cmd = [cc] + copt[cc_type] + ['-c',cpp_file.name,'-o',o_file.name]
p = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
lines = p.stdout.readlines()
p.communicate()
if p.returncode != 0:
os.remove(cpp_file.name)
if os.path.exists(o_file.name): os.remove(o_file.name)
return False

# Another named temporary file for the executable
exe_file = tempfile.NamedTemporaryFile(delete=False, suffix='.exe')
exe_file.close()

# Try linking
cmd = [cc] + lopt[cc_type] + [o_file.name,'-o',exe_file.name]
p = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
lines = p.stdout.readlines()
p.communicate()

if p.returncode and cc == 'cc':
# The linker needs to be a c++ linker, which isn't 'cc'. However, I couldn't figure
# out how to get setup.py to tell me the actual command to use for linking. All the
# executables available from build_ext.compiler.executables are 'cc', not 'c++'.
# I think this must be related to the bugs about not handling c++ correctly.
# http://bugs.python.org/issue9031
# http://bugs.python.org/issue1222585
# So just switch it manually and see if that works.
cmd = ['c++'] + lopt[cc_type] + [o_file.name,'-o',exe_file.name]
p = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
lines = p.stdout.readlines()
p.communicate()

# Remove the temp files
os.remove(cpp_file.name)
os.remove(o_file.name)
if os.path.exists(exe_file.name): os.remove(exe_file.name)
return p.returncode == 0


# This was supposed to remove the -Wstrict-prototypes flag
# But it doesn't work....
# Hopefully they'll fix this bug soon:
# http://bugs.python.org/issue9031
# http://bugs.python.org/issue1222585
#(opt,) = get_config_vars('OPT')
#os.environ['OPT'] = " ".join( flag for flag in opt.split() if flag != '-Wstrict-prototypes')

# Make a subclass of build_ext so we can do different things depending on which compiler we have.
# In particular, we may want to use different compiler options for OpenMP in each case.
# cf. http://stackoverflow.com/questions/724664/python-distutils-how-to-get-a-compiler-that-is-going-to-be-used
# We're not currently using OpenMP, but with this bit we'll be ready.
class my_builder( build_ext ):
def build_extensions(self):
# Figure out what compiler it will use
cc = self.compiler.executables['compiler_cxx'][0]
comp_type = get_compiler(cc)
if cc == comp_type:
print('Using compiler %s'%(cc))
else:
print('Using compiler %s, which is %s'%(cc,comp_type))
# Add the appropriate extra flags for that compiler.
eigen_path = find_eigen_dir(output=True)
pybind_path = find_pybind_path()
for e in self.extensions:
e.extra_compile_args = copt[ comp_type ]
e.extra_link_args = lopt[ comp_type ]
e.include_dirs = ['include']
e.include_dirs.append(eigen_path)
e.include_dirs.append(pybind_path)
# Now run the normal build function.
build_ext.build_extensions(self)

# AFAICT, setuptools doesn't provide any easy access to the final installation location of the
# executable scripts. This bit is just to save the value of script_dir so I can use it later.
# cf. http://stackoverflow.com/questions/12975540/correct-way-to-find-scripts-directory-from-setup-py-in-python-distutils/
class my_easy_install( easy_install ): # For setuptools

# Match the call signature of the easy_install version.
def write_script(self, script_name, contents, mode="t", *ignored):
# Run the normal version
easy_install.write_script(self, script_name, contents, mode, *ignored)
# Save the script install directory in the distribution object.
# This is the same thing that is returned by the setup function.
self.distribution.script_install_dir = self.script_dir

# For distutils, the appropriate thing is the install_scripts command class, not easy_install.
# So here is the appropriate thing in that case.
class my_install_scripts( install_scripts ): # For distutils
def run(self):
install_scripts.run(self)
self.distribution.script_install_dir = self.install_dir

build_dep = ['setuptools>=38', 'numpy>=1.17', 'pybind11>=2.2']
run_dep = ['galsim>=2.3', 'numpy>=1.17', 'scipy>=1.2', 'pyyaml>=5.1', 'treecorr>=4.3.1', 'fitsio>=1.0', 'matplotlib>=3.3', 'LSSTDESC.Coord>=1.0', 'treegp>=0.6', 'threadpoolctl>=3.1']

Expand All @@ -463,7 +195,12 @@ def run(self):
print('Piff version is %s'%(piff_version))

sources = glob.glob(os.path.join('src','*.cpp'))
ext = Pybind11Extension("piff._piff", sources)

ext = Pybind11Extension(
"piff._piff",
sources,
include_dirs=[find_eigen_dir(output=True), find_pybind_path()]
)

dist = setup(name="Piff",
version=piff_version,
Expand All @@ -479,25 +216,6 @@ def run(self):
setup_requires=build_dep,
install_requires=run_dep,
ext_modules=[ext],
cmdclass = {'build_ext': my_builder,
'install_scripts': my_install_scripts,
'easy_install': my_easy_install,
},
cmdclass = {'build_ext': build_ext},
scripts=scripts
)

# Check that the path includes the directory where the scripts are installed.
# NB. If not running install, then script_install_dir won't be there...
real_env_path = [os.path.realpath(d) for d in os.environ['PATH'].split(':')]
if (hasattr(dist,'script_install_dir') and
dist.script_install_dir not in os.environ['PATH'].split(':') and
os.path.realpath(dist.script_install_dir) not in real_env_path):

print('\nWARNING: The Piff executables were installed in a directory not in your PATH')
print(' If you want to use the executables, you should add the directory')
print('\n ',dist.script_install_dir,'\n')
print(' to your path. The current path is')
print('\n ',os.environ['PATH'],'\n')
print(' Alternatively, you can specify a different prefix with --prefix=PREFIX,')
print(' in which case the scripts will be installed in PREFIX/bin.')
print(' If you are installing via pip use --install-option="--prefix=PREFIX"')

0 comments on commit a4d3be5

Please sign in to comment.