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
|
// file : build/search.cxx -*- C++ -*-
// copyright : Copyright (c) 2014-2015 Code Synthesis Tools CC
// license : MIT; see accompanying LICENSE file
#include <build/search>
#include <utility> // move
#include <cassert>
#include <build/path>
#include <build/scope>
#include <build/target>
#include <build/prerequisite>
#include <build/timestamp>
#include <build/diagnostics>
using namespace std;
namespace build
{
target*
search_existing_target (prerequisite& p)
{
tracer trace ("search_existing_target");
assert (p.target == nullptr);
// Look for an existing target in this prerequisite's directory scope.
//
path d;
if (p.dir.absolute ())
d = p.dir; // Already normalized.
else
{
d = p.scope.path ();
if (!p.dir.empty ())
{
d /= p.dir;
d.normalize ();
}
}
auto i (targets.find (p.type.id, d, p.name, p.ext, trace));
if (i == targets.end ())
return 0;
target& t (**i);
level4 ([&]{trace << "existing target " << t << " for prerequsite "
<< p;});
p.target = &t;
return &t;
}
target*
search_existing_file (prerequisite& p, const paths& sp)
{
tracer trace ("search_existing_file");
assert (p.dir.relative ());
// Go over paths and extension looking for a file.
//
for (const path& d: sp)
{
path f (d / p.dir / path (p.name));
// @@ TMP: use target name as an extension.
//
const string& e (p.ext != nullptr ? *p.ext : p.type.name);
if (!e.empty ())
{
f += '.';
f += e;
}
timestamp mt (path_mtime (f));
if (mt == timestamp_nonexistent)
continue;
level4 ([&]{trace << "found existing file " << f << " for prerequsite "
<< p;});
// Find or insert.
//
auto r (targets.insert (p.type, f.directory (), p.name, p.ext, trace));
// Has to be a path_target.
//
path_target& t (dynamic_cast<path_target&> (r.first));
level4 ([&]{trace << (r.second ? "new" : "existing") << " target "
<< t << " for prerequsite " << p;});
t.path (move (f));
t.mtime (mt);
p.target = &t;
return &t;
}
return nullptr;
}
target&
create_new_target (prerequisite& p)
{
tracer trace ("search_new_target");
assert (p.target == nullptr);
// We default to the target in this prerequisite's directory scope.
//
path d;
if (p.dir.absolute ())
d = p.dir; // Already normalized.
else
{
d = p.scope.path ();
if (!p.dir.empty ())
{
d /= p.dir;
d.normalize ();
}
}
// Find or insert.
//
auto r (targets.insert (p.type, move (d), p.name, p.ext, trace));
assert (r.second);
target& t (r.first);
level4 ([&]{trace << "new target " << t << " for prerequsite " << p;});
p.target = &t;
return t;
}
}
|