1#
2#    ICRAR - International Centre for Radio Astronomy Research
3#    (c) UWA - The University of Western Australia, 2014
4#    Copyright by UWA (in the framework of the ICRAR)
5#    All rights reserved
6#
7#    This library is free software; you can redistribute it and/or
8#    modify it under the terms of the GNU Lesser General Public
9#    License as published by the Free Software Foundation; either
10#    version 2.1 of the License, or (at your option) any later version.
11#
12#    This library is distributed in the hope that it will be useful,
13#    but WITHOUT ANY WARRANTY; without even the implied warranty of
14#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15#    Lesser General Public License for more details.
16#
17#    You should have received a copy of the GNU Lesser General Public
18#    License along with this library; if not, write to the Free Software
19#    Foundation, Inc., 59 Temple Place, Suite 330, Boston,
20#    MA 02111-1307  USA
21#
22import glob
23import platform
24
25import distutils.ccompiler
26from setuptools import setup, Extension
27from setuptools.command.build_ext import build_ext
28
29
30crcmod_ext = Extension('crc32c',
31                       define_macros=[('NDEBUG', None)],
32                       depends=glob.glob('*.h'),
33                       language='c',
34                       sources=['_crc32c.c', 'checkarm.c', 'checksse42.c', 'crc32c_adler.c', 'crc32c_arm64.c', 'crc32c_sw.c'],
35                       include_dirs=['.'])
36
37def get_extra_compile_args(is_intel, is_arm):
38    # msvc is treated specially; otherwise we assume it's a unix compiler
39    comp = distutils.ccompiler.get_default_compiler()
40    if comp == 'msvc':
41        return ['/O2']
42    elif is_intel:
43        return ['-O3', '-msse4.2', '-mpclmul']
44    elif is_arm:
45        return ['-O3', '-march=armv8-a+crc+crypto']
46    else:
47        return ['-O3']
48
49class _build_ext(build_ext):
50    """Custom build_ext command that includes extra compilation arguments"""
51
52    user_options = build_ext.user_options + [
53        ('platform=', None, 'The target platform name')]
54
55    def initialize_options(self):
56        build_ext.initialize_options(self)
57        self.platform = platform.machine()
58
59    def run(self):
60        assert(len(self.distribution.ext_modules) == 1)
61        platform = self.platform.lower()
62        is_intel = platform in ['x86_64', 'amd64', 'i386', 'i686']
63        is_arm = platform in ['aarch64_be', 'aarch64', 'armv8b', 'armv8l']
64        distutils.log.info("platform: %s, is_intel: %d, is_arm: %d", platform, is_intel, is_arm)
65        self.distribution.ext_modules[0].extra_compile_args = get_extra_compile_args(is_intel, is_arm)
66        if is_intel:
67            self.distribution.ext_modules[0].define_macros += [('IS_INTEL', None)]
68        elif is_arm:
69            self.distribution.ext_modules[0].define_macros += [('IS_ARM', None)]
70        build_ext.run(self)
71
72
73classifiers = [
74    # There's no more specific classifier for LGPLv2.1+
75    "License :: OSI Approved :: GNU Lesser General Public License v2 or later (LGPLv2+)",
76    "Operating System :: OS Independent",
77    "Programming Language :: C",
78    "Programming Language :: Python :: 2.7",
79    "Programming Language :: Python :: 3.2",
80    "Programming Language :: Python :: 3.3",
81    "Programming Language :: Python :: 3.4",
82    "Programming Language :: Python :: 3.5",
83    "Programming Language :: Python :: 3.6",
84    "Programming Language :: Python :: 3.7",
85    "Programming Language :: Python :: 3.8",
86    "Programming Language :: Python :: 3.9",
87]
88
89with open('README.rst', 'rt') as f:
90    long_description = f.read()
91
92setup(name='crc32c',
93      author='The ICRAR DIA Team',
94      url='https://github.com/ICRAR/crc32c',
95      author_email='rtobar@icrar.org',
96      version='2.2',
97      license="LGPLv2.1+",
98      description=('A python package implementing the crc32c algorithm'
99      'in hardware and software'),
100      long_description=long_description,
101      long_description_content_type='text/x-rst',
102      classifiers=classifiers,
103      ext_modules=[crcmod_ext],
104      cmdclass = {'build_ext': _build_ext},
105      test_suite="test")
106