• 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  tee_enable = false
36  liteos_name = "OHOS_Image"
37  liteos_skip_make = false
38}
39
40tee = ""
41if (tee_enable) {
42  tee = "_tee"
43}
44
45declare_args() {
46  liteos_config_file = "${ohos_build_type}${tee}.config"
47}
48
49liteos_config_file =
50    rebase_path(liteos_config_file, "", "$product_path/kernel_configs")
51print("liteos_config_file:", liteos_config_file)
52
53exec_script("//build/lite/run_shell_cmd.py",
54            [ "env" + " CONFIG_=LOSCFG_" + " KCONFIG_CONFIG_HEADER='y=true'" +
55                  " KCONFIG_CONFIG=$liteos_config_file" +
56                  " DEVICE_PATH=$device_path" + " srctree=" + rebase_path(".") +
57                  " genconfig" + " --header-path $LITEOS_MENUCONFIG_H" +
58                  " --file-list kconfig_files.txt" +
59                  " --env-list kconfig_env.txt" + " --config-out config.gni" ],
60            "",
61            [ liteos_config_file ])
62
63import("liteos.gni")
64
65assert(ARCH != "", "ARCH not set!")
66assert(ARCH == arch, "ARCH not match! details: $ARCH != $arch")
67assert(tee_enable == defined(LOSCFG_TEE_ENABLE), "TEE switch not match!")
68assert(ohos_build_compiler == "clang" == defined(LOSCFG_COMPILER_CLANG_LLVM),
69       "compiler not match!")
70
71generate_notice_file("kernel_notice_file") {
72  module_name = "kernel"
73  module_source_dir_list = [
74    "$LITEOSTHIRDPARTY/FreeBSD",
75    "$LITEOSTHIRDPARTY/musl",
76    "$LITEOSTHIRDPARTY/zlib",
77    "$LITEOSTHIRDPARTY/FatFs",
78    "$LITEOSTHIRDPARTY/lwip",
79    "$LITEOSTHIRDPARTY/NuttX",
80    "$LITEOSTHIRDPARTY/mtd-utils",
81  ]
82}
83
84liteos_arch_cflags = []
85if (defined(LOSCFG_ARCH_ARM)) {
86  mcpu = LOSCFG_ARCH_CPU
87  if (defined(LOSCFG_ARCH_ARM_AARCH64) && defined(LOSCFG_ARCH_FPU_DISABLE)) {
88    mcpu += "+nofp"
89  }
90  liteos_arch_cflags += [ "-mcpu=$mcpu" ]
91  if (defined(LOSCFG_ARCH_ARM_AARCH32)) {
92    liteos_arch_cflags += [
93      "-mfloat-abi=softfp",
94      "-mfpu=$LOSCFG_ARCH_FPU",
95    ]
96  }
97}
98
99cc = "$ohos_current_cc_command " + string_join(" ", liteos_arch_cflags)
100if (ohos_build_compiler == "clang") {
101  cc += " --target=$target_triple"
102}
103
104config("arch_config") {
105  cflags = liteos_arch_cflags
106  asmflags = cflags
107  ldflags = cflags
108  if (defined(LOSCFG_ARCH_ARM_AARCH32)) {
109    if (!defined(LOSCFG_COMPILER_CLANG_LLVM)) {
110      cflags += [ "-mthumb-interwork" ]
111    }
112  }
113  if (defined(LOSCFG_THUMB)) {
114    cflags += [ "-mthumb" ]
115    if (defined(LOSCFG_COMPILER_CLANG_LLVM)) {
116      cflags += [ "-mimplicit-it=thumb" ]
117    } else {
118      cflags += [ "-Wa,-mimplicit-it=thumb" ]
119    }
120  }
121}
122
123config("stdinc_config") {
124  std_include = exec_script("//build/lite/run_shell_cmd.py",
125                            [ "$cc -print-file-name=include" ],
126                            "trim string")
127  cflags = [
128    "-isystem",
129    std_include,
130  ]
131  cflags += [ "-nostdinc" ]
132  asmflags = cflags
133}
134
135config("ssp_config") {
136  cflags = []
137  if (defined(LOSCFG_CC_STACKPROTECTOR_ALL)) {
138    cflags += [ "-fstack-protector-all" ]
139  } else if (defined(LOSCFG_CC_STACKPROTECTOR_STRONG)) {
140    cflags += [ "-fstack-protector-strong" ]
141  } else if (defined(LOSCFG_CC_STACKPROTECTOR)) {
142    cflags += [
143      "-fstack-protector",
144      "--param",
145      "ssp-buffer-size=4",
146    ]
147  } else {
148    cflags += [ "-fno-stack-protector" ]
149  }
150  asmflags = cflags
151}
152
153config("optimize_config") {
154  cflags = []
155  if (defined(LOSCFG_COMPILE_DEBUG)) {
156    cflags += [
157      "-g",
158      "-gdwarf-2",
159    ]
160    optimization_cflag = "-O0"
161  }
162  if (defined(LOSCFG_COMPILE_OPTIMIZE)) {
163    optimization_cflag = "-O2"
164  }
165  if (defined(LOSCFG_COMPILE_OPTIMIZE_SIZE)) {
166    if (defined(LOSCFG_COMPILER_CLANG_LLVM)) {
167      optimization_cflag = "-Oz"
168    } else {
169      optimization_cflag = "-Os"
170    }
171  }
172  if (defined(LOSCFG_COMPILE_LTO)) {
173    if (defined(LOSCFG_COMPILER_CLANG_LLVM)) {
174      cflags += [ "-flto=thin" ]
175    } else {
176      #cflags += [ "-flto" ]
177    }
178  }
179  cflags += [ optimization_cflag ]
180  asmflags = cflags
181}
182
183config("kconfig_config") {
184  cflags = [
185    "-imacros",
186    "$LITEOS_MENUCONFIG_H",
187  ]
188  asmflags = cflags
189}
190
191config("warn_config") {
192  cflags = [
193    "-Wall",
194    "-Werror",
195    "-Wpointer-arith",
196    "-Wstrict-prototypes",
197    "-Winvalid-pch",
198  ]
199  if (defined(LOSCFG_COMPILER_CLANG_LLVM)) {
200    cflags += [ "-Wno-address-of-packed-member" ]
201  }
202  asmflags = cflags
203}
204
205config("dialect_config") {
206  cflags_c = [ "-std=c99" ]
207  cflags_cc = [ "-std=c++11" ]
208}
209
210config("misc_config") {
211  defines = [ "__LITEOS__" ]
212  defines += [ "__LITEOS_A__" ]
213  if (!defined(LOSCFG_DEBUG_VERSION)) {
214    defines += [ "NDEBUG" ]
215  }
216
217  cflags = [
218    "-fno-pic",
219    "-fno-builtin",
220    "-fms-extensions",
221    "-fno-strict-aliasing",
222    "-fno-common",
223    "-fsigned-char",
224    "-ffunction-sections",
225    "-fdata-sections",
226    "-fno-exceptions",
227    "-fno-omit-frame-pointer",
228    "-fno-short-enums",
229    "-mno-unaligned-access",
230  ]
231
232  if (!defined(LOSCFG_COMPILER_CLANG_LLVM)) {
233    cflags += [ "-fno-aggressive-loop-optimizations" ]
234  }
235
236  asmflags = cflags
237}
238
239config("los_config") {
240  configs = [
241    ":arch_config",
242    ":kconfig_config",
243    ":stdinc_config",
244    ":dialect_config",
245    ":optimize_config",
246    ":ssp_config",
247    ":warn_config",
248    ":misc_config",
249  ]
250}
251
252cmd = "if [ -f $device_path/BUILD.gn ]; then echo true; else echo false; fi"
253HAVE_DEVICE_SDK = exec_script("//build/lite/run_shell_cmd.py", [ cmd ], "value")
254
255config("public") {
256  configs = [
257    "arch:public",
258    "kernel:public",
259    "compat:public",
260    "bsd:public",
261    "fs:public",
262    "drivers:public",
263    "security:public",
264    "net:public",
265    "shell:public",
266    "lib:public",
267  ]
268
269  configs += [
270    "$HDFTOPDIR:public",
271    "//drivers/liteos:public",
272  ]
273
274  if (HAVE_DEVICE_SDK) {
275    configs += [ "$device_path:public" ]
276  }
277}
278
279group("modules") {
280  deps = [
281    "arch",
282    "bsd",
283    "compat",
284    "drivers",
285    "fs",
286    "kernel",
287    "lib",
288    "net",
289    "security",
290    "shell",
291    "syscall",
292  ]
293
294  deps += [
295    "//drivers/liteos",
296    HDFTOPDIR,
297  ]
298
299  if (HAVE_DEVICE_SDK) {
300    deps += [ device_path ]
301  }
302}
303
304group("apps") {
305  deps = [ "apps" ]
306}
307
308group("tests") {
309  deps = [ "testsuites" ]
310}
311
312group("kernel") {
313  deps = [ ":build_kernel_image" ]
314}
315
316group("liteos_a") {
317  deps = [
318    ":apps",
319    ":kernel",
320    ":make",
321    ":tests",
322    "//prebuilts/lite/sysroot/build:strip",
323  ]
324}
325
326executable("liteos") {
327  configs = []  # clear default configs
328  configs += [ ":arch_config" ]
329  configs += [ ":public" ]
330
331  ldflags = [
332    "-static",
333    "-nostdlib",
334    "-Wl,--gc-sections",
335    "-Wl,-Map=$liteos_name.map",
336    "-Wl,--no-eh-frame-hdr",
337  ]
338
339  libgcc = exec_script("//build/lite/run_shell_cmd.py",
340                       [ "$cc -print-libgcc-file-name" ],
341                       "trim string")
342  libs = [ libgcc ]
343  if (defined(LOSCFG_COMPILER_CLANG_LLVM)) {
344    ldflags +=
345        [ "-Wl,-T" + rebase_path("tools/build/liteos_llvm.ld", root_build_dir) ]
346    inputs = [ "tools/build/liteos_llvm.ld" ]
347  } else {
348    ldflags +=
349        [ "-Wl,-T" + rebase_path("tools/build/liteos.ld", root_build_dir) ]
350    ldflags += [ "-Wl,-nostartfiles" ]
351    inputs = [ "tools/build/liteos.ld" ]
352  }
353
354  inputs += [ "$root_out_dir/board.ld" ]
355
356  output_dir = target_out_dir
357
358  deps = [
359    ":modules",
360    "platform:copy_board.ld",
361  ]
362}
363
364copy("copy_liteos") {
365  deps = [ ":liteos" ]
366  sources = [ "$target_out_dir/unstripped/bin/liteos" ]
367  outputs = [ "$root_out_dir/$liteos_name" ]
368}
369
370build_ext_component("build_kernel_image") {
371  deps = [ ":copy_liteos" ]
372  exec_path = rebase_path(root_out_dir)
373
374  objcopy = "${compile_prefix}objcopy$toolchain_cmd_suffix"
375  objdump = "${compile_prefix}objdump$toolchain_cmd_suffix"
376
377  command = "$objcopy -O binary $liteos_name $liteos_name.bin"
378  command +=
379      " && sh -c '$objdump -t $liteos_name | sort >$liteos_name.sym.sorted'"
380  command += " && sh -c '$objdump -d $liteos_name >$liteos_name.asm'"
381}
382
383build_ext_component("make") {
384  exec_path = rebase_path(".", root_build_dir)
385  outdir = rebase_path("$target_out_dir/${target_name}_out")
386  sysroot_path = rebase_path(ohos_current_sysroot)
387  arch_cflags = string_join(" ", target_arch_cflags)
388  command = "./build.sh \"$board_name\" \"$ohos_build_compiler\" \"$root_build_dir\" \"$ohos_build_type\" \"$tee_enable\""
389  command += " \"$device_company\" \"$product_path\" \"$outdir\" \"$ohos_version\" \"$sysroot_path\" \"$arch_cflags\""
390  command += " \"$device_path\" \"$compile_prefix\" \"$liteos_config_file\""
391  if (liteos_skip_make) {
392    print("build_ext_component \"$target_name\" skipped:", command)
393    command = "true"
394  }
395}
396