From 91c4062aa13a956d8775fdf2b6660425e852729b Mon Sep 17 00:00:00 2001 From: David Sankel Date: Fri, 3 Jan 2025 16:14:18 -0500 Subject: [PATCH] cargo-generate project template POC This is a proof of concept that turns exemplar into a template that generates skeleton Beman projects using the [cargo-generate tool](https://github.com/cargo-generate/cargo-generate). See https://github.com/camio/foobar.git for an example skeleton called "foobar" generated from this template. Use of this template requires a cargo-generate installation: 1. [Install Rust](https://www.rust-lang.org/tools/install) (a cargo-generate prerequisite) 2. `cargo install cargo-generate` (detailed instructions [here](https://cargo-generate.github.io/cargo-generate/installation.html)) Once `cargo-generate` is installed, run ``` cargo generate gh:camio/example --branch cargo-generate ``` and follow the prompts to generate a template. --- .genignore | 1 + CMakeLists.txt | 16 ++++++------ README.md | 2 +- examples/CMakeLists.txt | 6 ++--- examples/identity_as_default_projection.cpp | 6 ++--- examples/identity_direct_usage.cpp | 4 +-- .../identity.hpp | 10 ++++---- src/beman/exemplar/CMakeLists.txt | 25 ------------------- src/beman/{{project-name}}/CMakeLists.txt | 25 +++++++++++++++++++ .../identity.cpp | 2 +- tests/beman/exemplar/CMakeLists.txt | 12 --------- tests/beman/{{project-name}}/CMakeLists.txt | 12 +++++++++ .../identity.test.cpp | 4 +-- 13 files changed, 63 insertions(+), 62 deletions(-) create mode 100644 .genignore rename include/beman/{exemplar => {{project-name}}}/identity.hpp (75%) delete mode 100644 src/beman/exemplar/CMakeLists.txt create mode 100644 src/beman/{{project-name}}/CMakeLists.txt rename src/beman/{exemplar => {{project-name}}}/identity.cpp (56%) delete mode 100644 tests/beman/exemplar/CMakeLists.txt create mode 100644 tests/beman/{{project-name}}/CMakeLists.txt rename tests/beman/{exemplar => {{project-name}}}/identity.test.cpp (93%) diff --git a/.genignore b/.genignore new file mode 100644 index 0000000..211e957 --- /dev/null +++ b/.genignore @@ -0,0 +1 @@ +.github/workflows/pre-commit.yml \ No newline at end of file diff --git a/CMakeLists.txt b/CMakeLists.txt index 45a240a..51540d8 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -3,7 +3,7 @@ cmake_minimum_required(VERSION 3.25) project( - beman.exemplar # CMake Project Name, which is also the name of the top-level + beman.{{project-name}} # CMake Project Name, which is also the name of the top-level # targets (e.g., library, executable, etc.). DESCRIPTION "A Beman library exemplar" LANGUAGES CXX @@ -11,14 +11,14 @@ project( # [CMAKE.SKIP_TESTS] option( - BEMAN_EXEMPLAR_BUILD_TESTS + BEMAN_{{project-name | shouty_snake_case}}_BUILD_TESTS "Enable building tests and test infrastructure. Default: ON. Values: { ON, OFF }." ${PROJECT_IS_TOP_LEVEL} ) # [CMAKE.SKIP_EXAMPLES] option( - BEMAN_EXEMPLAR_BUILD_EXAMPLES + BEMAN_{{project-name | shouty_snake_case}}_BUILD_EXAMPLES "Enable building examples. Default: ON. Values: { ON, OFF }." ${PROJECT_IS_TOP_LEVEL} ) @@ -26,7 +26,7 @@ option( include(FetchContent) include(GNUInstallDirs) -if(BEMAN_EXEMPLAR_BUILD_TESTS) +if(BEMAN_{{project-name | shouty_snake_case}}_BUILD_TESTS) enable_testing() # Fetch GoogleTest @@ -43,12 +43,12 @@ if(BEMAN_EXEMPLAR_BUILD_TESTS) endblock() endif() -add_subdirectory(src/beman/exemplar) +add_subdirectory(src/beman/{{project-name}}) -if(BEMAN_EXEMPLAR_BUILD_TESTS) - add_subdirectory(tests/beman/exemplar) +if(BEMAN_{{project-name | shouty_snake_case}}_BUILD_TESTS) + add_subdirectory(tests/beman/{{project-name}}) endif() -if(BEMAN_EXEMPLAR_BUILD_EXAMPLES) +if(BEMAN_{{project-name | shouty_snake_case}}_BUILD_EXAMPLES) add_subdirectory(examples) endif() diff --git a/README.md b/README.md index 1d13969..d7d4d12 100644 --- a/README.md +++ b/README.md @@ -2,7 +2,7 @@ SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception --> -# beman.exemplar: A Beman Library Exemplar +# beman.{{project-name}}: A Beman Library ![Continuous Integration Tests](https://github.com/bemanproject/exemplar/actions/workflows/ci_tests.yml/badge.svg) diff --git a/examples/CMakeLists.txt b/examples/CMakeLists.txt index ba8168b..0f232db 100644 --- a/examples/CMakeLists.txt +++ b/examples/CMakeLists.txt @@ -18,7 +18,7 @@ endif() message("Examples to be built: ${ALL_EXAMPLES}") foreach(example ${ALL_EXAMPLES}) - add_executable(beman.exemplar.examples.${example}) - target_sources(beman.exemplar.examples.${example} PRIVATE ${example}.cpp) - target_link_libraries(beman.exemplar.examples.${example} beman::exemplar) + add_executable(beman.{{project-name}}.examples.${example}) + target_sources(beman.{{project-name}}.examples.${example} PRIVATE ${example}.cpp) + target_link_libraries(beman.{{project-name}}.examples.${example} beman::{{project-name}}) endforeach() diff --git a/examples/identity_as_default_projection.cpp b/examples/identity_as_default_projection.cpp index eecabac..82a2d4c 100644 --- a/examples/identity_as_default_projection.cpp +++ b/examples/identity_as_default_projection.cpp @@ -1,11 +1,11 @@ // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception -// This example demonstrates the usage of beman::exemplar::identity as a default projection in a range-printer. +// This example demonstrates the usage of beman::{{project-name}}::identity as a default projection in a range-printer. // Requires: range support (C++20) and std::identity support (C++20). // TODO Darius: Do we need to selectively compile this example? // Or should we assume that this project is compiled with C++20 support only? -#include +#include #include #include // std::identity @@ -13,7 +13,7 @@ #include #include -namespace exe = beman::exemplar; +namespace exe = beman::{{project-name}}; // Class with a pair of values. struct Pair { diff --git a/examples/identity_direct_usage.cpp b/examples/identity_direct_usage.cpp index 236a63d..b79dd24 100644 --- a/examples/identity_direct_usage.cpp +++ b/examples/identity_direct_usage.cpp @@ -1,10 +1,10 @@ // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception -#include +#include #include -namespace exe = beman::exemplar; +namespace exe = beman::{{project-name}}; int main() { std::cout << exe::identity()(2024) << '\n'; diff --git a/include/beman/exemplar/identity.hpp b/include/beman/{{project-name}}/identity.hpp similarity index 75% rename from include/beman/exemplar/identity.hpp rename to include/beman/{{project-name}}/identity.hpp index 9463125..2301522 100644 --- a/include/beman/exemplar/identity.hpp +++ b/include/beman/{{project-name}}/identity.hpp @@ -1,7 +1,7 @@ // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception -#ifndef BEMAN_EXEMPLAR_IDENTITY_HPP -#define BEMAN_EXEMPLAR_IDENTITY_HPP +#ifndef BEMAN_{{project-name | shouty_snake_case}}_IDENTITY_HPP +#define BEMAN_{{project-name | shouty_snake_case}}_IDENTITY_HPP // C++ Standard Library: std::identity equivalent. // See https://eel.is/c++draft/func.identity: @@ -22,7 +22,7 @@ #include // std::forward -namespace beman::exemplar { +namespace beman::{{project-name}} { struct __is_transparent; // not defined @@ -37,6 +37,6 @@ struct identity { using is_transparent = __is_transparent; }; -} // namespace beman::exemplar +} // namespace beman::{{project-name}} -#endif // BEMAN_EXEMPLAR_IDENTITY_HPP +#endif // BEMAN_{{project-name | shouty_snake_case}}_IDENTITY_HPP diff --git a/src/beman/exemplar/CMakeLists.txt b/src/beman/exemplar/CMakeLists.txt deleted file mode 100644 index edee25c..0000000 --- a/src/beman/exemplar/CMakeLists.txt +++ /dev/null @@ -1,25 +0,0 @@ -# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception - -add_library(beman.exemplar STATIC) -add_library(beman::exemplar ALIAS beman.exemplar) - -target_sources(beman.exemplar PRIVATE identity.cpp) - -target_sources( - beman.exemplar - PUBLIC - FILE_SET HEADERS - BASE_DIRS ${PROJECT_SOURCE_DIR}/include - FILES ${PROJECT_SOURCE_DIR}/include/beman/exemplar/identity.hpp -) - -set_target_properties(beman.exemplar PROPERTIES VERIFY_INTERFACE_HEADER_SETS ON) - -install( - TARGETS beman.exemplar - EXPORT beman.exemplar - DESTINATION - $<$:debug/>${CMAKE_INSTALL_LIBDIR} - RUNTIME DESTINATION $<$:debug/>${CMAKE_INSTALL_BINDIR} - FILE_SET HEADERS DESTINATION ${CMAKE_INSTALL_INCLUDEDIR} -) diff --git a/src/beman/{{project-name}}/CMakeLists.txt b/src/beman/{{project-name}}/CMakeLists.txt new file mode 100644 index 0000000..36ee786 --- /dev/null +++ b/src/beman/{{project-name}}/CMakeLists.txt @@ -0,0 +1,25 @@ +# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception + +add_library(beman.{{project-name}} STATIC) +add_library(beman::{{project-name}} ALIAS beman.{{project-name}}) + +target_sources(beman.{{project-name}} PRIVATE identity.cpp) + +target_sources( + beman.{{project-name}} + PUBLIC + FILE_SET HEADERS + BASE_DIRS ${PROJECT_SOURCE_DIR}/include + FILES ${PROJECT_SOURCE_DIR}/include/beman/{{project-name}}/identity.hpp +) + +set_target_properties(beman.{{project-name}} PROPERTIES VERIFY_INTERFACE_HEADER_SETS ON) + +install( + TARGETS beman.{{project-name}} + EXPORT beman.{{project-name}} + DESTINATION + $<$:debug/>${CMAKE_INSTALL_LIBDIR} + RUNTIME DESTINATION $<$:debug/>${CMAKE_INSTALL_BINDIR} + FILE_SET HEADERS DESTINATION ${CMAKE_INSTALL_INCLUDEDIR} +) diff --git a/src/beman/exemplar/identity.cpp b/src/beman/{{project-name}}/identity.cpp similarity index 56% rename from src/beman/exemplar/identity.cpp rename to src/beman/{{project-name}}/identity.cpp index 21ef59d..14a40f1 100644 --- a/src/beman/exemplar/identity.cpp +++ b/src/beman/{{project-name}}/identity.cpp @@ -1,3 +1,3 @@ // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception -#include +#include diff --git a/tests/beman/exemplar/CMakeLists.txt b/tests/beman/exemplar/CMakeLists.txt deleted file mode 100644 index 9e0e075..0000000 --- a/tests/beman/exemplar/CMakeLists.txt +++ /dev/null @@ -1,12 +0,0 @@ -# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception - -include(GoogleTest) - -add_executable(beman.exemplar.tests.identity) -target_sources(beman.exemplar.tests.identity PRIVATE identity.test.cpp) -target_link_libraries( - beman.exemplar.tests.identity - PRIVATE beman::exemplar GTest::gtest GTest::gtest_main -) - -gtest_add_tests(beman.exemplar.tests.identity "" AUTO) diff --git a/tests/beman/{{project-name}}/CMakeLists.txt b/tests/beman/{{project-name}}/CMakeLists.txt new file mode 100644 index 0000000..9a554fa --- /dev/null +++ b/tests/beman/{{project-name}}/CMakeLists.txt @@ -0,0 +1,12 @@ +# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception + +include(GoogleTest) + +add_executable(beman.{{project-name}}.tests.identity) +target_sources(beman.{{project-name}}.tests.identity PRIVATE identity.test.cpp) +target_link_libraries( + beman.{{project-name}}.tests.identity + PRIVATE beman::{{project-name}} GTest::gtest GTest::gtest_main +) + +gtest_add_tests(beman.{{project-name}}.tests.identity "" AUTO) diff --git a/tests/beman/exemplar/identity.test.cpp b/tests/beman/{{project-name}}/identity.test.cpp similarity index 93% rename from tests/beman/exemplar/identity.test.cpp rename to tests/beman/{{project-name}}/identity.test.cpp index 7f68fdf..96ae717 100644 --- a/tests/beman/exemplar/identity.test.cpp +++ b/tests/beman/{{project-name}}/identity.test.cpp @@ -1,13 +1,13 @@ // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception -#include +#include #include #include #include -namespace exe = beman::exemplar; +namespace exe = beman::{{project-name}}; TEST(IdentityTest, call_identity_with_int) { for (int i = -100; i < 100; ++i) {