Skip to content

Commit

Permalink
Project: Add kernel stub, and include root project workspace
Browse files Browse the repository at this point in the history
  • Loading branch information
corigan01 committed Mar 18, 2024
1 parent 0aa097d commit 697fa74
Show file tree
Hide file tree
Showing 7 changed files with 89 additions and 33 deletions.
5 changes: 5 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[workspace]
members = ["kernel", "meta"]
exclude = ["bootloader"]
default-members = ["meta"]
resolver = "2"
53 changes: 20 additions & 33 deletions bootloader/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,39 +53,11 @@ fn target_dir() -> String {
env::var("OUT_DIR").unwrap()
}

fn output_path(arch: ArchSelect, package: &str) -> String {
let target = target_dir();

// Maybe could just be a match, but to avoid having to
// update paths in two spots I made it take the target
// from the arch path.
let arch_name = arch.to_string().to_lowercase();
let arch_name = arch_name
.split("/")
.last()
.unwrap()
.split(".")
.nth(0)
.unwrap();

// FIXME: This is a hacky solution for what should be
// an easy problem. Maybe there is a better way todo this?
Path::new(target.as_str())
.join("../../../../../")
.join(arch_name)
.join(package)
.join(package)
.canonicalize()
.unwrap()
.to_str()
.unwrap()
.into()
}

fn cargo_helper(profile: Option<&str>, package: &str, arch: ArchSelect) -> String {
let cargo_bin = cargo_path();
let compile_mode = compile_mode();
let compile_mode = profile.unwrap_or(compile_mode.as_str());
let target_dir = target_dir();

println!("cargo:rerun-if-changed={}", package);

Expand All @@ -94,21 +66,27 @@ fn cargo_helper(profile: Option<&str>, package: &str, arch: ArchSelect) -> Strin
.env_remove("CARGO_ENCODED_RUSTFLAGS")
.env_remove("RUSTC_WORKSPACE_WRAPPER")
.args([
"build",
"--package",
"install",
"--path",
package,
"--profile",
compile_mode,
"--target",
arch.to_string().as_str(),
"--root",
target_dir.as_str(),
])
.status()
.unwrap()
.success()
.then_some(())
.expect("Failed to build");

output_path(arch, package)
PathBuf::from(target_dir)
.join("bin")
.join(package)
.to_string_lossy()
.into()
}

fn convert_bin(path: &str, arch: ArchSelect) -> String {
Expand Down Expand Up @@ -146,10 +124,19 @@ fn build_stages() {
ArchSelect::I386,
);

let target_dir = PathBuf::from(manifest_dir()).join("target").join("bin");
let target_dir = PathBuf::from(target_dir()).join("bin");
fs::create_dir_all(&target_dir).unwrap();
fs::copy(bootsector, target_dir.join("stage-bootsector.bin")).unwrap();
fs::copy(stage_16bit, target_dir.join("stage-16bit.bin")).unwrap();

println!(
"cargo:rustc-env=STAGE_BOOTSECTOR_PATH={}",
target_dir.join("stage-bootsector.bin").to_str().unwrap()
);
println!(
"cargo:rustc-env=STAGE_16BIT_PATH={}",
target_dir.join("stage-16bit.bin").to_str().unwrap()
);
}

fn main() {
Expand Down
6 changes: 6 additions & 0 deletions kernel/.cargo/.cargo/config.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[build]
target = "x86_64-unknown-none"

[unstable]
bindeps = true
build-std = ["core", "alloc"]
9 changes: 9 additions & 0 deletions kernel/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[package]
name = "kernel"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
quantum-loader = { path="../bootloader" }
4 changes: 4 additions & 0 deletions kernel/rust-toolchain.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
[toolchain]
channel = "nightly"
target = ["x86_64-unknown-none"]
components = ["rustfmt", "clippy", "rust-src", "llvm-tools-preview"]
3 changes: 3 additions & 0 deletions kernel/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
fn main() {
println!("Hello, world!");
}
42 changes: 42 additions & 0 deletions meta/src/artifacts.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
use std::{
env,
path::{Path, PathBuf},
};

use anyhow::{Context, Result};
use async_process::Command;

#[derive(Clone, Debug)]
pub struct BootloaderArtifacts {
pub bootsector: PathBuf,
pub stage_16: PathBuf,
// stage_32: PathBuf,
// stage_64: PathBuf
}

pub async fn build_bootloader(project_root: &Path, release: bool) -> Result<BootloaderArtifacts> {
Command::new("cargo")
.env_clear()
.env("PATH", env::var("PATH")?)
.current_dir(project_root.join("bootloader"))
.args([
"build",
"--profile",
if release { "release" } else { "dev" },
])
.status()
.await?
.success()
.then_some(())
.context("Could not build Quantum-OS's Bootloader!")?;

Ok(BootloaderArtifacts {
bootsector: project_root
.join("bootloader/target/bin/stage-bootsector.bin")
.canonicalize()
.unwrap(),
stage_16: project_root
.join("bootloader/target/bin/stage-16bit.bin")
.canonicalize()?,
})
}

0 comments on commit 697fa74

Please sign in to comment.