From 11b3061d475e7fdac82eb00489339b05a9918665 Mon Sep 17 00:00:00 2001 From: MoAlyousef Date: Thu, 10 Sep 2020 00:30:26 +0300 Subject: [PATCH] require cmake 3.14 or higher --- CMakeLists.txt | 2 +- README.md | 27 +++++++++++++++------------ cmake.toml | 6 +++--- src/gen.cpp | 44 +++++++++++++++++++++++++++++--------------- 4 files changed, 48 insertions(+), 31 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index c2d704c..23c18a4 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,4 +1,4 @@ -cmake_minimum_required(VERSION 3.0) +cmake_minimum_required(VERSION 3.14) project(cmkr VERSION 0.1.0) diff --git a/README.md b/README.md index ca1c76a..f86d136 100644 --- a/README.md +++ b/README.md @@ -3,7 +3,7 @@ A CMakeLists.txt generator from TOML. Still WIP. ## Building -cmkr requires a C++17 compiler, cmake and git. It depends on toml11 by ToruNiina, which is added as a git submodule. +cmkr requires a C++17 compiler, cmake >= 3.14 and git. It depends on toml11 by ToruNiina, which is added as a git submodule. ``` git clone https://github.com/moalyousef/cmkr cd cmkr @@ -16,7 +16,7 @@ cmake --build bin cmkr parses cmake.toml files (using toml11) at the project directory. A basic hello world format with the minimum required fields: ```toml [cmake] -minimum_required = "3.0" +minimum = "3.14" [project] name = "app" @@ -31,7 +31,7 @@ sources = ["src/main.cpp"] This project's cmake.toml: ```toml [cmake] -minimum_required = "3.0" +minimum = "3.14" [project] name = "cmkr" @@ -41,40 +41,43 @@ version = "0.1.0" name = "cmkrlib" type = "static" sources = ["src/args.cpp", "src/gen.cpp", "src/help.cpp"] -include_dirs = ["vendor"] +include-dirs = ["vendor"] features = ["cxx_std_17"] [[bin]] name = "cmkr" type = "exe" sources = ["src/main.cpp"] -link_libs = ["cmkrlib"] +link-libs = ["cmkrlib"] ``` Currently supported fields: ```toml [cmake] # required for top-level project -minimum_required = "3.0" # required -cpp_flags = [] # optional -c_flags = [] # optional -link_flags = [] # optional +minimum = "3.14" # required +cpp-flags = [] # optional +c-flags = [] # optional +link-flags = [] # optional subdirs = [] # optional [project] # required per project name = "app" # required version = "0.1.0" # required -[dependencies] # optional, runs find_package, use "*" to ignore version +[find] # optional, runs find_package, use "*" to ignore version boost = "1.74.0" # optional +[fetch] # optional, runs fetchContent +toml11 = { git = "https://github.com/ToruNiina/toml11", git-tag = "v3.5.0" } # optional + [[bin]] # required, can define several binaries name = "app" # required type = "exe" # required (exe || shared || static) sources = ["src/main.cpp"] # required -include_dirs = [] # optional +include-dirs = [] # optional features = [] # optional defines = [] # optional -link_libs = [] # optional +link-libs = [] # optional ``` The cmkr executable can be run from the command-line: diff --git a/cmake.toml b/cmake.toml index 54035dc..9ac386e 100644 --- a/cmake.toml +++ b/cmake.toml @@ -1,5 +1,5 @@ [cmake] -minimum_required = "3.0" +minimum = "3.14" [project] name = "cmkr" @@ -9,12 +9,12 @@ version = "0.1.0" name = "cmkrlib" type = "static" sources = ["src/args.cpp", "src/gen.cpp", "src/help.cpp"] -include_dirs = ["vendor"] +include-dirs = ["vendor"] features = ["cxx_std_17"] [[bin]] name = "cmkr" type = "exe" sources = ["src/main.cpp"] -link_libs = ["cmkrlib"] +link-libs = ["cmkrlib"] diff --git a/src/gen.cpp b/src/gen.cpp index 2d1d744..11f2293 100644 --- a/src/gen.cpp +++ b/src/gen.cpp @@ -39,7 +39,7 @@ void generate_project(const char *str) { std::ofstream ofs2("cmake.toml"); if (ofs2.is_open()) { - ofs2 << "[cmake]\nminimum_required = \"3.0\"\n\n[project]\nname = \"" + ofs2 << "[cmake]\nminimum = \"3.14\"\n\n[project]\nname = \"" << dir_name.string() << "\"\nversion = " "\"0.1.0\"\n\n[[bin]]\nname = \"" @@ -58,7 +58,7 @@ void generate_project(const char *str) { std::ofstream ofs2("cmake.toml"); if (ofs2.is_open()) { - ofs2 << "[cmake]\nminimum_required = \"3.0\"\n\n[project]\nname = \"" + ofs2 << "[cmake]\nminimum = \"3.14\"\n\n[project]\nname = \"" << dir_name.string() << "\"\nversion = " "\"0.1.0\"\n\n[[bin]]\nname = \"" @@ -79,30 +79,30 @@ void generate_cmake(const char *path) { const auto toml = toml::parse((fs::path(path) / "cmake.toml").string()); if (toml.contains("cmake")) { const auto &cmake = toml::find(toml, "cmake"); - const std::string cmake_min = toml::find(cmake, "minimum_required").as_string(); + const std::string cmake_min = toml::find(cmake, "minimum").as_string(); ss << "cmake_minimum_required(VERSION " << cmake_min << ")\n\n"; - if (cmake.contains("cpp_flags")) { + if (cmake.contains("cpp-flags")) { ss << "set(CMAKE_CXX_FLAGS ${CMAKE_CXX_FLAGS}"; - const auto flags = toml::find(cmake, "cpp_flags").as_array(); + const auto flags = toml::find(cmake, "cpp-flags").as_array(); for (const auto &flag : flags) { ss << " " << flag; } ss << ")\n\n"; } - if (cmake.contains("c_flags")) { + if (cmake.contains("c-flags")) { ss << "set(CMAKE_C_FLAGS ${CMAKE_C_FLAGS}"; - const auto flags = toml::find(cmake, "c_flags").as_array(); + const auto flags = toml::find(cmake, "c-flags").as_array(); for (const auto &flag : flags) { ss << " " << flag; } ss << ")\n\n"; } - if (cmake.contains("link_flags")) { + if (cmake.contains("link-flags")) { ss << "set(CMAKE_EXE_LINKER_FLAGS ${CMAKE_EXE_LINKER_FLAGS}"; - const auto flags = toml::find(cmake, "link_flags").as_array(); + const auto flags = toml::find(cmake, "link-flags").as_array(); for (const auto &flag : flags) { ss << " " << flag; } @@ -127,9 +127,9 @@ void generate_cmake(const char *path) { ss << "project(" << proj_name << " VERSION " << proj_version << ")\n\n"; } - if (toml.contains("dependencies")) { + if (toml.contains("find")) { std::map deps = - toml::find>(toml, "dependencies"); + toml::find>(toml, "find"); for (const auto &dep : deps) { ss << "find_package(" << dep.first; if (dep.second != "*") { @@ -140,6 +140,20 @@ void generate_cmake(const char *path) { } } + if (toml.contains("fetch")) { + std::map> deps = + toml::find>>(toml, "fetch"); + ss << "include(FetchContent)\n\n"; + for (const auto &dep : deps) { + ss << "FetchContent_Declare(\n\t" << dep.first << "\n"; + for (const auto &arg: dep.second) { + ss << "\t" << arg.first << " " << arg.second << "\n"; + } + ss << "\t)\n\n" + << "FetchContent_MakeAvailable("<< dep.first << ")\n\n"; + } + } + ss << "\nset(CMAKE_EXPORT_COMPILE_COMMANDS ON)\n\n"; if (toml.contains("bin")) { @@ -170,8 +184,8 @@ void generate_cmake(const char *path) { << add_command << "(" << bin_name << " " << bin_type << " ${" << detail::to_upper(bin_name) << "_SOURCES})\n\n"; - if (bin.contains("include_dirs")) { - const auto includes = toml::find(bin, "include_dirs").as_array(); + if (bin.contains("include-dirs")) { + const auto includes = toml::find(bin, "include-dirs").as_array(); ss << "target_include_directories(" << bin_name << " PUBLIC\n\t"; for (const auto &inc : includes) { ss << inc << "\n\t"; @@ -179,8 +193,8 @@ void generate_cmake(const char *path) { ss << ")\n\n"; } - if (bin.contains("link_libs")) { - const auto libraries = toml::find(bin, "link_libs").as_array(); + if (bin.contains("link-libs")) { + const auto libraries = toml::find(bin, "link-libs").as_array(); ss << "target_link_libraries(" << bin_name << " PUBLIC\n\t"; for (const auto &l : libraries) { ss << l << "\n\t";