Skip to content

Commit

Permalink
chore: add forced-target for x86_64-core (#226)
Browse files Browse the repository at this point in the history
This should result in it no longer breaking the build on non-x86_64
build host platforms. Unfortunately, it also means we need to remove the
 `mnemos-x86_64-core` crate from the workspace's `default-members`. See:
 #225 (comment)

Fixes #225

---------

Co-authored-by: James Munns <[email protected]>
  • Loading branch information
hawkw and jamesmunns authored Aug 12, 2023
1 parent 8a7f4e5 commit 455050b
Show file tree
Hide file tree
Showing 7 changed files with 122 additions and 89 deletions.
7 changes: 4 additions & 3 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ default-members = [
"platforms/beepy",
"platforms/melpomene",
"platforms/melpomene/melpo-config",
"platforms/x86_64/core",
]
# this isn't actually a crate
exclude = ["source/notes"]
Expand Down
14 changes: 12 additions & 2 deletions justfile
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ _d1_pkg := "mnemos-d1"

_espbuddy_pkg := "mnemos-esp32c3-buddy"

_x86_pkg := "mnemos-x86_64-bootloader"
_x86_bootloader_pkg := "mnemos-x86_64-bootloader"

# arguments to pass to all RustDoc invocations
Expand All @@ -54,27 +55,35 @@ default:
@just --list

# check all crates, across workspaces
check: && (check-crate _d1_pkg) (check-crate _espbuddy_pkg) (check-crate _x86_bootloader_pkg)
check: && (check-crate _d1_pkg) (check-crate _espbuddy_pkg) (check-crate _x86_pkg) (check-crate _x86_bootloader_pkg)
#!/usr/bin/env bash
set -euxo pipefail
{{ _cargo }} check \
--lib --bins --examples --tests --benches \
{{ _fmt_check_doc }}
# check a crate.
check-crate crate:
#!/usr/bin/env bash
set -euxo pipefail
{{ _cargo }} check \
--lib --bins --examples --tests --benches --all-features \
--package {{ crate }} \
{{ _fmt_check_doc }}
# run Clippy checks for all crates, across workspaces.
clippy: && (clippy-crate _d1_pkg) (clippy-crate _espbuddy_pkg) (clippy-crate _x86_bootloader_pkg)
clippy: && (clippy-crate _d1_pkg) (clippy-crate _espbuddy_pkg) (clippy-crate _x86_pkg) (clippy-crate _x86_bootloader_pkg)
#!/usr/bin/env bash
set -euxo pipefail
{{ _cargo }} clippy \
--lib --bins --examples --tests --benches --all-features \
{{ _fmt_clippy }}
# run clippy checks for a crate.
# NOTE: -Dwarnings is added by _fmt because reasons
clippy-crate crate:
#!/usr/bin/env bash
set -euxo pipefail
{{ _cargo }} clippy \
--lib --bins --examples --tests --benches \
--package {{ crate }} \
Expand All @@ -89,6 +98,7 @@ fmt:
{{ _cargo }} fmt
{{ _cargo }} fmt --package {{ _d1_pkg }}
{{ _cargo }} fmt --package {{ _espbuddy_pkg }}
{{ _cargo }} fmt --package {{ _x86_pkg }}
{{ _cargo }} fmt --package {{ _x86_bootloader_pkg }}

# build a Mnemos binary for the Allwinner D1
Expand Down
1 change: 1 addition & 0 deletions platforms/x86_64/bootloader/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ version = "0.1.0"
edition = "2021"

[dependencies]
anyhow = "1"
# used for UEFI booting in QEMU
ovmf-prebuilt = "0.1.0-alpha.1"
# used for the QEMU runner
Expand Down
98 changes: 98 additions & 0 deletions platforms/x86_64/bootloader/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
use anyhow::Context;
use clap::Parser;
use std::{fmt, path::PathBuf};

/// Boots a MnemOS x86_64 kernel using QEMU.
#[derive(Parser, Debug)]
pub struct Args {
/// Path to the UEFI disk image.
///
/// This environment variable is set by the build script and typically does
/// not need to be set manually.
#[clap(long, env = "UEFI_PATH")]
uefi_path: PathBuf,

/// Path to the BIOS disk image.
///
/// This environment variable is set by the build script and typically does
/// not need to be set manually.
#[clap(long, env = "BIOS_PATH")]
bios_path: PathBuf,

/// Path to the QEMU x86_64 executable.
///
/// Generally, this does not need to be overridden, unless the QEMU binary
/// has a non-standard name or is not on the PATH.
#[clap(long, default_value = "qemu-system-x86_64")]
qemu: PathBuf,

/// Whether to boot using UEFI or BIOS.
#[clap(long, default_value_t = BootMode::Uefi)]
boot: BootMode,

/// Extra arguments passed directly to the QEMU command.
#[arg(last = true)]
qemu_args: Vec<String>,
}

#[derive(Copy, Clone, Debug, Eq, PartialEq, clap::ValueEnum)]
enum BootMode {
Uefi,
Bios,
}

// === impl Args ===

impl Args {
pub fn run(self) -> anyhow::Result<()> {
let Args {
uefi_path,
bios_path,
boot,
qemu,
qemu_args,
} = self;

let mut cmd = std::process::Command::new(qemu);
match boot {
BootMode::Uefi => {
let uefi_path = uefi_path.display();
println!("booting using UEFI: {uefi_path}");

cmd.arg("-bios").arg(ovmf_prebuilt::ovmf_pure_efi());
cmd.arg("-drive")
.arg(format!("format=raw,file={uefi_path}"));
}
BootMode::Bios => {
let bios_path = bios_path.display();
println!("booting using BIOS: {bios_path}");
cmd.arg("-drive")
.arg(format!("format=raw,file={bios_path}"));
}
}

if !qemu_args.is_empty() {
cmd.args(&qemu_args);
}

let mut child = cmd.spawn().context("failed to spawn QEMU child process")?;
let status = child.wait().context("QEMU child process failed")?;

if !status.success() {
anyhow::bail!("QEMU exited with status: {}", status);
}

Ok(())
}
}

// === impl BootMode ===

impl fmt::Display for BootMode {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Uefi => f.write_str("uefi"),
Self::Bios => f.write_str("bios"),
}
}
}
85 changes: 3 additions & 82 deletions platforms/x86_64/bootloader/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,85 +1,6 @@
use clap::Parser;
use std::{fmt, path::PathBuf};
use mnemos_x86_64_bootloader::Args;

/// Boots a MnemOS x86_64 kernel using QEMU.
#[derive(Parser, Debug)]
struct Args {
/// Path to the UEFI disk image.
///
/// This environment variable is set by the build script and typically does
/// not need to be set manually.
#[clap(long, env = "UEFI_PATH")]
uefi_path: PathBuf,

/// Path to the BIOS disk image.
///
/// This environment variable is set by the build script and typically does
/// not need to be set manually.
#[clap(long, env = "BIOS_PATH")]
bios_path: PathBuf,

/// Path to the QEMU x86_64 executable.
///
/// Generally, this does not need to be overridden, unless the QEMU binary
/// has a non-standard name or is not on the PATH.
#[clap(long, default_value = "qemu-system-x86_64")]
qemu: PathBuf,

/// Whether to boot using UEFI or BIOS.
#[clap(long, default_value_t = BootMode::Uefi)]
boot: BootMode,

/// Extra arguments passed directly to the QEMU command.
#[arg(last = true)]
qemu_args: Vec<String>,
}

#[derive(Copy, Clone, Debug, Eq, PartialEq, clap::ValueEnum)]
enum BootMode {
Uefi,
Bios,
}

fn main() {
let Args {
uefi_path,
bios_path,
boot,
qemu,
qemu_args,
} = Args::parse();

let mut cmd = std::process::Command::new(qemu);
match boot {
BootMode::Uefi => {
let uefi_path = uefi_path.display();
println!("booting using UEFI: {uefi_path}");

cmd.arg("-bios").arg(ovmf_prebuilt::ovmf_pure_efi());
cmd.arg("-drive")
.arg(format!("format=raw,file={uefi_path}"));
}
BootMode::Bios => {
let bios_path = bios_path.display();
println!("booting using BIOS: {bios_path}");
cmd.arg("-drive")
.arg(format!("format=raw,file={bios_path}"));
}
}

if !qemu_args.is_empty() {
cmd.args(&qemu_args);
}

let mut child = cmd.spawn().unwrap();
child.wait().unwrap();
}

impl fmt::Display for BootMode {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Uefi => f.write_str("uefi"),
Self::Bios => f.write_str("bios"),
}
}
fn main() -> anyhow::Result<()> {
Args::parse().run()
}
5 changes: 4 additions & 1 deletion platforms/x86_64/core/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
cargo-features = ["per-package-target", "profile-rustflags"]

[package]
name = "mnemos-x86_64-core"
version = "0.1.0"
edition = "2021"
forced-target = "x86_64-unknown-none"

[lib]
test = false
Expand Down Expand Up @@ -52,4 +55,4 @@ features = ["buddy", "bump"]
[dependencies.futures]
version = "0.3.21"
features = ["async-await"]
default-features = false
default-features = false

0 comments on commit 455050b

Please sign in to comment.