summaryrefslogtreecommitdiff
path: root/src/rebuild.cc
blob: e017d925cdfc44be206177ff6675900917fd58f6 (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
// -*- c++ -*-
// Distributed under the BSD 2-Clause License.
// See accompanying file LICENSE for details.
#include "rebuild.h"

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

#include "configure.h"
#include "settings.h"
#include "libctor.h"
#include "tasks.h"
#include "build.h"

std::array<BuildConfigurationEntry, 1024> configFiles;
std::size_t numConfigFiles{0};

// TODO: Use c++20 when ready, somehing like this:
//int reg(const std::source_location location = std::source_location::current())
int reg(const char* location, std::vector<BuildConfiguration> (*cb)())
{
	// NOTE: std::cout cannot be used here
	if(numConfigFiles >= configFiles.size())
	{
		fprintf(stderr, "Max %d build configurations currently supported.\n",
		        (int)configFiles.size());
		exit(1);
	}

	configFiles[numConfigFiles].file = location;
	configFiles[numConfigFiles].cb = cb;
	++numConfigFiles;

	return 0;
}

int unreg(const char* location)
{
	std::size_t found{0};
	for(std::size_t i = 0; i < numConfigFiles;)
	{
		if(std::string(location) == configFiles[i].file)
		{
			++found;
			for(std::size_t j = i; j < numConfigFiles; ++j)
			{
				configFiles[j] = configFiles[j + 1];
			}
			--numConfigFiles;
		}
		else
		{
			++i;
		}
	}

	return found;
}

namespace
{
bool contains(const std::vector<Source>& sources, const std::string& file)
{
	for(const auto& source : sources)
	{
		if(source.file == file)
		{
			return true;
		}
	}

	return false;
}
}

void recompileCheck(const Settings& global_settings, int argc, char* argv[],
                    bool force, bool relaunch_allowed)
{
	using namespace std::string_literals;

	if(global_settings.verbose > 1)
	{
		std::cout << "Recompile check (" << numConfigFiles << "):\n";
	}

	BuildConfiguration config;

	config.name = "ctor";
	config.cxxflags = std::vector<std::string>({ "-s", "-O3", "-std=c++17" });
	if(hasConfiguration(cfg::ctor_includedir))
	{
		config.cxxflags.push_back("-I"s + getConfiguration(cfg::ctor_includedir));
	}
	if(hasConfiguration(cfg::ctor_libdir))
	{
		config.ldflags.push_back("-L"s + getConfiguration(cfg::ctor_libdir));
	}
	config.ldflags.push_back("-lctor");
	config.ldflags.push_back("-pthread");


	Settings settings{global_settings};
	settings.verbose = -1; // Make check completely silent.
	settings.builddir += "/ctor"; // override builddir to use ctor subdir

	{
		std::filesystem::path buildfile  = settings.builddir;
		std::filesystem::path currentfile = argv[0];
		config.target = std::filesystem::relative(currentfile, buildfile).string();
	}

	if(std::filesystem::exists(configurationFile))
	{
		config.sources.push_back(configurationFile.string());
	}

	for(std::size_t i = 0; i < numConfigFiles; ++i)
	{
		std::string location = configFiles[i].file;
		if(global_settings.verbose > 1)
		{
			std::cout << " - " << location << "\n";
		}

		// Ensure that files containing multiple configurations are only added once.
		if(!contains(config.sources, location))
		{
			config.sources.push_back(location);
		}
	}

	auto tasks = taskFactory({config}, settings, {});

	for(auto task : tasks)
	{
		if(task->registerDepTasks(tasks))
		{
			return;
		}
	}

	build(settings, "ctor", tasks);
}