aboutsummaryrefslogtreecommitdiff
path: root/build2/install/rule.cxx
blob: 9f668f9bcb498e89d2077ab76672c974eb2e165a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
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
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
// file      : build2/install/rule.cxx -*- C++ -*-
// copyright : Copyright (c) 2014-2017 Code Synthesis Ltd
// license   : MIT; see accompanying LICENSE file

#include <build2/install/rule>

#include <butl/filesystem> // dir_exists(), file_exists()

#include <build2/scope>
#include <build2/target>
#include <build2/algorithm>
#include <build2/filesystem>
#include <build2/diagnostics>

using namespace std;
using namespace butl;

namespace build2
{
  namespace install
  {
    // Lookup the install or install.* variable. Return NULL if not found or
    // if the value is the special 'false' name (which means do not install).
    // T is either scope or target.
    //
    template <typename P, typename T>
    static const P*
    lookup_install (T& t, const string& var)
    {
      auto l (t[var]);

      if (!l)
        return nullptr;

      const P& r (cast<P> (l));
      return r.simple () && r.string () == "false" ? nullptr : &r;
    }

    // alias_rule
    //
    match_result alias_rule::
    match (action, target&, const string&) const
    {
      return true;
    }

    recipe alias_rule::
    apply (action a, target& t) const
    {
      tracer trace ("install::alias_rule::apply");

      for (const prerequisite& p: group_prerequisites (t))
      {
        const target& pt (search (t, p));

        // Check if this prerequisite is explicitly "not installable",
        // that is, there is the 'install' variable and its value is
        // false.
        //
        // At first, this might seem redundand since we could have let
        // the file_rule below take care of it. The nuance is this: this
        // prerequsite can be in a different subproject that hasn't loaded
        // the install module (and therefore has no file_rule registered).
        // The typical example would be the 'tests' subproject.
        //
        // Note: not the same as lookup() above.
        //
        auto l (pt["install"]);

        if (l && cast<path> (l).string () == "false")
        {
          l5 ([&]{trace << "ignoring " << pt;});
          continue;
        }

        build2::match (a, pt);
        t.prerequisite_targets.push_back (&pt);
      }

      return default_recipe;
    }

    // file_rule
    //
    struct match_data
    {
      bool install;
    };

    static_assert (sizeof (match_data) <= target::data_size,
                   "insufficient space");

    match_result file_rule::
    match (action a, target& t, const string&) const
    {
      // First determine if this target should be installed (called
      // "installable" for short).
      //
      match_data md {lookup_install<path> (t, "install") != nullptr};
      match_result mr (true);

      if (a.operation () == update_id)
      {
        // If this is the update pre-operation and the target is installable,
        // change the recipe action to (update, 0) (i.e., "unconditional
        // update") so that we don't get matched for its prerequisites.
        //
        if (md.install)
          mr.recipe_action = action (a.meta_operation (), update_id);
        else
          // Otherwise, signal that we don't match so that some other rule can
          // take care of it.
          //
          return false;
      }

      t.data (md); // Save the data in the target's auxilary storage.
      return mr;
    }

    const target* file_rule::
    filter (action, const target& t, prerequisite_member p) const
    {
      const target& pt (p.search (t));
      return pt.in (t.root_scope ()) ? &pt : nullptr;
    }

    recipe file_rule::
    apply (action a, target& t) const
    {
      match_data md (move (t.data<match_data> ()));
      t.clear_data (); // In case delegated-to rule (or the rule that overrides
                       // us; see cc/install) also uses aux storage.

      if (!md.install) // Not installable.
        return noop_recipe;

      // Ok, if we are here, then this means:
      //
      // 1. This target is installable.
      // 2. The action is either
      //    a. (perform, [un]install, 0) or
      //    b. (*, update, [un]install)
      //
      // In both cases, the next step is to search, match, and collect all the
      // installable prerequisites.
      //
      // @@ Perhaps if [noinstall] will be handled by the
      // group_prerequisite_members machinery, then we can just
      // run standard search_and_match()? Will need an indicator
      // that it was forced (e.g., [install]) for filter() below.
      //
      auto r (group_prerequisite_members (a, t));
      for (auto i (r.begin ()); i != r.end (); ++i)
      {
        prerequisite_member p (*i);

        // Ignore unresolved targets that are imported from other projects.
        // We are definitely not installing those.
        //
        if (p.proj ())
          continue;

        // Let a customized rule have its say.
        //
        const target* pt (filter (a, t, p));
        if (pt == nullptr)
          continue;

        // See if we were explicitly instructed not to touch this target.
        //
        auto l ((*pt)["install"]);
        if (l && cast<path> (l).string () == "false")
          continue;

        // If the matched rule returned noop_recipe, then the target
        // state will be set to unchanged as an optimization. Use this
        // knowledge to optimize things on our side as well since this
        // will help a lot in case of any static installable content
        // (headers, documentation, etc).
        //
        if (!build2::match (a, *pt, unmatch::unchanged))
          t.prerequisite_targets.push_back (pt);

        // Skip members of ad hoc groups. We handle them explicitly below.
        //
        if (pt->adhoc_group ())
          i.leave_group ();
      }

      // This is where we diverge depending on the operation. In the
      // update pre-operation, we need to make sure that this target
      // as well as all its installable prerequisites are up to date.
      //
      if (a.operation () == update_id)
      {
        // Save the prerequisite targets that we found since the
        // call to match_delegate() below will wipe them out.
        //
        target::prerequisite_targets_type p;

        if (!t.prerequisite_targets.empty ())
          p.swap (t.prerequisite_targets);

        // Find the "real" update rule, that is, the rule that would
        // have been found if we signalled that we do not match from
        // match() above.
        //
        recipe d (match_delegate (a, t, *this).first);

        // If we have no installable prerequisites, then simply redirect
        // to it.
        //
        if (p.empty ())
          return d;

        // Ok, the worst case scenario: we need to cause update of
        // prerequisite targets and also delegate to the real update.
        //
        return [pt = move (p), dr = move (d)] (
          action a, const target& t) mutable -> target_state
        {
          // Do the target update first.
          //
          target_state r (execute_delegate (dr, a, t));

          // Swap our prerequisite targets back in and execute.
          //
          t.prerequisite_targets.swap (pt);
          r |= straight_execute_prerequisites (a, t);
          pt.swap (t.prerequisite_targets); // In case we get re-executed.

          return r;
        };
      }
      else if (a.operation () == install_id)
        return [this] (action a, const target& t)
        {
          return perform_install (a, t);
        };
      else
        return [this] (action a, const target& t)
        {
          return perform_uninstall (a, t);
        };
    }

    void file_rule::
    install_extra (const file&, const install_dir&) const {}

    bool file_rule::
    uninstall_extra (const file&, const install_dir&) const {return false;}

    struct install_dir
    {
      dir_path dir;

      // If not NULL, then point to the corresponding install.* value.
      //
      const string*  sudo     = nullptr;
      const path*    cmd      = nullptr;
      const strings* options  = nullptr;
      const string*  mode     = nullptr;
      const string*  dir_mode = nullptr;

      explicit
      install_dir (dir_path d = dir_path ()): dir (move (d)) {}

      install_dir (dir_path d, const install_dir& b)
          : dir (move (d)),
            sudo (b.sudo),
            cmd (b.cmd),
            options (b.options),
            mode (b.mode),
            dir_mode (b.dir_mode) {}
    };

    using install_dirs = vector<install_dir>;

    // Calculate a subdirectory based on l's location (*.subdirs) and if not
    // empty add it to install_dirs. Return the new last element.
    //
    static install_dir&
    resolve_subdir (install_dirs& rs,
                    const target& t,
                    const scope& s,
                    const lookup& l)
    {
      // Find the scope from which this value came and use as a base
      // to calculate the subdirectory.
      //
      for (const scope* p (&s); p != nullptr; p = p->parent_scope ())
      {
        if (l.belongs (*p)) // Ok since no target/type in lookup.
        {
          // The target can be in out or src.
          //
          const dir_path& d (t.out_dir ().leaf (p->out_path ()));

          // Add it as another leading directory rather than modifying
          // the last one directly; somehow, it feels right.
          //
          if (!d.empty ())
            rs.emplace_back (rs.back ().dir / d, rs.back ());
          break;
        }
      }

      return rs.back ();
    }

    // Resolve installation directory name to absolute directory path. Return
    // all the super-directories leading up to the destination (last).
    //
    static install_dirs
    resolve (const target& t, dir_path d, const string* var = nullptr)
    {
      install_dirs rs;

      if (d.absolute ())
        rs.emplace_back (move (d.normalize ()));
      else
      {
        // If it is relative, then the first component is treated as the
        // installation directory name, e.g., bin, sbin, lib, etc. Look it
        // up and recurse.
        //
        const string& sn (*d.begin ());
        const string var ("install." + sn);
        if (const dir_path* dn =
            lookup_install<dir_path> (t.base_scope (), var))
        {
          if (dn->empty ())
            fail << "empty installation directory for name " << sn <<
              info << "did you specified empty config." << var << "?";

          rs = resolve (t, *dn, &var);
          d = rs.back ().dir / dir_path (++d.begin (), d.end ());
          rs.emplace_back (move (d.normalize ()), rs.back ());
        }
        else
          fail << "unknown installation directory name '" << sn << "'" <<
            info << "did you forget to specify config." << var << "?";
      }

      install_dir* r (&rs.back ());
      const scope& s (t.base_scope ());

      // Override components in install_dir if we have our own.
      //
      if (var != nullptr)
      {
        if (auto l = s[*var + ".sudo"])     r->sudo     = &cast<string> (l);
        if (auto l = s[*var + ".cmd"])      r->cmd      = &cast<path> (l);
        if (auto l = s[*var + ".mode"])     r->mode     = &cast<string> (l);
        if (auto l = s[*var + ".dir_mode"]) r->dir_mode = &cast<string> (l);
        if (auto l = s[*var + ".options"])  r->options  = &cast<strings> (l);

        if (auto l = s[*var + ".subdirs"])
        {
          if (cast<bool> (l))
            r = &resolve_subdir (rs, t, s, l);
        }
      }

      // Set globals for unspecified components.
      //
      if (r->sudo == nullptr)
        r->sudo = cast_null<string> (s["config.install.sudo"]);

      if (r->cmd == nullptr)
        r->cmd = &cast<path> (s["config.install.cmd"]);

      if (r->options == nullptr)
        r->options = cast_null<strings> (s["config.install.options"]);

      if (r->mode == nullptr)
        r->mode = &cast<string> (s["config.install.mode"]);

      if (r->dir_mode == nullptr)
        r->dir_mode = &cast<string> (s["config.install.dir_mode"]);

      return rs;
    }

    // On Windows we use MSYS2 install.exe and MSYS2 by default ignores
    // filesystem permissions (noacl mount option). And this means, for
    // example, that .exe that we install won't be runnable by Windows (MSYS2
    // itself will still run them since it recognizes the file extension).
    //
    // The way we work around this (at least in our distribution of the MSYS2
    // tools) is by changing the mount option for cygdrives (/c, /d, etc) to
    // acl. But that's not all: we also have to install via a path that "hits"
    // one of those mount points, c:\foo won't work, we have to use /c/foo.
    // So this function translates an absolute Windows path to its MSYS
    // representation.
    //
    // Note that we return the result as a string, not dir_path since path
    // starting with / are illegal on Windows. Also note that the result
    // doesn't have the trailing slash.
    //
    static string
    msys_path (const dir_path& d)
    {
      assert (d.absolute ());
      string s (d.representation ());

      // First replace ':' with the drive letter (so the path is no longer
      // absolute) but postpone setting the first character to / until we are
      // a string.
      //
      s[1] = lcase (s[0]);
      s = dir_path (move (s)).posix_string ();
      s[0] = '/';

      return s;
    }

    // install -d <dir>
    //
    // If verbose is false, then only print the command at verbosity level 2
    // or higher.
    //
    static void
    install_d (const install_dir& base, const dir_path& d, bool verbose = true)
    {
      try
      {
        if (dir_exists (d)) // May throw (e.g., EACCES).
          return;
      }
      catch (const system_error& e)
      {
        fail << "invalid installation directory " << d << ": " << e;
      }

      // While install -d will create all the intermediate components between
      // base and dir, we do it explicitly, one at a time. This way the output
      // is symmetrical to uninstall() below.
      //
      if (d != base.dir)
      {
        dir_path pd (d.directory ());

        if (pd != base.dir)
          install_d (base, pd, verbose);
      }

      cstrings args;

      string reld (
        cast<string> ((*global_scope)["build.host.class"]) == "windows"
        ? msys_path (d)
        : relative (d).string ());

      if (base.sudo != nullptr)
        args.push_back (base.sudo->c_str ());

      args.push_back (base.cmd->string ().c_str ());
      args.push_back ("-d");

      if (base.options != nullptr)
        append_options (args, *base.options);

      args.push_back ("-m");
      args.push_back (base.dir_mode->c_str ());
      args.push_back (reld.c_str ());
      args.push_back (nullptr);

      try
      {
        process_path pp (process::path_search (args[0]));

        if (verb >= 2)
          print_process (args);
        else if (verb && verbose)
          text << "install " << d;

        process pr (pp, args.data ());

        if (!pr.wait ())
          throw failed ();
      }
      catch (const process_error& e)
      {
        error << "unable to execute " << args[0] << ": " << e;

        if (e.child)
          exit (1);

        throw failed ();
      }
    }

    // install <file> <dir>/
    // install <file> <file>
    //
    // If verbose is false, then only print the command at verbosity level 2
    // or higher.
    //
    static void
    install_f (const install_dir& base,
               const path& name,
               const file& t,
               bool verbose)
    {
      path relf (relative (t.path ()));

      string reld (
        cast<string> ((*global_scope)["build.host.class"]) == "windows"
        ? msys_path (base.dir)
        : relative (base.dir).string ());

      if (!name.empty ())
      {
        reld += path::traits::directory_separator;
        reld += name.string ();
      }

      cstrings args;

      if (base.sudo != nullptr)
        args.push_back (base.sudo->c_str ());

      args.push_back (base.cmd->string ().c_str ());

      if (base.options != nullptr)
        append_options (args, *base.options);

      args.push_back ("-m");
      args.push_back (base.mode->c_str ());
      args.push_back (relf.string ().c_str ());
      args.push_back (reld.c_str ());
      args.push_back (nullptr);

      try
      {
        process_path pp (process::path_search (args[0]));

        if (verb >= 2)
          print_process (args);
        else if (verb && verbose)
          text << "install " << t;

        process pr (pp, args.data ());

        if (!pr.wait ())
          throw failed ();
      }
      catch (const process_error& e)
      {
        error << "unable to execute " << args[0] << ": " << e;

        if (e.child)
          exit (1);

        throw failed ();
      }
    }

    void file_rule::
    install_l (const install_dir& base,
               const path& target,
               const path& link,
               bool verbose)
    {
      path rell (relative (base.dir));
      rell /= link;

      // We can create a symlink directly without calling ln. This, however,
      // won't work if we have sudo. Also, we would have to deal with existing
      // destinations (ln's -f takes care of that). So we are just going to
      // always use ln.
      //
      const char* args_a[] = {
        base.sudo != nullptr ? base.sudo->c_str () : nullptr,
        "ln",
        "-sf",
        target.string ().c_str (),
        rell.string ().c_str (),
        nullptr};

      const char** args (&args_a[base.sudo == nullptr ? 1 : 0]);

      try
      {
        process_path pp (process::path_search (args[0]));

        if (verb >= 2)
          print_process (args);
        else if (verb && verbose)
          text << "install " << rell << " -> " << target;

        process pr (pp, args);

        if (!pr.wait ())
          throw failed ();
      }
      catch (const process_error& e)
      {
        error << "unable to execute " << args[0] << ": " << e;

        if (e.child)
          exit (1);

        throw failed ();
      }
    }

    target_state file_rule::
    perform_install (action a, const target& xt) const
    {
      const file& t (xt.as<file> ());
      assert (!t.path ().empty ()); // Should have been assigned by update.

      auto install_target = [this] (const file& t, const path& p, bool verbose)
      {
        bool n (!p.to_directory ());
        dir_path d (n ? p.directory () : path_cast<dir_path> (p));

        // Resolve target directory.
        //
        install_dirs ids (resolve (t, d));

        // Handle install.subdirs if one was specified.
        //
        if (auto l = t["install.subdirs"])
        {
          if (cast<bool> (l))
            resolve_subdir (ids, t, t.base_scope (), l);
        }

        // Create leading directories. Note that we are using the leading
        // directory (if there is one) for the creation information (mode,
        // sudo, etc).
        //
        for (auto i (ids.begin ()), j (i); i != ids.end (); j = i++)
          install_d (*j, i->dir, verbose); // install -d

        install_dir& id (ids.back ());

        // Override mode if one was specified.
        //
        if (auto l = t["install.mode"])
          id.mode = &cast<string> (l);

        // Install the target and extras.
        //
        install_f (id, n ? p.leaf () : path (), t, verbose);
        install_extra (t, id);
      };

      // First handle installable prerequisites.
      //
      target_state r (straight_execute_prerequisites (a, t));

      // Then installable ad hoc group members, if any.
      //
      for (const target* m (t.member); m != nullptr; m = m->member)
      {
        if (const path* p = lookup_install<path> (*m, "install"))
          install_target (m->as<file> (), *p, false);
      }

      // Finally install the target itself (since we got here we know the
      // install variable is there).
      //
      install_target (t, cast<path> (t["install"]), true);

      return (r |= target_state::changed);
    }

    // uninstall -d <dir>
    //
    // We try remove all the directories between base and dir but not base
    // itself unless base == dir. Return false if nothing has been removed
    // (i.e., the directories do not exist or are not empty).
    //
    // If verbose is false, then only print the command at verbosity level 2
    // or higher.
    //
    static bool
    uninstall_d (const install_dir& base, const dir_path& d, bool verbose)
    {
      // Figure out if we should try to remove this directory. Note that if
      // it doesn't exist, then we may still need to remove outer ones.
      //
      bool r (false);
      try
      {
        if ((r = dir_exists (d))) // May throw (e.g., EACCES).
        {
          if (!dir_empty (d)) // May also throw.
            return false; // Won't be able to remove any outer directories.
        }
      }
      catch (const system_error& e)
      {
        fail << "invalid installation directory " << d << ": " << e;
      }

      if (r)
      {
        dir_path reld (relative (d));

        // Normally when we need to remove a file or directory we do it
        // directly without calling rm/rmdir. This however, won't work if we
        // have sudo. So we are going to do it both ways.
        //
        // While there is no sudo on Windows, deleting things that are being
        // used can get complicated. So we will always use rm/rmdir there.
        //
#ifndef _WIN32
        if (base.sudo == nullptr)
        {
          if (verb >= 2)
            text << "rmdir " << reld;
          else if (verb && verbose)
            text << "uninstall " << reld;

          try
          {
            try_rmdir (d);
          }
          catch (const system_error& e)
          {
            fail << "unable to remove directory " << d << ": " << e;
          }
        }
        else
#endif
        {
          const char* args_a[] = {
            base.sudo != nullptr ? base.sudo->c_str () : nullptr,
            "rmdir",
            reld.string ().c_str (),
            nullptr};

          const char** args (&args_a[base.sudo == nullptr ? 1 : 0]);

          try
          {
            process_path pp (process::path_search (args[0]));

            if (verb >= 2)
              print_process (args);
            else if (verb && verbose)
              text << "uninstall " << reld;

            process pr (pp, args);

            if (!pr.wait ())
              throw failed ();
          }
          catch (const process_error& e)
          {
            error << "unable to execute " << args[0] << ": " << e;

            if (e.child)
              exit (1);

            throw failed ();
          }
        }
      }

      // If we have more empty directories between base and dir, then try
      // to clean them up as well.
      //
      if (d != base.dir)
      {
        dir_path pd (d.directory ());

        if (pd != base.dir)
          r = uninstall_d (base, pd, verbose) || r;
      }

      return r;
    }

    bool file_rule::
    uninstall_f (const install_dir& base,
                 const file* t,
                 const path& name,
                 bool verbose)
    {
      assert (t != nullptr || !name.empty ());
      path f (base.dir / (name.empty () ? t->path ().leaf () : name));

      try
      {
        // Note: don't follow symlinks so if the target is a dangling symlinks
        // we will proceed to removing it.
        //
        if (!file_exists (f, false)) // May throw (e.g., EACCES).
          return false;
      }
      catch (const system_error& e)
      {
        fail << "invalid installation path " << f << ": " << e;
      }

      path relf (relative (f));

      if (verb == 1 && verbose)
      {
        if (t != nullptr)
          text << "uninstall " << *t;
        else
          text << "uninstall " << relf;
      }

      // The same story as with uninstall -d.
      //
#ifndef _WIN32
      if (base.sudo == nullptr)
      {
        if (verb >= 2)
          text << "rm " << relf;

        try
        {
          try_rmfile (f);
        }
        catch (const system_error& e)
        {
          fail << "unable to remove file " << f << ": " << e;
        }
      }
      else
#endif
      {
        const char* args_a[] = {
          base.sudo != nullptr ? base.sudo->c_str () : nullptr,
          "rm",
          "-f",
          relf.string ().c_str (),
          nullptr};

        const char** args (&args_a[base.sudo == nullptr ? 1 : 0]);

        try
        {
          process_path pp (process::path_search (args[0]));

          if (verb >= 2)
            print_process (args);

          process pr (pp, args);

          if (!pr.wait ())
            throw failed ();
        }
        catch (const process_error& e)
        {
          error << "unable to execute " << args[0] << ": " << e;

          if (e.child)
            exit (1);

          throw failed ();
        }
      }

      return true;
    }

    target_state file_rule::
    perform_uninstall (action a, const target& xt) const
    {
      const file& t (xt.as<file> ());
      assert (!t.path ().empty ()); // Should have been assigned by update.

      auto uninstall_target = [this] (
        const file& t, const path& p, bool verbose) -> target_state
      {
        bool n (!p.to_directory ());
        dir_path d (n ? p.directory () : path_cast<dir_path> (p));

        // Resolve target directory.
        //
        install_dirs ids (resolve (t, d));

        // Handle install.subdirs if one was specified.
        //
        if (auto l = t["install.subdirs"])
        {
          if (cast<bool> (l))
            resolve_subdir (ids, t, t.base_scope (), l);
        }

        // Remove extras and the target itself.
        //
        const install_dir& id (ids.back ());

        target_state r (uninstall_extra (t, id)
                        ? target_state::changed
                        : target_state::unchanged);

        if (uninstall_f (id, &t, n ? p.leaf () : path (), verbose))
          r |= target_state::changed;

        // Clean up empty leading directories (in reverse).
        //
        // Note that we are using the leading directory (if there is one)
        // for the clean up information (sudo, etc).
        //
        for (auto i (ids.rbegin ()), j (i), e (ids.rend ()); i != e; j = ++i)
        {
          if (install::uninstall_d (++j != e ? *j : *i, i->dir, verbose))
            r |= target_state::changed;
        }

        return r;
      };

      // Reverse order of installation: first the target itself (since we got
      // here we know the install variable is there).
      //
      target_state r (uninstall_target (t, cast<path> (t["install"]), true));

      // Then installable ad hoc group members, if any. To be anally precise
      // we would have to do it in reverse, but that's not easy (it's a
      // single-linked list).
      //
      for (const target* m (t.member); m != nullptr; m = m->member)
      {
        if (const path* p = lookup_install<path> (*m, "install"))
          r |= uninstall_target (
            m->as<file> (), *p, r != target_state::changed);
      }

      // Finally handle installable prerequisites.
      //
      r |= reverse_execute_prerequisites (a, t);

      return r;
    }
  }
}