• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Copyright (c) 2013-2019 Huawei Technologies Co., Ltd. All rights reserved.
2# Copyright (c) 2020-2021 Huawei Device Co., Ltd. All rights reserved.
3#
4# Redistribution and use in source and binary forms, with or without modification,
5# are permitted provided that the following conditions are met:
6#
7# 1. Redistributions of source code must retain the above copyright notice, this list of
8#    conditions and the following disclaimer.
9#
10# 2. Redistributions in binary form must reproduce the above copyright notice, this list
11#    of conditions and the following disclaimer in the documentation and/or other materials
12#    provided with the distribution.
13#
14# 3. Neither the name of the copyright holder nor the names of its contributors may be used
15#    to endorse or promote products derived from this software without specific prior written
16#    permission.
17#
18# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
20# THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
21# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
22# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
23# EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
25# OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
26# WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
27# OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
28# ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29
30import("//build/lite/config/component/lite_component.gni")
31
32LITEOS_MENUCONFIG_H = rebase_path("$root_out_dir/config.h")
33
34declare_args() {
35  liteos_name = "OHOS_Image"
36  liteos_kernel_only = false
37}
38
39import("liteos.gni")
40
41liteos_arch_cflags = []
42if (defined(LOSCFG_ARCH_ARM)) {
43  mcpu = LOSCFG_ARCH_CPU
44  if (defined(LOSCFG_ARCH_ARM_AARCH64) && defined(LOSCFG_ARCH_FPU_DISABLE)) {
45    mcpu += "+nofp"
46  }
47  liteos_arch_cflags += [ "-mcpu=$mcpu" ]
48  if (defined(LOSCFG_ARCH_ARM_AARCH32) && defined(LOSCFG_ARCH_FPU)) {
49    liteos_arch_cflags += [ "-mfpu=$LOSCFG_ARCH_FPU" ]
50  }
51}
52
53cc = "$ohos_current_cc_command " + string_join(" ", liteos_arch_cflags)
54if (ohos_build_compiler == "clang") {
55  cc += " --target=$target_triple"
56}
57
58config("arch_config") {
59  cflags = liteos_arch_cflags
60  asmflags = cflags
61  ldflags = cflags
62  if (defined(LOSCFG_ARCH_ARM_AARCH32)) {
63    if (!defined(LOSCFG_COMPILER_CLANG_LLVM)) {
64      cflags += [ "-mthumb-interwork" ]
65    }
66  }
67  if (defined(LOSCFG_THUMB)) {
68    cflags += [ "-mthumb" ]
69    if (defined(LOSCFG_COMPILER_CLANG_LLVM)) {
70      cflags += [ "-mimplicit-it=thumb" ]
71    } else {
72      cflags += [ "-Wa,-mimplicit-it=thumb" ]
73    }
74  }
75}
76
77config("stdinc_config") {
78  std_include = exec_script("//build/lite/run_shell_cmd.py",
79                            [ "$cc -print-file-name=include" ],
80                            "trim string")
81  cflags = [
82    "-isystem",
83    std_include,
84  ]
85  cflags += [ "-nostdinc" ]
86  asmflags = cflags
87  if (defined(LOSCFG_LIBC_NEWLIB)) {
88    cflags -= [ "-nostdinc" ]
89  }
90}
91
92config("ssp_config") {
93  cflags = []
94  if (defined(LOSCFG_CC_STACKPROTECTOR_ALL)) {
95    cflags += [ "-fstack-protector-all" ]
96  } else if (defined(LOSCFG_CC_STACKPROTECTOR_STRONG)) {
97    cflags += [ "-fstack-protector-strong" ]
98  } else if (defined(LOSCFG_CC_STACKPROTECTOR)) {
99    cflags += [
100      "-fstack-protector",
101      "--param",
102      "ssp-buffer-size=4",
103    ]
104  } else {
105    cflags += [ "-fno-stack-protector" ]
106  }
107  asmflags = cflags
108}
109
110config("optimize_config") {
111  cflags = []
112  if (defined(LOSCFG_COMPILE_DEBUG)) {
113    cflags += [
114      "-g",
115      "-gdwarf-2",
116    ]
117    optimization_cflag = "-O0"
118  }
119  if (defined(LOSCFG_COMPILE_OPTIMIZE)) {
120    optimization_cflag = "-O2"
121  }
122  if (defined(LOSCFG_COMPILE_OPTIMIZE_SIZE)) {
123    if (defined(LOSCFG_COMPILER_CLANG_LLVM)) {
124      optimization_cflag = "-Oz"
125    } else {
126      optimization_cflag = "-Os"
127    }
128  }
129  if (defined(LOSCFG_COMPILE_LTO)) {
130    if (defined(LOSCFG_COMPILER_CLANG_LLVM)) {
131      cflags += [ "-flto=thin" ]
132    } else {
133      #cflags += [ "-flto" ]
134    }
135  }
136  cflags += [ optimization_cflag ]
137  asmflags = cflags
138}
139
140config("kconfig_config") {
141  cflags = [
142    "-imacros",
143    "$LITEOS_MENUCONFIG_H",
144  ]
145  asmflags = cflags
146  cflags_cc = cflags
147}
148
149config("warn_config") {
150  cflags = [
151    "-Wall",
152    "-Werror",
153    "-Wpointer-arith",
154    "-Wstrict-prototypes",
155    "-Winvalid-pch",
156    "-Wno-address-of-packed-member",
157  ]
158  asmflags = cflags
159}
160
161config("dialect_config") {
162  cflags_c = [ "-std=c99" ]
163  cflags_cc = [ "-std=c++11" ]
164}
165
166config("misc_config") {
167  defines = [ "__LITEOS__" ]
168  defines += [ "__LITEOS_M__" ]
169  if (!defined(LOSCFG_DEBUG_VERSION)) {
170    defines += [ "NDEBUG" ]
171  }
172
173  cflags = [
174    "-fno-pic",
175    "-fno-builtin",
176    "-fms-extensions",
177    "-fno-strict-aliasing",
178    "-fno-common",
179    "-fsigned-char",
180    "-ffunction-sections",
181    "-fdata-sections",
182    "-fno-exceptions",
183    "-fno-omit-frame-pointer",
184  ]
185
186  if (!defined(LOSCFG_COMPILER_CLANG_LLVM)) {
187    cflags += [ "-fno-aggressive-loop-optimizations" ]
188  }
189
190  asmflags = cflags
191  asmflags += [ "-DCLZ=CLZ" ]
192}
193
194config("los_config") {
195  configs = [
196    #":arch_config",
197    ":kconfig_config",
198
199    ":stdinc_config",
200    ":dialect_config",
201    ":optimize_config",
202    ":ssp_config",
203
204    #":warn_config",
205    ":misc_config",
206  ]
207}
208
209cmd = "if [ -f $device_path/BUILD.gn ]; then echo true; else echo false; fi"
210HAVE_DEVICE_SDK = exec_script("//build/lite/run_shell_cmd.py", [ cmd ], "value")
211
212# board and soc decoupling feature, device_path should contains board
213BOARD_SOC_FEATURE = device_path != string_replace(device_path, "/board/", "")
214
215config("public") {
216  configs = [
217    "arch:public",
218    "kernel:public",
219    "kal:public",
220    "components:public",
221    "utils:public",
222  ]
223
224  if (BOARD_SOC_FEATURE) {
225    configs += [ "//device/board/$device_company:public" ]
226    configs += [ "//device/soc/$LOSCFG_SOC_COMPANY:public" ]
227  } else {
228    if (HAVE_DEVICE_SDK) {
229      configs += [ "$device_path:public" ]
230    }
231  }
232}
233
234group("modules") {
235  deps = [
236    "arch",
237    "components",
238    "kal",
239    "kernel",
240    "testsuites",
241    "utils",
242    HDFTOPDIR,
243  ]
244
245  if (BOARD_SOC_FEATURE) {
246    deps += [ "//device/board/$device_company" ]
247    deps += [ "//device/soc/$LOSCFG_SOC_COMPANY" ]
248  } else {
249    if (HAVE_DEVICE_SDK) {
250      deps += [ device_path ]
251    }
252  }
253}
254
255# when HAVE_DEVICE_SDK is not reached, gn raises an error. so we just use it as
256# not needed
257not_needed([ "HAVE_DEVICE_SDK" ])
258
259static_library("libkernel") {
260  deps = [ ":modules" ]
261  complete_static_lib = false
262}
263
264group("kernel") {
265  deps = [ ":libkernel" ]
266}
267
268group("liteos_m") {
269}
270
271executable("liteos") {
272  configs += [
273    ":public",
274    ":los_config",
275  ]
276
277  ldflags = [
278    "-static",
279    "-Wl,--gc-sections",
280    "-Wl,-Map=$liteos_name.map",
281  ]
282
283  output_dir = target_out_dir
284
285  if (liteos_kernel_only) {
286    deps = [ ":kernel" ]
287  } else {
288    deps = [ "//build/lite:ohos" ]
289  }
290}
291
292copy("copy_liteos") {
293  deps = [ ":liteos" ]
294  sources = [ "$target_out_dir/unstripped/bin/liteos" ]
295  outputs = [ "$root_out_dir/$liteos_name" ]
296}
297
298build_ext_component("build_kernel_image") {
299  deps = [ ":copy_liteos" ]
300  exec_path = rebase_path(root_out_dir)
301
302  objcopy = "${compile_prefix}objcopy$toolchain_cmd_suffix"
303  objdump = "${compile_prefix}objdump$toolchain_cmd_suffix"
304
305  command = "$objcopy -O binary $liteos_name $liteos_name.bin"
306  command +=
307      " && sh -c '$objdump -t $liteos_name | sort >$liteos_name.sym.sorted'"
308  command += " && sh -c '$objdump -d $liteos_name >$liteos_name.asm'"
309}
310