Refactor cmake generation

vcpkg-wip
Duncan Ogilvie 4 years ago
parent 1af0b5f1cc
commit f2dbd1fcb8

@ -76,8 +76,10 @@ target_link_libraries(cmkr PRIVATE
)
install(
TARGETS cmkr
DESTINATION ${CMAKE_INSTALL_PREFIX}/bin
COMPONENT cmkr
TARGETS
cmkr
DESTINATION
"${CMAKE_INSTALL_PREFIX}/bin"
COMPONENT
cmkr
)

@ -16,8 +16,6 @@
namespace cmkr {
namespace gen {
namespace detail {
inline std::string to_upper(const std::string &str) {
std::string temp;
temp.reserve(str.size());
@ -65,7 +63,18 @@ static std::vector<std::string> expand_cmake_path(const fs::path &p) {
return temp;
}
} // namespace detail
static std::vector<std::string> expand_cmake_paths(const std::vector<std::string> &sources) {
// TODO: add duplicate checking
std::vector<std::string> result;
for (const auto &src : sources) {
auto path = fs::path(src);
auto expanded = expand_cmake_path(path);
for (const auto &f : expanded) {
result.push_back(f);
}
}
return result;
}
int generate_project(const char *str) {
fs::create_directory("src");
@ -76,12 +85,12 @@ int generate_project(const char *str) {
std::string target;
std::string dest;
if (!strcmp(str, "executable")) {
mainbuf = detail::format(hello_world, "main");
mainbuf = format(hello_world, "main");
installed = "targets";
target = dir_name;
dest = "bin";
} else if (!strcmp(str, "static") || !strcmp(str, "shared") || !strcmp(str, "library")) {
mainbuf = detail::format(hello_world, "test");
mainbuf = format(hello_world, "test");
installed = "targets";
target = dir_name;
dest = "lib";
@ -94,7 +103,7 @@ int generate_project(const char *str) {
"! Supported types are: executable, library, shared, static, interface");
}
const auto tomlbuf = detail::format(cmake_toml, dir_name.c_str(), dir_name.c_str(), str, installed.c_str(), target.c_str(), dest.c_str());
const auto tomlbuf = format(cmake_toml, dir_name.c_str(), dir_name.c_str(), str, installed.c_str(), target.c_str(), dest.c_str());
if (strcmp(str, "interface")) {
std::ofstream ofs("src/main.cpp");
@ -121,15 +130,6 @@ struct CommandEndl {
void endl() { ss << '\n'; }
};
template <typename T>
struct NamedArg {
std::string name;
T value;
NamedArg() = default;
NamedArg(std::string name, T value) : name(std::move(name)), value(std::move(value)) {}
};
// Credit: JustMagic
struct Command {
std::stringstream &ss;
@ -182,11 +182,11 @@ struct Command {
return true;
}
had_newline = true;
for (const auto &value : vec) {
ss << '\n' << indent(depth + 1) << quote(value);
print_arg(value);
}
had_newline = true;
first_arg = false;
return true;
}
@ -197,25 +197,9 @@ struct Command {
}
for (const auto &itr : map) {
ss << '\n' << indent(depth + 1) << itr.first;
ss << '\n' << indent(depth + 2) << quote(itr.second);
}
had_newline = true;
first_arg = false;
return true;
}
bool print_arg(const std::string &value) {
if (value.empty()) {
return true;
print_arg(itr);
}
if (first_arg) {
first_arg = false;
} else {
ss << (had_newline ? '\n' : ' ');
}
ss << quote(value);
return true;
}
@ -225,40 +209,57 @@ struct Command {
return true;
}
ss << '\n' << indent(depth + 1) << kv.first;
had_newline = true;
print_arg(kv.first);
depth++;
for (const auto &s : kv.second) {
ss << '\n' << indent(depth + 2) << quote(s);
print_arg(s);
}
had_newline = true;
first_arg = false;
depth--;
return true;
}
template <class K, class V>
bool print_arg(const std::pair<K, V> &kv) {
std::stringstream tmp;
tmp << kv.second;
auto str = tmp.str();
if (kv.second.empty()) {
return true;
}
ss << '\n' << indent(depth + 1) << kv.first;
ss << '\n' << indent(depth + 2) << quote(kv.second);
had_newline = true;
first_arg = false;
print_arg(kv.first);
depth++;
print_arg(str);
depth--;
return true;
}
template <class T>
bool print_arg(const T &value) {
if (first_arg) {
bool print_arg(const T &value, bool indentation = true) {
std::stringstream tmp;
tmp << value;
auto str = tmp.str();
if (str.empty()) {
return true;
}
if (indentation) {
if (had_newline) {
first_arg = false;
ss << '\n' << indent(depth + 1);
} else if (first_arg) {
first_arg = false;
} else {
ss << (had_newline ? '\n' : ' ');
ss << ' ';
}
std::stringstream tmp;
tmp << value;
ss << quote(tmp.str());
}
ss << quote(str);
return true;
}
@ -275,10 +276,12 @@ struct Command {
};
int generate_cmake(const char *path, bool root) {
if (fs::exists(fs::path(path) / "cmake.toml")) {
cmake::CMake cmake(path, false);
std::stringstream ss;
if (!fs::exists(fs::path(path) / "cmake.toml")) {
throw std::runtime_error("No cmake.toml found!");
}
// Helper lambdas for more convenient CMake generation
std::stringstream ss;
int indent = 0;
auto cmd = [&ss, &indent](const std::string &command) {
if (command.empty())
@ -298,7 +301,6 @@ int generate_cmake(const char *path, bool root) {
return CommandEndl(ss);
};
auto endl = [&ss]() { ss << '\n'; };
auto tolf = [](const std::string &str) {
std::string result;
for (char ch : str) {
@ -308,20 +310,25 @@ int generate_cmake(const char *path, bool root) {
}
return result;
};
comment("This file was generated automatically by cmkr.").endl();
if (!cmake.cmake_before.empty()) {
ss << tolf(cmake.cmake_before) << "\n\n";
}
if (!cmake.include_before.empty()) {
for (const auto &file : cmake.include_before) {
auto inject_includes = [&cmd, &endl](const std::vector<std::string> &includes) {
if (!includes.empty()) {
for (const auto &file : includes) {
// TODO: warn/error if file doesn't exist?
cmd("include")(file);
}
endl();
}
};
auto inject_cmake = [&ss, &tolf](const std::string &cmake) {
if (!cmake.empty()) {
ss << tolf(cmake) << "\n\n";
}
};
comment("This file was generated automatically by cmkr.").endl();
// TODO: add link with proper documentation
cmake::CMake cmake(path, false);
if (root) {
comment("Regenerate CMakeLists.txt file when necessary");
@ -365,6 +372,9 @@ int generate_cmake(const char *path, bool root) {
ss << "\")\n\n";
}
inject_includes(cmake.include_before);
inject_cmake(cmake.cmake_before);
if (!cmake.project_name.empty()) {
auto languages = std::make_pair("LANGUAGES", cmake.project_languages);
auto version = std::make_pair("VERSION", cmake.project_version);
@ -372,41 +382,24 @@ int generate_cmake(const char *path, bool root) {
cmd("project")(cmake.project_name, languages, version, description).endl();
}
if (!cmake.cmake_after.empty()) {
ss << tolf(cmake.cmake_after) << "\n\n";
}
if (!cmake.include_after.empty()) {
for (const auto &file : cmake.include_after) {
// TODO: warn/error if file doesn't exist?
cmd("include")(file);
}
endl();
}
inject_includes(cmake.include_after);
inject_cmake(cmake.cmake_after);
if (!cmake.packages.empty()) {
for (const auto &dep : cmake.packages) {
ss << "find_package(" << dep.name << ' ';
if (dep.version != "*") {
ss << dep.version << " ";
}
if (dep.required) {
ss << "REQUIRED ";
}
if (!dep.components.empty()) {
ss << "COMPONENTS ";
for (const auto &comp : dep.components) {
ss << comp << " ";
}
}
ss << ")\n\n";
auto version = dep.version;
if (version == "*")
version.clear();
auto required = dep.required ? "REQUIRED" : "";
auto components = std::make_pair("COMPONENTS", dep.components);
cmd("find_package")(dep.name, version, required, components).endl();
}
}
if (!cmake.contents.empty()) {
ss << "include(FetchContent)\n\n";
cmd("include")("FetchContent").endl();
for (const auto &dep : cmake.contents) {
ss << "message(STATUS \"Fetching " << dep.first << "...\")\n";
cmd("message")("STATUS", "Fetching " + dep.first + "...");
ss << "FetchContent_Declare(\n\t" << dep.first << "\n";
for (const auto &arg : dep.second) {
std::string first_arg = arg.first;
@ -425,17 +418,18 @@ int generate_cmake(const char *path, bool root) {
} else {
// don't change arg
}
ss << "\t" << first_arg << " " << arg.second << "\n";
ss << "\t" << first_arg << "\n\t\t" << arg.second << "\n";
}
ss << ")\n"
<< "FetchContent_MakeAvailable(" << dep.first << ")\n\n";
ss << ")\n";
cmd("FetchContent_MakeAvailable")(dep.first).endl();
}
}
if (!cmake.options.empty()) {
for (const auto &opt : cmake.options) {
ss << "option(" << opt.name << " \"" << opt.comment << "\" " << (opt.val ? "ON" : "OFF") << ")\n\n";
cmd("option")(opt.name, opt.comment, opt.val ? "ON" : "OFF");
}
endl();
}
if (!cmake.settings.empty()) {
@ -446,23 +440,19 @@ int generate_cmake(const char *path, bool root) {
} else {
set_val = mpark::get<0>(set.val) ? "ON" : "OFF";
}
ss << "set(" << set.name << " " << set_val;
;
if (set.cache) {
std::string typ;
if (set.val.index() == 1)
typ = "STRING";
else
typ = "BOOL";
ss << " CACHE " << typ << " \"" << set.comment << "\"";
if (set.force)
ss << " FORCE";
auto typ = set.val.index() == 1 ? "STRING" : "BOOL";
auto force = set.force ? "FORCE" : "";
cmd("set")(set.name, set_val, typ, set.comment, force);
} else {
cmd("set")(set.name, set_val);
}
ss << ")\n\n";
}
endl();
}
// generate_cmake is called on these recursively later
// generate_cmake is called on the subdirectories recursively later
if (!cmake.subdirs.empty()) {
for (const auto &dir : cmake.subdirs) {
// clang-format off
@ -482,28 +472,11 @@ int generate_cmake(const char *path, bool root) {
if (!cmake.targets.empty()) {
for (const auto &target : cmake.targets) {
comment("Target " + target.name);
if (!target.cmake_before.empty()) {
ss << tolf(target.cmake_before) << "\n\n";
}
if (!target.include_before.empty()) {
for (const auto &file : target.include_before) {
// TODO: warn/error if file doesn't exist?
cmd("include")(file);
}
endl();
}
inject_includes(target.include_before);
inject_cmake(target.cmake_before);
if (!target.sources.empty()) {
// TODO: add duplicate checking
std::vector<std::string> sources;
for (const auto &src : target.sources) {
auto path = fs::path(src);
auto expanded = detail::expand_cmake_path(path);
for (const auto &f : expanded) {
sources.push_back(f);
}
}
auto sources = expand_cmake_paths(target.sources);
if (sources.empty()) {
throw std::runtime_error(target.name + " sources wildcard found 0 files");
}
@ -583,17 +556,8 @@ int generate_cmake(const char *path, bool root) {
cmd("set_target_properties")(target.name, "PROPERTIES", target.properties).endl();
}
if (!target.cmake_after.empty()) {
ss << tolf(target.cmake_after) << "\n\n";
}
if (!target.include_after.empty()) {
for (const auto &file : target.include_after) {
// TODO: warn/error if file doesn't exist?
cmd("include")(file);
}
endl();
}
inject_includes(target.include_after);
inject_cmake(target.cmake_after);
}
}
@ -601,59 +565,32 @@ int generate_cmake(const char *path, bool root) {
cmd("include")("CTest");
cmd("enable_testing")().endl();
for (const auto &test : cmake.tests) {
ss << "add_test(NAME " << test.name << " COMMAND " << test.cmd;
if (!test.args.empty()) {
for (const auto &arg : test.args) {
ss << " " << arg;
}
}
ss << ")\n\n";
auto name = std::make_pair("NAME", test.name);
auto command = std::make_pair("COMMAND", test.cmd);
cmd("add_test")(name, command, test.args).endl();
}
}
if (!cmake.installs.empty()) {
for (const auto &inst : cmake.installs) {
ss << "install(\n";
if (!inst.targets.empty()) {
ss << "\tTARGETS ";
for (const auto &target : inst.targets) {
ss << target << " ";
}
}
if (!inst.dirs.empty()) {
ss << "\tDIRS ";
for (const auto &dir : inst.dirs) {
ss << dir << " ";
}
}
auto targets = std::make_pair("TARGETS", inst.targets);
auto dirs = std::make_pair("DIRS", inst.dirs);
std::vector<std::string> files_data;
if (!inst.files.empty()) {
ss << "\tFILES ";
int files_added = 0;
for (const auto &file : inst.files) {
auto path = detail::expand_cmake_path(fs::path(file));
for (const auto &f : path) {
ss << f << " ";
files_added++;
}
}
if (files_added == 0) {
files_data = expand_cmake_paths(inst.files);
if (files_data.empty()) {
throw std::runtime_error("[[install]] files wildcard did not resolve to any files");
}
}
if (!inst.configs.empty()) {
ss << "\tCONFIGURATIONS";
for (const auto &conf : inst.configs) {
ss << conf << " ";
}
}
ss << "\n\tDESTINATION " << inst.destination << "\n\t";
if (!inst.targets.empty())
ss << "COMPONENT " << inst.targets[0] << "\n)\n\n";
else
ss << "\n)\n\n";
auto files = std::make_pair("FILES", inst.files);
auto configs = std::make_pair("CONFIGURATIONS", inst.configs);
auto destination = std::make_pair("DESTINATION", inst.destination);
auto component = std::make_pair("COMPONENT", inst.targets.empty() ? "" : inst.targets.front());
cmd("install")(targets, dirs, files, configs, destination, component);
}
}
// Generate CMakeLists.txt
auto list_path = fs::path(path) / "CMakeLists.txt";
auto should_regenerate = [&list_path, &ss]() {
@ -682,9 +619,7 @@ int generate_cmake(const char *path, bool root) {
if (fs::exists(fs::path(sub) / "cmake.toml"))
generate_cmake(sub.c_str(), false);
}
} else {
throw std::runtime_error("No cmake.toml found!");
}
return 0;
}
} // namespace gen

Loading…
Cancel
Save