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 16 17import os 18import sys 19import optparse 20import subprocess 21 22 23def check_output(cmd, **kwargs): 24 process = subprocess.Popen(cmd, 25 stdout=subprocess.PIPE, 26 stderr=subprocess.STDOUT, 27 universal_newlines=True, 28 **kwargs) 29 for line in iter(process.stdout.readline, ''): 30 sys.stdout.write(line) 31 sys.stdout.flush() 32 process.wait() 33 return process.returncode 34 35 36def warning(msg): 37 print(f'\033[93m{msg}\033[0m') 38 39 40def do_build(args): 41 build_py = os.path.join(args.source_root_dir, 'build.py') 42 cmd = [ 43 'python3', 44 build_py, 45 '-p', 46 args.product_name, 47 ] 48 build_targets = args.build_target 49 gn_args = args.gn_args 50 51 if args.target_cpu is not None: 52 cmd.extend(['--target-cpu', ''.join(args.target_cpu)]) 53 54 if args.compile_config is not None: 55 cmd.extend(['--compile-config', ''.join(args.compile_config)]) 56 57 if args.product_name == 'ohos-sdk': 58 gn_args.append('build_ohos_ndk=true') 59 gn_args.append('build_ohos_sdk=true') 60 build_targets.append('build_ohos_sdk') 61 62 if args.build_only_gn: 63 cmd.append('--build-only-gn') 64 65 if args.sparse_image: 66 gn_args.append('sparse_image=true') 67 68 for item in args.export_para: 69 key, value = item.split(':') 70 if key == 'PYCACHE_ENABLE' and value == 'true': 71 gn_args.append('pycache_enable=true') 72 73 if build_targets: 74 cmd.append('-T') 75 for target in build_targets: 76 cmd.append(target) 77 78 if gn_args: 79 cmd.extend(['--gn-args', ' '.join(gn_args)]) 80 if args.ninja_args: 81 warning('--ninja-args is obsoleted, ' 82 'please use --verbose or --keep-ninja-going instead') 83 if args.verbose: 84 cmd.append('-v') 85 if args.keep_ninja_going: 86 cmd.append('--keep-ninja-going') 87 if args.fast_rebuild: 88 cmd.append('--fast-rebuild') 89 if args.disable_package_image: 90 cmd.append('--disable-package-image') 91 if args.log_level == 'debug': 92 cmd.append('--log-level=debug') 93 if args.device_type: 94 cmd.append('--device-type=' + args.device_type) 95 if args.build_variant: 96 cmd.append('--build-variant=' + args.build_variant) 97 if args.share_ccache: 98 cmd.append('--share-ccache=' + args.share_ccache) 99 if args.disable_post_build: 100 cmd.append('--disable-post-build') 101 if args.disable_part_of_post_build: 102 cmd.append('--disable-part-of-post-build') 103 for option in args.disable_part_of_post_build: 104 cmd.append(option) 105 try: 106 return check_output(cmd) 107 except KeyboardInterrupt: 108 print('User abort') 109 return -1 110 111 112def main(): 113 parser = optparse.OptionParser() 114 parser.add_option('--source-root-dir') 115 parser.add_option('--product-name') 116 parser.add_option('--device-name') 117 parser.add_option('--target-cpu') 118 parser.add_option('--target-os') 119 parser.add_option('--compile-config') 120 parser.add_option('-T', '--build-target', action='append', default=[]) 121 parser.add_option('--gn-args', action='append', default=[]) 122 parser.add_option('--ninja-args', action='append', default=[]) 123 parser.add_option('-v', '--verbose', action='store_true') 124 parser.add_option('--keep-ninja-going', action='store_true') 125 parser.add_option('--sparse-image', action='store_true') 126 parser.add_option('--jobs') 127 parser.add_option('--export-para', action='append', default=[]) 128 parser.add_option('--build-only-gn', action='store_true') 129 parser.add_option('--ccache', action='store_true') 130 parser.add_option('--fast-rebuild', action='store_true') 131 parser.add_option('--disable-package-image', action='store_true') 132 parser.add_option('--disable-post-build', action='store_true') 133 parser.add_option('--disable-part-of-post-build', action='append', default=[]) 134 parser.add_option('--log-level', default='info') 135 parser.add_option('--device-type', default='default') 136 parser.add_option('--build-variant', default='root') 137 parser.add_option('--share-ccache') 138 args, _ = parser.parse_args() 139 140 if args.source_root_dir is None: 141 print('Error: source_root_dir must be provided to entry.py') 142 return -1 143 if args.product_name is None: 144 print('Error: product_name must be provided to entry.py') 145 return -1 146 147 return do_build(args) 148 149 150if __name__ == '__main__': 151 sys.exit(main()) 152