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 optparse 17import os 18import sys 19import zipfile 20 21sys.path.append(os.path.join(os.path.dirname(__file__), os.pardir, os.pardir)) 22from scripts.util import build_utils # noqa: E402 23 24 25def parse_args(args): 26 args = build_utils.expand_file_args(args) 27 28 parser = optparse.OptionParser() 29 build_utils.add_depfile_option(parser) 30 parser.add_option('--output', help='generated ndk stub file') 31 parser.add_option('--os-irrelevant-dir', 32 help='base directory of ndk common files') 33 parser.add_option('--os-specific-dir', 34 help='base directory of os specific stuff') 35 parser.add_option('--prefix', 36 help='prefix string of directory in archive zipfile') 37 parser.add_option('--notice-file', help='path to notice file') 38 parser.add_option('--record-path', help='path to md5.stamp file') 39 parser.add_option('--platform', help='os platform') 40 41 options, _ = parser.parse_args(args) 42 return options 43 44 45def do_archive(output: str, directory: str, prefix: str, compress_fn, filter_file_name: list): 46 files = [] 47 for root, _, filenames in os.walk(directory): 48 for f in filenames: 49 if f not in filter_file_name: 50 files.extend([os.path.join(root, f)]) 51 with zipfile.ZipFile(output, 'a') as outfile: 52 for f in files: 53 compress = compress_fn(f) if compress_fn else None 54 if prefix: 55 zip_path = os.path.join(prefix, os.path.relpath(f, directory)) 56 else: 57 zip_path = os.path.relpath(f, directory) 58 build_utils.add_to_zip_hermetic(outfile, 59 zip_path, 60 src_path=f, 61 compress=compress) 62 63 64def archive_ndk(output: str, os_irrelevant_dir: str, os_specific_dir: str, prefix: str, 65 compress_fn, notice: str, filter_file_name: list): 66 # Create an empty zipfile first, then add stuff to it. 67 with zipfile.ZipFile(output, 'w') as outfile: 68 pass 69 for directory in [os_irrelevant_dir, os_specific_dir]: 70 do_archive(output, directory, prefix, compress_fn, filter_file_name) 71 72 with zipfile.ZipFile(output, 'a') as zip_file: 73 compress = compress_fn(notice) if compress_fn else None 74 if prefix: 75 zip_path = os.path.join(prefix, os.path.basename(notice)) 76 else: 77 zip_path = os.path.basename(notice) 78 build_utils.add_to_zip_hermetic(zip_file, 79 zip_path, 80 src_path=notice, 81 compress=compress) 82 83 84def file_filter(os_irrelevant_dir: str): 85 filter_file_name = [] 86 target_dir = os.path.join(os_irrelevant_dir, 'sysroot/usr/include/linux') 87 filter_file_path = build_utils.find_in_directory(target_dir, '*[A-Z].h') 88 for file_path in filter_file_path: 89 file_name = os.path.basename(file_path) 90 if os.path.exists(os.path.join(os.path.dirname(file_path), file_name.lower())): 91 filter_file_name.append(file_name) 92 return filter_file_name 93 94 95def main(args): 96 options = parse_args(args) 97 98 os_irrelevant_dir = options.os_irrelevant_dir 99 os_specific_dir = options.os_specific_dir 100 filter_file_name = [] 101 if options.platform == 'windows': 102 filter_file_name = file_filter(os_irrelevant_dir) 103 depfile_deps = set( 104 build_utils.get_all_files(os_irrelevant_dir) + 105 build_utils.get_all_files(os_specific_dir)) 106 depfile_deps.add(options.notice_file) 107 108 build_utils.call_and_write_depfile_if_stale(lambda: archive_ndk( 109 options.output, os_irrelevant_dir, os_specific_dir, options.prefix, 110 lambda _: True, options.notice_file, filter_file_name), 111 options, 112 depfile_deps=depfile_deps, 113 input_paths=depfile_deps, 114 output_paths=([options.output]), 115 record_path=options.record_path, 116 force=False, 117 add_pydeps=False) 118 119 120if __name__ == '__main__': 121 sys.exit(main(sys.argv[1:])) 122