xref: /aosp_15_r20/external/bazelbuild-rules_rust/examples/cargo/build_script.rs (revision d4726bddaa87cc4778e7472feed243fa4b6c267f)
1*d4726bddSHONG Yifan // Copyright 2020 The Bazel Authors. All rights reserved.
2*d4726bddSHONG Yifan //
3*d4726bddSHONG Yifan // Licensed under the Apache License, Version 2.0 (the "License");
4*d4726bddSHONG Yifan // you may not use this file except in compliance with the License.
5*d4726bddSHONG Yifan // You may obtain a copy of the License at
6*d4726bddSHONG Yifan //
7*d4726bddSHONG Yifan //    http://www.apache.org/licenses/LICENSE-2.0
8*d4726bddSHONG Yifan //
9*d4726bddSHONG Yifan // Unless required by applicable law or agreed to in writing, software
10*d4726bddSHONG Yifan // distributed under the License is distributed on an "AS IS" BASIS,
11*d4726bddSHONG Yifan // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12*d4726bddSHONG Yifan // See the License for the specific language governing permissions and
13*d4726bddSHONG Yifan // limitations under the License.
14*d4726bddSHONG Yifan use std::env;
15*d4726bddSHONG Yifan use std::fs::File;
16*d4726bddSHONG Yifan use std::io::prelude::*;
17*d4726bddSHONG Yifan 
main()18*d4726bddSHONG Yifan fn main() {
19*d4726bddSHONG Yifan     let bleh = env::var("CARGO_FEATURE_BLEH").unwrap();
20*d4726bddSHONG Yifan     let some_env = env::var("SOME_ENV").unwrap();
21*d4726bddSHONG Yifan     let out_dir = env::var("OUT_DIR").unwrap();
22*d4726bddSHONG Yifan     let data = std::fs::read("test.txt").unwrap();
23*d4726bddSHONG Yifan     assert!(!bleh.is_empty());
24*d4726bddSHONG Yifan     assert_eq!(some_env, "42");
25*d4726bddSHONG Yifan     println!(
26*d4726bddSHONG Yifan         r#"cargo:rustc-env=FOO=BAR
27*d4726bddSHONG Yifan cargo:rustc-env=BAR=FOO
28*d4726bddSHONG Yifan cargo:rustc-flags=--cfg=blah="bleh"
29*d4726bddSHONG Yifan cargo:rustc-flags=--cfg=data="{}"
30*d4726bddSHONG Yifan cargo:rustc-cfg=foobar"#,
31*d4726bddSHONG Yifan         std::str::from_utf8(&data).unwrap()
32*d4726bddSHONG Yifan     );
33*d4726bddSHONG Yifan     let mut file = File::create(format!("{}/hello.world.txt", out_dir)).unwrap();
34*d4726bddSHONG Yifan     file.write_all(b"Hello, world!").unwrap();
35*d4726bddSHONG Yifan }
36