Skip to content

Commit 28bdb78

Browse files
feat(apollo_starknet_os_program): add feature for exposing cairo file source map (#6259)
* feat(apollo_starknet_os_program): cairo file list test + fixer * feat(apollo_starknet_os_program): source file dump on build Signed-off-by: Dori Medini <dori@starkware.co> --------- Signed-off-by: Dori Medini <dori@starkware.co>
1 parent 9f38b68 commit 28bdb78

File tree

6 files changed

+95
-1
lines changed

6 files changed

+95
-1
lines changed

.gitignore

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ CLI_TARGET/
1212
*.pdb
1313

1414
*.egg-info
15-
build
15+
/build
1616
dist
1717
target
1818
*/.vscode/*
@@ -30,6 +30,9 @@ __pycache__/
3030
.idea/
3131
**/.venv
3232

33+
# Native blockifier artifacts.
34+
/crates/native_blockifier/build
35+
3336
# Python artifacts.
3437
scripts/__pycache__
3538
monitoring_venv/

Cargo.lock

Lines changed: 4 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/apollo_starknet_os_program/Cargo.toml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,17 @@ edition.workspace = true
55
repository.workspace = true
66
license-file.workspace = true
77
description = "The source (Cairo) code of the Starknet OS."
8+
build = "build/main.rs"
9+
10+
[features]
11+
dump_source_files = []
812

913
[lints]
1014
workspace = true
15+
16+
[dependencies]
17+
serde_json.workspace = true
18+
19+
[build-dependencies]
20+
apollo_infra_utils.workspace = true
21+
serde_json.workspace = true
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
use std::collections::HashMap;
2+
use std::fs::DirEntry;
3+
use std::path::PathBuf;
4+
5+
use apollo_infra_utils::compile_time_cargo_manifest_dir;
6+
7+
/// Utility function to recursively find all cairo files.
8+
fn get_cairo_file_map_recursive(entry: DirEntry) -> HashMap<String, String> {
9+
let file_type = entry.file_type().unwrap();
10+
let path = entry.path();
11+
if file_type.is_dir() {
12+
std::fs::read_dir(path)
13+
.unwrap()
14+
.flat_map(|entry| get_cairo_file_map_recursive(entry.unwrap()).into_iter())
15+
.collect()
16+
} else {
17+
assert!(file_type.is_file());
18+
if path.extension().unwrap_or_default() == "cairo" {
19+
HashMap::from([(
20+
path.to_str().unwrap().to_string(),
21+
std::fs::read_to_string(path).unwrap(),
22+
)])
23+
} else {
24+
HashMap::new()
25+
}
26+
}
27+
}
28+
29+
/// Find all files with a .cairo extension in the `src` directory, insert them into a map and dump
30+
/// the map as JSON to the specified location.
31+
pub fn dump_source_files(dump_to: PathBuf) {
32+
println!("cargo::warning=Dumping OS source files...");
33+
34+
// Recursively fetch all cairo files and contents, and convert the paths to relative paths.
35+
let base_path = PathBuf::from(compile_time_cargo_manifest_dir!()).join("src/cairo");
36+
let base_path_string = base_path.to_str().unwrap();
37+
let map_without_prefixes: HashMap<String, String> = std::fs::read_dir(base_path_string)
38+
.unwrap()
39+
.flat_map(|entry| get_cairo_file_map_recursive(entry.unwrap()))
40+
.map(|(path, contents)| {
41+
assert!(path.starts_with(base_path_string));
42+
let path =
43+
path.strip_prefix(base_path_string).unwrap().strip_prefix("/").unwrap().to_string();
44+
(path, contents)
45+
})
46+
.collect();
47+
48+
// Serialize and dump the map to the specified location.
49+
let serialized = serde_json::to_string(&map_without_prefixes).unwrap();
50+
std::fs::write(&dump_to, serialized)
51+
.unwrap_or_else(|error| panic!("Failed to write to {dump_to:?}: {error:?}."));
52+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#[cfg(feature = "dump_source_files")]
2+
use std::path::PathBuf;
3+
4+
#[cfg(feature = "dump_source_files")]
5+
mod dump_source;
6+
7+
/// Build script for the `apollo_starknet_os_program` crate.
8+
/// Recompiles the OS program if the source files change.
9+
/// Optionally, also exposes all source cairo files in a mapping from file path to contents.
10+
fn main() {
11+
#[cfg(feature = "dump_source_files")]
12+
let out_dir = std::env::var("OUT_DIR").expect("OUT_DIR not set.");
13+
#[cfg(feature = "dump_source_files")]
14+
dump_source::dump_source_files(PathBuf::from(out_dir).join("cairo_files_map.json"));
15+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,10 @@
1+
#[cfg(feature = "dump_source_files")]
2+
use std::collections::HashMap;
3+
#[cfg(feature = "dump_source_files")]
4+
use std::sync::LazyLock;
15

6+
#[cfg(feature = "dump_source_files")]
7+
pub static CAIRO_FILES_MAP: LazyLock<HashMap<String, String>> = LazyLock::new(|| {
8+
serde_json::from_str(include_str!(concat!(env!("OUT_DIR"), "/cairo_files_map.json")))
9+
.unwrap_or_else(|error| panic!("Failed to deserialize cairo_files_map.json: {error:?}."))
10+
});

0 commit comments

Comments
 (0)