Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
sagemath
GitHub Repository: sagemath/sagesmc
Path: blob/master/build/pkgs/atlas/configuration.py
8817 views
1
######################################################################
2
# Distributed under the terms of the GNU General Public License (GPL)
3
# http://www.gnu.org/licenses/
4
######################################################################
5
6
from __future__ import print_function
7
8
import platform, os, sys, time, shutil, glob, subprocess
9
10
11
# this dictionary will hold all configuration information
12
conf = dict()
13
14
# Here is a list of keys and (some of) their possible values. First,
15
# strings:
16
#
17
# system: Linux, SunOS, Darwin, FreeBSD, CYGWIN
18
# release: (Version number of the operating system, output of uname -r)
19
# machine: i386, x86_64, # Linux, Darwin, *BSD
20
# sun4u, i86pc # SunOS
21
# processor: i386, x86_64, powerpc, sparc
22
# bits: 32bit, 64bit
23
# fortran: g95, gfortran
24
# ld: GNU, Solaris, Darwin
25
26
# The following pre-defined boolean values are determined from the
27
# strings. The keys are distinguished by a question mark at the
28
# end. If possible use these keys as they guard against typos.
29
#
30
# Linux?, Solaris?, Darwin?, FreeBSD?, CYGWIN? # system
31
# OS_X_Lion? # release
32
# Intel?, PPC?, SPARC? # processor
33
# 64bit?, 32bit? # bits
34
# fortran_g95?, fortran_GNU? # fortran
35
# linker_GNU?, linker_Solaris?, linker_Darwin? # ld
36
37
38
######################################################################
39
### Sanity check
40
######################################################################
41
42
if 'SAGE_LOCAL' not in os.environ:
43
print("SAGE_LOCAL undefined ... exiting")
44
print("Maybe run 'sage -sh'?")
45
sys.exit(1)
46
47
48
######################################################################
49
### Functions
50
######################################################################
51
52
def try_run(command, ignore=False):
53
"""
54
Try to execute ``command`` and return its output as string.
55
56
Return ``None`` if command not found. Return ``None`` if execution
57
fails and ``ignore=False`` (default). Otherwise, return output of
58
``command`` as string. Here, output always means the concatenation
59
of stdout and stderr.
60
"""
61
f = subprocess.Popen(command, shell=True,
62
stdout=subprocess.PIPE, stderr=subprocess.PIPE)
63
result = f.communicate()
64
rc = f.wait()
65
if (not ignore) and (rc!=0):
66
return None
67
# concatenate stdout and stderr
68
return (result[0].strip() + result[1].strip()).decode('utf8')
69
70
71
def cp(source_pattern, destination):
72
"""
73
Portable implementation of "cp -p"
74
"""
75
for filename in glob.iglob(source_pattern):
76
print('Copying', filename, 'to', destination)
77
shutil.copy2(filename, destination)
78
79
80
def ln(source, destination):
81
"""
82
Portable implementation of "ln -sf"
83
"""
84
if os.path.exists(destination):
85
os.remove(destination)
86
print('Linking', source, 'to', destination)
87
os.symlink(source, destination)
88
89
90
def is_executable_file(fpath):
91
"""
92
Check that `fpath` is a file and executable (ie not a directory with +x set)
93
"""
94
return os.path.isfile(fpath) and os.access(fpath, os.X_OK)
95
96
def which(program):
97
"""
98
Portable implementation of "which"
99
"""
100
fpath, fname = os.path.split(program)
101
if fpath != '':
102
return program
103
for path in os.environ['PATH'].split(os.pathsep):
104
f = os.path.join(path, program)
105
if is_executable_file(f):
106
return f
107
raise ValueError('The program '+program+' is not in the $PATH.')
108
109
class edit_in_place(object):
110
"""
111
Edit a file in-place by search/replacing regular expressions.
112
"""
113
def __init__(self, filename):
114
self.filename = filename
115
f = open(filename, 'r')
116
self.data = f.read()
117
f.close()
118
119
def replace(self, find_regex, subs, count=0):
120
self.data = glob.re.sub(find_regex, subs, self.data, count)
121
return self
122
123
def close(self):
124
f = open(self.filename, 'w')
125
f.write(self.data)
126
f.close()
127
128
129
######################################################################
130
### uname
131
######################################################################
132
133
try:
134
conf['system'] = os.environ['UNAME']
135
except KeyError:
136
conf['system'] = platform.system()
137
138
try:
139
conf['release'] = subprocess.check_output(['uname', '-r']).decode('utf8').strip()
140
except subprocess.CalledProcessError:
141
conf['release'] = ""
142
143
conf['Linux?' ] = (conf['system'] == 'Linux')
144
conf['Solaris?'] = (conf['system'] == 'SunOS')
145
conf['Darwin?' ] = (conf['system'] == 'Darwin')
146
conf['OS_X_Lion?'] = (conf['Darwin?'] and conf['release'].startswith('11.'))
147
conf['FreeBSD?'] = (conf['system'] == 'FreeBSD')
148
conf['CYGWIN?' ] = (conf['system'] == 'CYGWIN')
149
150
conf['machine'] = platform.machine()
151
152
conf['processor'] = platform.processor()
153
154
conf['Intel?'] = (platform.machine() in ('i386', 'i486', 'i586', 'i686', 'x86_64',
155
'AMD64', 'i86pc'))
156
conf['IA64?'] = ((platform.processor() == 'ia64')
157
or (platform.machine() == 'ia64'))
158
conf['PPC?'] = (platform.processor() == 'powerpc')
159
conf['SPARC?'] = (platform.processor() == 'sparc')
160
161
conf['generic_binary?'] = (os.environ.get('SAGE_FAT_BINARY', 'no') == 'yes')
162
163
######################################################################
164
### bit width
165
######################################################################
166
167
conf['bits'] = platform.architecture()[0]
168
169
if os.environ.get('SAGE64', 'no') == 'yes':
170
assert conf['bits'] == '64bit', 'SAGE64=yes on a 32-bit system!'
171
conf['64bit?'] = True
172
else:
173
conf['64bit?'] = (conf['bits'] == '64bit')
174
175
conf['32bit?'] = not conf['64bit?']
176
177
178
######################################################################
179
### fortran compiler
180
######################################################################
181
182
fortran_version = try_run('$FC --version')
183
if fortran_version is None:
184
print('Cannot execute fortran compiler ($FC)!')
185
sys.exit(3)
186
if 'G95' in fortran_version:
187
conf['fortran'] = 'g95'
188
elif 'GNU Fortran' in fortran_version:
189
conf['fortran'] = 'gfortran'
190
else:
191
print('Unknown fortran compiler version: '+fortran_version)
192
conf['fortran'] = None
193
194
195
conf['fortran_g95?'] = (conf['fortran'] == 'g95')
196
conf['fortran_GNU?'] = (conf['fortran'] == 'gfortran')
197
198
199
if conf['fortran_g95?']:
200
g95_dir = glob.glob(os.environ['SAGE_LOCAL']+'/lib/gcc-lib/*/*')
201
assert len(g95_dir)==1, 'Could not find G95 dir: '+str(g95_dir)
202
conf['fortran_g95_dir'] = g95_dir[0]
203
204
######################################################################
205
### linker
206
######################################################################
207
208
if conf['Solaris?']:
209
# Note: Solaris linker does not accept --version
210
# ignore error code as 'ld -V' likes to error on some Solaris versions
211
ld_version = try_run('ld -V', ignore=True)
212
else:
213
ld_version = try_run('ld -v')
214
215
if ld_version is None:
216
print('Cannot execute ld!')
217
sys.exit(3)
218
if 'GNU' in ld_version:
219
conf['ld'] = 'GNU'
220
elif 'Solaris' in ld_version:
221
conf['ld'] = 'Solaris'
222
elif 'Apple' in ld_version:
223
conf['ld'] = 'Darwin'
224
else:
225
print('Unknown linker: '+ld_version)
226
conf['ld'] = None
227
228
conf['linker_GNU?'] = (conf['ld'] == 'GNU')
229
conf['linker_Solaris?'] = (conf['ld'] == 'Solaris')
230
conf['linker_Darwin?'] = (conf['ld'] == 'Darwin')
231
232
233
if conf['Solaris?'] and conf['linker_GNU?']:
234
print("WARNING: You are using the GNU linker from 'binutils'")
235
print("Generally it is considered better to use the Sun linker")
236
print("but Sage has been built on Solaris using the GNU linker")
237
print("although that was a very old version of Sage, which")
238
print("never passes all the Sage test-suite.")
239
240
241
242
######################################################################
243
### paths, files, and environment variables
244
######################################################################
245
246
conf['SPKG_DIR'] = os.getcwd()
247
conf['SAGE_LOCAL'] = os.environ['SAGE_LOCAL']
248
249
250
######################################################################
251
### The end: print configuration
252
######################################################################
253
254
print("Configuration:")
255
for key, value in conf.items():
256
print(' '+str(key)+': '+str(value))
257
258
259
260
261
262