-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathsetup.py
executable file
·175 lines (138 loc) · 5.25 KB
/
setup.py
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
#!/usr/bin/env python
'''
GFDM Cython kernels
~~~~~~~
This setup.py file exists to assist with Cython install.
It may be fragile in case assumptions and dependencies are not met.
It depends on various tools!
Python: setuptools, Cython, subprocess, shlex, inspect, os
C/C++: libfftw3-dev, libvolk-dev (or VOLK from github)
Cython files must be located in ./python/cython!
C/C++ files must be located in ./lib and ./include/gfdm
Additional Python modules must be in python/pygfdm!
This installs 2 additional Python modules!
pygfdm: Python GFDM tools
cgfdm: Cython wrapper to fast C++ GFDM implementation.
'''
from __future__ import print_function, division
from setuptools import setup
from setuptools.extension import Extension
from Cython.Build import cythonize
import subprocess
import inspect
import os
import sys
pv = sys.version_info
print('Running script with Python {}.{}'.format(pv.major, pv.minor))
# the actual classes to be compiled!
cython_targets = ["modulator_kernel_cc", "add_cyclic_prefix_cc",
"resource_mapper_kernel_cc", "receiver_kernel_cc",
"resource_demapper_kernel_cc", "gfdm_kernel_utils",
"preamble_channel_estimator_cc"]
# assume those are the only additional libraries to link against.
libraries = ['fftw3f', 'volk']
deps = [
'numpy',
'matplotlib',
'scipy',
'scikit-commpy',
]
def run_pkg_config(opts, params):
if pv.major < 3:
return subprocess.check_output(("pkg-config", "--" + opts, params))
else:
return subprocess.check_output(("pkg-config", "--" + opts,
params)).decode()
def sanitize_pkg_output(pkg_str):
ps = pkg_str.split()
ps = [i for i in ps if not i == '\n']
return ps
def get_pkg_option(opt, name):
ps = run_pkg_config(opt, name)
return sanitize_pkg_output(ps)
def get_library_shared_objects(lib):
lib_dirs = []
lib_arg = get_pkg_option('libs', lib)
for la in lib_arg:
if '-L' in la:
lib_dirs += [la.replace('-L', ''), ]
return lib_dirs
def find_shared_objects(libraries):
lib_dirs = []
for l in libraries:
lib_dirs += get_library_shared_objects(l)
return lib_dirs
def get_library_headers(lib):
shared_dirs = []
comp_arg = get_pkg_option('cflags', lib)
for c in comp_arg:
shared_dirs += [c.replace('-I', ''), ]
return shared_dirs
def find_headers(libraries):
shared_dirs = []
for l in libraries:
shared_dirs += get_library_headers(l)
return shared_dirs
def find_source_files(project_top_level_dir, targets):
source_files = []
for target in targets:
header_file = os.path.join(project_top_level_dir, 'include/gfdm', target + '.h')
source_file = os.path.join(project_top_level_dir, 'lib', target + '.cc')
if not os.path.exists(header_file) or not os.path.exists(source_file):
raise ValueError('ERROR: Could not find header(' + header_file + ') and source(' + source_file + ') files!')
source_files += [source_file, ]
return source_files
def get_project_top_level_dir():
own_file_path = inspect.getfile(inspect.currentframe())
own_file_path = os.path.abspath(own_file_path) # make sure full path is available!
# print 'own_file_path: ', own_file_path
# print os.path.abspath(own_file_path)
path, filename = os.path.split(own_file_path)
if not filename == 'setup.py':
raise ValueError('Assumption FAILED: assumed to run setup.py in order to compile Cython module.')
if not path.endswith('gr-gfdm/python'):
print(path)
raise ValueError("Assumption FAILED: assumed project structure is 'gr-gfdm/cython'")
return path.strip('python')
def get_available_dependencies(deps):
avail = []
for d in deps:
try:
__import__(d)
print(d)
avail += [d, ]
except ImportError:
print('Python module:', d, 'not available!')
return avail
project_top_level_dir = get_project_top_level_dir()
print('project TOP level directory:', project_top_level_dir)
source_files = [os.path.join(project_top_level_dir, 'python/cython/cgfdm.pyx'), ]
source_files += find_source_files(project_top_level_dir, cython_targets)
print(source_files)
include_dirs = [os.path.join(project_top_level_dir, 'include/gfdm'), os.path.join(project_top_level_dir, 'include'), ]
include_dirs += find_headers(libraries)
library_dirs = find_shared_objects(libraries)
print('include_dirs: ', include_dirs)
print('library_dirs: ', library_dirs)
ext_modules = [
Extension("cgfdm",
include_dirs=include_dirs,
library_dirs=library_dirs,
sources=source_files,
libraries=libraries,
language="c++",
)
]
# setuptools Doc: https://pythonhosted.org/an_example_pypi_project/setuptools.html
setup(
name="python-gfdm",
version="0.0.1",
author="Johannes Demel, Andrej Rode",
author_email="[email protected], [email protected]",
description='Python GFDM utils',
keywords="GFDM Python Cython",
packages=['pygfdm'],
install_requires=deps,
ext_modules=cythonize(ext_modules),
# ext_modules=cythonize(ext_modules, annotate=True) # this produces an .HTML which helps identify function overhead.
)