Skip to content

Commit

Permalink
Add build script for mithril-common
Browse files Browse the repository at this point in the history
This computes the map of API version retrieved from openapi.yaml files given their path.
  • Loading branch information
jpraynaud committed Mar 17, 2023
1 parent bcad6b1 commit cb8a7f4
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 1 deletion.
8 changes: 7 additions & 1 deletion mithril-common/Cargo.toml
Expand Up @@ -61,11 +61,17 @@ mithril-stm = { path = "../mithril-stm" }
mithril-stm = { path = "../mithril-stm", default-features = false, features = ["num-integer-backend"] }

[dev-dependencies]
criterion = { version = "0.4.0", features = ["html_reports", "async_tokio"] }
criterion = { version = "0.4.0", features = ["html_reports", "async_tokio"] }
slog-async = "2.7.0"
slog-scope = "4.4.0"
slog-term = "2.9.0"

[build-dependencies]
glob = "0.3"
semver = "1.0"
serde_json = "1.0"
serde_yaml = "0.9.10"

[features]
default = []
portable = ["mithril-stm/portable"] # portable feature avoids SIGILL crashes on CPUs not supporting Intel ADX instruction set when built on CPUs that support it
Expand Down
64 changes: 64 additions & 0 deletions mithril-common/build.rs
@@ -0,0 +1,64 @@
// build.rs

use glob::glob;
use semver::Version;
use std::collections::HashMap;
use std::env;
use std::fs;
use std::path::Path;
use std::path::PathBuf;

type OpenAPIFileName = String;
type OpenAPIVersionRaw = String;

fn read_version_from_open_api_spec_file(spec_file_path: PathBuf) -> OpenAPIVersionRaw {
let yaml_spec = std::fs::read_to_string(spec_file_path).unwrap();
let open_api: serde_yaml::Value = serde_yaml::from_str(&yaml_spec).unwrap();
open_api["info"]["version"].as_str().unwrap().to_owned()
}

fn list_all_open_api_spec_files() -> Vec<PathBuf> {
let mut open_api_spec_files = Vec::new();
for entry in glob("../openapi*.yaml").unwrap() {
open_api_spec_files.push(entry.unwrap())
}

open_api_spec_files
}

fn main() {
let out_dir = env::var_os("OUT_DIR").unwrap();
let dest_path = Path::new(&out_dir).join("open_api.rs");
let open_api_spec_files = list_all_open_api_spec_files();
let open_api_versions = open_api_spec_files
.into_iter()
.map(|path| (path.clone(), read_version_from_open_api_spec_file(path)))
.map(|(path, version_raw)| {
(
path.file_name().unwrap().to_str().unwrap().to_string(),
Version::parse(&version_raw).unwrap().to_string(),
)
})
.collect::<HashMap<OpenAPIFileName, OpenAPIVersionRaw>>();
let open_api_versions_json = format!(
"r#\"{}\"#",
serde_json::to_string(&open_api_versions).unwrap()
);
fs::write(
dest_path,
format!(
r#"
/// Type alias
pub type OpenAPIFileName = String;
pub type OpenAPIVersionRaw = String;
/// Build Open API versions mapping
pub fn get_open_api_versions_mapping() -> HashMap<OpenAPIFileName, OpenAPIVersionRaw> {{
serde_json::from_str({}).unwrap()
}}
"#,
open_api_versions_json
),
)
.unwrap();
}

0 comments on commit cb8a7f4

Please sign in to comment.