declare_args() {
  is_debug = false

  # Note that this uses the 'cc' and 'c++' links that should map to GCC on linux
  # systems and clang on macs.
  ar = "ar"
  cc = "cc"
  cxx = "c++"

  ndk = ""
  ndk_api = 24
}

# Platform detection
if (target_os == "") {
  target_os = host_os
}
if (current_os == "") {
  current_os = target_os
}

is_android = current_os == "android"
is_ios = current_os == "ios"
is_linux = current_os == "linux"
is_mac = current_os == "mac"
is_win = current_os == "win"

is_apple = is_mac || is_ios
is_desktop = is_mac || is_linux || is_win
is_mobile = is_android || is_ios
is_unix = is_android || is_linux

if (target_cpu == "") {
  if (is_mobile) {
    target_cpu = "arm64"
  } else {
    target_cpu = host_cpu
  }
}
if (target_cpu == "x86_64") {
  target_cpu = "x64"
}
if (current_cpu == "") {
  current_cpu = target_cpu
}

if (is_android) {
  assert(ndk != "", "NDK path argument is empty")

  ndk_host = ""
  ndk_target = ""

  if (host_os == "linux") {
    ndk_host = "linux-x86_64"
  } else if (host_os == "mac") {
    ndk_host = "darwin-x86_64"
  } else if (host_os == "win") {
    ndk_host = "windows-x86_64"
  }

  if (target_cpu == "arm64") {
    ndk_target = "aarch64-linux-android"
  } else if (target_cpu == "arm") {
    ndk_target = "armv7a-linux-androideabi"
  } else if (target_cpu == "x64") {
    ndk_target = "x86_64-linux-android"
  } else if (target_cpu == "x86") {
    ndk_target = "i686-linux-android"
  }

  _prefix = "$ndk/toolchains/llvm/prebuilt/$ndk_host/bin"
  if (host_os == "win") {
    ar = "$_prefix/llvm-ar.exe"
    cc = "$_prefix/clang.exe --target=$ndk_target$ndk_api -fno-addrsig"
    cxx = "$_prefix/clang++.exe --target=$ndk_target$ndk_api -fno-addrsig"
  } else {
    ar = "$_prefix/llvm-ar"
    cc = "$_prefix/$ndk_target$ndk_api-clang"
    cxx = "$_prefix/$ndk_target$ndk_api-clang++"
  }
}

# Set defaults
_default_config = [
  "//build:default",
  "//build:warnings",
]

if (is_debug) {
  _default_config += [ "//build:debug" ]
} else {
  _default_config += [ "//build:release" ]
}

set_defaults("executable") {
  configs = [ "//build:executable" ] + _default_config
}
set_defaults("static_library") {
  configs = _default_config
}
set_defaults("shared_library") {
  configs = [ "//build:shared_library" ] + _default_config
}
set_defaults("source_set") {
  configs = _default_config
}

if (is_win) {
  set_default_toolchain("//build/toolchain/msvc")
} else {
  # Clang behaves like GCC in most cases.
  set_default_toolchain("//build/toolchain/gcc")
}
