Skip to main content

rtz_build/
lib.rs

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