Skip to content

Commit

Permalink
Add build.rs generation back (wayland-* is absurd to compile w/o it)
Browse files Browse the repository at this point in the history
  • Loading branch information
acmcarther committed Dec 4, 2017
1 parent f767930 commit 1258b4d
Show file tree
Hide file tree
Showing 7 changed files with 79 additions and 3 deletions.
1 change: 1 addition & 0 deletions src/bazel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ impl BazelRenderer {
// Configure tera with a bogus template dir: We don't want any runtime template support
let mut renderer = Tera::new("src/not/a/dir/*").unwrap();
renderer.add_raw_templates(vec![
("templates/partials/build_script.template", include_str!("templates/partials/build_script.template")),
("templates/partials/rust_binary.template", include_str!("templates/partials/rust_binary.template")),
("templates/partials/rust_library.template", include_str!("templates/partials/rust_library.template")),
("templates/workspace.BUILD.template", include_str!("templates/workspace.BUILD.template")),
Expand Down
15 changes: 12 additions & 3 deletions src/planning.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,15 +97,24 @@ impl <'a> BuildPlanner<'a> {

let mut targets = try!(identify_targets(&full_name, &package));
targets.sort();
let build_script_target = targets.iter().find(|t| t.kind.deref() == "custom-build").cloned();
let targets_sans_build_script =
targets.into_iter().filter(|t| t.kind.deref() != "custom-build").collect::<Vec<_>>();

let possible_crate_settings =
self.settings.crates
.get(id.name())
.and_then(|c| c.get(&id.version().to_string()));

let should_gen_buildrs =
possible_crate_settings.map(|s| s.gen_buildrs.clone()).unwrap_or(false);
let build_script_target = if should_gen_buildrs {
targets.iter().find(|t| t.kind.deref() == "custom-build").cloned()
} else {
None
};

let targets_sans_build_script =
targets.into_iter().filter(|t| t.kind.deref() != "custom-build").collect::<Vec<_>>();


let additional_deps =
possible_crate_settings.map(|s| s.additional_deps.clone()).unwrap_or(Vec::new());

Expand Down
7 changes: 7 additions & 0 deletions src/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,11 @@ pub struct CrateSettings {
/** Flags to be added to the crate compilation process, in the form "--flag". */
#[serde(default)]
pub additional_flags: Vec<String>,

#[serde(default = "default_gen_buildrs")]
pub gen_buildrs: bool,
}

fn default_gen_buildrs() -> bool {
false
}
3 changes: 3 additions & 0 deletions src/templates/crate.BUILD.template
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ load(
"rust_bench_test",
)
{% set crate_name_sanitized = crate.pkg_name | replace(from="-", to="_") %}
{% if crate.build_script_target %}
{% include "templates/partials/build_script.template" %}
{% endif %}
{% for target in crate.targets %}
{% if target.kind == "lib" %}
{% include "templates/partials/rust_library.template" %}
Expand Down
50 changes: 50 additions & 0 deletions src/templates/partials/build_script.template
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
rust_binary(
name = "{{ crate_name_sanitized }}_build_script",
srcs = glob(["**/*.rs"]),
{% if crate.build_script_target.path %}
crate_root = "{{ crate.build_script_target.path }}",
{% else %}
crate_root = "build.rs",
{% endif %}
data = glob([
"*"
]),
{% if crate.dependencies | length > 0 or crate.build_dependencies | length > 0 %}
deps = [
{% for dependency in crate.build_dependencies %}
"{{path_prefix}}/vendor/{{dependency.name}}-{{dependency.version}}:{{dependency.name | replace(from="-", to="_") }}",
{% endfor %}
],
{% endif %}
rustc_flags = [
"--cap-lints allow",
"--target={{crate.platform_triple}}",
],
{% if crate.features | length > 0 %}
crate_features = [
{% for feature in crate.features %}
"{{feature}}",
{% endfor %}
],
{% endif %}
visibility = ["//visibility:private"],
)

genrule(
name = "{{ crate_name_sanitized }}_build_script_executor",
srcs = glob(["*", "**/*.rs"]),
outs = ["{{ crate_name_sanitized }}_out_dir_outputs.tar.gz"],
tools = [":{{ crate_name_sanitized }}_build_script"],
local = 1,
cmd = "mkdir {{ crate_name_sanitized}}_out_dir_outputs/;"
+ " (export CARGO_MANIFEST_DIR=\"$$PWD/{{ path_prefix | replace(from="//", to="") }}/vendor/{{ crate.pkg_name }}-{{ crate.pkg_version }}\";"
+ " export TARGET='{{ crate.platform_triple }}';"
+ " export RUST_BACKTRACE=1;"
{% for feature in crate.features %}
+ " export CARGO_FEATURE_{{ feature | upper | replace(from="-", to="_") }}=1;"
{% endfor %}
+ " export OUT_DIR=$$PWD/{{ crate_name_sanitized }}_out_dir_outputs;"
+ " export BINARY_PATH=\"$$PWD/$(location :{{ crate_name_sanitized }}_build_script)\";"
+ " export OUT_TAR=$$PWD/$@;"
+ " cd $$(dirname $(location :Cargo.toml)) && $$BINARY_PATH && tar -czf $$OUT_TAR -C $$OUT_DIR .)"
)
3 changes: 3 additions & 0 deletions src/templates/partials/rust_binary.template
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ rust_binary(
"{{flag}}",
{% endfor %}
],
{% if crate.build_script_target %}
out_dir_tar = ":{{ crate.pkg_name | replace(from="-", to="_")}}_build_script_executor",
{% endif %}
crate_features = [
{% for feature in crate.features %}
"{{feature}}",
Expand Down
3 changes: 3 additions & 0 deletions src/templates/partials/rust_library.template
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ rust_library(
"{{flag}}",
{% endfor %}
],
{% if crate.build_script_target %}
out_dir_tar = ":{{ crate.pkg_name | replace(from="-", to="_")}}_build_script_executor",
{% endif %}
crate_features = [
{% for feature in crate.features %}
"{{feature}}",
Expand Down

0 comments on commit 1258b4d

Please sign in to comment.