forked from flexible-atomic-code/fac
-
Notifications
You must be signed in to change notification settings - Fork 0
/
setup.py
executable file
·217 lines (199 loc) · 7.02 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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
#
# FAC - Flexible Atomic Code
# Copyright (C) 2001-2015 Ming Feng Gu
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
try:
from setuptools import distutils, setup, Extension
except:
import distutils
from distutils.core import setup, Extension
from distutils import sysconfig
from distutils.ccompiler import CCompiler
from distutils.unixccompiler import UnixCCompiler
from distutils.util import get_platform
import sys
import os
import re
incdir = []
libdir = []
libs = []
extralink = []
rmpylink = []
extracomp = []
bsfac = ''
no_setup = 0
x = sys.argv[2:]
sys.argv = sys.argv[0:2]
CC = None
# Obtain version & consts from faclib/consts.h
def parse_consts(filename=None, vfile=None, cfile=None):
if not filename:
filename = os.path.join(
os.path.dirname(__file__), 'faclib', 'consts.h')
if not vfile:
vfile = os.path.join(
os.path.dirname(__file__), 'python', 'version.py')
if not cfile:
cfile = os.path.join(
os.path.dirname(__file__), 'python', 'consts.py')
consts = {}
with open(filename, 'r') as f:
lines = f.readlines()
for line in lines:
a = line.strip().split()
if len(a) < 3:
continue
if a[0] != '#define':
continue
if a[1] == '_CONSTS_H_':
continue
ak = a[1]
if ak[0] == '_':
ak = ak[1:]
consts[ak] = a[2]
a = open(vfile, 'w')
version = '%s.%s.%s'%(consts['VERSION'], consts['SUBVERSION'], consts['SUBSUBVERSION'])
try:
a.write("version = '%s'\n"%version)
finally:
a.close()
a = open(cfile, 'w')
try:
for k in consts.keys():
v = consts[k]
try:
if v[0] == '(' and v[-1] == ')':
v = v[1:-1]
dv = float(v)
a.write("%s = %s\n"%(k, v))
except:
pass
finally:
a.close()
return version
VERSION = parse_consts()
for s in x:
if (s[0:11] == '-ccompiler='):
CC = s[11:]
elif (s[0:11] == '-extracomp='):
for a in s[11:].split():
if (a[0:2] == '-I'):
incdir.append(a[2:])
elif (a[0:2] == '-L'):
libdir.append(a[2:])
elif (a[0:2] == '-l'):
libs.append(a[2:])
else:
extracomp.append(a)
elif (s[0:11] == '-extralink='):
for a in s[11:].split():
if (a[0:2] == '-I'):
incdir.append(a[2:])
elif (a[0:2] == '-L'):
libdir.append(a[2:])
elif (a[0:2] == '-l'):
libs.append(a[2:])
else:
extralink.append(a)
elif (s[0:10] == '-rmpylink='):
for a in s[10:].split():
rmpylink.append(a)
elif (s[0:11] == '-addpylink='):
for a in s[11:].split():
extralink.append(a)
elif (s[0:6] == '-bsfac'):
bsfac = 'SFAC-%s.%s.tar'%(VERSION, get_platform())
elif (s[0:4] == '-mpy'):
pyinc = sysconfig.get_config_var('INCLUDEPY')
pylib = (sysconfig.get_config_var('LIBPL') +
'/' + sysconfig.get_config_var('LDLIBRARY'))
os.system('cd python; export PYINC=%s PYLIB=%s; make mpy'%(pyinc, pylib))
no_setup = 1
else:
sys.argv.append(s)
# We studied a lot from Intel/pyMIC repoif CC is None:
# https://github.com/intel/pyMIC/blob/master/setup.py
# compiler driver for Anaconda Composer for C/C++
class MyCompiler(UnixCCompiler, object):
"""Compiler wrapper for anaconda gcc_linux-64 """
def set_executables(self, **args):
# basically, we ignore all the tool chain coming in
if CC is not None:
super(self.__class__, self).set_executables(
compiler=CC, compiler_so=CC, linker_exe=CC,
linker_so=CC)
def _fix_lib_args(self, libraries, library_dirs, runtime_library_dirs):
# we need to have this method here, to avoid an endless
# recursion in UnixCCompiler._fix_lib_args.
libraries, library_dirs, runtime_library_dirs = \
CCompiler._fix_lib_args(self, libraries, library_dirs,
runtime_library_dirs)
libdir = sysconfig.get_config_var('LIBDIR')
if runtime_library_dirs and (libdir in runtime_library_dirs):
runtime_library_dirs.remove(libdir)
return libraries, library_dirs, runtime_library_dirs
# register Intel Composer for C/C++ as the main compiler for pyMIC
def my_new_compiler(plat=None, compiler=None, verbose=0, dry_run=0, force=0):
compiler = MyCompiler(verbose, dry_run, force)
return compiler
# override compiler construction
distutils.ccompiler.new_compiler = my_new_compiler
if CC is not None:
ldshared = sysconfig.get_config_var('LDSHARED')
ldshared = ldshared.split(' ')
for a in ldshared[1:]:
skip = 0
for b in rmpylink:
if a == b:
skip = 1
break
if re.match(b, a) != None:
skip = 1
break
if re.match('-Wl,.*', a):
skip = 1
b = '-Wl,.*'
if a == '':
skip = 1
b=''
if skip == 0:
extralink.append(a)
else:
print('skip python link flag: %s %s'%(a, b))
Extensions = [Extension(mod, src, include_dirs=incdir, library_dirs=libdir,
libraries=libs, extra_compile_args=extracomp,
extra_link_args=extralink)
for mod, src in (("pfac.fac", ["python/fac.c"]),
("pfac.crm", ["python/pcrm.c"]),
("pfac.pol", ["python/ppol.c"]),
("pfac.util", ["python/util.c"]))]
if (no_setup == 0):
setup(name="PFAC",
version=VERSION,
package_dir={'pfac': 'python'},
py_modules=['pfac.const', 'pfac.config', 'pfac.table',
'pfac.atom', 'pfac.spm', 'pfac.rfac',
'pfac.version', 'pfac.consts', 'pfac.aa'],
ext_modules=Extensions)
if (sys.argv[1][0:5] == 'bdist' and bsfac != ''):
print('Creating SFAC binary ...')
c = 'cd sfac; mkdir bin;'
c += 'cp sfac bin; cp scrm bin; cp spol bin;'
c += 'tar cvf %s ./bin;'%(bsfac)
c += 'gzip %s;'%(bsfac)
c += 'rm -rf bin;'
c += 'mv %s.gz ../dist/;'%(bsfac)
os.system(c)