Skip to content

Commit ad9a314

Browse files
committed
Use default_target to build package
1 parent 925b30b commit ad9a314

File tree

2 files changed

+45
-30
lines changed

2 files changed

+45
-30
lines changed

src/docbuilder/chroot_builder.rs

Lines changed: 39 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11

22
use super::DocBuilder;
33
use super::crates::crates_from_path;
4+
use super::metadata::Metadata;
45
use utils::{get_package, source_path, copy_dir, copy_doc_dir,
56
update_sources, parse_rustc_version, command_result};
67
use 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 {

src/utils/copy.rs

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,7 @@ pub fn copy_dir<P: AsRef<Path>>(source: P, destination: P) -> Result<()> {
1414
copy_files_and_handle_html(source.as_ref().to_path_buf(),
1515
destination.as_ref().to_path_buf(),
1616
false,
17-
"",
18-
false)
17+
"")
1918
}
2019

2120

@@ -27,23 +26,20 @@ pub fn copy_dir<P: AsRef<Path>>(source: P, destination: P) -> Result<()> {
2726
/// to rename common files (css files, jquery.js, playpen.js, main.js etc.) in a standard rustdoc.
2827
pub fn copy_doc_dir<P: AsRef<Path>>(target: P,
2928
destination: P,
30-
rustc_version: &str,
31-
target_platform: bool)
29+
rustc_version: &str)
3230
-> Result<()> {
3331
let source = PathBuf::from(target.as_ref()).join("doc");
3432
copy_files_and_handle_html(source,
3533
destination.as_ref().to_path_buf(),
3634
true,
37-
rustc_version,
38-
target_platform)
35+
rustc_version)
3936
}
4037

4138

4239
fn copy_files_and_handle_html(source: PathBuf,
4340
destination: PathBuf,
4441
handle_html: bool,
45-
rustc_version: &str,
46-
target: bool)
42+
rustc_version: &str)
4743
-> Result<()> {
4844

4945
// FIXME: handle_html is useless since we started using --resource-suffix
@@ -72,8 +68,7 @@ fn copy_files_and_handle_html(source: PathBuf,
7268
try!(copy_files_and_handle_html(file.path(),
7369
destination_full_path,
7470
handle_html,
75-
&rustc_version,
76-
target));
71+
&rustc_version))
7772
} else if handle_html && dup_regex.is_match(&file.file_name().into_string().unwrap()[..]) {
7873
continue;
7974
} else {
@@ -118,7 +113,7 @@ mod test {
118113
let pkg_dir = format!("rand-{}", pkg.manifest().version());
119114
let target = Path::new(&pkg_dir);
120115
let destination = tempdir::TempDir::new("cratesfyi").unwrap();
121-
let res = copy_doc_dir(target, destination.path(), "UNKNOWN", false);
116+
let res = copy_doc_dir(target, destination.path(), "UNKNOWN");
122117

123118
// remove build and temp dir
124119
fs::remove_dir_all(target).unwrap();

0 commit comments

Comments
 (0)