b8387be2b0230e44382f322657cdf4875c00df94
[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 #ifndef WINDOWS
728 static volatile sig_atomic_t got_SIGCHLD = 0;
729
730 static void sigchld_handler(int)
731 {
732         got_SIGCHLD = 1;
733 }
734
735 static void sigint_handler(int)
736 {
737         // Child processes will receive the signal too, so just prevent
738         // new jobs from starting and wait for the running jobs to fail.
739         keep_going = false;
740 }
741 #endif
742
743 struct log
744 {
745         bool active, open;
746         int depth;
747         log(): active(false), open(false), depth(0)
748         {
749         }
750         std::ostream &operator()()
751         {
752                 if (open) std::cerr << std::endl;
753                 assert(depth >= 0);
754                 std::cerr << std::string(depth * 2, ' ');
755                 open = false;
756                 return std::cerr;
757         }
758         std::ostream &operator()(bool o)
759         {
760                 if (o && open) std::cerr << std::endl;
761                 if (!o) --depth;
762                 assert(depth >= 0);
763                 if (o || !open) std::cerr << std::string(depth * 2, ' ');
764                 if (o) ++depth;
765                 open = o;
766                 return std::cerr;
767         }
768 };
769
770 log debug;
771
772 struct log_auto_close
773 {
774         bool still_open;
775         log_auto_close(): still_open(true)
776         {
777         }
778         ~log_auto_close()
779         {
780                 if (debug.active && still_open) debug(false) << "done\n";
781         }
782 };
783
784 #define DEBUG if (debug.active) debug()
785 #define DEBUG_open log_auto_close auto_close; if (debug.active) debug(true)
786 #define DEBUG_close if ((auto_close.still_open = false), debug.active) debug(false)
787
788 /**
789  * Strong typedef for strings that need escaping.
790  * @note The string is stored as a reference, so the constructed object is
791  *       meant to be immediately consumed.
792  */
793 struct escape_string
794 {
795         std::string const &input;
796         escape_string(std::string const &s): input(s) {}
797 };
798
799 /**
800  * Write the string in @a se to @a out if it does not contain any special
801  * characters, a quoted and escaped string otherwise.
802  */
803 static std::ostream &operator<<(std::ostream &out, escape_string const &se)
804 {
805         std::string const &s = se.input;
806         char const *quoted_char = ",: '";
807         char const *escaped_char = "\"\\$!";
808         bool need_quotes = false;
809         char *buf = NULL;
810         size_t len = s.length(), last = 0, j = 0;
811         for (size_t i = 0; i < len; ++i)
812         {
813                 if (strchr(escaped_char, s[i]))
814                 {
815                         need_quotes = true;
816                         if (!buf) buf = new char[len * 2];
817                         memcpy(&buf[j], &s[last], i - last);
818                         j += i - last;
819                         buf[j++] = '\\';
820                         buf[j++] = s[i];
821                         last = i + 1;
822                 }
823                 if (!need_quotes && strchr(quoted_char, s[i]))
824                         need_quotes = true;
825         }
826         if (!need_quotes) return out << s;
827         out << '"';
828         if (!buf) return out << s << '"';
829         out.write(buf, j);
830         out.write(&s[last], len - last);
831         delete[] buf;
832         return out << '"';
833 }
834
835 /**
836  * @defgroup paths Path helpers
837  *
838  * @{
839  */
840
841 /**
842  * Initialize #working_dir.
843  */
844 static void init_working_dir()
845 {
846         char buf[1024];
847         char *res = getcwd(buf, sizeof(buf));
848         if (!res)
849         {
850                 perror("Failed to get working directory");
851                 exit(EXIT_FAILURE);
852         }
853         working_dir = buf;
854 #ifdef WINDOWS
855         for (size_t i = 0, l = working_dir.size(); i != l; ++i)
856         {
857                 if (working_dir[i] == '\\') working_dir[i] = '/';
858         }
859 #endif
860         prefix_dir = working_dir;
861 }
862
863 /**
864  * Initialize #prefix_dir and switch to it.
865  */
866 static void init_prefix_dir()
867 {
868         for (;;)
869         {
870                 struct stat s;
871                 if (stat((prefix_dir + "/Remakefile").c_str(), &s) == 0)
872                 {
873                         chdir(prefix_dir.c_str());
874                         return;
875                 }
876                 size_t pos = prefix_dir.find_last_of('/');
877                 if (pos == std::string::npos)
878                 {
879                         std::cerr << "Failed to locate Remakefile in the current directory or one of its parents" << std::endl;
880                         exit(EXIT_FAILURE);
881                 }
882                 prefix_dir.erase(pos);
883         }
884 }
885
886 /**
887  * Normalize an absolute path with respect to @a p.
888  * Paths outside the subtree are left unchanged.
889  */
890 static std::string normalize_abs(std::string const &s, std::string const &p)
891 {
892         size_t l = p.length();
893         if (s.compare(0, l, p)) return s;
894         size_t ll = s.length();
895         if (ll == l) return ".";
896         if (s[l] != '/')
897         {
898                 size_t pos = s.rfind('/', l);
899                 assert(pos != std::string::npos);
900                 return s.substr(pos + 1);
901         }
902         if (ll == l + 1) return ".";
903         return s.substr(l + 1);
904 }
905
906 /**
907  * Normalize path @a s (possibly relative to @a w) with respect to @a p.
908  *
909  * - If both @a p and @a w are empty, the function just removes ".", "..", "//".
910  * - If only @a p is empty, the function returns an absolute path.
911  */
912 static std::string normalize(std::string const &s, std::string const &w, std::string const &p)
913 {
914 #ifdef WINDOWS
915         char const *delim = "/\\";
916 #else
917         char delim = '/';
918 #endif
919         size_t pos = s.find_first_of(delim);
920         if (pos == std::string::npos && w == p) return s;
921         bool absolute = pos == 0;
922         if (!absolute && w != p && !w.empty())
923                 return normalize(w + '/' + s, w, p);
924         size_t prev = 0, len = s.length();
925         string_list l;
926         for (;;)
927         {
928                 if (pos != prev)
929                 {
930                         std::string n = s.substr(prev, pos - prev);
931                         if (n == "..")
932                         {
933                                 if (!l.empty()) l.pop_back();
934                                 else if (!absolute && !w.empty())
935                                         return normalize(w + '/' + s, w, p);
936                         }
937                         else if (n != ".")
938                                 l.push_back(n);
939                 }
940                 ++pos;
941                 if (pos >= len) break;
942                 prev = pos;
943                 pos = s.find_first_of(delim, prev);
944                 if (pos == std::string::npos) pos = len;
945         }
946         string_list::const_iterator i = l.begin(), i_end = l.end();
947         if (i == i_end) return absolute ? "/" : ".";
948         std::string n;
949         if (absolute) n.push_back('/');
950         n.append(*i);
951         for (++i; i != i_end; ++i)
952         {
953                 n.push_back('/');
954                 n.append(*i);
955         }
956         if (absolute && !p.empty()) return normalize_abs(n, p);
957         return n;
958 }
959
960 /**
961  * Normalize the content of a list of targets.
962  */
963 static void normalize_list(string_list &l, std::string const &w, std::string const &p)
964 {
965         for (string_list::iterator i = l.begin(),
966              i_end = l.end(); i != i_end; ++i)
967         {
968                 *i = normalize(*i, w, p);
969         }
970 }
971
972 /** @} */
973
974 /**
975  * @defgroup lexer Lexer
976  *
977  * @{
978  */
979
980 /**
981  * Skip spaces.
982  */
983 static void skip_spaces(std::istream &in)
984 {
985         char c;
986         while (strchr(" \t", (c = in.get()))) {}
987         if (in.good()) in.putback(c);
988 }
989
990 /**
991  * Skip empty lines.
992  */
993 static void skip_empty(std::istream &in)
994 {
995         char c;
996         while (strchr("\r\n", (c = in.get()))) {}
997         if (in.good()) in.putback(c);
998 }
999
1000 /**
1001  * Skip end of line. If @a multi is true, skip the following empty lines too.
1002  * @return true if there was a line to end.
1003  */
1004 static bool skip_eol(std::istream &in, bool multi = false)
1005 {
1006         char c = in.get();
1007         if (c == '\r') c = in.get();
1008         if (c != '\n' && in.good()) in.putback(c);
1009         if (c != '\n' && !in.eof()) return false;
1010         if (multi) skip_empty(in);
1011         return true;
1012 }
1013
1014 enum
1015 {
1016   Unexpected = 0,
1017   Word       = 1 << 1,
1018   Colon      = 1 << 2,
1019   Equal      = 1 << 3,
1020   Dollarpar  = 1 << 4,
1021   Rightpar   = 1 << 5,
1022   Comma      = 1 << 6,
1023   Plusequal  = 1 << 7,
1024   Pipe       = 1 << 8,
1025 };
1026
1027 /**
1028  * Skip spaces and peek at the next token.
1029  * If it is one of @a mask, skip it (if it is not Word) and return it.
1030  * @note For composite tokens allowed by @a mask, input characters might
1031  *       have been eaten even for an Unexpected result.
1032  */
1033 static int expect_token(std::istream &in, int mask)
1034 {
1035         while (true)
1036         {
1037                 skip_spaces(in);
1038                 char c = in.peek();
1039                 if (!in.good()) return Unexpected;
1040                 int tok;
1041                 switch (c)
1042                 {
1043                 case '\r':
1044                 case '\n': return Unexpected;
1045                 case ':': tok = Colon; break;
1046                 case ',': tok = Comma; break;
1047                 case '=': tok = Equal; break;
1048                 case ')': tok = Rightpar; break;
1049                 case '|': tok = Pipe; break;
1050                 case '$':
1051                         if (!(mask & Dollarpar)) return Unexpected;
1052                         in.ignore(1);
1053                         tok = Dollarpar;
1054                         if (in.peek() != '(') return Unexpected;
1055                         break;
1056                 case '+':
1057                         if (!(mask & Plusequal)) return Unexpected;
1058                         in.ignore(1);
1059                         tok = Plusequal;
1060                         if (in.peek() != '=') return Unexpected;
1061                         break;
1062                 case '\\':
1063                         in.ignore(1);
1064                         if (skip_eol(in)) continue;
1065                         in.putback('\\');
1066                         return mask & Word ? Word : Unexpected;
1067                 default:
1068                         return mask & Word ? Word : Unexpected;
1069                 }
1070                 if (!(tok & mask)) return Unexpected;
1071                 in.ignore(1);
1072                 return tok;
1073         }
1074 }
1075
1076 /**
1077  * Read a (possibly quoted) word.
1078  */
1079 static std::string read_word(std::istream &in)
1080 {
1081         int c = in.get();
1082         std::string res;
1083         if (!in.good()) return res;
1084         char const *separators = " \t\r\n:$(),=+\"";
1085         bool quoted = c == '"';
1086         if (!quoted)
1087         {
1088                 if (strchr(separators, c))
1089                 {
1090                         in.putback(c);
1091                         return res;
1092                 }
1093                 res += c;
1094         }
1095         while (true)
1096         {
1097                 c = in.get();
1098                 if (!in.good()) return res;
1099                 if (quoted)
1100                 {
1101                         if (c == '\\')
1102                                 res += in.get();
1103                         else if (c == '"')
1104                                 return res;
1105                         else
1106                                 res += c;
1107                 }
1108                 else
1109                 {
1110                         if (strchr(separators, c))
1111                         {
1112                                 in.putback(c);
1113                                 return res;
1114                         }
1115                         res += c;
1116                 }
1117         }
1118 }
1119
1120 /** @} */
1121
1122 /**
1123  * @defgroup stream Token streams
1124  *
1125  * @{
1126  */
1127
1128 /**
1129  * Possible results from word producers.
1130  */
1131 enum input_status
1132 {
1133         Success,
1134         SyntaxError,
1135         Eof
1136 };
1137
1138 /**
1139  * Interface for word producers.
1140  */
1141 struct generator
1142 {
1143         virtual ~generator() {}
1144         virtual input_status next(std::string &) = 0;
1145 };
1146
1147 /**
1148  * Generator for the words of a variable.
1149  */
1150 struct variable_generator: generator
1151 {
1152         std::string name;
1153         string_list::const_iterator vcur, vend;
1154         variable_generator(std::string const &, variable_map const *);
1155         input_status next(std::string &);
1156 };
1157
1158 variable_generator::variable_generator(std::string const &n,
1159         variable_map const *local_variables): name(n)
1160 {
1161         if (local_variables)
1162         {
1163                 variable_map::const_iterator i = local_variables->find(name);
1164                 if (i != local_variables->end())
1165                 {
1166                         vcur = i->second.begin();
1167                         vend = i->second.end();
1168                         return;
1169                 }
1170         }
1171         variable_map::const_iterator i = variables.find(name);
1172         if (i == variables.end()) return;
1173         vcur = i->second.begin();
1174         vend = i->second.end();
1175 }
1176
1177 input_status variable_generator::next(std::string &res)
1178 {
1179         if (vcur != vend)
1180         {
1181                 res = *vcur;
1182                 ++vcur;
1183                 return Success;
1184         }
1185         return Eof;
1186 }
1187
1188 /**
1189  * Generator for the words of an input stream.
1190  */
1191 struct input_generator
1192 {
1193         std::istream &in;
1194         generator *nested;
1195         variable_map const *local_variables;
1196         bool earliest_exit, done;
1197         input_generator(std::istream &i, variable_map const *lv, bool e = false)
1198                 : in(i), nested(NULL), local_variables(lv), earliest_exit(e), done(false) {}
1199         input_status next(std::string &);
1200         ~input_generator() { assert(!nested); }
1201 };
1202
1203 static generator *get_function(input_generator const &, std::string const &);
1204
1205 input_status input_generator::next(std::string &res)
1206 {
1207         if (nested)
1208         {
1209                 restart:
1210                 input_status s = nested->next(res);
1211                 if (s == Success) return Success;
1212                 delete nested;
1213                 nested = NULL;
1214                 if (s == SyntaxError) return SyntaxError;
1215         }
1216         if (done) return Eof;
1217         if (earliest_exit) done = true;
1218         switch (expect_token(in, Word | Dollarpar))
1219         {
1220         case Word:
1221                 res = read_word(in);
1222                 return Success;
1223         case Dollarpar:
1224         {
1225                 std::string name = read_word(in);
1226                 if (name.empty()) return SyntaxError;
1227                 if (expect_token(in, Rightpar))
1228                         nested = new variable_generator(name, local_variables);
1229                 else
1230                 {
1231                         nested = get_function(*this, name);
1232                         if (!nested) return SyntaxError;
1233                 }
1234                 goto restart;
1235         }
1236         default:
1237                 return Eof;
1238         }
1239 }
1240
1241 /**
1242  * Read a list of words from an input generator.
1243  * @return false if a syntax error was encountered.
1244  */
1245 static bool read_words(input_generator &in, string_list &res)
1246 {
1247         while (true)
1248         {
1249                 res.push_back(std::string());
1250                 input_status s = in.next(res.back());
1251                 if (s == Success) continue;
1252                 res.pop_back();
1253                 return s == Eof;
1254         }
1255 }
1256
1257 static bool read_words(std::istream &in, string_list &res)
1258 {
1259         input_generator gen(in, NULL);
1260         return read_words(gen, res);
1261 }
1262
1263 /**
1264  * Generator for the result of function addprefix.
1265  */
1266 struct addprefix_generator: generator
1267 {
1268         input_generator gen;
1269         string_list pre;
1270         string_list::const_iterator prei;
1271         size_t prej, prel;
1272         std::string suf;
1273         addprefix_generator(input_generator const &, bool &);
1274         input_status next(std::string &);
1275 };
1276
1277 addprefix_generator::addprefix_generator(input_generator const &top, bool &ok)
1278         : gen(top.in, top.local_variables)
1279 {
1280         if (!read_words(gen, pre)) return;
1281         if (!expect_token(gen.in, Comma)) return;
1282         prej = 0;
1283         prel = pre.size();
1284         ok = true;
1285 }
1286
1287 input_status addprefix_generator::next(std::string &res)
1288 {
1289         if (prej)
1290         {
1291                 produce:
1292                 if (prej == prel)
1293                 {
1294                         res = *prei + suf;
1295                         prej = 0;
1296                 }
1297                 else
1298                 {
1299                         res = *prei++;
1300                         ++prej;
1301                 }
1302                 return Success;
1303         }
1304         switch (gen.next(res))
1305         {
1306         case Success:
1307                 if (!prel) return Success;
1308                 prei = pre.begin();
1309                 prej = 1;
1310                 suf = res;
1311                 goto produce;
1312         case Eof:
1313                 return expect_token(gen.in, Rightpar) ? Eof : SyntaxError;
1314         default:
1315                 return SyntaxError;
1316         }
1317 }
1318
1319 /**
1320  * Generator for the result of function addsuffix.
1321  */
1322 struct addsuffix_generator: generator
1323 {
1324         input_generator gen;
1325         string_list suf;
1326         string_list::const_iterator sufi;
1327         size_t sufj, sufl;
1328         std::string pre;
1329         addsuffix_generator(input_generator const &, bool &);
1330         input_status next(std::string &);
1331 };
1332
1333 addsuffix_generator::addsuffix_generator(input_generator const &top, bool &ok)
1334         : gen(top.in, top.local_variables)
1335 {
1336         if (!read_words(gen, suf)) return;
1337         if (!expect_token(gen.in, Comma)) return;
1338         sufj = 0;
1339         sufl = suf.size();
1340         ok = true;
1341 }
1342
1343 input_status addsuffix_generator::next(std::string &res)
1344 {
1345         if (sufj)
1346         {
1347                 if (sufj != sufl)
1348                 {
1349                         res = *sufi++;
1350                         ++sufj;
1351                         return Success;
1352                 }
1353                 sufj = 0;
1354         }
1355         switch (gen.next(res))
1356         {
1357         case Success:
1358                 if (!sufl) return Success;
1359                 sufi = suf.begin();
1360                 sufj = 1;
1361                 res += *sufi++;
1362                 return Success;
1363         case Eof:
1364                 return expect_token(gen.in, Rightpar) ? Eof : SyntaxError;
1365         default:
1366                 return SyntaxError;
1367         }
1368 }
1369
1370 /**
1371  * Return a generator for function @a name.
1372  */
1373 static generator *get_function(input_generator const &in, std::string const &name)
1374 {
1375         skip_spaces(in.in);
1376         generator *g = NULL;
1377         bool ok = false;
1378         if (name == "addprefix") g = new addprefix_generator(in, ok);
1379         else if (name == "addsuffix") g = new addsuffix_generator(in, ok);
1380         if (!g || ok) return g;
1381         delete g;
1382         return NULL;
1383 }
1384
1385 /** @} */
1386
1387 /**
1388  * @defgroup database Dependency database
1389  *
1390  * @{
1391  */
1392
1393 /**
1394  * Load dependencies from @a in.
1395  */
1396 static void load_dependencies(std::istream &in)
1397 {
1398         if (false)
1399         {
1400                 error:
1401                 std::cerr << "Failed to load database" << std::endl;
1402                 exit(EXIT_FAILURE);
1403         }
1404
1405         while (!in.eof())
1406         {
1407                 string_list targets;
1408                 if (!read_words(in, targets)) goto error;
1409                 if (in.eof()) return;
1410                 if (targets.empty()) goto error;
1411                 DEBUG << "reading dependencies of target " << targets.front() << std::endl;
1412                 if (in.get() != ':') goto error;
1413                 ref_ptr<dependency_t> dep;
1414                 dep->targets = targets;
1415                 string_list deps;
1416                 if (!read_words(in, deps)) goto error;
1417                 dep->deps.insert(deps.begin(), deps.end());
1418                 for (string_list::const_iterator i = targets.begin(),
1419                      i_end = targets.end(); i != i_end; ++i)
1420                 {
1421                         dependencies[*i] = dep;
1422                 }
1423                 skip_empty(in);
1424         }
1425 }
1426
1427 /**
1428  * Load known dependencies from file <tt>.remake</tt>.
1429  */
1430 static void load_dependencies()
1431 {
1432         DEBUG_open << "Loading database... ";
1433         std::ifstream in(".remake");
1434         if (!in.good())
1435         {
1436                 DEBUG_close << "not found\n";
1437                 return;
1438         }
1439         load_dependencies(in);
1440 }
1441
1442
1443 /**
1444  * Save all the dependencies in file <tt>.remake</tt>.
1445  */
1446 static void save_dependencies()
1447 {
1448         DEBUG_open << "Saving database... ";
1449         std::ofstream db(".remake");
1450         while (!dependencies.empty())
1451         {
1452                 ref_ptr<dependency_t> dep = dependencies.begin()->second;
1453                 for (string_list::const_iterator i = dep->targets.begin(),
1454                      i_end = dep->targets.end(); i != i_end; ++i)
1455                 {
1456                         db << escape_string(*i) << ' ';
1457                         dependencies.erase(*i);
1458                 }
1459                 db << ':';
1460                 for (string_set::const_iterator i = dep->deps.begin(),
1461                      i_end = dep->deps.end(); i != i_end; ++i)
1462                 {
1463                         db << ' ' << escape_string(*i);
1464                 }
1465                 db << std::endl;
1466         }
1467 }
1468
1469 /** @} */
1470
1471 static void merge_rule(rule_t &dest, rule_t const &src);
1472
1473 /**
1474  * @defgroup parser Rule parser
1475  *
1476  * @{
1477  */
1478
1479 /**
1480  * Register a specific rule with an empty script:
1481  *
1482  * - Check that none of the targets already has an associated rule with a
1483  *   nonempty script.
1484  * - Create a new rule with a single target for each target, if needed.
1485  * - Add the prerequisites of @a rule to all these associated rules.
1486  */
1487 static void register_transparent_rule(rule_t const &rule, string_list const &targets)
1488 {
1489         assert(rule.script.empty());
1490         for (string_list::const_iterator i = targets.begin(),
1491              i_end = targets.end(); i != i_end; ++i)
1492         {
1493                 std::pair<rule_map::iterator, bool> j =
1494                         specific_rules.insert(std::make_pair(*i, ref_ptr<rule_t>()));
1495                 ref_ptr<rule_t> &r = j.first->second;
1496                 if (j.second)
1497                 {
1498                         r = ref_ptr<rule_t>(rule);
1499                         r->targets = string_list(1, *i);
1500                         continue;
1501                 }
1502                 if (!r->script.empty())
1503                 {
1504                         std::cerr << "Failed to load rules: " << *i
1505                                 << " cannot be the target of several rules" << std::endl;
1506                         exit(EXIT_FAILURE);
1507                 }
1508                 assert(r->targets.size() == 1 && r->targets.front() == *i);
1509                 merge_rule(*r, rule);
1510         }
1511
1512         for (string_list::const_iterator i = targets.begin(),
1513              i_end = targets.end(); i != i_end; ++i)
1514         {
1515                 ref_ptr<dependency_t> &dep = dependencies[*i];
1516                 if (dep->targets.empty()) dep->targets.push_back(*i);
1517                 dep->deps.insert(rule.deps.begin(), rule.deps.end());
1518         }
1519 }
1520
1521 /**
1522  * Register a specific rule with a nonempty script:
1523  *
1524  * - Check that none of the targets already has an associated rule.
1525  * - Create a single shared rule and associate it to all the targets.
1526  * - Merge the prerequisites of all the targets into a single set and
1527  *   add the prerequisites of the rule to it. (The preexisting
1528  *   prerequisites, if any, come from a previous run.)
1529  */
1530 static void register_scripted_rule(rule_t const &rule)
1531 {
1532         ref_ptr<rule_t> r(rule);
1533         for (string_list::const_iterator i = rule.targets.begin(),
1534              i_end = rule.targets.end(); i != i_end; ++i)
1535         {
1536                 std::pair<rule_map::iterator, bool> j =
1537                         specific_rules.insert(std::make_pair(*i, r));
1538                 if (j.second) continue;
1539                 std::cerr << "Failed to load rules: " << *i
1540                         << " cannot be the target of several rules" << std::endl;
1541                 exit(EXIT_FAILURE);
1542         }
1543
1544         ref_ptr<dependency_t> dep;
1545         dep->targets = rule.targets;
1546         dep->deps.insert(rule.deps.begin(), rule.deps.end());
1547         for (string_list::const_iterator i = rule.targets.begin(),
1548              i_end = rule.targets.end(); i != i_end; ++i)
1549         {
1550                 ref_ptr<dependency_t> &d = dependencies[*i];
1551                 dep->deps.insert(d->deps.begin(), d->deps.end());
1552                 d = dep;
1553         }
1554 }
1555
1556 /**
1557  * Read a rule starting with target @a first, if nonempty.
1558  * Store into #generic_rules or #specific_rules depending on its genericity.
1559  */
1560 static void load_rule(std::istream &in, std::string const &first)
1561 {
1562         DEBUG_open << "Reading rule for target " << first << "... ";
1563         if (false)
1564         {
1565                 error:
1566                 DEBUG_close << "failed\n";
1567                 std::cerr << "Failed to load rules: syntax error" << std::endl;
1568                 exit(EXIT_FAILURE);
1569         }
1570         rule_t rule;
1571
1572         // Read targets and check genericity.
1573         string_list targets;
1574         if (!read_words(in, targets)) goto error;
1575         if (!first.empty()) targets.push_front(first);
1576         else if (targets.empty()) goto error;
1577         else DEBUG << "actual target: " << targets.front() << std::endl;
1578         bool generic = false;
1579         normalize_list(targets, "", "");
1580         for (string_list::const_iterator i = targets.begin(),
1581              i_end = targets.end(); i != i_end; ++i)
1582         {
1583                 if (i->empty()) goto error;
1584                 if ((i->find('%') != std::string::npos) != generic)
1585                 {
1586                         if (i == targets.begin()) generic = true;
1587                         else goto error;
1588                 }
1589         }
1590         std::swap(rule.targets, targets);
1591         skip_spaces(in);
1592         if (in.get() != ':') goto error;
1593
1594         bool assignment = false;
1595
1596         // Read dependencies.
1597         {
1598                 string_list v;
1599                 if (expect_token(in, Word))
1600                 {
1601                         std::string d = read_word(in);
1602                         if (int tok = expect_token(in, Equal | Plusequal))
1603                         {
1604                                 if (!read_words(in, v)) goto error;
1605                                 assign_t &a = rule.assigns[d];
1606                                 a.append = tok == Plusequal;
1607                                 a.value.swap(v);
1608                                 assignment = true;
1609                                 goto end_line;
1610                         }
1611                         v.push_back(d);
1612                 }
1613
1614                 if (!read_words(in, v)) goto error;
1615                 normalize_list(v, "", "");
1616                 rule.deps.swap(v);
1617
1618                 if (expect_token(in, Pipe))
1619                 {
1620                         if (!read_words(in, v)) goto error;
1621                         normalize_list(v, "", "");
1622                         rule.wdeps.swap(v);
1623                 }
1624         }
1625
1626         end_line:
1627         skip_spaces(in);
1628         if (!skip_eol(in, true)) goto error;
1629
1630         // Read script.
1631         std::ostringstream buf;
1632         while (true)
1633         {
1634                 char c = in.get();
1635                 if (!in.good()) break;
1636                 if (c == '\t' || c == ' ')
1637                 {
1638                         in.get(*buf.rdbuf());
1639                         if (in.fail() && !in.eof()) in.clear();
1640                 }
1641                 else if (c == '\r' || c == '\n')
1642                         buf << c;
1643                 else
1644                 {
1645                         in.putback(c);
1646                         break;
1647                 }
1648         }
1649         rule.script = buf.str();
1650
1651         // Add generic rules to the correct set.
1652         if (generic)
1653         {
1654                 if (assignment) goto error;
1655                 generic_rules.push_back(rule);
1656                 return;
1657         }
1658
1659         if (!rule.script.empty())
1660         {
1661                 if (assignment) goto error;
1662                 register_scripted_rule(rule);
1663         }
1664         else
1665         {
1666                 // Swap away the targets to avoid costly copies when registering.
1667                 string_list targets;
1668                 std::swap(rule.targets, targets);
1669                 register_transparent_rule(rule, targets);
1670                 std::swap(rule.targets, targets);
1671         }
1672
1673         // If there is no default target yet, mark it as such.
1674         if (first_target.empty())
1675                 first_target = rule.targets.front();
1676 }
1677
1678 /**
1679  * Load rules from @a remakefile.
1680  * If some rules have dependencies and non-generic targets, add these
1681  * dependencies to the targets.
1682  */
1683 static void load_rules(std::string const &remakefile)
1684 {
1685         DEBUG_open << "Loading rules... ";
1686         if (false)
1687         {
1688                 error:
1689                 std::cerr << "Failed to load rules: syntax error" << std::endl;
1690                 exit(EXIT_FAILURE);
1691         }
1692         std::ifstream in(remakefile.c_str());
1693         if (!in.good())
1694         {
1695                 std::cerr << "Failed to load rules: no Remakefile found" << std::endl;
1696                 exit(EXIT_FAILURE);
1697         }
1698         skip_empty(in);
1699
1700         string_list options;
1701
1702         // Read rules
1703         while (in.good())
1704         {
1705                 char c = in.peek();
1706                 if (c == '#')
1707                 {
1708                         while (in.get() != '\n') {}
1709                         skip_empty(in);
1710                         continue;
1711                 }
1712                 if (c == ' ' || c == '\t') goto error;
1713                 if (expect_token(in, Word))
1714                 {
1715                         std::string name = read_word(in);
1716                         if (name.empty()) goto error;
1717                         if (int tok = expect_token(in, Equal | Plusequal))
1718                         {
1719                                 DEBUG << "Assignment to variable " << name << std::endl;
1720                                 string_list value;
1721                                 if (!read_words(in, value)) goto error;
1722                                 string_list &dest =
1723                                         *(name == ".OPTIONS" ? &options : &variables[name]);
1724                                 if (tok == Equal) dest.swap(value);
1725                                 else dest.splice(dest.end(), value);
1726                                 if (!skip_eol(in, true)) goto error;
1727                         }
1728                         else load_rule(in, name);
1729                 }
1730                 else load_rule(in, std::string());
1731         }
1732
1733         // Set actual options.
1734         for (string_list::const_iterator i = options.begin(),
1735              i_end = options.end(); i != i_end; ++i)
1736         {
1737                 if (*i == "variable-propagation") propagate_vars = true;
1738                 else
1739                 {
1740                         std::cerr << "Failed to load rules: unrecognized option" << std::endl;
1741                         exit(EXIT_FAILURE);
1742                 }
1743         }
1744 }
1745
1746 /** @} */
1747
1748 /**
1749  * @defgroup rules Rule resolution
1750  *
1751  * @{
1752  */
1753
1754 static void merge_rule(rule_t &dest, rule_t const &src)
1755 {
1756         dest.deps.insert(dest.deps.end(), src.deps.begin(), src.deps.end());
1757         dest.wdeps.insert(dest.wdeps.end(), src.wdeps.begin(), src.wdeps.end());
1758         for (assign_map::const_iterator i = src.assigns.begin(),
1759              i_end = src.assigns.end(); i != i_end; ++i)
1760         {
1761                 if (!i->second.append)
1762                 {
1763                         new_assign:
1764                         dest.assigns[i->first] = i->second;
1765                         continue;
1766                 }
1767                 assign_map::iterator j = dest.assigns.find(i->first);
1768                 if (j == dest.assigns.end()) goto new_assign;
1769                 j->second.value.insert(j->second.value.end(),
1770                         i->second.value.begin(), i->second.value.end());
1771         }
1772 }
1773
1774 /**
1775  * Substitute a pattern into a list of strings.
1776  */
1777 static void substitute_pattern(std::string const &pat, string_list const &src, string_list &dst)
1778 {
1779         for (string_list::const_iterator i = src.begin(),
1780              i_end = src.end(); i != i_end; ++i)
1781         {
1782                 size_t pos = i->find('%');
1783                 if (pos == std::string::npos) dst.push_back(*i);
1784                 else dst.push_back(i->substr(0, pos) + pat + i->substr(pos + 1));
1785         }
1786 }
1787
1788 /**
1789  * Find a generic rule matching @a target:
1790  * - the one leading to shorter matches has priority,
1791  * - among equivalent rules, the earliest one has priority.
1792  */
1793 static void find_generic_rule(job_t &job, std::string const &target)
1794 {
1795         size_t tlen = target.length(), plen = tlen + 1;
1796         for (rule_list::const_iterator i = generic_rules.begin(),
1797              i_end = generic_rules.end(); i != i_end; ++i)
1798         {
1799                 for (string_list::const_iterator j = i->targets.begin(),
1800                      j_end = i->targets.end(); j != j_end; ++j)
1801                 {
1802                         size_t len = j->length();
1803                         if (tlen < len) continue;
1804                         if (plen <= tlen - (len - 1)) continue;
1805                         size_t pos = j->find('%');
1806                         if (pos == std::string::npos) continue;
1807                         size_t len2 = len - (pos + 1);
1808                         if (j->compare(0, pos, target, 0, pos) ||
1809                             j->compare(pos + 1, len2, target, tlen - len2, len2))
1810                                 continue;
1811                         plen = tlen - (len - 1);
1812                         job.stem = target.substr(pos, plen);
1813                         job.rule = rule_t();
1814                         job.rule.script = i->script;
1815                         substitute_pattern(job.stem, i->targets, job.rule.targets);
1816                         substitute_pattern(job.stem, i->deps, job.rule.deps);
1817                         substitute_pattern(job.stem, i->wdeps, job.rule.wdeps);
1818                         break;
1819                 }
1820         }
1821 }
1822
1823 /**
1824  * Find a specific rule matching @a target. Return a generic one otherwise.
1825  * If there is both a specific rule with an empty script and a generic rule, the
1826  * generic one is returned after adding the dependencies of the specific one.
1827  */
1828 static void find_rule(job_t &job, std::string const &target)
1829 {
1830         rule_map::const_iterator i = specific_rules.find(target),
1831                 i_end = specific_rules.end();
1832         // If there is a specific rule with a script, return it.
1833         if (i != i_end && !i->second->script.empty())
1834         {
1835                 job.rule = *i->second;
1836                 return;
1837         }
1838         find_generic_rule(job, target);
1839         // If there is no generic rule, return the specific rule (no script), if any.
1840         if (job.rule.targets.empty())
1841         {
1842                 if (i != i_end)
1843                 {
1844                         job.rule = *i->second;
1845                         return;
1846                 }
1847         }
1848         // Optimize the lookup when there is only one target (already looked up).
1849         if (job.rule.targets.size() == 1)
1850         {
1851                 if (i == i_end) return;
1852                 merge_rule(job.rule, *i->second);
1853                 return;
1854         }
1855         // Add the dependencies of the specific rules of every target to the
1856         // generic rule. If any of those rules has a nonempty script, error out.
1857         for (string_list::const_iterator j = job.rule.targets.begin(),
1858              j_end = job.rule.targets.end(); j != j_end; ++j)
1859         {
1860                 i = specific_rules.find(*j);
1861                 if (i == i_end) continue;
1862                 if (!i->second->script.empty()) return;
1863                 merge_rule(job.rule, *i->second);
1864         }
1865 }
1866
1867 /** @} */
1868
1869 /**
1870  * @defgroup status Target status
1871  *
1872  * @{
1873  */
1874
1875 /**
1876  * Compute and memoize the status of @a target:
1877  * - if the file does not exist, the target is obsolete,
1878  * - if any dependency is obsolete or younger than the file, it is obsolete,
1879  * - otherwise it is up-to-date.
1880  *
1881  * @note For rules with multiple targets, all the targets share the same
1882  *       status. (If one is obsolete, they all are.) The second rule above
1883  *       is modified in that case: the latest target is chosen, not the oldest!
1884  */
1885 static status_t const &get_status(std::string const &target)
1886 {
1887         std::pair<status_map::iterator,bool> i =
1888                 status.insert(std::make_pair(target, status_t()));
1889         status_t &ts = i.first->second;
1890         if (!i.second) return ts;
1891         DEBUG_open << "Checking status of " << target << "... ";
1892         dependency_map::const_iterator j = dependencies.find(target);
1893         if (j == dependencies.end())
1894         {
1895                 struct stat s;
1896                 if (stat(target.c_str(), &s) != 0)
1897                 {
1898                         DEBUG_close << "missing\n";
1899                         ts.status = Todo;
1900                         ts.last = 0;
1901                         return ts;
1902                 }
1903                 DEBUG_close << "up-to-date\n";
1904                 ts.status = Uptodate;
1905                 ts.last = s.st_mtime;
1906                 return ts;
1907         }
1908         dependency_t const &dep = *j->second;
1909         status_e st = Uptodate;
1910         time_t latest = 0;
1911         for (string_list::const_iterator k = dep.targets.begin(),
1912              k_end = dep.targets.end(); k != k_end; ++k)
1913         {
1914                 struct stat s;
1915                 if (stat(k->c_str(), &s) != 0)
1916                 {
1917                         if (st == Uptodate) DEBUG_close << *k << " missing\n";
1918                         s.st_mtime = 0;
1919                         st = Todo;
1920                 }
1921                 status[*k].last = s.st_mtime;
1922                 if (s.st_mtime > latest) latest = s.st_mtime;
1923         }
1924         if (st != Uptodate) goto update;
1925         for (string_set::const_iterator k = dep.deps.begin(),
1926              k_end = dep.deps.end(); k != k_end; ++k)
1927         {
1928                 status_t const &ts_ = get_status(*k);
1929                 if (latest < ts_.last)
1930                 {
1931                         DEBUG_close << "older than " << *k << std::endl;
1932                         st = Todo;
1933                         goto update;
1934                 }
1935                 if (ts_.status != Uptodate && st != Recheck)
1936                 {
1937                         DEBUG << "obsolete dependency " << *k << std::endl;
1938                         st = Recheck;
1939                 }
1940         }
1941         if (st == Uptodate) DEBUG_close << "all siblings up-to-date\n";
1942         update:
1943         for (string_list::const_iterator k = dep.targets.begin(),
1944              k_end = dep.targets.end(); k != k_end; ++k)
1945         {
1946                 status[*k].status = st;
1947         }
1948         return ts;
1949 }
1950
1951 /**
1952  * Change the status of @a target to #Remade or #Uptodate depending on whether
1953  * its modification time changed.
1954  */
1955 static void update_status(std::string const &target)
1956 {
1957         DEBUG_open << "Rechecking status of " << target << "... ";
1958         status_map::iterator i = status.find(target);
1959         assert(i != status.end());
1960         status_t &ts = i->second;
1961         ts.status = Remade;
1962         if (ts.last >= now)
1963         {
1964                 DEBUG_close << "possibly remade\n";
1965                 return;
1966         }
1967         struct stat s;
1968         if (stat(target.c_str(), &s) != 0)
1969         {
1970                 DEBUG_close << "missing\n";
1971                 ts.last = 0;
1972         }
1973         else if (s.st_mtime != ts.last)
1974         {
1975                 DEBUG_close << "remade\n";
1976                 ts.last = s.st_mtime;
1977         }
1978         else
1979         {
1980                 DEBUG_close << "unchanged\n";
1981                 ts.status = Uptodate;
1982         }
1983 }
1984
1985 /**
1986  * Check whether all the prerequisites of @a target ended being up-to-date.
1987  */
1988 static bool still_need_rebuild(std::string const &target)
1989 {
1990         DEBUG_open << "Rechecking obsoleteness of " << target << "... ";
1991         status_map::const_iterator i = status.find(target);
1992         assert(i != status.end());
1993         if (i->second.status != Recheck) return true;
1994         dependency_map::const_iterator j = dependencies.find(target);
1995         assert(j != dependencies.end());
1996         dependency_t const &dep = *j->second;
1997         for (string_set::const_iterator k = dep.deps.begin(),
1998              k_end = dep.deps.end(); k != k_end; ++k)
1999         {
2000                 if (status[*k].status != Uptodate) return true;
2001         }
2002         for (string_list::const_iterator k = dep.targets.begin(),
2003              k_end = dep.targets.end(); k != k_end; ++k)
2004         {
2005                 status[*k].status = Uptodate;
2006         }
2007         DEBUG_close << "no longer obsolete\n";
2008         return false;
2009 }
2010
2011 /** @} */
2012
2013 /**
2014  * @defgroup server Server
2015  *
2016  * @{
2017  */
2018
2019 /**
2020  * Handle job completion.
2021  */
2022 static void complete_job(int job_id, bool success)
2023 {
2024         DEBUG_open << "Completing job " << job_id << "... ";
2025         job_map::iterator i = jobs.find(job_id);
2026         assert(i != jobs.end());
2027         string_list const &targets = i->second.rule.targets;
2028         if (success)
2029         {
2030                 for (string_list::const_iterator j = targets.begin(),
2031                      j_end = targets.end(); j != j_end; ++j)
2032                 {
2033                         update_status(*j);
2034                 }
2035         }
2036         else
2037         {
2038                 DEBUG_close << "failed\n";
2039                 std::cerr << "Failed to build";
2040                 for (string_list::const_iterator j = targets.begin(),
2041                      j_end = targets.end(); j != j_end; ++j)
2042                 {
2043                         status[*j].status = Failed;
2044                         std::cerr << ' ' << *j;
2045                         remove(j->c_str());
2046                 }
2047                 std::cerr << std::endl;
2048         }
2049         jobs.erase(i);
2050 }
2051
2052 /**
2053  * Return the script obtained by substituting variables.
2054  */
2055 static std::string prepare_script(job_t const &job)
2056 {
2057         std::string const &s = job.rule.script;
2058         std::istringstream in(s);
2059         std::ostringstream out;
2060         size_t len = s.size();
2061
2062         while (!in.eof())
2063         {
2064                 size_t pos = in.tellg(), p = s.find('$', pos);
2065                 if (p == std::string::npos || p == len - 1) p = len;
2066                 out.write(&s[pos], p - pos);
2067                 if (p == len) break;
2068                 ++p;
2069                 switch (s[p])
2070                 {
2071                 case '$':
2072                         out << '$';
2073                         in.seekg(p + 1);
2074                         break;
2075                 case '<':
2076                         if (!job.rule.deps.empty())
2077                                 out << job.rule.deps.front();
2078                         in.seekg(p + 1);
2079                         break;
2080                 case '^':
2081                 {
2082                         bool first = true;
2083                         for (string_list::const_iterator i = job.rule.deps.begin(),
2084                              i_end = job.rule.deps.end(); i != i_end; ++i)
2085                         {
2086                                 if (first) first = false;
2087                                 else out << ' ';
2088                                 out << *i;
2089                         }
2090                         in.seekg(p + 1);
2091                         break;
2092                 }
2093                 case '@':
2094                         assert(!job.rule.targets.empty());
2095                         out << job.rule.targets.front();
2096                         in.seekg(p + 1);
2097                         break;
2098                 case '*':
2099                         out << job.stem;
2100                         in.seekg(p + 1);
2101                         break;
2102                 case '(':
2103                 {
2104                         in.seekg(p - 1);
2105                         bool first = true;
2106                         input_generator gen(in, &job.vars, true);
2107                         while (true)
2108                         {
2109                                 std::string w;
2110                                 input_status s = gen.next(w);
2111                                 if (s == SyntaxError)
2112                                 {
2113                                         // TODO
2114                                         return "false";
2115                                 }
2116                                 if (s == Eof) break;
2117                                 if (first) first = false;
2118                                 else out << ' ';
2119                                 out << w;
2120                         }
2121                         break;
2122                 }
2123                 default:
2124                         // Let dollars followed by an unrecognized character
2125                         // go through. This differs from Make, which would
2126                         // use a one-letter variable.
2127                         out << '$';
2128                         in.seekg(p);
2129                 }
2130         }
2131
2132         return out.str();
2133 }
2134
2135 /**
2136  * Execute the script from @a rule.
2137  */
2138 static status_e run_script(int job_id, job_t const &job)
2139 {
2140         if (show_targets)
2141         {
2142                 std::cout << "Building";
2143                 for (string_list::const_iterator i = job.rule.targets.begin(),
2144                      i_end = job.rule.targets.end(); i != i_end; ++i)
2145                 {
2146                         std::cout << ' ' << *i;
2147                 }
2148                 std::cout << std::endl;
2149         }
2150
2151         ref_ptr<dependency_t> dep;
2152         dep->targets = job.rule.targets;
2153         dep->deps.insert(job.rule.deps.begin(), job.rule.deps.end());
2154         for (string_list::const_iterator i = job.rule.targets.begin(),
2155              i_end = job.rule.targets.end(); i != i_end; ++i)
2156         {
2157                 dependencies[*i] = dep;
2158         }
2159
2160         std::string script = prepare_script(job);
2161
2162         std::ostringstream job_id_buf;
2163         job_id_buf << job_id;
2164         std::string job_id_ = job_id_buf.str();
2165
2166         DEBUG_open << "Starting script for job " << job_id << "... ";
2167         if (script.empty())
2168         {
2169                 DEBUG_close << "no script\n";
2170                 complete_job(job_id, true);
2171                 return Remade;
2172         }
2173
2174         if (false)
2175         {
2176                 error:
2177                 DEBUG_close << "failed\n";
2178                 complete_job(job_id, false);
2179                 return Failed;
2180         }
2181
2182 #ifdef WINDOWS
2183         HANDLE pfd[2];
2184         if (false)
2185         {
2186                 error2:
2187                 CloseHandle(pfd[0]);
2188                 CloseHandle(pfd[1]);
2189                 goto error;
2190         }
2191         if (!CreatePipe(&pfd[0], &pfd[1], NULL, 0))
2192                 goto error;
2193         if (!SetHandleInformation(pfd[0], HANDLE_FLAG_INHERIT, HANDLE_FLAG_INHERIT))
2194                 goto error2;
2195         STARTUPINFO si;
2196         ZeroMemory(&si, sizeof(STARTUPINFO));
2197         si.cb = sizeof(STARTUPINFO);
2198         si.hStdError = GetStdHandle(STD_ERROR_HANDLE);
2199         si.hStdOutput = GetStdHandle(STD_OUTPUT_HANDLE);
2200         si.hStdInput = pfd[0];
2201         si.dwFlags |= STARTF_USESTDHANDLES;
2202         PROCESS_INFORMATION pi;
2203         ZeroMemory(&pi, sizeof(PROCESS_INFORMATION));
2204         if (!SetEnvironmentVariable("REMAKE_JOB_ID", job_id_.c_str()))
2205                 goto error2;
2206         char const *argv = echo_scripts ? "SH.EXE -e -s -v" : "SH.EXE -e -s";
2207         if (!CreateProcess(NULL, (char *)argv, NULL, NULL,
2208             true, 0, NULL, NULL, &si, &pi))
2209         {
2210                 goto error2;
2211         }
2212         CloseHandle(pi.hThread);
2213         DWORD len = script.length(), wlen;
2214         if (!WriteFile(pfd[1], script.c_str(), len, &wlen, NULL) || wlen < len)
2215                 std::cerr << "Unexpected failure while sending script to shell" << std::endl;
2216         CloseHandle(pfd[0]);
2217         CloseHandle(pfd[1]);
2218         ++running_jobs;
2219         job_pids[pi.hProcess] = job_id;
2220         return Running;
2221 #else
2222         int pfd[2];
2223         if (false)
2224         {
2225                 error2:
2226                 close(pfd[0]);
2227                 close(pfd[1]);
2228                 goto error;
2229         }
2230         if (pipe(pfd) == -1)
2231                 goto error;
2232         if (setenv("REMAKE_JOB_ID", job_id_.c_str(), 1))
2233                 goto error2;
2234         if (pid_t pid = vfork())
2235         {
2236                 if (pid == -1) goto error2;
2237                 ssize_t len = script.length();
2238                 if (write(pfd[1], script.c_str(), len) < len)
2239                         std::cerr << "Unexpected failure while sending script to shell" << std::endl;
2240                 close(pfd[0]);
2241                 close(pfd[1]);
2242                 ++running_jobs;
2243                 job_pids[pid] = job_id;
2244                 return Running;
2245         }
2246         // Child process starts here. Notice the use of vfork above.
2247         char const *argv[5] = { "sh", "-e", "-s", NULL, NULL };
2248         if (echo_scripts) argv[3] = "-v";
2249         close(pfd[1]);
2250         if (pfd[0] != 0)
2251         {
2252                 dup2(pfd[0], 0);
2253                 close(pfd[0]);
2254         }
2255         execve("/bin/sh", (char **)argv, environ);
2256         _exit(EXIT_FAILURE);
2257 #endif
2258 }
2259
2260 /**
2261  * Create a job for @a target according to the loaded rules.
2262  * Mark all the targets from the rule as running and reset their dependencies.
2263  * Inherit variables from @a current, if enabled.
2264  * If the rule has dependencies, create a new client to build them just
2265  * before @a current, and change @a current so that it points to it.
2266  */
2267 static status_e start(std::string const &target, client_list::iterator &current)
2268 {
2269         int job_id = job_counter++;
2270         DEBUG_open << "Starting job " << job_id << " for " << target << "... ";
2271         job_t &job = jobs[job_id];
2272         find_rule(job, target);
2273         if (job.rule.targets.empty())
2274         {
2275                 status[target].status = Failed;
2276                 DEBUG_close << "failed\n";
2277                 std::cerr << "No rule for building " << target << std::endl;
2278                 return Failed;
2279         }
2280         for (string_list::const_iterator i = job.rule.targets.begin(),
2281              i_end = job.rule.targets.end(); i != i_end; ++i)
2282         {
2283                 status[*i].status = Running;
2284         }
2285         if (propagate_vars) job.vars = current->vars;
2286         for (assign_map::const_iterator i = job.rule.assigns.begin(),
2287              i_end = job.rule.assigns.end(); i != i_end; ++i)
2288         {
2289                 std::pair<variable_map::iterator, bool> k =
2290                         job.vars.insert(std::make_pair(i->first, string_list()));
2291                 string_list &v = k.first->second;
2292                 if (i->second.append)
2293                 {
2294                         if (k.second)
2295                         {
2296                                 variable_map::const_iterator j = variables.find(i->first);
2297                                 if (j != variables.end()) v = j->second;
2298                         }
2299                 }
2300                 else if (!k.second) v.clear();
2301                 v.insert(v.end(), i->second.value.begin(), i->second.value.end());
2302         }
2303         if (!job.rule.deps.empty() || !job.rule.wdeps.empty())
2304         {
2305                 current = clients.insert(current, client_t());
2306                 current->job_id = job_id;
2307                 current->pending = job.rule.deps;
2308                 current->pending.insert(current->pending.end(),
2309                         job.rule.wdeps.begin(), job.rule.wdeps.end());
2310                 if (propagate_vars) current->vars = job.vars;
2311                 current->delayed = true;
2312                 return Recheck;
2313         }
2314         return run_script(job_id, job);
2315 }
2316
2317 /**
2318  * Send a reply to a client then remove it.
2319  * If the client was a dependency client, start the actual script.
2320  */
2321 static void complete_request(client_t &client, bool success)
2322 {
2323         DEBUG_open << "Completing request from client of job " << client.job_id << "... ";
2324         if (client.delayed)
2325         {
2326                 assert(client.socket == INVALID_SOCKET);
2327                 if (success)
2328                 {
2329                         job_map::const_iterator i = jobs.find(client.job_id);
2330                         assert(i != jobs.end());
2331                         if (still_need_rebuild(i->second.rule.targets.front()))
2332                                 run_script(client.job_id, i->second);
2333                         else complete_job(client.job_id, true);
2334                 }
2335                 else complete_job(client.job_id, false);
2336         }
2337         else if (client.socket != INVALID_SOCKET)
2338         {
2339                 char res = success ? 1 : 0;
2340                 send(client.socket, &res, 1, MSG_NOSIGNAL);
2341         #ifdef WINDOWS
2342                 closesocket(client.socket);
2343         #else
2344                 close(client.socket);
2345         #endif
2346                 --waiting_jobs;
2347         }
2348
2349         if (client.job_id < 0 && !success) build_failure = true;
2350 }
2351
2352 /**
2353  * Return whether there are slots for starting new jobs.
2354  */
2355 static bool has_free_slots()
2356 {
2357         if (max_active_jobs <= 0) return true;
2358         return running_jobs - waiting_jobs < max_active_jobs;
2359 }
2360
2361 /**
2362  * Handle client requests:
2363  * - check for running targets that have finished,
2364  * - start as many pending targets as allowed,
2365  * - complete the request if there are neither running nor pending targets
2366  *   left or if any of them failed.
2367  *
2368  * @return true if some child processes are still running.
2369  *
2370  * @post If there are pending requests, at least one child process is running.
2371  *
2372  * @invariant New free slots cannot appear during a run, since the only way to
2373  *            decrease #running_jobs is #finalize_job and the only way to
2374  *            increase #waiting_jobs is #accept_client. None of these functions
2375  *            are called during a run. So breaking out as soon as there no free
2376  *            slots left is fine.
2377  */
2378 static bool handle_clients()
2379 {
2380         DEBUG_open << "Handling client requests... ";
2381         restart:
2382         bool need_restart = false;
2383
2384         for (client_list::iterator i = clients.begin(), i_next = i,
2385              i_end = clients.end(); i != i_end && has_free_slots(); i = i_next)
2386         {
2387                 ++i_next;
2388                 DEBUG_open << "Handling client from job " << i->job_id << "... ";
2389
2390                 // Remove running targets that have finished.
2391                 for (string_set::iterator j = i->running.begin(), j_next = j,
2392                      j_end = i->running.end(); j != j_end; j = j_next)
2393                 {
2394                         ++j_next;
2395                         status_map::const_iterator k = status.find(*j);
2396                         assert(k != status.end());
2397                         switch (k->second.status)
2398                         {
2399                         case Running:
2400                                 break;
2401                         case Failed:
2402                                 i->failed = true;
2403                                 if (!keep_going) goto complete;
2404                                 // no break
2405                         case Uptodate:
2406                         case Remade:
2407                                 i->running.erase(j);
2408                                 break;
2409                         case Recheck:
2410                         case Todo:
2411                                 assert(false);
2412                         }
2413                 }
2414
2415                 // Start pending targets.
2416                 while (!i->pending.empty())
2417                 {
2418                         std::string target = i->pending.front();
2419                         i->pending.pop_front();
2420                         switch (get_status(target).status)
2421                         {
2422                         case Running:
2423                                 i->running.insert(target);
2424                                 break;
2425                         case Failed:
2426                                 pending_failed:
2427                                 i->failed = true;
2428                                 if (!keep_going) goto complete;
2429                                 // no break
2430                         case Uptodate:
2431                         case Remade:
2432                                 break;
2433                         case Recheck:
2434                         case Todo:
2435                                 client_list::iterator j = i;
2436                                 switch (start(target, i))
2437                                 {
2438                                 case Failed:
2439                                         goto pending_failed;
2440                                 case Running:
2441                                         // A shell was started, check for free slots.
2442                                         j->running.insert(target);
2443                                         if (!has_free_slots()) return true;
2444                                         break;
2445                                 case Recheck:
2446                                         // Switch to the dependency client that was inserted.
2447                                         j->running.insert(target);
2448                                         i_next = j;
2449                                         break;
2450                                 case Remade:
2451                                         // Nothing to run.
2452                                         need_restart = true;
2453                                         break;
2454                                 default:
2455                                         assert(false);
2456                                 }
2457                         }
2458                 }
2459
2460                 // Try to complete the request.
2461                 // (This might start a new job if it was a dependency client.)
2462                 if (i->running.empty() || i->failed)
2463                 {
2464                         complete:
2465                         complete_request(*i, !i->failed);
2466                         DEBUG_close << (i->failed ? "failed\n" : "finished\n");
2467                         clients.erase(i);
2468                         need_restart = true;
2469                 }
2470         }
2471
2472         if (running_jobs != waiting_jobs) return true;
2473         if (running_jobs == 0 && clients.empty()) return false;
2474         if (need_restart) goto restart;
2475
2476         // There is a circular dependency.
2477         // Try to break it by completing one of the requests.
2478         assert(!clients.empty());
2479         std::cerr << "Circular dependency detected" << std::endl;
2480         client_list::iterator i = clients.begin();
2481         complete_request(*i, false);
2482         clients.erase(i);
2483         goto restart;
2484 }
2485
2486 /**
2487  * Create a named unix socket that listens for build requests. Also set
2488  * the REMAKE_SOCKET environment variable that will be inherited by all
2489  * the job scripts.
2490  */
2491 static void create_server()
2492 {
2493         if (false)
2494         {
2495                 error:
2496                 perror("Failed to create server");
2497 #ifndef WINDOWS
2498                 error2:
2499 #endif
2500                 exit(EXIT_FAILURE);
2501         }
2502         DEBUG_open << "Creating server... ";
2503
2504 #ifdef WINDOWS
2505         // Prepare a windows socket.
2506         struct sockaddr_in socket_addr;
2507         socket_addr.sin_family = AF_INET;
2508         socket_addr.sin_addr.s_addr = inet_addr("127.0.0.1");
2509         socket_addr.sin_port = 0;
2510
2511         // Create and listen to the socket.
2512         socket_fd = socket(AF_INET, SOCK_STREAM, 0);
2513         if (socket_fd == INVALID_SOCKET) goto error;
2514         if (!SetHandleInformation((HANDLE)socket_fd, HANDLE_FLAG_INHERIT, 0))
2515                 goto error;
2516         if (bind(socket_fd, (struct sockaddr *)&socket_addr, sizeof(sockaddr_in)))
2517                 goto error;
2518         int len = sizeof(sockaddr_in);
2519         if (getsockname(socket_fd, (struct sockaddr *)&socket_addr, &len))
2520                 goto error;
2521         std::ostringstream buf;
2522         buf << socket_addr.sin_port;
2523         if (!SetEnvironmentVariable("REMAKE_SOCKET", buf.str().c_str()))
2524                 goto error;
2525         if (listen(socket_fd, 1000)) goto error;
2526 #else
2527         // Set signal handlers for SIGCHLD and SIGINT.
2528         // Block SIGCHLD (unblocked during select).
2529         sigset_t sigmask;
2530         sigemptyset(&sigmask);
2531         sigaddset(&sigmask, SIGCHLD);
2532         if (sigprocmask(SIG_BLOCK, &sigmask, NULL) == -1) goto error;
2533         struct sigaction sa;
2534         sa.sa_flags = 0;
2535         sigemptyset(&sa.sa_mask);
2536         sa.sa_handler = &sigchld_handler;
2537         if (sigaction(SIGCHLD, &sa, NULL) == -1) goto error;
2538         sa.sa_handler = &sigint_handler;
2539         if (sigaction(SIGINT, &sa, NULL) == -1) goto error;
2540
2541         // Prepare a named unix socket in temporary directory.
2542         socket_name = tempnam(NULL, "rmk-");
2543         if (!socket_name) goto error2;
2544         struct sockaddr_un socket_addr;
2545         size_t len = strlen(socket_name);
2546         if (len >= sizeof(socket_addr.sun_path) - 1) goto error2;
2547         socket_addr.sun_family = AF_UNIX;
2548         strcpy(socket_addr.sun_path, socket_name);
2549         len += sizeof(socket_addr.sun_family);
2550         if (setenv("REMAKE_SOCKET", socket_name, 1)) goto error;
2551
2552         // Create and listen to the socket.
2553 #ifdef LINUX
2554         socket_fd = socket(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0);
2555         if (socket_fd == INVALID_SOCKET) goto error;
2556 #else
2557         socket_fd = socket(AF_UNIX, SOCK_STREAM, 0);
2558         if (socket_fd == INVALID_SOCKET) goto error;
2559         if (fcntl(socket_fd, F_SETFD, FD_CLOEXEC) < 0) goto error;
2560 #endif
2561         if (bind(socket_fd, (struct sockaddr *)&socket_addr, len))
2562                 goto error;
2563         if (listen(socket_fd, 1000)) goto error;
2564 #endif
2565 }
2566
2567 /**
2568  * Accept a connection from a client, get the job it spawned from,
2569  * get the targets, and mark them as dependencies of the job targets.
2570  */
2571 static void accept_client()
2572 {
2573         DEBUG_open << "Handling client request... ";
2574
2575         // Accept connection.
2576 #ifdef WINDOWS
2577         socket_t fd = accept(socket_fd, NULL, NULL);
2578         if (fd == INVALID_SOCKET) return;
2579         if (!SetHandleInformation((HANDLE)fd, HANDLE_FLAG_INHERIT, 0))
2580         {
2581                 error2:
2582                 std::cerr << "Unexpected failure while setting connection with client" << std::endl;
2583                 closesocket(fd);
2584                 return;
2585         }
2586         // WSAEventSelect puts sockets into nonblocking mode, so disable it here.
2587         u_long nbio = 0;
2588         if (ioctlsocket(fd, FIONBIO, &nbio)) goto error2;
2589 #elif defined(LINUX)
2590         int fd = accept4(socket_fd, NULL, NULL, SOCK_CLOEXEC);
2591         if (fd < 0) return;
2592 #else
2593         int fd = accept(socket_fd, NULL, NULL);
2594         if (fd < 0) return;
2595         if (fcntl(fd, F_SETFD, FD_CLOEXEC) < 0) return;
2596 #endif
2597         clients.push_front(client_t());
2598         client_list::iterator proc = clients.begin();
2599
2600         if (false)
2601         {
2602                 error:
2603                 DEBUG_close << "failed\n";
2604                 std::cerr << "Received an ill-formed client message" << std::endl;
2605         #ifdef WINDOWS
2606                 closesocket(fd);
2607         #else
2608                 close(fd);
2609         #endif
2610                 clients.erase(proc);
2611                 return;
2612         }
2613
2614         // Receive message. Stop when encountering two nuls in a row.
2615         std::vector<char> buf;
2616         size_t len = 0;
2617         while (len < sizeof(int) + 2 || buf[len - 1] || buf[len - 2])
2618         {
2619                 buf.resize(len + 1024);
2620                 ssize_t l = recv(fd, &buf[0] + len, 1024, 0);
2621                 if (l <= 0) goto error;
2622                 len += l;
2623         }
2624
2625         // Parse job that spawned the client.
2626         int job_id;
2627         memcpy(&job_id, &buf[0], sizeof(int));
2628         proc->socket = fd;
2629         proc->job_id = job_id;
2630         job_map::const_iterator i = jobs.find(job_id);
2631         if (i == jobs.end()) goto error;
2632         DEBUG << "receiving request from job " << job_id << std::endl;
2633         if (propagate_vars) proc->vars = i->second.vars;
2634
2635         // Parse the targets and the variable assignments.
2636         // Mark the targets as dependencies of the job targets.
2637         dependency_t &dep = *dependencies[i->second.rule.targets.front()];
2638         string_list *last_var = NULL;
2639         char const *p = &buf[0] + sizeof(int);
2640         while (true)
2641         {
2642                 len = strlen(p);
2643                 if (len == 0)
2644                 {
2645                         ++waiting_jobs;
2646                         break;
2647                 }
2648                 switch (*p)
2649                 {
2650                 case 'T':
2651                 {
2652                         if (len == 1) goto error;
2653                         std::string target(p + 1, p + len);
2654                         DEBUG << "adding dependency " << target << " to job\n";
2655                         proc->pending.push_back(target);
2656                         dep.deps.insert(target);
2657                         break;
2658                 }
2659                 case 'V':
2660                 {
2661                         if (len == 1) goto error;
2662                         std::string var(p + 1, p + len);
2663                         DEBUG << "adding variable " << var << " to job\n";
2664                         last_var = &proc->vars[var];
2665                         last_var->clear();
2666                         break;
2667                 }
2668                 case 'W':
2669                 {
2670                         if (!last_var) goto error;
2671                         last_var->push_back(std::string(p + 1, p + len));
2672                         break;
2673                 }
2674                 default:
2675                         goto error;
2676                 }
2677                 p += len + 1;
2678         }
2679
2680         if (!propagate_vars && !proc->vars.empty())
2681         {
2682                 std::cerr << "Assignments are ignored unless 'variable-propagation' is enabled" << std::endl;
2683                 proc->vars.clear();
2684         }
2685 }
2686
2687 /**
2688  * Handle child process exit status.
2689  */
2690 static void finalize_job(pid_t pid, bool res)
2691 {
2692         pid_job_map::iterator i = job_pids.find(pid);
2693         assert(i != job_pids.end());
2694         int job_id = i->second;
2695         job_pids.erase(i);
2696         --running_jobs;
2697         complete_job(job_id, res);
2698 }
2699
2700 /**
2701  * Loop until all the jobs have finished.
2702  *
2703  * @post There are no client requests left, not even virtual ones.
2704  */
2705 static void server_loop()
2706 {
2707         while (handle_clients())
2708         {
2709                 DEBUG_open << "Handling events... ";
2710         #ifdef WINDOWS
2711                 size_t len = job_pids.size() + 1;
2712                 HANDLE h[len];
2713                 int num = 0;
2714                 for (pid_job_map::const_iterator i = job_pids.begin(),
2715                      i_end = job_pids.end(); i != i_end; ++i, ++num)
2716                 {
2717                         h[num] = i->first;
2718                 }
2719                 WSAEVENT aev = WSACreateEvent();
2720                 h[num] = aev;
2721                 WSAEventSelect(socket_fd, aev, FD_ACCEPT);
2722                 DWORD w = WaitForMultipleObjects(len, h, false, INFINITE);
2723                 WSAEventSelect(socket_fd, aev, 0);
2724                 WSACloseEvent(aev);
2725                 if (len <= w)
2726                         continue;
2727                 if (w == len - 1)
2728                 {
2729                         accept_client();
2730                         continue;
2731                 }
2732                 pid_t pid = h[w];
2733                 DWORD s = 0;
2734                 bool res = GetExitCodeProcess(pid, &s) && s == 0;
2735                 CloseHandle(pid);
2736                 finalize_job(pid, res);
2737         #else
2738                 sigset_t emptymask;
2739                 sigemptyset(&emptymask);
2740                 fd_set fdset;
2741                 FD_ZERO(&fdset);
2742                 FD_SET(socket_fd, &fdset);
2743                 int ret = pselect(socket_fd + 1, &fdset, NULL, NULL, NULL, &emptymask);
2744                 if (ret > 0 /* && FD_ISSET(socket_fd, &fdset)*/) accept_client();
2745                 if (!got_SIGCHLD) continue;
2746                 got_SIGCHLD = 0;
2747                 pid_t pid;
2748                 int status;
2749                 while ((pid = waitpid(-1, &status, WNOHANG)) > 0)
2750                 {
2751                         bool res = WIFEXITED(status) && WEXITSTATUS(status) == 0;
2752                         finalize_job(pid, res);
2753                 }
2754         #endif
2755         }
2756
2757         assert(clients.empty());
2758 }
2759
2760 /**
2761  * Load dependencies and rules, listen to client requests, and loop until
2762  * all the requests have completed.
2763  * If Remakefile is obsolete, perform a first run with it only, then reload
2764  * the rules, and perform a second with the original clients.
2765  */
2766 static void server_mode(std::string const &remakefile, string_list const &targets)
2767 {
2768         load_dependencies();
2769         load_rules(remakefile);
2770         create_server();
2771         if (get_status(remakefile).status != Uptodate)
2772         {
2773                 clients.push_back(client_t());
2774                 clients.back().pending.push_back(remakefile);
2775                 server_loop();
2776                 if (build_failure) goto early_exit;
2777                 variables.clear();
2778                 specific_rules.clear();
2779                 generic_rules.clear();
2780                 first_target.clear();
2781                 load_rules(remakefile);
2782         }
2783         clients.push_back(client_t());
2784         if (!targets.empty()) clients.back().pending = targets;
2785         else if (!first_target.empty())
2786                 clients.back().pending.push_back(first_target);
2787         server_loop();
2788         early_exit:
2789         close(socket_fd);
2790 #ifndef WINDOWS
2791         remove(socket_name);
2792         free(socket_name);
2793 #endif
2794         save_dependencies();
2795         exit(build_failure ? EXIT_FAILURE : EXIT_SUCCESS);
2796 }
2797
2798 /** @} */
2799
2800 /**
2801  * @defgroup client Client
2802  *
2803  * @{
2804  */
2805
2806 /**
2807  * Connect to the server @a socket_name, send a request for building @a targets
2808  * with some @a variables, and exit with the status returned by the server.
2809  */
2810 static void client_mode(char *socket_name, string_list const &targets)
2811 {
2812         if (false)
2813         {
2814                 error:
2815                 perror("Failed to send targets to server");
2816                 exit(EXIT_FAILURE);
2817         }
2818         if (targets.empty()) exit(EXIT_SUCCESS);
2819         DEBUG_open << "Connecting to server... ";
2820
2821         // Connect to server.
2822 #ifdef WINDOWS
2823         struct sockaddr_in socket_addr;
2824         socket_fd = socket(AF_INET, SOCK_STREAM, 0);
2825         if (socket_fd == INVALID_SOCKET) goto error;
2826         socket_addr.sin_family = AF_INET;
2827         socket_addr.sin_addr.s_addr = inet_addr("127.0.0.1");
2828         socket_addr.sin_port = atoi(socket_name);
2829         if (connect(socket_fd, (struct sockaddr *)&socket_addr, sizeof(sockaddr_in)))
2830                 goto error;
2831 #else
2832         struct sockaddr_un socket_addr;
2833         size_t len = strlen(socket_name);
2834         if (len >= sizeof(socket_addr.sun_path) - 1) exit(EXIT_FAILURE);
2835         socket_fd = socket(AF_UNIX, SOCK_STREAM, 0);
2836         if (socket_fd == INVALID_SOCKET) goto error;
2837         socket_addr.sun_family = AF_UNIX;
2838         strcpy(socket_addr.sun_path, socket_name);
2839         if (connect(socket_fd, (struct sockaddr *)&socket_addr, sizeof(socket_addr.sun_family) + len))
2840                 goto error;
2841 #ifdef MACOSX
2842         int set_option = 1;
2843         if (setsockopt(socket_fd, SOL_SOCKET, SO_NOSIGPIPE, &set_option, sizeof(set_option)))
2844                 goto error;
2845 #endif
2846 #endif
2847
2848         // Send current job id.
2849         char *id = getenv("REMAKE_JOB_ID");
2850         int job_id = id ? atoi(id) : -1;
2851         if (send(socket_fd, (char *)&job_id, sizeof(job_id), MSG_NOSIGNAL) != sizeof(job_id))
2852                 goto error;
2853
2854         // Send targets.
2855         for (string_list::const_iterator i = targets.begin(),
2856              i_end = targets.end(); i != i_end; ++i)
2857         {
2858                 DEBUG_open << "Sending target " << *i << "... ";
2859                 std::string s = 'T' + *i;
2860                 ssize_t len = s.length() + 1;
2861                 if (send(socket_fd, s.c_str(), len, MSG_NOSIGNAL) != len)
2862                         goto error;
2863         }
2864
2865         // Send variables.
2866         for (variable_map::const_iterator i = variables.begin(),
2867              i_end = variables.end(); i != i_end; ++i)
2868         {
2869                 DEBUG_open << "Sending variable " << i->first << "... ";
2870                 std::string s = 'V' + i->first;
2871                 ssize_t len = s.length() + 1;
2872                 if (send(socket_fd, s.c_str(), len, MSG_NOSIGNAL) != len)
2873                         goto error;
2874                 for (string_list::const_iterator j = i->second.begin(),
2875                      j_end = i->second.end(); j != j_end; ++j)
2876                 {
2877                         std::string s = 'W' + *j;
2878                         len = s.length() + 1;
2879                         if (send(socket_fd, s.c_str(), len, MSG_NOSIGNAL) != len)
2880                                 goto error;
2881                 }
2882         }
2883
2884         // Send terminating nul and wait for reply.
2885         char result = 0;
2886         if (send(socket_fd, &result, 1, MSG_NOSIGNAL) != 1) goto error;
2887         if (recv(socket_fd, &result, 1, 0) != 1) exit(EXIT_FAILURE);
2888         exit(result ? EXIT_SUCCESS : EXIT_FAILURE);
2889 }
2890
2891 /** @} */
2892
2893 /**
2894  * @defgroup ui User interface
2895  *
2896  * @{
2897  */
2898
2899 /**
2900  * Display usage and exit with @a exit_status.
2901  */
2902 static void usage(int exit_status)
2903 {
2904         std::cerr << "Usage: remake [options] [target] ...\n"
2905                 "Options\n"
2906                 "  -d                     Echo script commands.\n"
2907                 "  -d -d                  Print lots of debugging information.\n"
2908                 "  -f FILE                Read FILE as Remakefile.\n"
2909                 "  -h, --help             Print this message and exit.\n"
2910                 "  -j[N], --jobs=[N]      Allow N jobs at once; infinite jobs with no arg.\n"
2911                 "  -k                     Keep going when some targets cannot be made.\n"
2912                 "  -r                     Look up targets from the dependencies on stdin.\n"
2913                 "  -s, --silent, --quiet  Do not echo targets.\n";
2914         exit(exit_status);
2915 }
2916
2917 /**
2918  * This program behaves in two different ways.
2919  *
2920  * - If the environment contains the REMAKE_SOCKET variable, the client
2921  *   connects to this socket and sends to the server its build targets.
2922  *   It exits once it receives the server reply.
2923  *
2924  * - Otherwise, it creates a server that waits for build requests. It
2925  *   also creates a pseudo-client that requests the targets passed on the
2926  *   command line.
2927  */
2928 int main(int argc, char *argv[])
2929 {
2930         init_working_dir();
2931
2932         std::string remakefile;
2933         string_list targets;
2934         bool literal_targets = false;
2935         bool indirect_targets = false;
2936
2937         // Parse command-line arguments.
2938         for (int i = 1; i < argc; ++i)
2939         {
2940                 std::string arg = argv[i];
2941                 if (arg.empty()) usage(EXIT_FAILURE);
2942                 if (literal_targets) goto new_target;
2943                 if (arg == "-h" || arg == "--help") usage(EXIT_SUCCESS);
2944                 if (arg == "-d")
2945                         if (echo_scripts) debug.active = true;
2946                         else echo_scripts = true;
2947                 else if (arg == "-k" || arg =="--keep-going")
2948                         keep_going = true;
2949                 else if (arg == "-s" || arg == "--silent" || arg == "--quiet")
2950                         show_targets = false;
2951                 else if (arg == "-r")
2952                         indirect_targets = true;
2953                 else if (arg == "-f")
2954                 {
2955                         if (++i == argc) usage(EXIT_FAILURE);
2956                         remakefile = argv[i];
2957                 }
2958                 else if (arg == "--")
2959                         literal_targets = true;
2960                 else if (arg.compare(0, 2, "-j") == 0)
2961                         max_active_jobs = atoi(arg.c_str() + 2);
2962                 else if (arg.compare(0, 7, "--jobs=") == 0)
2963                         max_active_jobs = atoi(arg.c_str() + 7);
2964                 else
2965                 {
2966                         if (arg[0] == '-') usage(EXIT_FAILURE);
2967                         if (arg.find('=') != std::string::npos)
2968                         {
2969                                 std::istringstream in(arg);
2970                                 std::string name = read_word(in);
2971                                 if (name.empty() || !expect_token(in, Equal)) usage(EXIT_FAILURE);
2972                                 read_words(in, variables[name]);
2973                                 continue;
2974                         }
2975                         new_target:
2976                         targets.push_back(normalize(arg, working_dir, working_dir));
2977                         DEBUG << "New target: " << arg << '\n';
2978                 }
2979         }
2980
2981         if (indirect_targets)
2982         {
2983                 load_dependencies(std::cin);
2984                 string_list l;
2985                 targets.swap(l);
2986                 if (l.empty() && !dependencies.empty())
2987                 {
2988                         l.push_back(dependencies.begin()->second->targets.front());
2989                 }
2990                 for (string_list::const_iterator i = l.begin(),
2991                      i_end = l.end(); i != i_end; ++i)
2992                 {
2993                         dependency_map::const_iterator j = dependencies.find(*i);
2994                         if (j == dependencies.end()) continue;
2995                         dependency_t const &dep = *j->second;
2996                         for (string_set::const_iterator k = dep.deps.begin(),
2997                              k_end = dep.deps.end(); k != k_end; ++k)
2998                         {
2999                                 targets.push_back(normalize(*k, working_dir, working_dir));
3000                         }
3001                 }
3002                 dependencies.clear();
3003         }
3004
3005 #ifdef WINDOWS
3006         WSADATA wsaData;
3007         if (WSAStartup(MAKEWORD(2,2), &wsaData))
3008         {
3009                 std::cerr << "Unexpected failure while initializing Windows Socket" << std::endl;
3010                 return 1;
3011         }
3012 #endif
3013
3014         // Run as client if REMAKE_SOCKET is present in the environment.
3015         if (char *sn = getenv("REMAKE_SOCKET")) client_mode(sn, targets);
3016
3017         // Otherwise run as server.
3018         if (remakefile.empty())
3019         {
3020                 remakefile = "Remakefile";
3021                 init_prefix_dir();
3022         }
3023         normalize_list(targets, working_dir, prefix_dir);
3024         server_mode(remakefile, targets);
3025 }
3026
3027 /** @} */