Skip to content

Commit f0af5ca

Browse files
committed
Store patch data per target. This prevents rebuilding because patch data was global with patch needed flag.
1 parent cf700d9 commit f0af5ca

4 files changed

Lines changed: 42 additions & 19 deletions

File tree

src/sw/driver/functions.cpp

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,8 @@ static auto set_lwt(const path &from_fn, const path &tsf) {
4949
fs::last_write_time(tsf, fs::last_write_time(from_fn));
5050
}
5151

52-
static bool should_patch(const path &fn, const path &hf, const path &hfn)
52+
static bool should_patch(patch_data_type &to_patch, const path &fn, const path &hf, const path &hfn)
5353
{
54-
static std::map<path, bool> to_patch;
55-
5654
auto tsf = path{ hf } += ".ts"s;
5755
ScopedFileLock fl(tsf);
5856

@@ -68,7 +66,7 @@ static bool should_patch(const path &fn, const path &hf, const path &hfn)
6866
return false;
6967
}
7068

71-
void replaceInFileOnce(const path &fn, const String &from, const String &to, const path &lock_dir)
69+
void replaceInFileOnce(patch_data_type &to_patch, const path &fn, const String &from, const String &to, const path &lock_dir)
7270
{
7371
auto hf = sha1(to_string(normalize_path(fn)));
7472

@@ -78,16 +76,16 @@ void replaceInFileOnce(const path &fn, const String &from, const String &to, con
7876

7977
const auto lock = lock_dir / hf;
8078
auto tsf = path{ lock_dir / hf } += ".ts"s;
81-
if (!should_patch(fn, lock, hfn))
79+
if (!should_patch(to_patch, fn, lock, hfn))
8280
return;
8381

8482
ScopedFileLock fl(lock);
8583

8684
// double check
87-
if (!should_patch(fn, lock, hfn))
85+
if (!should_patch(to_patch, fn, lock, hfn))
8886
return;
8987

90-
LOG_DEBUG(logger, std::format("patching {} with patch file {}:\nfrom:\n{}\nto:\n{}", fn.string(), hfn.string(), from, to));
88+
LOG_DEBUG(logger, std::format("patching\n{}\nwith patch file\n{}\n:\nfrom:\n{}\nto:\n{}\n", fn.string(), hfn.string(), from, to));
9189

9290
auto s = read_file(fn);
9391
boost::replace_all(s, from, to);
@@ -96,7 +94,7 @@ void replaceInFileOnce(const path &fn, const String &from, const String &to, con
9694
set_lwt(fn, tsf);
9795
}
9896

99-
void pushFrontToFileOnce(const path &fn, const String &text, const path &lock_dir)
97+
void pushFrontToFileOnce(patch_data_type &to_patch, const path &fn, const String &text, const path &lock_dir)
10098
{
10199
auto hf = sha1(to_string(normalize_path(fn)));
102100

@@ -106,13 +104,13 @@ void pushFrontToFileOnce(const path &fn, const String &text, const path &lock_di
106104

107105
const auto lock = lock_dir / hf;
108106
auto tsf = path{ lock_dir / hf } += ".ts"s;
109-
if (!should_patch(fn, lock, hfn))
107+
if (!should_patch(to_patch, fn, lock, hfn))
110108
return;
111109

112110
ScopedFileLock fl(lock);
113111

114112
// double check
115-
if (!should_patch(fn, lock, hfn))
113+
if (!should_patch(to_patch, fn, lock, hfn))
116114
return;
117115

118116
LOG_DEBUG(logger, std::format("pushFrontToFileOnce {} with patch file {}:\n{}", fn.string(), hfn.string(), text));
@@ -124,7 +122,7 @@ void pushFrontToFileOnce(const path &fn, const String &text, const path &lock_di
124122
set_lwt(fn, tsf);
125123
}
126124

127-
void pushBackToFileOnce(const path &fn, const String &text, const path &lock_dir)
125+
void pushBackToFileOnce(patch_data_type &to_patch, const path &fn, const String &text, const path &lock_dir)
128126
{
129127
auto hf = sha1(to_string(normalize_path(fn)));
130128

@@ -134,13 +132,13 @@ void pushBackToFileOnce(const path &fn, const String &text, const path &lock_dir
134132

135133
const auto lock = lock_dir / hf;
136134
auto tsf = path{ lock_dir / hf } += ".ts"s;
137-
if (!should_patch(fn, lock, hfn))
135+
if (!should_patch(to_patch, fn, lock, hfn))
138136
return;
139137

140138
ScopedFileLock fl(lock);
141139

142140
// double check
143-
if (!should_patch(fn, lock, hfn))
141+
if (!should_patch(to_patch, fn, lock, hfn))
144142
return;
145143

146144
LOG_DEBUG(logger, std::format("pushBackToFileOnce {} with patch file {}:\n{}", fn.string(), hfn.string(), text));

src/sw/driver/functions.h

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,20 +9,22 @@
99
namespace sw
1010
{
1111

12+
using patch_data_type = std::map<path, bool>;
13+
1214
SW_DRIVER_CPP_API
1315
void writeFileOnce(const path &fn, const String &content, const path &lock_dir);
1416

1517
SW_DRIVER_CPP_API
1618
void writeFileSafe(const path &fn, const String &content, const path &lock_dir);
1719

1820
SW_DRIVER_CPP_API
19-
void replaceInFileOnce(const path &fn, const String &from, const String &to, const path &lock_dir);
21+
void replaceInFileOnce(patch_data_type &, const path &fn, const String &from, const String &to, const path &lock_dir);
2022

2123
SW_DRIVER_CPP_API
22-
void pushFrontToFileOnce(const path &fn, const String &text, const path &lock_dir);
24+
void pushFrontToFileOnce(patch_data_type &, const path &fn, const String &text, const path &lock_dir);
2325

2426
SW_DRIVER_CPP_API
25-
void pushBackToFileOnce(const path &fn, const String &text, const path &lock_dir);
27+
void pushBackToFileOnce(patch_data_type &, const path &fn, const String &text, const path &lock_dir);
2628

2729
SW_DRIVER_CPP_API
2830
bool patch(const path &fn, const String &text, const path &lock_dir);

src/sw/driver/target/base.h

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,13 @@ struct SW_DRIVER_CPP_API Target : ITarget, TargetBase, ProgramStorage,
248248
};
249249
const TargetSettings *ts = nullptr;*/
250250

251+
struct predefined_storage_ids {
252+
enum {
253+
// append only
254+
patch_data,
255+
};
256+
};
257+
251258
// Data storage for objects that must be alive with the target.
252259
// For example, program clones etc.
253260
std::vector<std::any> Storage;
@@ -364,6 +371,21 @@ struct SW_DRIVER_CPP_API Target : ITarget, TargetBase, ProgramStorage,
364371
loader(*this);
365372
}
366373

374+
template <typename T>
375+
T &add_storage_data_to_slot(T &&d, auto id) {
376+
if (Storage.size() != id) {
377+
throw SW_LOGIC_ERROR("bad storage slot");
378+
}
379+
return std::any_cast<T&>(Storage.emplace_back(std::move(d)));
380+
}
381+
template <typename T>
382+
T &get_storage_data_from_slot(auto id) {
383+
if (Storage.size() <= id) {
384+
throw SW_LOGIC_ERROR("bad storage slot");
385+
}
386+
return std::any_cast<T&>(Storage[id]);
387+
}
388+
367389
private:
368390
void addTest(Test &cb, const String &name);
369391
Test addTest1(const String &name, const Target &t);

src/sw/driver/target/native.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,7 @@ path NativeTarget::getOutputFile() const
218218
NativeCompiledTarget::NativeCompiledTarget(TargetBase &parent, const PackageId &id)
219219
: NativeTarget(parent, id), NativeTargetOptionsGroup((Target &)*this)
220220
{
221+
add_storage_data_to_slot(patch_data_type{}, predefined_storage_ids::patch_data);
221222
}
222223

223224
NativeCompiledTarget::~NativeCompiledTarget()
@@ -5043,7 +5044,7 @@ void NativeCompiledTarget::patch(const path &fn, const String &from, const Strin
50435044
bool source_dir = false;
50445045
path p = fn;
50455046
check_absolute(p, false, &source_dir);
5046-
::sw::replaceInFileOnce(p, from, to, getPatchDir(!source_dir));
5047+
::sw::replaceInFileOnce(get_storage_data_from_slot<patch_data_type>(predefined_storage_ids::patch_data), p, from, to, getPatchDir(!source_dir));
50475048

50485049
//File f(p, getFs());
50495050
//f.getFileRecord().load();
@@ -5075,7 +5076,7 @@ void NativeCompiledTarget::pushFrontToFileOnce(const path &fn, const String &tex
50755076
bool source_dir = false;
50765077
path p = fn;
50775078
check_absolute(p, false, &source_dir);
5078-
::sw::pushFrontToFileOnce(p, text, getPatchDir(!source_dir));
5079+
::sw::pushFrontToFileOnce(get_storage_data_from_slot<patch_data_type>(predefined_storage_ids::patch_data), p, text, getPatchDir(!source_dir));
50795080

50805081
//File f(p, getFs());
50815082
//f.getFileRecord().load();
@@ -5091,7 +5092,7 @@ void NativeCompiledTarget::pushBackToFileOnce(const path &fn, const String &text
50915092
bool source_dir = false;
50925093
path p = fn;
50935094
check_absolute(p, false, &source_dir);
5094-
::sw::pushBackToFileOnce(p, text, getPatchDir(!source_dir));
5095+
::sw::pushBackToFileOnce(get_storage_data_from_slot<patch_data_type>(predefined_storage_ids::patch_data), p, text, getPatchDir(!source_dir));
50955096

50965097
//File f(p, getFs());
50975098
//f.getFileRecord().load();

0 commit comments

Comments
 (0)