diff --git a/CMakeLists.txt b/CMakeLists.txt index 41d9798..45bfd6d 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,7 +1,7 @@ cmake_minimum_required(VERSION 3.0) project(cmkr) -project(cmkr VERSION 0.1.0 LANGUAGES CXX) +project(cmkr VERSION 0.1.0) set(CMAKE_EXPORT_COMPILE_COMMANDS ON) diff --git a/cmake.toml b/cmake.toml index 72487dd..ff6088c 100644 --- a/cmake.toml +++ b/cmake.toml @@ -1,16 +1,12 @@ [cmake] # required minimum_required = "3.0" # required -arguments = [""] # optional cpp_flags = [""] # optional c_flags = [""] # optional link_flags = [""] # optional -prefix_path = [""] # optional -export_compile_commands = true # optional [project] # required name = "cmkr" # required version = "0.1.0" # required -languages = [""] # optional (C, CXX, OBJC, OBJCXX) [dependencies] # optional, runs find_package # boost = "1.74.0" @@ -18,7 +14,6 @@ languages = [""] # optional (C, CXX, OBJC, OBJCXX) [[app]] # optional name = "cmkr" # required sources = ["src/main.cpp", "src/args.cpp", "src/gen.cpp", "src/help.cpp"] # required -headers = [""] # optional include_directories = ["vendor"] # optional compile_features = ["cxx_std_17"] # optional link_libraries = [""] # optional @@ -27,7 +22,6 @@ link_libraries = [""] # optional # name = "cmkr" # required # sources = ["src/args.cpp", "src/gen.cpp", "src/help.cpp"] # required # type = "shared" # required (shared, static) -# headers = [""] # optional # include_directories = ["vendor"] # optional # compile_features = ["cxx_std_17"] # optional # link_libraries = [""] # optional diff --git a/src/gen.cpp b/src/gen.cpp index e04cc70..8ef8cbe 100644 --- a/src/gen.cpp +++ b/src/gen.cpp @@ -1,6 +1,8 @@ #include "gen.hpp" #include #include +#include +#include #include #include #include @@ -79,7 +81,23 @@ void generate_cmake() { const std::string proj_version = toml::find(project, "version").as_string(); ss << "cmake_minimum_required(VERSION " << cmake_min << ")\n\n" - << "project(" << proj_name << " VERSION " << proj_version << ")\n\n"; + << "project(" << proj_name << " VERSION " << proj_version << ")\n\n" + << "set(CMAKE_EXPORT_COMPILE_COMMANDS ON)\n\n"; + + if (toml.contains("dependencies")) { + std::map deps = + toml::find>(toml, "dependencies"); + for (const auto& dep : deps) { + ss << "find_package(" << dep.first; + if (dep.second != "*") { + ss << " " << dep.second << " CONFIG REQUIRED)\n"; + } else { + ss << " CONFIG REQUIRED)\n"; + } + } + } + + ss << "\n"; if (toml.contains("app")) { const auto &bins = toml::find(toml, "app"); @@ -97,6 +115,34 @@ void generate_cmake() { ss << "\t)\n\n" << "add_executable(" << bin_name << " ${" << detail::to_upper(bin_name) << "_SOURCES})\n\n"; + + if (bin.contains("include_directories")) { + ss << "target_include_directories(" << bin_name << " PUBLIC\n\t"; + const auto includes = toml::find(bin, "link_libraries"); + for (auto k = 0; k < includes.size(); ++k) { + const std::string inc = toml::find(includes, i).as_string(); + ss << inc << "\n\t"; + } + ss << ")"; + } + if (bin.contains("link_libraries")) { + ss << "target_link_libraries(" << bin_name << " PUBLIC\n\t"; + const auto includes = toml::find(bin, "link_libraries"); + for (auto k = 0; k < includes.size(); ++k) { + const std::string inc = toml::find(includes, i).as_string(); + ss << inc << "\n\t"; + } + ss << ")"; + } + if (bin.contains("compile_features")) { + ss << "target_compile_features(" << bin_name << " PUBLIC\n\t"; + const auto includes = toml::find(bin, "compile_features"); + for (auto k = 0; k < includes.size(); ++k) { + const std::string inc = toml::find(includes, i).as_string(); + ss << inc << "\n\t"; + } + ss << ")"; + } } } @@ -117,13 +163,42 @@ void generate_cmake() { ss << "\t)\n\n" << "add_library(" << lib_name << " " << detail::to_upper(type) << " ${" << detail::to_upper(lib_name) << "_SOURCES})\n\n"; + + if (lib.contains("include_directories")) { + ss << "target_include_directories(" << lib_name << " PUBLIC\n\t"; + const auto includes = toml::find(lib, "include_directories"); + for (auto k = 0; k < includes.size(); ++k) { + const std::string inc = toml::find(includes, i).as_string(); + ss << inc << "\n\t"; + } + ss << ")"; + } + if (lib.contains("link_libraries")) { + ss << "target_link_libraries(" << lib_name << " PUBLIC\n\t"; + const auto includes = toml::find(lib, "link_libraries"); + for (auto k = 0; k < includes.size(); ++k) { + const std::string inc = toml::find(includes, i).as_string(); + ss << inc << "\n\t"; + } + ss << ")"; + } + if (lib.contains("compile_features")) { + ss << "target_compile_features(" << lib_name << " PUBLIC\n\t"; + const auto includes = toml::find(lib, "compile_features"); + for (auto k = 0; k < includes.size(); ++k) { + const std::string inc = toml::find(includes, i).as_string(); + ss << inc << "\n\t"; + } + ss << ")"; + } } + + std::ofstream ofs("CMakeLists.txt"); + if (ofs.is_open()) { + ofs << ss.rdbuf(); + } + ofs.flush(); + ofs.close(); } - std::ofstream ofs("CMakeLists.txt"); - if (ofs.is_open()) { - ofs << ss.rdbuf(); - } - ofs.flush(); - ofs.close(); } } // namespace cmkr::gen \ No newline at end of file