2 #include <unordered_map>
5 // This program generates an mconfig.h file. It is used in the build process.
7 // Map of variable name to value. Variables are passed via command line and stored
9 std::unordered_map<std::string, std::string> vars;
11 char to_hex_digit(int i)
13 if (i < 10) return i + '0';
17 // turn a string into a C++-source string, eg:
20 // "he said \"hello\""
21 static std::string stringify(std::string a)
23 std::string out = "\"";
25 for (std::string::size_type i = 0; i < a.length(); i++) {
27 if (c == '\n') out += "\\n";
28 else if (c == '\t') out += "\\t";
29 else if (c == '\"') out += "\\\"";
32 out += to_hex_digit((c & 0xF0) >> 4);
33 out += to_hex_digit((c & 0x0F));
42 // parse a NAME=VALUE argument and store in the variable map
43 void parse_arg(std::string arg)
45 auto idx = arg.find("=", 0, 1);
46 if (idx == std::string::npos) {
47 throw std::string("Couldn't parse argument: ") + arg;
50 auto name = arg.substr(0, idx);
51 auto value = arg.substr(idx + 1);
52 vars.emplace(std::move(name), std::move(value));
55 int main(int argc, char **argv)
57 for (int i = 1; i < argc; i++) {
62 cout << "// This file is auto-generated by mconfig-gen.cc." << endl;
63 cout << "constexpr static char SYSCONTROLSOCKET[] = " << stringify(vars["SYSCONTROLSOCKET"]) << ";" << endl;
64 cout << "constexpr static char SBINDIR[] = " << stringify(vars["SBINDIR"]) << ";" << endl;