1#!/usr/bin/env python
2# -*- coding: utf-8 -*-
3# Copyright (c) 2024 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
16template("hc_gen") {
17  assert(defined(invoker.sources), "sources are must")
18  if (defined(invoker.outputs)) {
19    foreach(o, invoker.outputs) {
20      if (o == string_replace(o, "{{", "")) {
21        specified_output_name = true
22      }
23    }
24  }
25  if (defined(specified_output_name) && specified_output_name) {
26    target_type = "action"
27  } else {
28    target_type = "action_foreach"
29  }
30
31  # get all hcs file by sources
32  hcs_inputs = exec_script("//build/config/components/hc_gen/hcs_build_info.py",
33                           rebase_path(invoker.sources),
34                           "list lines")
35
36  target(target_type, target_name) {
37    script = "/usr/bin/env"
38    if (defined(ohos_lite)) {
39      script = "//build/lite/run_shell_cmd.py"
40    }
41    inputs = hcs_inputs
42    sources = invoker.sources
43    if (defined(invoker.hc_gen_hex) && invoker.hc_gen_hex) {
44      hc_flags = [
45        "-b",
46        "-i",
47        "-a",
48      ]
49      output_suffix = "_hex.c"
50      output_suffix2 = ".hcb"
51    } else if (defined(invoker.hc_gen_c) && invoker.hc_gen_c) {
52      hc_flags = [ "-t" ]
53      output_suffix = ".c"
54      output_suffix2 = ".h"
55    } else if (defined(invoker.hc_gen_macro) && invoker.hc_gen_macro) {
56      hc_flags = [ "-m" ]
57      output_suffix = ".h"
58    } else if (defined(invoker.hc_gen_start_cfg) && invoker.hc_gen_start_cfg) {
59      hc_flags = [ "-s" ]
60      output_suffix = ".cfg"
61    } else {
62      hc_flags = []
63      output_suffix = ".hcb"
64    }
65
66    if (defined(invoker.outputs)) {
67      outputs = invoker.outputs
68    } else {
69      outputs = [ "$target_gen_dir/{{source_name_part}}$output_suffix" ]
70    }
71    if (defined(output_suffix2)) {
72      outputs += [ string_replace(outputs[0], output_suffix, output_suffix2) ]
73    }
74    if (target_type == "action") {
75      src = rebase_path(sources[0], root_build_dir)
76    } else {
77      src = "{{source}}"
78    }
79
80    args = [ rebase_path("//prebuilts/hc_gen/hc-gen") ]
81    args += hc_flags
82    args += [
83      "-o",
84      rebase_path(string_replace(outputs[0], output_suffix, "")),
85      src,
86    ]
87  }
88}
89
90template("hc_gen_c") {
91  hc_gen_c = true
92  hc_gen(target_name) {
93    forward_variables_from(invoker, "*")
94  }
95}
96
97template("hc_gen_hex") {
98  hc_gen_hex = true
99  hc_gen(target_name) {
100    forward_variables_from(invoker, "*")
101  }
102}
103
104template("hc_gen_macro") {
105  hc_gen_macro = true
106  hc_gen(target_name) {
107    forward_variables_from(invoker, "*")
108  }
109}
110
111template("hc_gen_start_cfg") {
112  hc_gen_start_cfg = true
113  hc_gen(target_name) {
114    forward_variables_from(invoker, "*")
115  }
116}
117