-
Notifications
You must be signed in to change notification settings - Fork 3.2k
/
config.py
250 lines (214 loc) 路 8.12 KB
/
config.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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
#!/usr/bin/env python
# For MinGW build requires Python 2.4 or better and win32api.
"""Quick tool to help setup the needed paths and flags
in your Setup file. This will call the appropriate sub-config
scripts automatically.
each platform config file only needs a "main" routine
that returns a list of instances. the instances must
contain the following variables.
name: name of the dependency, as references in Setup (SDL, FONT, etc)
inc_dir: path to include
lib_dir: library directory
lib: name of library to be linked to
found: true if the dep is available
cflags: extra compile flags
"""
try:
import msysio
except ImportError:
import buildconfig.msysio as msysio
import sys, os, shutil, logging
import re
BASE_PATH = '.'
def print_(*args, **kwds):
"""Similar to the Python 3.0 print function"""
# This not only supports MSYS but is also a head start on the move to
# Python 3.0. Also, this function can be overridden for testing.
msysio.print_(*args, **kwds)
def is_msys2():
"""Return true if this in an MSYS2 build"""
return ('MSYSTEM' in os.environ and
re.match(r'MSYS|MINGW.*|CLANG.*|UCRT.*', os.environ['MSYSTEM']))
def is_msys_mingw():
"""Return true if this in an MinGW/MSYS build
The user may prompted for confirmation so only call this function
once.
"""
return False
# if msysio.is_msys():
# return 1
# if ('MINGW_ROOT_DIRECTORY' in os.environ or
# os.path.isfile(mingwcfg.path)):
# return confirm("Is this an mingw/msys build")
# return 0
def prepdep(dep, basepath):
"""add some vars to a dep"""
if dep.libs:
dep.line = dep.name + ' ='
for lib in dep.libs:
dep.line += ' -l' + lib
else:
dep.line = dep.name + ' = -I.'
dep.varname = '$('+dep.name+')'
if not dep.found:
if dep.name == 'SDL': #fudge if this is unfound SDL
dep.line = 'SDL = -I/NEED_INC_PATH_FIX -L/NEED_LIB_PATH_FIX -lSDL'
dep.varname = '$('+dep.name+')'
dep.found = 1
return
incs = []
lids = []
IPREFIX = ' -I$(BASE)' if basepath else ' -I'
LPREFIX = ' -L$(BASE)' if basepath else ' -L'
startind = len(basepath) if basepath else 0
if dep.inc_dir:
if isinstance(dep.inc_dir, str):
incs.append(IPREFIX+dep.inc_dir[startind:])
else:
for dir in dep.inc_dir:
incs.append(IPREFIX+dir[startind:])
if dep.lib_dir:
if isinstance(dep.lib_dir, str):
lids.append(LPREFIX+dep.lib_dir[startind:])
else:
for dir in dep.lib_dir:
lids.append(LPREFIX+dir[startind:])
libs = ''
for lib in dep.libs:
libs += ' -l' + lib
if dep.name.startswith('COPYLIB_'):
dep.line = dep.name + libs + ''.join(lids)
else:
dep.line = dep.name+' =' + ''.join(incs) + ''.join(lids) + ' ' + dep.cflags + libs
def writesetupfile(deps, basepath, additional_lines):
"""create a modified copy of Setup.SDLx.in"""
sdl_setup_filename = os.path.join(BASE_PATH, 'buildconfig',
'Setup.SDL2.in')
with open(sdl_setup_filename, 'r') as origsetup, \
open(os.path.join(BASE_PATH, 'Setup'), 'w') as newsetup:
line = ''
while line.find('#--StartConfig') == -1:
newsetup.write(line)
line = origsetup.readline()
while line.find('#--EndConfig') == -1:
line = origsetup.readline()
if basepath:
newsetup.write('BASE = ' + basepath + '\n')
for d in deps:
newsetup.write(d.line + '\n')
lines = origsetup.readlines()
# overwrite lines which already exist with new ones.
new_lines = []
for l in lines:
addit = 1
parts = l.split()
for al in additional_lines:
aparts = al.split()
if aparts and parts:
if aparts[0] == parts[0]:
#print ('the same!' + repr(aparts) + repr(parts))
#the same, we should not add the old one.
#It will be overwritten by the new one.
addit = 0
if addit:
new_lines.append(l)
new_lines.extend(additional_lines)
lines = new_lines
legalVars = set(d.varname for d in deps)
legalVars.add('$(DEBUG)')
for line in lines:
useit = 1
if not line.startswith('COPYLIB') and not (line and line[0]=='#'):
lineDeps = set(re.findall(r'\$\([a-z0-9\w]+\)', line, re.I))
if lineDeps.difference(legalVars):
newsetup.write('#'+line)
useit = 0
if useit:
for d in deps:
if d.varname in lineDeps and not d.found:
useit = 0
newsetup.write('#'+line)
break
if useit:
legalVars.add('$(%s)' % line.split('=')[0].strip())
if useit:
newsetup.write(line)
def main(auto=False):
additional_platform_setup = []
conan = "-conan" in sys.argv
if '-sdl2' in sys.argv:
sys.argv.remove('-sdl2')
if '-sdl1' in sys.argv:
raise SystemExit("""Building PyGame with SDL1.2 is no longer supported.
Only SDL2 is supported now.""")
kwds = {}
if conan:
print_('Using CONAN configuration...\n')
try:
import config_conan as CFG
except ImportError:
import buildconfig.config_conan as CFG
elif (sys.platform == 'win32' and
(sys.version_info >= (3, 8) and is_msys2())):
print_('Using WINDOWS MSYS2 configuration...\n')
try:
import config_msys2 as CFG
except ImportError:
import buildconfig.config_msys2 as CFG
elif (sys.platform == 'win32' and
# Note that msys builds supported for 2.6 and greater. Use prebuilt.
(sys.version_info >= (2, 6) or not is_msys_mingw())):
print_('Using WINDOWS configuration...\n')
try:
import config_win as CFG
except ImportError:
import buildconfig.config_win as CFG
elif sys.platform == 'win32':
print_('Using WINDOWS mingw/msys configuration...\n')
try:
import config_msys as CFG
except ImportError:
import buildconfig.config_msys as CFG
elif sys.platform == 'darwin':
print_('Using Darwin configuration...\n')
try:
import config_darwin as CFG
except ImportError:
import buildconfig.config_darwin as CFG
else:
print_('Using UNIX configuration...\n')
try:
import config_unix as CFG
except ImportError:
import buildconfig.config_unix as CFG
if sys.platform == 'win32' and sys.version_info > (3,):
additional_platform_setup = open(
os.path.join(BASE_PATH, 'buildconfig', "Setup_Win_Camera.in"), "r"
).readlines()
elif sys.platform == 'darwin':
additional_platform_setup = open(
os.path.join(BASE_PATH, 'buildconfig', "Setup_Darwin.in"), "r"
).readlines()
else:
additional_platform_setup = open(
os.path.join(BASE_PATH, 'buildconfig', "Setup_Unix.in"), "r"
).readlines()
if os.path.isfile('Setup'):
if auto:
logging.info('Backing up existing "Setup" file into Setup.bak')
shutil.copyfile(os.path.join(BASE_PATH, 'Setup'), os.path.join(BASE_PATH, 'Setup.bak'))
deps = CFG.main(**kwds)
if '-conan' in sys.argv:
sys.argv.remove('-conan')
if deps:
basepath = None
for d in deps:
prepdep(d, basepath)
writesetupfile(deps, basepath, additional_platform_setup, **kwds)
print_("""\nIf you get compiler errors during install, double-check
the compiler flags in the "Setup" file.\n""")
else:
print_("""\nThere was an error creating the Setup file, check for errors
or make a copy of "Setup.in" and edit by hand.""")
if __name__ == '__main__':
main()