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