summaryrefslogtreecommitdiff
path: root/src/task_fn.cc
diff options
context:
space:
mode:
Diffstat (limited to 'src/task_fn.cc')
-rw-r--r--src/task_fn.cc121
1 files changed, 121 insertions, 0 deletions
diff --git a/src/task_fn.cc b/src/task_fn.cc
new file mode 100644
index 0000000..ab00fae
--- /dev/null
+++ b/src/task_fn.cc
@@ -0,0 +1,121 @@
+// -*- c++ -*-
+// Distributed under the BSD 2-Clause License.
+// See accompanying file LICENSE for details.
+#include "task_fn.h"
+
+#include <iostream>
+#include <fstream>
+#include <cassert>
+
+#include "libctor.h"
+#include "execute.h"
+#include "util.h"
+
+TaskFn::TaskFn(const BuildConfiguration& config, const Settings& settings,
+ const std::string& sourceDir, const Source& source)
+ : Task(config, settings, sourceDir)
+ , config(config)
+ , settings(settings)
+{
+ sourceFile = sourceDir;
+ sourceFile /= source.file;
+
+ std::filesystem::create_directories(std::filesystem::path(settings.builddir) / sourceFile.parent_path());
+
+ target_type = config.type;
+ source_language = source.language;
+
+ if(source.output.empty())
+ {
+ std::cerr << "Missing output file for functional target\n";
+ exit(1);
+ }
+
+ _targetFile = source.output;
+}
+
+bool TaskFn::dirtyInner()
+{
+ if(!std::filesystem::exists(sourceFile))
+ {
+ //std::cout << "Missing source file: " << std::string(sourceFile) << "\n";
+ return true;
+ }
+
+ if(!std::filesystem::exists(targetFile()))
+ {
+ //std::cout << "Missing targetFile\n";
+ return true;
+ }
+
+ if(std::filesystem::last_write_time(sourceFile) >
+ std::filesystem::last_write_time(targetFile()))
+ {
+ //std::cout << "The targetFile older than sourceFile\n";
+ return true;
+ }
+
+ return false;
+}
+
+int TaskFn::runInner()
+{
+ if(!std::filesystem::exists(sourceFile))
+ {
+ std::cout << "Missing source file: " << sourceFile.string() << "\n";
+ return 1;
+ }
+
+ if(settings.verbose >= 0)
+ {
+ std::cout << "Fn" << " " <<
+ sourceFile.lexically_normal().string() << " => " <<
+ targetFile().lexically_normal().string() << "\n";
+ }
+
+ return config.function(sourceFile.string(),
+ targetFile().string(),
+ config,
+ settings);
+}
+
+int TaskFn::clean()
+{
+ if(std::filesystem::exists(targetFile()))
+ {
+ std::cout << "Removing " << targetFile().string() << "\n";
+ std::filesystem::remove(targetFile());
+ }
+
+ return 0;
+}
+
+std::vector<std::string> TaskFn::depends() const
+{
+ return {};
+}
+
+std::string TaskFn::target() const
+{
+ return _targetFile;
+}
+
+std::filesystem::path TaskFn::targetFile() const
+{
+ return std::filesystem::path(settings.builddir) / sourceDir / _targetFile;
+}
+
+bool TaskFn::derived() const
+{
+ return false;
+}
+
+std::string TaskFn::toJSON() const
+{
+ return {}; // TODO: Not sure how to express this...
+}
+
+std::string TaskFn::source() const
+{
+ return sourceFile.string();
+}