From 6cb8119f392dcec7283d2cae1f64d6d883c949d8 Mon Sep 17 00:00:00 2001 From: Scott Bailey Date: Wed, 19 Apr 2023 16:09:40 -0500 Subject: [PATCH 1/7] Add 'file(GLOB -source /*)' to the 'build' subcommand generated CMake files. --- include/antler/project/cmake.hpp | 19 ++++++++++--------- src/cmake_templates.cpp | 22 ++++++++++++++-------- 2 files changed, 24 insertions(+), 17 deletions(-) diff --git a/include/antler/project/cmake.hpp b/include/antler/project/cmake.hpp index ee02dd7a..289099ef 100644 --- a/include/antler/project/cmake.hpp +++ b/include/antler/project/cmake.hpp @@ -54,8 +54,8 @@ namespace antler::project { /// @param v object that will be inserted in this CMakeLists.txt /// @return CMakeLists stream template - inline std::ostream& operator<<(T&& v) { - outs << std::forward(v); + inline std::ostream& operator<<(T&& v) { + outs << std::forward(v); outs.flush(); return outs; } @@ -117,7 +117,7 @@ namespace antler::project { } template - inline void emit_add_subdirectory(Stream& s, system::fs::path path, std::string_view name) noexcept { + inline void emit_add_subdirectory(Stream& s, system::fs::path path, std::string_view name) noexcept { s << add_subdirectory_template.render(datum{"path", (path / name).string()}); } @@ -156,15 +156,16 @@ namespace antler::project { template inline void emit_entry(Stream& s) noexcept { s << entry_template.render(datum{"proj", proj->name()}); } - + template inline void emit_object(Pops& pops, Stream& s, const object& obj) { km::mustache& temp = std::is_same_v ? add_contract_template : add_library_template; s << temp.render(datum{"obj_name", obj.name()} ("target_name", target_name(obj)) - ("obj_source", std::string(obj.name())+system::extension(obj.language()))); - + ("source_ext", system::extension(obj.language())) + ("open_hack", "${")); // open_hack must be "${" to work around Mustache decoding the wrong pair in "${{{". + s << target_include_template.render(datum{"target_name", target_name(obj)} ("obj_name", obj.name())); @@ -220,7 +221,7 @@ namespace antler::project { } private: - + // simple helper to clean km::data usage struct datum { template @@ -231,9 +232,9 @@ namespace antler::project { } template - inline datum(Str&& key, T&& val) + inline datum(Str&& key, T&& val) : data(std::forward(key), fwrd(std::forward(val))) {} - + template datum& operator()(Str&& key, T&& val) { data.set(std::forward(key), fwrd(std::forward(val))); diff --git a/src/cmake_templates.cpp b/src/cmake_templates.cpp index b6204e0e..846c6270 100644 --- a/src/cmake_templates.cpp +++ b/src/cmake_templates.cpp @@ -3,7 +3,8 @@ namespace antler::project { km::mustache cmake::add_subdirectory_template = {"add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/{{path}})\n\n"}; -km::mustache cmake::add_subdirectory2_template = {"add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/{{src_path}} ${CMAKE_CURRENT_BINARY_DIR}/{{bin_path}})\n\n"}; +km::mustache cmake::add_subdirectory2_template = {"add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/{{src_path}}\n" + " ${CMAKE_CURRENT_BINARY_DIR}/{{bin_path}})\n\n"}; km::mustache cmake::preamble_template = {"# Generated with {{tool}}, modify at your own risk\n" "cmake_minimum_required(VERSION {{major}}.{{minor}})\n" @@ -15,9 +16,9 @@ km::mustache cmake::project_stub_template = {"find_package(cdt)\n\n" km::mustache cmake::target_compile_template = {"target_compile_options({{target_name}} PUBLIC {{opt}})\n\n"}; -km::mustache cmake::target_include_template = {"target_include_directories({{target_name}}" - " PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/../../../include" - " ${CMAKE_CURRENT_SOURCE_DIR}/../../../include/{{obj_name}}" +km::mustache cmake::target_include_template = {"target_include_directories({{target_name}}\n" + " PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/../../../include\n" + " ${CMAKE_CURRENT_SOURCE_DIR}/../../../include/{{obj_name}}\n" " ./)\n\n"}; km::mustache cmake::target_link_libs_template = {"target_link_libraries({{target_name}} PUBLIC {{dep_name}})\n\n"}; @@ -38,8 +39,13 @@ km::mustache cmake::entry_template = {"include(ExternalProject)\n" " BUILD_ALWAYS 1\n" ")\n\n"}; -km::mustache cmake::add_contract_template = {"add_contract({{obj_name}} {{target_name}} ${CMAKE_CURRENT_SOURCE_DIR}/../../../apps/{{obj_name}}/{{obj_source}})\n\n"}; -km::mustache cmake::add_library_template = {"add_library({{target_name}} ${CMAKE_CURRENT_SOURCE_DIR}/../../../libs/{{obj_name}}/{{obj_source}})\n\n"}; - +// Note the open_hack value in the following 2 templates. open_hack must be set to "${" as a workaround for Mustache decoding the +// wrong pair in "${{{", +km::mustache cmake::add_contract_template = { + "file(GLOB {{target_name}}-source ${CMAKE_CURRENT_SOURCE_DIR}/../../../apps/{{obj_name}}/*{{source_ext}})\n" + "add_contract({{obj_name}} {{target_name}} {{open_hack}}{{target_name}}-source})\n\n"}; +km::mustache cmake::add_library_template = { + "file(GLOB {{target_name}}-source ${CMAKE_CURRENT_SOURCE_DIR}/../../../libs/{{obj_name}}/*{{source_ext}})\n\n" + "add_library({{target_name}} {{open_hack}}{{target_name}}-source})\n\n"}; + } // namespace antler::project - From 5d4528dac9f10ee6edfd75196fa0cfb34acda13e Mon Sep 17 00:00:00 2001 From: Scott Bailey Date: Wed, 19 Apr 2023 16:25:51 -0500 Subject: [PATCH 2/7] Reduce whitespace delta from editor. --- include/antler/project/cmake.hpp | 10 +++++----- src/cmake_templates.cpp | 1 + 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/include/antler/project/cmake.hpp b/include/antler/project/cmake.hpp index 289099ef..96b0c954 100644 --- a/include/antler/project/cmake.hpp +++ b/include/antler/project/cmake.hpp @@ -117,7 +117,7 @@ namespace antler::project { } template - inline void emit_add_subdirectory(Stream& s, system::fs::path path, std::string_view name) noexcept { + inline void emit_add_subdirectory(Stream& s, system::fs::path path, std::string_view name) noexcept { s << add_subdirectory_template.render(datum{"path", (path / name).string()}); } @@ -156,7 +156,7 @@ namespace antler::project { template inline void emit_entry(Stream& s) noexcept { s << entry_template.render(datum{"proj", proj->name()}); } - + template inline void emit_object(Pops& pops, Stream& s, const object& obj) { km::mustache& temp = std::is_same_v ? add_contract_template : add_library_template; @@ -221,7 +221,7 @@ namespace antler::project { } private: - + // simple helper to clean km::data usage struct datum { template @@ -232,9 +232,9 @@ namespace antler::project { } template - inline datum(Str&& key, T&& val) + inline datum(Str&& key, T&& val) : data(std::forward(key), fwrd(std::forward(val))) {} - + template datum& operator()(Str&& key, T&& val) { data.set(std::forward(key), fwrd(std::forward(val))); diff --git a/src/cmake_templates.cpp b/src/cmake_templates.cpp index 846c6270..63360630 100644 --- a/src/cmake_templates.cpp +++ b/src/cmake_templates.cpp @@ -49,3 +49,4 @@ km::mustache cmake::add_library_template = { "add_library({{target_name}} {{open_hack}}{{target_name}}-source})\n\n"}; } // namespace antler::project + From 884bce6953453a79191dbd7b842816441a36e67d Mon Sep 17 00:00:00 2001 From: Scott Bailey Date: Fri, 21 Apr 2023 16:48:25 -0500 Subject: [PATCH 3/7] Use a slightly less "hacky" name. --- include/antler/project/cmake.hpp | 12 ++++++------ src/cmake_templates.cpp | 7 +++---- 2 files changed, 9 insertions(+), 10 deletions(-) diff --git a/include/antler/project/cmake.hpp b/include/antler/project/cmake.hpp index 96b0c954..2b92cb7d 100644 --- a/include/antler/project/cmake.hpp +++ b/include/antler/project/cmake.hpp @@ -117,7 +117,7 @@ namespace antler::project { } template - inline void emit_add_subdirectory(Stream& s, system::fs::path path, std::string_view name) noexcept { + inline void emit_add_subdirectory(Stream& s, system::fs::path path, std::string_view name) noexcept { s << add_subdirectory_template.render(datum{"path", (path / name).string()}); } @@ -156,7 +156,7 @@ namespace antler::project { template inline void emit_entry(Stream& s) noexcept { s << entry_template.render(datum{"proj", proj->name()}); } - + template inline void emit_object(Pops& pops, Stream& s, const object& obj) { km::mustache& temp = std::is_same_v ? add_contract_template : add_library_template; @@ -164,7 +164,7 @@ namespace antler::project { s << temp.render(datum{"obj_name", obj.name()} ("target_name", target_name(obj)) ("source_ext", system::extension(obj.language())) - ("open_hack", "${")); // open_hack must be "${" to work around Mustache decoding the wrong pair in "${{{". + ("dollar_brace", "${")); // dollar_brace must be "${" to work around Mustache decoding the wrong pair in "${{{". s << target_include_template.render(datum{"target_name", target_name(obj)} ("obj_name", obj.name())); @@ -221,7 +221,7 @@ namespace antler::project { } private: - + // simple helper to clean km::data usage struct datum { template @@ -232,9 +232,9 @@ namespace antler::project { } template - inline datum(Str&& key, T&& val) + inline datum(Str&& key, T&& val) : data(std::forward(key), fwrd(std::forward(val))) {} - + template datum& operator()(Str&& key, T&& val) { data.set(std::forward(key), fwrd(std::forward(val))); diff --git a/src/cmake_templates.cpp b/src/cmake_templates.cpp index 63360630..74fe09b5 100644 --- a/src/cmake_templates.cpp +++ b/src/cmake_templates.cpp @@ -39,14 +39,13 @@ km::mustache cmake::entry_template = {"include(ExternalProject)\n" " BUILD_ALWAYS 1\n" ")\n\n"}; -// Note the open_hack value in the following 2 templates. open_hack must be set to "${" as a workaround for Mustache decoding the +// Note the dollar_brace value in the following 2 templates. dollar_brace must be set to "${" as a workaround for Mustache decoding the // wrong pair in "${{{", km::mustache cmake::add_contract_template = { "file(GLOB {{target_name}}-source ${CMAKE_CURRENT_SOURCE_DIR}/../../../apps/{{obj_name}}/*{{source_ext}})\n" - "add_contract({{obj_name}} {{target_name}} {{open_hack}}{{target_name}}-source})\n\n"}; + "add_contract({{obj_name}} {{target_name}} {{dollar_brace}}{{target_name}}-source})\n\n"}; km::mustache cmake::add_library_template = { "file(GLOB {{target_name}}-source ${CMAKE_CURRENT_SOURCE_DIR}/../../../libs/{{obj_name}}/*{{source_ext}})\n\n" - "add_library({{target_name}} {{open_hack}}{{target_name}}-source})\n\n"}; + "add_library({{target_name}} {{dollar_brace}}{{target_name}}-source})\n\n"}; } // namespace antler::project - From 471a0580a5886de2e452045251168b552466cd58 Mon Sep 17 00:00:00 2001 From: Scott Bailey Date: Fri, 21 Apr 2023 16:49:39 -0500 Subject: [PATCH 4/7] Add tests for populate and build. --- tools/CMakeLists.txt | 6 +++-- .../pandb_contract/apps/test/greeting.cpp | 3 +++ tools/tests/pandb_contract/apps/test/test.cpp | 12 +++++++++ .../pandb_contract/include/test/test.hpp | 13 +++++++++ tools/tests/pandb_contract/project.yaml | 13 +++++++++ tools/tests/populate_and_build.py | 27 +++++++++++++++++++ 6 files changed, 72 insertions(+), 2 deletions(-) create mode 100644 tools/tests/pandb_contract/apps/test/greeting.cpp create mode 100644 tools/tests/pandb_contract/apps/test/test.cpp create mode 100644 tools/tests/pandb_contract/include/test/test.hpp create mode 100644 tools/tests/pandb_contract/project.yaml create mode 100644 tools/tests/populate_and_build.py diff --git a/tools/CMakeLists.txt b/tools/CMakeLists.txt index 219e7c8b..66081aea 100644 --- a/tools/CMakeLists.txt +++ b/tools/CMakeLists.txt @@ -1,11 +1,12 @@ # @copyright See `LICENSE` in the root directory of this project. -add_executable(antler-proj main.cpp) +file(GLOB src_hpp *.hpp) +add_executable(antler-proj main.cpp ${src_hpp}) set_property(TARGET antler-proj PROPERTY CXX_STANDARD 17) target_link_libraries(antler-proj PUBLIC antler-project) macro(add_cli_test name file) - configure_file(${CMAKE_CURRENT_SOURCE_DIR}/tests/${file} ${CMAKE_CURRENT_BINARY_DIR}/tests/${file} COPYONLY) + configure_file(${CMAKE_CURRENT_SOURCE_DIR}/tests/${file} ${CMAKE_CURRENT_BINARY_DIR}/tests/${file} @ONLY) add_test(NAME ${name} COMMAND python3 ${file} WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/tests) endmacro() @@ -16,4 +17,5 @@ if (ANTLER_PROJ_BUILD_TESTS) #add_cli_test(add_to_project_tests add_to_tests.py) add_cli_test(init_tests init_tests.py) add_cli_test(add_and_update add_and_update.py) + add_cli_test(populate_and_build populate_and_build.py) endif() diff --git a/tools/tests/pandb_contract/apps/test/greeting.cpp b/tools/tests/pandb_contract/apps/test/greeting.cpp new file mode 100644 index 00000000..1adf6da2 --- /dev/null +++ b/tools/tests/pandb_contract/apps/test/greeting.cpp @@ -0,0 +1,3 @@ +#include + +std::string GREETING{"Hi there : %"}; diff --git a/tools/tests/pandb_contract/apps/test/test.cpp b/tools/tests/pandb_contract/apps/test/test.cpp new file mode 100644 index 00000000..f258532a --- /dev/null +++ b/tools/tests/pandb_contract/apps/test/test.cpp @@ -0,0 +1,12 @@ +#include + + +[[eosio::action]] +void test::hi( name nm ) { + /* generated example action */ + print_f("Hello : %", nm); +} + + +extern const std::string GREETING; +std::string greeting() { return GREETING; } diff --git a/tools/tests/pandb_contract/include/test/test.hpp b/tools/tests/pandb_contract/include/test/test.hpp new file mode 100644 index 00000000..47c8ff96 --- /dev/null +++ b/tools/tests/pandb_contract/include/test/test.hpp @@ -0,0 +1,13 @@ +#include + +using namespace eosio; + +class [[eosio::contract]] test: public contract { + public: + using contract::contract; + + [[eosio::action]] + void hi( name nm ); + using hi_action = action_wrapper<"hi"_n, &test::hi>; +}; + diff --git a/tools/tests/pandb_contract/project.yaml b/tools/tests/pandb_contract/project.yaml new file mode 100644 index 00000000..1bfbec5d --- /dev/null +++ b/tools/tests/pandb_contract/project.yaml @@ -0,0 +1,13 @@ +project: foo +version: 1.0.0 +apps: + - name: test + lang: CXX + compile_options: "" + link_options: "" +# depends: +# - name: ret42 +# location: larryk85/antler-proj-example-hello +# tag: "" +# release: ">=1.0" +# hash: "" diff --git a/tools/tests/populate_and_build.py b/tools/tests/populate_and_build.py new file mode 100644 index 00000000..f17822be --- /dev/null +++ b/tools/tests/populate_and_build.py @@ -0,0 +1,27 @@ +#! /usr/bin/env python3 + +""" Test `antler-proj add` and `antler proj update` commands. """ + +import os +import shutil +import subprocess + +from util import APROJ_EXE + +PROJECT_NAME="pandb_contract" +PROJECT_PATH=os.path.join("./",PROJECT_NAME) +SOURCE_PATH=os.path.join("@CMAKE_CURRENT_SOURCE_DIR@/tests",PROJECT_NAME) + + +def test_populate_and_build(): + """ Setup a clean project, then populate and build it. + """ + shutil.rmtree(PROJECT_PATH, ignore_errors=True) + assert shutil.copytree(SOURCE_PATH, PROJECT_PATH) + + subprocess.run([APROJ_EXE,"populate", PROJECT_PATH], check=True) + subprocess.run([APROJ_EXE,"build", PROJECT_PATH], check=True) + + +if __name__ == "__main__": + test_populate_and_build() From 1a0a18b8496505da83ba036292b20c3c01b64263 Mon Sep 17 00:00:00 2001 From: Scott Bailey Date: Sat, 22 Apr 2023 12:14:30 -0500 Subject: [PATCH 5/7] Add more documentation and debug prints. --- tools/tests/populate_and_build.py | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/tools/tests/populate_and_build.py b/tools/tests/populate_and_build.py index f17822be..198d5402 100644 --- a/tools/tests/populate_and_build.py +++ b/tools/tests/populate_and_build.py @@ -16,11 +16,21 @@ def test_populate_and_build(): """ Setup a clean project, then populate and build it. """ + # Remove any existing test artifact, then copy the test project over. shutil.rmtree(PROJECT_PATH, ignore_errors=True) assert shutil.copytree(SOURCE_PATH, PROJECT_PATH) - subprocess.run([APROJ_EXE,"populate", PROJECT_PATH], check=True) - subprocess.run([APROJ_EXE,"build", PROJECT_PATH], check=True) + # Populate the project. + result = subprocess.run([APROJ_EXE,"populate", PROJECT_PATH], capture_output=True, text=True, check=False) + print(result.stdout) + print(result.stderr) + assert result.returncode == 0 + + # Build the project. + result = subprocess.run([APROJ_EXE,"build", PROJECT_PATH], capture_output=True, text=True, check=False) + print(result.stdout) + print(result.stderr) + assert result.returncode == 0 if __name__ == "__main__": From 2421b6103a0d0297ab4b7c058e8dd448d5772fa5 Mon Sep 17 00:00:00 2001 From: Scott Bailey Date: Sat, 22 Apr 2023 12:18:57 -0500 Subject: [PATCH 6/7] Fix pylintrc to have a line length slightly more consistent with modern laptop monitors. --- .pylintrc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.pylintrc b/.pylintrc index a44c6018..e651501a 100644 --- a/.pylintrc +++ b/.pylintrc @@ -73,7 +73,7 @@ ignored-modules= # Control the amount of potential inferred values when inferring a single # object. This can help the performance when dealing with large functions or # complex, nested conditions. -limit-inference-results=100 +limit-inference-results=140 # List of plugins (as comma separated values of python module names) to load, # usually to register additional checkers. From 1cd481de34dae49770a436bbd27eabf6f192de3b Mon Sep 17 00:00:00 2001 From: Scott Bailey Date: Mon, 24 Apr 2023 17:02:41 -0500 Subject: [PATCH 7/7] Removal of Mustache in favor of fmt::format for eventual replacement of std::format. --- .gitmodules | 3 - external/CMakeLists.txt | 2 - external/Mustache | 1 - include/antler/project/cmake.hpp | 114 ++++++++++++---------------- include/antler/project/mustache.hpp | 42 ---------- src/CMakeLists.txt | 14 ++-- src/cmake_templates.cpp | 98 +++++++++++++----------- 7 files changed, 106 insertions(+), 168 deletions(-) delete mode 160000 external/Mustache delete mode 100644 include/antler/project/mustache.hpp diff --git a/.gitmodules b/.gitmodules index 67c55ed1..5c2e504c 100644 --- a/.gitmodules +++ b/.gitmodules @@ -10,9 +10,6 @@ [submodule "external/cturtle"] path = external/cturtle url = https://github.com/dimas1185/cturtle -[submodule "external/Mustache"] - path = external/Mustache - url = https://github.com/kainjow/Mustache [submodule "external/fmt"] path = external/fmt url = https://github.com/fmtlib/fmt diff --git a/external/CMakeLists.txt b/external/CMakeLists.txt index 5249f7ac..feec37d8 100644 --- a/external/CMakeLists.txt +++ b/external/CMakeLists.txt @@ -22,5 +22,3 @@ add_subdirectory(fmt) set(CTURTLE_ENABLE_TESTS OFF CACHE INTERNAL "Skip cturtle tests") set(CTURTLE_ENABLE_INSTALL OFF CACHE INTERNAL "Skip cturtle install") add_subdirectory(cturtle) - -add_subdirectory(Mustache) diff --git a/external/Mustache b/external/Mustache deleted file mode 160000 index 04277d55..00000000 --- a/external/Mustache +++ /dev/null @@ -1 +0,0 @@ -Subproject commit 04277d5552c6e46bee41a946b7d175a660ea1b3d diff --git a/include/antler/project/cmake.hpp b/include/antler/project/cmake.hpp index 2b92cb7d..9a8bdc91 100644 --- a/include/antler/project/cmake.hpp +++ b/include/antler/project/cmake.hpp @@ -9,13 +9,9 @@ #include #include -#include - namespace antler::project { - namespace km = kainjow::mustache; - /// @brief struct to encapsulate a CMakeLists.txt file struct cmake_lists { constexpr inline static std::string_view filename = "CMakeLists.txt"; @@ -76,17 +72,17 @@ namespace antler::project { constexpr inline static std::string_view libs_dir_name = "libs"; constexpr inline static std::string_view tests_dir_name = "tests"; - // `mustache` templates for the cmake - static km::mustache add_subdirectory_template; - static km::mustache add_subdirectory2_template; - static km::mustache preamble_template; - static km::mustache project_stub_template; - static km::mustache target_compile_template; - static km::mustache target_include_template; - static km::mustache target_link_libs_template; - static km::mustache entry_template; - static km::mustache add_contract_template; - static km::mustache add_library_template; + // std::format templates for the cmake + static std::string_view add_subdirectory_template; + static std::string_view add_subdirectory2_template; + static std::string_view preamble_template; + static std::string_view project_stub_template; + static std::string_view target_compile_template; + static std::string_view target_include_template; + static std::string_view target_link_libs_template; + static std::string_view entry_template; + static std::string_view add_contract_template; + static std::string_view add_library_template; cmake(const project& proj) : proj(&proj), @@ -118,22 +114,25 @@ namespace antler::project { template inline void emit_add_subdirectory(Stream& s, system::fs::path path, std::string_view name) noexcept { - s << add_subdirectory_template.render(datum{"path", (path / name).string()}); + s << fmt::format(add_subdirectory_template, (path / name).string()); } template inline void emit_preamble(Stream& s) noexcept { - s << preamble_template.render(datum{"tool", "antler-proj"} - ("major", minimum_major) - ("minor", minimum_minor) - ("proj_name", proj->name()) - ("proj_major", proj->version().major()) - ("proj_minor", proj->version().minor()) - ("proj_patch", proj->version().patch())); + s << fmt::format(preamble_template, + "antler-proj", + minimum_major, + minimum_minor, + proj->name(), + proj->version().major(), + proj->version().minor(), + proj->version().patch()); } template - inline void emit_project_stub(Stream& s) noexcept { s << project_stub_template.render({}); } + inline void emit_project_stub(Stream& s) noexcept { + s << fmt::format(project_stub_template); + } template inline void emit_dependencies(Populators&& pops, Stream& s, const object& obj) noexcept { @@ -142,43 +141,49 @@ namespace antler::project { system::debug_log("emitting dependencies for {0} at {1}", dep.name(), dep.location().empty() ? "local" : dep.location()); if (!dep.location().empty()) { std::string repo = std::string(github::get_repo(dep.location())); - s << add_subdirectory2_template.render(datum{"src_path", "../../dependencies/"+repo+"/build/apps/"} - ("bin_path", repo)); - s << target_link_libs_template.render(datum{"target_name", target_name(obj)} - ("dep_name", pops.get_mapping(dep)+"-"+dep.name())); + s << fmt::format(add_subdirectory2_template, + "../../dependencies/"+repo+"/build/apps/", + repo); + s << fmt::format(target_link_libs_template, + target_name(obj), + pops.get_mapping(dep)+"-"+dep.name()); } else { - s << target_link_libs_template.render(datum{"target_name", target_name(obj)} - ("dep_name", target_name(dep))); + s << fmt::format(target_link_libs_template, + target_name(obj), + target_name(dep)); } } s << "\n"; } template - inline void emit_entry(Stream& s) noexcept { s << entry_template.render(datum{"proj", proj->name()}); } + inline void emit_entry(Stream& s) noexcept { s << fmt::format(entry_template, proj->name()); } template inline void emit_object(Pops& pops, Stream& s, const object& obj) { - km::mustache& temp = std::is_same_v ? add_contract_template : add_library_template; + std::string_view temp = std::is_same_v ? add_contract_template : add_library_template; - s << temp.render(datum{"obj_name", obj.name()} - ("target_name", target_name(obj)) - ("source_ext", system::extension(obj.language())) - ("dollar_brace", "${")); // dollar_brace must be "${" to work around Mustache decoding the wrong pair in "${{{". + s << fmt::format(temp, + obj.name(), + target_name(obj), + system::extension(obj.language())); - s << target_include_template.render(datum{"target_name", target_name(obj)} - ("obj_name", obj.name())); + s << fmt::format(target_include_template, + target_name(obj), + obj.name()); for (const auto& o : obj.compile_options()) { - s << target_compile_template.render(datum{"target_name", target_name(obj)} - ("opt", o)); + s << fmt::format(target_compile_template, + target_name(obj), + o); } s << "\n"; for (const auto& o : obj.link_options()) { - s << target_link_libs_template.render(datum{"target_name", target_name(obj)} - ("dep_name", o)); + s << fmt::format(target_link_libs_template, + target_name(obj), + o); } s << "\n"; @@ -222,31 +227,6 @@ namespace antler::project { private: - // simple helper to clean km::data usage - struct datum { - template - decltype(auto) fwrd(T&& val) { - std::stringstream ss; - ss << std::forward(val); - return ss.str(); - } - - template - inline datum(Str&& key, T&& val) - : data(std::forward(key), fwrd(std::forward(val))) {} - - template - datum& operator()(Str&& key, T&& val) { - data.set(std::forward(key), fwrd(std::forward(val))); - return *this; - } - - operator km::data() const { return data; } - operator km::data&() { return data; } - - km::data data; - }; - const project* proj = nullptr; // non-owning pointer to project system::fs::path base_path; cmake_lists base_lists; diff --git a/include/antler/project/mustache.hpp b/include/antler/project/mustache.hpp deleted file mode 100644 index bd31c56b..00000000 --- a/include/antler/project/mustache.hpp +++ /dev/null @@ -1,42 +0,0 @@ -#pragma once - -/// @copyright See `LICENSE` in the root directory of this project. - -#include - -#include -#include - -// TODO this library is not very good -// I will create a better one that uses fmt in the near future -#include - -namespace antler::project { - using kainjow::mustache::mustache; - - template - struct mustache_data { - template - inline mustache_data(Str&& key, T&& val) - : internal_data(key, std::forward(val)) {} - - operator kainjow::mustache::data() const { return internal_data; } - operator kainjow::mustache::data()& { return internal_data; } - - kainjow::mustache::data internal_data; - }; - - template - struct mustache_list { - mustache_list(Ts&&... ts) - : internal_list(std::forward(ts)...) {} - - std::vector internal_list; - }; - - template - struct mustache_list { - mustache_list() - }; - -} // namespace antler::project \ No newline at end of file diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 751661c3..008377ce 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -4,22 +4,20 @@ pkg_check_modules(CURL libcurl REQUIRED) add_library(antler-project cmake_templates.cpp - dependency.cpp - location.cpp + dependency.cpp + location.cpp populator.cpp project.cpp - version_constraint.cpp + version_constraint.cpp ) -target_include_directories(antler-project PUBLIC ../include +target_include_directories(antler-project PUBLIC ../include ${CMAKE_CURRENT_BINARY_DIR}/../include - ${CMAKE_SOURCE_DIR}/external/Mustache ${CURL_INCLUDE_DIRS} ) -target_link_libraries(antler-project PUBLIC mustache - nlohmann_json::nlohmann_json +target_link_libraries(antler-project PUBLIC nlohmann_json::nlohmann_json yaml-cpp::yaml-cpp bluegrass::cturtle ${CURL_LIBRARIES} -lstdc++fs) -set_property(TARGET antler-project PROPERTY CXX_STANDARD 17) \ No newline at end of file +set_property(TARGET antler-project PROPERTY CXX_STANDARD 17) diff --git a/src/cmake_templates.cpp b/src/cmake_templates.cpp index 74fe09b5..fa1a463b 100644 --- a/src/cmake_templates.cpp +++ b/src/cmake_templates.cpp @@ -2,50 +2,58 @@ namespace antler::project { -km::mustache cmake::add_subdirectory_template = {"add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/{{path}})\n\n"}; -km::mustache cmake::add_subdirectory2_template = {"add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/{{src_path}}\n" - " ${CMAKE_CURRENT_BINARY_DIR}/{{bin_path}})\n\n"}; - -km::mustache cmake::preamble_template = {"# Generated with {{tool}}, modify at your own risk\n" - "cmake_minimum_required(VERSION {{major}}.{{minor}})\n" - "project(\"{{proj_name}}\" VERSION {{proj_major}}.{{proj_minor}}.{{proj_patch}})\n\n"}; - -km::mustache cmake::project_stub_template = {"find_package(cdt)\n\n" - "add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/../libs ${CMAKE_CURRENT_BINARY_DIR}/libs)\n" - "add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/../tests ${CMAKE_CURRENT_BINARY_DIR}/tests)\n\n"}; - -km::mustache cmake::target_compile_template = {"target_compile_options({{target_name}} PUBLIC {{opt}})\n\n"}; - -km::mustache cmake::target_include_template = {"target_include_directories({{target_name}}\n" - " PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/../../../include\n" - " ${CMAKE_CURRENT_SOURCE_DIR}/../../../include/{{obj_name}}\n" - " ./)\n\n"}; - -km::mustache cmake::target_link_libs_template = {"target_link_libraries({{target_name}} PUBLIC {{dep_name}})\n\n"}; - -km::mustache cmake::entry_template = {"include(ExternalProject)\n" - "if(CDT_ROOT STREQUAL \"\" OR NOT CDT_ROOT)\n" - " find_package(cdt)\n" - "endif()\n\n" - "ExternalProject_Add(\n" - " {{proj}}_project\n" - " SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/apps\n" - " BINARY_DIR ${CMAKE_CURRENT_BINARY_DIR}/{{proj}}\n" - " CMAKE_ARGS -DCMAKE_TOOLCHAIN_FILE=${CDT_ROOT}/lib/cmake/cdt/CDTWasmToolchain.cmake\n" - " UPDATE_COMMAND \"\"\n" - " PATCH_COMMAND \"\"\n" - " TEST_COMMAND \"\"\n" - " INSTALL_COMMAND \"\"\n" - " BUILD_ALWAYS 1\n" - ")\n\n"}; - -// Note the dollar_brace value in the following 2 templates. dollar_brace must be set to "${" as a workaround for Mustache decoding the -// wrong pair in "${{{", -km::mustache cmake::add_contract_template = { - "file(GLOB {{target_name}}-source ${CMAKE_CURRENT_SOURCE_DIR}/../../../apps/{{obj_name}}/*{{source_ext}})\n" - "add_contract({{obj_name}} {{target_name}} {{dollar_brace}}{{target_name}}-source})\n\n"}; -km::mustache cmake::add_library_template = { - "file(GLOB {{target_name}}-source ${CMAKE_CURRENT_SOURCE_DIR}/../../../libs/{{obj_name}}/*{{source_ext}})\n\n" - "add_library({{target_name}} {{dollar_brace}}{{target_name}}-source})\n\n"}; +// 0: path +std::string_view cmake::add_subdirectory_template = {"add_subdirectory(${{CMAKE_CURRENT_SOURCE_DIR}}/{0})\n\n"}; +// 0: src_path, 1: bin_path +std::string_view cmake::add_subdirectory2_template = {"add_subdirectory(${{CMAKE_CURRENT_SOURCE_DIR}}/{0}\n" + " ${{CMAKE_CURRENT_BINARY_DIR}}/{1})\n\n"}; + +// 0: tool, 1: major, 2: minor, 3: proj_name, 4: proj_major, 5: proj_minor, 6: proj_patch +std::string_view cmake::preamble_template = {"# Generated with {0}, modify at your own risk\n" + "cmake_minimum_required(VERSION {1}.{2})\n" + "project(\"{3}\" VERSION {4}.{5}.{6})\n\n"}; + +// NONE, but we still need to send it through format. +std::string_view cmake::project_stub_template = {"find_package(cdt)\n\n" + "add_subdirectory(${{CMAKE_CURRENT_SOURCE_DIR}}/../libs ${{CMAKE_CURRENT_BINARY_DIR}}/libs)\n" + "add_subdirectory(${{CMAKE_CURRENT_SOURCE_DIR}}/../tests ${{CMAKE_CURRENT_BINARY_DIR}}/tests)\n\n"}; + +// 0: target_name, 1: opt +std::string_view cmake::target_compile_template = {"target_compile_options({0} PUBLIC {1})\n\n"}; + +// 0: target name, 1: obj name +std::string_view cmake::target_include_template = {"target_include_directories({0}\n" + " PUBLIC ${{CMAKE_CURRENT_SOURCE_DIR}}/../../../include\n" + " ${{CMAKE_CURRENT_SOURCE_DIR}}/../../../include/{1}\n" + " ./)\n\n"}; + +// 0: target name, 1: link libs +std::string_view cmake::target_link_libs_template = {"target_link_libraries({0} PUBLIC {1})\n\n"}; + +//0: proj +std::string_view cmake::entry_template = {"include(ExternalProject)\n" + "if(CDT_ROOT STREQUAL \"\" OR NOT CDT_ROOT)\n" + " find_package(cdt)\n" + "endif()\n\n" + "ExternalProject_Add(\n" + " {0}_project\n" + " SOURCE_DIR ${{CMAKE_CURRENT_SOURCE_DIR}}/apps\n" + " BINARY_DIR ${{CMAKE_CURRENT_BINARY_DIR}}/{0}\n" + " CMAKE_ARGS -DCMAKE_TOOLCHAIN_FILE=${{CDT_ROOT}}/lib/cmake/cdt/CDTWasmToolchain.cmake\n" + " UPDATE_COMMAND \"\"\n" + " PATCH_COMMAND \"\"\n" + " TEST_COMMAND \"\"\n" + " INSTALL_COMMAND \"\"\n" + " BUILD_ALWAYS 1\n" + ")\n\n"}; + +// 0: object name, 1: target name, 2: source extension +std::string_view cmake::add_contract_template = {"file(GLOB {1}-source ${{CMAKE_CURRENT_SOURCE_DIR}}/../../../apps/{0}/*{2})\n" + "add_contract({0} {1} ${{{1}-source}})\n\n"}; + +// 0: object name, 1: target name, 2: source extension +std::string_view cmake::add_library_template = {"file(GLOB {1}-source ${{CMAKE_CURRENT_SOURCE_DIR}}/../../../libs/{0}/*{2})\n\n" + "add_library({0} ${{{1}-source}})\n\n"}; + } // namespace antler::project