build.py 4.5 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 21 22
#!/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

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


def add_options(parser):
    parser.add_argument('component', help='name of the component', nargs='*',
G
gzyang 已提交
29
                        default=None)
M
mamingshuai 已提交
30 31 32 33 34 35 36
    parser.add_argument('-b', '--build_type', help='release or debug version',
                        nargs=1, default=['debug'])
    parser.add_argument('-c', '--compiler', help='specify compiler',
                        nargs=1, default=['clang'])
    parser.add_argument('-t', '--test', help='compile test suit', nargs='*')
    parser.add_argument('--dmverity', help='Enable dmverity',
                        action="store_true")
P
pilipala195 已提交
37 38
    parser.add_argument('--tee', help='Enable tee',
                        action="store_true")
M
mamingshuai 已提交
39 40 41 42 43 44 45 46 47 48 49 50 51 52
    parser.add_argument('-p', '--product', help='build a specified product '
                        'with {product_name}@{company}, eg: camera@huawei',
                        nargs=1, default=[])
    parser.add_argument('-f', '--full',
                        help='full code compilation', action='store_true')
    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',
                        help='show all command lines while building',
                        action='store_true')
    parser.add_argument('-shs', '--sign_haps_by_server',
                        help='sign haps by server', action='store_true')
53 54
    parser.add_argument('--patch', help='apply product patch before compiling',
                        action='store_true')
55 56 57
    parser.add_argument('--gn-args', nargs=1, default='',
                        help='specifies gn build arguments, '
                             'eg: --gn-args="foo="bar" enable=true blah=7"')
M
mamingshuai 已提交
58 59 60


def exec_command(args):
G
gzyang 已提交
61 62 63
    if len(args.product):
        product, company = args.product[0].split('@')
        set_product(product_name=product, company=company)
M
mamingshuai 已提交
64

G
gzyang 已提交
65 66
    build = Build(args.component)
    cmd_args = defaultdict(list)
M
mamingshuai 已提交
67 68

    build.register_args('ohos_build_type', args.build_type[0])
69 70 71 72
    # 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 已提交
73 74 75 76 77 78 79 80

    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 已提交
81 82 83 84 85
        build.config.fs_attr.add('dmverity_enable')

    if args.tee:
        build.register_args('tee_enable', 'true', quota=False)
        build.config.fs_attr.add('tee_enable')
M
mamingshuai 已提交
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104

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

    if hasattr(args, 'target') and len(args.target):
        build.register_args('ohos_build_target', args.target)

    if hasattr(args, 'verbose') and args.verbose:
        cmd_args['gn'].append('-v')
        cmd_args['ninja'].append('-v')

    if hasattr(args, 'ninja'):
        return build.build(args.full, ninja=args.ninja)

    if args.sign_haps_by_server:
        build.register_args('ohos_sign_haps_by_server',
                            'true',
                            quota=False)

105 106 107 108 109 110 111 112
    if len(args.gn_args):
        for gn_arg in args.gn_args[0].split(' '):
            try:
                variable, value = gn_arg.split('=')
                build.register_args(variable, value)
            except ValueError:
                raise OHOSException(f'Invalid gn args: {gn_arg}')

113
    return build.build(args.full, patch=args.patch, cmd_args=cmd_args)