summaryrefslogtreecommitdiff
path: root/src/task.h
blob: 6fe168617d9915c00d8dd6628b63a3ee4234a5c3 (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
// -*- c++ -*-
// Distributed under the BSD 2-Clause License.
// See accompanying file LICENSE for details.
#pragma once

#include <vector>
#include <string>
#include <atomic>
#include <memory>
#include <filesystem>

#include "ctor.h"

enum class State
{
	Unknown,
	Ready,
	Running,
	Done,
	Error,
};

class Task
{
public:
	Task(const ctor::build_configuration& config, const ctor::settings& settings,
	     const std::string& sourceDir);
	virtual ~Task() = default;

	int registerDepTasks(const std::vector<std::shared_ptr<Task>>& tasks);
	virtual int registerDepTasksInner(const std::vector<std::shared_ptr<Task>>& tasks) { return 0; }

	bool operator==(const std::string& dep);

	virtual std::string name() const;
	bool dirty();
	bool ready();
	int run();
	State state() const;
	virtual int clean() = 0 ;
	virtual std::vector<std::string> depends() const = 0;

	//! Raw target name as stated in ctor.cc config file or (in case of a derived
	//! target) the calculated target without builddir prefix.
	virtual std::string target() const = 0;

	//! Target file with full path prefix
	virtual std::filesystem::path targetFile() const = 0;

	//! Returns true for tasks that are non-target tasks, ie. for example derived
	//! objects files from target sources.
	virtual bool derived() const = 0;

	virtual std::string toJSON() const { return {}; };

	//! Returns a reference to the originating build config.
	//! Note: the build config of a derived task will be that of its parent
	//! (target) task.
	const ctor::build_configuration& buildConfig() const;

	ctor::target_type targetType() const;
	ctor::language sourceLanguage() const;
	ctor::output_system outputSystem() const;
	std::string compiler() const;

	std::vector<std::shared_ptr<Task>> getDependsTasks();

	virtual std::string source() const { return {}; }

protected:
	std::atomic<State> task_state{State::Unknown};
	virtual int runInner() { return 0; };
	virtual bool dirtyInner() { return false; }

	std::vector<std::string> dependsStr;
	std::vector<std::shared_ptr<Task>> dependsTasks;
	const ctor::build_configuration& config;
	ctor::target_type target_type{ctor::target_type::automatic};
	ctor::language source_language{ctor::language::automatic};
	ctor::output_system output_system{ctor::output_system::host};
	const ctor::settings& settings;
	std::string sourceDir;
};