Skip to main content

bootimage/builder/
error.rs

1use std::{io, path::PathBuf};
2use thiserror::Error;
3
4/// Represents an error that occurred while creating a new `Builder`.
5#[derive(Debug, Error)]
6pub enum BuilderError {
7    /// Failed to locate cargo manifest
8    #[error("Could not find Cargo.toml file starting from current folder: {0:?}")]
9    LocateCargoManifest(#[from] locate_cargo_manifest::LocateManifestError),
10}
11
12/// Represents an error that occurred when building the kernel.
13#[derive(Debug, Error)]
14#[non_exhaustive]
15pub enum BuildKernelError {
16    /// An unexpected I/O error occurred
17    #[error("I/O error: {message}:\n{error}")]
18    Io {
19        /// Desciption of the failed I/O operation
20        message: &'static str,
21        /// The I/O error that occurred
22        error: io::Error,
23    },
24
25    /// Could not find the `cargo xbuild` tool. Perhaps it is not installed?
26    #[error(
27        "Failed to run `cargo xbuild`. Perhaps it is not installed?\n\
28    Run `cargo install cargo-xbuild` to install it."
29    )]
30    XbuildNotFound,
31
32    /// Running `cargo build` failed.
33    #[error("Kernel build failed.\nStderr: {}", String::from_utf8_lossy(.stderr))]
34    BuildFailed {
35        /// The standard error output.
36        stderr: Vec<u8>,
37    },
38
39    /// The output of `cargo build --message-format=json` was not valid UTF-8
40    #[error("Output of kernel build with --message-format=json is not valid UTF-8:\n{0}")]
41    BuildJsonOutputInvalidUtf8(std::string::FromUtf8Error),
42    /// The output of `cargo build --message-format=json` was not valid JSON
43    #[error("Output of kernel build with --message-format=json is not valid JSON:\n{0}")]
44    BuildJsonOutputInvalidJson(json::Error),
45}
46
47/// Represents an error that occurred when creating a bootimage.
48#[derive(Debug, Error)]
49#[non_exhaustive]
50pub enum CreateBootimageError {
51    /// Failed to build the bootloader.
52    #[error("An error occurred while trying to build the bootloader: {0}")]
53    Bootloader(#[from] BootloaderError),
54
55    /// Error while running `cargo metadata`
56    #[error("Error while running `cargo metadata` for current project: {0:?}")]
57    CargoMetadata(#[from] cargo_metadata::Error),
58
59    /// Building the bootloader failed
60    #[error("Bootloader build failed.\nStderr: {}", String::from_utf8_lossy(.stderr))]
61    BootloaderBuildFailed {
62        /// The `cargo build` output to standard error
63        stderr: Vec<u8>,
64    },
65
66    /// Disk image creation failed
67    #[error("An error occurred while trying to create the disk image: {0}")]
68    DiskImage(#[from] DiskImageError),
69
70    /// An unexpected I/O error occurred
71    #[error("I/O error: {message}:\n{error}")]
72    Io {
73        /// Desciption of the failed I/O operation
74        message: &'static str,
75        /// The I/O error that occurred
76        error: io::Error,
77    },
78
79    /// The output of `cargo build --message-format=json` was not valid UTF-8
80    #[error("Output of bootloader build with --message-format=json is not valid UTF-8:\n{0}")]
81    BuildJsonOutputInvalidUtf8(std::string::FromUtf8Error),
82    /// The output of `cargo build --message-format=json` was not valid JSON
83    #[error("Output of bootloader build with --message-format=json is not valid JSON:\n{0}")]
84    BuildJsonOutputInvalidJson(json::Error),
85}
86
87/// There is something wrong with the bootloader dependency.
88#[derive(Debug, Error)]
89pub enum BootloaderError {
90    /// Bootloader dependency not found
91    #[error(
92        "Bootloader dependency not found\n\n\
93        You need to add a dependency on a crate named `bootloader` in your Cargo.toml."
94    )]
95    BootloaderNotFound,
96
97    /// Bootloader dependency has not the right format
98    #[error("The `bootloader` dependency has not the right format: {0}")]
99    BootloaderInvalid(String),
100
101    /// Could not find kernel package in cargo metadata
102    #[error(
103        "Could not find package with manifest path `{manifest_path}` in cargo metadata output"
104    )]
105    KernelPackageNotFound {
106        /// The manifest path of the kernel package
107        manifest_path: PathBuf,
108    },
109
110    /// Could not find some required information in the `cargo metadata` output
111    #[error("Could not find required key `{key}` in cargo metadata output")]
112    CargoMetadataIncomplete {
113        /// The required key that was not found
114        key: String,
115    },
116}
117
118/// Creating the disk image failed.
119#[derive(Debug, Error)]
120pub enum DiskImageError {
121    /// The `llvm-tools-preview` rustup component was not found
122    #[error(
123        "Could not find the `llvm-tools-preview` rustup component.\n\n\
124        You can install by executing `rustup component add llvm-tools-preview`."
125    )]
126    LlvmToolsNotFound,
127
128    /// There was another problem locating the `llvm-tools-preview` rustup component
129    #[error("Failed to locate the `llvm-tools-preview` rustup component: {0:?}")]
130    LlvmTools(llvm_tools::Error),
131
132    /// The llvm-tools component did not contain the required `llvm-objcopy` executable
133    #[error("Could not find `llvm-objcopy` in the `llvm-tools-preview` rustup component.")]
134    LlvmObjcopyNotFound,
135
136    /// The `llvm-objcopy` command failed
137    #[error("Failed to run `llvm-objcopy`: {}", String::from_utf8_lossy(.stderr))]
138    ObjcopyFailed {
139        /// The output of `llvm-objcopy` to standard error
140        stderr: Vec<u8>,
141    },
142
143    /// An unexpected I/O error occurred
144    #[error("I/O error: {message}:\n{error}")]
145    Io {
146        /// Desciption of the failed I/O operation
147        message: &'static str,
148        /// The I/O error that occurred
149        error: io::Error,
150    },
151}
152
153impl From<llvm_tools::Error> for DiskImageError {
154    fn from(err: llvm_tools::Error) -> Self {
155        match err {
156            llvm_tools::Error::NotFound => DiskImageError::LlvmToolsNotFound,
157            other => DiskImageError::LlvmTools(other),
158        }
159    }
160}