Add the results of reference implementations back.
[tatoo.git] / remake.cpp
1 /**
2 @mainpage Remake, a build system that bridges the gap between make and redo.
3
4 As with <b>make</b>, <b>remake</b> uses a centralized rule file, which is
5 named <b>Remakefile</b>. It contains rules with a <em>make</em>-like
6 syntax:
7
8 @verbatim
9 target1 target2 ... : prerequisite1 prerequisite2 ...
10         shell script
11         that builds
12         the targets
13 @endverbatim
14
15 A target is known to be up-to-date if all its prerequisites are. If it
16 has no known prerequisites yet the file already exits, it is assumed to
17 be up-to-date. Obsolete targets are rebuilt thanks to the shell script
18 provided by the rule.
19
20 As with <b>redo</b>, <b>remake</b> supports dynamic dependencies in
21 addition to these static dependencies. Whenever a script executes
22 <tt>remake prerequisite4 prerequisite5 ...</tt>, these prerequisites are
23 rebuilt if they are obsolete. (So <b>remake</b> acts like
24 <b>redo-ifchange</b>.) Moreover, all the dependencies are stored in file
25 <b>.remake</b> so that they are remembered in subsequent runs. Note that
26 dynamic dependencies from previous runs are only used to decide whether a
27 target is obsolete; they are not automatically rebuilt when they are
28 obsolete yet a target depends on them. They will only be rebuilt once the
29 dynamic call to <b>remake</b> is executed.
30
31 In other words, the following two rules have almost the same behavior.
32
33 @verbatim
34 target1 target2 ... : prerequisite1 prerequisite2 ...
35         shell script
36
37 target1 target2 ... :
38         remake prerequisite1 prerequisite2 ...
39         shell script
40 @endverbatim
41
42 (There is a difference if the targets already exist, have never been
43 built before, and the prerequisites are either younger or obsolete, since
44 the targets will not be rebuilt in the second case.)
45
46 The above usage of dynamic dependencies is hardly useful. Their strength
47 lies in the fact that they can be computed on the fly:
48
49 @verbatim
50 %.o : %.c
51         gcc -MMD -MF $@.d -o $@ -c $<
52         remake -r < $@.d
53         rm $@.d
54
55 %.cmo : %.ml
56         ocamldep $< | remake -r $@
57         ocamlc -c $<
58
59 after.xml: before.xml rules.xsl
60         xsltproc --load-trace -o after.xml rules.xsl before.xml 2> deps
61         remake `sed -n -e "\\,//,! s,^.*URL=\"\\([^\"]*\\).*\$,\\1,p" deps`
62         rm deps
63 @endverbatim
64
65 Note that the first rule fails if any of the header files included by
66 a C source file has to be automatically generated. In that case, one
67 should perform a first call to <b>remake</b> them before calling the
68 compiler. (Dependencies from several calls to <b>remake</b> are
69 cumulative, so they will all be remembered the next time.)
70
71 \section sec-usage Usage
72
73 Usage: <tt>remake <i>options</i> <i>targets</i></tt>
74
75 Options:
76
77 - <tt>-d</tt>: Echo script commands.
78 - <tt>-f FILE</tt>: Read <tt>FILE</tt> as <b>Remakefile</b>.
79 - <tt>-j[N]</tt>, <tt>--jobs=[N]</tt>: Allow <tt>N</tt> jobs at once;
80   infinite jobs with no argument.
81 - <tt>-k</tt>, <tt>--keep-going</tt>: Keep going when some targets cannot be made.
82 - <tt>-r</tt>: Look up targets from the dependencies on standard input.
83 - <tt>-s</tt>, <tt>--silent</tt>, <tt>--quiet</tt>: Do not echo targets.
84
85 \section sec-syntax Syntax
86
87 Lines starting with a space character or a tabulation are assumed to be rule
88 scripts. They are only allowed after a rule header.
89
90 Lines starting with <tt>#</tt> are considered to be comments and are ignored.
91 They do interrupt rule scripts though.
92
93 Any other line is either a variable definition or a rule header. If such a
94 line ends with a backslash, the following line break is ignored and the line
95 extends to the next one.
96
97 Variable definitions are a single name followed by equal followed by a list
98 of names, possibly empty.
99
100 Rule headers are a nonempty list of names, followed by a colon, followed by
101 another list of names, possibly empty. Basically, the syntax of a rule is as
102 follows:
103
104 @verbatim
105 targets : prerequisites
106         shell script
107 @endverbatim
108
109 List of names are space-separated sequences of names. If a name contains a
110 space character, it should be put into double quotes. Names can not be any
111 of the following special characters <tt>:$(),="</tt>. Again, quotation
112 should be used. Quotation marks can be escaped by a backslash inside
113 quoted names.
114
115 \subsection sec-variables Variables
116
117 Variables can be used to factor lists of targets or prerequisites. They are
118 expanded as they are encountered during <b>Remakefile</b> parsing.
119
120 @verbatim
121 VAR2 = a
122 VAR1 = c d
123 VAR2 += $(VAR1) b
124 $(VAR2) e :
125 @endverbatim
126
127 Variable assignments can appear instead of prerequisites inside non-generic
128 rules with no script. They are then expanded inside the corresponding
129 generic rule.
130
131 @verbatim
132 foo.o: CFLAGS += -DBAR
133
134 %.o : %.c
135         gcc $(CFLAGS) -MMD -MF $@.d -o $@ -c $<
136         remake -r < $@.d
137         rm $@.d
138 @endverbatim
139
140 Note: contrarily to <b>make</b>, variable names have to be enclosed in
141 parentheses. For instance, <tt>$y</tt> is not a shorthand for <tt>\$(y)</tt> and
142 is left unexpanded.
143
144 \subsection sec-autovars Automatic variables
145
146 The following special symbols can appear inside scripts:
147
148 - <tt>$&lt;</tt> expands to the first static prerequisite of the rule.
149 - <tt>$^</tt> expands to all the static prerequisites of the rule, including
150   duplicates if any.
151 - <tt>$\@</tt> expands to the first target of the rule.
152 - <tt>$*</tt> expands to the string that matched <tt>%</tt> in a generic rule.
153 - <tt>$$</tt> expands to a single dollar symbol.
154
155 Note: contrarily to <b>make</b>, there are no corresponding variables. For
156 instance, <tt>$^</tt> is not a shorthand for <tt>$(^)</tt>. Another
157 difference is that <tt>$\@</tt> is always the first target, not the one that
158 triggered the rule.
159
160 \subsection sec-functions Built-in functions
161
162 <b>remake</b> also supports a few built-in functions inspired from <b>make</b>.
163
164 - <tt>$(addprefix <i>prefix</i>, <i>list</i>)</tt> returns the list obtained
165   by prepending its first argument to each element of its second argument.
166 - <tt>$(addsuffix <i>suffix</i>, <i>list</i>)</tt> returns the list obtained
167   by appending its first argument to each element of its second argument.
168
169 \subsection sec-order Order-only prerequisites
170
171 If the static prerequisites of a rule contain a pipe symbol, prerequisites
172 on its right do not cause the targets to become obsolete if they are newer
173 (unless they are also dynamically registered as dependencies). They are
174 meant to be used when the targets do not directly depend on them, but the
175 computation of their dynamic dependencies does.
176
177 @verbatim
178 %.o : %.c | parser.h
179         gcc -MMD -MF $@.d -o $@ -c $<
180         remake -r < $@.d
181         rm $@.d
182
183 parser.c parser.h: parser.y
184         yacc -d -o parser.c parser.y
185 @endverbatim
186
187 \subsection sec-special-var Special variables
188
189 Variable <tt>.OPTIONS</tt> is handled specially. Its content enables some
190 features of <b>remake</b> that are not enabled by default.
191
192 - <tt>variable-propagation</tt>: When a variable is set in the prerequisite
193   part of a rule, it is propagated to the rules of all the targets this rule
194   depends on. This option also enables variables to be set on the command
195   line. Note that, as in <b>make</b>, this features introduces non-determinism:
196   the content of some variables will depend on the build order.
197
198 \section sec-semantics Semantics
199
200 \subsection src-obsolete When are targets obsolete?
201
202 A target is obsolete:
203
204 - if there is no file corresponding to the target, or to one of its siblings
205   in a multi-target rule,
206 - if any of its dynamic prerequisites from a previous run or any of its static
207   prerequisites is obsolete,
208 - if the latest file corresponding to its siblings or itself is older than any
209   of its dynamic prerequisites or static prerequisites.
210
211 In all the other cases, it is assumed to be up-to-date (and so are all its
212 siblings). Note that the last rule above says "latest" and not "earliest". While
213 it might cause some obsolete targets to go unnoticed in corner cases, it allows
214 for the following kind of rules:
215
216 @verbatim
217 config.h stamp-config_h: config.h.in config.status
218         ./config.status config.h
219         touch stamp-config_h
220 @endverbatim
221
222 A <tt>config.status</tt> file generally does not update header files (here
223 <tt>config.h</tt>) if they would not change. As a consequence, if not for the
224 <tt>stamp-config_h</tt> file above, a header would always be considered obsolete
225 once one of its prerequisites is modified. Note that touching <tt>config.h</tt>
226 rather than <tt>stamp-config_h</tt> would defeat the point of not updating it
227 in the first place, since the program files would need to be rebuilt.
228
229 Once all the static prerequisites of a target have been rebuilt, <b>remake</b>
230 checks whether the target still needs to be built. If it was obsolete only
231 because its prerequisites needed to be rebuilt and none of them changed, the
232 target is assumed to be up-to-date.
233
234 \subsection sec-rules How are targets (re)built?
235
236 There are two kinds of rules. If any of the targets or prerequisites contains
237 a <tt>%</tt> character, the rule is said to be <em>generic</em>. All the
238 targets of the rule shall then contain a single <tt>%</tt> character. All the
239 other rules are said to be <em>specific</em>.
240
241 A rule is said to <em>match</em> a given target:
242
243 - if it is specific and the target appears inside its target list,
244 - if it is generic and there is a way to replace the <tt>%</tt> character
245   from one of its targets so that it matches the given target.
246
247 When <b>remake</b> tries to build a given target, it looks for a specific rule
248 that matches it. If there is one and its script is nonempty, it uses it to
249 rebuild the target.
250
251 Otherwise, it looks for a generic rule that matches the target. If there are
252 several matching rules, it chooses the one with the shortest pattern (and if
253 there are several ones, the earliest one). <b>remake</b> then looks for
254 specific rules that match each target of the generic rule. All the
255 prerequisites of these specific rules are added to those of the generic rule.
256 The script of the generic rule is used to build the target.
257
258 Example:
259
260 @verbatim
261 t%1 t2%: p1 p%2
262         commands building t%1 and t2%
263
264 t2z: p4
265         commands building t2z
266
267 ty1: p3
268
269 # t2x is built by the first rule (which also builds tx1) and its prerequisites are p1, px2
270 # t2y is built by the first rule (which also builds ty1) and its prerequisites are p1, py2, p3
271 # t2z is built by the second rule and its prerequisite is p4
272 @endverbatim
273
274 The set of rules from <b>Remakefile</b> is ill-formed:
275
276 - if any specific rule matching a target of the generic rule has a nonempty script,
277 - if any target of the generic rule is matched by a generic rule with a shorter pattern.
278
279 \section sec-compilation Compilation
280
281 - On Linux, MacOSX, and BSD: <tt>g++ -o remake remake.cpp</tt>
282 - On Windows: <tt>g++ -o remake.exe remake.cpp -lws2_32</tt>
283
284 Installing <b>remake</b> is needed only if <b>Remakefile</b> does not
285 specify the path to the executable for its recursive calls. Thanks to its
286 single source file, <b>remake</b> can be shipped inside other packages and
287 built at configuration time.
288
289 \section sec-differences Differences with other build systems
290
291 Differences with <b>make</b>:
292
293 - Dynamic dependencies are supported.
294 - For rules with multiple targets, the shell script is executed only once
295   and is assumed to build all the targets. There is no need for
296   convoluted rules that are robust enough for parallel builds. For generic
297   rules, this is similar to the behavior of pattern rules from <b>gmake</b>.
298 - As with <b>redo</b>, only one shell is run when executing a script,
299   rather than one per script line. Note that the shells are run with
300   option <tt>-e</tt>, thus causing them to exit as soon as an error is
301   encountered.
302 - The prerequisites of generic rules (known as implicit rules in make lingo)
303   are not used to decide between several of them. <b>remake</b> does not
304   select one for which it could satisfy the dependencies.
305 - Variables and built-in functions are expanded as they are encountered
306   during <b>Remakefile</b> parsing.
307 - Target-specific variables are not propagated, unless specifically enabled,
308   since this causes non-deterministic builds. This is the same for variables
309   set on the command line.
310
311 Differences with <b>redo</b>:
312
313 - As with <b>make</b>, it is possible to write the following kind of rules
314   in <b>remake</b>.
315 @verbatim
316 Remakefile: Remakefile.in ./config.status
317         ./config.status Remakefile
318 @endverbatim
319 - If a target is already built the first time <b>remake</b> runs, it still
320   uses the static prerequisites of rules mentioning it to check whether it
321   needs to be rebuilt. It does not assume it to be up-to-date. As with
322   <b>redo</b> though, if its obsolete status would be due to a dynamic
323   prerequisite, it will go unnoticed; it should be removed beforehand.
324 - Multiple targets are supported.
325 - <b>remake</b> has almost no features: no checksum-based dependencies, no
326   compatibility with job servers, etc.
327
328 \section sec-limitations Limitations
329
330 - If a rule script calls <b>remake</b>, the current working directory should
331   be the directory containing <b>Remakefile</b> (or the working directory
332   from the original <b>remake</b> if it was called with option <b>-f</b>).
333 - As with <b>make</b>, variables passed on the command line should keep
334   the same values, to ensure deterministic builds.
335 - Some cases of ill-formed rules are not caught by <b>remake</b> and can
336   thus lead to unpredictable behaviors.
337
338 \section sec-links Links
339
340 @see http://cr.yp.to/redo.html for the philosophy of <b>redo</b> and
341 https://github.com/apenwarr/redo for an implementation and some comprehensive documentation.
342
343 \section sec-licensing Licensing
344
345 @author Guillaume Melquiond
346 @version 0.9
347 @date 2012-2013
348 @copyright
349 This program is free software: you can redistribute it and/or modify
350 it under the terms of the GNU General Public License as published by
351 the Free Software Foundation, either version 3 of the License, or
352 (at your option) any later version.
353 \n
354 This program is distributed in the hope that it will be useful,
355 but WITHOUT ANY WARRANTY; without even the implied warranty of
356 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
357 GNU General Public License for more details.
358
359 \section sec-internals Internals
360
361 The parent <b>remake</b> process acts as a server. The other ones have a
362 REMAKE_SOCKET environment variable that tells them how to contact the
363 server. They send the content of the REMAKE_JOB_ID environment variable,
364 so that the server can associate the child targets to the jobs that
365 spawned them. They then wait for completion and exit with the status
366 returned by the server. This is handled by #client_mode.
367
368 The server calls #load_dependencies and #save_dependencies to serialize
369 dynamic dependencies from <b>.remake</b>. It loads <b>Remakefile</b> with
370 #load_rules. It then runs #server_mode, which calls #server_loop.
371
372 When building a target, the following sequence of events happens:
373
374 - #start calls #find_rule (and #find_generic_rule) to get the rule.
375 - It then creates a pseudo-client if the rule has static dependencies, or
376   calls #run_script otherwise. In both cases, a new job is created and its
377   targets are put into #job_targets.
378 - #run_script creates a shell process and stores it in #job_pids. It
379   increases #running_jobs.
380 - The child process possibly calls <b>remake</b> with a list of targets.
381 - #accept_client receives a build request from a child process and adds
382   it to #clients. It also records the new dependencies of the job into
383   #dependencies. It increases #waiting_jobs.
384 - #handle_clients uses #get_status to look up the obsoleteness of the
385   targets.
386 - Once the targets of a request have been built or one of them has failed,
387   #handle_clients calls #complete_request and removes the request from
388   #clients.
389 - If the build targets come from a pseudo-client, #complete_request calls
390   #run_script. Otherwise it sends the reply to the corresponding child
391   process and decreases #waiting_jobs.
392 - When a child process ends, #server_loop calls #finalize_job, which
393   removes the process from #job_pids, decreases #running_jobs, and calls
394   #complete_job.
395 - #complete_job removes the job from #job_targets and calls #update_status
396   to change the status of the targets. It also removes the target files in
397   case of failure.
398 */
399
400 #ifdef _WIN32
401 #define WIN32_LEAN_AND_MEAN
402 #define WINDOWS
403 #endif
404
405 #include <fstream>
406 #include <iostream>
407 #include <list>
408 #include <map>
409 #include <set>
410 #include <sstream>
411 #include <string>
412 #include <vector>
413 #include <cassert>
414 #include <cstdlib>
415 #include <cstring>
416 #include <ctime>
417 #include <errno.h>
418 #include <fcntl.h>
419 #include <signal.h>
420 #include <unistd.h>
421 #include <sys/stat.h>
422 #include <sys/types.h>
423
424 #ifdef __APPLE__
425 #define MACOSX
426 #endif
427
428 #ifdef __linux__
429 #define LINUX
430 #endif
431
432 #ifdef WINDOWS
433 #include <windows.h>
434 #include <winbase.h>
435 #include <winsock2.h>
436 #define pid_t HANDLE
437 typedef SOCKET socket_t;
438 #else
439 #include <sys/socket.h>
440 #include <sys/un.h>
441 #include <sys/wait.h>
442 typedef int socket_t;
443 enum { INVALID_SOCKET = -1 };
444 extern char **environ;
445 #endif
446
447 #if defined(WINDOWS) || defined(MACOSX)
448 enum { MSG_NOSIGNAL = 0 };
449 #endif
450
451 typedef std::list<std::string> string_list;
452
453 typedef std::set<std::string> string_set;
454
455 /**
456  * Reference-counted shared object.
457  * @note The default constructor delays the creation of the object until it
458  *       is first dereferenced.
459  */
460 template<class T>
461 struct ref_ptr
462 {
463         struct content
464         {
465                 size_t cnt;
466                 T val;
467                 content(): cnt(1) {}
468                 content(T const &t): cnt(1), val(t) {}
469         };
470         mutable content *ptr;
471         ref_ptr(): ptr(NULL) {}
472         ref_ptr(T const &t): ptr(new content(t)) {}
473         ref_ptr(ref_ptr const &p): ptr(p.ptr) { if (ptr) ++ptr->cnt; }
474         ~ref_ptr() { if (ptr && --ptr->cnt == 0) delete ptr; }
475         ref_ptr &operator=(ref_ptr const &p)
476         {
477                 if (ptr == p.ptr) return *this;
478                 if (ptr && --ptr->cnt == 0) delete ptr;
479                 ptr = p.ptr;
480                 if (ptr) ++ptr->cnt;
481                 return *this;
482         }
483         T &operator*() const
484         {
485                 if (!ptr) ptr = new content;
486                 return ptr->val;
487         }
488         T *operator->() const { return &**this; }
489 };
490
491 struct dependency_t
492 {
493         string_list targets;
494         string_set deps;
495 };
496
497 typedef std::map<std::string, ref_ptr<dependency_t> > dependency_map;
498
499 typedef std::map<std::string, string_list> variable_map;
500
501 /**
502  * Build status of a target.
503  */
504 enum status_e
505 {
506         Uptodate, ///< Target is up-to-date.
507         Todo,     ///< Target is missing or obsolete.
508         Recheck,  ///< Target has an obsolete dependency.
509         Running,  ///< Target is being rebuilt.
510         Remade,   ///< Target was successfully rebuilt.
511         Failed    ///< Build failed for target.
512 };
513
514 /**
515  * Build status of a target.
516  */
517 struct status_t
518 {
519         status_e status; ///< Actual status.
520         time_t last;     ///< Last-modified date.
521 };
522
523 typedef std::map<std::string, status_t> status_map;
524
525 /**
526  * Delayed assignment to a variable.
527  */
528 struct assign_t
529 {
530         bool append;
531         string_list value;
532 };
533
534 typedef std::map<std::string, assign_t> assign_map;
535
536 /**
537  * A rule loaded from Remakefile.
538  */
539 struct rule_t
540 {
541         string_list targets; ///< Files produced by this rule.
542         string_list deps;    ///< Dependencies used for an implicit call to remake at the start of the script.
543         string_list wdeps;   ///< Like #deps, except that they are not registered as dependencies.
544         assign_map assigns;  ///< Assignment of variables.
545         std::string script;  ///< Shell script for building the targets.
546 };
547
548 typedef std::list<rule_t> rule_list;
549
550 typedef std::map<std::string, ref_ptr<rule_t> > rule_map;
551
552 /**
553  * A job created from a set of rules.
554  */
555
556 struct job_t
557 {
558         rule_t rule;       ///< Original rule.
559         std::string stem;  ///< Pattern used to instantiate the generic rule, if any.
560         variable_map vars; ///< Values of local variables.
561 };
562
563 typedef std::map<int, job_t> job_map;
564
565 typedef std::map<pid_t, int> pid_job_map;
566
567 /**
568  * Client waiting for a request to complete.
569  *
570  * There are two kinds of clients:
571  * - real clients, which are instances of remake created by built scripts,
572  * - pseudo clients, which are created by the server to build specific targets.
573  *
574  * Among pseudo clients, there are two categories:
575  * - original clients, which are created for the targets passed on the
576  *   command line by the user or for the initial regeneration of the rule file,
577  * - dependency clients, which are created to handle rules that have
578  *   explicit dependencies and thus to emulate a call to remake.
579  */
580 struct client_t
581 {
582         socket_t socket;     ///< Socket used to reply to the client (invalid for pseudo clients).
583         int job_id;          ///< Job for which the built script called remake and spawned the client (negative for original clients).
584         bool failed;         ///< Whether some targets failed in mode -k.
585         string_list pending; ///< Targets not yet started.
586         string_set running;  ///< Targets being built.
587         variable_map vars;   ///< Variables set on request.
588         bool delayed;        ///< Whether it is a dependency client and a script has to be started on request completion.
589         client_t(): socket(INVALID_SOCKET), job_id(-1), failed(false), delayed(false) {}
590 };
591
592 typedef std::list<client_t> client_list;
593
594 /**
595  * Map from variable names to their content.
596  * Initialized with the values passed on the command line.
597  */
598 static variable_map variables;
599
600 /**
601  * Map from targets to their known dependencies.
602  */
603 static dependency_map dependencies;
604
605 /**
606  * Map from targets to their build status.
607  */
608 static status_map status;
609
610 /**
611  * Set of generic rules loaded from Remakefile.
612  */
613 static rule_list generic_rules;
614
615 /**
616  * Map from targets to specific rules loaded from Remakefile.
617  */
618 static rule_map specific_rules;
619
620 /**
621  * Map of jobs being built.
622  */
623 static job_map jobs;
624
625 /**
626  * Map from jobs to shell pids.
627  */
628 static pid_job_map job_pids;
629
630 /**
631  * List of clients waiting for a request to complete.
632  * New clients are put to front, so that the build process is depth-first.
633  */
634 static client_list clients;
635
636 /**
637  * Maximum number of parallel jobs (non-positive if unbounded).
638  * Can be modified by the -j option.
639  */
640 static int max_active_jobs = 1;
641
642 /**
643  * Whether to keep building targets in case of failure.
644  * Can be modified by the -k option.
645  */
646 static bool keep_going = false;
647
648 /**
649  * Number of jobs currently running:
650  * - it increases when a process is created in #run_script,
651  * - it decreases when a completion message is received in #finalize_job.
652  *
653  * @note There might be some jobs running while #clients is empty.
654  *       Indeed, if a client requested two targets to be rebuilt, if they
655  *       are running concurrently, if one of them fails, the client will
656  *       get a failure notice and might terminate before the other target
657  *       finishes.
658  */
659 static int running_jobs = 0;
660
661 /**
662  * Number of jobs currently waiting for a build request to finish:
663  * - it increases when a build request is received in #accept_client
664  *   (since the client is presumably waiting for the reply),
665  * - it decreases when a reply is sent in #complete_request.
666  */
667 static int waiting_jobs = 0;
668
669 /**
670  * Global counter used to produce increasing job numbers.
671  * @see job_targets
672  */
673 static int job_counter = 0;
674
675 /**
676  * Socket on which the server listens for client request.
677  */
678 static socket_t socket_fd;
679
680 /**
681  * Whether the request of an original client failed.
682  */
683 static bool build_failure;
684
685 #ifndef WINDOWS
686 /**
687  * Name of the server socket in the file system.
688  */
689 static char *socket_name;
690 #endif
691
692 /**
693  * Name of the first target of the first specific rule, used for default run.
694  */
695 static std::string first_target;
696
697 /**
698  * Whether a short message should be displayed for each target.
699  */
700 static bool show_targets = true;
701
702 /**
703  * Whether script commands are echoed.
704  */
705 static bool echo_scripts = false;
706
707 /**
708  * Time at the start of the program.
709  */
710 static time_t now = time(NULL);
711
712 /**
713  * Directory with respect to which command-line names are relative.
714  */
715 static std::string working_dir;
716
717 /**
718  * Directory with respect to which targets are relative.
719  */
720 static std::string prefix_dir;
721
722 /**
723  * Whether target-specific variables are propagated to prerequisites.
724  */
725 static bool propagate_vars = false;
726
727
728 /**
729  * Whether to print that we changed working directory (to be nice with emacs)
730  */
731 static bool print_change_working_dir = false;
732
733 #ifndef WINDOWS
734 static volatile sig_atomic_t got_SIGCHLD = 0;
735
736 static void sigchld_handler(int)
737 {
738         got_SIGCHLD = 1;
739 }
740
741 static void sigint_handler(int)
742 {
743         // Child processes will receive the signal too, so just prevent
744         // new jobs from starting and wait for the running jobs to fail.
745         keep_going = false;
746 }
747 #endif
748
749 struct log
750 {
751         bool active, open;
752         int depth;
753         log(): active(false), open(false), depth(0)
754         {
755         }
756         std::ostream &operator()()
757         {
758                 if (open) std::cerr << std::endl;
759                 assert(depth >= 0);
760                 std::cerr << std::string(depth * 2, ' ');
761                 open = false;
762                 return std::cerr;
763         }
764         std::ostream &operator()(bool o)
765         {
766                 if (o && open) std::cerr << std::endl;
767                 if (!o) --depth;
768                 assert(depth >= 0);
769                 if (o || !open) std::cerr << std::string(depth * 2, ' ');
770                 if (o) ++depth;
771                 open = o;
772                 return std::cerr;
773         }
774 };
775
776 log debug;
777
778 struct log_auto_close
779 {
780         bool still_open;
781         log_auto_close(): still_open(true)
782         {
783         }
784         ~log_auto_close()
785         {
786                 if (debug.active && still_open) debug(false) << "done\n";
787         }
788 };
789
790 #define DEBUG if (debug.active) debug()
791 #define DEBUG_open log_auto_close auto_close; if (debug.active) debug(true)
792 #define DEBUG_close if ((auto_close.still_open = false), debug.active) debug(false)
793
794 /**
795  * Strong typedef for strings that need escaping.
796  * @note The string is stored as a reference, so the constructed object is
797  *       meant to be immediately consumed.
798  */
799 struct escape_string
800 {
801         std::string const &input;
802         escape_string(std::string const &s): input(s) {}
803 };
804
805 /**
806  * Write the string in @a se to @a out if it does not contain any special
807  * characters, a quoted and escaped string otherwise.
808  */
809 static std::ostream &operator<<(std::ostream &out, escape_string const &se)
810 {
811         std::string const &s = se.input;
812         char const *quoted_char = ",: '";
813         char const *escaped_char = "\"\\$!";
814         bool need_quotes = false;
815         char *buf = NULL;
816         size_t len = s.length(), last = 0, j = 0;
817         for (size_t i = 0; i < len; ++i)
818         {
819                 if (strchr(escaped_char, s[i]))
820                 {
821                         need_quotes = true;
822                         if (!buf) buf = new char[len * 2];
823                         memcpy(&buf[j], &s[last], i - last);
824                         j += i - last;
825                         buf[j++] = '\\';
826                         buf[j++] = s[i];
827                         last = i + 1;
828                 }
829                 if (!need_quotes && strchr(quoted_char, s[i]))
830                         need_quotes = true;
831         }
832         if (!need_quotes) return out << s;
833         out << '"';
834         if (!buf) return out << s << '"';
835         out.write(buf, j);
836         out.write(&s[last], len - last);
837         delete[] buf;
838         return out << '"';
839 }
840
841 /**
842  * @defgroup paths Path helpers
843  *
844  * @{
845  */
846
847 /**
848  * Initialize #working_dir.
849  */
850 static void init_working_dir()
851 {
852         char buf[1024];
853         char *res = getcwd(buf, sizeof(buf));
854         if (!res)
855         {
856                 perror("Failed to get working directory");
857                 exit(EXIT_FAILURE);
858         }
859         working_dir = buf;
860 #ifdef WINDOWS
861         for (size_t i = 0, l = working_dir.size(); i != l; ++i)
862         {
863                 if (working_dir[i] == '\\') working_dir[i] = '/';
864         }
865 #endif
866         prefix_dir = working_dir;
867 }
868
869 /**
870  * Initialize #prefix_dir and switch to it.
871  */
872 static void init_prefix_dir()
873 {
874         std::string old_prefix_dir = prefix_dir;
875         for (;;)
876         {
877                 struct stat s;
878                 if (stat((prefix_dir + "/Remakefile").c_str(), &s) == 0)
879                 {
880                         if (0 == chdir(prefix_dir.c_str()))
881                         {
882                                 if (old_prefix_dir != prefix_dir)
883                                 {
884                                         std::cout << "remake: Entering directory `"
885                                                   << prefix_dir << "'"
886                                                   << std::endl;
887                                         print_change_working_dir = true;
888                                 }
889                                 return;
890                         }
891                         else
892                         {
893                                 std::cerr << "Cannot change working directory to '" << prefix_dir << "'";
894                                 exit(EXIT_FAILURE);
895                         }
896                 }
897                 size_t pos = prefix_dir.find_last_of('/');
898                 if (pos == std::string::npos)
899                 {
900                         std::cerr << "Failed to locate Remakefile in the current directory or one of its parents" << std::endl;
901                         exit(EXIT_FAILURE);
902                 }
903                 prefix_dir.erase(pos);
904         }
905 }
906
907 /**
908  * Normalize an absolute path with respect to @a p.
909  * Paths outside the subtree are left unchanged.
910  */
911 static std::string normalize_abs(std::string const &s, std::string const &p)
912 {
913         size_t l = p.length();
914         if (s.compare(0, l, p)) return s;
915         size_t ll = s.length();
916         if (ll == l) return ".";
917         if (s[l] != '/')
918         {
919                 size_t pos = s.rfind('/', l);
920                 assert(pos != std::string::npos);
921                 return s.substr(pos + 1);
922         }
923         if (ll == l + 1) return ".";
924         return s.substr(l + 1);
925 }
926
927 /**
928  * Normalize path @a s (possibly relative to @a w) with respect to @a p.
929  *
930  * - If both @a p and @a w are empty, the function just removes ".", "..", "//".
931  * - If only @a p is empty, the function returns an absolute path.
932  */
933 static std::string normalize(std::string const &s, std::string const &w, std::string const &p)
934 {
935 #ifdef WINDOWS
936         char const *delim = "/\\";
937 #else
938         char delim = '/';
939 #endif
940         size_t pos = s.find_first_of(delim);
941         if (pos == std::string::npos && w == p) return s;
942         bool absolute = pos == 0;
943         if (!absolute && w != p && !w.empty())
944                 return normalize(w + '/' + s, w, p);
945         size_t prev = 0, len = s.length();
946         string_list l;
947         for (;;)
948         {
949                 if (pos != prev)
950                 {
951                         std::string n = s.substr(prev, pos - prev);
952                         if (n == "..")
953                         {
954                                 if (!l.empty()) l.pop_back();
955                                 else if (!absolute && !w.empty())
956                                         return normalize(w + '/' + s, w, p);
957                         }
958                         else if (n != ".")
959                                 l.push_back(n);
960                 }
961                 ++pos;
962                 if (pos >= len) break;
963                 prev = pos;
964                 pos = s.find_first_of(delim, prev);
965                 if (pos == std::string::npos) pos = len;
966         }
967         string_list::const_iterator i = l.begin(), i_end = l.end();
968         if (i == i_end) return absolute ? "/" : ".";
969         std::string n;
970         if (absolute) n.push_back('/');
971         n.append(*i);
972         for (++i; i != i_end; ++i)
973         {
974                 n.push_back('/');
975                 n.append(*i);
976         }
977         if (absolute && !p.empty()) return normalize_abs(n, p);
978         return n;
979 }
980
981 /**
982  * Normalize the content of a list of targets.
983  */
984 static void normalize_list(string_list &l, std::string const &w, std::string const &p)
985 {
986         for (string_list::iterator i = l.begin(),
987              i_end = l.end(); i != i_end; ++i)
988         {
989                 *i = normalize(*i, w, p);
990         }
991 }
992
993 /** @} */
994
995 /**
996  * @defgroup lexer Lexer
997  *
998  * @{
999  */
1000
1001 /**
1002  * Skip spaces.
1003  */
1004 static void skip_spaces(std::istream &in)
1005 {
1006         char c;
1007         while (strchr(" \t", (c = in.get()))) {}
1008         if (in.good()) in.putback(c);
1009 }
1010
1011 /**
1012  * Skip empty lines.
1013  */
1014 static void skip_empty(std::istream &in)
1015 {
1016         char c;
1017         while (strchr("\r\n", (c = in.get()))) {}
1018         if (in.good()) in.putback(c);
1019 }
1020
1021 /**
1022  * Skip end of line. If @a multi is true, skip the following empty lines too.
1023  * @return true if there was a line to end.
1024  */
1025 static bool skip_eol(std::istream &in, bool multi = false)
1026 {
1027         char c = in.get();
1028         if (c == '\r') c = in.get();
1029         if (c != '\n' && in.good()) in.putback(c);
1030         if (c != '\n' && !in.eof()) return false;
1031         if (multi) skip_empty(in);
1032         return true;
1033 }
1034
1035 enum
1036 {
1037   Unexpected = 0,
1038   Word       = 1 << 1,
1039   Colon      = 1 << 2,
1040   Equal      = 1 << 3,
1041   Dollarpar  = 1 << 4,
1042   Rightpar   = 1 << 5,
1043   Comma      = 1 << 6,
1044   Plusequal  = 1 << 7,
1045   Pipe       = 1 << 8,
1046 };
1047
1048 /**
1049  * Skip spaces and peek at the next token.
1050  * If it is one of @a mask, skip it (if it is not Word) and return it.
1051  * @note For composite tokens allowed by @a mask, input characters might
1052  *       have been eaten even for an Unexpected result.
1053  */
1054 static int expect_token(std::istream &in, int mask)
1055 {
1056         while (true)
1057         {
1058                 skip_spaces(in);
1059                 char c = in.peek();
1060                 if (!in.good()) return Unexpected;
1061                 int tok;
1062                 switch (c)
1063                 {
1064                 case '\r':
1065                 case '\n': return Unexpected;
1066                 case ':': tok = Colon; break;
1067                 case ',': tok = Comma; break;
1068                 case '=': tok = Equal; break;
1069                 case ')': tok = Rightpar; break;
1070                 case '|': tok = Pipe; break;
1071                 case '$':
1072                         if (!(mask & Dollarpar)) return Unexpected;
1073                         in.ignore(1);
1074                         tok = Dollarpar;
1075                         if (in.peek() != '(') return Unexpected;
1076                         break;
1077                 case '+':
1078                         if (!(mask & Plusequal)) return Unexpected;
1079                         in.ignore(1);
1080                         tok = Plusequal;
1081                         if (in.peek() != '=') return Unexpected;
1082                         break;
1083                 case '\\':
1084                         in.ignore(1);
1085                         if (skip_eol(in)) continue;
1086                         in.putback('\\');
1087                         return mask & Word ? Word : Unexpected;
1088                 default:
1089                         return mask & Word ? Word : Unexpected;
1090                 }
1091                 if (!(tok & mask)) return Unexpected;
1092                 in.ignore(1);
1093                 return tok;
1094         }
1095 }
1096
1097 /**
1098  * Read a (possibly quoted) word.
1099  */
1100 static std::string read_word(std::istream &in)
1101 {
1102         int c = in.get();
1103         std::string res;
1104         if (!in.good()) return res;
1105         char const *separators = " \t\r\n:$(),=+\"";
1106         bool quoted = c == '"';
1107         if (!quoted)
1108         {
1109                 if (strchr(separators, c))
1110                 {
1111                         in.putback(c);
1112                         return res;
1113                 }
1114                 res += c;
1115         }
1116         while (true)
1117         {
1118                 c = in.get();
1119                 if (!in.good()) return res;
1120                 if (quoted)
1121                 {
1122                         if (c == '\\')
1123                                 res += in.get();
1124                         else if (c == '"')
1125                                 return res;
1126                         else
1127                                 res += c;
1128                 }
1129                 else
1130                 {
1131                         if (strchr(separators, c))
1132                         {
1133                                 in.putback(c);
1134                                 return res;
1135                         }
1136                         res += c;
1137                 }
1138         }
1139 }
1140
1141 /** @} */
1142
1143 /**
1144  * @defgroup stream Token streams
1145  *
1146  * @{
1147  */
1148
1149 /**
1150  * Possible results from word producers.
1151  */
1152 enum input_status
1153 {
1154         Success,
1155         SyntaxError,
1156         Eof
1157 };
1158
1159 /**
1160  * Interface for word producers.
1161  */
1162 struct generator
1163 {
1164         virtual ~generator() {}
1165         virtual input_status next(std::string &) = 0;
1166 };
1167
1168 /**
1169  * Generator for the words of a variable.
1170  */
1171 struct variable_generator: generator
1172 {
1173         std::string name;
1174         string_list::const_iterator vcur, vend;
1175         variable_generator(std::string const &, variable_map const *);
1176         input_status next(std::string &);
1177 };
1178
1179 variable_generator::variable_generator(std::string const &n,
1180         variable_map const *local_variables): name(n)
1181 {
1182         if (local_variables)
1183         {
1184                 variable_map::const_iterator i = local_variables->find(name);
1185                 if (i != local_variables->end())
1186                 {
1187                         vcur = i->second.begin();
1188                         vend = i->second.end();
1189                         return;
1190                 }
1191         }
1192         variable_map::const_iterator i = variables.find(name);
1193         if (i == variables.end()) return;
1194         vcur = i->second.begin();
1195         vend = i->second.end();
1196 }
1197
1198 input_status variable_generator::next(std::string &res)
1199 {
1200         if (vcur != vend)
1201         {
1202                 res = *vcur;
1203                 ++vcur;
1204                 return Success;
1205         }
1206         return Eof;
1207 }
1208
1209 /**
1210  * Generator for the words of an input stream.
1211  */
1212 struct input_generator
1213 {
1214         std::istream &in;
1215         generator *nested;
1216         variable_map const *local_variables;
1217         bool earliest_exit, done;
1218         input_generator(std::istream &i, variable_map const *lv, bool e = false)
1219                 : in(i), nested(NULL), local_variables(lv), earliest_exit(e), done(false) {}
1220         input_status next(std::string &);
1221         ~input_generator() { assert(!nested); }
1222 };
1223
1224 static generator *get_function(input_generator const &, std::string const &);
1225
1226 input_status input_generator::next(std::string &res)
1227 {
1228         if (nested)
1229         {
1230                 restart:
1231                 input_status s = nested->next(res);
1232                 if (s == Success) return Success;
1233                 delete nested;
1234                 nested = NULL;
1235                 if (s == SyntaxError) return SyntaxError;
1236         }
1237         if (done) return Eof;
1238         if (earliest_exit) done = true;
1239         switch (expect_token(in, Word | Dollarpar))
1240         {
1241         case Word:
1242                 res = read_word(in);
1243                 return Success;
1244         case Dollarpar:
1245         {
1246                 std::string name = read_word(in);
1247                 if (name.empty()) return SyntaxError;
1248                 if (expect_token(in, Rightpar))
1249                         nested = new variable_generator(name, local_variables);
1250                 else
1251                 {
1252                         nested = get_function(*this, name);
1253                         if (!nested) return SyntaxError;
1254                 }
1255                 goto restart;
1256         }
1257         default:
1258                 return Eof;
1259         }
1260 }
1261
1262 /**
1263  * Read a list of words from an input generator.
1264  * @return false if a syntax error was encountered.
1265  */
1266 static bool read_words(input_generator &in, string_list &res)
1267 {
1268         while (true)
1269         {
1270                 res.push_back(std::string());
1271                 input_status s = in.next(res.back());
1272                 if (s == Success) continue;
1273                 res.pop_back();
1274                 return s == Eof;
1275         }
1276 }
1277
1278 static bool read_words(std::istream &in, string_list &res)
1279 {
1280         input_generator gen(in, NULL);
1281         return read_words(gen, res);
1282 }
1283
1284 /**
1285  * Generator for the result of function addprefix.
1286  */
1287 struct addprefix_generator: generator
1288 {
1289         input_generator gen;
1290         string_list pre;
1291         string_list::const_iterator prei;
1292         size_t prej, prel;
1293         std::string suf;
1294         addprefix_generator(input_generator const &, bool &);
1295         input_status next(std::string &);
1296 };
1297
1298 addprefix_generator::addprefix_generator(input_generator const &top, bool &ok)
1299         : gen(top.in, top.local_variables)
1300 {
1301         if (!read_words(gen, pre)) return;
1302         if (!expect_token(gen.in, Comma)) return;
1303         prej = 0;
1304         prel = pre.size();
1305         ok = true;
1306 }
1307
1308 input_status addprefix_generator::next(std::string &res)
1309 {
1310         if (prej)
1311         {
1312                 produce:
1313                 if (prej == prel)
1314                 {
1315                         res = *prei + suf;
1316                         prej = 0;
1317                 }
1318                 else
1319                 {
1320                         res = *prei++;
1321                         ++prej;
1322                 }
1323                 return Success;
1324         }
1325         switch (gen.next(res))
1326         {
1327         case Success:
1328                 if (!prel) return Success;
1329                 prei = pre.begin();
1330                 prej = 1;
1331                 suf = res;
1332                 goto produce;
1333         case Eof:
1334                 return expect_token(gen.in, Rightpar) ? Eof : SyntaxError;
1335         default:
1336                 return SyntaxError;
1337         }
1338 }
1339
1340 /**
1341  * Generator for the result of function addsuffix.
1342  */
1343 struct addsuffix_generator: generator
1344 {
1345         input_generator gen;
1346         string_list suf;
1347         string_list::const_iterator sufi;
1348         size_t sufj, sufl;
1349         std::string pre;
1350         addsuffix_generator(input_generator const &, bool &);
1351         input_status next(std::string &);
1352 };
1353
1354 addsuffix_generator::addsuffix_generator(input_generator const &top, bool &ok)
1355         : gen(top.in, top.local_variables)
1356 {
1357         if (!read_words(gen, suf)) return;
1358         if (!expect_token(gen.in, Comma)) return;
1359         sufj = 0;
1360         sufl = suf.size();
1361         ok = true;
1362 }
1363
1364 input_status addsuffix_generator::next(std::string &res)
1365 {
1366         if (sufj)
1367         {
1368                 if (sufj != sufl)
1369                 {
1370                         res = *sufi++;
1371                         ++sufj;
1372                         return Success;
1373                 }
1374                 sufj = 0;
1375         }
1376         switch (gen.next(res))
1377         {
1378         case Success:
1379                 if (!sufl) return Success;
1380                 sufi = suf.begin();
1381                 sufj = 1;
1382                 res += *sufi++;
1383                 return Success;
1384         case Eof:
1385                 return expect_token(gen.in, Rightpar) ? Eof : SyntaxError;
1386         default:
1387                 return SyntaxError;
1388         }
1389 }
1390
1391 /**
1392  * Return a generator for function @a name.
1393  */
1394 static generator *get_function(input_generator const &in, std::string const &name)
1395 {
1396         skip_spaces(in.in);
1397         generator *g = NULL;
1398         bool ok = false;
1399         if (name == "addprefix") g = new addprefix_generator(in, ok);
1400         else if (name == "addsuffix") g = new addsuffix_generator(in, ok);
1401         if (!g || ok) return g;
1402         delete g;
1403         return NULL;
1404 }
1405
1406 /** @} */
1407
1408 /**
1409  * @defgroup database Dependency database
1410  *
1411  * @{
1412  */
1413
1414 /**
1415  * Load dependencies from @a in.
1416  */
1417 static void load_dependencies(std::istream &in)
1418 {
1419         if (false)
1420         {
1421                 error:
1422                 std::cerr << "Failed to load database" << std::endl;
1423                 exit(EXIT_FAILURE);
1424         }
1425
1426         while (!in.eof())
1427         {
1428                 string_list targets;
1429                 if (!read_words(in, targets)) goto error;
1430                 if (in.eof()) return;
1431                 if (targets.empty()) goto error;
1432                 DEBUG << "reading dependencies of target " << targets.front() << std::endl;
1433                 if (in.get() != ':') goto error;
1434                 ref_ptr<dependency_t> dep;
1435                 dep->targets = targets;
1436                 string_list deps;
1437                 if (!read_words(in, deps)) goto error;
1438                 dep->deps.insert(deps.begin(), deps.end());
1439                 for (string_list::const_iterator i = targets.begin(),
1440                      i_end = targets.end(); i != i_end; ++i)
1441                 {
1442                         dependencies[*i] = dep;
1443                 }
1444                 skip_empty(in);
1445         }
1446 }
1447
1448 /**
1449  * Load known dependencies from file <tt>.remake</tt>.
1450  */
1451 static void load_dependencies()
1452 {
1453         DEBUG_open << "Loading database... ";
1454         std::ifstream in(".remake");
1455         if (!in.good())
1456         {
1457                 DEBUG_close << "not found\n";
1458                 return;
1459         }
1460         load_dependencies(in);
1461 }
1462
1463
1464 /**
1465  * Save all the dependencies in file <tt>.remake</tt>.
1466  */
1467 static void save_dependencies()
1468 {
1469         DEBUG_open << "Saving database... ";
1470         std::ofstream db(".remake");
1471         while (!dependencies.empty())
1472         {
1473                 ref_ptr<dependency_t> dep = dependencies.begin()->second;
1474                 for (string_list::const_iterator i = dep->targets.begin(),
1475                      i_end = dep->targets.end(); i != i_end; ++i)
1476                 {
1477                         db << escape_string(*i) << ' ';
1478                         dependencies.erase(*i);
1479                 }
1480                 db << ':';
1481                 for (string_set::const_iterator i = dep->deps.begin(),
1482                      i_end = dep->deps.end(); i != i_end; ++i)
1483                 {
1484                         db << ' ' << escape_string(*i);
1485                 }
1486                 db << std::endl;
1487         }
1488 }
1489
1490 /** @} */
1491
1492 static void merge_rule(rule_t &dest, rule_t const &src);
1493
1494 /**
1495  * @defgroup parser Rule parser
1496  *
1497  * @{
1498  */
1499
1500 /**
1501  * Register a specific rule with an empty script:
1502  *
1503  * - Check that none of the targets already has an associated rule with a
1504  *   nonempty script.
1505  * - Create a new rule with a single target for each target, if needed.
1506  * - Add the prerequisites of @a rule to all these associated rules.
1507  */
1508 static void register_transparent_rule(rule_t const &rule, string_list const &targets)
1509 {
1510         assert(rule.script.empty());
1511         for (string_list::const_iterator i = targets.begin(),
1512              i_end = targets.end(); i != i_end; ++i)
1513         {
1514                 std::pair<rule_map::iterator, bool> j =
1515                         specific_rules.insert(std::make_pair(*i, ref_ptr<rule_t>()));
1516                 ref_ptr<rule_t> &r = j.first->second;
1517                 if (j.second)
1518                 {
1519                         r = ref_ptr<rule_t>(rule);
1520                         r->targets = string_list(1, *i);
1521                         continue;
1522                 }
1523                 if (!r->script.empty())
1524                 {
1525                         std::cerr << "Failed to load rules: " << *i
1526                                 << " cannot be the target of several rules" << std::endl;
1527                         exit(EXIT_FAILURE);
1528                 }
1529                 assert(r->targets.size() == 1 && r->targets.front() == *i);
1530                 merge_rule(*r, rule);
1531         }
1532
1533         for (string_list::const_iterator i = targets.begin(),
1534              i_end = targets.end(); i != i_end; ++i)
1535         {
1536                 ref_ptr<dependency_t> &dep = dependencies[*i];
1537                 if (dep->targets.empty()) dep->targets.push_back(*i);
1538                 dep->deps.insert(rule.deps.begin(), rule.deps.end());
1539         }
1540 }
1541
1542 /**
1543  * Register a specific rule with a nonempty script:
1544  *
1545  * - Check that none of the targets already has an associated rule.
1546  * - Create a single shared rule and associate it to all the targets.
1547  * - Merge the prerequisites of all the targets into a single set and
1548  *   add the prerequisites of the rule to it. (The preexisting
1549  *   prerequisites, if any, come from a previous run.)
1550  */
1551 static void register_scripted_rule(rule_t const &rule)
1552 {
1553         ref_ptr<rule_t> r(rule);
1554         for (string_list::const_iterator i = rule.targets.begin(),
1555              i_end = rule.targets.end(); i != i_end; ++i)
1556         {
1557                 std::pair<rule_map::iterator, bool> j =
1558                         specific_rules.insert(std::make_pair(*i, r));
1559                 if (j.second) continue;
1560                 std::cerr << "Failed to load rules: " << *i
1561                         << " cannot be the target of several rules" << std::endl;
1562                 exit(EXIT_FAILURE);
1563         }
1564
1565         ref_ptr<dependency_t> dep;
1566         dep->targets = rule.targets;
1567         dep->deps.insert(rule.deps.begin(), rule.deps.end());
1568         for (string_list::const_iterator i = rule.targets.begin(),
1569              i_end = rule.targets.end(); i != i_end; ++i)
1570         {
1571                 ref_ptr<dependency_t> &d = dependencies[*i];
1572                 dep->deps.insert(d->deps.begin(), d->deps.end());
1573                 d = dep;
1574         }
1575 }
1576
1577 /**
1578  * Read a rule starting with target @a first, if nonempty.
1579  * Store into #generic_rules or #specific_rules depending on its genericity.
1580  */
1581 static void load_rule(std::istream &in, std::string const &first)
1582 {
1583         DEBUG_open << "Reading rule for target " << first << "... ";
1584         if (false)
1585         {
1586                 error:
1587                 DEBUG_close << "failed\n";
1588                 std::cerr << "Failed to load rules: syntax error" << std::endl;
1589                 exit(EXIT_FAILURE);
1590         }
1591         rule_t rule;
1592
1593         // Read targets and check genericity.
1594         string_list targets;
1595         if (!read_words(in, targets)) goto error;
1596         if (!first.empty()) targets.push_front(first);
1597         else if (targets.empty()) goto error;
1598         else DEBUG << "actual target: " << targets.front() << std::endl;
1599         bool generic = false;
1600         normalize_list(targets, "", "");
1601         for (string_list::const_iterator i = targets.begin(),
1602              i_end = targets.end(); i != i_end; ++i)
1603         {
1604                 if (i->empty()) goto error;
1605                 if ((i->find('%') != std::string::npos) != generic)
1606                 {
1607                         if (i == targets.begin()) generic = true;
1608                         else goto error;
1609                 }
1610         }
1611         std::swap(rule.targets, targets);
1612         skip_spaces(in);
1613         if (in.get() != ':') goto error;
1614
1615         bool assignment = false;
1616
1617         // Read dependencies.
1618         {
1619                 string_list v;
1620                 if (expect_token(in, Word))
1621                 {
1622                         std::string d = read_word(in);
1623                         if (int tok = expect_token(in, Equal | Plusequal))
1624                         {
1625                                 if (!read_words(in, v)) goto error;
1626                                 assign_t &a = rule.assigns[d];
1627                                 a.append = tok == Plusequal;
1628                                 a.value.swap(v);
1629                                 assignment = true;
1630                                 goto end_line;
1631                         }
1632                         v.push_back(d);
1633                 }
1634
1635                 if (!read_words(in, v)) goto error;
1636                 normalize_list(v, "", "");
1637                 rule.deps.swap(v);
1638
1639                 if (expect_token(in, Pipe))
1640                 {
1641                         if (!read_words(in, v)) goto error;
1642                         normalize_list(v, "", "");
1643                         rule.wdeps.swap(v);
1644                 }
1645         }
1646
1647         end_line:
1648         skip_spaces(in);
1649         if (!skip_eol(in, true)) goto error;
1650
1651         // Read script.
1652         std::ostringstream buf;
1653         while (true)
1654         {
1655                 char c = in.get();
1656                 if (!in.good()) break;
1657                 if (c == '\t' || c == ' ')
1658                 {
1659                         in.get(*buf.rdbuf());
1660                         if (in.fail() && !in.eof()) in.clear();
1661                 }
1662                 else if (c == '\r' || c == '\n')
1663                         buf << c;
1664                 else
1665                 {
1666                         in.putback(c);
1667                         break;
1668                 }
1669         }
1670         rule.script = buf.str();
1671
1672         // Add generic rules to the correct set.
1673         if (generic)
1674         {
1675                 if (assignment) goto error;
1676                 generic_rules.push_back(rule);
1677                 return;
1678         }
1679
1680         if (!rule.script.empty())
1681         {
1682                 if (assignment) goto error;
1683                 register_scripted_rule(rule);
1684         }
1685         else
1686         {
1687                 // Swap away the targets to avoid costly copies when registering.
1688                 string_list targets;
1689                 std::swap(rule.targets, targets);
1690                 register_transparent_rule(rule, targets);
1691                 std::swap(rule.targets, targets);
1692         }
1693
1694         // If there is no default target yet, mark it as such.
1695         if (first_target.empty())
1696                 first_target = rule.targets.front();
1697 }
1698
1699 /**
1700  * Load rules from @a remakefile.
1701  * If some rules have dependencies and non-generic targets, add these
1702  * dependencies to the targets.
1703  */
1704 static void load_rules(std::string const &remakefile)
1705 {
1706         DEBUG_open << "Loading rules... ";
1707         if (false)
1708         {
1709                 error:
1710                 std::cerr << "Failed to load rules: syntax error" << std::endl;
1711                 exit(EXIT_FAILURE);
1712         }
1713         std::ifstream in(remakefile.c_str());
1714         if (!in.good())
1715         {
1716                 std::cerr << "Failed to load rules: no Remakefile found" << std::endl;
1717                 exit(EXIT_FAILURE);
1718         }
1719         skip_empty(in);
1720
1721         string_list options;
1722
1723         // Read rules
1724         while (in.good())
1725         {
1726                 char c = in.peek();
1727                 if (c == '#')
1728                 {
1729                         while (in.get() != '\n') {}
1730                         skip_empty(in);
1731                         continue;
1732                 }
1733                 if (c == ' ' || c == '\t') goto error;
1734                 if (expect_token(in, Word))
1735                 {
1736                         std::string name = read_word(in);
1737                         if (name.empty()) goto error;
1738                         if (int tok = expect_token(in, Equal | Plusequal))
1739                         {
1740                                 DEBUG << "Assignment to variable " << name << std::endl;
1741                                 string_list value;
1742                                 if (!read_words(in, value)) goto error;
1743                                 string_list &dest =
1744                                         *(name == ".OPTIONS" ? &options : &variables[name]);
1745                                 if (tok == Equal) dest.swap(value);
1746                                 else dest.splice(dest.end(), value);
1747                                 if (!skip_eol(in, true)) goto error;
1748                         }
1749                         else load_rule(in, name);
1750                 }
1751                 else load_rule(in, std::string());
1752         }
1753
1754         // Set actual options.
1755         for (string_list::const_iterator i = options.begin(),
1756              i_end = options.end(); i != i_end; ++i)
1757         {
1758                 if (*i == "variable-propagation") propagate_vars = true;
1759                 else
1760                 {
1761                         std::cerr << "Failed to load rules: unrecognized option" << std::endl;
1762                         exit(EXIT_FAILURE);
1763                 }
1764         }
1765 }
1766
1767 /** @} */
1768
1769 /**
1770  * @defgroup rules Rule resolution
1771  *
1772  * @{
1773  */
1774
1775 static void merge_rule(rule_t &dest, rule_t const &src)
1776 {
1777         dest.deps.insert(dest.deps.end(), src.deps.begin(), src.deps.end());
1778         dest.wdeps.insert(dest.wdeps.end(), src.wdeps.begin(), src.wdeps.end());
1779         for (assign_map::const_iterator i = src.assigns.begin(),
1780              i_end = src.assigns.end(); i != i_end; ++i)
1781         {
1782                 if (!i->second.append)
1783                 {
1784                         new_assign:
1785                         dest.assigns[i->first] = i->second;
1786                         continue;
1787                 }
1788                 assign_map::iterator j = dest.assigns.find(i->first);
1789                 if (j == dest.assigns.end()) goto new_assign;
1790                 j->second.value.insert(j->second.value.end(),
1791                         i->second.value.begin(), i->second.value.end());
1792         }
1793 }
1794
1795 /**
1796  * Substitute a pattern into a list of strings.
1797  */
1798 static void substitute_pattern(std::string const &pat, string_list const &src, string_list &dst)
1799 {
1800         for (string_list::const_iterator i = src.begin(),
1801              i_end = src.end(); i != i_end; ++i)
1802         {
1803                 size_t pos = i->find('%');
1804                 if (pos == std::string::npos) dst.push_back(*i);
1805                 else dst.push_back(i->substr(0, pos) + pat + i->substr(pos + 1));
1806         }
1807 }
1808
1809 /**
1810  * Find a generic rule matching @a target:
1811  * - the one leading to shorter matches has priority,
1812  * - among equivalent rules, the earliest one has priority.
1813  */
1814 static void find_generic_rule(job_t &job, std::string const &target)
1815 {
1816         size_t tlen = target.length(), plen = tlen + 1;
1817         for (rule_list::const_iterator i = generic_rules.begin(),
1818              i_end = generic_rules.end(); i != i_end; ++i)
1819         {
1820                 for (string_list::const_iterator j = i->targets.begin(),
1821                      j_end = i->targets.end(); j != j_end; ++j)
1822                 {
1823                         size_t len = j->length();
1824                         if (tlen < len) continue;
1825                         if (plen <= tlen - (len - 1)) continue;
1826                         size_t pos = j->find('%');
1827                         if (pos == std::string::npos) continue;
1828                         size_t len2 = len - (pos + 1);
1829                         if (j->compare(0, pos, target, 0, pos) ||
1830                             j->compare(pos + 1, len2, target, tlen - len2, len2))
1831                                 continue;
1832                         plen = tlen - (len - 1);
1833                         job.stem = target.substr(pos, plen);
1834                         job.rule = rule_t();
1835                         job.rule.script = i->script;
1836                         substitute_pattern(job.stem, i->targets, job.rule.targets);
1837                         substitute_pattern(job.stem, i->deps, job.rule.deps);
1838                         substitute_pattern(job.stem, i->wdeps, job.rule.wdeps);
1839                         break;
1840                 }
1841         }
1842 }
1843
1844 /**
1845  * Find a specific rule matching @a target. Return a generic one otherwise.
1846  * If there is both a specific rule with an empty script and a generic rule, the
1847  * generic one is returned after adding the dependencies of the specific one.
1848  */
1849 static void find_rule(job_t &job, std::string const &target)
1850 {
1851         rule_map::const_iterator i = specific_rules.find(target),
1852                 i_end = specific_rules.end();
1853         // If there is a specific rule with a script, return it.
1854         if (i != i_end && !i->second->script.empty())
1855         {
1856                 job.rule = *i->second;
1857                 return;
1858         }
1859         find_generic_rule(job, target);
1860         // If there is no generic rule, return the specific rule (no script), if any.
1861         if (job.rule.targets.empty())
1862         {
1863                 if (i != i_end)
1864                 {
1865                         job.rule = *i->second;
1866                         return;
1867                 }
1868         }
1869         // Optimize the lookup when there is only one target (already looked up).
1870         if (job.rule.targets.size() == 1)
1871         {
1872                 if (i == i_end) return;
1873                 merge_rule(job.rule, *i->second);
1874                 return;
1875         }
1876         // Add the dependencies of the specific rules of every target to the
1877         // generic rule. If any of those rules has a nonempty script, error out.
1878         for (string_list::const_iterator j = job.rule.targets.begin(),
1879              j_end = job.rule.targets.end(); j != j_end; ++j)
1880         {
1881                 i = specific_rules.find(*j);
1882                 if (i == i_end) continue;
1883                 if (!i->second->script.empty()) return;
1884                 merge_rule(job.rule, *i->second);
1885         }
1886 }
1887
1888 /** @} */
1889
1890 /**
1891  * @defgroup status Target status
1892  *
1893  * @{
1894  */
1895
1896 /**
1897  * Compute and memoize the status of @a target:
1898  * - if the file does not exist, the target is obsolete,
1899  * - if any dependency is obsolete or younger than the file, it is obsolete,
1900  * - otherwise it is up-to-date.
1901  *
1902  * @note For rules with multiple targets, all the targets share the same
1903  *       status. (If one is obsolete, they all are.) The second rule above
1904  *       is modified in that case: the latest target is chosen, not the oldest!
1905  */
1906 static status_t const &get_status(std::string const &target)
1907 {
1908         std::pair<status_map::iterator,bool> i =
1909                 status.insert(std::make_pair(target, status_t()));
1910         status_t &ts = i.first->second;
1911         if (!i.second) return ts;
1912         DEBUG_open << "Checking status of " << target << "... ";
1913         dependency_map::const_iterator j = dependencies.find(target);
1914         if (j == dependencies.end())
1915         {
1916                 struct stat s;
1917                 if (stat(target.c_str(), &s) != 0)
1918                 {
1919                         DEBUG_close << "missing\n";
1920                         ts.status = Todo;
1921                         ts.last = 0;
1922                         return ts;
1923                 }
1924                 DEBUG_close << "up-to-date\n";
1925                 ts.status = Uptodate;
1926                 ts.last = s.st_mtime;
1927                 return ts;
1928         }
1929         dependency_t const &dep = *j->second;
1930         status_e st = Uptodate;
1931         time_t latest = 0;
1932         for (string_list::const_iterator k = dep.targets.begin(),
1933              k_end = dep.targets.end(); k != k_end; ++k)
1934         {
1935                 struct stat s;
1936                 if (stat(k->c_str(), &s) != 0)
1937                 {
1938                         if (st == Uptodate) DEBUG_close << *k << " missing\n";
1939                         s.st_mtime = 0;
1940                         st = Todo;
1941                 }
1942                 status[*k].last = s.st_mtime;
1943                 if (s.st_mtime > latest) latest = s.st_mtime;
1944         }
1945         if (st != Uptodate) goto update;
1946         for (string_set::const_iterator k = dep.deps.begin(),
1947              k_end = dep.deps.end(); k != k_end; ++k)
1948         {
1949                 status_t const &ts_ = get_status(*k);
1950                 if (latest < ts_.last)
1951                 {
1952                         DEBUG_close << "older than " << *k << std::endl;
1953                         st = Todo;
1954                         goto update;
1955                 }
1956                 if (ts_.status != Uptodate && st != Recheck)
1957                 {
1958                         DEBUG << "obsolete dependency " << *k << std::endl;
1959                         st = Recheck;
1960                 }
1961         }
1962         if (st == Uptodate) DEBUG_close << "all siblings up-to-date\n";
1963         update:
1964         for (string_list::const_iterator k = dep.targets.begin(),
1965              k_end = dep.targets.end(); k != k_end; ++k)
1966         {
1967                 status[*k].status = st;
1968         }
1969         return ts;
1970 }
1971
1972 /**
1973  * Change the status of @a target to #Remade or #Uptodate depending on whether
1974  * its modification time changed.
1975  */
1976 static void update_status(std::string const &target)
1977 {
1978         DEBUG_open << "Rechecking status of " << target << "... ";
1979         status_map::iterator i = status.find(target);
1980         assert(i != status.end());
1981         status_t &ts = i->second;
1982         ts.status = Remade;
1983         if (ts.last >= now)
1984         {
1985                 DEBUG_close << "possibly remade\n";
1986                 return;
1987         }
1988         struct stat s;
1989         if (stat(target.c_str(), &s) != 0)
1990         {
1991                 DEBUG_close << "missing\n";
1992                 ts.last = 0;
1993         }
1994         else if (s.st_mtime != ts.last)
1995         {
1996                 DEBUG_close << "remade\n";
1997                 ts.last = s.st_mtime;
1998         }
1999         else
2000         {
2001                 DEBUG_close << "unchanged\n";
2002                 ts.status = Uptodate;
2003         }
2004 }
2005
2006 /**
2007  * Check whether all the prerequisites of @a target ended being up-to-date.
2008  */
2009 static bool still_need_rebuild(std::string const &target)
2010 {
2011         DEBUG_open << "Rechecking obsoleteness of " << target << "... ";
2012         status_map::const_iterator i = status.find(target);
2013         assert(i != status.end());
2014         if (i->second.status != Recheck) return true;
2015         dependency_map::const_iterator j = dependencies.find(target);
2016         assert(j != dependencies.end());
2017         dependency_t const &dep = *j->second;
2018         for (string_set::const_iterator k = dep.deps.begin(),
2019              k_end = dep.deps.end(); k != k_end; ++k)
2020         {
2021                 if (status[*k].status != Uptodate) return true;
2022         }
2023         for (string_list::const_iterator k = dep.targets.begin(),
2024              k_end = dep.targets.end(); k != k_end; ++k)
2025         {
2026                 status[*k].status = Uptodate;
2027         }
2028         DEBUG_close << "no longer obsolete\n";
2029         return false;
2030 }
2031
2032 /** @} */
2033
2034 /**
2035  * @defgroup server Server
2036  *
2037  * @{
2038  */
2039
2040 /**
2041  * Handle job completion.
2042  */
2043 static void complete_job(int job_id, bool success)
2044 {
2045         DEBUG_open << "Completing job " << job_id << "... ";
2046         job_map::iterator i = jobs.find(job_id);
2047         assert(i != jobs.end());
2048         string_list const &targets = i->second.rule.targets;
2049         if (success)
2050         {
2051                 for (string_list::const_iterator j = targets.begin(),
2052                      j_end = targets.end(); j != j_end; ++j)
2053                 {
2054                         update_status(*j);
2055                 }
2056         }
2057         else
2058         {
2059                 DEBUG_close << "failed\n";
2060                 std::cerr << "Failed to build";
2061                 for (string_list::const_iterator j = targets.begin(),
2062                      j_end = targets.end(); j != j_end; ++j)
2063                 {
2064                         status[*j].status = Failed;
2065                         std::cerr << ' ' << *j;
2066                         remove(j->c_str());
2067                 }
2068                 std::cerr << std::endl;
2069         }
2070         jobs.erase(i);
2071 }
2072
2073 /**
2074  * Return the script obtained by substituting variables.
2075  */
2076 static std::string prepare_script(job_t const &job)
2077 {
2078         std::string const &s = job.rule.script;
2079         std::istringstream in(s);
2080         std::ostringstream out;
2081         size_t len = s.size();
2082
2083         while (!in.eof())
2084         {
2085                 size_t pos = in.tellg(), p = s.find('$', pos);
2086                 if (p == std::string::npos || p == len - 1) p = len;
2087                 out.write(&s[pos], p - pos);
2088                 if (p == len) break;
2089                 ++p;
2090                 switch (s[p])
2091                 {
2092                 case '$':
2093                         out << '$';
2094                         in.seekg(p + 1);
2095                         break;
2096                 case '<':
2097                         if (!job.rule.deps.empty())
2098                                 out << job.rule.deps.front();
2099                         in.seekg(p + 1);
2100                         break;
2101                 case '^':
2102                 {
2103                         bool first = true;
2104                         for (string_list::const_iterator i = job.rule.deps.begin(),
2105                              i_end = job.rule.deps.end(); i != i_end; ++i)
2106                         {
2107                                 if (first) first = false;
2108                                 else out << ' ';
2109                                 out << *i;
2110                         }
2111                         in.seekg(p + 1);
2112                         break;
2113                 }
2114                 case '@':
2115                         assert(!job.rule.targets.empty());
2116                         out << job.rule.targets.front();
2117                         in.seekg(p + 1);
2118                         break;
2119                 case '*':
2120                         out << job.stem;
2121                         in.seekg(p + 1);
2122                         break;
2123                 case '(':
2124                 {
2125                         in.seekg(p - 1);
2126                         bool first = true;
2127                         input_generator gen(in, &job.vars, true);
2128                         while (true)
2129                         {
2130                                 std::string w;
2131                                 input_status s = gen.next(w);
2132                                 if (s == SyntaxError)
2133                                 {
2134                                         // TODO
2135                                         return "false";
2136                                 }
2137                                 if (s == Eof) break;
2138                                 if (first) first = false;
2139                                 else out << ' ';
2140                                 out << w;
2141                         }
2142                         break;
2143                 }
2144                 default:
2145                         // Let dollars followed by an unrecognized character
2146                         // go through. This differs from Make, which would
2147                         // use a one-letter variable.
2148                         out << '$';
2149                         in.seekg(p);
2150                 }
2151         }
2152
2153         return out.str();
2154 }
2155
2156 /**
2157  * Execute the script from @a rule.
2158  */
2159 static status_e run_script(int job_id, job_t const &job)
2160 {
2161         if (show_targets)
2162         {
2163                 std::cout << "Building";
2164                 for (string_list::const_iterator i = job.rule.targets.begin(),
2165                      i_end = job.rule.targets.end(); i != i_end; ++i)
2166                 {
2167                         std::cout << ' ' << *i;
2168                 }
2169                 std::cout << std::endl;
2170         }
2171
2172         ref_ptr<dependency_t> dep;
2173         dep->targets = job.rule.targets;
2174         dep->deps.insert(job.rule.deps.begin(), job.rule.deps.end());
2175         for (string_list::const_iterator i = job.rule.targets.begin(),
2176              i_end = job.rule.targets.end(); i != i_end; ++i)
2177         {
2178                 dependencies[*i] = dep;
2179         }
2180
2181         std::string script = prepare_script(job);
2182
2183         std::ostringstream job_id_buf;
2184         job_id_buf << job_id;
2185         std::string job_id_ = job_id_buf.str();
2186
2187         DEBUG_open << "Starting script for job " << job_id << "... ";
2188         if (script.empty())
2189         {
2190                 DEBUG_close << "no script\n";
2191                 complete_job(job_id, true);
2192                 return Remade;
2193         }
2194
2195         if (false)
2196         {
2197                 error:
2198                 DEBUG_close << "failed\n";
2199                 complete_job(job_id, false);
2200                 return Failed;
2201         }
2202
2203 #ifdef WINDOWS
2204         HANDLE pfd[2];
2205         if (false)
2206         {
2207                 error2:
2208                 CloseHandle(pfd[0]);
2209                 CloseHandle(pfd[1]);
2210                 goto error;
2211         }
2212         if (!CreatePipe(&pfd[0], &pfd[1], NULL, 0))
2213                 goto error;
2214         if (!SetHandleInformation(pfd[0], HANDLE_FLAG_INHERIT, HANDLE_FLAG_INHERIT))
2215                 goto error2;
2216         STARTUPINFO si;
2217         ZeroMemory(&si, sizeof(STARTUPINFO));
2218         si.cb = sizeof(STARTUPINFO);
2219         si.hStdError = GetStdHandle(STD_ERROR_HANDLE);
2220         si.hStdOutput = GetStdHandle(STD_OUTPUT_HANDLE);
2221         si.hStdInput = pfd[0];
2222         si.dwFlags |= STARTF_USESTDHANDLES;
2223         PROCESS_INFORMATION pi;
2224         ZeroMemory(&pi, sizeof(PROCESS_INFORMATION));
2225         if (!SetEnvironmentVariable("REMAKE_JOB_ID", job_id_.c_str()))
2226                 goto error2;
2227         char const *argv = echo_scripts ? "SH.EXE -e -s -v" : "SH.EXE -e -s";
2228         if (!CreateProcess(NULL, (char *)argv, NULL, NULL,
2229             true, 0, NULL, NULL, &si, &pi))
2230         {
2231                 goto error2;
2232         }
2233         CloseHandle(pi.hThread);
2234         DWORD len = script.length(), wlen;
2235         if (!WriteFile(pfd[1], script.c_str(), len, &wlen, NULL) || wlen < len)
2236                 std::cerr << "Unexpected failure while sending script to shell" << std::endl;
2237         CloseHandle(pfd[0]);
2238         CloseHandle(pfd[1]);
2239         ++running_jobs;
2240         job_pids[pi.hProcess] = job_id;
2241         return Running;
2242 #else
2243         int pfd[2];
2244         if (false)
2245         {
2246                 error2:
2247                 close(pfd[0]);
2248                 close(pfd[1]);
2249                 goto error;
2250         }
2251         if (pipe(pfd) == -1)
2252                 goto error;
2253         if (setenv("REMAKE_JOB_ID", job_id_.c_str(), 1))
2254                 goto error2;
2255         if (pid_t pid = vfork())
2256         {
2257                 if (pid == -1) goto error2;
2258                 ssize_t len = script.length();
2259                 if (write(pfd[1], script.c_str(), len) < len)
2260                         std::cerr << "Unexpected failure while sending script to shell" << std::endl;
2261                 close(pfd[0]);
2262                 close(pfd[1]);
2263                 ++running_jobs;
2264                 job_pids[pid] = job_id;
2265                 return Running;
2266         }
2267         // Child process starts here. Notice the use of vfork above.
2268         char const *argv[5] = { "sh", "-e", "-s", NULL, NULL };
2269         if (echo_scripts) argv[3] = "-v";
2270         close(pfd[1]);
2271         if (pfd[0] != 0)
2272         {
2273                 dup2(pfd[0], 0);
2274                 close(pfd[0]);
2275         }
2276         execve("/bin/sh", (char **)argv, environ);
2277         _exit(EXIT_FAILURE);
2278 #endif
2279 }
2280
2281 /**
2282  * Create a job for @a target according to the loaded rules.
2283  * Mark all the targets from the rule as running and reset their dependencies.
2284  * Inherit variables from @a current, if enabled.
2285  * If the rule has dependencies, create a new client to build them just
2286  * before @a current, and change @a current so that it points to it.
2287  */
2288 static status_e start(std::string const &target, client_list::iterator &current)
2289 {
2290         int job_id = job_counter++;
2291         DEBUG_open << "Starting job " << job_id << " for " << target << "... ";
2292         job_t &job = jobs[job_id];
2293         find_rule(job, target);
2294         if (job.rule.targets.empty())
2295         {
2296                 status[target].status = Failed;
2297                 DEBUG_close << "failed\n";
2298                 std::cerr << "No rule for building " << target << std::endl;
2299                 return Failed;
2300         }
2301         for (string_list::const_iterator i = job.rule.targets.begin(),
2302              i_end = job.rule.targets.end(); i != i_end; ++i)
2303         {
2304                 status[*i].status = Running;
2305         }
2306         if (propagate_vars) job.vars = current->vars;
2307         for (assign_map::const_iterator i = job.rule.assigns.begin(),
2308              i_end = job.rule.assigns.end(); i != i_end; ++i)
2309         {
2310                 std::pair<variable_map::iterator, bool> k =
2311                         job.vars.insert(std::make_pair(i->first, string_list()));
2312                 string_list &v = k.first->second;
2313                 if (i->second.append)
2314                 {
2315                         if (k.second)
2316                         {
2317                                 variable_map::const_iterator j = variables.find(i->first);
2318                                 if (j != variables.end()) v = j->second;
2319                         }
2320                 }
2321                 else if (!k.second) v.clear();
2322                 v.insert(v.end(), i->second.value.begin(), i->second.value.end());
2323         }
2324         if (!job.rule.deps.empty() || !job.rule.wdeps.empty())
2325         {
2326                 current = clients.insert(current, client_t());
2327                 current->job_id = job_id;
2328                 current->pending = job.rule.deps;
2329                 current->pending.insert(current->pending.end(),
2330                         job.rule.wdeps.begin(), job.rule.wdeps.end());
2331                 if (propagate_vars) current->vars = job.vars;
2332                 current->delayed = true;
2333                 return Recheck;
2334         }
2335         return run_script(job_id, job);
2336 }
2337
2338 /**
2339  * Send a reply to a client then remove it.
2340  * If the client was a dependency client, start the actual script.
2341  */
2342 static void complete_request(client_t &client, bool success)
2343 {
2344         DEBUG_open << "Completing request from client of job " << client.job_id << "... ";
2345         if (client.delayed)
2346         {
2347                 assert(client.socket == INVALID_SOCKET);
2348                 if (success)
2349                 {
2350                         job_map::const_iterator i = jobs.find(client.job_id);
2351                         assert(i != jobs.end());
2352                         if (still_need_rebuild(i->second.rule.targets.front()))
2353                                 run_script(client.job_id, i->second);
2354                         else complete_job(client.job_id, true);
2355                 }
2356                 else complete_job(client.job_id, false);
2357         }
2358         else if (client.socket != INVALID_SOCKET)
2359         {
2360                 char res = success ? 1 : 0;
2361                 send(client.socket, &res, 1, MSG_NOSIGNAL);
2362         #ifdef WINDOWS
2363                 closesocket(client.socket);
2364         #else
2365                 close(client.socket);
2366         #endif
2367                 --waiting_jobs;
2368         }
2369
2370         if (client.job_id < 0 && !success) build_failure = true;
2371 }
2372
2373 /**
2374  * Return whether there are slots for starting new jobs.
2375  */
2376 static bool has_free_slots()
2377 {
2378         if (max_active_jobs <= 0) return true;
2379         return running_jobs - waiting_jobs < max_active_jobs;
2380 }
2381
2382 /**
2383  * Handle client requests:
2384  * - check for running targets that have finished,
2385  * - start as many pending targets as allowed,
2386  * - complete the request if there are neither running nor pending targets
2387  *   left or if any of them failed.
2388  *
2389  * @return true if some child processes are still running.
2390  *
2391  * @post If there are pending requests, at least one child process is running.
2392  *
2393  * @invariant New free slots cannot appear during a run, since the only way to
2394  *            decrease #running_jobs is #finalize_job and the only way to
2395  *            increase #waiting_jobs is #accept_client. None of these functions
2396  *            are called during a run. So breaking out as soon as there no free
2397  *            slots left is fine.
2398  */
2399 static bool handle_clients()
2400 {
2401         DEBUG_open << "Handling client requests... ";
2402         restart:
2403         bool need_restart = false;
2404
2405         for (client_list::iterator i = clients.begin(), i_next = i,
2406              i_end = clients.end(); i != i_end && has_free_slots(); i = i_next)
2407         {
2408                 ++i_next;
2409                 DEBUG_open << "Handling client from job " << i->job_id << "... ";
2410
2411                 // Remove running targets that have finished.
2412                 for (string_set::iterator j = i->running.begin(), j_next = j,
2413                      j_end = i->running.end(); j != j_end; j = j_next)
2414                 {
2415                         ++j_next;
2416                         status_map::const_iterator k = status.find(*j);
2417                         assert(k != status.end());
2418                         switch (k->second.status)
2419                         {
2420                         case Running:
2421                                 break;
2422                         case Failed:
2423                                 i->failed = true;
2424                                 if (!keep_going) goto complete;
2425                                 // no break
2426                         case Uptodate:
2427                         case Remade:
2428                                 i->running.erase(j);
2429                                 break;
2430                         case Recheck:
2431                         case Todo:
2432                                 assert(false);
2433                         }
2434                 }
2435
2436                 // Start pending targets.
2437                 while (!i->pending.empty())
2438                 {
2439                         std::string target = i->pending.front();
2440                         i->pending.pop_front();
2441                         switch (get_status(target).status)
2442                         {
2443                         case Running:
2444                                 i->running.insert(target);
2445                                 break;
2446                         case Failed:
2447                                 pending_failed:
2448                                 i->failed = true;
2449                                 if (!keep_going) goto complete;
2450                                 // no break
2451                         case Uptodate:
2452                         case Remade:
2453                                 break;
2454                         case Recheck:
2455                         case Todo:
2456                                 client_list::iterator j = i;
2457                                 switch (start(target, i))
2458                                 {
2459                                 case Failed:
2460                                         goto pending_failed;
2461                                 case Running:
2462                                         // A shell was started, check for free slots.
2463                                         j->running.insert(target);
2464                                         if (!has_free_slots()) return true;
2465                                         break;
2466                                 case Recheck:
2467                                         // Switch to the dependency client that was inserted.
2468                                         j->running.insert(target);
2469                                         i_next = j;
2470                                         break;
2471                                 case Remade:
2472                                         // Nothing to run.
2473                                         need_restart = true;
2474                                         break;
2475                                 default:
2476                                         assert(false);
2477                                 }
2478                         }
2479                 }
2480
2481                 // Try to complete the request.
2482                 // (This might start a new job if it was a dependency client.)
2483                 if (i->running.empty() || i->failed)
2484                 {
2485                         complete:
2486                         complete_request(*i, !i->failed);
2487                         DEBUG_close << (i->failed ? "failed\n" : "finished\n");
2488                         clients.erase(i);
2489                         need_restart = true;
2490                 }
2491         }
2492
2493         if (running_jobs != waiting_jobs) return true;
2494         if (running_jobs == 0 && clients.empty()) return false;
2495         if (need_restart) goto restart;
2496
2497         // There is a circular dependency.
2498         // Try to break it by completing one of the requests.
2499         assert(!clients.empty());
2500         std::cerr << "Circular dependency detected" << std::endl;
2501         client_list::iterator i = clients.begin();
2502         complete_request(*i, false);
2503         clients.erase(i);
2504         goto restart;
2505 }
2506
2507 /**
2508  * Create a named unix socket that listens for build requests. Also set
2509  * the REMAKE_SOCKET environment variable that will be inherited by all
2510  * the job scripts.
2511  */
2512 static void create_server()
2513 {
2514         if (false)
2515         {
2516                 error:
2517                 perror("Failed to create server");
2518 #ifndef WINDOWS
2519                 error2:
2520 #endif
2521                 exit(EXIT_FAILURE);
2522         }
2523         DEBUG_open << "Creating server... ";
2524
2525 #ifdef WINDOWS
2526         // Prepare a windows socket.
2527         struct sockaddr_in socket_addr;
2528         socket_addr.sin_family = AF_INET;
2529         socket_addr.sin_addr.s_addr = inet_addr("127.0.0.1");
2530         socket_addr.sin_port = 0;
2531
2532         // Create and listen to the socket.
2533         socket_fd = socket(AF_INET, SOCK_STREAM, 0);
2534         if (socket_fd == INVALID_SOCKET) goto error;
2535         if (!SetHandleInformation((HANDLE)socket_fd, HANDLE_FLAG_INHERIT, 0))
2536                 goto error;
2537         if (bind(socket_fd, (struct sockaddr *)&socket_addr, sizeof(sockaddr_in)))
2538                 goto error;
2539         int len = sizeof(sockaddr_in);
2540         if (getsockname(socket_fd, (struct sockaddr *)&socket_addr, &len))
2541                 goto error;
2542         std::ostringstream buf;
2543         buf << socket_addr.sin_port;
2544         if (!SetEnvironmentVariable("REMAKE_SOCKET", buf.str().c_str()))
2545                 goto error;
2546         if (listen(socket_fd, 1000)) goto error;
2547 #else
2548         // Set signal handlers for SIGCHLD and SIGINT.
2549         // Block SIGCHLD (unblocked during select).
2550         sigset_t sigmask;
2551         sigemptyset(&sigmask);
2552         sigaddset(&sigmask, SIGCHLD);
2553         if (sigprocmask(SIG_BLOCK, &sigmask, NULL) == -1) goto error;
2554         struct sigaction sa;
2555         sa.sa_flags = 0;
2556         sigemptyset(&sa.sa_mask);
2557         sa.sa_handler = &sigchld_handler;
2558         if (sigaction(SIGCHLD, &sa, NULL) == -1) goto error;
2559         sa.sa_handler = &sigint_handler;
2560         if (sigaction(SIGINT, &sa, NULL) == -1) goto error;
2561
2562         // Prepare a named unix socket in temporary directory.
2563         struct stat tmpstat;
2564         char * tmpdir;
2565         if ((0 == stat(tmpdir = getenv("TMPDIR"), &tmpstat)
2566              && (S_ISDIR(tmpstat.st_mode)))
2567             || ((0 == stat(tmpdir = (char*)P_tmpdir, &tmpstat))
2568                 && (S_ISDIR(tmpstat.st_mode)))
2569             || ((0 == stat(tmpdir = (char*)"/tmp", &tmpstat))
2570                 && (S_ISDIR(tmpstat.st_mode))));
2571         else goto error;
2572         std::stringstream tmpname;
2573         long int rnd = now ^ getpid();
2574         do
2575         {
2576                 srandom(rnd);
2577                 rnd = random();
2578                 tmpname << tmpdir << "/rmk-";
2579                 tmpname.fill('0');
2580                 tmpname.width(8);
2581                 tmpname << rnd;
2582         }
2583         while (access (tmpname.str().c_str(), F_OK) == 0);
2584         socket_name = strdup(tmpname.str().c_str());
2585         if (!socket_name) goto error2;
2586         struct sockaddr_un socket_addr;
2587         size_t len = strlen(socket_name);
2588         if (len >= sizeof(socket_addr.sun_path) - 1) goto error2;
2589         socket_addr.sun_family = AF_UNIX;
2590         strcpy(socket_addr.sun_path, socket_name);
2591         len += sizeof(socket_addr.sun_family);
2592         if (setenv("REMAKE_SOCKET", socket_name, 1)) goto error;
2593
2594         // Create and listen to the socket.
2595 #ifdef LINUX
2596         socket_fd = socket(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0);
2597         if (socket_fd == INVALID_SOCKET) goto error;
2598 #else
2599         socket_fd = socket(AF_UNIX, SOCK_STREAM, 0);
2600         if (socket_fd == INVALID_SOCKET) goto error;
2601         if (fcntl(socket_fd, F_SETFD, FD_CLOEXEC) < 0) goto error;
2602 #endif
2603         if (bind(socket_fd, (struct sockaddr *)&socket_addr, len))
2604                 goto error;
2605         if (listen(socket_fd, 1000)) goto error;
2606 #endif
2607 }
2608
2609 /**
2610  * Accept a connection from a client, get the job it spawned from,
2611  * get the targets, and mark them as dependencies of the job targets.
2612  */
2613 static void accept_client()
2614 {
2615         DEBUG_open << "Handling client request... ";
2616
2617         // Accept connection.
2618 #ifdef WINDOWS
2619         socket_t fd = accept(socket_fd, NULL, NULL);
2620         if (fd == INVALID_SOCKET) return;
2621         if (!SetHandleInformation((HANDLE)fd, HANDLE_FLAG_INHERIT, 0))
2622         {
2623                 error2:
2624                 std::cerr << "Unexpected failure while setting connection with client" << std::endl;
2625                 closesocket(fd);
2626                 return;
2627         }
2628         // WSAEventSelect puts sockets into nonblocking mode, so disable it here.
2629         u_long nbio = 0;
2630         if (ioctlsocket(fd, FIONBIO, &nbio)) goto error2;
2631 #elif defined(LINUX)
2632         int fd = accept4(socket_fd, NULL, NULL, SOCK_CLOEXEC);
2633         if (fd < 0) return;
2634 #else
2635         int fd = accept(socket_fd, NULL, NULL);
2636         if (fd < 0) return;
2637         if (fcntl(fd, F_SETFD, FD_CLOEXEC) < 0) return;
2638 #endif
2639         clients.push_front(client_t());
2640         client_list::iterator proc = clients.begin();
2641
2642         if (false)
2643         {
2644                 error:
2645                 DEBUG_close << "failed\n";
2646                 std::cerr << "Received an ill-formed client message" << std::endl;
2647         #ifdef WINDOWS
2648                 closesocket(fd);
2649         #else
2650                 close(fd);
2651         #endif
2652                 clients.erase(proc);
2653                 return;
2654         }
2655
2656         // Receive message. Stop when encountering two nuls in a row.
2657         std::vector<char> buf;
2658         size_t len = 0;
2659         while (len < sizeof(int) + 2 || buf[len - 1] || buf[len - 2])
2660         {
2661                 buf.resize(len + 1024);
2662                 ssize_t l = recv(fd, &buf[0] + len, 1024, 0);
2663                 if (l <= 0) goto error;
2664                 len += l;
2665         }
2666
2667         // Parse job that spawned the client.
2668         int job_id;
2669         memcpy(&job_id, &buf[0], sizeof(int));
2670         proc->socket = fd;
2671         proc->job_id = job_id;
2672         job_map::const_iterator i = jobs.find(job_id);
2673         if (i == jobs.end()) goto error;
2674         DEBUG << "receiving request from job " << job_id << std::endl;
2675         if (propagate_vars) proc->vars = i->second.vars;
2676
2677         // Parse the targets and the variable assignments.
2678         // Mark the targets as dependencies of the job targets.
2679         dependency_t &dep = *dependencies[i->second.rule.targets.front()];
2680         string_list *last_var = NULL;
2681         char const *p = &buf[0] + sizeof(int);
2682         while (true)
2683         {
2684                 len = strlen(p);
2685                 if (len == 0)
2686                 {
2687                         ++waiting_jobs;
2688                         break;
2689                 }
2690                 switch (*p)
2691                 {
2692                 case 'T':
2693                 {
2694                         if (len == 1) goto error;
2695                         std::string target(p + 1, p + len);
2696                         DEBUG << "adding dependency " << target << " to job\n";
2697                         proc->pending.push_back(target);
2698                         dep.deps.insert(target);
2699                         break;
2700                 }
2701                 case 'V':
2702                 {
2703                         if (len == 1) goto error;
2704                         std::string var(p + 1, p + len);
2705                         DEBUG << "adding variable " << var << " to job\n";
2706                         last_var = &proc->vars[var];
2707                         last_var->clear();
2708                         break;
2709                 }
2710                 case 'W':
2711                 {
2712                         if (!last_var) goto error;
2713                         last_var->push_back(std::string(p + 1, p + len));
2714                         break;
2715                 }
2716                 default:
2717                         goto error;
2718                 }
2719                 p += len + 1;
2720         }
2721
2722         if (!propagate_vars && !proc->vars.empty())
2723         {
2724                 std::cerr << "Assignments are ignored unless 'variable-propagation' is enabled" << std::endl;
2725                 proc->vars.clear();
2726         }
2727 }
2728
2729 /**
2730  * Handle child process exit status.
2731  */
2732 static void finalize_job(pid_t pid, bool res)
2733 {
2734         pid_job_map::iterator i = job_pids.find(pid);
2735         assert(i != job_pids.end());
2736         int job_id = i->second;
2737         job_pids.erase(i);
2738         --running_jobs;
2739         complete_job(job_id, res);
2740 }
2741
2742 /**
2743  * Loop until all the jobs have finished.
2744  *
2745  * @post There are no client requests left, not even virtual ones.
2746  */
2747 static void server_loop()
2748 {
2749         while (handle_clients())
2750         {
2751                 DEBUG_open << "Handling events... ";
2752         #ifdef WINDOWS
2753                 size_t len = job_pids.size() + 1;
2754                 HANDLE h[len];
2755                 int num = 0;
2756                 for (pid_job_map::const_iterator i = job_pids.begin(),
2757                      i_end = job_pids.end(); i != i_end; ++i, ++num)
2758                 {
2759                         h[num] = i->first;
2760                 }
2761                 WSAEVENT aev = WSACreateEvent();
2762                 h[num] = aev;
2763                 WSAEventSelect(socket_fd, aev, FD_ACCEPT);
2764                 DWORD w = WaitForMultipleObjects(len, h, false, INFINITE);
2765                 WSAEventSelect(socket_fd, aev, 0);
2766                 WSACloseEvent(aev);
2767                 if (len <= w)
2768                         continue;
2769                 if (w == len - 1)
2770                 {
2771                         accept_client();
2772                         continue;
2773                 }
2774                 pid_t pid = h[w];
2775                 DWORD s = 0;
2776                 bool res = GetExitCodeProcess(pid, &s) && s == 0;
2777                 CloseHandle(pid);
2778                 finalize_job(pid, res);
2779         #else
2780                 sigset_t emptymask;
2781                 sigemptyset(&emptymask);
2782                 fd_set fdset;
2783                 FD_ZERO(&fdset);
2784                 FD_SET(socket_fd, &fdset);
2785                 int ret = pselect(socket_fd + 1, &fdset, NULL, NULL, NULL, &emptymask);
2786                 if (ret > 0 /* && FD_ISSET(socket_fd, &fdset)*/) accept_client();
2787                 if (!got_SIGCHLD) continue;
2788                 got_SIGCHLD = 0;
2789                 pid_t pid;
2790                 int status;
2791                 while ((pid = waitpid(-1, &status, WNOHANG)) > 0)
2792                 {
2793                         bool res = WIFEXITED(status) && WEXITSTATUS(status) == 0;
2794                         finalize_job(pid, res);
2795                 }
2796         #endif
2797         }
2798
2799         assert(clients.empty());
2800 }
2801
2802 /**
2803  * Load dependencies and rules, listen to client requests, and loop until
2804  * all the requests have completed.
2805  * If Remakefile is obsolete, perform a first run with it only, then reload
2806  * the rules, and perform a second with the original clients.
2807  */
2808 static void server_mode(std::string const &remakefile, string_list const &targets)
2809 {
2810         load_dependencies();
2811         load_rules(remakefile);
2812         create_server();
2813         if (get_status(remakefile).status != Uptodate)
2814         {
2815                 clients.push_back(client_t());
2816                 clients.back().pending.push_back(remakefile);
2817                 server_loop();
2818                 if (build_failure) goto early_exit;
2819                 variables.clear();
2820                 specific_rules.clear();
2821                 generic_rules.clear();
2822                 first_target.clear();
2823                 load_rules(remakefile);
2824         }
2825         clients.push_back(client_t());
2826         if (!targets.empty()) clients.back().pending = targets;
2827         else if (!first_target.empty())
2828                 clients.back().pending.push_back(first_target);
2829         server_loop();
2830         early_exit:
2831         close(socket_fd);
2832 #ifndef WINDOWS
2833         remove(socket_name);
2834         free(socket_name);
2835 #endif
2836         save_dependencies();
2837         if (print_change_working_dir)
2838                 std::cout << "remake: Leaving directory `" << prefix_dir 
2839                           << "'" <<std::endl;
2840         exit(build_failure ? EXIT_FAILURE : EXIT_SUCCESS);
2841 }
2842
2843 /** @} */
2844
2845 /**
2846  * @defgroup client Client
2847  *
2848  * @{
2849  */
2850
2851 /**
2852  * Connect to the server @a socket_name, send a request for building @a targets
2853  * with some @a variables, and exit with the status returned by the server.
2854  */
2855 static void client_mode(char *socket_name, string_list const &targets)
2856 {
2857         if (false)
2858         {
2859                 error:
2860                 perror("Failed to send targets to server");
2861                 exit(EXIT_FAILURE);
2862         }
2863         if (targets.empty()) exit(EXIT_SUCCESS);
2864         DEBUG_open << "Connecting to server... ";
2865
2866         // Connect to server.
2867 #ifdef WINDOWS
2868         struct sockaddr_in socket_addr;
2869         socket_fd = socket(AF_INET, SOCK_STREAM, 0);
2870         if (socket_fd == INVALID_SOCKET) goto error;
2871         socket_addr.sin_family = AF_INET;
2872         socket_addr.sin_addr.s_addr = inet_addr("127.0.0.1");
2873         socket_addr.sin_port = atoi(socket_name);
2874         if (connect(socket_fd, (struct sockaddr *)&socket_addr, sizeof(sockaddr_in)))
2875                 goto error;
2876 #else
2877         struct sockaddr_un socket_addr;
2878         size_t len = strlen(socket_name);
2879         if (len >= sizeof(socket_addr.sun_path) - 1) exit(EXIT_FAILURE);
2880         socket_fd = socket(AF_UNIX, SOCK_STREAM, 0);
2881         if (socket_fd == INVALID_SOCKET) goto error;
2882         socket_addr.sun_family = AF_UNIX;
2883         strcpy(socket_addr.sun_path, socket_name);
2884         if (connect(socket_fd, (struct sockaddr *)&socket_addr, sizeof(socket_addr.sun_family) + len))
2885                 goto error;
2886 #ifdef MACOSX
2887         int set_option = 1;
2888         if (setsockopt(socket_fd, SOL_SOCKET, SO_NOSIGPIPE, &set_option, sizeof(set_option)))
2889                 goto error;
2890 #endif
2891 #endif
2892
2893         // Send current job id.
2894         char *id = getenv("REMAKE_JOB_ID");
2895         int job_id = id ? atoi(id) : -1;
2896         if (send(socket_fd, (char *)&job_id, sizeof(job_id), MSG_NOSIGNAL) != sizeof(job_id))
2897                 goto error;
2898
2899         // Send targets.
2900         for (string_list::const_iterator i = targets.begin(),
2901              i_end = targets.end(); i != i_end; ++i)
2902         {
2903                 DEBUG_open << "Sending target " << *i << "... ";
2904                 std::string s = 'T' + *i;
2905                 ssize_t len = s.length() + 1;
2906                 if (send(socket_fd, s.c_str(), len, MSG_NOSIGNAL) != len)
2907                         goto error;
2908         }
2909
2910         // Send variables.
2911         for (variable_map::const_iterator i = variables.begin(),
2912              i_end = variables.end(); i != i_end; ++i)
2913         {
2914                 DEBUG_open << "Sending variable " << i->first << "... ";
2915                 std::string s = 'V' + i->first;
2916                 ssize_t len = s.length() + 1;
2917                 if (send(socket_fd, s.c_str(), len, MSG_NOSIGNAL) != len)
2918                         goto error;
2919                 for (string_list::const_iterator j = i->second.begin(),
2920                      j_end = i->second.end(); j != j_end; ++j)
2921                 {
2922                         std::string s = 'W' + *j;
2923                         len = s.length() + 1;
2924                         if (send(socket_fd, s.c_str(), len, MSG_NOSIGNAL) != len)
2925                                 goto error;
2926                 }
2927         }
2928
2929         // Send terminating nul and wait for reply.
2930         char result = 0;
2931         if (send(socket_fd, &result, 1, MSG_NOSIGNAL) != 1) goto error;
2932         if (recv(socket_fd, &result, 1, 0) != 1) exit(EXIT_FAILURE);
2933         exit(result ? EXIT_SUCCESS : EXIT_FAILURE);
2934 }
2935
2936 /** @} */
2937
2938 /**
2939  * @defgroup ui User interface
2940  *
2941  * @{
2942  */
2943
2944 /**
2945  * Display usage and exit with @a exit_status.
2946  */
2947 static void usage(int exit_status)
2948 {
2949         std::cerr << "Usage: remake [options] [target] ...\n"
2950                 "Options\n"
2951                 "  -d                     Echo script commands.\n"
2952                 "  -d -d                  Print lots of debugging information.\n"
2953                 "  -f FILE                Read FILE as Remakefile.\n"
2954                 "  -h, --help             Print this message and exit.\n"
2955                 "  -j[N], --jobs=[N]      Allow N jobs at once; infinite jobs with no arg.\n"
2956                 "  -k                     Keep going when some targets cannot be made.\n"
2957                 "  -r                     Look up targets from the dependencies on stdin.\n"
2958                 "  -s, --silent, --quiet  Do not echo targets.\n";
2959         exit(exit_status);
2960 }
2961
2962 /**
2963  * This program behaves in two different ways.
2964  *
2965  * - If the environment contains the REMAKE_SOCKET variable, the client
2966  *   connects to this socket and sends to the server its build targets.
2967  *   It exits once it receives the server reply.
2968  *
2969  * - Otherwise, it creates a server that waits for build requests. It
2970  *   also creates a pseudo-client that requests the targets passed on the
2971  *   command line.
2972  */
2973 int main(int argc, char *argv[])
2974 {
2975         init_working_dir();
2976
2977         std::string remakefile;
2978         string_list targets;
2979         bool literal_targets = false;
2980         bool indirect_targets = false;
2981
2982         // Parse command-line arguments.
2983         for (int i = 1; i < argc; ++i)
2984         {
2985                 std::string arg = argv[i];
2986                 if (arg.empty()) usage(EXIT_FAILURE);
2987                 if (literal_targets) goto new_target;
2988                 if (arg == "-h" || arg == "--help") usage(EXIT_SUCCESS);
2989                 if (arg == "-d")
2990                         if (echo_scripts) debug.active = true;
2991                         else echo_scripts = true;
2992                 else if (arg == "-k" || arg =="--keep-going")
2993                         keep_going = true;
2994                 else if (arg == "-s" || arg == "--silent" || arg == "--quiet")
2995                         show_targets = false;
2996                 else if (arg == "-r")
2997                         indirect_targets = true;
2998                 else if (arg == "-f")
2999                 {
3000                         if (++i == argc) usage(EXIT_FAILURE);
3001                         remakefile = argv[i];
3002                 }
3003                 else if (arg == "--")
3004                         literal_targets = true;
3005                 else if (arg.compare(0, 2, "-j") == 0)
3006                         max_active_jobs = atoi(arg.c_str() + 2);
3007                 else if (arg.compare(0, 7, "--jobs=") == 0)
3008                         max_active_jobs = atoi(arg.c_str() + 7);
3009                 else
3010                 {
3011                         if (arg[0] == '-') usage(EXIT_FAILURE);
3012                         if (arg.find('=') != std::string::npos)
3013                         {
3014                                 std::istringstream in(arg);
3015                                 std::string name = read_word(in);
3016                                 if (name.empty() || !expect_token(in, Equal)) usage(EXIT_FAILURE);
3017                                 read_words(in, variables[name]);
3018                                 continue;
3019                         }
3020                         new_target:
3021                         targets.push_back(normalize(arg, working_dir, working_dir));
3022                         DEBUG << "New target: " << arg << '\n';
3023                 }
3024         }
3025
3026         if (indirect_targets)
3027         {
3028                 load_dependencies(std::cin);
3029                 string_list l;
3030                 targets.swap(l);
3031                 if (l.empty() && !dependencies.empty())
3032                 {
3033                         l.push_back(dependencies.begin()->second->targets.front());
3034                 }
3035                 for (string_list::const_iterator i = l.begin(),
3036                      i_end = l.end(); i != i_end; ++i)
3037                 {
3038                         dependency_map::const_iterator j = dependencies.find(*i);
3039                         if (j == dependencies.end()) continue;
3040                         dependency_t const &dep = *j->second;
3041                         for (string_set::const_iterator k = dep.deps.begin(),
3042                              k_end = dep.deps.end(); k != k_end; ++k)
3043                         {
3044                                 targets.push_back(normalize(*k, working_dir, working_dir));
3045                         }
3046                 }
3047                 dependencies.clear();
3048         }
3049
3050 #ifdef WINDOWS
3051         WSADATA wsaData;
3052         if (WSAStartup(MAKEWORD(2,2), &wsaData))
3053         {
3054                 std::cerr << "Unexpected failure while initializing Windows Socket" << std::endl;
3055                 return 1;
3056         }
3057 #endif
3058
3059         // Run as client if REMAKE_SOCKET is present in the environment.
3060         if (char *sn = getenv("REMAKE_SOCKET")) client_mode(sn, targets);
3061
3062         // Otherwise run as server.
3063         if (remakefile.empty())
3064         {
3065                 remakefile = "Remakefile";
3066                 init_prefix_dir();
3067         }
3068         normalize_list(targets, working_dir, prefix_dir);
3069         server_mode(remakefile, targets);
3070 }
3071
3072 /** @} */