From b6d629b9f4cc0a9c61d4f8832cc50fa8612ada9d Mon Sep 17 00:00:00 2001 From: Duncan Ogilvie Date: Thu, 18 Mar 2021 01:07:07 +0100 Subject: [PATCH] Support more target_xxx commands --- .gitignore | 4 +++- CMakeLists.txt | 8 ++++---- src/cmkrlib/cmake.cpp | 29 ++++++++++++++--------------- src/cmkrlib/cmake.hpp | 13 ++++++++++--- src/cmkrlib/gen.cpp | 28 +++++++++++++--------------- 5 files changed, 44 insertions(+), 38 deletions(-) diff --git a/.gitignore b/.gitignore index 2c4432e..26843a5 100644 --- a/.gitignore +++ b/.gitignore @@ -5,4 +5,6 @@ compile_commands.json temp.* .vs .cache -build*/ \ No newline at end of file +build*/ +.idea/ +cmake-build*/ diff --git a/CMakeLists.txt b/CMakeLists.txt index ebf4421..2f0c920 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -38,6 +38,10 @@ add_library(cmkrlib STATIC ${cmkrlib_SOURCES}) source_group(TREE ${PROJECT_SOURCE_DIR} FILES ${cmkrlib_SOURCES}) +target_compile_features(cmkrlib PUBLIC + cxx_std_11 +) + target_include_directories(cmkrlib PUBLIC include ) @@ -49,10 +53,6 @@ target_link_libraries(cmkrlib PUBLIC ordered_map ) -target_compile_features(cmkrlib PUBLIC - cxx_std_11 -) - set(cmkr_SOURCES src/main.cpp cmake.toml diff --git a/src/cmkrlib/cmake.cpp b/src/cmkrlib/cmake.cpp index 0ae3898..2c55cd3 100644 --- a/src/cmkrlib/cmake.cpp +++ b/src/cmkrlib/cmake.cpp @@ -196,21 +196,20 @@ CMake::CMake(const std::string &path, bool build) { #undef renamed - if (t.contains("include-directories")) { - target.include_directories = detail::to_string_vec(toml::find(t, "include-directories").as_array()); - } - - if (t.contains("link-libraries")) { - target.link_libraries = detail::to_string_vec(toml::find(t, "link-libraries").as_array()); - } - - if (t.contains("compile-features")) { - target.compile_features = detail::to_string_vec(toml::find(t, "compile-features").as_array()); - } - - if (t.contains("compile-definitions")) { - target.compile_definitions = detail::to_string_vec(toml::find(t, "compile-definitions").as_array()); - } + auto optional_array = [&t](const toml::key &k, std::vector &v) { + if (t.contains(k)) { + v = detail::to_string_vec(toml::find(t, k).as_array()); + } + }; + + optional_array("compile-definitions", target.compile_definitions); + optional_array("compile-features", target.compile_features); + optional_array("compile-options", target.compile_options); + optional_array("include-directories", target.include_directories); + optional_array("link-directories", target.link_directories); + optional_array("link-libraries", target.link_libraries); + optional_array("link-options", target.link_options); + optional_array("precompile-headers", target.precompile_headers); if (t.contains("alias")) { target.alias = toml::find(t, "alias").as_string(); diff --git a/src/cmkrlib/cmake.hpp b/src/cmkrlib/cmake.hpp index 2d91772..5d3403b 100644 --- a/src/cmkrlib/cmake.hpp +++ b/src/cmkrlib/cmake.hpp @@ -34,11 +34,18 @@ struct Package { struct Target { std::string name; std::string type; - std::vector sources; - std::vector include_directories; - std::vector compile_features; + + // https://cmake.org/cmake/help/latest/manual/cmake-commands.7.html#project-commands std::vector compile_definitions; + std::vector compile_features; + std::vector compile_options; + std::vector include_directories; + std::vector link_directories; std::vector link_libraries; + std::vector link_options; + std::vector precompile_headers; + std::vector sources; + std::string alias; std::map properties; }; diff --git a/src/cmkrlib/gen.cpp b/src/cmkrlib/gen.cpp index a52596e..beb08cc 100644 --- a/src/cmkrlib/gen.cpp +++ b/src/cmkrlib/gen.cpp @@ -436,21 +436,19 @@ int generate_cmake(const char *path) { cmd("add_library")(target.alias, "ALIAS", target.name); } - if (!target.include_directories.empty()) { - cmd("target_include_directories")(target.name, target_scope, target.include_directories).endl(); - } - - if (!target.link_libraries.empty()) { - cmd("target_link_libraries")(target.name, target_scope, target.link_libraries).endl(); - } - - if (!target.compile_features.empty()) { - cmd("target_compile_features")(target.name, target_scope, target.compile_features).endl(); - } - - if (!target.compile_definitions.empty()) { - cmd("target_compile_definitions")(target.name, target_scope, target.compile_definitions).endl(); - } + auto target_cmd = [&](const char* command, const std::vector& args) { + if(!args.empty()) { + cmd(command)(target.name, target_scope, args).endl(); + } + }; + + target_cmd("target_compile_definitions", target.compile_definitions); + target_cmd("target_compile_features", target.compile_features); + target_cmd("target_compile_options", target.compile_options); + target_cmd("target_include_directories", target.include_directories); + target_cmd("target_link_directories", target.link_directories); + target_cmd("target_link_libraries", target.link_libraries); + target_cmd("target_precompile_headers", target.precompile_headers); if (!target.properties.empty()) { cmd("set_target_properties")(target.name, "PROPERTIES", target.properties).endl();