aboutsummaryrefslogtreecommitdiff
path: root/build/prerequisite.cxx
blob: 7ccf37d1e413e49b946bf677f2cba30f7d0d225a (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
// file      : build/prerequisite.cxx -*- C++ -*-
// copyright : Copyright (c) 2014-2015 Code Synthesis Ltd
// license   : MIT; see accompanying LICENSE file

#include <build/prerequisite>

#include <ostream>

#include <build/scope>
#include <build/target> // target_type
#include <build/context>
#include <build/diagnostics>

using namespace std;

namespace build
{
  ostream&
  operator<< (ostream& os, const prerequisite_key& pk)
  {
    // Print scope unless the prerequisite's directory is absolute.
    //
    if (!pk.tk.dir->absolute ())
    {
      string s (diag_relative (pk.scope->path (), false));

      if (!s.empty ())
        os << s << ':';
    }

    return os << pk.tk;
  }

  // prerequisite_set
  //
  auto prerequisite_set::
  insert (const target_type& tt,
          dir_path dir,
          std::string name,
          const std::string* ext,
          scope& s,
          tracer& trace) -> pair<prerequisite&, bool>
  {
    //@@ OPT: would be nice to somehow first check if this prerequisite is
    //   already in the set before allocating a new instance.

    // Find or insert.
    //
    auto r (emplace (tt, move (dir), move (name), ext, s));
    prerequisite& p (const_cast<prerequisite&> (*r.first));

    // Update extension if the existing prerequisite has it unspecified.
    //
    if (p.ext != ext)
    {
      level4 ([&]{
          diag_record r (trace);
          r << "assuming prerequisite " << p << " is the same as the "
            << "one with ";
          if (ext == nullptr)
            r << "unspecified extension";
          else if (ext->empty ())
            r << "no extension";
          else
            r << "extension " << *ext;
        });

      if (ext != nullptr)
        p.ext = ext;
    }

    return pair<prerequisite&, bool> (p, r.second);
  }
}