summaryrefslogtreecommitdiff
path: root/src/rebuild.cc
blob: 4b0c8e55201f3732b6f928b6bb809d3720558a4f (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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
// -*- c++ -*-
// Distributed under the BSD 2-Clause License.
// See accompanying file LICENSE for details.
#include "rebuild.h"

#include <iostream>
#include <filesystem>
#include <algorithm>
#include <source_location>
#include <cstring>

#include "configure.h"
#include "libctor.h"
#include "tasks.h"
#include "build.h"
#include "execute.h"
#include "tools.h"
#include "util.h"

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

int reg(BuildConfigurations (*cb)(const Settings&),
        const std::source_location location)
{
	// 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);
	}

	auto loc = std::filesystem::path(location.file_name());
	if(loc.is_absolute())
	{
		auto pwd = std::filesystem::current_path();
		auto rel = std::filesystem::relative(loc, pwd);
		configFiles[numConfigFiles].file = strdup(rel.string().data()); // NOTE: This intentionally leaks memory
	}
	else
	{
		configFiles[numConfigFiles].file = location.file_name();
	}
	configFiles[numConfigFiles].cb = cb;
	++numConfigFiles;

	return 0;
}

int reg(const char* location)
{
	// 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 =
		[](const Settings&){ return std::vector<BuildConfiguration>{}; };
	++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;
		}
	}

	for(std::size_t i = 0; i < numExternalConfigFiles;)
	{
		if(std::string(location) == externalConfigFiles[i].file)
		{
			++found;
			for(std::size_t j = i; j < numExternalConfigFiles; ++j)
			{
				externalConfigFiles[j] = externalConfigFiles[j + 1];
			}
			--numExternalConfigFiles;
		}
		else
		{
			++i;
		}
	}

	return found;
}

std::array<ExternalConfigurationEntry, 1024> externalConfigFiles;
std::size_t numExternalConfigFiles{0};

int reg(ExternalConfigurations (*cb)(const Settings&),
        const std::source_location location)
{
	// NOTE: std::cout cannot be used here
	if(numExternalConfigFiles >= externalConfigFiles.size())
	{
		fprintf(stderr, "Max %d external configurations currently supported.\n",
		        (int)externalConfigFiles.size());
		exit(1);
	}

	externalConfigFiles[numExternalConfigFiles].file = location.file_name();
	externalConfigFiles[numExternalConfigFiles].cb = cb;
	++numExternalConfigFiles;

	return 0;
}

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;
}
}

#ifdef _WIN32
#define WIN32_LEAN_AND_MEAN
#include <windows.h>

// From: http://www.catch22.net/tuts/win32/self-deleting-executables
void DeleteSelf()
{
//#ifdef Windows NT/2000
#if 1
	char buf[MAX_PATH];
	HMODULE module;

	module = GetModuleHandle(0);
	GetModuleFileName(module, buf, MAX_PATH);
	CloseHandle((HANDLE)4);

	__asm
	{
		lea eax, buf
		push 0
		push 0
		push eax
		push ExitProcess
		push module
		push DeleteFile
		push UnmapViewOfFile
		ret
	}
#endif
//#elif Windows 95/98
#if 0
	char buf[MAX_PATH];
	HMODULE module;

	module = GetModuleHandle(0);
	GetModuleFileName(module, buf, MAX_PATH);

	__asm
	{
		lea eax, buf
		push 0
		push 0
		push eax
		push ExitProcess
		push module
		push DeleteFile
		push FreeLibrary
		ret
	}
#endif
//endif
}
#endif // _WIN32

void copyAndRelaunch(bool dirty_tasks, bool delete_me,
                     int argc, char* argv[],
                     const std::string& copy_src,
                     const std::string& copy_tar,
                     const std::string& compilation_name)
{
#ifdef _WIN32
	if(dirty_tasks && !delete_me)
	{
		CopyFile(copy_src.data(), copy_tar.data(), false);
		SetFileAttributes(copy_tar.data(), FILE_ATTRIBUTE_HIDDEN);
		auto t = std::filesystem::last_write_time(copy_src);
		std::filesystem::last_write_time(copy_tar, t);

		STARTUPINFO si{};
		PROCESS_INFORMATION pi{};
		si.cb = sizeof(pi);
		std::string args = copy_tar;
		for(int i = 1; i < argc; ++i)
		{
			args += " ";
			args += argv[i];
		}
		args += " --name ";
		args += compilation_name;

		CreateProcess(nullptr, args.data(), 0,0,0,0,0,0,&si,&pi);
		exit(0);
	}
#endif // _WIN32
}

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

	if(global_settings.verbose > 1)
	{
		std::cout << "Recompile check (" << numConfigFiles << "):\n";
	}
	std::filesystem::path settings_file(global_settings.name);
	std::filesystem::path argv0_file(argv[0]);

	if(settings_file.is_relative())
	{
		settings_file = std::filesystem::current_path() / settings_file;
	}
	if(argv0_file.is_relative())
	{
		argv0_file = std::filesystem::current_path() / argv0_file;
	}

	bool delete_me = settings_file.string() != argv0_file.string();

	BuildConfiguration config;

	config.type = TargetType::Executable;
	config.name = "ctor";
	config.system = OutputSystem::Build;

	auto tool_chain = getToolChain(config.system);

	append(config.flags.cxxflags,
	       getOption(tool_chain, opt::optimization, "2"));
	append(config.flags.cxxflags,
	       getOption(tool_chain, opt::cpp_std, "c++20"));
	config.flags.cxxflags.push_back("/nologo");
	if(hasConfiguration(cfg::ctor_includedir))
	{
		append(config.flags.cxxflags,
		       getOption(tool_chain, opt::include_path,
		                 getConfiguration(cfg::ctor_includedir)));
	}

	if(hasConfiguration(cfg::ctor_libdir))
	{
		append(config.flags.ldflags,
		       getOption(tool_chain, opt::library_path,
		                 getConfiguration(cfg::ctor_libdir)));
	}
//	append(config.flags.ldflags, getOption(tool_chain, opt::link, "ctor"));
	config.flags.ldflags.push_back("build/libctor.lib");
	append(config.flags.ldflags, getOption(tool_chain, opt::threads));

	config.flags.ldflags.push_back("/nologo");
	config.flags.ldflags.push_back("/SUBSYSTEM:CONSOLE");

	Settings settings{global_settings};
	settings.parallel_processes = 1;
	settings.verbose = 2;//-1; // Make check completely silent.
	settings.builddir += "/ctor"; // override builddir to use ctor subdir
	{
		std::filesystem::path buildfile  = settings.builddir;
		std::filesystem::path currentfile = settings.name;

		if(currentfile.is_relative())
		{
			currentfile = std::filesystem::current_path() / currentfile;
		}
		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);
		}
	}

	for(std::size_t i = 0; i < numExternalConfigFiles; ++i)
	{
		std::string location = externalConfigFiles[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 false;
		}
	}

	// Find out if reconfigure is needed
	bool reconfigure{false};
	for(auto task : tasks)
	{
		if(task->dirty() &&
		   task->source() != "" && // only look at source files
		   task->source() != "configuration.cc") // don't reconfigure if only configuration.cc is changed.
		{
			reconfigure |= true;
		}
	}

	auto dirty_tasks = build(settings, "ctor", tasks, true); // dryrun

	copyAndRelaunch(dirty_tasks, delete_me, argc, argv,
	                settings.name/*argv[0]*/, "ctor-rebuild-tmp.exe", settings.name);

	bool was_rebuilt{false};
	if(dirty_tasks)
	{
		std::cout << "Rebuilding config.\n";
		auto ret = build(settings, "ctor", tasks); // run for real
		if(ret != 0)
		{
			if(delete_me)
			{
				DeleteSelf();
			}
			return ret;
		}
		was_rebuilt = true;
	}

	if(reconfigure)
	{
		copyAndRelaunch(true, delete_me, argc, argv,
		                settings.name,
		                "ctor-reconfigure-tmp.exe", settings.name);
		std::vector<std::string> args;
		args.push_back("reconfigure");
		if(!relaunch_allowed)
		{
			args.push_back("--no-rerun");
		}
		if(delete_me)
		{
			args.push_back("--name " + settings.name);
		}
		for(int i = 1; i < argc; ++i)
		{
			args.push_back(argv[i]);
		}
		auto ret = execute(settings.name, args, {});
		//if(ret != 0)
		{
			if(delete_me)
			{
				DeleteSelf();
			}
			exit(ret);
		}

	}

	if(delete_me)
	{
		DeleteSelf();
	}

	return dirty_tasks;
}