source: rtems-tools/rtemstoolkit/linux.py @ 31e22e3

5
Last change on this file since 31e22e3 was b0fa2ae, checked in by Chris Johns <chrisj@…>, on 03/03/16 at 05:46:18

Update rtems-tool to support Python 2 and 3.

Add solaris and netbsd.

Close #2619.

  • Property mode set to 100644
File size: 6.0 KB
Line 
1#
2# RTEMS Tools Project (http://www.rtems.org/)
3# Copyright 2010-2016 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
36import pprint
37import os
38import platform
39
40#
41# Support to handle use in a package and as a unit test.
42# If there is a better way to let us know.
43#
44try:
45    from . import execute
46    from . import path
47except (ValueError, SystemError):
48    import execute
49    import path
50
51def load():
52    uname = os.uname()
53    smp_mflags = ''
54    processors = '/bin/grep processor /proc/cpuinfo'
55    e = execute.capture_execution()
56    exit_code, proc, output = e.shell(processors)
57    ncpus = 0
58    if exit_code == 0:
59        try:
60            for l in output.split('\n'):
61                count = l.split(':')[1].strip()
62                if int(count) > ncpus:
63                    ncpus = int(count)
64        except:
65            pass
66    ncpus = str(ncpus + 1)
67    if uname[4].startswith('arm'):
68        cpu = 'arm'
69    else:
70        cpu = uname[4]
71
72    defines = {
73        '_ncpus':         ('none',    'none',     ncpus),
74        '_os':            ('none',    'none',     'linux'),
75        '_host':          ('triplet', 'required', cpu + '-linux-gnu'),
76        '_host_vendor':   ('none',    'none',     'gnu'),
77        '_host_os':       ('none',    'none',     'linux'),
78        '_host_cpu':      ('none',    'none',     cpu),
79        '_host_alias':    ('none',    'none',     '%{nil}'),
80        '_host_arch':     ('none',    'none',     cpu),
81        '_usr':           ('dir',     'required', '/usr'),
82        '_var':           ('dir',     'required', '/var'),
83        '__bzip2':        ('exe',     'required', '/usr/bin/bzip2'),
84        '__gzip':         ('exe',     'required', '/bin/gzip'),
85        '__tar':          ('exe',     'required', '/bin/tar')
86        }
87
88    # Works for LSB distros
89    try:
90        distro = platform.dist()[0]
91        distro_ver = float(platform.dist()[1])
92    except ValueError:
93        # Non LSB distro found, use failover"
94        pass
95
96    # Non LSB - fail over to issue
97    if distro == '':
98        try:
99            issue = open('/etc/issue').read()
100            distro = issue.split(' ')[0]
101            distro_ver = float(issue.split(' ')[2])
102        except:
103            pass
104
105    # Manage distro aliases
106    if distro in ['centos']:
107        distro = 'redhat'
108    elif distro in ['fedora']:
109        if distro_ver < 17:
110            distro = 'redhat'
111    elif distro in ['centos', 'fedora']:
112        distro = 'redhat'
113    elif distro in ['Ubuntu', 'ubuntu']:
114        distro = 'debian'
115    elif distro in ['Arch']:
116        distro = 'arch'
117    elif distro in ['SuSE']:
118        distro = 'suse'
119
120    variations = {
121        'debian' : { '__bzip2':        ('exe',     'required', '/bin/bzip2'),
122                     '__chgrp':        ('exe',     'required', '/bin/chgrp'),
123                     '__chown':        ('exe',     'required', '/bin/chown'),
124                     '__grep':         ('exe',     'required', '/bin/grep'),
125                     '__sed':          ('exe',     'required', '/bin/sed') },
126        'redhat' : { '__bzip2':        ('exe',     'required', '/bin/bzip2'),
127                     '__chgrp':        ('exe',     'required', '/bin/chgrp'),
128                     '__chown':        ('exe',     'required', '/bin/chown'),
129                     '__install_info': ('exe',     'required', '/sbin/install-info'),
130                     '__grep':         ('exe',     'required', '/bin/grep'),
131                     '__sed':          ('exe',     'required', '/bin/sed'),
132                     '__touch':        ('exe',     'required', '/bin/touch') },
133        'fedora' : { '__chown':        ('exe',     'required', '/usr/bin/chown'),
134                     '__install_info': ('exe',     'required', '/usr/sbin/install-info') },
135        'arch'   : { '__gzip':         ('exe',     'required', '/usr/bin/gzip'),
136                     '__chown':        ('exe',     'required', '/usr/bin/chown') },
137        'suse'   : { '__chgrp':        ('exe',     'required', '/usr/bin/chgrp'),
138                     '__chown':        ('exe',     'required', '/usr/sbin/chown') },
139        }
140
141    if distro in variations:
142        for v in variations[distro]:
143            if path.exists(variations[distro][v][2]):
144                defines[v] = variations[distro][v]
145
146    defines['_build']        = defines['_host']
147    defines['_build_vendor'] = defines['_host_vendor']
148    defines['_build_os']     = defines['_host_os']
149    defines['_build_cpu']    = defines['_host_cpu']
150    defines['_build_alias']  = defines['_host_alias']
151    defines['_build_arch']   = defines['_host_arch']
152
153    return defines
154
155if __name__ == '__main__':
156    pprint.pprint(load())
Note: See TracBrowser for help on using the repository browser.