[config]
skip_core_tasks = true

[tasks.create-target-dir]
script_runner = "@rust"
script = ['''
  fn main() {
    std::fs::DirBuilder::new()
      .recursive(true)
      .create("./target/")
      .unwrap();
  }
''']

[tasks.assemble-rsrt0]
dependencies = ["create-target-dir"]
command = "arm-none-eabi-as"
args = ["rsrt0.s", "-o", "target/rsrt0.o", "-mcpu=arm7tdmi", "-mthumb-interwork"]

[tasks.build-examples]
dependencies = ["assemble-rsrt0"]
command = "cargo"
args = ["xbuild", "--examples", "--target", "thumbv4-none-agb.json", "--release"]

[tasks.pack-roms]
script_runner = "@rust"
script = ['''
  fn main() -> std::io::Result<()> {
    for entry in std::fs::read_dir("examples/")? {
      let entry = entry?;
      let mut path = entry.path();
      if path.is_dir() {
        continue;
      } else {
        path.set_extension("");
        let name = path.file_name().unwrap().to_str().unwrap();
        println!("{}", name);
        std::process::Command::new("arm-none-eabi-objcopy").args(
          &["-O", "binary",
          &format!("target/thumbv4-none-agb/release/examples/{}",name),
          &format!("target/{}.gba",name)])
          .output().expect("failed to objcopy!");
      }
    }
    Ok(())
  }
''']

[tasks.test]
command = "cargo"
args = ["test", "--lib"]

[tasks.justrelease]
dependencies = ["build-examples-release", "pack-roms"]

[tasks.build-all]
dependencies = ["build-examples", "pack-roms"]

[tasks.default]
alias = "build-all"