|
14 | 14 | // KIND, either express or implied. See the License for the |
15 | 15 | // specific language governing permissions and limitations |
16 | 16 | // under the License. |
17 | | -use std::{env, path::PathBuf}; |
18 | | - |
19 | | -// Since relative path differs between build.rs and a file under `src/`, use the |
20 | | -// absolute path. |
21 | | -fn get_absolute_path(path: String) -> PathBuf { |
22 | | - std::path::absolute(PathBuf::from(path)).expect("Failed to get absolute path") |
23 | | -} |
24 | | - |
25 | | -fn configure_bindings_path(prebuilt_bindings_path: String) -> (PathBuf, bool) { |
26 | | - // If SEDONA_PROJ_BINDINGS_OUTPUT_PATH is set, honor the explicit output |
27 | | - // path to regenerate bindings. |
28 | | - if let Ok(output_path) = env::var("SEDONA_PROJ_BINDINGS_OUTPUT_PATH") { |
29 | | - let output_path = get_absolute_path(output_path); |
30 | | - if let Some(output_dir) = output_path.parent() { |
31 | | - std::fs::create_dir_all(output_dir).expect("Failed to create parent dirs"); |
32 | | - } |
33 | | - return (output_path, true); |
34 | | - } |
35 | | - |
36 | | - // If a prebuilt bindings exists, use it and skip bindgen. |
37 | | - let prebuilt_bindings_path = get_absolute_path(prebuilt_bindings_path); |
38 | | - if prebuilt_bindings_path.exists() { |
39 | | - return (prebuilt_bindings_path, false); |
40 | | - } |
41 | | - |
42 | | - let output_dir = env::var("OUT_DIR").unwrap(); |
43 | | - let output_path = PathBuf::from(output_dir).join("bindings.rs"); |
44 | | - |
45 | | - (output_path, true) |
46 | | -} |
47 | 17 |
|
48 | 18 | fn main() { |
49 | 19 | println!("cargo:rerun-if-changed=src/proj_dyn.c"); |
50 | | - println!("cargo:rerun-if-env-changed=SEDONA_PROJ_BINDINGS_OUTPUT_PATH"); |
51 | 20 |
|
52 | 21 | cc::Build::new().file("src/proj_dyn.c").compile("proj_dyn"); |
53 | | - |
54 | | - let target_triple = std::env::var("TARGET").unwrap(); |
55 | | - let prebuilt_bindings_path = format!("src/bindings/{target_triple}.rs"); |
56 | | - |
57 | | - let (bindings_path, generate_bindings) = configure_bindings_path(prebuilt_bindings_path); |
58 | | - |
59 | | - println!( |
60 | | - "cargo::rustc-env=BINDINGS_PATH={}", |
61 | | - bindings_path.to_string_lossy() |
62 | | - ); |
63 | | - |
64 | | - if !generate_bindings { |
65 | | - return; |
66 | | - } |
67 | | - |
68 | | - let bindings = bindgen::Builder::default() |
69 | | - .header("src/proj_dyn.h") |
70 | | - .generate_comments(false) |
71 | | - .parse_callbacks(Box::new(bindgen::CargoCallbacks::new())) |
72 | | - .generate() |
73 | | - .expect("Unable to generate bindings"); |
74 | | - |
75 | | - // Write the bindings to the $OUT_DIR/bindings.rs file. |
76 | | - bindings |
77 | | - .write_to_file(&bindings_path) |
78 | | - .expect("Couldn't write bindings!"); |
79 | 22 | } |
0 commit comments