Skip to content

Commit bb7874a

Browse files
committed
Improve compilation times
Compilation with make went from 13 -> 10 seconds. CMAKE_UNITY_BUILD went even lower to 6 seconds. Closes #11
1 parent 91dbf29 commit bb7874a

File tree

3 files changed

+33
-24
lines changed

3 files changed

+33
-24
lines changed

include/project_parser.hpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
#pragma once
22

3-
#include <map>
43
#include <mpark/variant.hpp>
5-
#include <stdexcept>
64
#include <string>
75
#include <tsl/ordered_map.h>
86
#include <vector>

src/cmake_generator.cpp

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,10 @@
44
#include <resources/cmkr.hpp>
55

66
#include "fs.hpp"
7-
#include <cassert>
87
#include <cstdio>
9-
#include <cstring>
108
#include <fstream>
11-
#include <iomanip>
12-
#include <new>
13-
#include <regex>
149
#include <sstream>
1510
#include <stdexcept>
16-
#include <string>
1711

1812
namespace cmkr {
1913
namespace gen {
@@ -157,9 +151,9 @@ struct Command {
157151
Command(std::stringstream &ss, int depth, std::string command, std::string post_comment)
158152
: ss(ss), depth(depth), command(std::move(command)), post_comment(std::move(post_comment)) {}
159153

160-
~Command() {
154+
~Command() noexcept(false) {
161155
if (!generated) {
162-
assert(false && "Incorrect usage of cmd(), you probably forgot ()");
156+
throw std::runtime_error("Incorrect usage of cmd(), you probably forgot ()");
163157
}
164158
}
165159

@@ -407,10 +401,28 @@ struct Generator {
407401
};
408402

409403
static bool vcpkg_valid_identifier(const std::string &name) {
410-
const std::regex reserved("prn|aux|nul|con|lpt[1-9]|com[1-9]|core|default");
411-
const std::regex ok("[a-z0-9]+(-[a-z0-9]+)*");
412-
std::cmatch m;
413-
return !std::regex_match(name.c_str(), m, reserved) && std::regex_match(name.c_str(), m, ok);
404+
// prn|aux|nul|con|lpt[1-9]|com[1-9]|core|default
405+
auto is_reserved = [](const std::string &s) {
406+
if (s == "prn" || s == "aux" || s == "nul" || s == "con" || s == "core" || s == "default") {
407+
return true;
408+
}
409+
if (s.length() == 4 && (s.compare(0, 3, "lpt") == 0 || s.compare(0, 3, "com") == 0) && (s[3] >= '1' && s[3] <= '9')) {
410+
return true;
411+
}
412+
return false;
413+
};
414+
// [a-z0-9]+(-[a-z0-9]+)*
415+
auto is_identifier = [](const std::string &s) {
416+
for (size_t i = 0; i < s.length(); i++) {
417+
auto c = s[i];
418+
if ((c >= 'a' && c <= 'z') || (c >= '0' && c <= '9') || (i > 0 && c == '-')) {
419+
continue;
420+
}
421+
return false;
422+
}
423+
return true;
424+
};
425+
return is_identifier(name) && !is_reserved(name);
414426
}
415427

416428
static std::string vcpkg_escape_identifier(const std::string &name) {
@@ -795,7 +807,7 @@ void generate_cmake(const char *path, const parser::Project *parent_project) {
795807
target_scope = "PUBLIC";
796808
break;
797809
default:
798-
assert("Unimplemented enum value" && false);
810+
throw std::runtime_error("Unimplemented enum value");
799811
}
800812

801813
cmd(add_command)(target.name, target_type).endl();

src/project_parser.cpp

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,14 @@
66
#include <stdexcept>
77
#include <toml.hpp>
88
#include <tsl/ordered_map.h>
9-
#include <tsl/ordered_set.h>
109

1110
template <>
1211
const char *enumStrings<cmkr::parser::TargetType>::data[] = {"executable", "library", "shared", "static", "interface", "custom", "object"};
1312

1413
namespace cmkr {
1514
namespace parser {
1615

17-
using TomlBasicValue = toml::basic_value<toml::preserve_comments, tsl::ordered_map, std::vector>;
16+
using TomlBasicValue = toml::basic_value<toml::discard_comments, tsl::ordered_map, std::vector>;
1817

1918
template <typename EnumType>
2019
static EnumType to_enum(const std::string &str, const std::string &help_name) {
@@ -63,8 +62,8 @@ static std::string format_key_error(const std::string &error, const toml::key &k
6362

6463
class TomlChecker {
6564
const TomlBasicValue &m_v;
66-
tsl::ordered_set<toml::key> m_visited;
67-
tsl::ordered_set<toml::key> m_conditionVisited;
65+
tsl::ordered_map<toml::key, bool> m_visited;
66+
tsl::ordered_map<toml::key, bool> m_conditionVisited;
6867

6968
public:
7069
TomlChecker(const TomlBasicValue &v, const toml::key &ky) : m_v(toml::find(v, ky)) {}
@@ -91,7 +90,7 @@ class TomlChecker {
9190
// Handle visiting logic
9291
for (const auto &itr : destination) {
9392
if (!itr.first.empty()) {
94-
m_conditionVisited.emplace(itr.first);
93+
m_conditionVisited.emplace(itr.first, true);
9594
}
9695
}
9796
visit(ky);
@@ -122,7 +121,7 @@ class TomlChecker {
122121
return toml::find(m_v, ky);
123122
}
124123

125-
void visit(const toml::key &ky) { m_visited.insert(ky); }
124+
void visit(const toml::key &ky) { m_visited.emplace(ky, true); }
126125

127126
void check(const tsl::ordered_map<std::string, std::string> &conditions) const {
128127
for (const auto &itr : m_v.as_table()) {
@@ -182,7 +181,7 @@ Project::Project(const Project *parent, const std::string &path, bool build) {
182181
if (!fs::exists(toml_path)) {
183182
throw std::runtime_error("No cmake.toml was found!");
184183
}
185-
const auto toml = toml::parse<toml::preserve_comments, tsl::ordered_map, std::vector>(toml_path.string());
184+
const auto toml = toml::parse<toml::discard_comments, tsl::ordered_map, std::vector>(toml_path.string());
186185

187186
TomlCheckerRoot checker;
188187

@@ -274,7 +273,7 @@ Project::Project(const Project *parent, const std::string &path, bool build) {
274273
}
275274

276275
if (toml.contains("settings")) {
277-
using set_map = std::map<std::string, TomlBasicValue>;
276+
using set_map = tsl::ordered_map<std::string, TomlBasicValue>;
278277
const auto &sets = toml::find<set_map>(toml, "settings");
279278
for (const auto &itr : sets) {
280279
Setting s;
@@ -507,7 +506,7 @@ bool is_root_path(const std::string &path) {
507506
if (!fs::exists(toml_path)) {
508507
return false;
509508
}
510-
const auto toml = toml::parse<toml::preserve_comments, tsl::ordered_map, std::vector>(toml_path.string());
509+
const auto toml = toml::parse<toml::discard_comments, tsl::ordered_map, std::vector>(toml_path.string());
511510
return toml.contains("project");
512511
}
513512

0 commit comments

Comments
 (0)