Skip to content

Commit

Permalink
Updated setup script
Browse files Browse the repository at this point in the history
  • Loading branch information
LaurentRDC committed May 1, 2024
1 parent a262f3a commit 00f5a98
Showing 1 changed file with 2 additions and 68 deletions.
70 changes: 2 additions & 68 deletions setup.py
Original file line number Diff line number Diff line change
@@ -1,25 +1,18 @@
# -*- encoding: utf-8 -*-

import os
import platform
import re
import shutil
import tempfile
from glob import glob
from itertools import chain
from pathlib import Path

import numpy
from setuptools import Extension, find_packages, setup
from setuptools.command.build_ext import build_ext
from distutils.errors import CompileError, LinkError
from setuptools import find_packages, setup

PACKAGE_NAME = "crystals"
DESCRIPTION = "Data structures for crystallography"
URL = "http://crystals.readthedocs.io"
DOWNLOAD_URL = "http://github.com/LaurentRDC/crystals"
AUTHOR = "Laurent P. René de Cotret"
AUTHOR_EMAIL = "laurent.renedecotret@mail.mcgill.ca"
AUTHOR_EMAIL = "laurent.decotret@outlook.com"

CIF_FILES = chain.from_iterable([glob("crystals\\cifs\\*.cif")])

Expand All @@ -40,64 +33,6 @@
REQUIREMENTS = [line for line in f.read().split("\n") if len(line.strip())]


# Support for openmp
# This idea is from scikit-image:
# https://github.com/scikit-image/scikit-image/
class BuildExtWithOpenMP(build_ext):
"""
Try to compile extensions with OpenMP if possible
"""

def can_compile_link(self, compile_flags, link_flags):
cc = self.compiler
fname = "test.c"
cwd = os.getcwd()
tmpdir = tempfile.mkdtemp()

code = "#include <omp.h>" "int main(int argc, char** argv) { return(0); }"

if self.compiler.compiler_type == "msvc":
# make sure we build a DLL on Windows
local_link_flags = link_flags + ["/DLL"]
else:
local_link_flags = link_flags

try:
os.chdir(tmpdir)
with open(fname, "wt") as fobj:
fobj.write(code)
try:
objects = cc.compile([fname], extra_postargs=compile_flags)
except CompileError:
return False
try:
# Link shared lib rather then executable to avoid
# http://bugs.python.org/issue4431 with MSVC 10+
cc.link_shared_lib(objects, "testlib", extra_postargs=local_link_flags)
except (LinkError, TypeError):
return False
finally:
os.chdir(cwd)
shutil.rmtree(tmpdir)
return True

def build_extensions(self):
"""Hook into extension building to set compiler flags"""
if self.compiler.compiler_type == "msvc":
compile_flags = ["/openmp"]
link_flags = []
else:
compile_flags = ["-fopenmp"]
link_flags = ["-fopenmp"]

if self.can_compile_link(compile_flags, link_flags):
for ext in self.extensions:
ext.extra_compile_args += compile_flags
ext.extra_link_args += link_flags

return super().build_extensions()


if __name__ == "__main__":
setup(
name=PACKAGE_NAME,
Expand All @@ -122,7 +57,6 @@ def build_extensions(self):
packages=find_packages(),
data_files=[("crystals\\cifs", CIF_FILES)],
include_package_data=True,
cmdclass=dict(build_ext=BuildExtWithOpenMP),
zip_safe=False,
entry_points={"console_scripts": ["crystals = crystals.__main__:main"]},
# list of possible classifiers:
Expand Down

0 comments on commit 00f5a98

Please sign in to comment.