1- #![ feature( test) ]
1+ #![ feature( test) ] // compiletest_rs requires this attribute
22
33use compiletest_rs as compiletest;
44use compiletest_rs:: common:: Mode as TestMode ;
@@ -11,51 +11,88 @@ use std::path::{Path, PathBuf};
1111
1212mod cargo;
1313
14- #[ must_use]
15- fn rustc_test_suite ( ) -> Option < PathBuf > {
16- option_env ! ( "RUSTC_TEST_SUITE" ) . map ( PathBuf :: from)
14+ fn host_lib ( ) -> PathBuf {
15+ if let Some ( path) = option_env ! ( "HOST_LIBS" ) {
16+ PathBuf :: from ( path)
17+ } else {
18+ cargo:: CARGO_TARGET_DIR . join ( env ! ( "PROFILE" ) )
19+ }
1720}
1821
19- #[ must_use]
20- fn rustc_lib_path ( ) -> PathBuf {
21- option_env ! ( "RUSTC_LIB_PATH" ) . unwrap ( ) . into ( )
22+ fn clippy_driver_path ( ) -> PathBuf {
23+ if let Some ( path) = option_env ! ( "CLIPPY_DRIVER_PATH" ) {
24+ PathBuf :: from ( path)
25+ } else {
26+ cargo:: TARGET_LIB . join ( "clippy-driver" )
27+ }
28+ }
29+
30+ // When we'll want to use `extern crate ..` for a dependency that is used
31+ // both by the crate and the compiler itself, we can't simply pass -L flags
32+ // as we'll get a duplicate matching versions. Instead, disambiguate with
33+ // `--extern dep=path`.
34+ // See https://github.com/rust-lang/rust-clippy/issues/4015.
35+ //
36+ // FIXME: We cannot use `cargo build --message-format=json` to resolve to dependency files.
37+ // Because it would force-rebuild if the options passed to `build` command is not the same
38+ // as what we manually pass to `cargo` invocation
39+ fn third_party_crates ( ) -> String {
40+ use std:: collections:: hash_map:: Entry :: Vacant ;
41+ use std:: collections:: HashMap ;
42+ static CRATES : & [ & str ] = & [ "serde" , "serde_derive" , "regex" , "clippy_lints" ] ;
43+ let dep_dir = cargo:: TARGET_LIB . join ( "deps" ) ;
44+ let mut crates: HashMap < & str , PathBuf > = HashMap :: with_capacity ( CRATES . len ( ) ) ;
45+ for entry in fs:: read_dir ( dep_dir) . unwrap ( ) {
46+ let path = match entry {
47+ Ok ( entry) => entry. path ( ) ,
48+ _ => continue ,
49+ } ;
50+ if let Some ( name) = path. file_name ( ) . and_then ( OsStr :: to_str) {
51+ for dep in CRATES {
52+ if name. starts_with ( & format ! ( "lib{}-" , dep) ) && name. ends_with ( ".rlib" ) {
53+ crates. entry ( dep) . or_insert ( path) ;
54+ break ;
55+ }
56+ }
57+ }
58+ }
59+
60+ let v: Vec < _ > = crates
61+ . into_iter ( )
62+ . map ( |( dep, path) | format ! ( "--extern {}={}" , dep, path. display( ) ) )
63+ . collect ( ) ;
64+ v. join ( " " )
2265}
2366
2467fn default_config ( ) -> compiletest:: Config {
25- let build_info = cargo:: BuildInfo :: new ( ) ;
2668 let mut config = compiletest:: Config :: default ( ) ;
2769
2870 if let Ok ( name) = env:: var ( "TESTNAME" ) {
2971 config. filter = Some ( name) ;
3072 }
3173
32- if rustc_test_suite ( ) . is_some ( ) {
33- let path = rustc_lib_path ( ) ;
74+ if let Some ( path ) = option_env ! ( "RUSTC_LIB_PATH" ) {
75+ let path = PathBuf :: from ( path ) ;
3476 config. run_lib_path = path. clone ( ) ;
3577 config. compile_lib_path = path;
3678 }
3779
38- let disambiguated: Vec < _ > = cargo:: BuildInfo :: third_party_crates ( )
39- . iter ( )
40- . map ( |( krate, path) | format ! ( "--extern {}={}" , krate, path. display( ) ) )
41- . collect ( ) ;
42-
4380 config. target_rustcflags = Some ( format ! (
4481 "-L {0} -L {1} -Dwarnings -Zui-testing {2}" ,
45- build_info . host_lib( ) . join( "deps" ) . display( ) ,
46- build_info . target_lib ( ) . join( "deps" ) . display( ) ,
47- disambiguated . join ( " " )
82+ host_lib( ) . join( "deps" ) . display( ) ,
83+ cargo :: TARGET_LIB . join( "deps" ) . display( ) ,
84+ third_party_crates ( ) ,
4885 ) ) ;
4986
50- config. build_base = if rustc_test_suite ( ) . is_some ( ) {
51- // we don't need access to the stderr files on travis
87+ config. build_base = if cargo :: is_rustc_test_suite ( ) {
88+ // This make the stderr files go to clippy OUT_DIR on rustc repo build dir
5289 let mut path = PathBuf :: from ( env ! ( "OUT_DIR" ) ) ;
5390 path. push ( "test_build_base" ) ;
5491 path
5592 } else {
56- build_info . host_lib ( ) . join ( "test_build_base" )
93+ host_lib ( ) . join ( "test_build_base" )
5794 } ;
58- config. rustc_path = build_info . clippy_driver_path ( ) ;
95+ config. rustc_path = clippy_driver_path ( ) ;
5996 config
6097}
6198
0 commit comments