# 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.

# Template for host toolchains.
template("host_cc_toolchain") {
  toolchain(target_name) {
    assert(defined(invoker.ar), "\"ar\" must be defined for ${target_name}.")
    assert(defined(invoker.cc), "\"cc\" must be defined for ${target_name}.")
    assert(defined(invoker.cxx), "\"cxx\" must be defined for ${target_name}.")

    # Collect extra flags from the toolchain.
    extra_defines = ""
    extra_cflags = ""
    extra_ldflags = ""
    if (defined(invoker.extra_defines)) {
      extra_defines += " ${invoker.extra_defines}"
    }
    if (defined(invoker.extra_cflags)) {
      extra_cflags += " ${invoker.extra_cflags}"
    }
    if (defined(invoker.extra_ldflags)) {
      extra_ldflags += " ${invoker.extra_ldflags}"
    }

    tool("cc") {
      depfile = "{{output}}.d"
      command = "${invoker.cc} -MMD -MF $depfile ${extra_defines} {{defines}} {{include_dirs}} ${extra_cflags} {{cflags}} {{cflags_c}} -c {{source}} -o {{output}}"
      depsformat = "gcc"
      description = "CC {{output}}"
      outputs =
          [ "{{source_out_dir}}/{{target_output_name}}.{{source_name_part}}.o" ]
    }

    tool("cxx") {
      depfile = "{{output}}.d"
      command = "${invoker.cxx} -MMD -MF $depfile ${extra_defines} {{defines}} {{include_dirs}} ${extra_cflags} {{cflags}} {{cflags_cc}} -c {{source}} -o {{output}}"
      depsformat = "gcc"
      description = "CXX {{output}}"
      outputs =
          [ "{{source_out_dir}}/{{target_output_name}}.{{source_name_part}}.o" ]
    }

    tool("alink") {
      rspfile = "{{output}}.rsp"
      command = "rm -f {{output}} && ${invoker.ar} rcs {{output}} @$rspfile"
      description = "AR {{target_output_name}}{{output_extension}}"
      rspfile_content = "{{inputs}}"
      outputs =
          [ "{{target_out_dir}}/{{target_output_name}}{{output_extension}}" ]
      default_output_extension = ".a"
      output_prefix = "lib"
    }

    tool("solink") {
      soname = "{{target_output_name}}{{output_extension}}"  # e.g. "libfoo.so".
      sofile = "{{output_dir}}/$soname"
      rspfile = soname + ".rsp"

      command = "${invoker.cxx} -shared ${extra_ldflags} {{ldflags}} -o $sofile -Wl,-soname=$soname @$rspfile"
      rspfile_content = "-Wl,--whole-archive {{inputs}} {{solibs}} -Wl,--no-whole-archive {{libs}}"

      description = "SOLINK $soname"

      # Use this for {{output_extension}} expansions unless a target manually
      # overrides it (in which case {{output_extension}} will be what the target
      # specifies).
      default_output_extension = ".so"

      # Use this for {{output_dir}} expansions unless a target manually overrides
      # it (in which case {{output_dir}} will be what the target specifies).
      default_output_dir = "{{root_out_dir}}"

      outputs = [ sofile ]
      link_output = sofile
      depend_output = sofile
      output_prefix = "lib"
    }

    tool("link") {
      outfile = "{{output_dir}}/{{target_output_name}}{{output_extension}}"
      rspfile = "$outfile.rsp"
      command = "${invoker.cxx} ${extra_ldflags} {{ldflags}} -o $outfile -Wl,--start-group @$rspfile {{solibs}} -Wl,--end-group {{libs}}"
      description = "LINK $outfile"
      default_output_dir = "{{root_out_dir}}"
      rspfile_content = "{{inputs}}"
      outputs = [ outfile ]
    }

    tool("stamp") {
      command = "touch {{output}}"
      description = "STAMP {{output}}"
    }

    tool("copy") {
      command = "cp -af {{source}} {{output}}"
      description = "COPY {{source}} {{output}}"
    }

    if (defined(invoker.toolchain_args)) {
      toolchain_args = {
        forward_variables_from(invoker.toolchain_args, "*")
      }
    }
  }
}

template("host_toolchain") {
  assert(defined(invoker.use_platform),
         "\"use_platform\" must be defined for ${target_name}.")
  if (invoker.use_platform) {
    assert(defined(invoker.heap_pages),
           "\"heap_pages\" must be defined for ${target_name}.")
    assert(defined(invoker.max_cpus),
           "\"max_cpus\" must be defined for ${target_name}.")
    assert(defined(invoker.max_vms),
           "\"max_vms\" must be defined for ${target_name}.")
  }

  # Specialize for clang.
  host_cc_toolchain("${target_name}_clang") {
    ar = "llvm-ar"
    cc = "clang -fcolor-diagnostics"
    cxx = "clang++ -fcolor-diagnostics -stdlib=libc++"

    # TODO: remove the need for this
    extra_defines = "-DPL011_BASE=0"

    if (invoker.use_platform) {
      toolchain_args = {
        use_platform = true

        # When building for the ${target_name}, use the fake architecture to make things
        # testable.
        plat_memory_protect = "//src/memory_protect:absent"
        plat_arch = "fake"
        plat_boot_flow = "//src/arch/fake:boot_flow"
        plat_console = "//src/arch/fake:console"
        plat_iommu = "//src/iommu:absent"
        plat_interrupts = "//src/arch/aarch64/plat/interrupts:absent"
        plat_heap_pages = invoker.heap_pages
        plat_max_cpus = invoker.max_cpus
        plat_max_vms = invoker.max_vms
      }
    }
  }
}
