Fixed ninja binary location logic to use ninja.BIN_DIR. Previous logic no longer...
[scons.git] / SCons / Tool / rpmutils.py
blobcc1cc23ba5d154a13572088874a0dcc315d27ada
1 """SCons.Tool.rpmutils.py
3 RPM specific helper routines for general usage in the test framework
4 and SCons core modules.
6 Since we check for the RPM package target name in several places,
7 we have to know which machine/system name RPM will use for the current
8 hardware setup. The following dictionaries and functions try to
9 mimic the exact naming rules of the RPM source code.
10 They were directly derived from the file "rpmrc.in" of the version
11 rpm-4.9.1.3. For updating to a more recent version of RPM, this Python
12 script can be used standalone. The usage() function below shows the
13 exact syntax.
15 """
17 # __COPYRIGHT__
19 # Permission is hereby granted, free of charge, to any person obtaining
20 # a copy of this software and associated documentation files (the
21 # "Software"), to deal in the Software without restriction, including
22 # without limitation the rights to use, copy, modify, merge, publish,
23 # distribute, sublicense, and/or sell copies of the Software, and to
24 # permit persons to whom the Software is furnished to do so, subject to
25 # the following conditions:
27 # The above copyright notice and this permission notice shall be included
28 # in all copies or substantial portions of the Software.
30 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY
31 # KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
32 # WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
33 # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
34 # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
35 # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
36 # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
37 __revision__ = "__FILE__ __REVISION__ __DATE__ __DEVELOPER__"
40 import platform
41 import subprocess
43 import SCons.Util
45 # Start of rpmrc dictionaries (Marker, don't change or remove!)
46 os_canon = {
47 'AIX' : ['AIX','5'],
48 'AmigaOS' : ['AmigaOS','5'],
49 'BSD_OS' : ['bsdi','12'],
50 'CYGWIN32_95' : ['cygwin32','15'],
51 'CYGWIN32_NT' : ['cygwin32','14'],
52 'Darwin' : ['darwin','21'],
53 'FreeBSD' : ['FreeBSD','8'],
54 'HP-UX' : ['hpux10','6'],
55 'IRIX' : ['Irix','2'],
56 'IRIX64' : ['Irix64','10'],
57 'Linux' : ['Linux','1'],
58 'Linux/390' : ['OS/390','20'],
59 'Linux/ESA' : ['VM/ESA','20'],
60 'MacOSX' : ['macosx','21'],
61 'MiNT' : ['FreeMiNT','17'],
62 'NEXTSTEP' : ['NextStep','11'],
63 'OS/390' : ['OS/390','18'],
64 'OSF1' : ['osf1','7'],
65 'SCO_SV' : ['SCO_SV3.2v5.0.2','9'],
66 'SunOS4' : ['SunOS','4'],
67 'SunOS5' : ['solaris','3'],
68 'UNIX_SV' : ['MP_RAS','16'],
69 'VM/ESA' : ['VM/ESA','19'],
70 'machten' : ['machten','13'],
71 'osf3.2' : ['osf1','7'],
72 'osf4.0' : ['osf1','7'],
75 buildarch_compat = {
76 'alpha' : ['noarch'],
77 'alphaev5' : ['alpha'],
78 'alphaev56' : ['alphaev5'],
79 'alphaev6' : ['alphapca56'],
80 'alphaev67' : ['alphaev6'],
81 'alphapca56' : ['alphaev56'],
82 'amd64' : ['x86_64'],
83 'armv3l' : ['noarch'],
84 'armv4b' : ['noarch'],
85 'armv4l' : ['armv3l'],
86 'armv4tl' : ['armv4l'],
87 'armv5tejl' : ['armv5tel'],
88 'armv5tel' : ['armv4tl'],
89 'armv6l' : ['armv5tejl'],
90 'armv7l' : ['armv6l'],
91 'atariclone' : ['m68kmint','noarch'],
92 'atarist' : ['m68kmint','noarch'],
93 'atariste' : ['m68kmint','noarch'],
94 'ataritt' : ['m68kmint','noarch'],
95 'athlon' : ['i686'],
96 'falcon' : ['m68kmint','noarch'],
97 'geode' : ['i586'],
98 'hades' : ['m68kmint','noarch'],
99 'hppa1.0' : ['parisc'],
100 'hppa1.1' : ['hppa1.0'],
101 'hppa1.2' : ['hppa1.1'],
102 'hppa2.0' : ['hppa1.2'],
103 'i386' : ['noarch','fat'],
104 'i486' : ['i386'],
105 'i586' : ['i486'],
106 'i686' : ['i586'],
107 'ia32e' : ['x86_64'],
108 'ia64' : ['noarch'],
109 'm68k' : ['noarch'],
110 'milan' : ['m68kmint','noarch'],
111 'mips' : ['noarch'],
112 'mipsel' : ['noarch'],
113 'parisc' : ['noarch'],
114 'pentium3' : ['i686'],
115 'pentium4' : ['pentium3'],
116 'ppc' : ['noarch','fat'],
117 'ppc32dy4' : ['noarch'],
118 'ppc64' : ['noarch','fat'],
119 'ppc64iseries' : ['ppc64'],
120 'ppc64pseries' : ['ppc64'],
121 'ppc8260' : ['noarch'],
122 'ppc8560' : ['noarch'],
123 'ppciseries' : ['noarch'],
124 'ppcpseries' : ['noarch'],
125 's390' : ['noarch'],
126 's390x' : ['noarch'],
127 'sh3' : ['noarch'],
128 'sh4' : ['noarch'],
129 'sh4a' : ['sh4'],
130 'sparc' : ['noarch'],
131 'sparc64' : ['sparcv9v'],
132 'sparc64v' : ['sparc64'],
133 'sparcv8' : ['sparc'],
134 'sparcv9' : ['sparcv8'],
135 'sparcv9v' : ['sparcv9'],
136 'sun4c' : ['noarch'],
137 'sun4d' : ['noarch'],
138 'sun4m' : ['noarch'],
139 'sun4u' : ['noarch'],
140 'x86_64' : ['noarch'],
143 os_compat = {
144 'BSD_OS' : ['bsdi'],
145 'Darwin' : ['MacOSX'],
146 'FreeMiNT' : ['mint','MiNT','TOS'],
147 'IRIX64' : ['IRIX'],
148 'MiNT' : ['FreeMiNT','mint','TOS'],
149 'TOS' : ['FreeMiNT','MiNT','mint'],
150 'bsdi4.0' : ['bsdi'],
151 'hpux10.00' : ['hpux9.07'],
152 'hpux10.01' : ['hpux10.00'],
153 'hpux10.10' : ['hpux10.01'],
154 'hpux10.20' : ['hpux10.10'],
155 'hpux10.30' : ['hpux10.20'],
156 'hpux11.00' : ['hpux10.30'],
157 'hpux9.05' : ['hpux9.04'],
158 'hpux9.07' : ['hpux9.05'],
159 'mint' : ['FreeMiNT','MiNT','TOS'],
160 'ncr-sysv4.3' : ['ncr-sysv4.2'],
161 'osf4.0' : ['osf3.2','osf1'],
162 'solaris2.4' : ['solaris2.3'],
163 'solaris2.5' : ['solaris2.3','solaris2.4'],
164 'solaris2.6' : ['solaris2.3','solaris2.4','solaris2.5'],
165 'solaris2.7' : ['solaris2.3','solaris2.4','solaris2.5','solaris2.6'],
168 arch_compat = {
169 'alpha' : ['axp','noarch'],
170 'alphaev5' : ['alpha'],
171 'alphaev56' : ['alphaev5'],
172 'alphaev6' : ['alphapca56'],
173 'alphaev67' : ['alphaev6'],
174 'alphapca56' : ['alphaev56'],
175 'amd64' : ['x86_64','athlon','noarch'],
176 'armv3l' : ['noarch'],
177 'armv4b' : ['noarch'],
178 'armv4l' : ['armv3l'],
179 'armv4tl' : ['armv4l'],
180 'armv5tejl' : ['armv5tel'],
181 'armv5tel' : ['armv4tl'],
182 'armv6l' : ['armv5tejl'],
183 'armv7l' : ['armv6l'],
184 'atariclone' : ['m68kmint','noarch'],
185 'atarist' : ['m68kmint','noarch'],
186 'atariste' : ['m68kmint','noarch'],
187 'ataritt' : ['m68kmint','noarch'],
188 'athlon' : ['i686'],
189 'falcon' : ['m68kmint','noarch'],
190 'geode' : ['i586'],
191 'hades' : ['m68kmint','noarch'],
192 'hppa1.0' : ['parisc'],
193 'hppa1.1' : ['hppa1.0'],
194 'hppa1.2' : ['hppa1.1'],
195 'hppa2.0' : ['hppa1.2'],
196 'i370' : ['noarch'],
197 'i386' : ['noarch','fat'],
198 'i486' : ['i386'],
199 'i586' : ['i486'],
200 'i686' : ['i586'],
201 'ia32e' : ['x86_64','athlon','noarch'],
202 'ia64' : ['noarch'],
203 'milan' : ['m68kmint','noarch'],
204 'mips' : ['noarch'],
205 'mipsel' : ['noarch'],
206 'osfmach3_i386' : ['i486'],
207 'osfmach3_i486' : ['i486','osfmach3_i386'],
208 'osfmach3_i586' : ['i586','osfmach3_i486'],
209 'osfmach3_i686' : ['i686','osfmach3_i586'],
210 'osfmach3_ppc' : ['ppc'],
211 'parisc' : ['noarch'],
212 'pentium3' : ['i686'],
213 'pentium4' : ['pentium3'],
214 'powerpc' : ['ppc'],
215 'powerppc' : ['ppc'],
216 'ppc' : ['rs6000'],
217 'ppc32dy4' : ['ppc'],
218 'ppc64' : ['ppc'],
219 'ppc64iseries' : ['ppc64'],
220 'ppc64pseries' : ['ppc64'],
221 'ppc8260' : ['ppc'],
222 'ppc8560' : ['ppc'],
223 'ppciseries' : ['ppc'],
224 'ppcpseries' : ['ppc'],
225 'rs6000' : ['noarch','fat'],
226 's390' : ['noarch'],
227 's390x' : ['s390','noarch'],
228 'sh3' : ['noarch'],
229 'sh4' : ['noarch'],
230 'sh4a' : ['sh4'],
231 'sparc' : ['noarch'],
232 'sparc64' : ['sparcv9'],
233 'sparc64v' : ['sparc64'],
234 'sparcv8' : ['sparc'],
235 'sparcv9' : ['sparcv8'],
236 'sparcv9v' : ['sparcv9'],
237 'sun4c' : ['sparc'],
238 'sun4d' : ['sparc'],
239 'sun4m' : ['sparc'],
240 'sun4u' : ['sparc64'],
241 'x86_64' : ['amd64','athlon','noarch'],
244 buildarchtranslate = {
245 'alphaev5' : ['alpha'],
246 'alphaev56' : ['alpha'],
247 'alphaev6' : ['alpha'],
248 'alphaev67' : ['alpha'],
249 'alphapca56' : ['alpha'],
250 'amd64' : ['x86_64'],
251 'armv3l' : ['armv3l'],
252 'armv4b' : ['armv4b'],
253 'armv4l' : ['armv4l'],
254 'armv4tl' : ['armv4tl'],
255 'armv5tejl' : ['armv5tejl'],
256 'armv5tel' : ['armv5tel'],
257 'armv6l' : ['armv6l'],
258 'armv7l' : ['armv7l'],
259 'atariclone' : ['m68kmint'],
260 'atarist' : ['m68kmint'],
261 'atariste' : ['m68kmint'],
262 'ataritt' : ['m68kmint'],
263 'athlon' : ['i386'],
264 'falcon' : ['m68kmint'],
265 'geode' : ['i386'],
266 'hades' : ['m68kmint'],
267 'i386' : ['i386'],
268 'i486' : ['i386'],
269 'i586' : ['i386'],
270 'i686' : ['i386'],
271 'ia32e' : ['x86_64'],
272 'ia64' : ['ia64'],
273 'milan' : ['m68kmint'],
274 'osfmach3_i386' : ['i386'],
275 'osfmach3_i486' : ['i386'],
276 'osfmach3_i586' : ['i386'],
277 'osfmach3_i686' : ['i386'],
278 'osfmach3_ppc' : ['ppc'],
279 'pentium3' : ['i386'],
280 'pentium4' : ['i386'],
281 'powerpc' : ['ppc'],
282 'powerppc' : ['ppc'],
283 'ppc32dy4' : ['ppc'],
284 'ppc64iseries' : ['ppc64'],
285 'ppc64pseries' : ['ppc64'],
286 'ppc8260' : ['ppc'],
287 'ppc8560' : ['ppc'],
288 'ppciseries' : ['ppc'],
289 'ppcpseries' : ['ppc'],
290 's390' : ['s390'],
291 's390x' : ['s390x'],
292 'sh3' : ['sh3'],
293 'sh4' : ['sh4'],
294 'sh4a' : ['sh4'],
295 'sparc64v' : ['sparc64'],
296 'sparcv8' : ['sparc'],
297 'sparcv9' : ['sparc'],
298 'sparcv9v' : ['sparc'],
299 'sun4c' : ['sparc'],
300 'sun4d' : ['sparc'],
301 'sun4m' : ['sparc'],
302 'sun4u' : ['sparc64'],
303 'x86_64' : ['x86_64'],
306 optflags = {
307 'alpha' : ['-O2','-g','-mieee'],
308 'alphaev5' : ['-O2','-g','-mieee','-mtune=ev5'],
309 'alphaev56' : ['-O2','-g','-mieee','-mtune=ev56'],
310 'alphaev6' : ['-O2','-g','-mieee','-mtune=ev6'],
311 'alphaev67' : ['-O2','-g','-mieee','-mtune=ev67'],
312 'alphapca56' : ['-O2','-g','-mieee','-mtune=pca56'],
313 'amd64' : ['-O2','-g'],
314 'armv3l' : ['-O2','-g','-march=armv3'],
315 'armv4b' : ['-O2','-g','-march=armv4'],
316 'armv4l' : ['-O2','-g','-march=armv4'],
317 'armv4tl' : ['-O2','-g','-march=armv4t'],
318 'armv5tejl' : ['-O2','-g','-march=armv5te'],
319 'armv5tel' : ['-O2','-g','-march=armv5te'],
320 'armv6l' : ['-O2','-g','-march=armv6'],
321 'armv7l' : ['-O2','-g','-march=armv7'],
322 'atariclone' : ['-O2','-g','-fomit-frame-pointer'],
323 'atarist' : ['-O2','-g','-fomit-frame-pointer'],
324 'atariste' : ['-O2','-g','-fomit-frame-pointer'],
325 'ataritt' : ['-O2','-g','-fomit-frame-pointer'],
326 'athlon' : ['-O2','-g','-march=athlon'],
327 'falcon' : ['-O2','-g','-fomit-frame-pointer'],
328 'fat' : ['-O2','-g','-arch','i386','-arch','ppc'],
329 'geode' : ['-Os','-g','-m32','-march=geode'],
330 'hades' : ['-O2','-g','-fomit-frame-pointer'],
331 'hppa1.0' : ['-O2','-g','-mpa-risc-1-0'],
332 'hppa1.1' : ['-O2','-g','-mpa-risc-1-0'],
333 'hppa1.2' : ['-O2','-g','-mpa-risc-1-0'],
334 'hppa2.0' : ['-O2','-g','-mpa-risc-1-0'],
335 'i386' : ['-O2','-g','-march=i386','-mtune=i686'],
336 'i486' : ['-O2','-g','-march=i486'],
337 'i586' : ['-O2','-g','-march=i586'],
338 'i686' : ['-O2','-g','-march=i686'],
339 'ia32e' : ['-O2','-g'],
340 'ia64' : ['-O2','-g'],
341 'm68k' : ['-O2','-g','-fomit-frame-pointer'],
342 'milan' : ['-O2','-g','-fomit-frame-pointer'],
343 'mips' : ['-O2','-g'],
344 'mipsel' : ['-O2','-g'],
345 'parisc' : ['-O2','-g','-mpa-risc-1-0'],
346 'pentium3' : ['-O2','-g','-march=pentium3'],
347 'pentium4' : ['-O2','-g','-march=pentium4'],
348 'ppc' : ['-O2','-g','-fsigned-char'],
349 'ppc32dy4' : ['-O2','-g','-fsigned-char'],
350 'ppc64' : ['-O2','-g','-fsigned-char'],
351 'ppc8260' : ['-O2','-g','-fsigned-char'],
352 'ppc8560' : ['-O2','-g','-fsigned-char'],
353 'ppciseries' : ['-O2','-g','-fsigned-char'],
354 'ppcpseries' : ['-O2','-g','-fsigned-char'],
355 's390' : ['-O2','-g'],
356 's390x' : ['-O2','-g'],
357 'sh3' : ['-O2','-g'],
358 'sh4' : ['-O2','-g','-mieee'],
359 'sh4a' : ['-O2','-g','-mieee'],
360 'sparc' : ['-O2','-g','-m32','-mtune=ultrasparc'],
361 'sparc64' : ['-O2','-g','-m64','-mtune=ultrasparc'],
362 'sparc64v' : ['-O2','-g','-m64','-mtune=niagara'],
363 'sparcv8' : ['-O2','-g','-m32','-mtune=ultrasparc','-mv8'],
364 'sparcv9' : ['-O2','-g','-m32','-mtune=ultrasparc'],
365 'sparcv9v' : ['-O2','-g','-m32','-mtune=niagara'],
366 'x86_64' : ['-O2','-g'],
369 arch_canon = {
370 'IP' : ['sgi','7'],
371 'alpha' : ['alpha','2'],
372 'alphaev5' : ['alphaev5','2'],
373 'alphaev56' : ['alphaev56','2'],
374 'alphaev6' : ['alphaev6','2'],
375 'alphaev67' : ['alphaev67','2'],
376 'alphapca56' : ['alphapca56','2'],
377 'amd64' : ['amd64','1'],
378 'armv3l' : ['armv3l','12'],
379 'armv4b' : ['armv4b','12'],
380 'armv4l' : ['armv4l','12'],
381 'armv5tejl' : ['armv5tejl','12'],
382 'armv5tel' : ['armv5tel','12'],
383 'armv6l' : ['armv6l','12'],
384 'armv7l' : ['armv7l','12'],
385 'atariclone' : ['m68kmint','13'],
386 'atarist' : ['m68kmint','13'],
387 'atariste' : ['m68kmint','13'],
388 'ataritt' : ['m68kmint','13'],
389 'athlon' : ['athlon','1'],
390 'falcon' : ['m68kmint','13'],
391 'geode' : ['geode','1'],
392 'hades' : ['m68kmint','13'],
393 'i370' : ['i370','14'],
394 'i386' : ['i386','1'],
395 'i486' : ['i486','1'],
396 'i586' : ['i586','1'],
397 'i686' : ['i686','1'],
398 'ia32e' : ['ia32e','1'],
399 'ia64' : ['ia64','9'],
400 'm68k' : ['m68k','6'],
401 'm68kmint' : ['m68kmint','13'],
402 'milan' : ['m68kmint','13'],
403 'mips' : ['mips','4'],
404 'mipsel' : ['mipsel','11'],
405 'pentium3' : ['pentium3','1'],
406 'pentium4' : ['pentium4','1'],
407 'ppc' : ['ppc','5'],
408 'ppc32dy4' : ['ppc32dy4','5'],
409 'ppc64' : ['ppc64','16'],
410 'ppc64iseries' : ['ppc64iseries','16'],
411 'ppc64pseries' : ['ppc64pseries','16'],
412 'ppc8260' : ['ppc8260','5'],
413 'ppc8560' : ['ppc8560','5'],
414 'ppciseries' : ['ppciseries','5'],
415 'ppcpseries' : ['ppcpseries','5'],
416 'rs6000' : ['rs6000','8'],
417 's390' : ['s390','14'],
418 's390x' : ['s390x','15'],
419 'sh' : ['sh','17'],
420 'sh3' : ['sh3','17'],
421 'sh4' : ['sh4','17'],
422 'sh4a' : ['sh4a','17'],
423 'sparc' : ['sparc','3'],
424 'sparc64' : ['sparc64','2'],
425 'sparc64v' : ['sparc64v','2'],
426 'sparcv8' : ['sparcv8','3'],
427 'sparcv9' : ['sparcv9','3'],
428 'sparcv9v' : ['sparcv9v','3'],
429 'sun4' : ['sparc','3'],
430 'sun4c' : ['sparc','3'],
431 'sun4d' : ['sparc','3'],
432 'sun4m' : ['sparc','3'],
433 'sun4u' : ['sparc64','2'],
434 'x86_64' : ['x86_64','1'],
435 'xtensa' : ['xtensa','18'],
438 # End of rpmrc dictionaries (Marker, don't change or remove!)
440 def defaultMachine(use_rpm_default: bool=True):
441 """ Return the canonicalized machine name. """
443 if use_rpm_default:
444 try:
445 # This should be the most reliable way to get the default arch
446 rmachine = subprocess.check_output(['rpm', '--eval=%_target_cpu'], shell=False).rstrip()
447 rmachine = SCons.Util.to_str(rmachine)
448 except Exception as e:
449 # Something went wrong, try again by looking up platform.machine()
450 return defaultMachine(False)
451 else:
452 rmachine = platform.machine()
454 # Try to lookup the string in the canon table
455 if rmachine in arch_canon:
456 rmachine = arch_canon[rmachine][0]
458 return rmachine
460 def defaultSystem():
461 """ Return the canonicalized system name. """
462 rsystem = platform.system()
464 # Try to lookup the string in the canon tables
465 if rsystem in os_canon:
466 rsystem = os_canon[rsystem][0]
468 return rsystem
470 def defaultNames():
471 """ Return the canonicalized machine and system name. """
472 return defaultMachine(), defaultSystem()
474 def updateRpmDicts(rpmrc, pyfile) -> None:
475 """ Read the given rpmrc file with RPM definitions and update the
476 info dictionaries in the file pyfile with it.
477 The arguments will usually be 'rpmrc.in' from a recent RPM source
478 tree, and 'rpmutils.py' referring to this script itself.
479 See also usage() below.
481 try:
482 # Read old rpmutils.py file
483 with open(pyfile) as f:
484 oldpy = f.readlines()
485 # Read current rpmrc.in file
486 with open(rpmrc) as f:
487 rpm = f.readlines()
488 # Parse for data
489 data = {}
490 # Allowed section names that get parsed
491 sections = ['optflags',
492 'arch_canon',
493 'os_canon',
494 'buildarchtranslate',
495 'arch_compat',
496 'os_compat',
497 'buildarch_compat']
498 for l in rpm:
499 l = l.rstrip('\n').replace(':',' ')
500 # Skip comments
501 if l.lstrip().startswith('#'):
502 continue
503 tokens = l.strip().split()
504 if len(tokens):
505 key = tokens[0]
506 if key in sections:
507 # Have we met this section before?
508 if tokens[0] not in data:
509 # No, so insert it
510 data[key] = {}
511 # Insert data
512 data[key][tokens[1]] = tokens[2:]
513 # Write new rpmutils.py file
514 with open(pyfile,"w") as out:
515 pm = 0
516 for l in oldpy:
517 if pm:
518 if l.startswith('# End of rpmrc dictionaries'):
519 pm = 0
520 out.write(l)
521 else:
522 out.write(l)
523 if l.startswith('# Start of rpmrc dictionaries'):
524 pm = 1
525 # Write data sections to single dictionaries
526 for key, entries in data.items():
527 out.write("%s = {\n" % key)
528 for arch in sorted(entries.keys()):
529 out.write(" '%s' : ['%s'],\n" % (arch, "','".join(entries[arch])))
530 out.write("}\n\n")
531 except:
532 pass
534 def usage() -> None:
535 print("rpmutils.py rpmrc.in rpmutils.py")
537 def main() -> None:
538 import sys
540 if len(sys.argv) < 3:
541 usage()
542 sys.exit(0)
543 updateRpmDicts(sys.argv[1], sys.argv[2])
545 if __name__ == "__main__":
546 main()