// file : libbuild2/rule.hxx -*- C++ -*- // license : MIT; see accompanying LICENSE file #ifndef LIBBUILD2_RULE_HXX #define LIBBUILD2_RULE_HXX #include #include #include #include #include #include #include namespace build2 { // Once a rule is registered (for a scope), it is treated as immutable. If // you need to modify some state (e.g., counters or some such), then make // sure it is MT-safe. // // Note: match() is only called once but may not be followed by apply(). // class rule { public: virtual bool match (action, target&, const string& hint) const = 0; virtual recipe apply (action, target&) const = 0; rule () = default; rule (const rule&) = delete; rule& operator= (const rule&) = delete; }; // Fallback rule that only matches if the file exists. It will also match // an mtime_target provided it has a set timestamp. // class LIBBUILD2_SYMEXPORT file_rule: public rule { public: virtual bool match (action, target&, const string&) const override; virtual recipe apply (action, target&) const override; file_rule () {} static const file_rule instance; }; class LIBBUILD2_SYMEXPORT alias_rule: public rule { public: virtual bool match (action, target&, const string&) const override; virtual recipe apply (action, target&) const override; alias_rule () {} static const alias_rule instance; }; // Note that this rule ignores the dry_run flag; see mkdir() in filesystem // for the rationale. // class LIBBUILD2_SYMEXPORT fsdir_rule: public rule { public: virtual bool match (action, target&, const string&) const override; virtual recipe apply (action, target&) const override; static target_state perform_update (action, const target&); static target_state perform_clean (action, const target&); // Sometimes, as an optimization, we want to emulate execute_direct() // of fsdir{} without the overhead of switching to the execute phase. // static void perform_update_direct (action, const target&); fsdir_rule () {} static const fsdir_rule instance; }; // Fallback rule that always matches and does nothing. // class LIBBUILD2_SYMEXPORT noop_rule: public rule { public: virtual bool match (action, target&, const string&) const override; virtual recipe apply (action, target&) const override; noop_rule () {} static const noop_rule instance; }; // Ad hoc rule. // class LIBBUILD2_SYMEXPORT adhoc_rule: rule { public: using location_type = build2::location; // Diagnostics-related information. // path_name_value buildfile; // Buildfile of recipe. location_type location; // Buildfile location of recipe. size_t braces; // Number of braces in multi-brace tokens. build2::rule_match rule_match; adhoc_rule (const location_type& l, size_t b) : buildfile (l.file), location (buildfile, l.line, l.column), braces (b), rule_match ("adhoc", *this) {} public: // Some of the operations come in compensating pairs, sush as update and // clean, install and uninstall. An ad hoc rule implementation may choose // to provide a fallback implementation of a compensating operation if it // is providing the other half (passed in the fallback argument). // // The default implementation calls rule::match() if fallback is absent // and returns false if fallback is present. So an implementation that // doesn't care about this semantics can implement the straight rule // interface. // virtual bool match (action, target&, const string&, optional fallback) const; virtual bool match (action, target&, const string&) const override; virtual void dump (ostream&, const string& indentation) const = 0; }; // Ad hoc script rule. // // Note: should not be used directly (i.e., registered). // class LIBBUILD2_SYMEXPORT adhoc_script_rule: public adhoc_rule { public: virtual bool match (action, target&, const string&, optional) const override; virtual recipe apply (action, target&) const override; target_state perform_update_file (action, const target&) const; target_state default_action (action, const target&) const; virtual void dump (ostream&, const string&) const override; adhoc_script_rule (string s, optional d, const location_type& l, size_t b) : adhoc_rule (l, b), script (move (s)), diag (move (d)) {} public: string script; optional diag; // Command name for low-verbosity diagnostics. }; } #endif // LIBBUILD2_RULE_HXX