# Copyright 2018 The Hafnium Authors.
#
# Use of this source code is governed by a BSD-style
# license that can be found in the LICENSE file or at
# https://opensource.org/licenses/BSD-3-Clause.

import("//build/image/image.gni")
import("//build/toolchain/platform.gni")

declare_args() {
  # Target which implements the standard output interface.
  stdout = ":stdout"
}

# The hypervisor image.
hypervisor("hafnium") {
  deps = [
    ":layout",
    ":src_not_testable_yet",
  ]
}

# Hypervisor specific code that isn't. One day it will be testable and both the
# src targets will merge!
source_set("src_not_testable_yet") {
  public_configs = [ "//src/arch/${plat_arch}:arch_config" ]
  sources = [
    "api.c",
    "cpio.c",
    "ffa_memory.c",
    "init.c",
    "load.c",
    "main.c",
  ]
  deps = [
    ":src_testable",
    "//project/${project}/${plat_name}",
    "//src/arch/${plat_arch}/hypervisor:other_world",
    plat_boot_flow,
    plat_console,
    plat_iommu,
  ]
}

# One day, this will contain all the hypervisor's source but only once it can
# all be built against the fake arch for unit tests. Utilities that are shared
# e.g. with VM used in the VM tests have their own targets to facilitate
# sharing.
source_set("src_testable") {
  public_configs = [ "//src/arch/${plat_arch}:arch_config" ]
  sources = [
    "boot_info.c",
    "cpu.c",
    "hf_ipi.c",
    "manifest.c",
    "partition_pkg.c",
    "sp_pkg.c",
    "timer_mgmt.c",
    "vcpu.c",
  ]

  deps = [
    ":abort",
    ":dlog",
    ":fdt",
    ":fdt_handler",
    ":memiter",
    ":mm",
    ":panic",
    ":std",
    ":vm",
    "//src/arch/${plat_arch}:arch",
    "//src/arch/${plat_arch}/hypervisor",
    "//src/arch/${plat_arch}/hypervisor:other_world",
    "//src/transfer_list:transfer_list",
    "//vmlib",
    plat_boot_flow,
    plat_console,
    plat_interrupts,
    plat_iommu,
    plat_memory_protect,
  ]
}

source_set("layout") {
  sources = [ "layout.c" ]
}

source_set("mm") {
  sources = [
    "mm.c",
    "mpool.c",
  ]
}

source_set("vm") {
  public_configs = [ "//src/arch/${plat_arch}:arch_config" ]
  sources = [ "vm.c" ]
}

# Standard library functions.
source_set("std") {
  sources = [ "std.c" ]

  deps = [ "//src/arch/${plat_arch}:std" ]
}

# Default implementation of stdout which sends the character to the
# 'plat_console' driver.
source_set("stdout") {
  sources = [ "stdout.c" ]
  deps = [ plat_console ]
}

# Debug code that is not specific to a certain image so can be shared.
source_set("dlog") {
  sources = [ "dlog.c" ]

  deps = [
    ":std",
    stdout,
  ]
}

source_set("string") {
  sources = [ "string.c" ]
  deps = [
    ":memiter",
    ":std",
  ]
}

source_set("fdt_handler") {
  sources = [ "fdt_handler.c" ]
  deps = [
    ":dlog",
    ":fdt",
    ":fdt_patch",
  ]
}

# Flattened Device Tree (FDT) utilities.
source_set("fdt") {
  sources = [ "fdt.c" ]

  deps = [
    ":memiter",
    ":string",
    "//third_party/dtc:libfdt",
  ]
}

source_set("fdt_patch") {
  sources = [ "fdt_patch.c" ]
  deps = [
    ":dlog",
    ":fdt",
    "//third_party/dtc:libfdt",
  ]
}

source_set("memiter") {
  sources = [ "memiter.c" ]
}

source_set("panic") {
  sources = [ "panic.c" ]
}

source_set("abort") {
  sources = [ "abort.c" ]
}

executable("unit_tests") {
  testonly = true
  sources = [
    "bits_test.cc",
    "cpu_test.cc",
    "fdt_handler_test.cc",
    "fdt_test.cc",
    "ipi_test.cc",
    "manifest_test.cc",
    "mm_test.cc",
    "mpool_test.cc",
    "string_test.cc",
    "vcpu_test.cc",
    "vm_test.cc",
  ]
  sources += [ "layout_fake.c" ]
  cflags_cc = [
    "-Wno-c99-extensions",
    "-Wno-nested-anon-types",
  ]
  deps = [
    ":memiter",
    ":src_testable",
    "//third_party/googletest:gtest_main",
  ]
}
