aboutsummaryrefslogtreecommitdiff
path: root/tests/recipe/cxx/testscript
blob: c94148e8731b89225b053f8a8c089ab3f53b332b (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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
# file      : tests/recipe/cxx/testscript
# license   : MIT; see accompanying LICENSE file

# Ad hoc C++ recipes not supported in a statically-linked build system. Also
# disable when cross-testing for the sake of simplicity.
#
if (!$static && $test.target == $build.host)
{
  +mkdir build
  +cat <<EOI >=build/bootstrap.build
    project = test
    amalgamation =
    subprojects =

    using config
    using test
    EOI

  +cat <<EOI >=build/root.build
    EOI

  +cat <<EOI >=buildfile
    ./:
    {{ c++ 1
      // Dummy recipe to trigger cleanup.
    }}
    EOI

  +export BDEP_SYNC=0

  : update-clean
  :
  {
    echo 'bar' >=bar;

    cat <<EOI >=buildfile;
      foo: bar
      % update clean
      {{ c++ 1
        recipe
        apply (action a, target& xt) const override
        {
          file& t (xt.as<file> ());

          t.derive_path ();
          inject_fsdir (a, t);
          match_prerequisite_members (a, t);

          switch (a)
          {
          case perform_update_id:  return perform_update;
          case perform_clean_id:   return perform_clean_depdb;
          default: assert (false); return noop_recipe;
          }
        }

        static target_state
        perform_update (action a, const target& xt)
        {
          const file& t (xt.as<file> ());
          const path& tp (t.path ());

          timestamp mt (t.load_mtime ());
          auto pr (execute_prerequisites<file> (a, t, mt));

          bool update (!pr.first);
          target_state r (update ? target_state::changed : *pr.first);

          const file& s (pr.second);
          const path& sp (s.path ());

          depdb dd (tp + ".d");
          dd.expect (sp);

          if (dd.writing () || dd.mtime > mt)
            update = true;

          dd.close ();

          if (!update)
            return r;

          if (verb == 1)
            text << "cp " << t;
          else if (verb >= 2)
            text << "cp " << sp << ' ' << tp;

          cpfile (sp, tp);
          return target_state::changed;
        }
      }}
      EOI

    $* 2>>~%EOE%;
      %^(c\+\+|ld).*%+
      cp file{foo}
      EOE

    cat <<<foo >'bar';

    # While at it, make sure there is no rebuild.
    #
    $* 2>/'info: dir{./} is up to date';

    $* clean 2>-
  }

  : test
  :
  {
    echo 'bar' >=bar;

    cat <<EOI >=buildfile;
      foo: bar
      {{
        cp $path($<) $path($>)
      }}
      % test
      {{ c++ 1 --

        #include <iostream>

        --

        recipe
        apply (action a, target& t) const override
        {
          if (a.outer ())
          {
            match_inner (a, t);
            return execute_inner;
          }
          else
            return perform_test;
        }

        static target_state
        perform_test (action, const target& xt)
        {
          const file& t (xt.as<file> ());
          const path& tp (t.path ());

          if (verb == 1)
            text << "test " << t;
          else if (verb >= 2)
            text << "cat " << tp;

          ifdstream ifs (tp);
          if (ifs.peek () != ifdstream::traits_type::eof ())
            std::cerr << ifs.rdbuf ();
          ifs.close ();

          return target_state::changed;
        }
      }}
      EOI

    $* test 2>>~%EOE%;
      %^(c\+\+|ld).*%+
      cp file{foo}
      test file{foo}
      bar
      EOE

    $* clean 2>-
  }

  : rule
  :
  {
    cat <<EOI >=buildfile;
      alias{far}: alias{bar}
      alias{bar}:

      alias{~'/f(.+)/'}: alias{~'/b\1/'}
      {{ c++ 1 --

        #include <iostream>

        --

        recipe
        apply (action, target& t) const override
        {
          const auto& mrs (t.data<regex_match_results> ());

          return [this, mr = mrs.str (1)] (action a, const target& t)
          {
            return perform_update (a, t, mr);
          };
        }

        target_state
        perform_update (action, const target&, const string& mr) const
        {
          text << pattern->rule_name << ": " << mr;
          return target_state::changed;
        }
      }}
      EOI

    $* 2>>~%EOE%
      %^(c\+\+|ld).*%+
      <ad hoc pattern rule #1>: ar
      EOE
  }

  # Clean recipe builds if the testscript is enabled (see above for details).
  #
  -$* clean 2>-
}