11
22use super :: DocBuilder ;
33use super :: crates:: crates_from_path;
4+ use super :: metadata:: Metadata ;
45use utils:: { get_package, source_path, copy_dir, copy_doc_dir,
56 update_sources, parse_rustc_version, command_result} ;
67use db:: { connect_db, add_package_into_database, add_build_into_database, add_path_into_database} ;
@@ -81,15 +82,19 @@ impl DocBuilder {
8182
8283 // get_package (and cargo) is using semver, add '=' in front of version.
8384 let pkg = try!( get_package ( name, Some ( & format ! ( "={}" , version) [ ..] ) ) ) ;
84- let res = self . build_package_in_chroot ( & pkg) ;
85+ let metadata = Metadata :: from_package ( & pkg) ?;
86+ let res = self . build_package_in_chroot ( & pkg, metadata. default_target . clone ( ) ) ;
8587
8688 // copy sources and documentation
8789 let file_list = try!( self . add_sources_into_database ( & conn, & pkg) ) ;
8890 let successfully_targets = if res. have_doc {
89- try!( self . copy_documentation ( & pkg, & res. rustc_version , None ) ) ;
91+ try!( self . copy_documentation ( & pkg,
92+ & res. rustc_version ,
93+ metadata. default_target . as_ref ( ) . map ( String :: as_str) ,
94+ true ) ) ;
9095 let successfully_targets = self . build_package_for_all_targets ( & pkg) ;
9196 for target in & successfully_targets {
92- try!( self . copy_documentation ( & pkg, & res. rustc_version , Some ( target) ) ) ;
97+ try!( self . copy_documentation ( & pkg, & res. rustc_version , Some ( target) , false ) ) ;
9398 }
9499 try!( self . add_documentation_into_database ( & conn, & pkg) ) ;
95100 successfully_targets
@@ -115,18 +120,19 @@ impl DocBuilder {
115120
116121
117122 /// Builds documentation of a package with cratesfyi in chroot environment
118- fn build_package_in_chroot ( & self , package : & Package ) -> ChrootBuilderResult {
123+ fn build_package_in_chroot ( & self , package : & Package , default_target : Option < String > ) -> ChrootBuilderResult {
119124 debug ! ( "Building package in chroot" ) ;
120125 let ( rustc_version, cratesfyi_version) = self . get_versions ( ) ;
121- let cmd = format ! ( "cratesfyi doc {} ={}" ,
126+ let cmd = format ! ( "cratesfyi doc {} ={} {} " ,
122127 package. manifest( ) . name( ) ,
123- package. manifest( ) . version( ) ) ;
128+ package. manifest( ) . version( ) ,
129+ default_target. as_ref( ) . unwrap_or( & "" . to_string( ) ) ) ;
124130 match self . chroot_command ( cmd) {
125131 Ok ( o) => {
126132 ChrootBuilderResult {
127133 output : o,
128134 build_success : true ,
129- have_doc : self . have_documentation ( & package) ,
135+ have_doc : self . have_documentation ( & package, default_target ) ,
130136 have_examples : self . have_examples ( & package) ,
131137 rustc_version : rustc_version,
132138 cratesfyi_version : cratesfyi_version,
@@ -194,22 +200,31 @@ impl DocBuilder {
194200 fn copy_documentation ( & self ,
195201 package : & Package ,
196202 rustc_version : & str ,
197- target : Option < & str > )
203+ target : Option < & str > ,
204+ is_default_target : bool )
198205 -> Result < ( ) > {
199206 let crate_doc_path = PathBuf :: from ( & self . options . chroot_path )
200207 . join ( "home" )
201208 . join ( & self . options . chroot_user )
202209 . join ( "cratesfyi" )
203210 . join ( target. unwrap_or ( "" ) ) ;
204- let destination = PathBuf :: from ( & self . options . destination )
211+ let mut destination = PathBuf :: from ( & self . options . destination )
205212 . join ( format ! ( "{}/{}" ,
206213 package. manifest( ) . name( ) ,
207- package. manifest( ) . version( ) ) )
208- . join ( target. unwrap_or ( "" ) ) ;
214+ package. manifest( ) . version( ) ) ) ;
215+
216+ // only add target name to destination directory when we are copying a non-default target.
217+ // this is allowing us to host documents in the root of the crate documentation directory.
218+ // for example win-api will be available in docs.rs/win-api/$version/win-api/ for it's
219+ // default target: x86_64-pc-windows-msvc. But since it will be built under
220+ // cratesfyi/x86_64-pc-windows-msvc we still need target in this function.
221+ if !is_default_target {
222+ destination. push ( target. unwrap_or ( "" ) ) ;
223+ }
224+
209225 copy_doc_dir ( crate_doc_path,
210226 destination,
211- parse_rustc_version ( rustc_version) ?. trim ( ) ,
212- target. is_some ( ) )
227+ parse_rustc_version ( rustc_version) ?. trim ( ) )
213228 }
214229
215230
@@ -270,13 +285,18 @@ impl DocBuilder {
270285 ///
271286 /// This function is checking first target in targets to see if documentation exists for a
272287 /// crate. Package must be successfully built in chroot environment first.
273- fn have_documentation ( & self , package : & Package ) -> bool {
274- let crate_doc_path = PathBuf :: from ( & self . options . chroot_path )
288+ fn have_documentation ( & self , package : & Package , default_target : Option < String > ) -> bool {
289+ let mut crate_doc_path = PathBuf :: from ( & self . options . chroot_path )
275290 . join ( "home" )
276291 . join ( & self . options . chroot_user )
277- . join ( "cratesfyi" )
278- . join ( "doc" )
279- . join ( package. targets ( ) [ 0 ] . name ( ) . replace ( "-" , "_" ) . to_string ( ) ) ;
292+ . join ( "cratesfyi" ) ;
293+
294+ if let Some ( default_doc_path) = default_target {
295+ crate_doc_path. push ( default_doc_path) ;
296+ }
297+
298+ crate_doc_path. push ( "doc" ) ;
299+ crate_doc_path. push ( package. targets ( ) [ 0 ] . name ( ) . replace ( "-" , "_" ) . to_string ( ) ) ;
280300 crate_doc_path. exists ( )
281301 }
282302
@@ -352,7 +372,7 @@ impl DocBuilder {
352372
353373 // acme-client-0.0.0 is an empty library crate and it will always build
354374 let pkg = try!( get_package ( "acme-client" , Some ( "=0.0.0" ) ) ) ;
355- let res = self . build_package_in_chroot ( & pkg) ;
375+ let res = self . build_package_in_chroot ( & pkg, None ) ;
356376 let rustc_version = parse_rustc_version ( & res. rustc_version ) ?;
357377
358378 if !res. build_success {
0 commit comments