99
1010pub mod config;
1111pub mod error_code;
12+ pub mod parser;
1213pub mod rule;
14+ pub mod signal_handler;
1315pub mod special_target;
1416
15- use std:: { collections:: HashSet , fs, time:: SystemTime } ;
17+ use std:: {
18+ collections:: HashSet ,
19+ fs:: { self } ,
20+ time:: SystemTime ,
21+ } ;
1622
17- use makefile_lossless :: { Makefile , VariableDefinition } ;
23+ use parser :: { Makefile , VariableDefinition } ;
1824
25+ use crate :: special_target:: InferenceTarget ;
1926use config:: Config ;
2027use error_code:: ErrorCode :: { self , * } ;
2128use rule:: { prerequisite:: Prerequisite , target:: Target , Rule } ;
@@ -29,12 +36,11 @@ const DEFAULT_SHELL: &str = "/bin/sh";
2936
3037/// Represents the make utility with its data and configuration.
3138///
32- /// The only way to create a ` Make` is from a ` Makefile` and a ` Config` .
39+ /// The only way to create a Make is from a Makefile and a Config.
3340pub struct Make {
3441 macros : Vec < VariableDefinition > ,
3542 rules : Vec < Rule > ,
3643 default_rule : Option < Rule > , // .DEFAULT
37-
3844 pub config : Config ,
3945}
4046
@@ -43,8 +49,8 @@ impl Make {
4349 ///
4450 /// # Returns
4551 ///
46- /// - ` Some(rule)` if a rule with the target exists.
47- /// - ` None` if no rule with the target exists.
52+ /// - Some(rule) if a rule with the target exists.
53+ /// - None if no rule with the target exists.
4854 fn rule_by_target_name ( & self , target : impl AsRef < str > ) -> Option < & Rule > {
4955 self . rules
5056 . iter ( )
@@ -59,9 +65,9 @@ impl Make {
5965 /// Builds the target with the given name.
6066 ///
6167 /// # Returns
62- /// - ` Ok(true)` if the target was built.
63- /// - ` Ok(false)` if the target was already up to date.
64- /// - ` Err(_)` if any errors occur.
68+ /// - Ok(true) if the target was built.
69+ /// - Ok(false) if the target was already up to date.
70+ /// - Err(_) if any errors occur.
6571 pub fn build_target ( & self , name : impl AsRef < str > ) -> Result < bool , ErrorCode > {
6672 let rule = match self . rule_by_target_name ( & name) {
6773 Some ( rule) => rule,
@@ -82,9 +88,9 @@ impl Make {
8288 /// Runs the given rule.
8389 ///
8490 /// # Returns
85- /// - Ok(` true` ) if the rule was run.
86- /// - Ok(` false` ) if the rule was already up to date.
87- /// - ` Err(_)` if any errors occur.
91+ /// - Ok(true) if the rule was run.
92+ /// - Ok(false) if the rule was already up to date.
93+ /// - Err(_) if any errors occur.
8894 fn run_rule_with_prerequisites ( & self , rule : & Rule , target : & Target ) -> Result < bool , ErrorCode > {
8995 if self . are_prerequisites_recursive ( target) {
9096 return Err ( RecursivePrerequisite {
@@ -93,15 +99,19 @@ impl Make {
9399 }
94100
95101 let newer_prerequisites = self . get_newer_prerequisites ( target) ;
96- if newer_prerequisites. is_empty ( ) && get_modified_time ( target) . is_some ( ) {
102+ let mut up_to_date = newer_prerequisites. is_empty ( ) && get_modified_time ( target) . is_some ( ) ;
103+ if rule. config . phony {
104+ up_to_date = false ;
105+ }
106+
107+ if up_to_date {
97108 return Ok ( false ) ;
98109 }
99110
100- for prerequisite in newer_prerequisites {
111+ for prerequisite in & newer_prerequisites {
101112 self . build_target ( prerequisite) ?;
102113 }
103-
104- rule. run ( & self . config , & self . macros , target) ?;
114+ rule. run ( & self . config , & self . macros , target, up_to_date) ?;
105115
106116 Ok ( true )
107117 }
@@ -134,7 +144,7 @@ impl Make {
134144 }
135145
136146 /// Checks if the target has recursive prerequisites.
137- /// Returns ` true` if the target has recursive prerequisites.
147+ /// Returns true if the target has recursive prerequisites.
138148 fn are_prerequisites_recursive ( & self , target : impl AsRef < str > ) -> bool {
139149 let mut visited = HashSet :: from ( [ target. as_ref ( ) ] ) ;
140150 let mut stack = HashSet :: from ( [ target. as_ref ( ) ] ) ;
@@ -176,13 +186,18 @@ impl TryFrom<(Makefile, Config)> for Make {
176186 fn try_from ( ( makefile, config) : ( Makefile , Config ) ) -> Result < Self , Self :: Error > {
177187 let mut rules = vec ! [ ] ;
178188 let mut special_rules = vec ! [ ] ;
189+ let mut inference_rules = vec ! [ ] ;
190+
179191 for rule in makefile. rules ( ) {
180192 let rule = Rule :: from ( rule) ;
181193 let Some ( target) = rule. targets ( ) . next ( ) else {
182194 return Err ( NoTarget { target : None } ) ;
183195 } ;
196+
184197 if SpecialTarget :: try_from ( target. clone ( ) ) . is_ok ( ) {
185198 special_rules. push ( rule) ;
199+ } else if InferenceTarget :: try_from ( ( target. clone ( ) , config. clone ( ) ) ) . is_ok ( ) {
200+ inference_rules. push ( rule) ;
186201 } else {
187202 rules. push ( rule) ;
188203 }
0 commit comments