1
1
//! Provides functions to build the kernel and the bootloader.
2
2
3
+ use cargo_metadata:: Metadata ;
3
4
use error:: { BootloaderError , BuildKernelError , BuilderError , CreateBootimageError } ;
4
5
use std:: {
5
6
path:: { Path , PathBuf } ,
@@ -16,6 +17,7 @@ pub mod error;
16
17
/// Allows building the kernel and creating a bootable disk image with it.
17
18
pub struct Builder {
18
19
manifest_path : PathBuf ,
20
+ project_metadata : Option < Metadata > ,
19
21
}
20
22
21
23
impl Builder {
@@ -24,7 +26,10 @@ impl Builder {
24
26
/// If None is passed for `manifest_path`, it is automatically searched.
25
27
pub fn new ( manifest_path : Option < PathBuf > ) -> Result < Self , BuilderError > {
26
28
let manifest_path = manifest_path. unwrap_or ( locate_cargo_manifest:: locate_manifest ( ) ?) ;
27
- Ok ( Builder { manifest_path } )
29
+ Ok ( Builder {
30
+ manifest_path,
31
+ project_metadata : None ,
32
+ } )
28
33
}
29
34
30
35
/// Returns the path to the Cargo.toml file of the project.
@@ -111,17 +116,17 @@ impl Builder {
111
116
///
112
117
/// If the quiet argument is set to true, all output to stdout is suppressed.
113
118
pub fn create_bootimage (
114
- & self ,
119
+ & mut self ,
115
120
bin_name : & str ,
116
121
bin_path : & Path ,
117
122
output_bin_path : & Path ,
118
123
quiet : bool ,
119
124
) -> Result < ( ) , CreateBootimageError > {
120
- let project_metadata = cargo_metadata :: MetadataCommand :: new ( )
121
- . manifest_path ( & self . manifest_path )
122
- . exec ( ) ? ;
123
- let bootloader_build_config =
124
- bootloader :: BuildConfig :: from_metadata ( & project_metadata , bin_name , bin_path ) ?;
125
+ let bootloader_build_config = bootloader :: BuildConfig :: from_metadata (
126
+ self . project_metadata ( ) ? ,
127
+ kernel_manifest_path ,
128
+ bin_path ,
129
+ ) ?;
125
130
126
131
// build bootloader
127
132
if !quiet {
@@ -181,4 +186,14 @@ impl Builder {
181
186
182
187
Ok ( ( ) )
183
188
}
189
+
190
+ fn project_metadata ( & mut self ) -> Result < & Metadata , cargo_metadata:: Error > {
191
+ if let Some ( ref metadata) = self . project_metadata {
192
+ return Ok ( metadata) ;
193
+ }
194
+ let metadata = cargo_metadata:: MetadataCommand :: new ( )
195
+ . manifest_path ( & self . manifest_path )
196
+ . exec ( ) ?;
197
+ Ok ( self . project_metadata . get_or_insert ( metadata) )
198
+ }
184
199
}
0 commit comments