aboutsummaryrefslogtreecommitdiff
path: root/build2/search.cxx
blob: 3df88772e71a976f0a46a2ef789fa18267eac206 (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
// file      : build2/search.cxx -*- C++ -*-
// copyright : Copyright (c) 2014-2016 Code Synthesis Ltd
// license   : MIT; see accompanying LICENSE file

#include <build2/search>

#include <utility>  // move()
#include <cassert>

#include <butl/filesystem>

#include <build2/scope>
#include <build2/target>
#include <build2/prerequisite>
#include <build2/diagnostics>

using namespace std;
using namespace butl;

namespace build2
{
  target*
  search_existing_target (const prerequisite_key& pk)
  {
    tracer trace ("search_existing_target");

    const target_key& tk (pk.tk);

    // Look for an existing target in this directory scope.
    //
    dir_path d;
    if (tk.dir->absolute ())
      d = *tk.dir; // Already normalized.
    else
    {
      d = pk.scope->out_path ();

      if (!tk.dir->empty ())
      {
        d /= *tk.dir;
        d.normalize ();
      }
    }

    auto i (targets.find (*tk.type, d, *tk.name, tk.ext, trace));

    if (i == targets.end ())
      return 0;

    target& t (**i);

    level5 ([&]{trace << "existing target " << t << " for prerequisite "
                      << pk;});

    return &t;
  }

  target*
  search_existing_file (const prerequisite_key& cpk, const dir_paths& sp)
  {
    tracer trace ("search_existing_file");

    const target_key& ctk (cpk.tk);
    assert (ctk.dir->relative ());

    // Figure out the extension. Pretty similar logic to file::derive_path().
    //
    const string* ext (ctk.ext);

    if (ext == nullptr)
    {
      if (auto f = ctk.type->extension)
      {
        ext = f (ctk, *cpk.scope); // Already from the pool.
      }

      if (ext == nullptr)
      {
        // What should we do here, fail or say we didn't find anything?
        // Current think is that if the target type couldn't find the default
        // extension, then we simply shouldn't search for any existing files
        // (of course, if the user specified the extension explicitly, we will
        // still do so).
        //
        level4 ([&]{trace << "no existing file for prerequisite " << cpk;});
        return nullptr;
      }
    }

    // Make a copy with the updated extension.
    //
    const prerequisite_key pk {
      cpk.proj, target_key {ctk.type, ctk.dir, ctk.name, ext}, cpk.scope};
    const target_key& tk (pk.tk);

    // Go over paths looking for a file.
    //
    for (const dir_path& d: sp)
    {
      path f (d / *tk.dir / path (*tk.name));
      f.normalize ();

      if (!ext->empty ())
      {
        f += '.';
        f += *ext;
      }

      timestamp mt (file_mtime (f));

      if (mt == timestamp_nonexistent)
        continue;

      level5 ([&]{trace << "found existing file " << f << " for prerequisite "
                        << cpk;});

      // Find or insert. Note: using our updated extension.
      //
      auto r (targets.insert (*tk.type, f.directory (), *tk.name, ext, trace));

      // Has to be a file_target.
      //
      file& t (dynamic_cast<file&> (r.first));

      level5 ([&]{trace << (r.second ? "new" : "existing") << " target "
                        << t << " for prerequisite " << cpk;});

      if (t.path ().empty ())
        t.path (move (f));

      t.mtime (mt);
      return &t;
    }

    level4 ([&]{trace << "no existing file for prerequisite " << cpk;});
    return nullptr;
  }

  target&
  create_new_target (const prerequisite_key& pk)
  {
    tracer trace ("create_new_target");

    const target_key& tk (pk.tk);

    // We default to the target in this directory scope.
    //
    dir_path d;
    if (tk.dir->absolute ())
      d = *tk.dir; // Already normalized.
    else
    {
      d = pk.scope->out_path ();

      if (!tk.dir->empty ())
      {
        d /= *tk.dir;
        d.normalize ();
      }
    }

    // Find or insert.
    //
    auto r (targets.insert (*tk.type, move (d), *tk.name, tk.ext, trace));
    assert (r.second);

    target& t (r.first);

    level5 ([&]{trace << "new target " << t << " for prerequisite " << pk;});

    return t;
  }
}