-
Notifications
You must be signed in to change notification settings - Fork 0
Add intermediate representation structures #32
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,70 @@ | ||
| //! Intermediate Representation structures. | ||
| //! | ||
| //! This module defines the backend-agnostic build graph used by Netsuke after | ||
| //! validation. The IR mirrors the conceptual model of Ninja without embedding | ||
| //! any Ninja-specific syntax. | ||
| //! | ||
| //! # Examples | ||
| //! | ||
| //! ``` | ||
| //! use netsuke::ir::{Action, BuildGraph, BuildEdge}; | ||
| //! use netsuke::ast::Recipe; | ||
| //! use std::path::PathBuf; | ||
| //! | ||
| //! let action = Action { | ||
| //! recipe: Recipe::Command { command: "echo hi".into() }, | ||
| //! description: None, | ||
| //! depfile: None, | ||
| //! deps_format: None, | ||
| //! pool: None, | ||
| //! restat: false, | ||
| //! }; | ||
| //! let mut graph = BuildGraph::default(); | ||
| //! graph.actions.insert("a".into(), action); | ||
| //! graph.default_targets.push(PathBuf::from("hello")); | ||
| //! ``` | ||
| // | ||
| use crate::ast::Recipe; | ||
| use std::collections::HashMap; | ||
| use std::path::PathBuf; | ||
|
|
||
| /// The complete, static build graph. | ||
| #[derive(Debug, Default, Clone)] | ||
| pub struct BuildGraph { | ||
| /// All unique actions in the build keyed by a stable hash. | ||
| pub actions: HashMap<String, Action>, | ||
| /// All target files to be built keyed by output path. | ||
| pub targets: HashMap<PathBuf, BuildEdge>, | ||
| /// Targets built when no explicit target is requested. | ||
| pub default_targets: Vec<PathBuf>, | ||
| } | ||
|
|
||
| /// A reusable command analogous to a Ninja rule. | ||
| #[derive(Debug, Clone, PartialEq)] | ||
| pub struct Action { | ||
| pub recipe: Recipe, | ||
| pub description: Option<String>, | ||
| pub depfile: Option<String>, | ||
| pub deps_format: Option<String>, | ||
| pub pool: Option<String>, | ||
| pub restat: bool, | ||
| } | ||
|
|
||
| /// A single build statement connecting inputs to outputs. | ||
| #[derive(Debug, Clone, PartialEq)] | ||
| pub struct BuildEdge { | ||
| /// Identifier of the [`Action`] used for this edge. | ||
| pub action_id: String, | ||
| /// Explicit inputs that trigger a rebuild when changed. | ||
| pub inputs: Vec<PathBuf>, | ||
| /// Outputs explicitly generated by the command. | ||
| pub explicit_outputs: Vec<PathBuf>, | ||
| /// Outputs implicitly generated by the command (Ninja `|`). | ||
| pub implicit_outputs: Vec<PathBuf>, | ||
| /// Order-only dependencies that do not trigger rebuilds (Ninja `||`). | ||
| pub order_only_deps: Vec<PathBuf>, | ||
| /// Always run the command even if the output exists. | ||
| pub phony: bool, | ||
| /// Run the command on every invocation regardless of timestamps. | ||
| pub always: bool, | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5,5 +5,6 @@ | |
|
|
||
| pub mod ast; | ||
| pub mod cli; | ||
| pub mod ir; | ||
| pub mod manifest; | ||
| pub mod runner; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| Feature: BuildGraph | ||
|
|
||
| Scenario: New BuildGraph is empty | ||
| When a new BuildGraph is created | ||
| Then the graph has 0 actions | ||
| And the graph has 0 targets | ||
| And the graph has 0 default targets | ||
|
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,112 @@ | ||
| //! Unit tests for IR structures. | ||
|
|
||
| use netsuke::ast::Recipe; | ||
| use netsuke::ir::{Action, BuildEdge, BuildGraph}; | ||
| use rstest::rstest; | ||
| use std::path::PathBuf; | ||
|
|
||
| #[rstest] | ||
| fn build_graph_default_is_empty() { | ||
| let graph = BuildGraph::default(); | ||
| assert!(graph.actions.is_empty()); | ||
| assert!(graph.targets.is_empty()); | ||
| assert!(graph.default_targets.is_empty()); | ||
| } | ||
|
|
||
| #[rstest] | ||
| fn create_action_and_edge() { | ||
| let action = Action { | ||
| recipe: Recipe::Command { | ||
| command: "echo".into(), | ||
| }, | ||
| description: Some("desc".into()), | ||
| depfile: Some("$out.d".into()), | ||
| deps_format: Some("gcc".into()), | ||
| pool: None, | ||
| restat: false, | ||
| }; | ||
| let edge = BuildEdge { | ||
| action_id: "id".into(), | ||
| inputs: vec![PathBuf::from("in")], | ||
| explicit_outputs: vec![PathBuf::from("out")], | ||
| implicit_outputs: Vec::new(), | ||
| order_only_deps: Vec::new(), | ||
| phony: false, | ||
| always: true, | ||
| }; | ||
| let mut graph = BuildGraph::default(); | ||
| graph.actions.insert("id".into(), action); | ||
| graph.targets.insert(PathBuf::from("out"), edge); | ||
| assert_eq!(graph.actions.len(), 1); | ||
| assert_eq!(graph.targets.len(), 1); | ||
| } | ||
|
|
||
| #[test] | ||
| fn build_graph_duplicate_action_ids() { | ||
| let mut graph = BuildGraph::default(); | ||
| let action1 = Action { | ||
| recipe: Recipe::Command { | ||
| command: "one".into(), | ||
| }, | ||
| description: None, | ||
| depfile: None, | ||
| deps_format: None, | ||
| pool: None, | ||
| restat: false, | ||
| }; | ||
| let action2 = Action { | ||
| recipe: Recipe::Command { | ||
| command: "two".into(), | ||
| }, | ||
| description: None, | ||
| depfile: None, | ||
| deps_format: None, | ||
| pool: None, | ||
| restat: false, | ||
| }; | ||
| let prev = graph.actions.insert("a".into(), action1); | ||
| assert!(prev.is_none()); | ||
| let prev = graph.actions.insert("a".into(), action2); | ||
| assert!(prev.is_some()); | ||
| assert_eq!(graph.actions.len(), 1); | ||
| if let Recipe::Command { command } = &graph.actions.get("a").expect("action").recipe { | ||
| assert_eq!(command, "two"); | ||
| } else { | ||
| panic!("unexpected recipe type"); | ||
| } | ||
| } | ||
|
|
||
| #[test] | ||
| fn build_graph_duplicate_targets() { | ||
| let mut graph = BuildGraph::default(); | ||
| let edge1 = BuildEdge { | ||
| action_id: "a".into(), | ||
| inputs: vec![PathBuf::from("in")], | ||
| explicit_outputs: vec![PathBuf::from("out")], | ||
| implicit_outputs: Vec::new(), | ||
| order_only_deps: Vec::new(), | ||
| phony: false, | ||
| always: false, | ||
| }; | ||
| let edge2 = BuildEdge { | ||
| action_id: "a".into(), | ||
| inputs: vec![PathBuf::from("in")], | ||
| explicit_outputs: vec![PathBuf::from("out")], | ||
| implicit_outputs: Vec::new(), | ||
| order_only_deps: Vec::new(), | ||
| phony: false, | ||
| always: true, | ||
| }; | ||
| let prev = graph.targets.insert(PathBuf::from("out"), edge1); | ||
| assert!(prev.is_none()); | ||
| let prev = graph.targets.insert(PathBuf::from("out"), edge2); | ||
| assert!(prev.is_some()); | ||
| assert_eq!(graph.targets.len(), 1); | ||
| assert!( | ||
| graph | ||
| .targets | ||
| .get(&PathBuf::from("out")) | ||
| .expect("edge") | ||
| .always | ||
| ); | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| //! Step definitions for `BuildGraph` scenarios. | ||
|
|
||
| use crate::CliWorld; | ||
| use cucumber::{then, when}; | ||
| use netsuke::ir::BuildGraph; | ||
|
|
||
| #[when("a new BuildGraph is created")] | ||
| fn create_graph(world: &mut CliWorld) { | ||
| world.build_graph = Some(BuildGraph::default()); | ||
| } | ||
|
|
||
| #[then(expr = "the graph has {int} actions")] | ||
| fn graph_actions(world: &mut CliWorld, count: usize) { | ||
| let g = world.build_graph.as_ref().expect("graph"); | ||
| assert_eq!(g.actions.len(), count); | ||
| } | ||
|
|
||
| #[then(expr = "the graph has {int} targets")] | ||
| fn graph_targets(world: &mut CliWorld, count: usize) { | ||
| let g = world.build_graph.as_ref().expect("graph"); | ||
| assert_eq!(g.targets.len(), count); | ||
| } | ||
|
|
||
| #[then(expr = "the graph has {int} default targets")] | ||
| fn graph_defaults(world: &mut CliWorld, count: usize) { | ||
| let g = world.build_graph.as_ref().expect("graph"); | ||
| assert_eq!(g.default_targets.len(), count); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,2 +1,3 @@ | ||
| mod cli_steps; | ||
| mod ir_steps; | ||
| mod manifest_steps; |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.