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

#include <filesystem>
#include <iostream>

#include <cassert>

ctor::toolchain getToolChain(const std::string& compiler)
{
	std::filesystem::path cc(compiler);
	auto cc_cmd = cc.stem().string();

	// Note: "g++" is a substring of "clang++" so "clang++" must be tested first.
	if(cc_cmd.find("clang++") != std::string::npos)
	{
		return ctor::toolchain::clang;
	}
	else if(cc_cmd.find("g++") != std::string::npos)
	{
		return ctor::toolchain::gcc;
	}

	std::cerr << "Unsupported output system.\n";
	return ctor::toolchain::gcc;
}

ctor::toolchain getToolChain(ctor::output_system system)
{
	const auto& cfg = ctor::get_configuration();
	if(system == ctor::output_system::host)
	{
		if(cfg.host_toolchain != ctor::toolchain::none)
		{
			return cfg.host_toolchain;
		}
		return getToolChain(cfg.get(ctor::cfg::host_cxx, "g++"));
	}
	else
	{
		if(cfg.build_toolchain != ctor::toolchain::none)
		{
			return cfg.build_toolchain;
		}
		return getToolChain(cfg.get(ctor::cfg::build_cxx, "g++"));
	}
}

namespace
{
std::vector<std::string> getOptionGcc(opt option, const std::string& arg)
{
	switch(option)
	{
	case opt::output:
		return {"-o", arg};
	case opt::debug:
		return {"-g"};
	case opt::strip:
		return {"-s"};
	case opt::warn_all:
		return {"-Wall"};
	case opt::warnings_as_errors:
		return {"-Werror"};
	case opt::generate_dep_tree:
		return {"-MMD"};
	case opt::no_link:
		return {"-c"};
	case opt::include_path:
		return {"-I" + arg};
	case opt::library_path:
		return {"-L" + arg};
	case opt::link:
		return {"-l" + arg};
	case opt::cpp_std:
		return {"-std=" + arg};
	case opt::build_shared:
		return {"-shared"};
	case opt::threads:
		return {"-pthread"};
	case opt::optimization:
		return {"-O" + arg};
	case opt::position_independent_code:
		return {"-fPIC"};
	case opt::position_independent_executable:
		return {"-fPIE"};
	case opt::custom:
		return {arg};
	}

	std::cerr << "Unsupported compiler option.\n";
	return {};
}
}

std::vector<std::string> getOption(ctor::toolchain toolchain,
                                   opt option,
                                   const std::string& arg)
{
	switch(toolchain)
	{
	case ctor::toolchain::gcc:
	case ctor::toolchain::clang:
		return getOptionGcc(option, arg);
	case ctor::toolchain::any:
	case ctor::toolchain::none:
		break;
	}

	std::cerr << "Unsupported tool-chain.\n";
	return {};
}

namespace {
std::pair<opt, std::string> getOptionGcc(const std::string& flag)
{
	if(flag.substr(0, 2) == "-I")
	{
		std::string path = flag.substr(2);
		path.erase(0, path.find_first_not_of(' '));
		return { opt::include_path, path };
	}

	if(flag.substr(0, 2) == "-L")
	{
		std::string path = flag.substr(2);
		path.erase(0, path.find_first_not_of(' '));
		return { opt::library_path, path };
	}

	return { opt::custom, flag };
}
}

std::pair<opt, std::string> getOption(const std::string& flag,
                                      ctor::toolchain toolchain)
{
	switch(toolchain)
	{
	case ctor::toolchain::gcc:
	case ctor::toolchain::clang:
		return getOptionGcc(flag);
	case ctor::toolchain::any:
	case ctor::toolchain::none:
		break;
	}

	std::cerr << "Unsupported tool-chain.\n";
	return { opt::custom, flag };
}