# Copyright (c) 2013-2019 Huawei Technologies Co., Ltd. All rights reserved.
# Copyright (c) 2020-2022 Huawei Device Co., Ltd. All rights reserved.
#
# Redistribution and use in source and binary forms, with or without modification,
# are permitted provided that the following conditions are met:
#
# 1. Redistributions of source code must retain the above copyright notice, this list of
#    conditions and the following disclaimer.
#
# 2. Redistributions in binary form must reproduce the above copyright notice, this list
#    of conditions and the following disclaimer in the documentation and/or other materials
#    provided with the distribution.
#
# 3. Neither the name of the copyright holder nor the names of its contributors may be used
#    to endorse or promote products derived from this software without specific prior written
#    permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
# THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
# EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
# OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
# WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
# OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
# ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

import("//build/lite/config/component/lite_component.gni")

LITEOS_MENUCONFIG_H = rebase_path("$root_out_dir/config.h")

import("liteos.gni")

if (defined(LOSCFG_COMPILER_ICCARM)) {
  import("config_iccarm.gni")
} else {
  import("config.gni")

  cc = "$ohos_current_cc_command " + string_join(" ", liteos_arch_config_cflags)
  if (ohos_build_compiler == "clang") {
    cc += " --target=$target_triple"
  }
}

config("arch_config") {
  cflags = arch_config_cflags
  asmflags = arch_config_asmflags
  ldflags = arch_config_ldflags
}

config("stdinc_config") {
  cflags = stdinc_config_cflags
  asmflags = stdinc_config_asmflags
  if (!defined(LOSCFG_COMPILER_ICCARM)) {
    std_include = exec_script("//build/lite/run_shell_cmd.py",
                              [ "$cc -print-file-name=include" ],
                              "trim string")
    cflags += [
      "-isystem",
      std_include,
    ]
  }
}

config("ssp_config") {
  cflags = ssp_config_cflags
  asmflags = ssp_config_asmflags
}

config("optimize_config") {
  cflags = optimize_config_cflags
  asmflags = optimize_config_asmflags
}

config("kconfig_config") {
  cflags = kconfig_config_cflags
  asmflags = kconfig_config_asmflags
}

config("warn_config") {
  cflags = warn_config_cflags
  asmflags = warn_config_asmflags
}

config("dialect_config") {
  cflags_c = dialect_config_cflags
  cflags_cc = dialect_config_ccflags
  asmflags = dialect_config_asmflags
}

config("misc_config") {
  if (!defined(LOSCFG_COMPILER_ICCARM)) {
    defines = [ "__LITEOS__" ]
    defines += [ "__LITEOS_M__" ]
  }
  if (!defined(LOSCFG_DEBUG_VERSION)) {
    defines += [ "NDEBUG" ]
  }
  cflags = misc_config_cflags
  asmflags = misc_config_asmflags
}

config("los_config") {
  configs = [
    #":arch_config",
    ":kconfig_config",

    ":stdinc_config",
    ":dialect_config",
    ":optimize_config",
    ":ssp_config",

    #":warn_config",
    ":misc_config",
  ]
}

cmd = "if [ -f $device_path/BUILD.gn ]; then echo true; else echo false; fi"
HAVE_DEVICE_SDK = exec_script("//build/lite/run_shell_cmd.py", [ cmd ], "value")

# If device_path points to vendor, use device_path directly,
# otherwise board is decoupled from soc, device_path should contain board
BOARD_SOC_FEATURE =
    device_path == string_replace(device_path, "/vendor/", "") &&
    device_path != string_replace(device_path, "/board/", "")

config("public") {
  configs = [
    "arch:public",
    "kernel:public",
    "kal:public",
    "components:public",
    "utils:public",
  ]

  if (BOARD_SOC_FEATURE) {
    configs += [ "//device/board/$device_company:public" ]
    configs += [ "//device/soc/$LOSCFG_SOC_COMPANY:public" ]
  } else {
    if (HAVE_DEVICE_SDK) {
      configs += [ "$device_path:public" ]
    }
  }
}

group("modules") {
  deps = [
    "arch",
    "components",
    "kal",
    "kernel",
    "testsuites",
    "utils",
    HDFTOPDIR,
  ]

  if (BOARD_SOC_FEATURE) {
    deps += [ "//device/board/$device_company" ]
    deps += [ "//device/soc/$LOSCFG_SOC_COMPANY" ]
  } else {
    if (HAVE_DEVICE_SDK) {
      deps += [ device_path ]
    }
  }
}

# when HAVE_DEVICE_SDK is not reached, gn raises an error. so we just use it as
# not needed
not_needed([ "HAVE_DEVICE_SDK" ])

static_library("libkernel") {
  deps = [ ":modules" ]
  if (defined(LOSCFG_COMPILER_ICCARM)) {
    complete_static_lib = true
  } else {
    complete_static_lib = false
  }
}

group("kernel") {
  deps = [ ":libkernel" ]
}

group("liteos_m") {
}

executable("liteos") {
  configs += [
    ":public",
    ":los_config",
  ]

  ldflags = executable_config_ldflags

  output_dir = target_out_dir

  if (liteos_kernel_only) {
    deps = [ ":kernel" ]
  } else {
    deps = [ "//build/lite:ohos" ]
  }
}

copy("copy_liteos") {
  deps = [ ":liteos" ]
  sources = [ "$target_out_dir/unstripped/bin/liteos" ]
  outputs = [ "$root_out_dir/$liteos_name" ]
}

build_ext_component("build_kernel_image") {
  deps = [ ":copy_liteos" ]
  exec_path = rebase_path(root_out_dir)

  command = toochain_config_command
}
