Skip to main content

rtz_build/
lib.rs

1//! The build script crate for `rtz`.
2
3#![cfg(not(target_family = "wasm"))]
4#![warn(rustdoc::broken_intra_doc_links, rust_2018_idioms, clippy::all, missing_docs)]
5#![allow(incomplete_features)]
6
7#[cfg(feature = "self-contained")]
8use std::path::PathBuf;
9
10/// Main entry point for build script.
11pub fn main() {
12    #[cfg(feature = "self-contained")]
13    generate_self_contained_bincodes();
14}
15
16/// Returns the assets directory of the crate currently being built.
17///
18/// Resolved via the `CARGO_MANIFEST_DIR` cargo sets for the build script at
19/// runtime, so it lands on `rtz/assets` both in the workspace and in a
20/// registry checkout (where the packaged crate ships the NED bincodes and
21/// generated ones are written alongside them).
22#[cfg(feature = "self-contained")]
23fn assets_dir() -> PathBuf {
24    PathBuf::from(std::env::var("CARGO_MANIFEST_DIR").expect("CARGO_MANIFEST_DIR is set by cargo for build scripts")).join("assets")
25}
26
27#[cfg(feature = "self-contained")]
28fn generate_self_contained_bincodes() {
29    #[cfg(feature = "tz-ned")]
30    generate_ned_tz_bincodes();
31    #[cfg(feature = "tz-osm")]
32    generate_osm_tz_bincodes();
33    #[cfg(feature = "admin-osm")]
34    generate_osm_admin_bincodes();
35}
36
37#[cfg(all(feature = "tz-ned", feature = "self-contained"))]
38fn generate_ned_tz_bincodes() {
39    use rtz_core::geo::{
40        shared::generate_bincodes,
41        tz::ned::{get_geojson_features_from_source, NedTimezone, LOOKUP_BINCODE_DESTINATION_NAME, TIMEZONE_BINCODE_DESTINATION_NAME},
42    };
43
44    let assets = assets_dir();
45    let timezone_bincode_destination = assets.join(TIMEZONE_BINCODE_DESTINATION_NAME);
46    let lookup_bincode_destination = assets.join(LOOKUP_BINCODE_DESTINATION_NAME);
47
48    #[cfg(not(feature = "force-rebuild"))]
49    if timezone_bincode_destination.exists() && lookup_bincode_destination.exists() {
50        return;
51    }
52
53    std::fs::create_dir_all(&assets).unwrap();
54
55    let features = get_geojson_features_from_source();
56    generate_bincodes::<NedTimezone>(features, timezone_bincode_destination, lookup_bincode_destination);
57}
58
59#[cfg(all(feature = "tz-osm", feature = "self-contained"))]
60fn generate_osm_tz_bincodes() {
61    use rtz_core::geo::{
62        shared::generate_bincodes,
63        tz::osm::{get_geojson_features_from_source, OsmTimezone, LOOKUP_BINCODE_DESTINATION_NAME, TIMEZONE_BINCODE_DESTINATION_NAME},
64    };
65
66    let assets = assets_dir();
67    let timezone_bincode_destination = assets.join(TIMEZONE_BINCODE_DESTINATION_NAME);
68    let lookup_bincode_destination = assets.join(LOOKUP_BINCODE_DESTINATION_NAME);
69
70    #[cfg(not(feature = "force-rebuild"))]
71    if timezone_bincode_destination.exists() && lookup_bincode_destination.exists() {
72        return;
73    }
74
75    std::fs::create_dir_all(&assets).unwrap();
76
77    let features = get_geojson_features_from_source();
78    generate_bincodes::<OsmTimezone>(features, timezone_bincode_destination, lookup_bincode_destination);
79}
80
81#[cfg(all(feature = "admin-osm", feature = "self-contained"))]
82fn generate_osm_admin_bincodes() {
83    use rtz_core::geo::{
84        admin::osm::{get_geojson_features_from_source, OsmAdmin, ADMIN_BINCODE_DESTINATION_NAME, LOOKUP_BINCODE_DESTINATION_NAME},
85        shared::generate_bincodes,
86    };
87
88    let assets = assets_dir();
89    let admin_bincode_destination = assets.join(ADMIN_BINCODE_DESTINATION_NAME);
90    let lookup_bincode_destination = assets.join(LOOKUP_BINCODE_DESTINATION_NAME);
91
92    #[cfg(not(feature = "force-rebuild"))]
93    if admin_bincode_destination.exists() && lookup_bincode_destination.exists() {
94        return;
95    }
96
97    std::fs::create_dir_all(&assets).unwrap();
98
99    let features = get_geojson_features_from_source();
100    generate_bincodes::<OsmAdmin>(features, admin_bincode_destination, lookup_bincode_destination);
101}