@@ -16,59 +16,13 @@ struct TreeSitterParser {
1616    extra_files :  Vec < & ' static  str > , 
1717} 
1818
19- /// Emit linking flags for this library, but specify `+whole-archive`. 
20- /// 
21- /// This should be possible in the cc crate directly after 
22- /// https://github.com/rust-lang/cc-rs/pull/671 
23- fn  emit_whole_archive_link_flags ( build :  & mut  cc:: Build ,  lib_name :  & str ,  is_cpp :  bool )  { 
24-     if  rustc:: is_max_version ( "1.60.0" ) . unwrap_or ( false )  { 
25-         // whole-archive was only stabilised in 1.61, and we don't 
26-         // need it in earlier versions. 
27-         return ; 
28-     } 
29- 
30-     build. cargo_metadata ( false ) ; 
31- 
32-     println ! ( "cargo:rustc-link-lib=static:+whole-archive={}" ,  lib_name) ; 
33-     println ! ( 
34-         "cargo:rustc-link-search=native={}" , 
35-         std:: env:: var( "OUT_DIR" ) . expect( "did not set OUT_DIR" ) 
36-     ) ; 
37- 
38-     if  is_cpp { 
39-         let  cpp_stdlib = if  let  Ok ( stdlib)  = std:: env:: var ( "CXXSTDLIB" )  { 
40-             if  stdlib. is_empty ( )  { 
41-                 None 
42-             }  else  { 
43-                 Some ( stdlib) 
44-             } 
45-         }  else  { 
46-             let  target = std:: env:: var ( "TARGET" ) . expect ( "TARGET environment should be set" ) ; 
47- 
48-             // Equivalent to https://github.com/rust-lang/cc-rs/blob/53fb72c87e5769a299f1886ead831901b9c775d6/src/lib.rs#L2528 
49-             if  target. contains ( "msvc" )  { 
50-                 None 
51-             }  else  if  target. contains ( "apple" )  { 
52-                 Some ( "c++" . to_string ( ) ) 
53-             }  else  if  target. contains ( "freebsd" )  { 
54-                 Some ( "c++" . to_string ( ) ) 
55-             }  else  if  target. contains ( "openbsd" )  { 
56-                 Some ( "c++" . to_string ( ) ) 
57-             }  else  if  target. contains ( "android" )  { 
58-                 Some ( "c++_shared" . to_string ( ) ) 
59-             }  else  { 
60-                 Some ( "stdc++" . to_string ( ) ) 
61-             } 
62-         } ; 
63- 
64-         if  let  Some ( cpp_stdlib)  = cpp_stdlib { 
65-             println ! ( "cargo:rustc-link-lib={}" ,  cpp_stdlib) ; 
66-         } 
67-     } 
68- } 
69- 
7019impl  TreeSitterParser  { 
7120    fn  build ( & self )  { 
21+         // In rustc 1.61+, we need to specify +whole-archive. 
22+         // See https://github.com/rust-lang/rust/blob/1.61.0/RELEASES.md#compatibility-notes 
23+         // and https://github.com/Wilfred/difftastic/issues/339. 
24+         let  rustc_supports_whole_archive = !rustc:: is_max_version ( "1.60.0" ) . unwrap_or ( false ) ; 
25+ 
7226        let  dir = PathBuf :: from ( & self . src_dir ) ; 
7327
7428        let  mut  c_files = vec ! [ "parser.c" ] ; 
@@ -110,7 +64,10 @@ impl TreeSitterParser {
11064                cpp_build. file ( dir. join ( file) ) ; 
11165            } 
11266
113-             emit_whole_archive_link_flags ( & mut  cpp_build,  & format ! ( "{}-cpp" ,  self . name) ,  true ) ; 
67+             if  rustc_supports_whole_archive { 
68+                 cpp_build. link_lib_modifier ( "+whole-archive" ) ; 
69+             } 
70+ 
11471            cpp_build. compile ( & format ! ( "{}-cpp" ,  self . name) ) ; 
11572        } 
11673
@@ -123,7 +80,10 @@ impl TreeSitterParser {
12380            build. file ( dir. join ( file) ) ; 
12481        } 
12582
126-         emit_whole_archive_link_flags ( & mut  build,  self . name ,  false ) ; 
83+         if  rustc_supports_whole_archive { 
84+             build. link_lib_modifier ( "+whole-archive" ) ; 
85+         } 
86+ 
12787        build. compile ( self . name ) ; 
12888    } 
12989} 
0 commit comments