load("@bazel_skylib//rules:common_settings.bzl", "bool_flag") load("@bazel_skylib//rules:write_file.bzl", "write_file") load("@rules_rust//rust:defs.bzl", "rust_binary", "rust_library", "rust_test") load(":input_from_different_cfg.bzl", "input_from_different_cfg") write_file( name = "generate_src", out = "src.rs", content = """ #[cfg(not(generated_file_as_root))] pub fn forty_two() -> i32 { 42 } #[cfg(generated_file_as_root)] mod lib_for_src; #[cfg(generated_file_as_root)] pub fn get_forty_two_as_string() -> String { format!("{}", lib_for_src::forty_two()) } """.splitlines(), newline = "unix", ) write_file( name = "generate_src_generated", out = "src/generated.rs", content = """ mod submodule; #[cfg(test)] #[test] fn test_foo() { assert_eq!(submodule::foo(), "foo"); } """.splitlines(), newline = "unix", ) rust_library( name = "use_generated_src", srcs = [ "lib.rs", "submodule/mod.rs", ":src.rs", ], edition = "2018", tags = ["norustfmt"], ) rust_library( name = "use_generated_src_with_crate_root_defined", srcs = [ "lib.rs", "submodule/mod.rs", ":src.rs", ], crate_root = "lib.rs", edition = "2018", tags = ["norustfmt"], ) rust_library( name = "use_generated_src_as_crate_root", srcs = [ "lib.rs", "lib_for_src.rs", ":src.rs", ], crate_root = ":src.rs", edition = "2018", rustc_flags = ["--cfg=generated_file_as_root"], tags = ["norustfmt"], ) rust_library( name = "use_generated_src_with_crate_root_in_subdir", srcs = [ "src/generated.rs", "src/generated/submodule.rs", "src/lib.rs", ], crate_root = "src/lib.rs", edition = "2018", tags = ["norustfmt"], ) rust_test( name = "use_generated_src_with_crate_root_in_subdir_test", crate = "use_generated_src_with_crate_root_in_subdir", edition = "2018", tags = ["norustfmt"], ) # When no lib.rs, main.rs file exists, we try to use the file that carries # the target's name as a crate_root. rust_library( name = "src", srcs = [ "lib_for_src.rs", ":src.rs", ], edition = "2018", rustc_flags = ["--cfg=generated_file_as_root"], tags = ["norustfmt"], ) rust_test( name = "generated_src_test", crate = ":use_generated_src_with_crate_root_defined", edition = "2018", tags = ["norustfmt"], ) rust_binary( name = "print_42", srcs = [ "main.rs", ":src.rs", ], edition = "2018", tags = ["norustfmt"], ) input_from_different_cfg( name = "generated_in_different_cfg", ) filegroup( name = "input_from_different_cfg", srcs = [":generated_in_different_cfg"], output_group = "generated_file", ) rust_library( name = "generated_src_in_different_cfg_as_root", srcs = [":input_from_different_cfg"], edition = "2018", tags = ["norustfmt"], ) rust_test( name = "generated_src_in_different_cfg_as_root_test", crate = "generated_src_in_different_cfg_as_root", edition = "2018", tags = ["norustfmt"], ) rust_library( name = "generated_src_in_different_cfg", srcs = [ "root.rs", ":input_from_different_cfg", ], crate_root = "root.rs", edition = "2018", tags = ["norustfmt"], ) rust_test( name = "generated_src_in_different_cfg_test", crate = "generated_src_in_different_cfg", edition = "2018", tags = ["norustfmt"], ) bool_flag( name = "change_cfg", build_setting_default = False, )