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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
|
// file : build/dist/operation.cxx -*- C++ -*-
// copyright : Copyright (c) 2014-2015 Code Synthesis Ltd
// license : MIT; see accompanying LICENSE file
#include <build/dist/operation>
#include <cassert>
#include <butl/process>
#include <butl/filesystem>
#include <build/file>
#include <build/dump>
#include <build/scope>
#include <build/target>
#include <build/context>
#include <build/algorithm>
#include <build/diagnostics>
using namespace std;
using namespace butl;
namespace build
{
namespace dist
{
static void
dist_meta_operation_pre ()
{
// Reset the dependency state so that we don't end up with stray
// files from previous batches.
//
// @@ This is called too late, after we have bootstrapped the
// project.
//
//reset ();
}
static operation_id
dist_operation_pre (operation_id o)
{
if (o != default_id)
fail << "explicit operation specified for dist meta-operation";
return o;
}
static void
dist_match (action, action_targets&)
{
// Don't match anything -- see execute ().
}
// install -d <dir>
//
static void
install (const string& cmd, const dir_path&);
// install <file> <dir>
//
static void
install (const string& cmd, file&, const dir_path&);
// cd <root> && tar|zip ... <pkg>.<ext> <pkg>
//
static void
archive (const dir_path& root, const string& pkg, const string& ext);
static void
dist_execute (action, const action_targets& ts, bool)
{
tracer trace ("dist_execute");
// For now we assume all the targets are from the same project.
//
target& t (*static_cast<target*> (ts[0]));
scope* rs (t.base_scope ().root_scope ());
if (rs == nullptr)
fail << "out of project target " << t;
const dir_path& out_root (rs->out_path ());
const dir_path& src_root (rs->src_path ());
if (out_root == src_root)
fail << "in-tree distribution of target " << t <<
info << "distribution requires out-of-tree build";
// Make sure we have the necessary configuration before
// we get down to business.
//
auto l (rs->vars["dist.root"]);
if (!l || l->empty ())
fail << "unknown root distribution directory" <<
info << "did you forget to specify config.dist.root?";
const dir_path& dist_root (as<dir_path> (*l));
if (!dir_exists (dist_root))
fail << "root distribution directory " << dist_root
<< " does not exist";
l = rs->vars["dist.package"];
if (!l || l->empty ())
fail << "unknown distribution package name" <<
info << "did you forget to set dist.package?";
const string& dist_package (as<string> (*l));
const string& dist_cmd (as<string> (*rs->vars["dist.cmd"]));
// Get the list of operations supported by this project. Skip
// default_id.
//
for (operations::size_type id (default_id + 1);
id < rs->operations.size ();
++id)
{
const operation_info* oi (rs->operations[id]);
if (oi == nullptr)
continue;
// Note that we are not calling operation_pre/post() callbacks
// here since the meta operation is dist and we know what we
// are doing.
//
current_inner_oif = oi;
current_outer_oif = nullptr;
current_mode = oi->mode;
dependency_count = 0;
action a (dist_id, id);
if (verb >= 6)
dump (a);
for (void* v: ts)
{
target& t (*static_cast<target*> (v));
if (rs != t.base_scope ().root_scope ())
fail << "out of project target " << t;
level5 ([&]{trace << diag_doing (a, t);});
match (a, t);
}
if (verb >= 6)
dump (a);
}
// Add buildfiles that are not normally loaded as part of the
// project, for example, the export stub. They will still be
// ignored on the next step if the user explicitly marked them
// nodist.
//
auto add_adhoc = [&src_root, &trace] (const char* f)
{
path p (src_root / path (f));
if (file_exists (p))
{
const char* e (p.extension ());
targets.insert<buildfile> (
p.directory (),
p.leaf ().base ().string (),
&extension_pool.find (e == nullptr ? "" : e), // Specified.
trace);
}
};
add_adhoc ("build/export.build");
// Collect the files. We want to take the snapshot of targets
// since updating some of them may result in more targets being
// entered.
//
action_targets files;
for (const auto& pt: targets)
{
file* ft (pt->is_a<file> ());
if (ft == nullptr) // Not a file.
continue;
if (ft->dir.sub (src_root))
{
// Include unless explicitly excluded.
//
files.push_back (ft);
continue;
}
if (ft->dir.sub (out_root))
{
// Exclude unless explicitly included.
//
//files.push_back (*ft);
continue;
}
}
// Make sure what we need to distribute is up to date.
//
{
if (perform.meta_operation_pre != nullptr)
perform.meta_operation_pre ();
current_mif = &perform;
if (perform.operation_pre != nullptr)
perform.operation_pre (update_id);
current_inner_oif = &update;
current_outer_oif = nullptr;
current_mode = update.mode;
dependency_count = 0;
action a (perform_id, update_id);
perform.match (a, files);
perform.execute (a, files, true); // Run quiet.
if (perform.operation_post != nullptr)
perform.operation_post (update_id);
if (perform.meta_operation_post != nullptr)
perform.meta_operation_post ();
}
dir_path td (dist_root / dir_path (dist_package));
// Clean up the target directory.
//
// @@ Not for incremental dist?
//
if (build::rmdir_r (td) == rmdir_status::not_empty)
fail << "unable to clean target directory " << td;
install (dist_cmd, td);
// Copy over all the files.
//
for (void* v: files)
{
file& t (*static_cast<file*> (v));
// Figure out where this file is inside the target directory.
//
dir_path d (td);
d /= t.dir.sub (src_root)
? t.dir.leaf (src_root)
: t.dir.leaf (out_root);
if (!dir_exists (d))
install (dist_cmd, d);
install (dist_cmd, t, d);
}
// Archive if requested.
//
if (auto l = rs->vars["dist.archives"])
{
for (const string& e: as<strings> (*l))
archive (dist_root, dist_package, e);
}
}
// install -d <dir>
//
static void
install (const string& cmd, const dir_path& d)
{
path reld (relative (d));
cstrings args {cmd.c_str (), "-d"};
args.push_back ("-m");
args.push_back ("755");
args.push_back (reld.string ().c_str ());
args.push_back (nullptr);
if (verb >= 2)
print_process (args);
else if (verb)
text << "dist -d " << d;
try
{
process pr (args.data ());
if (!pr.wait ())
throw failed ();
}
catch (const process_error& e)
{
error << "unable to execute " << args[0] << ": " << e.what ();
if (e.child ())
exit (1);
throw failed ();
}
}
// install <file> <dir>
//
static void
install (const string& cmd, file& t, const dir_path& d)
{
path reld (relative (d));
path relf (relative (t.path ()));
cstrings args {cmd.c_str ()};
// Preserve timestamps. This could becomes important if, for
// example, we have pre-generated sources. Note that the
// install-sh script doesn't support this option, while both
// Linux and BSD install's do.
//
args.push_back ("-p");
// Assume the file is executable if the owner has execute
// permission, in which case we make it executable for
// everyone.
//
args.push_back ("-m");
args.push_back (
(path_permissions (t.path ()) & permissions::xu) == permissions::xu
? "755"
: "644");
args.push_back (relf.string ().c_str ());
args.push_back (reld.string ().c_str ());
args.push_back (nullptr);
if (verb >= 2)
print_process (args);
else if (verb)
text << "dist " << t;
try
{
process pr (args.data ());
if (!pr.wait ())
throw failed ();
}
catch (const process_error& e)
{
error << "unable to execute " << args[0] << ": " << e.what ();
if (e.child ())
exit (1);
throw failed ();
}
}
static void
archive (const dir_path& root, const string& pkg, const string& e)
{
string a (pkg + '.' + e);
// Delete old archive for good measure.
//
path ap (root / path (a));
if (file_exists (ap))
rmfile (ap);
// Use zip for .zip archives. Everything else goes to tar in the
// auto-compress mode (-a).
//
cstrings args;
if (e == "zip")
args = {"zip", "-rq", a.c_str (), pkg.c_str (), nullptr};
else
args = {"tar", "-a", "-cf", a.c_str (), pkg.c_str (), nullptr};
if (verb >= 2)
print_process (args);
else if (verb)
text << args[0] << " " << ap;
try
{
// Change child's working directory to dist_root.
//
process pr (root.string ().c_str (), args.data ());
if (!pr.wait ())
throw failed ();
}
catch (const process_error& e)
{
error << "unable to execute " << args[0] << ": " << e.what ();
if (e.child ())
exit (1);
throw failed ();
}
}
meta_operation_info dist {
"dist",
"distribute",
"distributing",
"has nothing to distribute", // We cannot "be distributed".
&dist_meta_operation_pre,
&dist_operation_pre,
&load, // normal load
&search, // normal search
&dist_match,
&dist_execute,
nullptr, // operation post
nullptr // meta-operation post
};
}
}
|