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 re 20import tempfile 21import distutils.spawn 22import shutil 23 24sys.path.append(os.path.join(os.path.dirname(__file__), os.pardir, os.pardir)) 25from scripts.util import build_utils # noqa: E402 26 27 28 29def parse_args(args): 30 args = build_utils.expand_file_args(args) 31 32 parser = optparse.OptionParser() 33 build_utils.add_depfile_option(parser) 34 parser.add_option('--version', help='software version') 35 parser.add_option('--doxygen-file', help='doxygen config file') 36 parser.add_option('--output', help='output index.html') 37 parser.add_option('--record-path', help='path to md5.stamp file') 38 parser.add_option( 39 '--working-dir', 40 help='the directory where doxygen command will be executed') 41 42 options, _ = parser.parse_args(args) 43 return options 44 45 46def generate_ndk_docs(options, html_output_dir: str): 47 contents = None 48 with tempfile.NamedTemporaryFile( 49 suffix=os.path.basename(options.doxygen_file)) as doxygen_file: 50 shutil.copyfile(options.doxygen_file, doxygen_file.name) 51 with open(doxygen_file.name, 'r') as f: 52 contents = f.read() 53 if contents is None: 54 raise Exception('Failed to read %s' % options.doxygen_file) 55 56 keys = { 57 '%VERSION%': 58 options.version, 59 '%OUTPUT_DIR%': 60 os.path.relpath(html_output_dir, options.working_dir) 61 } 62 for (k, v) in list(keys.items()): 63 v = v.replace('\\', '\\\\') 64 contents = re.sub(k, v, contents) 65 66 with open(doxygen_file.name, 'w') as f: 67 f.write(contents) 68 69 old_cwd = os.getcwd() 70 try: 71 # if no ndk headers exist, return. 72 if os.path.exists(options.working_dir) is not True: 73 print("no ndk headers exist, return") 74 return 75 os.chdir(options.working_dir) 76 77 doxygen_path = distutils.spawn.find_executable('doxygen') 78 if doxygen_path is None: 79 print( 80 "Warning: Failed to find doxygen, please install doxygen with \"sudo apt-get install doxygen\" on Ubuntu" 81 ) 82 return 83 os.makedirs( 84 os.path.relpath(html_output_dir, options.working_dir), 85 exist_ok=True) 86 cmd = [doxygen_path, doxygen_file.name] 87 build_utils.check_output(cmd) 88 finally: 89 os.chdir(old_cwd) 90 91 92def main(args): 93 options = parse_args(args) 94 95 depfile_deps = ([options.doxygen_file]) 96 for root, _, filenames in os.walk(options.working_dir): 97 for f in filenames: 98 depfile_deps += ([os.path.join(root, f)]) 99 100 html_output_dir = options.output 101 build_utils.call_and_write_depfile_if_stale( 102 lambda: generate_ndk_docs(options, html_output_dir), 103 options, 104 depfile_deps=depfile_deps, 105 input_paths=depfile_deps, 106 input_strings=[options.version, options.working_dir], 107 output_paths=([html_output_dir]), 108 record_path=options.record_path, 109 force=False, 110 add_pydeps=False) 111 112 113if __name__ == '__main__': 114 sys.exit(main(sys.argv[1:])) 115