summaryrefslogtreecommitdiff
path: root/src/configure.cc
blob: 28049ae4700d4cef05e1d1bcf96517401a31dd5e (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
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
// -*- c++ -*-
// Distributed under the BSD 2-Clause License.
// See accompanying file LICENSE for details.
#include "configure.h"

#include <iostream>
#include <filesystem>
#include <fstream>
#include <optional>

#include <getoptpp/getoptpp.hpp>

#include "execute.h"
#include "ctor.h"
#include "tasks.h"
#include "rebuild.h"
#include "externals.h"

using namespace ctor;

std::filesystem::path configurationFile("configuration.cc");
std::filesystem::path configHeaderFile("config.h");

std::map<std::string, std::string> external_includedir;
std::map<std::string, std::string> external_libdir;

namespace ctor {
const Configuration default_configuration{};
const Configuration& __attribute__((weak)) configuration()
{
	return default_configuration;
}

std::optional<std::string> includedir;
std::optional<std::string> libdir;
std::optional<std::string> builddir;
std::map<std::string, std::string> conf_values;

bool hasConfiguration(const std::string& key)
{
	if(key == cfg::ctor_includedir && ctor::includedir)
	{
		return true;
	}

	if(key == cfg::ctor_libdir && ctor::libdir)
	{
		return true;
	}

	if(key == cfg::builddir && ctor::builddir)
	{
		return true;
	}

	if(ctor::conf_values.find(key) != ctor::conf_values.end())
	{
		return true;
	}

	const auto& c = configuration();
	return c.tools.find(key) != c.tools.end();
}

const std::string& getConfiguration(const std::string& key,
                                    const std::string& defaultValue)
{
	if(key == cfg::ctor_includedir && ctor::includedir)
	{
		return *ctor::includedir;
	}

	if(key == cfg::ctor_libdir && ctor::libdir)
	{
		return *ctor::libdir;
	}

	if(key == cfg::builddir && ctor::builddir)
	{
		return *ctor::builddir;
	}

	if(ctor::conf_values.find(key) != ctor::conf_values.end())
	{
		return ctor::conf_values[key];
	}

	const auto& c = configuration();
	if(hasConfiguration(key))
	{
		return c.tools.at(key);
	}

	return defaultValue;
}
} // namespace ctor::

std::string locate(const std::string& arch, const std::string& app)
{
	std::string path_env = std::getenv("PATH");
	//std::cout << path_env << "\n";

	std::string program = app;
	if(!arch.empty())
	{
		program = arch + "-" + app;
	}
	std::cout << "Looking for: " << program << "\n";
	std::vector<std::string> paths;

	{
		std::stringstream ss(path_env);
		std::string path;
		while (std::getline(ss, path, ':'))
		{
			paths.push_back(path);
		}
	}
	for(const auto& path_str : paths)
	{
		std::filesystem::path path(path_str);
		auto prog_path = path / program;
		if(std::filesystem::exists(prog_path))
		{
			std::cout << "Found file " << app << " in path: " << path << "\n";
			auto perms = std::filesystem::status(prog_path).permissions();
			if((perms & std::filesystem::perms::owner_exec) != std::filesystem::perms::none)
			{
				//std::cout << " - executable by owner\n";
			}
			if((perms & std::filesystem::perms::group_exec) != std::filesystem::perms::none)
			{
				//std::cout << " - executable by group\n";
			}
			if((perms & std::filesystem::perms::others_exec) != std::filesystem::perms::none)
			{
				//std::cout << " - executable by others\n";
			}

			return prog_path.string();
		}
	}

	std::cerr << "Could not locate " << app << " for the " << arch << " architecture\n";
	exit(1);
	return {};
}

class Args
	: public std::vector<char*>
{
public:
	Args(const std::vector<std::string>& args)
	{
		resize(args.size() + 1);
		(*this)[0] = strdup("./ctor");
		for(std::size_t i = 0; i < size() - 1; ++i)
		{
			(*this)[i + 1] = strdup(args[i].data());
		}
	}

	~Args()
	{
		for(std::size_t i = 0; i < size(); ++i)
		{
			free((*this)[i]);
		}
	}
};

// helper constant for the visitor
template<class> inline constexpr bool always_false_v = false;

int regenerateCache(Settings& settings,
                    const std::vector<std::string>& args,
                    const std::map<std::string, std::string>& env)
{
	Args vargs(args);

	dg::Options opt;
	int key{128};

	std::string build_arch;
	std::string build_path;
	std::string host_arch;
	std::string host_path;
	std::string cc_prog = "gcc";
	std::string cxx_prog = "g++";
	std::string ar_prog = "ar";
	std::string ld_prog = "ld";
	std::string ctor_includedir;
	std::string ctor_libdir;
	std::string builddir;

	opt.add("build-dir", required_argument, 'b',
	        "Set output directory for build files (default: '" +
	        settings.builddir + "').",
	        [&]() {
		        settings.builddir = optarg;
		        builddir = optarg;
		        return 0;
	        });

	opt.add("verbose", no_argument, 'v',
	        "Be verbose. Add multiple times for more verbosity.",
	        [&]() {
		        settings.verbose++;
		        return 0;
	        });

	opt.add("cc", required_argument, key++,
	        "Use specified c-compiler instead of gcc.",
	        [&]() {
		        cc_prog = optarg;
		        return 0;
	        });

	opt.add("cxx", required_argument, key++,
	        "Use specified c++-compiler instead of g++.",
	        [&]() {
		        cxx_prog = optarg;
		        return 0;
	        });

	opt.add("ar", required_argument, key++,
	        "Use specified archiver instead of ar.",
	        [&]() {
		        ar_prog = optarg;
		        return 0;
	        });

	opt.add("ld", required_argument, key++,
	        "Use specified linker instead of ld.",
	        [&]() {
		        ld_prog = optarg;
		        return 0;
	        });

	opt.add("build", required_argument, key++,
	        "Configure for building on specified architecture.",
	        [&]() {
		        build_arch = optarg;
		        return 0;
	        });

	opt.add("build-path", required_argument, key++,
	        "Set path to build tool-chain.",
	        [&]() {
		        build_path = optarg;
		        return 0;
	        });

	opt.add("host", required_argument, key++,
	        "Cross-compile to build programs to run on specified architecture.",
	        [&]() {
		        host_arch = optarg;
		        return 0;
	        });

	opt.add("host-path", required_argument, key++,
	        "Set path to cross-compile tool-chain.",
	        [&]() {
		        host_path = optarg;
		        return 0;
	        });

	opt.add("ctor-includedir", required_argument, key++,
	        "Set path to ctor header file, used for re-compiling.",
	        [&]() {
		        ctor_includedir = optarg;
		        return 0;
	        });

	opt.add("ctor-libdir", required_argument, key++,
	        "Set path to ctor library file, used for re-compiling.",
	        [&]() {
		        ctor_libdir = optarg;
		        return 0;
	        });

	// Resolv externals
	ExternalConfigurations externalConfigs;
	for(std::size_t i = 0; i < numExternalConfigFiles; ++i)
	{
		auto newExternalConfigs = externalConfigFiles[i].cb(settings);
		externalConfigs.insert(externalConfigs.end(),
		                       newExternalConfigs.begin(),
		                       newExternalConfigs.end());
	}

	auto add_path_args =
		[&](const std::string& name)
		{
			opt.add(name + "-includedir", required_argument, key++,
			        "Set path to " + name + " header file.",
			        [&]() {
				        external_includedir[name] = optarg;
				        return 0;
			        });

			opt.add(name + "-libdir", required_argument, key++,
			        "Set path to " + name + " libraries.",
			        [&]() {
				        external_libdir[name] = optarg;
				        return 0;
			        });
		};

	for(const auto& ext : externalConfigs)
	{
		std::visit([&](auto&& arg)
		           {
			           using T = std::decay_t<decltype(arg)>;
			           if constexpr (std::is_same_v<T, ExternalManual>)
			           {
				           add_path_args(ext.name);
			           }
			           else
			           {
				           static_assert(always_false_v<T>, "non-exhaustive visitor!");
			           }
		           }, ext.external);

	}

	opt.add("help", no_argument, 'h',
	        "Print this help text.",
	        [&]() {
		        std::cout << "configure usage stuff\n";
		        opt.help();
		        exit(0);
		        return 0;
	        });

	opt.process(vargs.size(), vargs.data());

	if(host_arch.empty())
	{
		host_arch = build_arch;
	}

	auto tasks = getTasks(settings, {}, false);
/*
	bool needs_cpp{false};
	bool needs_c{false};
	bool needs_ar{false};
	bool needs_asm{false};
	for(const auto& task :tasks)
	{
		switch(task->sourceLanguage())
		{
		case Language::Auto:
			std::cerr << "TargetLanguage not deduced!\n";
			exit(1);
			break;
		case Language::C:
			needs_cpp = false;
			break;
		case Language::Cpp:
			needs_c = true;
			break;
		case Language::Asm:
			needs_asm = true;
			break;
		}
	}
*/

	auto cc_env = env.find("CC");
	if(cc_env != env.end())
	{
		cc_prog = cc_env->second;
	}

	auto cxx_env = env.find("CXX");
	if(cxx_env != env.end())
	{
		cxx_prog = cxx_env->second;
	}

	auto ar_env = env.find("AR");
	if(ar_env != env.end())
	{
		ar_prog = ar_env->second;
	}

	auto ld_env = env.find("LD");
	if(ld_env != env.end())
	{
		ld_prog = ld_env->second;
	}

	std::string host_cc = locate(host_arch, cc_prog);
	std::string host_cxx = locate(host_arch, cxx_prog);
	std::string host_ar = locate(host_arch, ar_prog);
	std::string host_ld = locate(host_arch, ld_prog);
	std::string build_cc = locate(build_arch, cc_prog);
	std::string build_cxx = locate(build_arch, cxx_prog);
	std::string build_ar = locate(build_arch, ar_prog);
	std::string build_ld = locate(build_arch, ld_prog);

	// Store current values for execution in this execution context.
	if(!ctor_includedir.empty())
	{
		ctor::conf_values[cfg::ctor_includedir] = ctor_includedir;
	}
	if(!ctor_libdir.empty())
	{
		ctor::conf_values[cfg::ctor_libdir] = ctor_libdir;
	}
	if(!builddir.empty())
	{
		ctor::conf_values[cfg::builddir] = builddir;
	}
	ctor::conf_values[cfg::host_cxx] = host_cxx;
	ctor::conf_values[cfg::build_cxx] = build_cxx;

	std::cout << "Writing results to: " << configurationFile.string() << "\n";
	{
		std::ofstream istr(configurationFile);
		istr << "#include <ctor.h>\n\n";
		istr << "namespace ctor {\n";
		istr << "const Configuration& configuration()\n";
		istr << "{\n";
		istr << "	static Configuration cfg =\n";
		istr << "	{\n";
		istr << "		.args = {";
		for(const auto& arg : args)
		{
			istr << "\"" << arg << "\",";
		}
		istr << "},\n";
		istr << "		.env = {";
		for(const auto& e : env)
		{
			istr << "{\"" << e.first << "\", \"" << e.second << "\"}, ";
		}
		istr << "},\n";

		istr << "		.tools = {\n";
		if(!builddir.empty())
		{
			istr << "			{ \"" << cfg::builddir << "\", \"" << builddir << "\" },\n";
			ctor::builddir = builddir;
		}
		istr << "			{ \"" << cfg::host_cc << "\", \"" << host_cc << "\" },\n";
		istr << "			{ \"" << cfg::host_cxx << "\", \"" << host_cxx << "\" },\n";
		istr << "			{ \"" << cfg::host_ar << "\", \"" << host_ar << "\" },\n";
		istr << "			{ \"" << cfg::host_ld << "\", \"" << host_ld << "\" },\n";
		istr << "			{ \"" << cfg::build_cc << "\", \"" << build_cc << "\" },\n";
		istr << "			{ \"" << cfg::build_cxx << "\", \"" << build_cxx << "\" },\n";
		istr << "			{ \"" << cfg::build_ar << "\", \"" << build_ar << "\" },\n";
		istr << "			{ \"" << cfg::build_ld << "\", \"" << build_ld << "\" },\n";
		if(!ctor_includedir.empty())
		{
			istr << "			{ \"" << cfg::ctor_includedir << "\", \"" << ctor_includedir << "\" },\n";
			ctor::includedir = ctor_includedir;
		}
		if(!ctor_libdir.empty())
		{
			istr << "			{ \"" << cfg::ctor_libdir << "\", \"" << ctor_libdir << "\" },\n";
			ctor::libdir = ctor_libdir;
		}

		istr << "		},\n";
		istr << "		.externals = {\n";

		for(const auto& ext : externalConfigs)
		{
			istr << "			{ \"" << ext.name << "\", {\n";
			Flags resolved_flags;
			if(std::holds_alternative<ExternalManual>(ext.external))
			{
				if(auto ret = resolv(settings, ext,
				                     std::get<ExternalManual>(ext.external),
				                     resolved_flags))
				{
					return ret;
				}
			}
			else
			{
				std::cout << "Unknown external type\n";
				return 1;
			}

			if(!resolved_flags.cxxflags.empty())
			{
				istr << "			  .cxxflags = {";
				for(const auto& flag : resolved_flags.cxxflags)
				{
					istr << "\"" << flag << "\",";
				}
				istr << "},\n";
			}

			if(!resolved_flags.cflags.empty())
			{
				istr << "			  .cflags = {";
				for(const auto& flag : resolved_flags.cflags)
				{
					istr << "\"" << flag << "\",";
				}
				istr << "},\n";
			}

			if(!resolved_flags.ldflags.empty())
			{
				istr << "			  .ldflags = {";
				for(const auto& flag : resolved_flags.ldflags)
				{
					istr << "\"" << flag << "\",";
				}
				istr << "},\n";
			}

			if(!resolved_flags.asmflags.empty())
			{
				istr << "			  .asmflags = {";
				for(const auto& flag : resolved_flags.asmflags)
				{
					istr << "\"" << flag << "\",";
				}
				istr << "},\n";
			}
			istr << "			}},\n";
		}

		istr << "		},\n";
		istr << "	};\n";
		istr << "	return cfg;\n";
		istr << "}\n";
		istr << "} // namespace ctor::\n\n";
	}

	{
		std::ofstream istr(configHeaderFile);
		istr << "#pragma once\n\n";
		istr << "#define HAS_FOO 1\n";
		istr << "//#define HAS_BAR 1\n";
	}

	return 0;
}

int configure(const Settings& global_settings, int argc, char* argv[])
{
	Settings settings{global_settings};

	std::vector<std::string> args;
	for(int i = 2; i < argc; ++i) // skip command and the first 'configure' arg
	{
		args.push_back(argv[i]);
	}

	std::map<std::string, std::string> env;
	auto cc_env = getenv("CC");
	if(cc_env)
	{
		env["CC"] = cc_env;
	}

	auto cxx_env = getenv("CXX");
	if(cxx_env)
	{
		env["CXX"] = cxx_env;
	}

	auto ar_env = getenv("AR");
	if(ar_env)
	{
		env["AR"] = ar_env;
	}

	auto ld_env = getenv("LD");
	if(ld_env)
	{
		env["LD"] = ld_env;
	}

	auto ret = regenerateCache(settings, args, env);
	if(ret != 0)
	{
		return ret;
	}

	recompileCheck(settings, argc, argv, false);

	return 0;
}

int reconfigure(const Settings& global_settings, int argc, char* argv[])
{
	Settings settings{global_settings};

	bool no_rerun{false};

	std::vector<std::string> args;
	for(int i = 2; i < argc; ++i) // skip executable name and 'reconfigure' arg
	{
		if(i == 2 && std::string(argv[i]) == "--no-rerun")
		{
			no_rerun = true;
			continue;
		}
		args.push_back(argv[i]);
	}

	const auto& cfg = configuration();

	std::cout << "Re-running configure:\n";
	for(const auto& e : cfg.env)
	{
		std::cout << e.first << "=\"" << e.second << "\" ";
	}
	std::cout << argv[0] << " configure ";
	for(const auto& arg : cfg.args)
	{
		std::cout << arg << " ";
	}
	std::cout << "\n";

	auto ret = regenerateCache(settings, cfg.args, cfg.env);
	if(ret != 0)
	{
		return ret;
	}

	recompileCheck(settings, 1, argv, false);

	if(no_rerun)
	{
		return 0; // this was originally invoked by configure, don't loop
	}

	return execute(argv[0], args);
}