Bare-Metal on Android

To build a bare-metal Rust binary in AOSP, you need to use a rust_ffi_static Soong rule to build your Rust code, then a cc_binary with a linker script to produce the binary itself, and then a raw_binary to convert the ELF to a raw binary ready to be run.

  1. rust_ffi_static {
  2. name: "libvmbase_example",
  3. defaults: ["vmbase_ffi_defaults"],
  4. crate_name: "vmbase_example",
  5. srcs: ["src/main.rs"],
  6. rustlibs: [
  7. "libvmbase",
  8. ],
  9. }
  10. cc_binary {
  11. name: "vmbase_example",
  12. defaults: ["vmbase_elf_defaults"],
  13. srcs: [
  14. "idmap.S",
  15. ],
  16. static_libs: [
  17. "libvmbase_example",
  18. ],
  19. linker_scripts: [
  20. "image.ld",
  21. ":vmbase_sections",
  22. ],
  23. }
  24. raw_binary {
  25. name: "vmbase_example_bin",
  26. stem: "vmbase_example.bin",
  27. src: ":vmbase_example",
  28. enabled: false,
  29. target: {
  30. android_arm64: {
  31. enabled: true,
  32. },
  33. },
  34. }