aboutsummaryrefslogtreecommitdiff
path: root/brunch/lib/commands/product/build.py
blob: aa9d2362c58f93f1b59817d31e93c8f5fabd7d30 (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
#
# Copyright (C) 2015 The Android Open Source Project
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

"""Build a Product"""

import argparse
import os
import time

from cli import clicommand
from core import config
from core import util
from metrics.data_types import brillo_ga_hits
from metrics import send_hits

class Build(clicommand.Command):
  """Build a product project from the current directory"""

  @staticmethod
  def Args(parser):
    parser.add_argument('-s', '--submodule', action='store_true',
                        help='Build the submodule in the current directory')
    parser.add_argument('-p', '--product_path', default=util.GetProductDir(), required=False,
                        help='Path to the root of the product')
    parser.add_argument('-b', '--buildtype', default=None, required=False,
                        choices=['eng', 'userdebug', 'user'],
                        help='Override the configured type of build to perform')
    parser.add_argument('make_args', nargs=argparse.REMAINDER,
                        help='Arguments to pass through to make')

  def Run(self, args):
    sdk_path = util.GetDevToolsPath()

    store = config.ProductFileStore(args.product_path)

    # Pull the buildtype from the config if it is not overridden.
    if args.buildtype is None:
      args.buildtype = store.bdk.buildtype

    no_java = 'BRILLO_NO_JAVA=0'
    if store.bdk.java != '1':
      no_java = 'BRILLO_NO_JAVA=1'

    # This should only happen if the ProductFileStore is incomplete.
    if not args.buildtype:
      args.buildtype = 'userdebug'

    # Don't pass along the first '--' since it was meant for argparse.
    args.make_args.count('--') and args.make_args.remove('--')

    # If user options haven't been set up yet, do so.
    # TODO(arihc)(b/24381926): move this to install script instead of here.
    user_store = config.UserStore()
    if not user_store.complete():
      user_store.initialize()

    envcmd = 'make'
    start_time = time.time()

    if args.submodule == True:
       print "Building submodule only . . ."
       ret = os.system('make -f %s/build/wrap-product.mk %s ' \
                       'HERE="%s" ENVCMD=mm BUILDTYPE=%s %s' %
                       (sdk_path, no_java, os.getcwd(), args.buildtype,
                        util.AsShellArgs(args.make_args)))
       envcmd = 'mm'
    else:
       # Change to the product path and build the whole product.
       ret = os.system('cd %s;make -f %s/build/wrap-product.mk %s BUILDTYPE=%s %s' %
                       (args.product_path, sdk_path, no_java, args.buildtype,
                        util.AsShellArgs(args.make_args)))

    end_time = time.time()
    build_time = int(end_time - start_time)

    # Determine the envcmd for reporting purposes
    for arg in args.make_args:
      split_arg = arg.split('=')
      if split_arg[0] == 'ENVCMD' and len(split_arg) > 1:
        envcmd = split_arg[1]

    # TODO(arihc): also check for not --help, since this is what build_time > 0
    #   is primarily checking for, and the wrapper runs lunch first,
    #   making basically all builds > 0 time.
    if user_store.metrics_opt_in == '1' and ret == 0 and build_time > 0:
      send_hits.SendHitAndRetries(brillo_ga_hits.BuildTiming(envcmd,
                                                             build_time))

    return util.GetExitCode(ret)