|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152 |
- #include "DepNode.h"
-
- #include <mutex>
- #include <condition_variable>
- #include <unordered_set>
-
- #include "parallel.h"
- #include "logger.h"
-
- static void maybeAddStrings(
- std::unordered_set<std::string> &set,
- const std::vector<std::string> &vec,
- std::vector<std::string> &out) {
- for (auto &str: vec) {
- auto pair = set.emplace(str);
- bool inserted = pair.second;
- if (inserted) {
- out.push_back(str);
- }
- }
- }
-
- void DepNode::addChild(std::shared_ptr<DepNode> node) {
- deps_.push_back(std::move(node));
- }
-
- bool DepNode::haveDepsChanged(const std::string &outDir) {
- auto it = deps_.begin();
- bool changed = false;
- int workers = 0;
- std::mutex mut;
- std::condition_variable cond;
-
- std::unique_lock<std::mutex> lock(mut, std::defer_lock);
- while (it != deps_.end()) {
- lock.lock();
- if (changed) {
- cond.wait(lock, [&] { return workers == 0; });
- return true;
- }
-
- workers += 1;
- lock.unlock();
- parallel::run([&, it] {
- bool ch = (*it)->hasChanged(outDir);
- std::unique_lock<std::mutex> lock(mut);
- workers -= 1;
- if (ch) {
- changed = true;
- }
-
- lock.unlock();
- cond.notify_one();
- });
-
- it = ++it;
- }
-
- lock.lock();
- cond.wait(lock, [&] { return workers == 0; });
- return changed;
- }
-
- bool DepNode::hasChanged(const std::string &outDir) {
- std::lock_guard<std::mutex> lock(mut_);
-
- if (was_rebuilt_) {
- return true;
- }
-
- if (has_changed_ == TriState::TRUE) {
- return true;
- } else if (has_changed_ == TriState::FALSE) {
- return false;
- } else {
- bool changed = checkHasChanged(outDir);
- if (!changed) {
- changed = haveDepsChanged(outDir);
- }
-
- if (changed) {
- has_changed_ = TriState::TRUE;
- return true;
- } else {
- has_changed_ = TriState::FALSE;
- return false;
- }
- }
- }
-
- void DepNode::build(const std::string &outDir) {
- bool changed = hasChanged(outDir);
- std::lock_guard<std::mutex> lock(mut_);\
-
- if (!was_rebuilt_ && changed) {
- int workers = 0;
- std::mutex mut;
- std::condition_variable cond;
-
- std::unique_lock<std::mutex> lock(mut, std::defer_lock);
- for (auto &dep: deps_) {
- if (dep->hasChanged(outDir)) {
- lock.lock();
- workers += 1;
- lock.unlock();
- parallel::run([&] {
- dep->build(outDir);
- std::unique_lock<std::mutex> lock(mut);
- workers -= 1;
-
- lock.unlock();
- cond.notify_one();
- });
- }
- }
-
- lock.lock();
- cond.wait(lock, [&] { return workers == 0; });
- doBuild(outDir);
- was_rebuilt_ = true;
- }
- }
-
- std::vector<std::string> DepNode::publicLDFlags(const std::string &outDir) {
- std::unordered_set<std::string> set;
- std::vector<std::string> flags;
- maybeAddStrings(set, getPublicLDFlags(outDir), flags);
- for (auto &dep: deps_) {
- maybeAddStrings(set, dep->publicLDFlags(outDir), flags);
- }
- return flags;
- }
-
- std::vector<std::string> DepNode::publicLDLibs(const std::string &outDir) {
- std::unordered_set<std::string> set;
- std::vector<std::string> libs;
- maybeAddStrings(set, getPublicLDLibs(outDir), libs);
- for (auto &dep: deps_) {
- maybeAddStrings(set, dep->publicLDLibs(outDir), libs);
- }
- return libs;
- }
-
- std::vector<std::string> DepNode::publicObjects(const std::string &outDir) {
- std::vector<std::string> objs = getPublicObjects(outDir);;
- for (auto &dep: deps_) {
- for (auto &obj: dep->publicObjects(outDir)) {
- objs.push_back(obj);
- }
- }
- return objs;
- }
|