summaryrefslogtreecommitdiff
path: root/src/tasks.cc
blob: 67bed2b58b0f43987631c5cf3b2079f80933d895 (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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
// -*- c++ -*-
// Distributed under the BSD 2-Clause License.
// See accompanying file LICENSE for details.
#include "tasks.h"

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

#include "ctor.h"
#include "task.h"
#include "task_cc.h"
#include "task_ld.h"
#include "task_ar.h"
#include "task_so.h"
#include "task_fn.h"
#include "rebuild.h"
#include "configure.h"

const std::deque<Target>& getTargets(const ctor::settings& settings,
                                     bool resolve_externals)
{
	static bool initialised{false};
	static std::deque<Target> targets;
	if(!initialised)
	{
		const auto& externals = ctor::get_configuration().externals;
		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(settings);
			for(auto& config : configs)
			{
				if(resolve_externals)
				{
					// Resolv config externals
					for(const auto& external : config.externals)
					{
						if(externals.find(external) == externals.end())
						{
							std::cout << "External '" << external <<
								"' not found in cache - run configure.\n";
							exit(1);
						}
						const auto& flags = externals.at(external);
						config.flags.cflags.insert(config.flags.cflags.end(),
						                           flags.cflags.begin(),
						                           flags.cflags.end());
						config.flags.cxxflags.insert(config.flags.cxxflags.end(),
						                             flags.cxxflags.begin(),
						                             flags.cxxflags.end());
						config.flags.ldflags.insert(config.flags.ldflags.end(),
						                            flags.ldflags.begin(),
						                            flags.ldflags.end());
						config.flags.asmflags.insert(config.flags.asmflags.end(),
						                             flags.asmflags.begin(),
						                             flags.asmflags.end());
						//config.libs.insert(config.libs.end(),
						//                   libs.begin(),
						//                   libs.end());
					}
				}

				targets.push_back({config, path});
			}
		}
		initialised = true;
	}

	return targets;
}

std::set<std::shared_ptr<Task>> taskFactory(const ctor::build_configuration& config,
                                            const ctor::settings& settings,
                                            const std::string& sourceDir)
{
	std::set<std::shared_ptr<Task>> tasks;

	std::filesystem::path targetFile(config.target);

	ctor::target_type target_type{config.type};
	if(target_type == ctor::target_type::automatic)
	{
		if(config.function != nullptr)
		{
			target_type = ctor::target_type::function;
		}
		else if(targetFile.extension() == ".a")
		{
			target_type = ctor::target_type::static_library;
		}
		else if(targetFile.extension() == ".so")
		{
			target_type = ctor::target_type::dynamic_library;
		}
		else if(targetFile.extension() == "")
		{
			target_type = ctor::target_type::executable;
		}
		else
		{
			std::cerr << "Could not deduce target type from target " <<
				targetFile.string() << " please specify.\n";
			exit(1);
		}
	}

	std::vector<std::string> objects;
	if(target_type != ctor::target_type::function)
	{
		for(const auto& file : config.sources)
		{
			auto task = std::make_shared<TaskCC>(config, settings, sourceDir, file);
			tasks.insert(task);
			objects.push_back(task->targetFile().string());
		}
	}
#ifndef BOOTSTRAP
	else
	{
		for(const auto& file : config.sources)
		{
			auto task = std::make_shared<TaskFn>(config, settings, sourceDir, file);
			tasks.insert(task);
			objects.push_back(task->target());
		}
	}
#endif

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

	case ctor::target_type::static_library:
	case ctor::target_type::unit_test_library:
		tasks.insert(std::make_shared<TaskAR>(config, settings, config.target,
		                                      objects, sourceDir));
		break;
#ifndef BOOTSTRAP
	case ctor::target_type::dynamic_library:
		// 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.insert(std::make_shared<TaskSO>(config, settings, config.target,
		                                      objects, sourceDir));
		break;

	case ctor::target_type::executable:
	case ctor::target_type::unit_test:
		tasks.insert(std::make_shared<TaskLD>(config, settings, config.target,
		                                      objects, sourceDir));
		break;

	case ctor::target_type::object:
	case ctor::target_type::function:
		break;
#else
	default:
		break;
#endif
	}

	return tasks;
}

std::shared_ptr<Task> getNextTask(const std::set<std::shared_ptr<Task>>& allTasks,
                                  std::set<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::set<std::shared_ptr<Task>> getTasks(const ctor::settings& settings,
                                         const std::vector<std::string> names,
                                         bool resolve_externals)
{
	auto& targets = getTargets(settings, resolve_externals);
	std::set<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(t.begin(), t.end());
		}
	}

	return tasks;
}