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 sys
17import os
18import argparse
19import shutil
20
21WORK_SPACE = os.path.dirname(os.path.abspath(__file__))
22PROJECT_DIR = os.path.dirname(os.path.dirname(os.path.dirname(WORK_SPACE)))
23
24sys.path.append(os.path.join(PROJECT_DIR, 'build'))
25sys.path.append(os.path.join(PROJECT_DIR, 'build/hb'))
26from hb.util import log_util  # noqa: E402
27from scripts.util import file_utils  # noqa: E402
28from scripts.util import build_utils  # noqa: E402
29
30INTERFACE_DIR = os.path.join(WORK_SPACE, 'ohos_interface')
31INTERFACE_INCLUDE_DIR = os.path.join(INTERFACE_DIR, 'include')
32INTERFACE_OHOS_GLUE_DIR = os.path.join(INTERFACE_DIR, 'ohos_glue')
33
34def copy_dir(src_dir: str, dst_dir: str):
35    log_util.LogUtil.hb_info("begin to copy dir from '{}' to '{}'".format(src_dir, dst_dir))
36    if os.path.isdir(dst_dir):
37        shutil.rmtree(dst_dir)
38
39    if os.path.isdir(src_dir) and os.listdir(src_dir):
40        shutil.copytree(src_dir, dst_dir)
41
42    source_files = []
43    for root, dirs, files in os.walk(src_dir):
44        for name in files:
45            source_files.append(os.path.join(root, name))
46    return source_files
47
48def copy_files(src_dir: str, dst_dir: str):
49    log_util.LogUtil.hb_info("begin to copy files from '{}' to '{}'".format(src_dir, dst_dir))
50    source_files = []
51    for item in os.listdir(src_dir):
52        src_file = os.path.join(src_dir, item)
53        dst_file = os.path.join(dst_dir, item)
54        if os.path.isfile(src_file):
55            source_files.append(src_file)
56            shutil.copy2(src_file, dst_file)
57    return source_files
58
59def copy_include():
60    log_util.LogUtil.hb_info("begin to copy include dir")
61    nweb_include = os.path.join('ohos_nweb', 'include')
62    include_source_files = copy_files(os.path.join(INTERFACE_INCLUDE_DIR, 'ohos_nweb'),
63            os.path.join(WORK_SPACE, nweb_include))
64
65    adapter_include = os.path.join('ohos_adapter', 'interfaces')
66    include_source_files += copy_dir(os.path.join(INTERFACE_INCLUDE_DIR, 'ohos_adapter'),
67            os.path.join(WORK_SPACE, adapter_include))
68    return include_source_files
69
70def copy_glue_base(glue_dir: str):
71    log_util.LogUtil.hb_info("begin to copy glue base dir")
72    base_dir = os.path.join(glue_dir, 'base')
73    base_source_files = copy_dir(os.path.join(INTERFACE_OHOS_GLUE_DIR, 'base'), base_dir)
74
75    script_dir = os.path.join(glue_dir, 'scripts')
76    base_source_files += copy_dir(os.path.join(INTERFACE_OHOS_GLUE_DIR, 'scripts'), script_dir)
77    return base_source_files
78
79def copy_glue_module(glue_dir: str, module_name: str):
80    dir_name = 'ohos_' + module_name;
81    log_util.LogUtil.hb_info("begin to copy glue '{}' dir".format(dir_name))
82    dst_dir = os.path.join(glue_dir, dir_name)
83    if os.path.isdir(dst_dir):
84        shutil.rmtree(dst_dir)
85
86    src_dir = os.path.join(INTERFACE_OHOS_GLUE_DIR, dir_name)
87    module_source_files = copy_dir(os.path.join(src_dir, 'include'), os.path.join(dst_dir, 'include'))
88    module_source_files += copy_dir(os.path.join(os.path.join(src_dir, 'bridge'), 'webview'),
89            os.path.join(dst_dir, 'bridge'))
90    module_source_files += copy_dir(os.path.join(os.path.join(src_dir, 'cpptoc'), 'webview'),
91            os.path.join(dst_dir, 'cpptoc'))
92    module_source_files += copy_dir(os.path.join(os.path.join(src_dir, 'ctocpp'), 'webview'),
93            os.path.join(dst_dir, 'ctocpp'))
94    return module_source_files
95
96def main():
97    parser = argparse.ArgumentParser()
98    parser.add_argument('--command-type', required=True)
99    parser.add_argument('--ohos-glue-dir', required=True)
100    parser.add_argument('--outfile', required=True)
101    parser.add_argument('--depfile', required=False)
102    args = parser.parse_args()
103
104    if args.command_type == "include":
105        source_file_list = copy_include()
106    elif args.command_type == "base":
107        source_file_list = copy_glue_base(args.ohos_glue_dir)
108    else:
109        source_file_list = copy_glue_module(args.ohos_glue_dir, args.command_type)
110
111    file_utils.write_file(args.outfile, '\n'.join(source_file_list))
112
113    if args.depfile:
114        _dep_files = []
115        _dep_files.extend(source_file_list)
116        _dep_files.sort()
117        build_utils.write_depfile(args.depfile,
118                                  args.outfile,
119                                  _dep_files,
120                                  add_pydeps=False)
121
122if __name__ == '__main__':
123    sys.exit(main())
124
125