fork/rules_rust
git clone https://git.julia.blue/fork/rules_rust
cfcaf21d
0.11.0
1load ( "@bazel_skylib//rules:common_settings.bzl" , "bool_flag" ) 2load ( "@bazel_skylib//rules:write_file.bzl" , "write_file" ) 3load ( "@rules_rust//rust:defs.bzl" , "rust_binary" , "rust_library" , "rust_test" ) 4load ( ":input_from_different_cfg.bzl" , "input_from_different_cfg" ) 5 6write_file ( 7name = "generate_src" , 8out = "src.rs" , 9content = """ 10#[cfg(not(generated_file_as_root))] 11pub fn forty_two() -> i32 { 42 } 12 13#[cfg(generated_file_as_root)] 14mod lib_for_src; 15 16#[cfg(generated_file_as_root)] 17pub fn get_forty_two_as_string() -> String { 18format!("{}", lib_for_src::forty_two()) 19} 20""" . splitlines (), 21newline = "unix" , 22) 23 24write_file ( 25name = "generate_src_generated" , 26out = "src/generated.rs" , 27content = """ 28mod submodule; 29 30#[cfg(test)] 31#[test] 32fn test_foo() { 33assert_eq!(submodule::foo(), "foo"); 34} 35""" . splitlines (), 36newline = "unix" , 37) 38 39rust_library ( 40name = "use_generated_src" , 41srcs = [ 42"lib.rs" , 43"submodule/mod.rs" , 44":src.rs" , 45], 46edition = "2018" , 47tags = [ "norustfmt" ], 48) 49 50rust_library ( 51name = "use_generated_src_with_crate_root_defined" , 52srcs = [ 53"lib.rs" , 54"submodule/mod.rs" , 55":src.rs" , 56], 57crate_root = "lib.rs" , 58edition = "2018" , 59tags = [ "norustfmt" ], 60) 61 62rust_library ( 63name = "use_generated_src_as_crate_root" , 64srcs = [ 65"lib.rs" , 66"lib_for_src.rs" , 67":src.rs" , 68], 69crate_root = ":src.rs" , 70edition = "2018" , 71rustc_flags = [ "--cfg=generated_file_as_root" ], 72tags = [ "norustfmt" ], 73) 74 75rust_library ( 76name = "use_generated_src_with_crate_root_in_subdir" , 77srcs = [ 78"src/generated.rs" , 79"src/generated/submodule.rs" , 80"src/lib.rs" , 81], 82crate_root = "src/lib.rs" , 83edition = "2018" , 84tags = [ "norustfmt" ], 85) 86 87rust_test ( 88name = "use_generated_src_with_crate_root_in_subdir_test" , 89crate = "use_generated_src_with_crate_root_in_subdir" , 90edition = "2018" , 91tags = [ "norustfmt" ], 92) 93 94# When no lib.rs, main.rs file exists, we try to use the file that carries 95# the target's name as a crate_root. 96rust_library ( 97name = "src" , 98srcs = [ 99"lib_for_src.rs" , 100":src.rs" , 101], 102edition = "2018" , 103rustc_flags = [ "--cfg=generated_file_as_root" ], 104tags = [ "norustfmt" ], 105) 106 107rust_test ( 108name = "generated_src_test" , 109crate = ":use_generated_src_with_crate_root_defined" , 110edition = "2018" , 111tags = [ "norustfmt" ], 112) 113 114rust_binary ( 115name = "print_42" , 116srcs = [ 117"main.rs" , 118":src.rs" , 119], 120edition = "2018" , 121tags = [ "norustfmt" ], 122) 123 124input_from_different_cfg ( 125name = "generated_in_different_cfg" , 126) 127 128filegroup ( 129name = "input_from_different_cfg" , 130srcs = [ ":generated_in_different_cfg" ], 131output_group = "generated_file" , 132) 133 134rust_library ( 135name = "generated_src_in_different_cfg_as_root" , 136srcs = [ ":input_from_different_cfg" ], 137edition = "2018" , 138tags = [ "norustfmt" ], 139) 140 141rust_test ( 142name = "generated_src_in_different_cfg_as_root_test" , 143crate = "generated_src_in_different_cfg_as_root" , 144edition = "2018" , 145tags = [ "norustfmt" ], 146) 147 148rust_library ( 149name = "generated_src_in_different_cfg" , 150srcs = [ 151"root.rs" , 152":input_from_different_cfg" , 153], 154crate_root = "root.rs" , 155edition = "2018" , 156tags = [ "norustfmt" ], 157) 158 159rust_test ( 160name = "generated_src_in_different_cfg_test" , 161crate = "generated_src_in_different_cfg" , 162edition = "2018" , 163tags = [ "norustfmt" ], 164) 165 166bool_flag ( 167name = "change_cfg" , 168build_setting_default = False , 169)