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 argparse
17import sys
18import os
19
20sys.path.append(
21    os.path.dirname(
22        os.path.dirname(os.path.dirname(os.path.abspath(__file__)))))
23from scripts.util.file_utils import write_file, read_json_file  # noqa: E402
24
25# Import jinja2 from third_party/jinja2
26sys.path.insert(1, os.path.join(os.path.abspath(
27                   os.path.join(os.path.dirname(__file__),
28                   '..', '..', '..')), 'third_party'))
29from jinja2 import Template  # noqa: E402  # pylint: disable=F0401
30
31
32def read_display_name(type2displayname):
33    return read_json_file(type2displayname)
34
35
36def main():
37    parser = argparse.ArgumentParser()
38
39    parser.add_argument('--sdk-systems', action='append')
40    parser.add_argument('--sdk-modules')
41    parser.add_argument('--sdk-types', action='append')
42    parser.add_argument('--current-dir')
43    parser.add_argument('--generated-sdk-modules-gni')
44    parser.add_argument('--type2displayname')
45    parser.add_argument('--api-version')
46    parser.add_argument('--release-type')
47    parser.add_argument('--meta-version')
48    parser.add_argument('--sdk-class')
49    parser.add_argument('--use-current-sdk', action='store_true')
50
51    parser.add_argument('--output', required=True)
52
53    options = parser.parse_args()
54
55    template = Template("""#Generated code, DONOT modify it.
56import("//build/ohos/build_var.gni")
57import("//build/ohos/sdk/sdk.gni")
58import("${build_configs_path}/platforms_list.gni")
59import("{{ generated_sdk_modules_gni }}")
60
61{% for sdk_type in sdk_types %}
62  {% set _sdk_type = sdk_type.replace('-', '_') %}
63  if (defined({{ ohos_sdk_modules }}.{{ _sdk_type }})) {
64    {{ _sdk_type }}s = {{ ohos_sdk_modules }}. {{ _sdk_type }}
65    group("{{ _sdk_type }}") {
66      public_deps = []
67        {% for os in sdk_systems %}
68        public_deps += [":{{ _sdk_type }}_{{ os }}"]
69        {% endfor %}
70    }
71
72    {% for os in sdk_systems %}
73    make_{{ os }}_sdk_modules("{{ _sdk_type }}_{{ os }}") {
74      {% if use_current_sdk %}
75      sdk_class = "{{ sdk_class }}"
76      {% endif %}
77      sdk_type = "{{ sdk_type }}"
78      sdk_modules = {{ _sdk_type }}s.{{ os }}
79      {% if release_type != "" %}
80      zipfile_name =
81          "${sdk_type}-${{ "{" }}sdk_system_{{ os }}{{ "}" }}-${arch}-${current_sdk_version}-${release_type}.zip"
82      {% else %}
83      zipfile_name =
84          "${sdk_type}-${{ "{" }}sdk_system_{{ os }}{{ "}" }}-${arch}-${current_sdk_version}.zip"
85      {% endif %}
86    }
87    {% endfor %}
88  }
89{% endfor %}
90
91foreach(os, sdk_systems) {
92  {% for sdk_type in sdk_types %}
93    {% if display_name.get(sdk_type) %}
94        {% set _display_name = display_name.get(sdk_type) %}
95    {% else %}
96        {% set _display_name = sdk_type.capitalize() %}
97    {% endif %}
98    if (defined(ext_ndk_config_file) && ext_ndk_config_file != "") {
99      package_info_file =
100        "$ohos_sdk_copy_dir/$os/{{ sdk_type }}/uni-package.json"
101    } else {
102      package_info_file =
103          "$ohos_sdk_copy_dir/$os/{{ sdk_type }}/oh-uni-package.json"
104    }
105    {% if use_current_sdk %}
106    # override for current sdk.
107    package_info_file =
108      "$root_out_dir/{{ sdk_class }}-current-sdk/$os/$api_version/{{ sdk_type }}/oh-uni-package.json"
109    {% if sdk_class != "base" %}
110    package_info_file =
111      "$root_out_dir/{{ sdk_class }}-current-sdk/$os/$api_version/{{ sdk_type }}/uni-package.json"
112    {% endif %}
113    {% endif %}
114    package_info = {}
115    package_info = {
116      path = "{{ sdk_type }}"
117      displayName = "{{ _display_name }}"
118      version = current_sdk_version
119      apiVersion = "{{ api_version }}"
120      if (defined(ext_ndk_config_file) && ext_ndk_config_file != "") {
121        platformVersion = platform_version
122      }
123
124      {% if release_type != "" %}
125      releaseType = "{{ release_type }}"
126      {% endif %}
127
128      {% if meta_version != "" %}
129      meta = {
130        metaVersion = "{{ meta_version }}"
131      }
132      {% endif %}
133    }
134    write_file(package_info_file, package_info, "json")
135  {% endfor %}
136}
137
138group("generated_ohos_sdk") {
139  public_deps = []
140  {% for sdk_type in sdk_types %}
141    {% set _sdk_type = sdk_type.replace('-', '_') %}
142    if (defined({{ ohos_sdk_modules }}.{{ _sdk_type }})) {
143      public_deps += [ ":{{ _sdk_type }}" ]
144    }
145  {% endfor %}
146  public_deps += {{ ohos_sdk_modules }}.extras
147}""",  # noqa E501
148                        trim_blocks=True,
149                        lstrip_blocks=True)
150
151    contents = template.render(
152        ohos_sdk_modules=options.sdk_modules,
153        sdk_types=options.sdk_types,
154        current_dir=options.current_dir,
155        sdk_systems=options.sdk_systems,
156        display_name=read_display_name(options.type2displayname),
157        api_version=options.api_version,
158        release_type=options.release_type,
159        meta_version=options.meta_version,
160        generated_sdk_modules_gni=options.generated_sdk_modules_gni,
161        sdk_class=options.sdk_class,
162        use_current_sdk=options.use_current_sdk)
163    write_file(options.output, contents)
164
165
166if __name__ == '__main__':
167    sys.exit(main())
168