Skip to content

Commit 053eb8b

Browse files
Merge pull request #255 from onur/default-target
Use default-target to build package
2 parents b4c8f59 + 8030fe1 commit 053eb8b

File tree

2 files changed

+55
-33
lines changed

2 files changed

+55
-33
lines changed

src/docbuilder/chroot_builder.rs

Lines changed: 49 additions & 22 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};
@@ -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 {

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)