1
+ //! Provides functions to build the kernel and the bootloader.
2
+
1
3
use std:: {
2
4
fmt, fs, io,
3
5
path:: { Path , PathBuf } ,
4
6
process:: { self , Command } ,
5
7
} ;
6
8
9
+ /// Abstracts a build environment and provides methods for building the kernel and creating a
10
+ /// bootimage.
7
11
pub struct Builder {
8
12
kernel_manifest_path : PathBuf ,
9
13
kernel_metadata : cargo_metadata:: Metadata ,
10
14
}
11
15
12
16
impl Builder {
17
+ /// Creates a new Builder by searching for the kernel's Cargo manifest and running
18
+ /// `cargo metadata` on it.
13
19
pub fn new ( manifest_path : Option < PathBuf > ) -> Result < Self , BuilderError > {
14
20
let kernel_manifest_path =
15
21
manifest_path. unwrap_or ( locate_cargo_manifest:: locate_manifest ( ) ?) ;
@@ -22,20 +28,24 @@ impl Builder {
22
28
} )
23
29
}
24
30
31
+ /// Returns the path to the `Cargo.toml` file of the kernel.
25
32
pub fn kernel_manifest_path ( & self ) -> & Path {
26
33
& self . kernel_manifest_path
27
34
}
28
35
36
+ /// Returns the directory that contains the `Cargo.toml` of the kernel.
29
37
pub fn kernel_root ( & self ) -> & Path {
30
38
self . kernel_manifest_path
31
39
. parent ( )
32
40
. expect ( "kernel manifest has no parent directory" )
33
41
}
34
42
43
+ /// Returns a reference to the cargo metadata object.
35
44
pub fn kernel_metadata ( & self ) -> & cargo_metadata:: Metadata {
36
45
& self . kernel_metadata
37
46
}
38
47
48
+ /// Returns a reference to the kernel package in the `cargo metadata` output.
39
49
pub fn kernel_package ( & self ) -> Result < & cargo_metadata:: Package , String > {
40
50
let mut packages = self . kernel_metadata . packages . iter ( ) ;
41
51
let kernel_package = packages. find ( |p| & p. manifest_path == & self . kernel_manifest_path ) ;
@@ -45,6 +55,12 @@ impl Builder {
45
55
) )
46
56
}
47
57
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.
48
64
pub fn build_kernel (
49
65
& self ,
50
66
args : & [ String ] ,
@@ -110,6 +126,11 @@ impl Builder {
110
126
Ok ( executables)
111
127
}
112
128
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.
113
134
pub fn create_bootimage (
114
135
& self ,
115
136
kernel_bin_path : & Path ,
@@ -284,6 +305,7 @@ impl Builder {
284
305
}
285
306
}
286
307
308
+ /// Represents an error that occurred while creating a new `Builder`.
287
309
#[ derive( Debug ) ]
288
310
pub enum BuilderError {
289
311
/// Failed to locate cargo manifest
@@ -309,6 +331,7 @@ impl fmt::Display for BuilderError {
309
331
}
310
332
}
311
333
334
+ /// Represents an error that occurred when building the kernel.
312
335
#[ derive( Debug ) ]
313
336
pub enum BuildKernelError {
314
337
/// Could not find kernel package in cargo metadata, required for retrieving kernel crate name
@@ -320,11 +343,16 @@ pub enum BuildKernelError {
320
343
/// The I/O error that occured
321
344
error : io:: Error ,
322
345
} ,
346
+ /// Could not find the `cargo xbuild` tool. Perhaps it is not installed?
323
347
XbuildNotFound ,
348
+ /// Running `cargo xbuild` failed.
324
349
XbuildFailed {
350
+ /// The standard error output.
325
351
stderr : Vec < u8 > ,
326
352
} ,
353
+ /// The output of `cargo xbuild --message-format=json` was not valid UTF-8
327
354
XbuildJsonOutputInvalidUtf8 ( std:: string:: FromUtf8Error ) ,
355
+ /// The output of `cargo xbuild --message-format=json` was not valid JSON
328
356
XbuildJsonOutputInvalidJson ( json:: Error ) ,
329
357
#[ doc( hidden) ]
330
358
__NonExhaustive,
@@ -357,6 +385,7 @@ impl fmt::Display for BuildKernelError {
357
385
}
358
386
}
359
387
388
+ /// Represents an error that occurred when creating a bootimage.
360
389
#[ derive( Debug ) ]
361
390
pub enum CreateBootimageError {
362
391
/// Could not find some required information in the `cargo metadata` output
@@ -368,7 +397,9 @@ pub enum CreateBootimageError {
368
397
BootloaderNotFound ,
369
398
/// Bootloader dependency has not the right format
370
399
BootloaderInvalid ( String ) ,
400
+ /// Building the bootloader failed
371
401
BootloaderBuildFailed {
402
+ /// The `cargo xbuild` output to standard error
372
403
stderr : Vec < u8 > ,
373
404
} ,
374
405
/// An unexpected I/O error occurred
@@ -384,9 +415,12 @@ pub enum CreateBootimageError {
384
415
LlvmObjcopyNotFound ,
385
416
/// The `llvm-objcopy` command failed
386
417
ObjcopyFailed {
418
+ /// The output of `llvm-objcopy` to standard error
387
419
stderr : Vec < u8 > ,
388
420
} ,
421
+ /// The output of `cargo xbuild --message-format=json` was not valid UTF-8
389
422
XbuildJsonOutputInvalidUtf8 ( std:: string:: FromUtf8Error ) ,
423
+ /// The output of `cargo xbuild --message-format=json` was not valid JSON
390
424
XbuildJsonOutputInvalidJson ( json:: Error ) ,
391
425
#[ doc( hidden) ]
392
426
__NonExhaustive,
0 commit comments