summaryrefslogtreecommitdiff
path: root/src/tasks.cc
blob: 56988f94408b69fef6a21df6c8217d19e63732a7 (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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
// -*- c++ -*-
// Distributed under the BSD 2-Clause License.
// See accompanying file LICENSE for details.
#include "tasks.h"

#include <filesystem>
#include <deque>
#include <iostream>
#include <algorithm>

#include "settings.h"
#include "libctor.h"
#include "task.h"
#include "task_cc.h"
#include "task_ld.h"
#include "task_ar.h"
#include "task_so.h"
#include "rebuild.h"

const std::deque<Target>& getTargets(const Settings& settings)
{
	static bool initialised{false};
	static std::deque<Target> targets;
	if(!initialised)
	{
		for(std::size_t i = 0; i < numConfigFiles; ++i)
		{
			std::string path =
				std::filesystem::path(configFiles[i].file).parent_path().string();
			if(settings.verbose > 1)
			{
				std::cout << configFiles[i].file << " in path " << path << "\n";
			}
			auto configs = configFiles[i].cb();
			for(const auto& config : configs)
			{
				targets.push_back({config, path});
			}
		}
		initialised = true;
	}

	return targets;
}

std::list<std::shared_ptr<Task>> taskFactory(const BuildConfiguration& config,
                                             const Settings& settings,
                                             const std::string& sourceDir)
{
	std::filesystem::path targetFile(config.target);

	TargetType target_type{config.type};
	if(target_type == TargetType::Auto)
	{
		if(targetFile.extension() == ".a")
		{
			target_type = TargetType::StaticLibrary;
		}
		else if(targetFile.extension() == ".so")
		{
			target_type = TargetType::DynamicLibrary;
		}
		else if(targetFile.extension() == "")
		{
			target_type = TargetType::Executable;
		}
		else
		{
			std::cerr << "Could not deduce target type from target " <<
				targetFile.string() << " please specify.\n";
			exit(1);
		}
	}

	std::vector<std::string> objects;
	std::list<std::shared_ptr<Task>> tasks;
	for(const auto& file : config.sources)
	{
		tasks.emplace_back(std::make_shared<TaskCC>(config, settings,
		                                            sourceDir, file));
		objects.push_back(tasks.back()->target());
	}

	switch(target_type)
	{
	case TargetType::Auto:
		// The target_type cannot be Auto
		break;

	case TargetType::StaticLibrary:
		tasks.emplace_back(std::make_shared<TaskAR>(config, settings, config.target,
		                                            objects, sourceDir));
		break;
#ifndef BOOTSTRAP
	case TargetType::DynamicLibrary:
		// TODO: Use C++20 starts_with
		if(targetFile.stem().string().substr(0, 3) != "lib")
		{
			std::cerr << "Dynamic library target must have 'lib' prefix\n";
			exit(1);
		}
		tasks.emplace_back(std::make_shared<TaskSO>(config, settings, config.target,
		                                            objects, sourceDir));
		break;

	case TargetType::Executable:
	case TargetType::UnitTest:
		tasks.emplace_back(std::make_shared<TaskLD>(config, settings, config.target,
		                                            objects, sourceDir));
		break;

	case TargetType::Object:
		break;
#else
	default:
		break;
#endif
	}

	return tasks;
}

std::shared_ptr<Task> getNextTask(const std::list<std::shared_ptr<Task>>& allTasks,
                                  std::list<std::shared_ptr<Task>>& dirtyTasks)
{
	for(auto dirtyTask = dirtyTasks.begin();
	    dirtyTask != dirtyTasks.end();
	    ++dirtyTask)
	{
		//std::cout << "Examining target " << (*dirtyTask)->target() << "\n";
		if((*dirtyTask)->ready())
		{
			dirtyTasks.erase(dirtyTask);
			return *dirtyTask;
		}
	}

	//std::cout << "No task ready ... \n";
	return nullptr;
}

std::list<std::shared_ptr<Task>> getTasks(const Settings& settings,
                                          const std::vector<std::string> names)
{
	auto& targets = getTargets(settings);
	std::list<std::shared_ptr<Task>> tasks;
	for(const auto& target : targets)
	{
		if(names.empty() ||
		   std::find(std::begin(names), std::end(names), target.config.target) != std::end(names))
		{
			std::vector<std::string> objects;
			auto t = taskFactory(target.config, settings, target.path);
			tasks.insert(tasks.end(), t.begin(), t.end());
		}
	}

	return tasks;
}