1# Copyright (c) 2020 Huawei Device Co., Ltd.
2# Licensed under the Apache License, Version 2.0 (the "License");
3# you may not use this file except in compliance with the License.
4# You may obtain a copy of the License at
5#
6#     http://www.apache.org/licenses/LICENSE-2.0
7#
8# Unless required by applicable law or agreed to in writing, software
9# distributed under the License is distributed on an "AS IS" BASIS,
10# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11# See the License for the specific language governing permissions and
12# limitations under the License.
13import("//build/lite/ohos_var.gni")
14import("${device_path}/config.gni")
15
16if (target_os == "") {
17  target_os = "ohos"
18}
19
20if (target_cpu == "") {
21  target_cpu = board_cpu
22}
23
24target_arch_cflags = board_cflags
25if (board_arch != "") {
26  target_arch_cflags += [ "-march=$board_arch" ]
27}
28if (board_cpu != "") {
29  target_arch_cflags += [ "-mcpu=$board_cpu" ]
30}
31
32arch = "arm"
33if (ohos_kernel_type == "liteos_a") {
34  target_triple = "$arch-liteos-ohos"
35} else if (ohos_kernel_type == "linux") {
36  target_triple = "$arch-linux-ohos"
37}
38
39if (defined(board_configed_sysroot) && board_configed_sysroot != "") {
40  ohos_current_sysroot = board_configed_sysroot
41}
42
43# Only gcc available for liteos_m.
44if (ohos_kernel_type == "liteos_m" || ohos_kernel_type == "uniproton" ||
45    ohos_kernel_type == "linux") {
46  use_board_toolchain = true
47}
48
49toolchain_cmd_suffix = ""
50if (host_os == "win") {
51  toolchain_cmd_suffix = ".exe"
52}
53
54# enable ccache if ccache is installed,
55# or enable xcache if xcache is installed.
56if (ohos_build_enable_ccache) {
57  compile_prefix = "ccache "
58} else if (ohos_build_enable_xcache) {
59  compile_prefix = "opt/buildtools/nextbuild/xcache "
60} else {
61  compile_prefix = ""
62}
63
64# Load board adapter dir from board config.
65if (board_adapter_dir != "") {
66  ohos_board_adapter_dir = board_adapter_dir
67  ohos_vendor_adapter_dir = board_adapter_dir
68}
69
70# Set current toolchain with to board configuration.
71if (board_toolchain != "" && use_board_toolchain) {
72  ohos_build_compiler = board_toolchain_type
73  if (board_toolchain_path != "") {
74    compile_prefix += "${board_toolchain_path}/${board_toolchain_prefix}"
75  } else {
76    compile_prefix += "${board_toolchain_prefix}"
77  }
78  set_default_toolchain("//build/lite/toolchain:${board_toolchain}")
79  if (board_toolchain_type == "gcc") {
80    ohos_current_cc_command = "${compile_prefix}gcc$toolchain_cmd_suffix"
81    ohos_current_cxx_command = "${compile_prefix}g++$toolchain_cmd_suffix"
82    ohos_current_ar_command = "${compile_prefix}ar$toolchain_cmd_suffix"
83    ohos_current_ld_command = ohos_current_cc_command
84    ohos_current_strip_command =
85        "${compile_prefix}strip$toolchain_cmd_suffix --strip-unneeded"
86    default_target_configs = [ "//build/lite/config:gcc_opt" ]
87  } else if (board_toolchain_type == "clang") {
88    ohos_current_cc_command = "${compile_prefix}clang$toolchain_cmd_suffix"
89    ohos_current_cxx_command = "${compile_prefix}clang++$toolchain_cmd_suffix"
90    compile_prefix += "llvm-"
91    ohos_current_ar_command = "${compile_prefix}ar$toolchain_cmd_suffix"
92    ohos_current_ld_command = ohos_current_cc_command
93    ohos_current_strip_command =
94        "${compile_prefix}strip$toolchain_cmd_suffix --strip-unneeded"
95    default_target_configs = [ "//build/lite/config:clang_opt" ]
96  } else if (board_toolchain_type == "iccarm") {
97    ohos_current_cc_command = "${compile_prefix}iccarm$toolchain_cmd_suffix"
98    ohos_current_cxx_command = "${compile_prefix}iccarm$toolchain_cmd_suffix"
99    ohos_current_ar_command = "${compile_prefix}iarchive$toolchain_cmd_suffix"
100    ohos_current_as_command = "${compile_prefix}iasmarm$toolchain_cmd_suffix"
101    ohos_current_ld_command = "${compile_prefix}ilinkarm$toolchain_cmd_suffix"
102  }
103
104  # Overwrite ld cmd by customed cmd.
105  if (defined(board_customed_ld_cmd) && board_customed_ld_cmd != "") {
106    ohos_current_ld_command = board_customed_ld_cmd
107  }
108} else {
109  # OHOS default toolchain
110  ohos_build_compiler = "clang"
111  ohos_clang_toolchain_dir = rebase_path("${ohos_build_compiler_dir}/bin")
112  compile_prefix += "$ohos_clang_toolchain_dir/"
113  ohos_current_cc_command = "${compile_prefix}clang$toolchain_cmd_suffix"
114  ohos_current_cxx_command = "${compile_prefix}clang++$toolchain_cmd_suffix"
115  compile_prefix += "llvm-"
116  ohos_current_ar_command = "${compile_prefix}ar$toolchain_cmd_suffix"
117  ohos_current_ld_command = ohos_current_cxx_command
118  ohos_current_strip_command =
119      "${compile_prefix}strip$toolchain_cmd_suffix --strip-unneeded"
120  set_default_toolchain("//build/lite/toolchain:linux_x86_64_ohos_clang")
121  default_target_configs = [ "//build/lite/config:ohos_clang" ]
122  default_target_configs += [ "//build/lite/config:clang_opt" ]
123}
124
125if (board_toolchain_type != "iccarm") {
126  default_target_configs += [
127    "//build/lite/config:board_config",
128    "//build/lite/config:cpu_arch",
129    "//build/lite/config:common",
130    "//build/lite/config:default_link_path",
131    "//build/lite/config:language_c",
132    "//build/lite/config:language_cpp",
133    "//build/lite/config:kernel_macros",
134  ]
135  if (ohos_build_type == "debug") {
136    default_target_configs += [ "//build/lite/config:debug" ]
137  } else if (ohos_build_type == "release") {
138    default_target_configs += [ "//build/lite/config:release" ]
139  }
140
141  if (ohos_kernel_type == "liteos_m") {
142    default_target_configs += [ "//build/lite/config:stack_protector" ]
143  }
144}
145
146if (ohos_kernel_type == "liteos_a") {
147  default_target_configs +=
148      [ "//build/lite/config/kernel/liteos/cortex_a:default" ]
149}
150
151if (ohos_kernel_type == "liteos_a" || ohos_kernel_type == "linux") {
152  default_target_configs += [
153    "//build/lite/config:security",
154    "//build/lite/config:exceptions",
155  ]
156}
157default_shared_library_configs =
158    default_target_configs + [ "//build/lite/config:shared_library_config" ]
159default_static_library_configs = default_target_configs
160default_executable_configs = default_static_library_configs
161if (ohos_kernel_type != "liteos_m" && ohos_kernel_type != "uniproton") {
162  default_static_library_configs += [ "//build/lite/config:static_pie_config" ]
163  default_executable_configs += [ "//build/lite/config:static_pie_config" ]
164  default_executable_configs += [ "//build/lite/config:pie_executable_config" ]
165}
166
167set_defaults("executable") {
168  configs = default_executable_configs
169  configs += [ "//build/lite/config:board_exe_ld_flags" ]
170}
171
172set_defaults("static_library") {
173  configs = default_static_library_configs
174}
175
176set_defaults("shared_library") {
177  configs = default_shared_library_configs
178}
179
180set_defaults("source_set") {
181  configs = default_target_configs
182}
183
184_target_type_list = [
185  "executable",
186  "static_library",
187  "shared_library",
188  "source_set",
189  "action",
190  "action_foreach",
191]
192
193foreach(_target_type, _target_type_list) {
194  template(_target_type) {
195    target(_target_type, target_name) {
196      forward_variables_from(invoker, "*", [ "no_default_deps" ])
197      if (!defined(deps)) {
198        deps = []
199      }
200      if (!defined(invoker.no_default_deps) || !invoker.no_default_deps) {
201        deps += [ "//build/lite:prebuilts" ]
202      }
203    }
204  }
205}
206