Add basic support for [subdir.mydir]

toml-checker archive_3cd84a97
Duncan Ogilvie 3 years ago
parent 9932e501ce
commit 3cd84a9708

@ -110,6 +110,15 @@ struct Install {
std::string destination; std::string destination;
}; };
struct Subdir {
std::string name;
std::string condition;
Condition<std::string> cmake_before;
Condition<std::string> cmake_after;
ConditionVector include_before;
ConditionVector include_after;
};
struct Project { struct Project {
// This is the CMake version required to use all cmkr versions. // This is the CMake version required to use all cmkr versions.
std::string cmake_version = "3.15"; std::string cmake_version = "3.15";
@ -117,7 +126,7 @@ struct Project {
std::string build_dir = "build"; std::string build_dir = "build";
std::string generator; std::string generator;
std::string config; std::string config;
Condition<std::vector<std::string>> subdirs; Condition<std::vector<std::string>> project_subdirs;
std::vector<std::string> cppflags; std::vector<std::string> cppflags;
std::vector<std::string> cflags; std::vector<std::string> cflags;
std::vector<std::string> linkflags; std::vector<std::string> linkflags;
@ -140,9 +149,12 @@ struct Project {
std::vector<Test> tests; std::vector<Test> tests;
std::vector<Install> installs; std::vector<Install> installs;
tsl::ordered_map<std::string, std::string> conditions; tsl::ordered_map<std::string, std::string> conditions;
std::vector<Subdir> subdirs;
Project(const std::string &path, bool build); Project(const std::string &path, bool build);
}; };
bool is_root_path(const std::string &path);
} // namespace parser } // namespace parser
} // namespace cmkr } // namespace cmkr

@ -1,7 +1,7 @@
#include "cmake_generator.hpp" #include "cmake_generator.hpp"
#include "project_parser.hpp"
#include "error.hpp" #include "error.hpp"
#include "literals.hpp" #include "literals.hpp"
#include "project_parser.hpp"
#include "fs.hpp" #include "fs.hpp"
#include <cassert> #include <cassert>
@ -639,26 +639,52 @@ int generate_cmake(const char *path, bool root) {
endl(); endl();
} }
auto add_subdir = [&](const std::string &dir) {
// clang-format off
comment(dir);
cmd("set")("CMKR_CMAKE_FOLDER", "${CMAKE_FOLDER}");
cmd("if")("CMAKE_FOLDER");
cmd("set")("CMAKE_FOLDER", "${CMAKE_FOLDER}/" + dir);
cmd("else")();
cmd("set")("CMAKE_FOLDER", dir);
cmd("endif")();
// clang-format on
cmd("add_subdirectory")(dir);
cmd("set")("CMAKE_FOLDER", "${CMKR_CMAKE_FOLDER}").endl();
};
// generate_cmake is called on the subdirectories recursively later // generate_cmake is called on the subdirectories recursively later
if (!project.subdirs.empty()) { if (!project.project_subdirs.empty()) {
gen.handle_condition(project.subdirs, [&](const std::string &, const std::vector<std::string> &subdirs) { gen.handle_condition(project.project_subdirs, [&](const std::string &, const std::vector<std::string> &subdirs) {
for (const auto &dir : subdirs) { for (const auto &dir : subdirs) {
// clang-format off add_subdir(dir);
comment(dir);
cmd("set")("CMKR_CMAKE_FOLDER", "${CMAKE_FOLDER}");
cmd("if")("CMAKE_FOLDER");
cmd("set")("CMAKE_FOLDER", "${CMAKE_FOLDER}/" + dir);
cmd("else")();
cmd("set")("CMAKE_FOLDER", dir);
cmd("endif")();
// clang-format on
cmd("add_subdirectory")(dir);
cmd("set")("CMAKE_FOLDER", "${CMKR_CMAKE_FOLDER}").endl();
} }
}); });
endl(); endl();
} }
for (const auto &subdir : project.subdirs) {
if (!subdir.condition.empty()) {
const auto &condition = subdir.condition;
if (project.conditions.count(condition) == 0) {
throw std::runtime_error("Unknown condition '" + condition + "' for [subdir." + subdir.name + "]");
}
gen.cmd("if", condition)(RawArg(project.conditions[condition]));
}
gen.handle_condition(subdir.include_before,
[&](const std::string &, const std::vector<std::string> &includes) { inject_includes(includes); });
gen.handle_condition(subdir.cmake_before, [&](const std::string &, const std::string &cmake) { inject_cmake(cmake); });
add_subdir(subdir.name);
gen.handle_condition(subdir.include_after, [&](const std::string &, const std::vector<std::string> &includes) { inject_includes(includes); });
gen.handle_condition(subdir.cmake_after, [&](const std::string &, const std::string &cmake) { inject_cmake(cmake); });
if (!subdir.condition.empty()) {
cmd("endif")();
}
}
if (!project.targets.empty()) { if (!project.targets.empty()) {
for (const auto &target : project.targets) { for (const auto &target : project.targets) {
@ -875,13 +901,29 @@ int generate_cmake(const char *path, bool root) {
} }
} }
for (const auto &itr : project.subdirs) { auto generate_subdir = [path](const fs::path &sub) {
// Skip generating for subdirectories that have a cmake.toml with a [project] in it
fs::path subpath;
for (const auto &p : sub) {
subpath /= p;
if (parser::is_root_path(subpath.string())) {
return;
}
}
subpath = path / sub;
if (fs::exists(subpath / "cmake.toml")) {
generate_cmake(subpath.string().c_str(), false);
}
};
for (const auto &itr : project.project_subdirs) {
for (const auto &sub : itr.second) { for (const auto &sub : itr.second) {
auto subpath = fs::path(path) / fs::path(sub); generate_subdir(sub);
if (fs::exists(subpath / "cmake.toml"))
generate_cmake(subpath.string().c_str(), false);
} }
} }
for (const auto &subdir : project.subdirs) {
generate_subdir(subdir.name);
}
return 0; return 0;
} }

@ -62,10 +62,11 @@ static void get_optional(const TomlBasicValue &v, const toml::key &ky, T &destin
} }
Project::Project(const std::string &path, bool build) { Project::Project(const std::string &path, bool build) {
if (!fs::exists(fs::path(path) / "cmake.toml")) { const auto toml_path = fs::path(path) / "cmake.toml";
if (!fs::exists(toml_path)) {
throw std::runtime_error("No cmake.toml was found!"); throw std::runtime_error("No cmake.toml was found!");
} }
const auto toml = toml::parse<toml::preserve_comments, tsl::ordered_map, std::vector>((fs::path(path) / "cmake.toml").string()); const auto toml = toml::parse<toml::preserve_comments, tsl::ordered_map, std::vector>(toml_path.string());
if (build) { if (build) {
if (toml.contains("cmake")) { if (toml.contains("cmake")) {
const auto &cmake = toml::find(toml, "cmake"); const auto &cmake = toml::find(toml, "cmake");
@ -99,7 +100,21 @@ Project::Project(const std::string &path, bool build) {
get_optional(project, "cmake-after", cmake_after); get_optional(project, "cmake-after", cmake_after);
get_optional(project, "include-before", include_before); get_optional(project, "include-before", include_before);
get_optional(project, "include-after", include_after); get_optional(project, "include-after", include_after);
get_optional(project, "subdirs", subdirs); get_optional(project, "subdirs", project_subdirs);
}
if (toml.contains("subdir")) {
const auto &subs = toml::find(toml, "subdir").as_table();
for (const auto &sub : subs) {
Subdir subdir;
subdir.name = sub.first;
get_optional(sub.second, "condition", subdir.condition);
get_optional(sub.second, "cmake-before", subdir.cmake_before);
get_optional(sub.second, "cmake-after", subdir.cmake_after);
get_optional(sub.second, "include-before", subdir.include_before);
get_optional(sub.second, "include-after", subdir.include_after);
subdirs.push_back(subdir);
}
} }
if (toml.contains("settings")) { if (toml.contains("settings")) {
@ -311,5 +326,15 @@ Project::Project(const std::string &path, bool build) {
} }
} }
} }
bool is_root_path(const std::string &path) {
const auto toml_path = fs::path(path) / "cmake.toml";
if (!fs::exists(toml_path)) {
return false;
}
const auto toml = toml::parse<toml::preserve_comments, tsl::ordered_map, std::vector>(toml_path.string());
return toml.contains("project");
}
} // namespace parser } // namespace parser
} // namespace cmkr } // namespace cmkr

Loading…
Cancel
Save