5.6.5 : The module configuration : setup.py

The setup.py is the file which really makes the python module. First, we have to import tools to make the installation of our module :
1
2
3
4
5
6
from setuptools import setup
from setuptools import Extension
from setuptools.dist import Distribution
from sys import prefix
import subprocess
import numpy as np
Check is Cython package is installed. The Cython package makes the python extensions for wrappers.
1
2
3
4
5
6
7
8
try:
	from Cython.Distutils import build_ext
except ImportError:
	use_cython = False
	print('Cython not found')
	raise Exception('Please install Cython on your system')
else:
	use_cython = True
Some informations to be sure that we will not lose them is the file size increases :
1
2
3
4
5
6
7
NAME = 'astericshpc'
VERSION = '0.1'
AUTHOR = 'Pierre Aubert'
AUTHOR_EMAIL = 'aubertp7@gmail.com'
URL = ''
DESCRIPTION = 'Basic functions for ASTERICS HPC lecture'
LICENSE = 'CeCILL-C'
Catch the installation prefix is the user specifies one :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
def get_prefix():
	"""
	Get prefix from either config file or command line
	:return: str
	prefix install path
	"""
	dist = Distribution()
	dist.parse_config_files()
	dist.parse_command_line()
	try:
		user_prefix = dist.get_option_dict('install')['prefix'][1]
	except KeyError:
		user_prefix = prefix
	return user_prefix
Some C++ flags correction, because the python expects C sources and not C++ ones :
1
2
3
4
5
6
7
8
9
10
import distutils.sysconfig
cfg_vars = distutils.sysconfig.get_config_vars()
for key, value in cfg_vars.items():
	if type(value) == str:
		value = value.replace(" -Wstrict-prototypes ", " ")
		value = value.replace(" -DNDEBUG ", " ")
		value = value.replace(" -g ", " ")
		cfg_vars[key] = value

extra_compile_args = ['-Wno-invalid-offsetof']
Then, we have to deal with the compiler compatibility (GCC, CLANG, LINUX and OSX) :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
def use_clang():
	if 'gcc' in subprocess.getoutput("echo $CC"):
		return False
	elif 'clang' in subprocess.getoutput("echo $CC"):
		return True
	else:
		if 'not found' in subprocess.getoutput("gcc --version") or 'clang'  in subprocess.getoutput("gcc --version"):
			return True
		else: 			
			return False

if use_clang():
	clangVersion = subprocess.getoutput("clang --version").split()
	i = 0
	while clangVersion[i] != "version":
		i += 1
	clangMainVersion = int(clangVersion[i + 1].split(".")[0])
	print("Find version of Clang ", clangMainVersion)
	if clangMainVersion > 9:
		extra_compile_args.append('-Wno-unused-command-line-argument') #no need for clang 10.0
		extra_compile_args.append("-Wno-injected-class-name")
		extra_compile_args.append("-Wno-macro-redefined")
Now, we create the extension with our file sources and some cmake macro to handle the files positions :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
packageName = 'astericshpc'
ext_modules = [
	Extension(packageName, ['@CMAKE_CURRENT_SOURCE_DIR@/astericshpc.cpp',
		'@CMAKE_CURRENT_SOURCE_DIR@/allocTableWrapper.cpp',
		'@CMAKE_CURRENT_SOURCE_DIR@/allocMatrixWrapper.cpp',
		'@CMAKE_CURRENT_SOURCE_DIR@/timerWrapper.cpp'
	],
	libraries=["asterics_hpc"],
	library_dirs=['@ASTERICS_CPP_LIBRARY_BUILD@'],
	runtime_library_dirs=['@ASTERICS_CPP_LIBRARY_DIR@'],
	extra_link_args=['-Wl,-rpath,@ASTERICS_CPP_LIBRARY_BUILD@'],
	extra_compile_args=extra_compile_args,

	include_dirs=['.',
		'@ASTERICS_HPC_INCLUDE@',
		np.get_include()]
	)
]
Finally, we create the module and cacth exeptions if it failed :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
try:
	setup(name = NAME,
		version=VERSION,
		ext_modules=ext_modules,
		description=DESCRIPTION,
		install_requires=['numpy', 'cython'],
		author=AUTHOR,
		author_email=AUTHOR_EMAIL,
		license=LICENSE,
		url=URL,
		classifiers=[
		'Intended Audience :: Science/Research',
		'License :: OSI Approved ::Cecil-C',
		'Operating System :: OS Independent',
		'Programming Language :: Python :: 3',
		'Topic :: Scientific/Engineering :: Astronomy',
		'Development Status :: 3 - Alpha'],
	)

except Exception as e:
	print(str(e))
	sys.exit(-1)
The full setup.py file :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
'''
	Auteur : Pierre Aubert
	Mail : aubertp7@gmail.com
	Licence : CeCILL-C
'''

from setuptools import setup
from setuptools import Extension
from setuptools.dist import Distribution
from sys import prefix
import subprocess
import numpy as np

try:
	from Cython.Distutils import build_ext
except ImportError:
	use_cython = False
	print('Cython not found')
	raise Exception('Please install Cython on your system')
else:
	use_cython = True

NAME = 'astericshpc'
VERSION = '0.1'
AUTHOR = 'Pierre Aubert'
AUTHOR_EMAIL = 'aubertp7@gmail.com'
URL = ''
DESCRIPTION = 'Basic functions for ASTERICS HPC lecture'
LICENSE = 'CeCILL-C'

def get_prefix():
	"""
	Get prefix from either config file or command line
	:return: str
	prefix install path
	"""
	dist = Distribution()
	dist.parse_config_files()
	dist.parse_command_line()
	try:
		user_prefix = dist.get_option_dict('install')['prefix'][1]
	except KeyError:
		user_prefix = prefix
	return user_prefix

import distutils.sysconfig
cfg_vars = distutils.sysconfig.get_config_vars()
for key, value in cfg_vars.items():
	if type(value) == str:
		value = value.replace(" -Wstrict-prototypes ", " ")
		value = value.replace(" -DNDEBUG ", " ")
		value = value.replace(" -g ", " ")
		cfg_vars[key] = value

extra_compile_args = ['-Wno-invalid-offsetof']

def use_clang():
	if 'gcc' in subprocess.getoutput("echo $CC"):
		return False
	elif 'clang' in subprocess.getoutput("echo $CC"):
		return True
	else:
		if 'not found' in subprocess.getoutput("gcc --version") or 'clang'  in subprocess.getoutput("gcc --version"):
			return True
		else: 			
			return False

if use_clang():
	clangVersion = subprocess.getoutput("clang --version").split()
	i = 0
	while clangVersion[i] != "version":
		i += 1
	clangMainVersion = int(clangVersion[i + 1].split(".")[0])
	print("Find version of Clang ", clangMainVersion)
	if clangMainVersion > 9:
		extra_compile_args.append('-Wno-unused-command-line-argument') #no need for clang 10.0
		extra_compile_args.append("-Wno-injected-class-name")
		extra_compile_args.append("-Wno-macro-redefined")

packageName = 'astericshpc'
ext_modules = [
	Extension(packageName, ['@CMAKE_CURRENT_SOURCE_DIR@/astericshpc.cpp',
		'@CMAKE_CURRENT_SOURCE_DIR@/allocTableWrapper.cpp',
		'@CMAKE_CURRENT_SOURCE_DIR@/allocMatrixWrapper.cpp',
		'@CMAKE_CURRENT_SOURCE_DIR@/timerWrapper.cpp'
	],
	libraries=["asterics_hpc"],
	library_dirs=['@ASTERICS_CPP_LIBRARY_BUILD@'],
	runtime_library_dirs=['@ASTERICS_CPP_LIBRARY_DIR@'],
	extra_link_args=['-Wl,-rpath,@ASTERICS_CPP_LIBRARY_BUILD@'],
	extra_compile_args=extra_compile_args,

	include_dirs=['.',
		'@ASTERICS_HPC_INCLUDE@',
		np.get_include()]
	)
]

try:
	setup(name = NAME,
		version=VERSION,
		ext_modules=ext_modules,
		description=DESCRIPTION,
		install_requires=['numpy', 'cython'],
		author=AUTHOR,
		author_email=AUTHOR_EMAIL,
		license=LICENSE,
		url=URL,
		classifiers=[
		'Intended Audience :: Science/Research',
		'License :: OSI Approved ::Cecil-C',
		'Operating System :: OS Independent',
		'Programming Language :: Python :: 3',
		'Topic :: Scientific/Engineering :: Astronomy',
		'Development Status :: 3 - Alpha'],
	)

except Exception as e:
	print(str(e))
	sys.exit(-1)
You can download it here.