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.export_rust_project: 94 cmd.append('--export-rust-project') 95 if args.device_type: 96 cmd.append('--device-type=' + args.device_type) 97 if args.build_variant: 98 cmd.append('--build-variant=' + args.build_variant) 99 if args.share_ccache: 100 cmd.append('--share-ccache=' + args.share_ccache) 101 if args.disable_post_build: 102 cmd.append('--disable-post-build') 103 if args.disable_part_of_post_build: 104 cmd.append('--disable-part-of-post-build') 105 for option in args.disable_part_of_post_build: 106 cmd.append(option) 107 try: 108 return check_output(cmd) 109 except KeyboardInterrupt: 110 print('User abort') 111 return -1 112 113 114def main(): 115 parser = optparse.OptionParser() 116 parser.add_option('--source-root-dir') 117 parser.add_option('--product-name') 118 parser.add_option('--device-name') 119 parser.add_option('--target-cpu') 120 parser.add_option('--target-os') 121 parser.add_option('--compile-config') 122 parser.add_option('-T', '--build-target', action='append', default=[]) 123 parser.add_option('--gn-args', action='append', default=[]) 124 parser.add_option('--ninja-args', action='append', default=[]) 125 parser.add_option('-v', '--verbose', action='store_true') 126 parser.add_option('--keep-ninja-going', action='store_true') 127 parser.add_option('--sparse-image', action='store_true') 128 parser.add_option('--jobs') 129 parser.add_option('--export-para', action='append', default=[]) 130 parser.add_option('--export-rust-project', action='store_true') 131 parser.add_option('--build-only-gn', action='store_true') 132 parser.add_option('--ccache', action='store_true') 133 parser.add_option('--fast-rebuild', action='store_true') 134 parser.add_option('--disable-package-image', action='store_true') 135 parser.add_option('--disable-post-build', action='store_true') 136 parser.add_option('--disable-part-of-post-build', action='append', default=[]) 137 parser.add_option('--log-level', default='info') 138 parser.add_option('--device-type', default='default') 139 parser.add_option('--build-variant', default='root') 140 parser.add_option('--share-ccache') 141 args, _ = parser.parse_args() 142 143 if args.source_root_dir is None: 144 print('Error: source_root_dir must be provided to entry.py') 145 return -1 146 if args.product_name is None: 147 print('Error: product_name must be provided to entry.py') 148 return -1 149 150 return do_build(args) 151 152 153if __name__ == '__main__': 154 sys.exit(main()) 155