aboutsummaryrefslogtreecommitdiff
path: root/build/rule
blob: 8c7e4c126e59ddf21804ed4da853ef6b79aec816 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
// file      : build/rule -*- C++ -*-
// copyright : Copyright (c) 2014-2015 Code Synthesis Ltd
// license   : MIT; see accompanying LICENSE file

#ifndef BUILD_RULE
#define BUILD_RULE

#include <string>
#include <cstddef>        // nullptr_t
#include <typeindex>
#include <functional>     // reference_wrapper
#include <unordered_map>

#include <butl/prefix-map>

#include <build/types>
#include <build/target>
#include <build/operation>

namespace build
{
  class match_result
  {
  public:
    typedef build::target target_type;
    typedef build::prerequisite prerequisite_type;

    // Can contain neither (both are NULL), one of, or both. If both
    // are NULL, then it is a "no match" indicator.
    //
    prerequisite_type* prerequisite;
    target_type* target;

    match_result (std::nullptr_t v = nullptr): prerequisite (v), target (v) {}
    match_result (prerequisite_type& p): prerequisite (&p), target (nullptr) {}
    match_result (prerequisite_type* p): prerequisite (p), target (nullptr) {}
    match_result (target_type& t): prerequisite (nullptr), target (&t) {}
    match_result (target_type* t): prerequisite (nullptr), target (t) {}
    match_result (const prerequisite_member& pm)
        : prerequisite (&pm.prerequisite.get ()), target (pm.target) {}

    explicit
    operator bool () const
    {
      return prerequisite != nullptr || target != nullptr;
    }
  };

  class rule
  {
  public:
    virtual match_result
    match (action, target&, const std::string& hint) const = 0;

    virtual recipe
    apply (action, target&, const match_result&) const = 0;
  };

  using target_rule_map = std::unordered_map<
    std::type_index,
    butl::prefix_map<std::string, std::reference_wrapper<rule>, '.'>>;

  using operation_rule_map = std::unordered_map<operation_id, target_rule_map>;

  extern operation_rule_map rules;

  // Fallback rule that on update verifies that the path exists and is
  // not older than any of its prerequisites.
  //
  class path_rule: public rule
  {
  public:
    virtual match_result
    match (action, target&, const std::string& hint) const;

    virtual recipe
    apply (action, target&, const match_result&) const;

    static target_state
    perform_update (action, target&);
  };

  class dir_rule: public rule
  {
  public:
    virtual match_result
    match (action, target&, const std::string& hint) const;

    virtual recipe
    apply (action, target&, const match_result&) const;
  };

  class fsdir_rule: public rule
  {
  public:
    virtual match_result
    match (action, target&, const std::string& hint) const;

    virtual recipe
    apply (action, target&, const match_result&) const;

    static target_state
    perform_update (action, target&);

    static target_state
    perform_clean (action, target&);
  };
}

#endif // BUILD_RULE