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