• 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 optparse
17import os
18import sys
19import zipfile
20
21sys.path.append(os.path.join(os.path.dirname(__file__), os.pardir, os.pardir))
22from scripts.util import build_utils  # 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('--os-irrelevant-dir',
32                      help='base directory of ndk common files')
33    parser.add_option('--os-specific-dir',
34                      help='base directory of os specific stuff')
35    parser.add_option('--prefix',
36                      help='prefix string of directory in archive zipfile')
37    parser.add_option('--notice-file', help='path to notice file')
38    parser.add_option('--record-path', help='path to md5.stamp file')
39
40    options, _ = parser.parse_args(args)
41    return options
42
43
44def do_archive(output, directory, prefix, compress_fn):
45    files = []
46    for root, _, filenames in os.walk(directory):
47        for f in filenames:
48            files.extend([os.path.join(root, f)])
49    with zipfile.ZipFile(output, 'a') as outfile:
50        for f in files:
51            compress = compress_fn(f) if compress_fn else None
52            if prefix:
53                zip_path = os.path.join(prefix, os.path.relpath(f, directory))
54            else:
55                zip_path = os.path.relpath(f, directory)
56            build_utils.add_to_zip_hermetic(outfile,
57                                         zip_path,
58                                         src_path=f,
59                                         compress=compress)
60
61
62def archive_ndk(output, os_irrelevant_dir, os_specific_dir, prefix,
63                compress_fn, notice):
64    # Create an empty zipfile first, then add stuff to it.
65    with zipfile.ZipFile(output, 'w') as outfile:
66        pass
67    for directory in [os_irrelevant_dir, os_specific_dir]:
68        do_archive(output, directory, prefix, compress_fn)
69
70    with zipfile.ZipFile(output, 'a') as zip_file:
71        compress = compress_fn(notice) if compress_fn else None
72        if prefix:
73            zip_path = os.path.join(prefix, os.path.basename(notice))
74        else:
75            zip_path = os.path.basename(notice)
76        build_utils.add_to_zip_hermetic(zip_file,
77                                     zip_path,
78                                     src_path=notice,
79                                     compress=compress)
80
81
82def main(args):
83    options = parse_args(args)
84
85    os_irrelevant_dir = options.os_irrelevant_dir
86    os_specific_dir = options.os_specific_dir
87    depfile_deps = set(
88        build_utils.get_all_files(os_irrelevant_dir) +
89        build_utils.get_all_files(os_specific_dir))
90    depfile_deps.add(options.notice_file)
91
92    build_utils.call_and_write_depfile_if_stale(lambda: archive_ndk(
93        options.output, os_irrelevant_dir, os_specific_dir, options.prefix,
94        lambda _: True, options.notice_file),
95                                           options,
96                                           depfile_deps=depfile_deps,
97                                           input_paths=depfile_deps,
98                                           output_paths=([options.output]),
99                                           record_path=options.record_path,
100                                           force=False,
101                                           add_pydeps=False)
102
103
104if __name__ == '__main__':
105    sys.exit(main(sys.argv[1:]))
106