• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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, dirs_exist_ok=True)
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    if not os.path.exists(dst_dir):
52        os.makedirs(dst_dir)
53
54    for item in os.listdir(src_dir):
55        src_file = os.path.join(src_dir, item)
56        dst_file = os.path.join(dst_dir, item)
57        if os.path.isfile(src_file):
58            source_files.append(src_file)
59            shutil.copy2(src_file, dst_file)
60    return source_files
61
62
63def copy_include(src_dir: str):
64    log_util.LogUtil.hb_info("begin to copy include dir")
65    nweb_include = os.path.join('ohos_nweb', 'include')
66    include_source_files = copy_files(os.path.join(INTERFACE_INCLUDE_DIR, 'ohos_nweb'),
67            os.path.join(src_dir, '..', nweb_include))
68
69    adapter_include = os.path.join('ohos_adapter', 'interfaces')
70    include_source_files += copy_dir(os.path.join(WORK_SPACE, 'ohos_adapter'),
71            os.path.join(src_dir, '..', 'ohos_adapter'))
72    include_source_files += copy_dir(os.path.join(INTERFACE_INCLUDE_DIR, 'ohos_adapter'),
73            os.path.join(src_dir, '..', adapter_include))
74    include_source_files = copy_files(os.path.join(WORK_SPACE, nweb_include),
75            os.path.join(src_dir, '..', nweb_include))
76
77    return include_source_files
78
79def copy_glue_base(glue_dir: str):
80    log_util.LogUtil.hb_info("begin to copy glue base dir")
81    base_dir = os.path.join(glue_dir, 'base')
82    base_source_files = copy_dir(os.path.join(INTERFACE_OHOS_GLUE_DIR, 'base'), base_dir)
83
84    script_dir = os.path.join(glue_dir, 'scripts')
85    base_source_files += copy_dir(os.path.join(INTERFACE_OHOS_GLUE_DIR, 'scripts'), script_dir)
86    return base_source_files
87
88def copy_glue_module(glue_dir: str, module_name: str):
89    dir_name = 'ohos_' + module_name;
90    log_util.LogUtil.hb_info("begin to copy glue '{}' dir".format(dir_name))
91    dst_dir = os.path.join(glue_dir, dir_name)
92    if os.path.isdir(dst_dir):
93        shutil.rmtree(dst_dir)
94
95    src_dir = os.path.join(INTERFACE_OHOS_GLUE_DIR, dir_name)
96    module_source_files = copy_dir(os.path.join(src_dir, 'include'), os.path.join(dst_dir, 'include'))
97    module_source_files += copy_dir(os.path.join(os.path.join(src_dir, 'bridge'), 'webview'),
98            os.path.join(dst_dir, 'bridge'))
99    module_source_files += copy_dir(os.path.join(os.path.join(src_dir, 'cpptoc'), 'webview'),
100            os.path.join(dst_dir, 'cpptoc'))
101    module_source_files += copy_dir(os.path.join(os.path.join(src_dir, 'ctocpp'), 'webview'),
102            os.path.join(dst_dir, 'ctocpp'))
103    return module_source_files
104
105def main():
106    parser = argparse.ArgumentParser()
107    parser.add_argument('--command-type', required=True)
108    parser.add_argument('--ohos-glue-dir', required=True)
109    parser.add_argument('--outfile', required=True)
110    parser.add_argument('--depfile', required=False)
111    args = parser.parse_args()
112
113    if args.command_type == "include":
114        source_file_list = copy_include(args.ohos_glue_dir)
115    elif args.command_type == "base":
116        source_file_list = copy_glue_base(args.ohos_glue_dir)
117    else:
118        source_file_list = copy_glue_module(args.ohos_glue_dir, args.command_type)
119
120    file_utils.write_file(args.outfile, '\n'.join(source_file_list))
121
122    if args.depfile:
123        _dep_files = []
124        _dep_files.extend(source_file_list)
125        _dep_files.sort()
126        build_utils.write_depfile(args.depfile,
127                                  args.outfile,
128                                  _dep_files,
129                                  add_pydeps=False)
130
131if __name__ == '__main__':
132    sys.exit(main())
133
134