From d0163a8d4c654238c03cf26669f4ce057a568ff5 Mon Sep 17 00:00:00 2001 From: zzz701 Date: Tue, 19 Aug 2025 18:07:59 +0800 Subject: [PATCH] =?UTF-8?q?NDK=E6=89=93=E5=8C=85=E8=A3=81=E5=89=AA1.2?= =?UTF-8?q?=E6=8E=A5=E5=8F=A3=20Signed-off-by:=20zzz701=20?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- build-tools/clean_ndk_ani.py | 125 +++++++++++++++++++++++++++++++ build-tools/clean_ndk_ani.pydeps | 10 +++ 2 files changed, 135 insertions(+) create mode 100755 build-tools/clean_ndk_ani.py create mode 100644 build-tools/clean_ndk_ani.pydeps diff --git a/build-tools/clean_ndk_ani.py b/build-tools/clean_ndk_ani.py new file mode 100755 index 000000000..b6b0c3fe7 --- /dev/null +++ b/build-tools/clean_ndk_ani.py @@ -0,0 +1,125 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- +# Copyright (c) 2025 Huawei Device Co., Ltd. +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import os +import re +import argparse +import shutil +import json +from typing import List + +# ani header file list +_ANI_HEADER_LISTS = [ + "native_node_ani.h", +] + +# Precompiled regular expression +_HEADER_PATTERN = re.compile( + r'^\s*#\s*include\s+["<](.*/)?({})[">]'.format( + '|'.join(map(re.escape, _ANI_HEADER_LISTS)) + ) +) + + +def process_header_file(file_path): + """processing single header file""" + modified = False + try: + with open(file_path, 'r+', encoding='utf-8') as f: + content = f.read() + # Use a regular expression to process all rows at once + new_content = [] + for line in content.splitlines(): + if not _HEADER_PATTERN.match(line): + new_content.append(line) + else: + modified = True + + if modified: + f.seek(0) + f.write('\n'.join(new_content)) + f.truncate() + except Exception as e: + print(f"process file {file_path} failed: {str(e)}") + return modified + + +def clean_ndk_ani_headers(ndk_header_path): + if not _ANI_HEADER_LISTS: + print("Warning: ani header file list") + return + + # all files to be processed + file_paths = [] + for root, _, files in os.walk(ndk_header_path): + for file in files: + if not file.endswith('.h'): + continue + + file_path = os.path.join(root, file) + if file in _ANI_HEADER_LISTS: + try: + os.remove(file_path) + print(f"Deleted ani header file: {file_path}") + except OSError as e: + print(f"Error deleting {file_path}: {str(e)}") + else: + file_paths.append(file_path) + + # Bulk processing file include + for file_path in file_paths: + process_header_file(file_path) + + +def clean_json__systemCapability_headers(capability_header_path): + try: + with open(capability_header_path, 'r') as f: + systemCapabilitys = json.load(f) + except Exception as e: + print(f"Error reading JSON file: {str(e)}") + return + + # Traverse all levels of items + for _systemCapability in systemCapabilitys: + # filtering ani header file + systemCapabilitys[_systemCapability] = [item for item in systemCapabilitys[_systemCapability] + if os.path.basename(item) not in _ANI_HEADER_LISTS] + + # Saving the modified JSON + try: + with open(capability_header_path, 'w') as f: + json.dump(systemCapabilitys, f, indent=2) + print("JSON file updated successfully") + except Exception as e: + print(f"Error saving JSON file: {str(e)}") + + +def main(): + parser = argparse.ArgumentParser() + parser.add_argument('--ndk-header-path', help='ndk header path') + parser.add_argument('--system-capability-header-config', required=True) + args = parser.parse_args() + + if not os.path.isdir(args.ndk_header_path): + print(f"Error:path {args.ndk_header_path} is not exist!") + return + + clean_ndk_ani_headers(args.ndk_header_path) + clean_json__systemCapability_headers(args.system_capability_header_config) + print("Ani Header file cleanup complete!") + + +if __name__ == '__main__': + main() diff --git a/build-tools/clean_ndk_ani.pydeps b/build-tools/clean_ndk_ani.pydeps new file mode 100644 index 000000000..9ec5fde3f --- /dev/null +++ b/build-tools/clean_ndk_ani.pydeps @@ -0,0 +1,10 @@ +# Generated by running: +# build/print_python_deps.py --root build/ohos/ndk --output interface/sdk_c/build-tools/clean_ndk_ani.pydeps interface/sdk_c/build-tools/clean_ndk_ani.py +../../../build/gn_helpers.py +../../../build/scripts/__init__.py +../../../build/scripts/interface_mgr.py +../../../build/scripts/util/__init__.py +../../../build/scripts/util/build_utils.py +../../../build/scripts/util/md5_check.py +../../../build/scripts/util/pycache.py +clean_ndk_ani.py -- Gitee