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} ;
@@ -79,15 +80,19 @@ impl DocBuilder {
7980
8081 // get_package (and cargo) is using semver, add '=' in front of version.
8182 let pkg = try!( get_package ( name, Some ( & format ! ( "={}" , version) [ ..] ) ) ) ;
82- let res = self . build_package_in_chroot ( & pkg) ;
83+ let metadata = Metadata :: from_package ( & pkg) ?;
84+ let res = self . build_package_in_chroot ( & pkg, metadata. default_target . clone ( ) ) ;
8385
8486 // copy sources and documentation
8587 let file_list = try!( self . add_sources_into_database ( & conn, & pkg) ) ;
8688 let successfully_targets = if res. have_doc {
87- try!( self . copy_documentation ( & pkg, & res. rustc_version , None ) ) ;
89+ try!( self . copy_documentation ( & pkg,
90+ & res. rustc_version ,
91+ metadata. default_target . as_ref ( ) . map ( String :: as_str) ,
92+ true ) ) ;
8893 let successfully_targets = self . build_package_for_all_targets ( & pkg) ;
8994 for target in & successfully_targets {
90- try!( self . copy_documentation ( & pkg, & res. rustc_version , Some ( target) ) ) ;
95+ try!( self . copy_documentation ( & pkg, & res. rustc_version , Some ( target) , false ) ) ;
9196 }
9297 try!( self . add_documentation_into_database ( & conn, & pkg) ) ;
9398 successfully_targets
@@ -113,18 +118,19 @@ impl DocBuilder {
113118
114119
115120 /// Builds documentation of a package with cratesfyi in chroot environment
116- fn build_package_in_chroot ( & self , package : & Package ) -> ChrootBuilderResult {
121+ fn build_package_in_chroot ( & self , package : & Package , default_target : Option < String > ) -> ChrootBuilderResult {
117122 debug ! ( "Building package in chroot" ) ;
118123 let ( rustc_version, cratesfyi_version) = self . get_versions ( ) ;
119- let cmd = format ! ( "cratesfyi doc {} ={}" ,
124+ let cmd = format ! ( "cratesfyi doc {} ={} {} " ,
120125 package. manifest( ) . name( ) ,
121- package. manifest( ) . version( ) ) ;
126+ package. manifest( ) . version( ) ,
127+ default_target. as_ref( ) . unwrap_or( & "" . to_string( ) ) ) ;
122128 match self . chroot_command ( cmd) {
123129 Ok ( o) => {
124130 ChrootBuilderResult {
125131 output : o,
126132 build_success : true ,
127- have_doc : self . have_documentation ( & package) ,
133+ have_doc : self . have_documentation ( & package, default_target ) ,
128134 have_examples : self . have_examples ( & package) ,
129135 rustc_version : rustc_version,
130136 cratesfyi_version : cratesfyi_version,
@@ -192,22 +198,38 @@ impl DocBuilder {
192198 fn copy_documentation ( & self ,
193199 package : & Package ,
194200 rustc_version : & str ,
195- target : Option < & str > )
201+ target : Option < & str > ,
202+ is_default_target : bool )
196203 -> Result < ( ) > {
197- let crate_doc_path = PathBuf :: from ( & self . options . chroot_path )
204+ let mut crate_doc_path = PathBuf :: from ( & self . options . chroot_path )
198205 . join ( "home" )
199206 . join ( & self . options . chroot_user )
200- . join ( "cratesfyi" )
201- . join ( target. unwrap_or ( "" ) ) ;
202- let destination = PathBuf :: from ( & self . options . destination )
207+ . join ( "cratesfyi" ) ;
208+
209+ // docs are available in cratesfyi/$TARGET when target is being used
210+ if let Some ( target) = target {
211+ crate_doc_path. push ( target) ;
212+ }
213+
214+ let mut destination = PathBuf :: from ( & self . options . destination )
203215 . join ( format ! ( "{}/{}" ,
204216 package. manifest( ) . name( ) ,
205- package. manifest( ) . version( ) ) )
206- . join ( target. unwrap_or ( "" ) ) ;
217+ package. manifest( ) . version( ) ) ) ;
218+
219+ // only add target name to destination directory when we are copying a non-default target.
220+ // this is allowing us to host documents in the root of the crate documentation directory.
221+ // for example winapi will be available in docs.rs/winapi/$version/winapi/ for it's
222+ // default target: x86_64-pc-windows-msvc. But since it will be built under
223+ // cratesfyi/x86_64-pc-windows-msvc we still need target in this function.
224+ if !is_default_target {
225+ if let Some ( target) = target {
226+ destination. push ( target) ;
227+ }
228+ }
229+
207230 copy_doc_dir ( crate_doc_path,
208231 destination,
209- parse_rustc_version ( rustc_version) ?. trim ( ) ,
210- target. is_some ( ) )
232+ parse_rustc_version ( rustc_version) ?. trim ( ) )
211233 }
212234
213235
@@ -268,13 +290,18 @@ impl DocBuilder {
268290 ///
269291 /// This function is checking first target in targets to see if documentation exists for a
270292 /// crate. Package must be successfully built in chroot environment first.
271- fn have_documentation ( & self , package : & Package ) -> bool {
272- let crate_doc_path = PathBuf :: from ( & self . options . chroot_path )
293+ fn have_documentation ( & self , package : & Package , default_target : Option < String > ) -> bool {
294+ let mut crate_doc_path = PathBuf :: from ( & self . options . chroot_path )
273295 . join ( "home" )
274296 . join ( & self . options . chroot_user )
275- . join ( "cratesfyi" )
276- . join ( "doc" )
277- . join ( package. targets ( ) [ 0 ] . name ( ) . replace ( "-" , "_" ) . to_string ( ) ) ;
297+ . join ( "cratesfyi" ) ;
298+
299+ if let Some ( default_doc_path) = default_target {
300+ crate_doc_path. push ( default_doc_path) ;
301+ }
302+
303+ crate_doc_path. push ( "doc" ) ;
304+ crate_doc_path. push ( package. targets ( ) [ 0 ] . name ( ) . replace ( "-" , "_" ) . to_string ( ) ) ;
278305 crate_doc_path. exists ( )
279306 }
280307
@@ -350,7 +377,7 @@ impl DocBuilder {
350377
351378 // acme-client-0.0.0 is an empty library crate and it will always build
352379 let pkg = try!( get_package ( "acme-client" , Some ( "=0.0.0" ) ) ) ;
353- let res = self . build_package_in_chroot ( & pkg) ;
380+ let res = self . build_package_in_chroot ( & pkg, None ) ;
354381 let rustc_version = parse_rustc_version ( & res. rustc_version ) ?;
355382
356383 if !res. build_success {
0 commit comments