@@ -13,14 +13,15 @@ use num_traits::Zero;
13
13
use ra_ap_cfg:: { CfgAtom , CfgDiff } ;
14
14
use ra_ap_ide_db:: FxHashMap ;
15
15
use ra_ap_intern:: Symbol ;
16
+ use ra_ap_load_cargo:: { LoadCargoConfig , ProcMacroServerChoice } ;
16
17
use ra_ap_paths:: { AbsPath , AbsPathBuf , Utf8PathBuf } ;
17
18
use ra_ap_project_model:: { CargoConfig , CargoFeatures , CfgOverrides , RustLibSource , Sysroot } ;
18
19
use rust_extractor_macros:: extractor_cli_config;
19
20
use serde:: { Deserialize , Serialize } ;
20
21
use std:: collections:: HashSet ;
21
22
use std:: fmt:: Debug ;
22
23
use std:: ops:: Not ;
23
- use std:: path:: PathBuf ;
24
+ use std:: path:: { Path , PathBuf } ;
24
25
25
26
#[ derive( Debug , PartialEq , Eq , Default , Serialize , Deserialize , Clone , Copy , clap:: ValueEnum ) ]
26
27
#[ serde( rename_all = "lowercase" ) ]
@@ -64,6 +65,7 @@ pub struct Config {
64
65
pub rustc_src : Option < PathBuf > ,
65
66
pub build_script_command : Vec < String > ,
66
67
pub extra_includes : Vec < PathBuf > ,
68
+ pub proc_macro_server : Option < PathBuf > ,
67
69
}
68
70
69
71
impl Config {
@@ -93,70 +95,87 @@ impl Config {
93
95
figment. extract ( ) . context ( "loading configuration" )
94
96
}
95
97
96
- pub fn to_cargo_config ( & self , dir : & AbsPath ) -> CargoConfig {
97
- let path_buf_to_abs_path_buf = |path : & PathBuf | {
98
- let Ok ( path) = Utf8PathBuf :: from_path_buf ( path. clone ( ) ) else {
99
- panic ! ( "non utf8 input: {}" , path. display( ) )
100
- } ;
101
- dir. join ( path)
102
- } ;
103
- let sysroot_input = self . sysroot . as_ref ( ) . map ( path_buf_to_abs_path_buf) ;
104
- let sysroot_src_input = self . sysroot_src . as_ref ( ) . map ( path_buf_to_abs_path_buf) ;
105
- let rustc_src_input = self . rustc_src . as_ref ( ) . map ( path_buf_to_abs_path_buf) ;
106
-
107
- let sysroot = match ( sysroot_input, sysroot_src_input) {
98
+ fn sysroot ( & self , dir : & AbsPath ) -> Sysroot {
99
+ let sysroot_input = self . sysroot . as_ref ( ) . map ( |p| join_path_buf ( dir, p) ) ;
100
+ let sysroot_src_input = self . sysroot_src . as_ref ( ) . map ( |p| join_path_buf ( dir, p) ) ;
101
+ match ( sysroot_input, sysroot_src_input) {
108
102
( None , None ) => Sysroot :: discover ( dir, & self . cargo_extra_env ) ,
109
103
( Some ( sysroot) , None ) => Sysroot :: discover_sysroot_src_dir ( sysroot) ,
110
104
( None , Some ( sysroot_src) ) => {
111
105
Sysroot :: discover_with_src_override ( dir, & self . cargo_extra_env , sysroot_src)
112
106
}
113
107
( Some ( sysroot) , Some ( sysroot_src) ) => Sysroot :: new ( Some ( sysroot) , Some ( sysroot_src) ) ,
114
- } ;
115
- CargoConfig {
116
- all_targets : self . cargo_all_targets ,
117
- sysroot_src : sysroot. src_root ( ) . map ( ToOwned :: to_owned) ,
118
- rustc_source : rustc_src_input
119
- . or_else ( || sysroot. discover_rustc_src ( ) . map ( AbsPathBuf :: from) )
120
- . map ( RustLibSource :: Path ) ,
121
- sysroot : sysroot
122
- . root ( )
123
- . map ( ToOwned :: to_owned)
124
- . map ( RustLibSource :: Path ) ,
108
+ }
109
+ }
125
110
126
- extra_env : self . cargo_extra_env . clone ( ) ,
127
- extra_args : self . cargo_extra_args . clone ( ) ,
128
- extra_includes : self
129
- . extra_includes
130
- . iter ( )
131
- . map ( path_buf_to_abs_path_buf)
132
- . collect ( ) ,
133
- target_dir : Utf8PathBuf :: from_path_buf (
134
- self . cargo_target_dir
135
- . clone ( )
136
- . unwrap_or_else ( || self . scratch_dir . join ( "target" ) ) ,
137
- )
138
- . ok ( ) ,
139
- features : if self . cargo_features . is_empty ( ) {
140
- Default :: default ( )
141
- } else if self . cargo_features . contains ( & "*" . to_string ( ) ) {
142
- CargoFeatures :: All
143
- } else {
144
- CargoFeatures :: Selected {
145
- features : self . cargo_features . clone ( ) ,
146
- no_default_features : false ,
147
- }
111
+ fn proc_macro_server_choice ( & self , dir : & AbsPath ) -> ProcMacroServerChoice {
112
+ match & self . proc_macro_server {
113
+ Some ( path) => match path. to_str ( ) {
114
+ Some ( "none" ) => ProcMacroServerChoice :: None ,
115
+ Some ( "sysroot" ) => ProcMacroServerChoice :: Sysroot ,
116
+ _ => ProcMacroServerChoice :: Explicit ( join_path_buf ( dir, path) ) ,
148
117
} ,
149
- target : self . cargo_target . clone ( ) ,
150
- cfg_overrides : to_cfg_overrides ( & self . cargo_cfg_overrides ) ,
151
- wrap_rustc_in_build_scripts : false ,
152
- run_build_script_command : if self . build_script_command . is_empty ( ) {
153
- None
154
- } else {
155
- Some ( self . build_script_command . clone ( ) )
156
- } ,
157
- ..Default :: default ( )
118
+ None => ProcMacroServerChoice :: Sysroot ,
158
119
}
159
120
}
121
+
122
+ pub fn to_cargo_config ( & self , dir : & AbsPath ) -> ( CargoConfig , LoadCargoConfig ) {
123
+ let sysroot = self . sysroot ( dir) ;
124
+ (
125
+ CargoConfig {
126
+ all_targets : self . cargo_all_targets ,
127
+ sysroot_src : sysroot. src_root ( ) . map ( ToOwned :: to_owned) ,
128
+ rustc_source : self
129
+ . rustc_src
130
+ . as_ref ( )
131
+ . map ( |p| join_path_buf ( dir, p) )
132
+ . or_else ( || sysroot. discover_rustc_src ( ) . map ( AbsPathBuf :: from) )
133
+ . map ( RustLibSource :: Path ) ,
134
+ sysroot : sysroot
135
+ . root ( )
136
+ . map ( ToOwned :: to_owned)
137
+ . map ( RustLibSource :: Path ) ,
138
+
139
+ extra_env : self . cargo_extra_env . clone ( ) ,
140
+ extra_args : self . cargo_extra_args . clone ( ) ,
141
+ extra_includes : self
142
+ . extra_includes
143
+ . iter ( )
144
+ . map ( |p| join_path_buf ( dir, p) )
145
+ . collect ( ) ,
146
+ target_dir : Utf8PathBuf :: from_path_buf (
147
+ self . cargo_target_dir
148
+ . clone ( )
149
+ . unwrap_or_else ( || self . scratch_dir . join ( "target" ) ) ,
150
+ )
151
+ . ok ( ) ,
152
+ features : if self . cargo_features . is_empty ( ) {
153
+ Default :: default ( )
154
+ } else if self . cargo_features . contains ( & "*" . to_string ( ) ) {
155
+ CargoFeatures :: All
156
+ } else {
157
+ CargoFeatures :: Selected {
158
+ features : self . cargo_features . clone ( ) ,
159
+ no_default_features : false ,
160
+ }
161
+ } ,
162
+ target : self . cargo_target . clone ( ) ,
163
+ cfg_overrides : to_cfg_overrides ( & self . cargo_cfg_overrides ) ,
164
+ wrap_rustc_in_build_scripts : false ,
165
+ run_build_script_command : if self . build_script_command . is_empty ( ) {
166
+ None
167
+ } else {
168
+ Some ( self . build_script_command . clone ( ) )
169
+ } ,
170
+ ..Default :: default ( )
171
+ } ,
172
+ LoadCargoConfig {
173
+ load_out_dirs_from_check : true ,
174
+ with_proc_macro_server : self . proc_macro_server_choice ( dir) ,
175
+ prefill_caches : false ,
176
+ } ,
177
+ )
178
+ }
160
179
}
161
180
162
181
fn to_cfg_override ( spec : & str ) -> CfgAtom {
@@ -194,3 +213,10 @@ fn to_cfg_overrides(specs: &Vec<String>) -> CfgOverrides {
194
213
..Default :: default ( )
195
214
}
196
215
}
216
+
217
+ fn join_path_buf ( lhs : & AbsPath , rhs : & Path ) -> AbsPathBuf {
218
+ let Ok ( path) = Utf8PathBuf :: from_path_buf ( rhs. into ( ) ) else {
219
+ panic ! ( "non utf8 input: {}" , rhs. display( ) )
220
+ } ;
221
+ lhs. join ( path)
222
+ }
0 commit comments