summaryrefslogtreecommitdiff
path: root/setup_board
blob: 15bf3a04e73006643d43bf3a76bec9a286608a94 (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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
#!/usr/bin/python
# Copyright 2015 The Chromium OS Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

"""Create the initial board build directory and config files."""

from __future__ import print_function

import argparse
import getpass
import grp
import logging
import multiprocessing
import os
import sys


def GetParser():
  parser = argparse.ArgumentParser(description=__doc__)
  parser.add_argument('--board', type=str, required=True,
                      help='The board to use')
  return parser


def main(argv):
  parser = GetParser()
  opts = parser.parse_args(argv)

  source_root = os.path.dirname(os.path.dirname(os.path.dirname(os.path.dirname(
      os.path.realpath(__file__)))))

  overlay = os.path.join(source_root, 'src', 'overlays', 'overlay-%s' % opts.board)
  if not os.path.exists(overlay):
    print('overaly does not exist: %s' % overlay)

  sysroot = os.path.join(source_root, 'build', opts.board)
  print('Setting up %s' % sysroot)

  # Create /etc/portage/.
  etc_portage = os.path.join(sysroot, 'etc', 'portage')
  if not os.path.isdir(etc_portage):
    os.makedirs(etc_portage)

  # Create /etc/portage/make.profile.
  target = os.path.relpath(os.path.join(overlay, 'profiles', 'base'),
                           etc_portage)
  profile = os.path.join(etc_portage, 'make.profile')
  if not os.path.islink(profile):
    os.symlink(target, profile)

  # Stub a few packages out.
  profile = os.path.join(etc_portage, 'profile')
  if not os.path.isdir(profile):
    os.makedirs(profile)
  conf = os.path.join(profile, 'package.provided')
  with open(conf, 'w') as f:
    f.write("""# AUTO GENERATED.
# We use the git eclass which wants git to build things, but we don't actually
# need it in the sysroot as it's a host tool.  Stub it out.
dev-vcs/git-2
""")

  # Used by generation below.
  settings = {
      'source_root': source_root,
      'sysroot': sysroot,
      'overlay': overlay,
      'gprefix': os.path.join(source_root, 'src', 'third_party',
                              'portage-prefix'),
      'ncpus': multiprocessing.cpu_count(),
      'username': getpass.getuser(),
      'groupname': grp.getgrgid(os.getgid()).gr_name,
      'uid': os.getuid(),
      'gid': os.getgid(),
  }

  # Create /etc/portage/make.conf.
  conf = os.path.join(etc_portage, 'make.conf')
  with open(conf, 'w') as f:
    f.write("""# AUTO GENERATED.
EPREFIX='/'
SOURCE_ROOT="%(source_root)s"
CHROOT_SOURCE_ROOT="${SOURCE_ROOT}"
ANDROID_SOURCE="${SOURCE_ROOT}/android"
GENTOO_PREFIX="%(gprefix)s"
COMMON_BUILD="${SOURCE_ROOT}/build/portage"
DISTDIR="${COMMON_BUILD}/distdir"
ROOT="%(sysroot)s"
PORTAGE_TMPDIR="${ROOT}/tmp"
PORT_LOGDIR="${PORTAGE_TMPDIR}/logs"
SYSROOT="%(sysroot)s"
ROOTPATH="/usr/bin:/bin:/usr/sbin:/sbin:${GENTOO_PREFIX}/usr/bin"
ARCH="amd64"
BOARD_OVERLAY="%(overlay)s"
BOARD_USE="bruteus"
CHOST="x86_64-cros-linux-gnu"
MAKEOPTS="-j%(ncpus)i"
PKG_CONFIG="${SYSROOT}/build/bin/pkg-config"
PKGDIR="${SYSROOT}/packages"
PORTDIR_OVERLAY="${SOURCE_ROOT}/src/third_party/eclass-overlay
${SOURCE_ROOT}/src/third_party/portage-stable
${SOURCE_ROOT}/src/third_party/chromiumos-overlay
%(overlay)s"
source "${BOARD_OVERLAY}/make.conf"
PORTAGE_BUNZIP2_COMMAND="bzip2"
FEATURES="-sandbox -usersandbox"
EMERGE_DEFAULT_OPTS="--root-deps --quiet-build y --jobs %(ncpus)i"

TC_GCC_BASE="${ANDROID_SOURCE}/prebuilts/gcc/linux-x86/host/x86_64-linux-glibc2.15-4.8/bin"
TC_CLANG_BASE="${ANDROID_SOURCE}/prebuilts/clang/linux-x86/host/3.6/bin"
CC="${TC_CLANG_BASE}/clang"
CXX="${CXX}++"
AR="${TC_GCC_BASE}/x86_64-linux-ar"
RANLIB="${TC_GCC_BASE}/x86_64-linux-ranlib"
STRIP="${TC_GCC_BASE}/x86_64-linux-strip"

CBUILD="x86_64-pc-linux-gnu"
CHOST="x86_64-android-linux-gnu"

# TODO: These should be pushed back into portage itself.
PORTAGE_USER='%(username)s'
PORTAGE_GROUP='%(groupname)s'
PORTAGE_ROOT_USER='%(username)s'
PORTAGE_INST_UID='%(uid)s'
PORTAGE_INST_GID='%(gid)s'
""" % settings)

  # Now a few bin wrappers.
  bindir = os.path.join(sysroot, 'build', 'bin')
  if not os.path.isdir(bindir):
    os.makedirs(bindir)

  for wrapper in ('ebuild', 'emerge', 'portageq'):
    target = os.path.join(bindir, wrapper)
    settings['wrapper'] = wrapper
    with open(target, 'w') as f:
      f.write("""#!/bin/sh
PORTAGE_CONFIGROOT="%(sysroot)s" \
ROOT="%(sysroot)s" \
exec "%(gprefix)s/usr/bin/%(wrapper)s" "$@"
""" % settings)
    os.chmod(target, 0o755)

  # Done!
  print('You may now use the programs in %s' % bindir)


if __name__ == '__main__':
  sys.exit(main(sys.argv[1:]))