summaryrefslogtreecommitdiff
path: root/morphlib/buildenvironment.py
blob: 6ec82d457cc73bc6ba292d1d06b47d0f4f8e0b1a (plain)
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
# Copyright (C) 2012-2013, 2015  Codethink Limited
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; version 2 of the License.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program.  If not, see <http://www.gnu.org/licenses/>.

import copy
import cliapp
import os

import morphlib


class BuildEnvironment():

    '''Represents the build environment for an artifact

       This should be as consistent as possible across builds, but some
       artifacts will require tweaks. The intention of this object is
       to create one once and call populate() to create an initial state
       and when changes are required, call clone() to get another instance
       which can be modified.

    '''

    def __init__(self, settings, arch):
        '''Create a new BuildEnvironment object'''

        self.extra_path = []

        self.env = self._clean_env(settings)
        self.env.update(self._env_for_arch(arch))


    _osenv = os.environ
    _ccache_path = '/usr/lib/ccache'
    _override_home = '/tmp'
    _override_locale = 'C'
    _override_shell = '/bin/sh'
    _override_term = 'dumb'
    _override_username = 'tomjon'

    def _clean_env(self, settings):
        '''Create a fresh set of environment variables for a clean build.

        Return a dict with the new environment.

        '''

        # copy a set of white-listed variables from the original env
        copied_vars = dict.fromkeys([
            'DISTCC_HOSTS',
            'LD_PRELOAD',
            'LD_LIBRARY_PATH',
            'FAKEROOTKEY',
            'FAKED_MODE',
            'FAKEROOT_FD_BASE',
        ])
        for name in copied_vars:
            copied_vars[name] = self._osenv.get(name, None)

        env = {}

        # apply the copied variables to the clean env
        for name in copied_vars:
            if copied_vars[name] is not None:
                env[name] = copied_vars[name]

        env['TERM'] = self._override_term
        env['SHELL'] = self._override_shell
        env['USER'] = \
            env['USERNAME'] = \
            env['LOGNAME'] = self._override_username
        env['LC_ALL'] = self._override_locale
        env['HOME'] = self._override_home

        if not settings['no-ccache']:
            self.extra_path.append(self._ccache_path)
# FIXME: we should set CCACHE_BASEDIR so any objects that refer to their
#        current directory get corrected. This improve the cache hit rate
#            env['CCACHE_BASEDIR'] = self.tempdir.dirname
            env['CCACHE_DIR'] = '/tmp/ccache'
            env['CCACHE_EXTRAFILES'] = ':'.join(
                f for f in ('/baserock/binutils.meta',
                            '/baserock/eglibc.meta',
                            '/baserock/gcc.meta') if os.path.exists(f)
            )
            if not settings['no-distcc']:
                env['CCACHE_PREFIX'] = 'distcc'

        return env

    def _env_for_arch(self, arch):
        '''Set build environment variables specific to the target machine

           These are entirely controlled by the 'arch' field in the system
           morphology, which is passed to the morphologies as MORPH_ARCH to
           do what they like with.

        '''

        env = {}
        env['MORPH_ARCH'] = arch

        # GNU triplets are widely used, so we handle these in Morph rather
        # than leaving it up to individual morphologies.
        if arch == 'x86_32':
            cpu = 'i686'
        elif arch == 'armv8l64':  # pragma: no cover
            cpu = 'aarch64'
        elif arch == 'armv8b64':  # pragma: no cover
            cpu = 'aarch64_be'
        else:
            cpu = arch

        if arch.startswith('armv7'):
            abi = 'eabi'
        else:
            abi = ''

        env['TARGET'] = cpu + '-baserock-linux-gnu' + abi
        env['TARGET_STAGE1'] = cpu + '-bootstrap-linux-gnu' + abi

        return env