build.py 6.4 KB
Newer Older
M
mamingshuai 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
#!/usr/bin/env python
# -*- coding: utf-8 -*-

#
# Copyright (c) 2020 Huawei Device Co., Ltd.
# 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.
#

from collections import defaultdict

21 22 23 24
from hb_internal.build.build_process import Build
from hb_internal.set.set import set_product
from hb_internal.common.utils import get_current_time
from hb_internal.common.utils import OHOSException
M
mamingshuai 已提交
25 26 27


def add_options(parser):
28 29 30
    parser.add_argument('component',
                        help='name of the component, mini/small only',
                        nargs='*',
G
gzyang 已提交
31
                        default=None)
32 33 34 35 36 37 38 39 40 41
    parser.add_argument('-b',
                        '--build_type',
                        help='release or debug version, mini/small only',
                        nargs=1,
                        default=['debug'])
    parser.add_argument('-c',
                        '--compiler',
                        help='specify compiler, mini/small only',
                        nargs=1,
                        default=['clang'])
M
mamingshuai 已提交
42
    parser.add_argument('-t', '--test', help='compile test suit', nargs='*')
43 44
    parser.add_argument('--dmverity',
                        help='Enable dmverity',
M
mamingshuai 已提交
45
                        action="store_true")
46 47 48 49 50 51 52 53 54 55
    parser.add_argument('--tee', help='Enable tee', action="store_true")
    parser.add_argument('-p',
                        '--product',
                        help='build a specified product '
                        'with {product_name}@{company}',
                        nargs=1,
                        default=[])
    parser.add_argument('-f',
                        '--full',
                        help='full code compilation',
M
mamingshuai 已提交
56
                        action='store_true')
57 58 59 60 61 62 63 64
    parser.add_argument('-n', '--ndk', help='compile ndk', action='store_true')
    parser.add_argument('-T',
                        '--target',
                        help='Compile single target',
                        nargs='*',
                        default=[])
    parser.add_argument('-v',
                        '--verbose',
M
mamingshuai 已提交
65 66
                        help='show all command lines while building',
                        action='store_true')
67 68 69 70 71 72
    parser.add_argument('-shs',
                        '--sign_haps_by_server',
                        help='sign haps by server',
                        action='store_true')
    parser.add_argument('--patch',
                        help='apply product patch before compiling',
73
                        action='store_true')
Y
yaoxiaoyu 已提交
74
    parser.add_argument('--compact-mode',
Y
yaoxiaoyu 已提交
75
                        action='store_false',
Y
yaoxiaoyu 已提交
76
                        help='compactiable with standard build system'
Y
yaoxiaoyu 已提交
77 78
                        'set to false if we use build.sh as build entrance',
                        default=True)
79 80 81
    parser.add_argument('--gn-args',
                        nargs=1,
                        default='',
82
                        help='specifies gn build arguments, '
83 84 85 86
                        'eg: --gn-args="foo="bar" enable=true blah=7"')
    parser.add_argument('--keep-ninja-going',
                        action='store_true',
                        help='keeps ninja going until 1000000 jobs fail')
Y
yaoxiaoyu 已提交
87 88 89
    parser.add_argument('--build-only-gn',
                        action='store_true',
                        help='only do gn parse, donot run ninja')
M
mamingshuai 已提交
90 91 92


def exec_command(args):
G
gzyang 已提交
93
    if len(args.product):
94 95 96 97 98
        if '@' in args.product[0]:
            product, company = args.product[0].split('@')
        else:
            product = args.product[0]
            company = None
G
gzyang 已提交
99
        set_product(product_name=product, company=company)
M
mamingshuai 已提交
100

Y
yaoxiaoyu 已提交
101
    build = Build(args.component, args.compact_mode)
102 103 104
    cmd_args = defaultdict()
    cmd_args['gn'] = []
    cmd_args['ninja'] = {}
M
mamingshuai 已提交
105 106

    build.register_args('ohos_build_type', args.build_type[0])
107 108 109 110
    # Get the compilation time in timestamp and human readable format
    build.register_args('ohos_build_time', get_current_time(type='timestamp'))
    build.register_args('ohos_build_datetime',
                        get_current_time(type='datetime'))
M
mamingshuai 已提交
111 112 113 114 115 116 117 118

    if args.test is not None:
        build.test = args.test

    if args.dmverity:
        build.register_args('enable_ohos_security_dmverity',
                            'true',
                            quota=False)
P
pilipala195 已提交
119 120
        build.config.fs_attr.add('dmverity_enable')

Y
yaoxiaoyu 已提交
121
    if hasattr(args, 'tee') and args.tee:
P
pilipala195 已提交
122 123
        build.register_args('tee_enable', 'true', quota=False)
        build.config.fs_attr.add('tee_enable')
M
mamingshuai 已提交
124 125 126 127

    if args.ndk:
        build.register_args('ohos_build_ndk', 'true', quota=False)

Y
yaoxiaoyu 已提交
128 129
    if args.compact_mode is False and hasattr(args, 'target') and len(
            args.target):
M
mamingshuai 已提交
130 131 132
        build.register_args('ohos_build_target', args.target)

    if hasattr(args, 'verbose') and args.verbose:
133
        cmd_args['ninja']['verbose'] = True
M
mamingshuai 已提交
134

Y
yaoxiaoyu 已提交
135
    if hasattr(args, 'sign_haps_by_server') and args.sign_haps_by_server:
136
        build.register_args('ohos_sign_haps_by_server', 'true', quota=False)
M
mamingshuai 已提交
137

Y
yaoxiaoyu 已提交
138
    if hasattr(args, 'gn_args') and len(args.gn_args):
139 140 141
        for gn_arg in args.gn_args[0].split(' '):
            try:
                variable, value = gn_arg.split('=')
142
                build.register_args(variable, value, False)
143 144 145
            except ValueError:
                raise OHOSException(f'Invalid gn args: {gn_arg}')

Y
yaoxiaoyu 已提交
146
    if hasattr(args, 'compact_mode') and args.compact_mode:
Y
yaoxiaoyu 已提交
147
        if hasattr(args, 'target') and len(args.target):
148
            cmd_args['ninja']['targets'] = args.target
Y
yaoxiaoyu 已提交
149
        else:
150
            cmd_args['ninja']['default_target'] = 'packages'
151 152
        if hasattr(args, 'full') and args.full:
            cmd_args['ninja']['targets'] = ['make_all', 'make_test']
Y
yaoxiaoyu 已提交
153
    if hasattr(args, 'keep_ninja_going') and args.keep_ninja_going:
154
        cmd_args['ninja']['keep_ninja_going'] = True
Y
yaoxiaoyu 已提交
155

Y
yaoxiaoyu 已提交
156
    ninja = True
Y
yaoxiaoyu 已提交
157
    if hasattr(args, 'build_only_gn') and args.build_only_gn:
Y
yaoxiaoyu 已提交
158 159 160 161 162
        ninja = False
    return build.build(args.full,
                       patch=args.patch,
                       cmd_args=cmd_args,
                       ninja=ninja)