1 | # |
---|
2 | # RTEMS Tools Project (http://www.rtems.org/) |
---|
3 | # Copyright 2010-2017 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 | # Redistribution and use in source and binary forms, with or without |
---|
9 | # modification, are permitted provided that the following conditions are met: |
---|
10 | # |
---|
11 | # 1. Redistributions of source code must retain the above copyright notice, |
---|
12 | # this list of conditions and the following disclaimer. |
---|
13 | # |
---|
14 | # 2. Redistributions in binary form must reproduce the above copyright notice, |
---|
15 | # this list of conditions and the following disclaimer in the documentation |
---|
16 | # and/or other materials provided with the distribution. |
---|
17 | # |
---|
18 | # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
---|
19 | # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
---|
20 | # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
---|
21 | # ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE |
---|
22 | # LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
---|
23 | # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
---|
24 | # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
---|
25 | # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
---|
26 | # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
---|
27 | # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
---|
28 | # POSSIBILITY OF SUCH DAMAGE. |
---|
29 | # |
---|
30 | |
---|
31 | # |
---|
32 | # This code is based on what ever doco about spec files I could find and |
---|
33 | # RTEMS project's spec files. |
---|
34 | # |
---|
35 | |
---|
36 | import os |
---|
37 | import platform |
---|
38 | |
---|
39 | # |
---|
40 | # Support to handle use in a package and as a unit test. |
---|
41 | # If there is a better way to let us know. |
---|
42 | # |
---|
43 | try: |
---|
44 | from . import execute |
---|
45 | from . import path |
---|
46 | except (ValueError, SystemError): |
---|
47 | import execute |
---|
48 | import path |
---|
49 | |
---|
50 | def cpus(): |
---|
51 | processors = '/bin/grep processor /proc/cpuinfo' |
---|
52 | e = execute.capture_execution() |
---|
53 | exit_code, proc, output = e.shell(processors) |
---|
54 | ncpus = 0 |
---|
55 | if exit_code == 0: |
---|
56 | try: |
---|
57 | for l in output.split('\n'): |
---|
58 | count = l.split(':')[1].strip() |
---|
59 | if int(count) > ncpus: |
---|
60 | ncpus = int(count) |
---|
61 | except: |
---|
62 | pass |
---|
63 | return ncpus + 1 |
---|
64 | |
---|
65 | def overrides(): |
---|
66 | uname = os.uname() |
---|
67 | smp_mflags = '' |
---|
68 | ncpus = '%d' % cpus() |
---|
69 | if uname[4].startswith('arm'): |
---|
70 | cpu = 'arm' |
---|
71 | else: |
---|
72 | cpu = uname[4] |
---|
73 | |
---|
74 | defines = { |
---|
75 | '_ncpus': ('none', 'none', ncpus), |
---|
76 | '_os': ('none', 'none', 'linux'), |
---|
77 | '_host': ('triplet', 'required', cpu + '-linux-gnu'), |
---|
78 | '_host_vendor': ('none', 'none', 'gnu'), |
---|
79 | '_host_os': ('none', 'none', 'linux'), |
---|
80 | '_host_cpu': ('none', 'none', cpu), |
---|
81 | '_host_alias': ('none', 'none', '%{nil}'), |
---|
82 | '_host_arch': ('none', 'none', cpu), |
---|
83 | '_usr': ('dir', 'required', '/usr'), |
---|
84 | '_var': ('dir', 'required', '/var'), |
---|
85 | '__bzip2': ('exe', 'required', '/usr/bin/bzip2'), |
---|
86 | '__gzip': ('exe', 'required', '/bin/gzip'), |
---|
87 | '__tar': ('exe', 'required', '/bin/tar') |
---|
88 | } |
---|
89 | |
---|
90 | # Works for LSB distros |
---|
91 | try: |
---|
92 | distro = platform.dist()[0] |
---|
93 | distro_ver = float(platform.dist()[1]) |
---|
94 | except ValueError: |
---|
95 | # Non LSB distro found, use failover" |
---|
96 | pass |
---|
97 | |
---|
98 | # Non LSB - fail over to issue |
---|
99 | if distro == '': |
---|
100 | try: |
---|
101 | issue = open('/etc/issue').read() |
---|
102 | distro = issue.split(' ')[0] |
---|
103 | distro_ver = float(issue.split(' ')[2]) |
---|
104 | except: |
---|
105 | pass |
---|
106 | |
---|
107 | # Manage distro aliases |
---|
108 | if distro in ['centos']: |
---|
109 | distro = 'redhat' |
---|
110 | elif distro in ['fedora']: |
---|
111 | if distro_ver < 17: |
---|
112 | distro = 'redhat' |
---|
113 | elif distro in ['centos', 'fedora']: |
---|
114 | distro = 'redhat' |
---|
115 | elif distro in ['Ubuntu', 'ubuntu']: |
---|
116 | distro = 'debian' |
---|
117 | elif distro in ['Arch']: |
---|
118 | distro = 'arch' |
---|
119 | elif distro in ['SuSE']: |
---|
120 | distro = 'suse' |
---|
121 | |
---|
122 | variations = { |
---|
123 | 'debian' : { '__bzip2': ('exe', 'required', '/bin/bzip2'), |
---|
124 | '__chgrp': ('exe', 'required', '/bin/chgrp'), |
---|
125 | '__chown': ('exe', 'required', '/bin/chown'), |
---|
126 | '__grep': ('exe', 'required', '/bin/grep'), |
---|
127 | '__sed': ('exe', 'required', '/bin/sed') }, |
---|
128 | 'redhat' : { '__bzip2': ('exe', 'required', '/bin/bzip2'), |
---|
129 | '__chgrp': ('exe', 'required', '/bin/chgrp'), |
---|
130 | '__chown': ('exe', 'required', '/bin/chown'), |
---|
131 | '__install_info': ('exe', 'required', '/sbin/install-info'), |
---|
132 | '__grep': ('exe', 'required', '/bin/grep'), |
---|
133 | '__sed': ('exe', 'required', '/bin/sed'), |
---|
134 | '__touch': ('exe', 'required', '/bin/touch') }, |
---|
135 | 'fedora' : { '__chown': ('exe', 'required', '/usr/bin/chown'), |
---|
136 | '__install_info': ('exe', 'required', '/usr/sbin/install-info') }, |
---|
137 | 'arch' : { '__gzip': ('exe', 'required', '/usr/bin/gzip'), |
---|
138 | '__chown': ('exe', 'required', '/usr/bin/chown') }, |
---|
139 | 'suse' : { '__chgrp': ('exe', 'required', '/usr/bin/chgrp'), |
---|
140 | '__chown': ('exe', 'required', '/usr/sbin/chown') }, |
---|
141 | } |
---|
142 | |
---|
143 | if distro in variations: |
---|
144 | for v in variations[distro]: |
---|
145 | if path.exists(variations[distro][v][2]): |
---|
146 | defines[v] = variations[distro][v] |
---|
147 | |
---|
148 | defines['_build'] = defines['_host'] |
---|
149 | defines['_build_vendor'] = defines['_host_vendor'] |
---|
150 | defines['_build_os'] = defines['_host_os'] |
---|
151 | defines['_build_cpu'] = defines['_host_cpu'] |
---|
152 | defines['_build_alias'] = defines['_host_alias'] |
---|
153 | defines['_build_arch'] = defines['_host_arch'] |
---|
154 | |
---|
155 | return defines |
---|
156 | |
---|
157 | if __name__ == '__main__': |
---|
158 | import pprint |
---|
159 | pprint.pprint(cpus()) |
---|
160 | pprint.pprint(overrides()) |
---|