# Copyright (c) 2020 Project CHIP Authors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

declare_args() {
  # Debug build. Set this to false to remove development features.
  is_debug = true
}

if (target_os == "") {
  target_os = host_os
}

if (target_cpu == "") {
  target_cpu = host_cpu
}

if (current_cpu == "") {
  current_cpu = target_cpu
}
if (current_os == "") {
  current_os = target_os
}

_build_overrides = {
  import("//build_overrides/build.gni")
}

_pigweed_overrides = {
  import("//build_overrides/pigweed.gni")
}

_chip_defaults = {
  import("${_build_overrides.build_root}/config/defaults.gni")
}

declare_args() {
  # Toolchain to use for host. This is usually set by default.
  host_toolchain = ""
}

if (host_toolchain == "") {
  if (_chip_defaults.is_clang) {
    host_toolchain = "${_build_overrides.build_root}/toolchain/host:${host_os}_${host_cpu}_clang"
  } else {
    host_toolchain = "${_build_overrides.build_root}/toolchain/host:${host_os}_${host_cpu}_gcc"
  }
}

_custom_toolchain = {
  import("${_build_overrides.build_root}/toolchain/custom/custom.gni")
}

if (_chip_defaults.custom_toolchain != "") {
  if (filter_include([ _chip_defaults.custom_toolchain ], [ "/*" ]) == []) {
    # Interpret relative toolchain names relative to ${build_root}/toolchain/
    _default_toolchain = "${_build_overrides.build_root}/toolchain/${_chip_defaults.custom_toolchain}"
  } else {
    _default_toolchain = _chip_defaults.custom_toolchain
  }
} else if (target_os == "all") {
  _default_toolchain = "${_pigweed_overrides.dir_pw_toolchain}/default"
} else if (target_os == "linux") {
  if (_chip_defaults.is_clang) {
    _target_compiler = "clang"
  } else {
    _target_compiler = "gcc"
  }

  _default_toolchain = "${_build_overrides.build_root}/toolchain/linux:linux_${target_cpu}_${_target_compiler}"
} else if (target_os == host_os &&
           (target_cpu == host_cpu ||
            (target_cpu == "arm64e" && host_cpu == "arm64"))) {
  _default_toolchain = host_toolchain
} else if (target_os == "freertos") {
  if (_chip_defaults.is_clang) {
    _target_compiler = "clang"
  } else {
    _target_compiler = "gcc"
  }

  if (target_cpu == "arm") {
    _default_toolchain =
        "${_build_overrides.build_root}/toolchain/arm_${_target_compiler}"
  } else if (target_cpu == "riscv") {
    _default_toolchain = "${_build_overrides.build_root}/toolchain/riscv_gcc"
  } else {
    assert(false, "Unsupported target_cpu: ${target_cpu}")
  }
} else if (target_os == "android") {
  if (current_cpu == "arm") {
    _default_toolchain =
        "${_build_overrides.build_root}/toolchain/android:android_arm"
  } else if (current_cpu == "arm64") {
    _default_toolchain =
        "${_build_overrides.build_root}/toolchain/android:android_arm64"
  } else if (current_cpu == "x64") {
    _default_toolchain =
        "${_build_overrides.build_root}/toolchain/android:android_x64"
  } else if (current_cpu == "x86") {
    _default_toolchain =
        "${_build_overrides.build_root}/toolchain/android:android_x86"
  } else {
    assert(false, "Unsupported target_cpu: ${current_cpu}")
  }
} else if (target_os == "ios") {
  _default_toolchain =
      "${_build_overrides.build_root}/toolchain/ios:ios_${target_cpu}"
} else if (target_os == "tizen") {
  _default_toolchain =
      "${_build_overrides.build_root}/toolchain/tizen:tizen_${target_cpu}"
} else if (target_os == "webos") {
  if (target_cpu == "arm" || target_cpu == "arm64") {
    _default_toolchain = "${_build_overrides.build_root}/toolchain/webos"
  } else {
    assert(false,
           "Unsupported target_cpu: ${current_cpu}. Shall be arm for webOS")
  }
} else {
  assert(
      false,
      "No toolchain specified, please specify custom_toolchain for host_os='${host_os}', target_os='${target_os}', host_cpu='${host_cpu}', target_cpu='${target_cpu}'")
}

set_default_toolchain(_default_toolchain)

set_defaults("static_library") {
  configs = _chip_defaults.default_configs
}
set_defaults("source_set") {
  configs = _chip_defaults.default_configs
}
set_defaults("executable") {
  configs =
      _chip_defaults.default_configs + _chip_defaults.executable_default_configs
}
set_defaults("shared_library") {
  configs = _chip_defaults.default_configs
}
set_defaults("loadable_module") {
  configs = _chip_defaults.default_configs
}
