From 7ec2279a779a0f35f61a29512bd78ff3a687c400 Mon Sep 17 00:00:00 2001 From: MoAlyousef Date: Sat, 12 Sep 2020 16:38:16 +0300 Subject: [PATCH] throw when no cmake.toml is found --- src/cmake.cpp | 135 +++++++++++++++++++++++++------------------------- 1 file changed, 68 insertions(+), 67 deletions(-) diff --git a/src/cmake.cpp b/src/cmake.cpp index 62a4ef9..a4c88ae 100644 --- a/src/cmake.cpp +++ b/src/cmake.cpp @@ -1,6 +1,7 @@ #include "cmake.hpp" #include +#include #include namespace fs = std::filesystem; @@ -19,92 +20,92 @@ std::vector to_string_vec( } // namespace detail CMake::CMake(const std::string &path, bool build) { - if (fs::exists(fs::path(path) / "cmake.toml")) { - if (build) { - const auto toml = toml::parse("cmake.toml"); - if (toml.contains("cmake")) { - const auto &cmake = toml::find(toml, "cmake"); - - if (cmake.contains("bin-dir")) { - bin_dir = toml::find(cmake, "bin-dir").as_string(); - } - - if (cmake.contains("generator")) { - generator = toml::find(cmake, "generator").as_string(); - } + if (!fs::exists(fs::path(path) / "cmake.toml")) { + throw std::runtime_error("No cmake.toml was found!"); + } + if (build) { + const auto toml = toml::parse("cmake.toml"); + if (toml.contains("cmake")) { + const auto &cmake = toml::find(toml, "cmake"); - if (cmake.contains("arguments")) { - gen_args = detail::to_string_vec(toml::find(cmake, "arguments").as_array()); - } + if (cmake.contains("bin-dir")) { + bin_dir = toml::find(cmake, "bin-dir").as_string(); } - } else { - const auto toml = toml::parse((fs::path(path) / "cmake.toml").string()); - if (toml.contains("cmake")) { - const auto &cmake = toml::find(toml, "cmake"); - cmake_version = toml::find(cmake, "minimum").as_string(); - - if (cmake.contains("cpp-flags")) { - cppflags = detail::to_string_vec(toml::find(cmake, "cpp-flags").as_array()); - } - if (cmake.contains("c-flags")) { - cflags = detail::to_string_vec(toml::find(cmake, "c-flags").as_array()); - } - - if (cmake.contains("link-flags")) { - linkflags = detail::to_string_vec(toml::find(cmake, "link-flags").as_array()); - } + if (cmake.contains("generator")) { + generator = toml::find(cmake, "generator").as_string(); + } - if (cmake.contains("subdirs")) { - subdirs = detail::to_string_vec(toml::find(cmake, "subdirs").as_array()); - } + if (cmake.contains("arguments")) { + gen_args = detail::to_string_vec(toml::find(cmake, "arguments").as_array()); + } + } + } else { + const auto toml = toml::parse((fs::path(path) / "cmake.toml").string()); + if (toml.contains("cmake")) { + const auto &cmake = toml::find(toml, "cmake"); + cmake_version = toml::find(cmake, "minimum").as_string(); + + if (cmake.contains("cpp-flags")) { + cppflags = detail::to_string_vec(toml::find(cmake, "cpp-flags").as_array()); } - if (toml.contains("project")) { - const auto &project = toml::find(toml, "project"); - proj_name = toml::find(project, "name").as_string(); - proj_version = toml::find(project, "version").as_string(); + if (cmake.contains("c-flags")) { + cflags = detail::to_string_vec(toml::find(cmake, "c-flags").as_array()); } - if (toml.contains("find-package")) { - using pkg_map = std::map; - packages = toml::find(toml, "find-package"); + if (cmake.contains("link-flags")) { + linkflags = detail::to_string_vec(toml::find(cmake, "link-flags").as_array()); } - if (toml.contains("fetch-content")) { - using content_map = std::map>; - contents = toml::find(toml, "fetch-content"); + if (cmake.contains("subdirs")) { + subdirs = detail::to_string_vec(toml::find(cmake, "subdirs").as_array()); } + } + + if (toml.contains("project")) { + const auto &project = toml::find(toml, "project"); + proj_name = toml::find(project, "name").as_string(); + proj_version = toml::find(project, "version").as_string(); + } - if (toml.contains("bin")) { - const auto &bins = toml::find(toml, "bin").as_array(); + if (toml.contains("find-package")) { + using pkg_map = std::map; + packages = toml::find(toml, "find-package"); + } - for (const auto &bin : bins) { - Bin b; - b.name = toml::find(bin, "name").as_string(); - b.type = toml::find(bin, "type").as_string(); + if (toml.contains("fetch-content")) { + using content_map = std::map>; + contents = toml::find(toml, "fetch-content"); + } - b.sources = detail::to_string_vec(toml::find(bin, "sources").as_array()); + if (toml.contains("bin")) { + const auto &bins = toml::find(toml, "bin").as_array(); - if (bin.contains("include-dirs")) { - b.include_dirs = - detail::to_string_vec(toml::find(bin, "include-dirs").as_array()); - } + for (const auto &bin : bins) { + Bin b; + b.name = toml::find(bin, "name").as_string(); + b.type = toml::find(bin, "type").as_string(); - if (bin.contains("link-libs")) { - b.link_libs = - detail::to_string_vec(toml::find(bin, "link-libs").as_array()); - } + b.sources = detail::to_string_vec(toml::find(bin, "sources").as_array()); - if (bin.contains("features")) { - b.features = detail::to_string_vec(toml::find(bin, "features").as_array()); - } + if (bin.contains("include-dirs")) { + b.include_dirs = + detail::to_string_vec(toml::find(bin, "include-dirs").as_array()); + } + + if (bin.contains("link-libs")) { + b.link_libs = detail::to_string_vec(toml::find(bin, "link-libs").as_array()); + } + + if (bin.contains("features")) { + b.features = detail::to_string_vec(toml::find(bin, "features").as_array()); + } - if (bin.contains("defines")) { - b.defines = detail::to_string_vec(toml::find(bin, "defines").as_array()); - } - binaries.push_back(b); + if (bin.contains("defines")) { + b.defines = detail::to_string_vec(toml::find(bin, "defines").as_array()); } + binaries.push_back(b); } } }