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

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

#include <getoptpp/getoptpp.hpp>

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

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

const std::map<std::string, std::string> default_configuration{};
const std::map<std::string, std::string>& __attribute__((weak)) configuration()
{
	return default_configuration;
}

bool hasConfiguration(const std::string& key)
{
	const auto& c = configuration();
	return c.find(key) != c.end();
}

const std::string& getConfiguration(const std::string& key,
                                    const std::string& defaultValue)
{
	const auto& c = configuration();
	if(hasConfiguration(key))
	{
		return c.at(key);
	}

	return defaultValue;
}

int configure(int argc, char* argv[])
{
	Settings settings;

	settings.builddir = "build";

	std::string cmd_str;
	for(int i = 0; i < argc; ++i)
	{
		if(i > 0)
		{
			cmd_str += " ";
		}
		cmd_str += argv[i];
	}

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

	std::string build_arch;
	std::string host_arch;

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

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

	opt.add("build", required_argument, key++,
	        "Configure for building on specified architecture.",
	        [&]() {
		        build_arch = 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("help", no_argument, 'h',
	        "Print this help text.",
	        [&]() {
		        std::cout << "configure usage stuff\n";
		        opt.help();
		        exit(0);
		        return 0;
	        });

	opt.process(argc, argv);

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

	auto tasks = getTasks(settings);

	bool needs_cpp{false};
	bool needs_c{false};
	bool needs_ar{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;
		}
	}

	{
		std::ofstream istr(configurationFile);
		istr << "#include \"libcppbuild.h\"\n\n";
		istr << "const std::map<std::string, std::string>& configuration()\n";
		istr << "{\n";
		istr << "	static std::map<std::string, std::string> c =\n";
		istr << "	{\n";
		istr << "		{ \"cmd\", \"" << cmd_str << "\" },\n";
		istr << "		{ \"" << cfg::builddir << "\", \"" << settings.builddir << "\" },\n";
		istr << "		{ \"" << cfg::target_cc << "\", \"/usr/bin/gcc\" },\n";
		istr << "		{ \"" << cfg::target_cpp << "\", \"/usr/bin/g++\" },\n";
		istr << "		{ \"" << cfg::target_ar << "\", \"/usr/bin/ar\" },\n";
		istr << "		{ \"" << cfg::target_ld << "\", \"/usr/bin/ld\" },\n";
		istr << "		{ \"" << cfg::host_cc << "\", \"/usr/bin/gcc\" },\n";
		istr << "		{ \"" << cfg::host_cpp << "\", \"/usr/bin/g++\" },\n";
		istr << "		{ \"" << cfg::host_ar << "\", \"/usr/bin/ar\" },\n";
		istr << "		{ \"" << cfg::host_ld << "\", \"/usr/bin/ld\" },\n";
		istr << "	};\n";
		istr << "	return c;\n";
		istr << "}\n";
	}

	return 0;
}