• 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 shutil
20import tempfile
21import json
22
23from util import build_utils  # noqa: E402
24
25
26def parse_args(args):
27    args = build_utils.expand_file_args(args)
28
29    parser = optparse.OptionParser()
30    build_utils.add_depfile_option(parser)
31    parser.add_option('--output-resources-zipfile',
32                      help='path to packaged resources')
33    parser.add_option('--output-header-file',
34                      help='path to generated ResourceTable.h')
35    parser.add_option('--resources-dir', help='resources directory')
36    parser.add_option('--restool-path', help='path to restool')
37    parser.add_option('--hap-profile', help='path to hap profile')
38    parser.add_option('--package-name', help='package name of resource file')
39
40    options, _ = parser.parse_args(args)
41    options.resources_dir = build_utils.parse_gn_list(options.resources_dir)
42    return options
43
44
45def get_package_name_from_profile(profile):
46    with open(profile) as fp:
47        return json.load(fp)['module']['package']
48
49
50def compile_resources(options):
51    with build_utils.temp_dir() as build:
52        res_dir = os.path.join(build, 'resources')
53        gen_dir = os.path.join(build, 'gen')
54        header_dir = os.path.join(build, 'header')
55        os.makedirs(res_dir)
56        os.makedirs(gen_dir)
57        os.makedirs(header_dir)
58
59        for directory in options.resources_dir:
60            shutil.copytree(directory,
61                            os.path.join(res_dir, os.path.basename(directory)))
62        cmd = [options.restool_path, '-i', res_dir]
63        shutil.copy(options.hap_profile, os.path.join(res_dir, 'config.json'))
64        if options.package_name != "" and options.package_name is not None:
65            package_name = options.package_name
66        else:
67            package_name = get_package_name_from_profile(options.hap_profile)
68        generated_header_file = os.path.join(
69            header_dir, os.path.basename(options.output_header_file))
70        cmd.extend(
71            ['-p', package_name, '-o', gen_dir, '-r', generated_header_file])
72        build_utils.check_output(cmd)
73        R_txt_path = os.path.join(gen_dir, 'R.txt')
74        if os.path.exists(R_txt_path):
75            os.unlink(R_txt_path)
76        if options.output_resources_zipfile:
77            build_utils.zip_dir(options.output_resources_zipfile, gen_dir)
78        if options.output_header_file:
79            shutil.copy(generated_header_file, options.output_header_file)
80
81
82def main(args):
83    options = parse_args(args)
84
85    inputs = ([options.restool_path, options.hap_profile])
86    depfiles = []
87    for directory in options.resources_dir:
88        depfiles += (build_utils.get_all_files(directory))
89
90    input_strings = [options.package_name] if options.package_name else []
91    outputs = []
92    if options.output_resources_zipfile:
93        outputs.append(options.output_resources_zipfile)
94    if options.output_header_file:
95        outputs.append(options.output_header_file)
96    build_utils.call_and_write_depfile_if_stale(
97        lambda: compile_resources(options),
98        options,
99        depfile_deps=depfiles,
100        input_paths=inputs + depfiles,
101        input_strings=input_strings,
102        output_paths=(outputs),
103        force=False,
104        add_pydeps=False)
105
106
107if __name__ == '__main__':
108    sys.exit(main(sys.argv[1:]))
109