summaryrefslogtreecommitdiff
path: root/src/task_fn.cc
blob: b11ff1568374c186f55e6bbb24ff91f7013bd5e5 (plain)
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
// -*- 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 "ctor.h"
#include "execute.h"
#include "util.h"

TaskFn::TaskFn(const ctor::build_configuration& config, const ctor::settings& settings,
               const std::string& sourceDir, const ctor::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() << std::endl;
	}

	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();
}