summaryrefslogtreecommitdiff
path: root/task_ld.cc
blob: 0cd4c957b3e1adab74cc47d7f32fffea7b477f12 (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
#include "task_ld.h"

#include <iostream>
#include <fstream>

#include "libcppbuild.h"
#include "settings.h"
#include "execute.h"

TaskLD::TaskLD(const BuildConfiguration& config,
               const Settings& settings,
               const std::string& target,
               const std::vector<std::string>& objects)
	: config(config)
	, settings(settings)
{
	targetFile = settings.builddir;
	targetFile /= target;
	for(const auto& object : objects)
	{
		std::filesystem::path objectFile = object;
		objectFiles.push_back(objectFile);
	}
}

bool TaskLD::dirty()
{
	if(!std::filesystem::exists(targetFile))
	{
		return true;
	}

	for(const auto& objectFile : objectFiles)
	{
		if(std::filesystem::last_write_time(targetFile) <=
		   std::filesystem::last_write_time(objectFile))
		{
			return true;
		}
	}

	return false;
}

int TaskLD::run()
{
	std::string objectlist;
	for(const auto& objectFile : objectFiles)
	{
		if(!objectlist.empty())
		{
			objectlist += " ";
		}
		objectlist += std::string(objectFile);
	}

	std::vector<std::string> args;
	for(const auto& objectFile : objectFiles)
	{
		args.push_back(std::string(objectFile));
	}
	for(const auto& flag : config.ldflags)
	{
		args.push_back(flag);
	}
	args.push_back("-o");
	args.push_back(std::string(targetFile));

	return execute("/usr/bin/g++", args);
}

int TaskLD::clean()
{
	std::filesystem::remove(targetFile);
	return 0;
}

std::vector<std::string> TaskLD::depends() const
{
	return {};
}

std::string TaskLD::target() const
{
	std::cout << "Removing " << std::string(targetFile) << "\n";
	return std::string(targetFile);
}