content: #
  # SPDX-License-Identifier: BlueOak-1.0.0
  #
  # Copyright (c) Berkus Decker <berkus+vesper@metta.systems>
  #
  # Build nucleus
  #
  extend = "../Makefile.toml"
  
  [env]
  KERNEL_LINK = "libs/platform/src/platform/raspberrypi/linker/kernel.ld"
  
  [tasks.build]
  clear = true
  dependencies = ["build-target-nucleus", "convert-kernel-binary"]
  script_runner = "@duckscript"
  script = '''
    echo
    echo ✅ nucleus built for hw ${TARGET_BOARD} ${BOARD_RUSTFLAGS}.
    echo
  '''
  
  [tasks.build-target-nucleus]
  env = { TARGET_BOARD = "rpi4", TARGET_LINK_RUSTFLAGS = "-C link-arg=--script=${KERNEL_LINK}", BOARD_RUSTFLAGS = "${BOARD_rpi4_RUSTFLAGS}" }
  run_task = "build-target"
  
  # Generate a bootable binary file from KERNEL_ELF
  [tasks.convert-kernel-binary]
  workspace = false
  env = { "BINARY_FILE" = "${KERNEL_ELF}" }
  extend = "convert-custom-binary"
  
  [tasks.test-nucleus]
  env = { TARGET_LINK_RUSTFLAGS = "-C link-arg=--script=${KERNEL_LINK}" }
  run_task = "test-on-rpi3-qemu"
  
  [tasks.qemu]
  clear = true
  alias = "qemu-kernel"
  
  [tasks.qemu-kernel]
  env = { "QEMU_RUNNER_OPTS" = "${QEMU_SERIAL_OPTS}", TARGET_LINK_RUSTFLAGS = "-C link-arg=--script=${KERNEL_LINK}", "TARGET_DTB" = "${CARGO_MAKE_WORKSPACE_WORKING_DIRECTORY}/targets/bcm2710-rpi-3-b-plus.dtb" }
  run_task = "qemu-runner"
  
  [tasks.qemu-gdb]
  env = { "QEMU_RUNNER_OPTS" = "${QEMU_SERIAL_OPTS} ${QEMU_DISASM_OPTS} ${QEMU_GDB_OPTS}", "TARGET_DTB" = "${CARGO_MAKE_WORKSPACE_WORKING_DIRECTORY}/targets/bcm2710-rpi-3-b-plus.dtb" }
  extend = "qemu-runner"
  
  [tasks.zellij-nucleus]
  alias = "zellij-nucleus-override"
  
  [tasks.zellij-nucleus-override]
  env = { "KERNEL_BIN" = "${KERNEL_BIN}", "QEMU_RUNNER_OPTS" = "${QEMU_SERIAL_OPTS} ${QEMU_DISASM_OPTS} ${QEMU_GDB_OPTS}", "TARGET_DTB" = "${CARGO_MAKE_WORKSPACE_WORKING_DIRECTORY}/targets/bcm2710-rpi-3-b-plus.dtb" }
  extend = "zellij-config"
  
  [tasks.gdb]
  dependencies = ["nucleus", "gdb-config"]
  env = { "RUST_GDB" = "${GDB}" }
  script = ["exec < /dev/tty && pipx run gdbgui -g \"rust-gdb -x ${GDB_CONNECT_FILE} ${KERNEL_ELF}\""]
  
  [tasks.install-nm]
  install_crate = { crate_name = "cargo-binutils", binary = "rust-nm", test_arg = ["--help"] }
  
  [tasks.install-rustfilt]
  install_crate = { crate_name = "rustfilt", binary = "rustfilt", test_arg = ["--help"] }
  
  [tasks.xtool-nm]
  dependencies = ["build", "install-nm", "install-rustfilt"]
  script = ["${NM} ${KERNEL_ELF} | sort -k 1 | rustfilt"]
  
  [tasks.sdcard]  # kernel-sdcard ?
  dependencies = ["build"]
  script_runner = "@duckscript"
  script = [
    '''
      kernelImage = set "kernel8.img"
      cp ${KERNEL_BIN} ${VOLUME}/${kernelImage}
      echo 🔄 Copied nucleus to ${VOLUME}/${kernelImage}
  ''',
  ]
  
  [tasks.hopper]
  clear = true
  alias = "kernel-hopper"
  
  [tasks.kernel-hopper]
  dependencies = ["build"]
  # The cmd line below causes a bug in hopper, see https://www.dropbox.com/s/zyw5mfx0bepcjb1/hopperv4-RAW-bug.mov?dl=0
  # "hopper --loader RAW --base-address 0x80000 --entrypoint 0x80000 --file-offset 0 --plugin arm --cpu aarch64 --variant generic --contains-code true --executable ${KERNEL_BIN}"
  script = ["hopper --loader ELF --executable ${KERNEL_ELF}"]
  
  [tasks.clippy]
  clear = true
  dependencies = ["clippy-rpi3", "clippy-rpi4"]
  
  [tasks.clippy-rpi3]
  env = { TARGET_BOARD = "rpi3", TARGET_LINK_RUSTFLAGS = "-C link-arg=--script=${KERNEL_LINK}", BOARD_RUSTFLAGS = "${BOARD_rpi3_RUSTFLAGS}" }
  run_task = "xtool-clippy"
  
  [tasks.clippy-rpi4]
  env = { TARGET_BOARD = "rpi4", TARGET_LINK_RUSTFLAGS = "-C link-arg=--script=${KERNEL_LINK}", BOARD_RUSTFLAGS = "${BOARD_rpi4_RUSTFLAGS}" }
  run_task = "xtool-clippy"
  