diff --git a/CMakeLists.txt b/CMakeLists.txt index 11a63f4..543ab28 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -11,8 +11,12 @@ cmake_minimum_required(VERSION 2.8...3.8) set_property(GLOBAL PROPERTY USE_FOLDERS ON) -set(cmkr_PROJECT_VERSION 0.1.3) -project(cmkr VERSION ${cmkr_PROJECT_VERSION}) +project(cmkr + LANGUAGES + CXX + VERSION 0.1.3 + DESCRIPTION "CMakeLists generator from TOML" +) add_subdirectory(third_party) diff --git a/cmake.toml b/cmake.toml index 080f0f2..570a36c 100644 --- a/cmake.toml +++ b/cmake.toml @@ -4,6 +4,8 @@ minimum = "2.8...3.8" [project] name = "cmkr" version = "0.1.3" +description = "CMakeLists generator from TOML" +languages = ["CXX"] cmake-after = "add_subdirectory(third_party)" [target.cmkrlib] diff --git a/src/cmkrlib/cmake.cpp b/src/cmkrlib/cmake.cpp index c49ca2d..3a64862 100644 --- a/src/cmkrlib/cmake.cpp +++ b/src/cmkrlib/cmake.cpp @@ -64,6 +64,8 @@ CMake::CMake(const std::string &path, bool build) { const auto &project = toml::find(toml, "project"); project_name = toml::find(project, "name").as_string(); project_version = toml::find_or(project, "version", ""); + project_description = toml::find_or(project, "description", ""); + project_languages = optional_array(project, "languages"); cmake_before = toml::find_or(project, "cmake-before", ""); cmake_after = toml::find_or(project, "cmake-after", ""); include_before = optional_array(project, "include-before"); diff --git a/src/cmkrlib/cmake.hpp b/src/cmkrlib/cmake.hpp index 7c86fd7..97abbd4 100644 --- a/src/cmkrlib/cmake.hpp +++ b/src/cmkrlib/cmake.hpp @@ -92,6 +92,8 @@ struct CMake { std::vector build_args; std::string project_name; std::string project_version; + std::string project_description; + std::vector project_languages; std::string cmake_before; std::string cmake_after; std::vector include_before; diff --git a/src/cmkrlib/gen.cpp b/src/cmkrlib/gen.cpp index 24dab82..374a967 100644 --- a/src/cmkrlib/gen.cpp +++ b/src/cmkrlib/gen.cpp @@ -121,6 +121,15 @@ struct CommandEndl { void endl() { ss << '\n'; } }; +template +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; @@ -158,11 +167,12 @@ struct Command { return result; } - const char *indent(int n) { + std::string indent(int n) { + std::string result; for (int i = 0; i < n; i++) { - ss << '\t'; + result += '\t'; } - return ""; + return result; } template @@ -205,6 +215,32 @@ struct Command { return true; } + template + bool print_arg(const std::pair> &kv) { + if (kv.second.empty()) { + return true; + } + + ss << '\n'; + ss << indent(depth + 1) << kv.first << '\n'; + for (const auto &s : kv.second) { + ss << indent(depth + 2) << quote(s) << '\n'; + } + + return true; + } + + template + bool print_arg(const std::pair &kv) { + if (kv.second.empty()) { + return true; + } + + ss << indent(depth + 1) << kv.first << ' ' << quote(kv.second) << '\n'; + + return true; + } + template bool print_arg(const T &value) { if (first_arg) { @@ -320,11 +356,11 @@ int generate_cmake(const char *path, bool root) { ss << "\")\n\n"; } - if (!cmake.project_name.empty() && !cmake.project_version.empty()) { - auto name = cmake.project_name; - auto version = cmake.project_version; - cmd("set")(name + "_PROJECT_VERSION", version); - cmd("project")(name, "VERSION", "${" + name + "_PROJECT_VERSION}").endl(); + if (!cmake.project_name.empty()) { + auto languages = std::make_pair("LANGUAGES", cmake.project_languages); + auto version = std::make_pair("VERSION", cmake.project_version); + auto description = std::make_pair("DESCRIPTION", cmake.project_description); + cmd("project")(cmake.project_name, languages, version, description).endl(); } if (!cmake.cmake_after.empty()) {