Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
#define _XOPEN_SOURCE 700

#include "errors.h"
#include "fac.h"

#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <limits.h>
#include <sys/stat.h>
#include <assert.h>

#ifdef _WIN32
#include <windows.h>
/* fixme: the following is a version of realpath for windows! */
char *realpath(const char *p, char *unused) {
  char *r = malloc(4096);
  GetFullPathName(p, 4096, r, 0);
  return r;
}
#else
#include <unistd.h>
#endif


#if defined(_WIN32) || defined(__APPLE__)
int getline(char **lineptr, size_t *n, FILE *stream) {
  if (*n == 0) {
	  *n = 80;
	  *lineptr = realloc(*lineptr, *n);
  }
  int toread = *n;
  char *p = *lineptr;
  char *fgetsoutput = "foo";
  while ((fgetsoutput = fgets(p, toread, stream)) && strlen(p) == toread-1) {
    toread = *n;
    *n *= 2;
    *lineptr = realloc(*lineptr, *n);
    p = *lineptr + toread - 1;
  }
  if (!fgetsoutput) return -1;
  return strlen(*lineptr);
}
#endif

char *absolute_path(const char *dir, const char *rel) {
  char *myrel = strdup(rel);
  if (*rel == '/') return myrel;
  int len = strlen(myrel);
  for (int i=len-1;i>=0;i--) {
    if (myrel[i] == '/') {
      int lastbit_length = len - i - 1;
      myrel[i] = 0;
      len = strlen(dir)+strlen(myrel)+2;
      char *filename = malloc(len);
      if (snprintf(filename, len, "%s/%s", dir, myrel) >= len)
        error_at_line(1,0, __FILE__, __LINE__, "filename too large!!!");
      char *thepath = realpath(filename, NULL);
      if (!thepath) {
        if (errno != ENOENT) {
          fprintf(stderr, "Difficulty disambiguating %s: %s\n",
                  filename, strerror(errno));
        }
        thepath = filename;
      } else {
        free(filename);
      }
      free(myrel);
      len = strlen(thepath);
      thepath = realloc(thepath, len+lastbit_length+2);
      len = snprintf(thepath+len, lastbit_length+2, "/%s", rel+strlen(rel)-lastbit_length);
      assert(len < lastbit_length+2);
      return thepath;
    }
  }
  free(myrel);
  char *thepath = realpath(dir, NULL);
  if (!thepath)
#ifdef _WIN32
    error_at_line(1,0, __FILE__, __LINE__, "filename trouble");
#else
    error_at_line(1,errno, __FILE__, __LINE__, "filename trouble");
#endif
  int rel_len = strlen(rel);
  int dirlen = strlen(thepath);
  thepath = realloc(thepath, dirlen + rel_len + 2);
  int bytes_printed = snprintf(thepath+dirlen, rel_len+2, "/%s", rel);
  assert(bytes_printed < rel_len+2);
  return thepath;
}

static const char *relative_path(const char *myroot, const char *path) {
  int len = strlen(myroot);
  int pathlen = strlen(path);
  if (pathlen > len && path[len] == '/' && !memcmp(path, myroot, len)) {
    return path + len + 1;
  }
  return path;
}


char *done_name(const char *facfilename) {
  int bflen = strlen(facfilename);
  char *str = malloc(bflen+5);
  sprintf(str, "%s.tum", facfilename);
  return str;
}

void read_fac_file(struct all_targets *all, const char *path) {
  FILE *f = fopen(path, "r");
  if (!f) {
    fprintf(stderr, "error: unable to open file %s\n  %s\n",
            path, strerror(errno));
    exit(1);
  }

  char *rel_directory = strdup(path);
  for (int i=strlen(rel_directory)-1;i>=0;i--) {
    if (rel_directory[i] == '/') {
      rel_directory[i] = 0;
      break;
    }
  }
  char *the_directory = realpath(rel_directory, NULL);
  if (strlen(rel_directory) == strlen(path)) {
    free(rel_directory);
    free(the_directory);
    rel_directory = ".";
    the_directory = realpath(rel_directory, NULL);
  } else {
    free(rel_directory);
  }

  struct rule *therule = NULL;
  struct target *thetarget = NULL;
  struct hashstat *stat_last_file = NULL;

  int linenum = 0;
  char *one_line = NULL;
  size_t buffer_length = 0;
  while (getline(&one_line, &buffer_length, f) >= 0) {
    linenum++;
    int line_length = strlen(one_line);
    if (line_length > 0 && one_line[line_length-1] == '\n')
      one_line[line_length-- -1] = 0; /* trim newline */

    if (line_length < 2) continue;
    if (one_line[0] == '#') continue; /* it is a comment! */

    if (one_line[1] != ' ')
      error_at_line(1, 0, pretty_path(path), linenum,
                    "Second character of line should be a space");
    switch (one_line[0]) {
    case '?':
    case '|':
      {
        struct rule *existing = lookup_rule(all, one_line+2, the_directory);
        if (existing)
          error_at_line(1, 0, pretty_path(path), linenum,
                        "duplicate rule:  %s\nalso defined in %s:%d\n",
                        one_line+2, existing->facfile_path, existing->facfile_linenum);
        therule = create_rule(all, path, one_line+2, the_directory);
        therule->facfile_linenum = linenum;
        thetarget = NULL;
        stat_last_file = NULL;
        if (one_line[0] == '?') therule->is_default = false;
      }
      break;
    case 'C':
      if (!therule)
        error_at_line(1, 0, pretty_path(path), linenum,
                      "\"C\" cache lines must follow a \"|\" command line");
      {
        const char *prefix = one_line+2;
        if (strlen(prefix) > 2 && prefix[0] == '~' && prefix[1] == '/') {
          /* It is in the home directory.  We use realpath to handle
             the case where the home directory defined in $HOME
             actually is a symlink to the real home directory. */
          const char *home = realpath(getenv("HOME"), NULL);
          if (!home) {
            fprintf(stderr, "ignoring %s directive since $HOME does not exist...\n",
                    one_line);
          } else {
            const int len = strlen(home) + strlen(prefix) + 1;
            char *absolute_prefix = malloc(len);
            strncpy(absolute_prefix, home, len);
            strncat(absolute_prefix, prefix+1, len);
            add_cache_prefix(therule, absolute_prefix);
            free(absolute_prefix);
          }
        } else {
          char *path = absolute_path(the_directory, one_line+2);
          add_cache_prefix(therule, path);
          free(path);
        }
      }
      break;
    case 'c':
      if (!therule)
        error_at_line(1, 0, pretty_path(path), linenum,
                      "\"c\" cache lines must follow a \"|\" command line");
      add_cache_suffix(therule, one_line+2);
      break;
    case '<':
      if (!therule)
        error_at_line(1, 0, pretty_path(path), linenum,
                      "\"<\" input lines must follow a \"|\" command line");
      {
        char *path = absolute_path(the_directory, one_line+2);
        thetarget = create_target(all, path);
        add_explicit_input(therule, thetarget);
        stat_last_file = &therule->input_stats[therule->num_inputs-1];
        free(path);
      }
      break;
    case '>':
      if (!therule)
        error_at_line(1, 0, pretty_path(path), linenum,
                      "\">\" output lines must follow a \"|\" command line");
      {
        if (one_line[2] == '/' || one_line[2] == '~') {
          // this looks like an absolute path, which is wrong.
          error_at_line(1, 0, pretty_path(path), linenum,
                        "\">\" cannot be followed by an absolute path (%s)\n",
                        one_line+2);
        }
        char *filepath = absolute_path(the_directory, one_line+2);
        thetarget = create_target(all, filepath);
        if (thetarget->rule && therule != thetarget->rule) {
          error_at_line(1, 0, pretty_path(path), linenum,
                        "two rules to create the same file: %s\n| %s (in %s:%d)\n| %s (in %s:%d)\n",
                        one_line+2,
                        therule->command,
                        pretty_path(path),
                        therule->facfile_linenum,
                        thetarget->rule->command,
                        pretty_path(thetarget->rule->facfile_path),
                        thetarget->rule->facfile_linenum);
        }
        if (therule != thetarget->rule) {
          thetarget->rule = therule;
          add_explicit_output(therule, thetarget);
          stat_last_file = &therule->output_stats[therule->num_outputs-1];
          free(filepath);
        }
      }
      break;
    }
  }
  if (!feof(f)) {
    fprintf(stderr, "error: reading file %s\n  %s", path, strerror(errno));
    exit(1);
  }
  fclose(f);

  char *donename = done_name(path);
  /* am_deleting_output indicates if a rule has been removed, and we
     need to clean up the output of said rule. */
  bool am_deleting_output = false;
  f = fopen(donename, "r");
  if (f) {
    linenum = 0;
    while (getline(&one_line, &buffer_length, f) >= 0) {
      linenum++;
      int line_length = strlen(one_line);
      if (line_length > 0 && one_line[line_length-1] == '\n')
        one_line[line_length-- -1] = 0; /* trim newline */

      if (line_length < 2) continue;
      if (one_line[0] == '#') continue; /* it is a comment! */

      /* in case of error in the done file, just ignore the rest of
         it. */
      if (one_line[1] != ' ') break;

      switch (one_line[0]) {
      case '|':
        am_deleting_output = false;
        therule = lookup_rule(all, one_line+2, the_directory);
        if (!therule) am_deleting_output = true;
        //printf(":: %s\n", therule->command);
        thetarget = NULL;
        stat_last_file = NULL;
        break;
      case '<':
        if (therule) {
          char *path = absolute_path(the_directory, one_line+2);
          if (is_interesting_path(therule, path)) {
            thetarget = create_target(all, path);
            /* The following check is to deal with the case where a
               given file might actually be an output of a command, but
               was only read when being built, by a program that tries
               to be friendly to make by avoiding touching a file that
               would end up being identical when built. */
            if (thetarget->rule == therule) {
              /* It was actually an output, so let's trust the user and
                 treat it as such. */
              add_output(therule, thetarget);
              for (int i=0; i<therule->num_outputs; i++) {
                if (thetarget == therule->outputs[i]) {
                  stat_last_file = &therule->output_stats[i];
                  break;
                }
              }
            } else {
              //printf("  I see #%d %s\n", therule->num_inputs-1, path);
              add_input(therule, thetarget);
              for (int i=0;i<therule->num_inputs;i++) {
                if (thetarget == therule->inputs[i]) {
                  stat_last_file = &therule->input_stats[i];
                  break;
                }
              }
            }
          } else {
            // It is cached, so we need to ignore any stats of this file!
            thetarget = NULL;
            stat_last_file = NULL;
          }
          free(path);
        }
        break;
      case '>':
        if (therule) {
          char *path = absolute_path(the_directory, one_line+2);
          if (is_interesting_path(therule, path)) {
            thetarget = create_target(all, path);
            if (!thetarget->rule && is_facfile(path)) {
              /* We ignore any generated facfiles that were not
                 explicitly requested! */
              thetarget->status = dirty; /* this says to not read this file */
            } else {
              if (!thetarget->rule || thetarget->rule == therule) {
                thetarget->rule = therule;
                add_output(therule, thetarget);
                for (int i=0; i<therule->num_outputs; i++) {
                  if (thetarget == therule->outputs[i]) {
                    stat_last_file = &therule->output_stats[i];
                    break;
                  }
                }
              }
            }
          } else {
            /* It is a cache file, so we need to ignore any stats of
               this file! */
            thetarget = NULL;
            stat_last_file = NULL;
          }
          free(path);
        } else if (am_deleting_output) {
          char *path = absolute_path(the_directory, one_line+2);
          unlink(path);
          free(path);
        }
        break;
      case 'T':
        if (stat_last_file) {
          /* ignore errors in the done file: */
          stat_last_file->time = strtol(one_line+2, NULL, 0);
        }
        break;
      case 'S':
        if (stat_last_file) {
          /* ignore errors in the done file: */
          stat_last_file->size = strtol(one_line+2, NULL, 0);
        }
        break;
      case 'H':
        if (stat_last_file) {
          stat_last_file->hash = read_sha1(one_line+2);
        }
        break;
      case 'B':
        if (therule) {
          all->estimated_times[therule->status] -= therule->build_time;
          /* ignore errors in the done file: */
          sscanf(one_line+2, "%lg", &therule->build_time);
          all->estimated_times[therule->status] += therule->build_time;
        }
        break;
      }
    }
    fclose(f);
  }

  free(donename);
  free(one_line);
  free(the_directory);
}

void fprint_facfile(FILE *f, struct all_targets *tt, const char *bpath) {
  for (struct rule *r = (struct rule *)tt->r.first; r; r = (struct rule *)r->e.next) {
    if (!strcmp(r->facfile_path, bpath)) {
      fprintf(f, "| %s\n", r->command);
      if (r->status != failed) {
        if (r->build_time) {
          fprintf(f, "B %g\n", r->build_time);
        }
      }
      for (int i=0; i<r->num_outputs; i++) {
        fprintf(f, "> %s\n", relative_path(r->working_directory, r->outputs[i]->path));
        if (r->output_stats[i].time && r->status != failed) {
          fprintf(f, "T %ld\n", (long)r->output_stats[i].time);
          fprintf(f, "S %ld\n", (long)r->output_stats[i].size);
          sha1hash h = r->output_stats[i].hash;
          if (h.abc.a || h.abc.b || h.abc.c) {
            fprintf(f, "H ");
            fprint_sha1(f, h);
            fprintf(f, "\n");
          }
        }
      }
      for (int i=0; i<r->num_inputs; i++) {
        fprintf(f, "< %s\n", relative_path(r->working_directory, r->inputs[i]->path));
        if (r->input_stats[i].time) {
          fprintf(f, "T %ld\n", (long)r->input_stats[i].time);
          fprintf(f, "S %ld\n", (long)r->input_stats[i].size);
          sha1hash h = r->input_stats[i].hash;
          if (h.abc.a || h.abc.b || h.abc.c) {
            fprintf(f, "H ");
            fprint_sha1(f, h);
            fprintf(f, "\n");
          }
        }
      }
      fprintf(f, "\n");
    }
  }
}

static int target_cmp(const void *rr1, const void *rr2) {
  const struct target *r1 = *(struct target **)rr1;
  const struct target *r2 = *(struct target **)rr2;
  return strcmp(r1->path, r2->path);
}

static void sort_target_array(struct target **t, int num) {
  qsort(t, num, sizeof(struct target *), target_cmp);
}

static void fprint_makefile_escape(FILE *f, const char *path) {
  while (*path) {
    switch (*path) {
    case ' ':
    case '$':
    case '\\':
    case '"':
    case '\'':
      fputc('\\', f);
      fputc(*path, f);
      break;
    default:
      fputc(*path, f);
    }
    path++;
  }
}

static sha1hash dot_nodename(const struct target *t) {
  sha1nfo s;
  sha1_init(&s);
  sha1_write(&s, t->path, strlen(t->path));
  return sha1_out(&s);
}

static const int max_inputs_for_dot = 70;

static void fprint_dot_rule(FILE *f, struct rule *r) {
  if (r->is_printed) return;
  r->is_printed = true;
  int num_repo_inputs = 0;
  for (int i=0;i<r->num_inputs;i++) {
    if (is_in_root(r->inputs[i]->path)) num_repo_inputs++;
  }
  if (num_repo_inputs > max_inputs_for_dot) return;

  /* We sort the inputs and outputs before creating the makefile, so
     as to generate output that is the same on every computer, and on
     every invocation.  This is helpful because often one wants to put
     the generated file into the git repository for the benefit of
     users who have not installed fac. */
  struct target **inps = malloc(r->num_inputs*sizeof(struct target *));
  for (int i=0; i<r->num_inputs; i++) inps[i] = r->inputs[i];
  sort_target_array(inps, r->num_inputs);

  for (int i=0; i<r->num_inputs; i++) {
    if (inps[i]->rule) fprint_dot_rule(f, inps[i]->rule);
  }

  struct target **outs = malloc(r->num_outputs*sizeof(struct target *));
  for (int i=0; i<r->num_outputs; i++) outs[i] = r->outputs[i];
  sort_target_array(outs, r->num_outputs);

  for (int i=0; i<r->num_inputs; i++) {
    if (is_in_root(inps[i]->path)) {
      for (int j=0; j<r->num_outputs; j++) {
        fprintf(f, "   node");
        fprint_sha1(f, dot_nodename(inps[i]));
        fprintf(f, " -> node");
        fprint_sha1(f, dot_nodename(outs[j]));
        fprintf(f, ";\n");
      }
    }
  }
  free(inps);
  free(outs);
}

static void fprint_dot_nodes(FILE *f, struct rule *r) {
  if (r->is_printed) return;
  const int lenroot = strlen(root);
  r->is_printed = true;

  /* We sort the inputs and outputs before creating the makefile, so
     as to generate output that is the same on every computer, and on
     every invocation.  This is helpful because often one wants to put
     the generated file into the git repository for the benefit of
     users who have not installed fac. */
  struct target **inps = malloc(r->num_inputs*sizeof(struct target *));
  for (int i=0; i<r->num_inputs; i++) inps[i] = r->inputs[i];
  sort_target_array(inps, r->num_inputs);

  for (int i=0; i<r->num_inputs; i++) {
    if (inps[i]->rule) fprint_dot_nodes(f, inps[i]->rule);
  }

  bool is_intermediate = false;
  for (int i=0;i<r->num_outputs;i++) {
    if (r->outputs[i]->num_children) is_intermediate = true;
  }
  fprintf(f, "# %s\n", r->command);
  const char *color = "black";
  if (is_intermediate) color = "blue";
  for (int i=0;i<r->num_outputs;i++) {
    if (!r->outputs[i]->is_printed) {
      fprintf(f, "   node");
      fprint_sha1(f, dot_nodename(r->outputs[i]));
      fprintf(f, "[label=\"%s\", color=\"%s\"];\n",
              r->outputs[i]->path + lenroot+1, color);
      r->outputs[i]->is_printed = true;
    }
  }

  int num_repo_inputs = 0;
  for (int i=0;i<r->num_inputs;i++) {
    if (is_in_root(r->inputs[i]->path)) num_repo_inputs++;
  }
  if (num_repo_inputs > max_inputs_for_dot) return;

  for (int i=0;i<r->num_inputs;i++) {
    if (!r->inputs[i]->rule) {
      if (!r->inputs[i]->is_printed && is_in_root(r->inputs[i]->path)) {
        fprintf(f, "   node");
        fprint_sha1(f, dot_nodename(r->inputs[i]));
        if (is_in_root(r->inputs[i]->path)) {
          fprintf(f, "[label=\"%s\", color=\"red\"];\n",
                  r->inputs[i]->path + lenroot+1);
        } else {
          fprintf(f, "[label=\"%s\", color=\"green\"];\n",
                  r->inputs[i]->path);
        }
        r->inputs[i]->is_printed = true;
      }
    }
  }
}

void fprint_dot(FILE *f, struct all_targets *all) {
  fprintf(f, "digraph G {\n  rankdir=LR;\n");

  for (struct target *t = (struct target *)all->t.first; t; t = (struct target *)t->e.next) {
    t->is_printed = false;
  }
  for (struct rule *r = (struct rule *)all->r.first; r; r = (struct rule *)r->e.next) {
    r->is_printed = false;
  }

  for (struct rule *r = all->lists[marked]; r; r = r->status_next) {
    fprint_dot_nodes(f, r);
  }

  fprintf(f, "\n\n");

  for (struct rule *r = (struct rule *)all->r.first; r; r = (struct rule *)r->e.next) {
    r->is_printed = false;
  }
  for (struct rule *r = all->lists[marked]; r; r = r->status_next) {
    fprint_dot_rule(f, r);
  }
  fprintf(f, "}\n");
}

static void fprint_makefile_rule(FILE *f, struct rule *r) {
  if (r->is_printed) return;
  r->is_printed = true;
  const int lenroot = strlen(root);

  /* We sort the inputs and outputs before creating the makefile, so
     as to generate output that is the same on every computer, and on
     every invocation.  This is helpful because often one wants to put
     the generated file into the git repository for the benefit of
     users who have not installed fac. */
  struct target **inps = malloc(r->num_inputs*sizeof(struct target *));
  for (int i=0; i<r->num_inputs; i++) inps[i] = r->inputs[i];
  sort_target_array(inps, r->num_inputs);

  struct target **outs = malloc(r->num_outputs*sizeof(struct target *));
  for (int i=0; i<r->num_outputs; i++) outs[i] = r->outputs[i];
  sort_target_array(outs, r->num_outputs);

  for (int i=0; i<r->num_inputs; i++) {
    if (inps[i]->rule) fprint_makefile_rule(f, inps[i]->rule);
  }
  for (int i=0; i<r->num_outputs; i++) {
    fprint_makefile_escape(f, outs[i]->path + lenroot+1);
    fprintf(f, " ");
  }
  fprintf(f, ":");
  for (int i=0; i<r->num_inputs; i++) {
    if (is_in_root(inps[i]->path)) {
      fprintf(f, " ");
      fprint_makefile_escape(f, inps[i]->path + lenroot+1);
    }
  }
  if (is_in_root(r->working_directory)) {
    fprintf(f, "\n\tcd ");
    fprint_makefile_escape(f, r->working_directory + lenroot+1);
    fprintf(f, " && %s\n\n", r->command);
  } else {
    fprintf(f, "\n\t%s\n\n", r->command);
  }
  free(inps);
  free(outs);
}

static void fprint_makeclean_rule(FILE *f, struct rule *r) {
  if (r->is_printed) return;
  r->is_printed = true;
  const int lenroot = strlen(root);

  /* We sort the outputs before creating the rule, so as to generate
     output that is the same on every computer, and on every
     invocation.  This is helpful because often one wants to put the
     generated file into the git repository for the benefit of users
     who have not installed fac. */
  struct target **inps = malloc(r->num_inputs*sizeof(struct target *));
  for (int i=0; i<r->num_inputs; i++) inps[i] = r->inputs[i];
  sort_target_array(inps, r->num_inputs);

  struct target **outs = malloc(r->num_outputs*sizeof(struct target *));
  for (int i=0; i<r->num_outputs; i++) outs[i] = r->outputs[i];
  sort_target_array(outs, r->num_outputs);

  for (int i=0; i<r->num_inputs; i++) {
    if (inps[i]->rule) fprint_makeclean_rule(f, inps[i]->rule);
  }

  for (int i=0; i<r->num_outputs; i++) {
    fprintf(f, " ");
    fprint_makefile_escape(f, outs[i]->path + lenroot+1);
  }
  free(inps);
  free(outs);
}

void fprint_makefile(FILE *f, struct all_targets *all) {
  fprintf(f, ".PHONY: all clean\n");
  for (struct rule *r = (struct rule *)all->r.first; r; r = (struct rule *)r->e.next) {
    r->is_printed = false;
  }

  const int lenroot = strlen(root);
  fprintf(f, "all:");
  for (struct rule *r = all->lists[marked]; r; r = r->status_next) {
    /* First, let's identify whether this rule produces intermediate
       output, in which case it won't need to be included in the "all"
       target. */
    bool is_intermediate = false;
    for (int i=0;i<r->num_outputs;i++) {
      if (r->outputs[i]->num_children) is_intermediate = true;
    }
    if (r->num_outputs && !is_intermediate) {
      /* Only print one output per rule that we are building to save
         space.  Also, if there are no outputs yet (because they are
         not specified in the .fac file, and we have not yet built)
         that is a user error. */
      fprintf(f, " %s", r->outputs[0]->path + lenroot+1);
    } else if (r->status == marked) {
      /* A target was intermediate, but it was explicitly requested,
         so we should list it as part of "all" */
      fprintf(f, " %s", r->outputs[0]->path + lenroot+1);
    }
  }
  fprintf(f, "\n\n");

  /* Now we create the "clean" rule */
  fprintf(f, "clean:\n\trm -f");
  for (struct rule *r = all->lists[marked]; r; r = r->status_next) {
    fprint_makeclean_rule(f, r);
  }
  fprintf(f, "\n\n");

  for (struct rule *r = (struct rule *)all->r.first; r; r = (struct rule *)r->e.next) {
    r->is_printed = false;
  }
  for (struct rule *r = all->lists[marked]; r; r = r->status_next) {
    fprint_makefile_rule(f, r);
  }
}

static void fprint_script_rule(FILE *f, struct rule *r) {
  if (r->is_printed) return;
  r->is_printed = true;
  const int lenroot = strlen(root);

  /* We sort the inputs and outputs before creating the script, so
     as to generate output that is the same on every computer, and on
     every invocation.  This is helpful because often one wants to put
     the generated file into the git repository for the benefit of
     users who have not installed fac. */
  struct target **inps = malloc(r->num_inputs*sizeof(struct target *));
  for (int i=0; i<r->num_inputs; i++) inps[i] = r->inputs[i];
  sort_target_array(inps, r->num_inputs);

  for (int i=0; i<r->num_inputs; i++) {
    if (inps[i]->rule) fprint_script_rule(f, inps[i]->rule);
  }
  free(inps);
  if (is_in_root(r->working_directory)) {
    fprintf(f, "(cd ");
    fprint_makefile_escape(f, r->working_directory + lenroot+1);
    fprintf(f, " && %s)\n\n", r->command);
  } else {
    fprintf(f, "(%s)\n\n", r->command);
  }
}

void fprint_script(FILE *f, struct all_targets *all) {
  fprintf(f, "#!/bin/sh\n\nset -ev\n\n");
  for (struct rule *r = (struct rule *)all->r.first; r; r = (struct rule *)r->e.next) {
    r->is_printed = false;
  }
  for (struct rule *r = all->lists[marked]; r; r = r->status_next) {
    fprint_script_rule(f, r);
  }
}

static void fprint_tupfile_rule(FILE *f, struct rule *r) {
  if (r->is_printed) return;
  r->is_printed = true;
  const int lenroot = strlen(root);
  for (int i=0; i<r->num_inputs; i++) {
    if (r->inputs[i]->rule) fprint_tupfile_rule(f, r->inputs[i]->rule);
  }
  fprintf(f, ": ");
  for (int i=0; i<r->num_inputs; i++) {
    if (is_in_root(r->inputs[i]->path)) {
      fprint_makefile_escape(f, r->inputs[i]->path + lenroot+1);
      fprintf(f, " ");
    }
  }
  if (is_in_root(r->working_directory)) {
    fprintf(f, "|> cd ");
    fprint_makefile_escape(f, r->working_directory + lenroot+1);
    fprintf(f, " && %s |>", r->command);
  } else {
    fprintf(f, "|> %s |>", r->command);
  }
  for (int i=0; i<r->num_outputs; i++) {
    fprintf(f, " ");
    fprint_makefile_escape(f, r->outputs[i]->path + lenroot+1);
  }
  fprintf(f, "\n\n");
}

void fprint_tupfile(FILE *f, struct all_targets *all) {
  for (struct rule *r = (struct rule *)all->r.first; r; r = (struct rule *)r->e.next) {
    r->is_printed = false;
  }
  for (struct rule *r = all->lists[marked]; r; r = r->status_next) {
    fprint_tupfile_rule(f, r);
  }
}

static void cp_rule(const char *dir, struct rule *r) {
  if (r->is_printed) return;
  r->is_printed = true;
  const int lenroot = strlen(root);
  for (int i=0; i<r->num_inputs; i++) {
    if (r->inputs[i]->rule) {
      cp_rule(dir, r->inputs[i]->rule);
    } else if (is_in_root(r->inputs[i]->path)) {
      if (r->inputs[i]->is_file && r->inputs[i]->is_in_git) {
        // this is a source file, so we should copy it!
        // printf("cp %s %s/\n", r->inputs[i]->path + lenroot + 1, dir);
        cp_to_dir(r->inputs[i]->path + lenroot + 1, dir);
      }
    }
  }
}

void cp_inputs(const char *dir, struct all_targets *all) {
  for (struct rule *r = (struct rule *)all->r.first; r; r = (struct rule *)r->e.next) {
    r->is_printed = false;
  }
  for (struct rule *r = all->lists[marked]; r; r = r->status_next) {
    cp_rule(dir, r);
  }
}