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