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 19 20sys.path.append(os.path.join(os.path.dirname(__file__), os.pardir, os.pardir)) 21from scripts.util import build_utils # noqa: E402 22from scripts.interface_mgr import InterfaceMgr # 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('--headers', 32 action='append', 33 help='base directory of ndk common files') 34 parser.add_option('--generated-signature', 35 help='base directory of os specific stuff') 36 parser.add_option('--saved-signature', 37 help='prefix string of directory in archive zipfile') 38 parser.add_option('--check-signature', 39 action='store_true', 40 help='check ndk signature') 41 parser.add_option( 42 '--root-build-dir', 43 help='root build directory, used to strip relative address') 44 45 options, _ = parser.parse_args(args) 46 47 return options 48 49 50def header_signature(output: str, headers: list, root_build_dir: str, saved: str, generated: str, check: bool): 51 signature = [] 52 mgr = InterfaceMgr() 53 54 for f in headers: 55 signature.append('//{} {}'.format(os.path.relpath(f, root_build_dir), 56 mgr.get_file_sha256(f))) 57 58 os.makedirs(os.path.dirname(generated), exist_ok=True) 59 with open(generated, 'w') as g: 60 g.write('\n'.join(sorted(signature))) 61 62 if check and mgr.get_file_sha256(generated) != mgr.get_file_sha256(saved): 63 raise Exception( 64 "Error: ndk header signature changed. generated signature {} has " 65 "different signature with saved signature {}" 66 .format(generated, saved)) 67 68 build_utils.touch(output) 69 70 71def main(args): 72 options = parse_args(args) 73 74 depfile_deps = set() 75 if options.check_signature: 76 depfile_deps.add(options.saved_signature) 77 78 build_utils.call_and_write_depfile_if_stale(lambda: header_signature( 79 options.output, options.headers, options.root_build_dir, options. 80 saved_signature, options.generated_signature, options.check_signature), 81 options, 82 depfile_deps=depfile_deps, 83 input_paths=depfile_deps, 84 output_paths=([options.output 85 ]), 86 input_strings=args, 87 force=False, 88 add_pydeps=False) 89 90 91if __name__ == '__main__': 92 sys.exit(main(sys.argv[1:])) 93