1 | # |
---|
2 | # RTEMS Tools Project (http://www.rtems.org/) |
---|
3 | # Copyright 2010-2012 Chris Johns (chrisj@rtems.org) |
---|
4 | # All rights reserved. |
---|
5 | # |
---|
6 | # This file is part of the RTEMS Tools package in 'rtems-tools'. |
---|
7 | # |
---|
8 | # Permission to use, copy, modify, and/or distribute this software for any |
---|
9 | # purpose with or without fee is hereby granted, provided that the above |
---|
10 | # copyright notice and this permission notice appear in all copies. |
---|
11 | # |
---|
12 | # THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES |
---|
13 | # WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF |
---|
14 | # MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR |
---|
15 | # ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
---|
16 | # WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
---|
17 | # ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF |
---|
18 | # OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
---|
19 | |
---|
20 | # |
---|
21 | # This code is based on what ever doco about spec files I could find and |
---|
22 | # RTEMS project's spec files. |
---|
23 | # |
---|
24 | |
---|
25 | import multiprocessing |
---|
26 | import platform |
---|
27 | import pprint |
---|
28 | import os |
---|
29 | |
---|
30 | from . import path |
---|
31 | |
---|
32 | def load(): |
---|
33 | uname = os.uname() |
---|
34 | if uname[4].startswith('arm'): |
---|
35 | cpu = 'arm' |
---|
36 | else: |
---|
37 | cpu = uname[4] |
---|
38 | |
---|
39 | version = uname[2] |
---|
40 | defines = { |
---|
41 | '_ncpus': ('none', 'none', str(multiprocessing.cpu_count())), |
---|
42 | '_os': ('none', 'none', 'linux'), |
---|
43 | '_host': ('triplet', 'required', cpu + '-linux-gnu'), |
---|
44 | '_host_vendor': ('none', 'none', 'gnu'), |
---|
45 | '_host_os': ('none', 'none', 'linux'), |
---|
46 | '_host_os_version': ('none', 'none', version), |
---|
47 | '_host_cpu': ('none', 'none', cpu), |
---|
48 | '_host_alias': ('none', 'none', '%{nil}'), |
---|
49 | '_host_arch': ('none', 'none', cpu), |
---|
50 | '_usr': ('dir', 'required', '/usr'), |
---|
51 | '_var': ('dir', 'required', '/var'), |
---|
52 | '_prefix': ('dir', 'optional', '/opt'), |
---|
53 | '__bzip2': ('exe', 'required', '/usr/bin/bzip2'), |
---|
54 | '__gzip': ('exe', 'required', '/bin/gzip'), |
---|
55 | '__tar': ('exe', 'required', '/bin/tar') |
---|
56 | } |
---|
57 | |
---|
58 | # platform.dist() was removed in Python 3.8 |
---|
59 | if hasattr(platform, 'dist'): |
---|
60 | # Works for LSB distros |
---|
61 | try: |
---|
62 | distro = platform.dist()[0] |
---|
63 | distro_ver = float(platform.dist()[1]) |
---|
64 | except ValueError: |
---|
65 | # Non LSB distro found, use failover" |
---|
66 | pass |
---|
67 | else: |
---|
68 | distro = '' |
---|
69 | |
---|
70 | # Non LSB - fail over to issue |
---|
71 | if distro == '': |
---|
72 | try: |
---|
73 | issue = open('/etc/issue').read() |
---|
74 | distro = issue.split(' ')[0] |
---|
75 | distro_ver = float(issue.split(' ')[2]) |
---|
76 | except: |
---|
77 | pass |
---|
78 | |
---|
79 | # Manage distro aliases |
---|
80 | if distro in ['centos']: |
---|
81 | distro = 'redhat' |
---|
82 | elif distro in ['fedora']: |
---|
83 | if distro_ver < 17: |
---|
84 | distro = 'redhat' |
---|
85 | elif distro in ['centos', 'fedora']: |
---|
86 | distro = 'redhat' |
---|
87 | elif distro in ['Ubuntu', 'ubuntu', 'MX', 'LinuxMint', 'linuxmint']: |
---|
88 | distro = 'debian' |
---|
89 | elif distro in ['Arch']: |
---|
90 | distro = 'arch' |
---|
91 | elif distro in ['SuSE']: |
---|
92 | distro = 'suse' |
---|
93 | |
---|
94 | variations = { |
---|
95 | 'debian' : { '__bzip2': ('exe', 'required', '/bin/bzip2'), |
---|
96 | '__chgrp': ('exe', 'required', '/bin/chgrp'), |
---|
97 | '__chown': ('exe', 'required', '/bin/chown'), |
---|
98 | '__grep': ('exe', 'required', '/bin/grep'), |
---|
99 | '__sed': ('exe', 'required', '/bin/sed') }, |
---|
100 | 'redhat' : { '__bzip2': ('exe', 'required', '/bin/bzip2'), |
---|
101 | '__chgrp': ('exe', 'required', '/bin/chgrp'), |
---|
102 | '__chown': ('exe', 'required', '/bin/chown'), |
---|
103 | '__install_info': ('exe', 'required', '/sbin/install-info'), |
---|
104 | '__grep': ('exe', 'required', '/bin/grep'), |
---|
105 | '__sed': ('exe', 'required', '/bin/sed'), |
---|
106 | '__touch': ('exe', 'required', '/bin/touch') }, |
---|
107 | 'fedora' : { '__chown': ('exe', 'required', '/usr/bin/chown'), |
---|
108 | '__install_info': ('exe', 'required', '/usr/sbin/install-info') }, |
---|
109 | 'arch' : { '__gzip': ('exe', 'required', '/usr/bin/gzip'), |
---|
110 | '__chown': ('exe', 'required', '/usr/bin/chown') }, |
---|
111 | 'suse' : { '__chgrp': ('exe', 'required', '/usr/bin/chgrp'), |
---|
112 | '__chown': ('exe', 'required', '/usr/sbin/chown') }, |
---|
113 | 'gentoo' : { '__bzip2': ('exe', 'required', '/bin/bzip2'), |
---|
114 | '__chgrp': ('exe', 'required', '/bin/chgrp'), |
---|
115 | '__chown': ('exe', 'required', '/bin/chown'), |
---|
116 | '__gzip': ('exe', 'required', '/bin/gzip'), |
---|
117 | '__grep': ('exe', 'required', '/bin/grep'), |
---|
118 | '__sed': ('exe', 'required', '/bin/sed') }, |
---|
119 | } |
---|
120 | |
---|
121 | if distro in variations: |
---|
122 | for v in variations[distro]: |
---|
123 | if path.exists(variations[distro][v][2]): |
---|
124 | defines[v] = variations[distro][v] |
---|
125 | |
---|
126 | defines['_build'] = defines['_host'] |
---|
127 | defines['_build_vendor'] = defines['_host_vendor'] |
---|
128 | defines['_build_os'] = defines['_host_os'] |
---|
129 | defines['_build_cpu'] = defines['_host_cpu'] |
---|
130 | defines['_build_alias'] = defines['_host_alias'] |
---|
131 | defines['_build_arch'] = defines['_host_arch'] |
---|
132 | |
---|
133 | return defines |
---|
134 | |
---|
135 | if __name__ == '__main__': |
---|
136 | pprint.pprint(load()) |
---|