@@ -170,18 +170,44 @@ pub fn default_any_file_filter(path: &Path, config: &Config) -> bool {
170
170
171
171
/// The default per-file config used by `run_tests`.
172
172
pub fn default_per_file_config ( config : & mut Config , _path : & Path , file_contents : & [ u8 ] ) {
173
- // Heuristic:
174
- // * if the file contains `#[test]`, automatically pass `--cfg test`.
175
- // * if the file does not contain `fn main()` or `#[start]`, automatically pass `--crate-type=lib`.
176
- // This avoids having to spam `fn main() {}` in almost every test.
173
+ config. program . args . push (
174
+ match crate_type ( file_contents) {
175
+ CrateType :: ProcMacro => "--crate-type=proc-macro" ,
176
+ CrateType :: Test => "--test" ,
177
+ CrateType :: Bin => return ,
178
+ CrateType :: Lib => "--crate-type=lib" ,
179
+ }
180
+ . into ( ) ,
181
+ )
182
+ }
183
+
184
+ /// The kind of crate we're building here. Corresponds to `--crate-type` flags of rustc
185
+ pub enum CrateType {
186
+ /// A proc macro
187
+ ProcMacro ,
188
+ /// A file containing unit tests
189
+ Test ,
190
+ /// A binary file containing a main function or start function
191
+ Bin ,
192
+ /// A library crate
193
+ Lib ,
194
+ }
195
+
196
+ /// Heuristic:
197
+ /// * if the file contains `#[test]`, automatically pass `--cfg test`.
198
+ /// * if the file does not contain `fn main()` or `#[start]`, automatically pass `--crate-type=lib`.
199
+ /// This avoids having to spam `fn main() {}` in almost every test.
200
+ pub fn crate_type ( file_contents : & [ u8 ] ) -> CrateType {
177
201
if file_contents. find ( b"#[proc_macro" ) . is_some ( ) {
178
- config . program . args . push ( "--crate-type=proc-macro" . into ( ) )
202
+ CrateType :: ProcMacro
179
203
} else if file_contents. find ( b"#[test]" ) . is_some ( ) {
180
- config . program . args . push ( "--test" . into ( ) ) ;
204
+ CrateType :: Test
181
205
} else if file_contents. find ( b"fn main()" ) . is_none ( )
182
206
&& file_contents. find ( b"#[start]" ) . is_none ( )
183
207
{
184
- config. program . args . push ( "--crate-type=lib" . into ( ) ) ;
208
+ CrateType :: Lib
209
+ } else {
210
+ CrateType :: Bin
185
211
}
186
212
}
187
213
@@ -583,6 +609,12 @@ fn build_aux(
583
609
584
610
default_per_file_config ( & mut config, aux_file, & file_contents) ;
585
611
612
+ match crate_type ( & file_contents) {
613
+ // Proc macros must be run on the host
614
+ CrateType :: ProcMacro => config. target = config. host . clone ( ) ,
615
+ CrateType :: Test | CrateType :: Bin | CrateType :: Lib => { }
616
+ }
617
+
586
618
// Put aux builds into a separate directory per path so that multiple aux files
587
619
// from different directories (but with the same file name) don't collide.
588
620
let relative = strip_path_prefix ( aux_file. parent ( ) . unwrap ( ) , & config. out_dir ) ;
0 commit comments