1
- use crate :: core:: compiler:: { BuildOutput , CompileKind , CompileMode , CompileTarget , CrateType } ;
1
+ use crate :: core:: compiler:: {
2
+ BuildOutput , CompileKind , CompileMode , CompileTarget , Context , CrateType ,
3
+ } ;
2
4
use crate :: core:: { Dependency , Target , TargetKind , Workspace } ;
3
5
use crate :: util:: config:: { Config , StringList , TargetConfig } ;
4
- use crate :: util:: { CargoResult , CargoResultExt , ProcessBuilder , Rustc } ;
6
+ use crate :: util:: { paths , CargoResult , CargoResultExt , ProcessBuilder , Rustc } ;
5
7
use cargo_platform:: { Cfg , CfgExpr } ;
6
8
use serde:: { Deserialize , Serialize } ;
7
9
use std:: cell:: RefCell ;
8
10
use std:: collections:: hash_map:: { Entry , HashMap } ;
9
11
use std:: env;
10
- use std:: fs:: File ;
11
- use std:: path:: { Path , PathBuf } ;
12
+ use std:: path:: PathBuf ;
12
13
use std:: str:: { self , FromStr } ;
13
14
14
15
/// Information about the platform target gleaned from querying rustc.
@@ -754,65 +755,53 @@ impl RustcTargetData {
754
755
/// Structure used to deal with Rustdoc fingerprinting
755
756
#[ derive( Debug , Serialize , Deserialize ) ]
756
757
pub struct RustDocFingerprint {
757
- rustc_verbose_version : String ,
758
+ rustc_vv : String ,
758
759
}
759
760
760
761
impl RustDocFingerprint {
761
- /// Returns the version contained by this `RustDocFingerprint` instance.
762
- fn version ( & self ) -> & str {
763
- self . rustc_verbose_version . as_str ( )
764
- }
765
-
766
- /// Given the `doc` dir, returns the path to the rustdoc fingerprint file.
767
- fn path_to_fingerprint ( doc_dir : & Path ) -> PathBuf {
768
- doc_dir. to_path_buf ( ) . join ( ".rustdoc_fingerprint.json" )
762
+ /// Read the `RustDocFingerprint` info from the fingerprint file.
763
+ fn read < ' a , ' cfg > ( cx : & Context < ' a , ' cfg > ) -> CargoResult < Self > {
764
+ let rustdoc_data = paths:: read (
765
+ & cx. files ( )
766
+ . host_root ( )
767
+ . join ( ".rustdoc_fingerprint" )
768
+ . with_extension ( "json" ) ,
769
+ ) ?;
770
+ serde_json:: from_str ( & rustdoc_data) . map_err ( |e| anyhow:: anyhow!( "{:?}" , e) )
769
771
}
770
772
771
773
/// Write the `RustDocFingerprint` info into the fingerprint file.
772
- pub fn write ( & self , doc_dir : & Path ) -> std:: io:: Result < ( ) > {
773
- crate :: util:: paths:: create_dir_all ( doc_dir)
774
- . map_err ( |e| std:: io:: Error :: new ( std:: io:: ErrorKind :: Other , format ! ( "{:?}" , e) ) ) ?;
775
- let rustdoc_fingerprint_file =
776
- File :: create ( RustDocFingerprint :: path_to_fingerprint ( doc_dir) ) ?;
777
- // We write the actual `Rustc` version to it so that we just need to compile it straight
778
- // since there's no `doc/` folder to remove.
779
- serde_json:: to_writer ( & rustdoc_fingerprint_file, & self )
780
- . map_err ( |e| std:: io:: Error :: new ( std:: io:: ErrorKind :: Other , format ! ( "{:?}" , e) ) )
774
+ fn write < ' a , ' cfg > ( & self , cx : & Context < ' a , ' cfg > ) -> CargoResult < ( ) > {
775
+ paths:: write (
776
+ & cx. files ( )
777
+ . host_root ( )
778
+ . join ( ".rustdoc_fingerprint.json" )
779
+ . with_extension ( "json" ) ,
780
+ serde_json:: to_string ( & self ) ?. as_bytes ( ) ,
781
+ )
781
782
}
782
783
783
784
/// This function checks whether the latest version of `Rustc` used to compile this
784
785
/// `Workspace`'s docs was the same as the one is currently being used in this `cargo doc`
785
- /// call, returning `(Self, bool)` where the `bool` says if the `doc` folder was removed .
786
+ /// call.
786
787
///
787
788
/// In case it's not, it takes care of removig the `doc/` folder as well as overwriting
788
789
/// the rustdoc fingerprint info in order to guarantee that we won't end up with mixed
789
790
/// versions of the `js/html/css` files that `Rustc` autogenerates which do not have
790
791
/// any versioning.
791
- pub fn check_rustdoc_fingerprint (
792
- doc_dir : & Path ,
793
- rustc_verbose_ver : & str ,
794
- ) -> anyhow:: Result < ( Self , bool ) > {
792
+ pub fn check_rustdoc_fingerprint < ' a , ' cfg > ( cx : & Context < ' a , ' cfg > ) -> CargoResult < ( ) > {
795
793
let actual_rustdoc_target_data = RustDocFingerprint {
796
- rustc_verbose_version : rustc_verbose_ver . to_string ( ) ,
794
+ rustc_vv : cx . bcx . rustc ( ) . verbose_version . clone ( ) ,
797
795
} ;
798
-
799
- // Check wether `.rustdoc_fingerprint.json exists
800
- match std:: fs:: read_to_string ( Self :: path_to_fingerprint ( doc_dir) ) {
801
- Ok ( rustdoc_data) => {
802
- let rustdoc_fingerprint: Self = match serde_json:: from_str ( & rustdoc_data) {
803
- Ok ( res) => res,
804
- Err ( _) => {
805
- crate :: util:: paths:: remove_dir_all ( doc_dir) ?;
806
- return Ok ( ( actual_rustdoc_target_data, true ) ) ;
807
- }
808
- } ;
796
+ let doc_dir = cx. files ( ) . host_root ( ) . join ( "doc" ) ;
797
+ // Check wether `.rustdoc_fingerprint.json` exists
798
+ match Self :: read ( cx) {
799
+ Ok ( fingerprint) => {
809
800
// Check if rustc_version matches the one we just used. Otherways,
810
801
// remove the `doc` folder to trigger a re-compilation of the docs.
811
- if rustdoc_fingerprint. version ( ) != actual_rustdoc_target_data. version ( ) {
812
- crate :: util:: paths:: remove_dir_all ( doc_dir) ?;
813
- Ok ( ( actual_rustdoc_target_data, true ) )
814
- } else {
815
- Ok ( ( actual_rustdoc_target_data, false ) )
802
+ if fingerprint. rustc_vv != actual_rustdoc_target_data. rustc_vv {
803
+ paths:: remove_dir_all ( & doc_dir) ?;
804
+ actual_rustdoc_target_data. write ( cx) ?
816
805
}
817
806
}
818
807
// If the file does not exist, then we cannot assume that the docs were compiled
@@ -821,9 +810,10 @@ impl RustDocFingerprint {
821
810
// exists neither, we simply do nothing and continue.
822
811
Err ( _) => {
823
812
// We don't care if this suceeds as explained above.
824
- let _ = crate :: util :: paths:: remove_dir_all ( doc_dir) ;
825
- Ok ( ( actual_rustdoc_target_data, true ) )
813
+ let _ = paths:: remove_dir_all ( doc_dir) ;
814
+ actual_rustdoc_target_data. write ( cx ) ?
826
815
}
827
816
}
817
+ Ok ( ( ) )
828
818
}
829
819
}
0 commit comments