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 310group("modules") { 311 deps = [ 312 "arch", 313 "bsd", 314 "compat", 315 "drivers", 316 "fs", 317 "kernel", 318 "lib", 319 "net", 320 "security", 321 "shell", 322 "syscall", 323 "testsuites/kernel:kernel_test", 324 ] 325 326 deps += [ 327 "$DRIVERS_LITEOS_DIR", 328 "$HDFTOPDIR", 329 ] 330 331 if (HAVE_DEVICE_SDK) { 332 deps += [ device_path ] 333 } 334} 335 336group("apps") { 337 deps = [ "apps" ] 338} 339 340group("tests") { 341 deps = [ "testsuites" ] 342} 343 344group("kernel") { 345 deps = [ ":build_kernel_image" ] 346} 347 348group("liteos_a") { 349 deps = [ ":kernel" ] 350 if (!liteos_is_mini) { 351 deps += [ 352 ":apps", 353 ":tests", 354 "$THIRDPARTY_MUSL_DIR/scripts/build_lite:strip", 355 ] 356 if (liteos_skip_make == false) { 357 deps += [ ":make" ] 358 } 359 } 360} 361 362executable("liteos") { 363 configs = [] # clear default configs 364 configs += [ ":arch_config" ] 365 configs += [ ":public" ] 366 367 ldflags = [ 368 "-static", 369 "-nostdlib", 370 "-Wl,--gc-sections", 371 "-Wl,-Map=$liteos_name.map", 372 "-Wl,--no-eh-frame-hdr", 373 ] 374 375 if (defined(LOSCFG_LIBC_NEWLIB)) { 376 ldflags += [ 377 "-Wl,--wrap=_free_r", 378 "-Wl,--wrap,_malloc_usable_size_r", 379 "-Wl,--wrap,_malloc_r", 380 "-Wl,--wrap,_memalign_r", 381 "-Wl,--wrap,_realloc_r", 382 "-Wl,--wrap,_fseeko_r", 383 ] 384 ldflags -= [ "-nostdlib" ] 385 } 386 libgcc = exec_script("//build/lite/run_shell_cmd.py", 387 [ "$cc -print-libgcc-file-name" ], 388 "trim string") 389 libs = [ libgcc ] 390 if (defined(LOSCFG_COMPILER_CLANG_LLVM)) { 391 ldflags += 392 [ "-Wl,-T" + rebase_path("tools/build/liteos_llvm.ld", root_build_dir) ] 393 inputs = [ "tools/build/liteos_llvm.ld" ] 394 } else { 395 ldflags += 396 [ "-Wl,-T" + rebase_path("tools/build/liteos.ld", root_build_dir) ] 397 ldflags += [ "-nostartfiles" ] 398 inputs = [ "tools/build/liteos.ld" ] 399 } 400 401 inputs += [ "$root_out_dir/board.ld" ] 402 403 output_dir = target_out_dir 404 405 deps = [ 406 ":modules", 407 "platform:copy_board.ld", 408 ] 409} 410 411copy("copy_liteos") { 412 deps = [ ":liteos" ] 413 sources = [ "$target_out_dir/unstripped/bin/liteos" ] 414 outputs = [ "$root_out_dir/$liteos_name" ] 415} 416 417build_ext_component("build_kernel_image") { 418 deps = [ ":copy_liteos" ] 419 exec_path = rebase_path(root_out_dir) 420 421 objcopy = "${compile_prefix}objcopy$toolchain_cmd_suffix" 422 objdump = "${compile_prefix}objdump$toolchain_cmd_suffix" 423 424 command = "$objcopy -O binary $liteos_name $liteos_name.bin" 425 command += 426 " && sh -c '$objdump -t $liteos_name | sort >$liteos_name.sym.sorted'" 427 command += " && sh -c '$objdump -d $liteos_name >$liteos_name.asm'" 428} 429 430build_ext_component("make") { 431 exec_path = rebase_path(".", root_build_dir) 432 outdir = rebase_path("$target_out_dir/${target_name}_out") 433 sysroot_path = rebase_path(ohos_current_sysroot) 434 arch_cflags = string_join(" ", target_arch_cflags) 435 command = "./build.sh \"$board_name\" \"$ohos_build_compiler\" \"$root_build_dir\" \"$ohos_build_type\" \"$tee_enable\"" 436 command += " \"$device_company\" \"$product_path\" \"$outdir\" \"$ohos_version\" \"$sysroot_path\" \"$arch_cflags\"" 437 command += " \"$device_path\" \"$compile_prefix\" \"$liteos_config_file\"" 438 if (liteos_skip_make) { 439 print("build_ext_component \"$target_name\" skipped:", command) 440 command = "true" 441 } 442} 443