Skip to content

Commit 2e73c32

Browse files
committed
Document all library types
1 parent df26d3e commit 2e73c32

File tree

4 files changed

+75
-1
lines changed

4 files changed

+75
-1
lines changed

src/args.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
//! Parses command line arguments.
2+
13
use crate::{config::Config, Command, ErrorMessage};
24
use std::path::{Path, PathBuf};
35
use std::{env, mem};

src/builder.rs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,21 @@
1+
//! Provides functions to build the kernel and the bootloader.
2+
13
use std::{
24
fmt, fs, io,
35
path::{Path, PathBuf},
46
process::{self, Command},
57
};
68

9+
/// Abstracts a build environment and provides methods for building the kernel and creating a
10+
/// bootimage.
711
pub struct Builder {
812
kernel_manifest_path: PathBuf,
913
kernel_metadata: cargo_metadata::Metadata,
1014
}
1115

1216
impl Builder {
17+
/// Creates a new Builder by searching for the kernel's Cargo manifest and running
18+
/// `cargo metadata` on it.
1319
pub fn new(manifest_path: Option<PathBuf>) -> Result<Self, BuilderError> {
1420
let kernel_manifest_path =
1521
manifest_path.unwrap_or(locate_cargo_manifest::locate_manifest()?);
@@ -22,20 +28,24 @@ impl Builder {
2228
})
2329
}
2430

31+
/// Returns the path to the `Cargo.toml` file of the kernel.
2532
pub fn kernel_manifest_path(&self) -> &Path {
2633
&self.kernel_manifest_path
2734
}
2835

36+
/// Returns the directory that contains the `Cargo.toml` of the kernel.
2937
pub fn kernel_root(&self) -> &Path {
3038
self.kernel_manifest_path
3139
.parent()
3240
.expect("kernel manifest has no parent directory")
3341
}
3442

43+
/// Returns a reference to the cargo metadata object.
3544
pub fn kernel_metadata(&self) -> &cargo_metadata::Metadata {
3645
&self.kernel_metadata
3746
}
3847

48+
/// Returns a reference to the kernel package in the `cargo metadata` output.
3949
pub fn kernel_package(&self) -> Result<&cargo_metadata::Package, String> {
4050
let mut packages = self.kernel_metadata.packages.iter();
4151
let kernel_package = packages.find(|p| &p.manifest_path == &self.kernel_manifest_path);
@@ -45,6 +55,12 @@ impl Builder {
4555
))
4656
}
4757

58+
/// Builds the kernel by executing `cargo xbuild` with the given arguments.
59+
///
60+
/// Returns a list of paths to all built executables. For crates with only a single binary,
61+
/// the returned list contains only a single element.
62+
///
63+
/// If the quiet argument is set to true, all output to stdout is suppressed.
4864
pub fn build_kernel(
4965
&self,
5066
args: &[String],
@@ -110,6 +126,11 @@ impl Builder {
110126
Ok(executables)
111127
}
112128

129+
/// Creates a bootimage by combining the given kernel binary with the bootloader.
130+
///
131+
/// Places the resulting bootable disk image at the given `output_bin_path`.
132+
///
133+
/// If the quiet argument is set to true, all output to stdout is suppressed.
113134
pub fn create_bootimage(
114135
&self,
115136
kernel_bin_path: &Path,
@@ -284,6 +305,7 @@ impl Builder {
284305
}
285306
}
286307

308+
/// Represents an error that occurred while creating a new `Builder`.
287309
#[derive(Debug)]
288310
pub enum BuilderError {
289311
/// Failed to locate cargo manifest
@@ -309,6 +331,7 @@ impl fmt::Display for BuilderError {
309331
}
310332
}
311333

334+
/// Represents an error that occurred when building the kernel.
312335
#[derive(Debug)]
313336
pub enum BuildKernelError {
314337
/// Could not find kernel package in cargo metadata, required for retrieving kernel crate name
@@ -320,11 +343,16 @@ pub enum BuildKernelError {
320343
/// The I/O error that occured
321344
error: io::Error,
322345
},
346+
/// Could not find the `cargo xbuild` tool. Perhaps it is not installed?
323347
XbuildNotFound,
348+
/// Running `cargo xbuild` failed.
324349
XbuildFailed {
350+
/// The standard error output.
325351
stderr: Vec<u8>,
326352
},
353+
/// The output of `cargo xbuild --message-format=json` was not valid UTF-8
327354
XbuildJsonOutputInvalidUtf8(std::string::FromUtf8Error),
355+
/// The output of `cargo xbuild --message-format=json` was not valid JSON
328356
XbuildJsonOutputInvalidJson(json::Error),
329357
#[doc(hidden)]
330358
__NonExhaustive,
@@ -357,6 +385,7 @@ impl fmt::Display for BuildKernelError {
357385
}
358386
}
359387

388+
/// Represents an error that occurred when creating a bootimage.
360389
#[derive(Debug)]
361390
pub enum CreateBootimageError {
362391
/// Could not find some required information in the `cargo metadata` output
@@ -368,7 +397,9 @@ pub enum CreateBootimageError {
368397
BootloaderNotFound,
369398
/// Bootloader dependency has not the right format
370399
BootloaderInvalid(String),
400+
/// Building the bootloader failed
371401
BootloaderBuildFailed {
402+
/// The `cargo xbuild` output to standard error
372403
stderr: Vec<u8>,
373404
},
374405
/// An unexpected I/O error occurred
@@ -384,9 +415,12 @@ pub enum CreateBootimageError {
384415
LlvmObjcopyNotFound,
385416
/// The `llvm-objcopy` command failed
386417
ObjcopyFailed {
418+
/// The output of `llvm-objcopy` to standard error
387419
stderr: Vec<u8>,
388420
},
421+
/// The output of `cargo xbuild --message-format=json` was not valid UTF-8
389422
XbuildJsonOutputInvalidUtf8(std::string::FromUtf8Error),
423+
/// The output of `cargo xbuild --message-format=json` was not valid JSON
390424
XbuildJsonOutputInvalidJson(json::Error),
391425
#[doc(hidden)]
392426
__NonExhaustive,

src/config.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,25 @@
1+
//! Parses the `package.metadata.bootimage` configuration table
2+
13
use crate::ErrorMessage;
24
use std::path::Path;
35
use toml::Value;
46

7+
/// Represents the `package.metadata.bootimage` configuration table
8+
///
9+
/// The bootimage crate can be configured through a `package.metadata.bootimage` table
10+
/// in the `Cargo.toml` file of the kernel. This struct represents the parsed configuration
11+
/// options.
512
#[derive(Debug, Clone)]
613
pub struct Config {
14+
/// This target is used if no `--target` argument is passed
715
pub default_target: Option<String>,
16+
/// The run command that is invoked on `bootimage run` or `bootimage runner`
17+
///
18+
/// The substring "{}" will be replaced with the path to the bootable disk image.
819
pub run_command: Vec<String>,
20+
/// Additional arguments passed to the runner on `bootimage run` or `bootimage runner`
921
pub run_args: Option<Vec<String>>,
22+
/// The timeout for running an integration test through `bootimage test` in seconds
1023
pub test_timeout: u32,
1124
non_exhaustive: (),
1225
}

src/lib.rs

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,14 @@
1+
//! Provides functions to create a bootable OS image from a kernel binary.
2+
//!
3+
//! This crate is mainly built as a binary tool. Run `cargo install bootimage` to install it.
4+
5+
#![warn(missing_docs)]
6+
17
use args::{Args, RunnerArgs};
28
use std::{fmt, process};
39

4-
pub mod builder;
510
mod args;
11+
pub mod builder;
612
pub mod config;
713
pub mod help;
814

@@ -23,6 +29,13 @@ enum Command {
2329
Version,
2430
}
2531

32+
/// The entry point for the binaries.
33+
///
34+
/// We support two binaries, `bootimage` and `cargo-bootimage` that both just
35+
/// call into this function.
36+
///
37+
/// This function is just a small wrapper around [`run`] that prints error messages
38+
/// and exits with the correct exit code.
2639
pub fn lib_main() {
2740
match run() {
2841
Err(err) => {
@@ -36,6 +49,13 @@ pub fn lib_main() {
3649
}
3750
}
3851

52+
/// Run the invoked command.
53+
///
54+
/// This function parses the arguments and invokes the chosen subcommand.
55+
///
56+
/// On success, it optionally returns an exit code. This feature is used by the
57+
/// `run` and `runner` subcommand to pass through the exit code of the invoked
58+
/// run command.
3959
pub fn run() -> Result<Option<i32>, ErrorMessage> {
4060
let command = args::parse_args()?;
4161
let none = |()| None;
@@ -54,7 +74,12 @@ pub fn run() -> Result<Option<i32>, ErrorMessage> {
5474
}
5575
}
5676

77+
/// A simple error message that can be created from every type that implements `fmt::Display`.
78+
///
79+
/// We use this error type for the CLI interface, where text based, human readable error messages
80+
/// make sense. For the library part of this crate, we use custom error enums.
5781
pub struct ErrorMessage {
82+
/// The actual error message
5883
pub message: Box<dyn fmt::Display + Send>,
5984
}
6085

0 commit comments

Comments
 (0)