summaryrefslogtreecommitdiff
path: root/tools/minicargo/build.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'tools/minicargo/build.cpp')
-rw-r--r--tools/minicargo/build.cpp83
1 files changed, 83 insertions, 0 deletions
diff --git a/tools/minicargo/build.cpp b/tools/minicargo/build.cpp
new file mode 100644
index 00000000..a8032760
--- /dev/null
+++ b/tools/minicargo/build.cpp
@@ -0,0 +1,83 @@
+/*
+ */
+#include "manifest.h"
+#include <vector>
+#include <algorithm>
+
+struct BuildList
+{
+ struct BuildEnt {
+ const PackageManifest* package;
+ unsigned level;
+ };
+ ::std::vector<BuildEnt> m_list;
+
+ void add_package(const PackageManifest& p, unsigned level);
+ void sort_list();
+
+ struct Iter {
+ const BuildList& l;
+ size_t i;
+
+ const PackageManifest& operator*() const {
+ return *this->l.m_list[this->l.m_list.size() - this->i - 1].package;
+ }
+ void operator++() {
+ this->i++;
+ }
+ bool operator!=(const Iter& x) const {
+ return this->i != x.i;
+ }
+ Iter begin() const {
+ return *this;
+ }
+ Iter end() {
+ return Iter{ this->l, this->l.m_list.size() };
+ }
+ };
+
+ Iter iter() const {
+ return Iter { *this, 0 };
+ }
+};
+
+void MiniCargo_Build(const PackageManifest& manifest)
+{
+ BuildList list;
+ // Generate sorted dependency list
+ for (const auto& dep : manifest.dependencies())
+ {
+ list.add_package(dep.get_package(), 1);
+ }
+
+
+ // Build dependencies
+ for(const auto& p : list.iter())
+ {
+ p.build_lib();
+ }
+
+ manifest.build_lib();
+ // TODO: If the manifest doesn't have a library, build the binary
+}
+
+void BuildList::add_package(const PackageManifest& p, unsigned level)
+{
+ for(auto& ent : m_list)
+ {
+ if(ent.package == &p)
+ {
+ ent.level = level;
+ return ;
+ }
+ }
+ m_list.push_back({ &p, level });
+ for (const auto& dep : p.dependencies())
+ {
+ add_package(dep.get_package(), level+1);
+ }
+}
+void BuildList::sort_list()
+{
+ ::std::sort(m_list.begin(), m_list.end(), [](const auto& a, const auto& b){ return a.level < b.level; });
+}