1
- mod deserialize_vec ;
1
+ mod deserialize ;
2
2
3
3
use anyhow:: Context ;
4
4
use clap:: Parser ;
5
5
use codeql_extractor:: trap;
6
- use deserialize_vec:: deserialize_newline_or_comma_separated;
7
6
use figment:: {
8
7
providers:: { Env , Format , Serialized , Yaml } ,
9
8
value:: Value ,
@@ -13,14 +12,15 @@ use itertools::Itertools;
13
12
use ra_ap_cfg:: { CfgAtom , CfgDiff } ;
14
13
use ra_ap_ide_db:: FxHashMap ;
15
14
use ra_ap_intern:: Symbol ;
16
- use ra_ap_paths:: { AbsPath , Utf8PathBuf } ;
15
+ use ra_ap_load_cargo:: { LoadCargoConfig , ProcMacroServerChoice } ;
16
+ use ra_ap_paths:: { AbsPath , AbsPathBuf , Utf8PathBuf } ;
17
17
use ra_ap_project_model:: { CargoConfig , CargoFeatures , CfgOverrides , RustLibSource , Sysroot } ;
18
18
use rust_extractor_macros:: extractor_cli_config;
19
19
use serde:: { Deserialize , Serialize } ;
20
20
use std:: collections:: HashSet ;
21
21
use std:: fmt:: Debug ;
22
22
use std:: ops:: Not ;
23
- use std:: path:: PathBuf ;
23
+ use std:: path:: { Path , PathBuf } ;
24
24
25
25
#[ derive( Debug , PartialEq , Eq , Default , Serialize , Deserialize , Clone , Copy , clap:: ValueEnum ) ]
26
26
#[ serde( rename_all = "lowercase" ) ]
@@ -50,13 +50,22 @@ pub struct Config {
50
50
pub cargo_target : Option < String > ,
51
51
pub cargo_features : Vec < String > ,
52
52
pub cargo_cfg_overrides : Vec < String > ,
53
+ pub cargo_extra_env : FxHashMap < String , String > ,
54
+ pub cargo_extra_args : Vec < String > ,
55
+ pub cargo_all_targets : bool ,
53
56
pub logging_flamegraph : Option < PathBuf > ,
54
57
pub logging_verbosity : Option < String > ,
55
58
pub compression : Compression ,
56
59
pub inputs : Vec < PathBuf > ,
57
60
pub qltest : bool ,
58
61
pub qltest_cargo_check : bool ,
59
62
pub qltest_dependencies : Vec < String > ,
63
+ pub sysroot : Option < PathBuf > ,
64
+ pub sysroot_src : Option < PathBuf > ,
65
+ pub rustc_src : Option < PathBuf > ,
66
+ pub build_script_command : Vec < String > ,
67
+ pub extra_includes : Vec < PathBuf > ,
68
+ pub proc_macro_server : Option < PathBuf > ,
60
69
}
61
70
62
71
impl Config {
@@ -92,44 +101,86 @@ impl Config {
92
101
figment. extract ( ) . context ( "loading configuration" )
93
102
}
94
103
95
- pub fn to_cargo_config ( & self , dir : & AbsPath ) -> CargoConfig {
96
- let sysroot = Sysroot :: discover ( dir, & FxHashMap :: default ( ) ) ;
97
- let sysroot_src = sysroot. src_root ( ) . map ( ToOwned :: to_owned) ;
98
- let sysroot = sysroot
99
- . root ( )
100
- . map ( ToOwned :: to_owned)
101
- . map ( RustLibSource :: Path ) ;
102
-
103
- let target_dir = self
104
- . cargo_target_dir
105
- . clone ( )
106
- . unwrap_or_else ( || self . scratch_dir . join ( "target" ) ) ;
107
- let target_dir = Utf8PathBuf :: from_path_buf ( target_dir) . ok ( ) ;
108
-
109
- let features = if self . cargo_features . is_empty ( ) {
110
- Default :: default ( )
111
- } else if self . cargo_features . contains ( & "*" . to_string ( ) ) {
112
- CargoFeatures :: All
113
- } else {
114
- CargoFeatures :: Selected {
115
- features : self . cargo_features . clone ( ) ,
116
- no_default_features : false ,
104
+ fn sysroot ( & self , dir : & AbsPath ) -> Sysroot {
105
+ let sysroot_input = self . sysroot . as_ref ( ) . map ( |p| join_path_buf ( dir, p) ) ;
106
+ let sysroot_src_input = self . sysroot_src . as_ref ( ) . map ( |p| join_path_buf ( dir, p) ) ;
107
+ match ( sysroot_input, sysroot_src_input) {
108
+ ( None , None ) => Sysroot :: discover ( dir, & self . cargo_extra_env ) ,
109
+ ( Some ( sysroot) , None ) => Sysroot :: discover_sysroot_src_dir ( sysroot) ,
110
+ ( None , Some ( sysroot_src) ) => {
111
+ Sysroot :: discover_with_src_override ( dir, & self . cargo_extra_env , sysroot_src)
117
112
}
118
- } ;
113
+ ( Some ( sysroot) , Some ( sysroot_src) ) => Sysroot :: new ( Some ( sysroot) , Some ( sysroot_src) ) ,
114
+ }
115
+ }
119
116
120
- let target = self . cargo_target . clone ( ) ;
117
+ fn proc_macro_server_choice ( & self , dir : & AbsPath ) -> ProcMacroServerChoice {
118
+ match & self . proc_macro_server {
119
+ Some ( path) => match path. to_str ( ) {
120
+ Some ( "none" ) => ProcMacroServerChoice :: None ,
121
+ Some ( "sysroot" ) => ProcMacroServerChoice :: Sysroot ,
122
+ _ => ProcMacroServerChoice :: Explicit ( join_path_buf ( dir, path) ) ,
123
+ } ,
124
+ None => ProcMacroServerChoice :: Sysroot ,
125
+ }
126
+ }
121
127
122
- let cfg_overrides = to_cfg_overrides ( & self . cargo_cfg_overrides ) ;
128
+ pub fn to_cargo_config ( & self , dir : & AbsPath ) -> ( CargoConfig , LoadCargoConfig ) {
129
+ let sysroot = self . sysroot ( dir) ;
130
+ (
131
+ CargoConfig {
132
+ all_targets : self . cargo_all_targets ,
133
+ sysroot_src : sysroot. src_root ( ) . map ( ToOwned :: to_owned) ,
134
+ rustc_source : self
135
+ . rustc_src
136
+ . as_ref ( )
137
+ . map ( |p| join_path_buf ( dir, p) )
138
+ . or_else ( || sysroot. discover_rustc_src ( ) . map ( AbsPathBuf :: from) )
139
+ . map ( RustLibSource :: Path ) ,
140
+ sysroot : sysroot
141
+ . root ( )
142
+ . map ( ToOwned :: to_owned)
143
+ . map ( RustLibSource :: Path ) ,
123
144
124
- CargoConfig {
125
- sysroot,
126
- sysroot_src,
127
- target_dir,
128
- features,
129
- target,
130
- cfg_overrides,
131
- ..Default :: default ( )
132
- }
145
+ extra_env : self . cargo_extra_env . clone ( ) ,
146
+ extra_args : self . cargo_extra_args . clone ( ) ,
147
+ extra_includes : self
148
+ . extra_includes
149
+ . iter ( )
150
+ . map ( |p| join_path_buf ( dir, p) )
151
+ . collect ( ) ,
152
+ target_dir : Utf8PathBuf :: from_path_buf (
153
+ self . cargo_target_dir
154
+ . clone ( )
155
+ . unwrap_or_else ( || self . scratch_dir . join ( "target" ) ) ,
156
+ )
157
+ . ok ( ) ,
158
+ features : if self . cargo_features . is_empty ( ) {
159
+ Default :: default ( )
160
+ } else if self . cargo_features . contains ( & "*" . to_string ( ) ) {
161
+ CargoFeatures :: All
162
+ } else {
163
+ CargoFeatures :: Selected {
164
+ features : self . cargo_features . clone ( ) ,
165
+ no_default_features : false ,
166
+ }
167
+ } ,
168
+ target : self . cargo_target . clone ( ) ,
169
+ cfg_overrides : to_cfg_overrides ( & self . cargo_cfg_overrides ) ,
170
+ wrap_rustc_in_build_scripts : false ,
171
+ run_build_script_command : if self . build_script_command . is_empty ( ) {
172
+ None
173
+ } else {
174
+ Some ( self . build_script_command . clone ( ) )
175
+ } ,
176
+ ..Default :: default ( )
177
+ } ,
178
+ LoadCargoConfig {
179
+ load_out_dirs_from_check : true ,
180
+ with_proc_macro_server : self . proc_macro_server_choice ( dir) ,
181
+ prefill_caches : false ,
182
+ } ,
183
+ )
133
184
}
134
185
}
135
186
@@ -168,3 +219,10 @@ fn to_cfg_overrides(specs: &Vec<String>) -> CfgOverrides {
168
219
..Default :: default ( )
169
220
}
170
221
}
222
+
223
+ fn join_path_buf ( lhs : & AbsPath , rhs : & Path ) -> AbsPathBuf {
224
+ let Ok ( path) = Utf8PathBuf :: from_path_buf ( rhs. into ( ) ) else {
225
+ panic ! ( "non utf8 input: {}" , rhs. display( ) )
226
+ } ;
227
+ lhs. join ( path)
228
+ }
0 commit comments