• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/usr/bin/env python
2# -*- coding: utf-8 -*-
3# Copyright (c) 2021 Huawei Device Co., Ltd.
4# Licensed under the Apache License, Version 2.0 (the "License");
5# you may not use this file except in compliance with the License.
6# You may obtain a copy of the License at
7#
8#     http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS,
12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13# See the License for the specific language governing permissions and
14# limitations under the License.
15
16import os
17import sys
18import optparse
19import subprocess
20
21
22def check_output(cmd, **kwargs):
23    process = subprocess.Popen(cmd,
24                               stdout=subprocess.PIPE,
25                               stderr=subprocess.STDOUT,
26                               universal_newlines=True,
27                               **kwargs)
28    for line in iter(process.stdout.readline, ''):
29        sys.stdout.write(line)
30        sys.stdout.flush()
31    process.wait()
32    return process.returncode
33
34
35def warning(msg):
36    print(f'\033[93m{msg}\033[0m')
37
38
39def do_build(args):
40    build_py = os.path.join(args.source_root_dir, 'build.py')
41    cmd = [
42        'python3',
43        build_py,
44        '-p',
45        args.product_name,
46    ]
47    build_targets = args.build_target
48    gn_args = args.gn_args
49
50    if args.product_name == 'ohos-sdk':
51        gn_args.append('build_ohos_ndk=true')
52        gn_args.append('build_ohos_sdk=true')
53        build_targets.append('build_ohos_sdk')
54
55    if args.build_only_gn:
56        cmd.append('--build-only-gn')
57
58    if args.sparse_image:
59        gn_args.append('sparse_image=true')
60
61    for item in args.export_para:
62        key, value = item.split(':')
63        if key == 'PYCACHE_ENABLE' and value == 'true':
64            gn_args.append('pycache_enable=true')
65
66    if build_targets:
67        cmd.append('-T')
68        for target in build_targets:
69            cmd.append(target)
70
71    if gn_args:
72        cmd.extend(['--gn-args', ' '.join(gn_args)])
73    if args.ninja_args:
74        warning('--ninja-args is obsoleted, '
75                'please use --verbose or --keep-ninja-going instead')
76    if args.verbose:
77        cmd.append('-v')
78    if args.keep_ninja_going:
79        cmd.append('--keep-ninja-going')
80    try:
81        return check_output(cmd)
82    except KeyboardInterrupt:
83        print('User abort')
84        return -1
85
86
87def main():
88    parser = optparse.OptionParser()
89    parser.add_option('--source-root-dir')
90    parser.add_option('--product-name')
91    parser.add_option('--device-name')
92    parser.add_option('--target-cpu')
93    parser.add_option('--target-os')
94    parser.add_option('-T', '--build-target', action='append', default=[])
95    parser.add_option('--gn-args', action='append', default=[])
96    parser.add_option('--ninja-args', action='append', default=[])
97    parser.add_option('-v', '--verbose', action='store_true')
98    parser.add_option('--keep-ninja-going', action='store_true')
99    parser.add_option('--sparse-image', action='store_true')
100    parser.add_option('--jobs')
101    parser.add_option('--export-para', action='append', default=[])
102    parser.add_option('--build-only-gn', action='store_true')
103    parser.add_option('--ccache', action='store_true')
104    args, _ = parser.parse_args()
105
106    if args.source_root_dir is None:
107        print('Error: source_root_dir must be provided to entry.py')
108        return -1
109    if args.product_name is None:
110        print('Error: product_name must be provided to entry.py')
111        return -1
112
113    return do_build(args)
114
115
116if __name__ == '__main__':
117    sys.exit(main())
118