From 7d21956b0a71f9da41add5363cfa7d96d19a43f1 Mon Sep 17 00:00:00 2001
From: Erin <erin@erindesu.cz>
Date: Sat, 7 Jan 2023 22:11:21 +0100
Subject: [PATCH] Repbuild now can build kernel on non-POSIX systems and run on
 non-Linux systems.

---
 Cargo.lock           | 19 -----------------
 repbuild/Cargo.toml  |  5 -----
 repbuild/src/main.rs | 50 ++++++++++++++++++--------------------------
 3 files changed, 20 insertions(+), 54 deletions(-)

diff --git a/Cargo.lock b/Cargo.lock
index 7748189..f81bb7c 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -388,18 +388,6 @@ version = "2.5.0"
 source = "registry+https://github.com/rust-lang/crates.io-index"
 checksum = "2dffe52ecf27772e601905b7522cb4ef790d2cc203488bbd0e2fe85fcb74566d"
 
-[[package]]
-name = "nix"
-version = "0.26.1"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "46a58d1d356c6597d08cde02c2f09d785b09e28711837b1ed667dc652c08a694"
-dependencies = [
- "bitflags",
- "cfg-if",
- "libc",
- "static_assertions",
-]
-
 [[package]]
 name = "num-integer"
 version = "0.1.45"
@@ -492,7 +480,6 @@ dependencies = [
  "error-stack",
  "fatfs",
  "log",
- "nix",
 ]
 
 [[package]]
@@ -580,12 +567,6 @@ dependencies = [
  "lock_api",
 ]
 
-[[package]]
-name = "static_assertions"
-version = "1.1.0"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "a2eb9349b6444b326872e140eb1cf5e7c522154d69e7a0ffb0fb81c06b37543f"
-
 [[package]]
 name = "supports-color"
 version = "1.3.1"
diff --git a/repbuild/Cargo.toml b/repbuild/Cargo.toml
index 4d0b89b..5e185b8 100644
--- a/repbuild/Cargo.toml
+++ b/repbuild/Cargo.toml
@@ -8,8 +8,3 @@ env_logger = "0.10"
 error-stack = "0.2"
 fatfs = "0.3"
 log = "0.4"
-
-[dependencies.nix]
-version = "0.26"
-default-features = false
-features = ["fs"]
diff --git a/repbuild/src/main.rs b/repbuild/src/main.rs
index e6bbcb4..0a6d7da 100644
--- a/repbuild/src/main.rs
+++ b/repbuild/src/main.rs
@@ -24,17 +24,15 @@ fn main() -> Result<(), Error> {
             run()
         }
         Some("help" | "h") => {
-            println!(
-                concat!(
-                    "AbleOS RepBuild\n",
-                    "Subcommands:\n",
-                    "  build (b): Build a bootable disk image\n",
-                    "   help (h): Print this message\n",
-                    "    run (r): Build and run AbleOS in QEMU\n\n",
-                    "Options for build and run:\n",
-                    "  -r: build in release mode",
-                ),
-            );
+            println!(concat!(
+                "AbleOS RepBuild\n",
+                "Subcommands:\n",
+                "  build (b): Build a bootable disk image\n",
+                "   help (h): Print this message\n",
+                "    run (r): Build and run AbleOS in QEMU\n\n",
+                "Options for build and run:\n",
+                "  -r: build in release mode",
+            ),);
             Ok(())
         }
         _ => Err(report!(Error::InvalidSubCom)),
@@ -62,19 +60,7 @@ fn get_fs() -> Result<FileSystem<impl ReadWriteSeek>, io::Error> {
         .create(true)
         .open(path)?;
 
-    {
-        #[cfg(unix)]
-        nix::fcntl::fallocate(
-            img.as_raw_fd(),
-            FallocateFlags::from_bits(0).unwrap(),
-            0,
-            1024 * 1024 * 64,
-        )
-        .map_err(|e| std::io::Error::from_raw_os_error(e as i32))?;
-
-        #[cfg(not(any(unix)))]
-        compile_error!("unsupported platform");
-    }
+    img.set_len(1024 * 1024 * 64)?;
 
     fatfs::format_volume(&mut img, FormatVolumeOptions::new())?;
 
@@ -122,19 +108,23 @@ fn build(release: bool) -> Result<(), Error> {
 }
 
 fn run() -> Result<(), Error> {
+    let mut com = Command::new("qemu-system-x86_64");
+
     #[rustfmt::skip]
-    let args = [
+    com.args([
         "-bios", "/usr/share/OVMF/OVMF_CODE.fd",
-        "-enable-kvm",
-        "-cpu", "host",
         "-drive", "file=target/disk.img,format=raw",
         "-m", "4G",
         "-serial", "stdio",
         "-smp", "cores=2",
-    ];
+    ]);
 
-    match Command::new("qemu-system-x86_64")
-        .args(args)
+    #[cfg(target_os = "linux")]
+    {
+        com.args(["-enable-kvm", "-cpu", "host"]);
+    }
+
+    match com
         .status()
         .into_report()
         .change_context(Error::ProcessSpawn)?