diff --git a/src/bazel.rs b/src/bazel.rs index a0ba875..72563df 100644 --- a/src/bazel.rs +++ b/src/bazel.rs @@ -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")), diff --git a/src/planning.rs b/src/planning.rs index 0918519..71ab05d 100644 --- a/src/planning.rs +++ b/src/planning.rs @@ -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::>(); 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::>(); + + let additional_deps = possible_crate_settings.map(|s| s.additional_deps.clone()).unwrap_or(Vec::new()); diff --git a/src/settings.rs b/src/settings.rs index 055dc24..5a57a18 100644 --- a/src/settings.rs +++ b/src/settings.rs @@ -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, + + #[serde(default = "default_gen_buildrs")] + pub gen_buildrs: bool, +} + +fn default_gen_buildrs() -> bool { + false } diff --git a/src/templates/crate.BUILD.template b/src/templates/crate.BUILD.template index 87aa823..0ce1f82 100644 --- a/src/templates/crate.BUILD.template +++ b/src/templates/crate.BUILD.template @@ -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" %} diff --git a/src/templates/partials/build_script.template b/src/templates/partials/build_script.template new file mode 100644 index 0000000..12f4621 --- /dev/null +++ b/src/templates/partials/build_script.template @@ -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 .)" +) diff --git a/src/templates/partials/rust_binary.template b/src/templates/partials/rust_binary.template index 9b5553d..3f65fd4 100644 --- a/src/templates/partials/rust_binary.template +++ b/src/templates/partials/rust_binary.template @@ -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}}", diff --git a/src/templates/partials/rust_library.template b/src/templates/partials/rust_library.template index 6bcb50c..8346f70 100644 --- a/src/templates/partials/rust_library.template +++ b/src/templates/partials/rust_library.template @@ -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}}",