From a4a86d897efb8aec548bd5431c1b791f4c349fe5 Mon Sep 17 00:00:00 2001 From: friendlyanon Date: Sun, 20 Feb 2022 02:47:19 +0100 Subject: [PATCH] Implement a very simple template engine This template engine renders files in a single pass and allows for nested control flow in the templates. It's similar to Jinja2, except extremely barebones and there is no pretty error reporting for mistakes in the templates. The only error that is reported early is when blocks weren't terminated properly. This check is done using a depth counter, so don't expect too much. --- cmake-init/cmake_init.py | 42 +++++------- cmake-init/template.py | 67 +++++++++++++++++++ .../templates/c/common/example/CMakeLists.txt | 8 +-- .../templates/c/executable/CMakeLists.txt | 30 ++++----- .../templates/c/executable/source/lib.c | 2 +- .../c/executable/test/CMakeLists.txt | 10 +-- .../c/executable/test/source/__name___test.c | 2 +- cmake-init/templates/c/header/CMakeLists.txt | 30 ++++----- .../c/header/include/__name__/__name__.h | 4 +- .../c/header/test/source/__name___test.c | 4 +- cmake-init/templates/c/shared/CMakeLists.txt | 44 ++++++------ .../c/shared/include/__name__/__name__.h | 4 +- .../templates/c/shared/source/__name__.c | 4 +- .../c/shared/test/source/__name___test.c | 4 +- cmake-init/templates/common/.clang-format | 20 +++--- cmake-init/templates/common/.codespellrc | 6 +- .../templates/common/.github/workflows/ci.yml | 10 +-- cmake-init/templates/common/BUILDING.md | 16 ++--- cmake-init/templates/common/CMakePresets.json | 20 +++--- .../templates/common/CMakeUserPresets.json | 8 +-- cmake-init/templates/common/HACKING.md | 4 +- cmake-init/templates/common/README.md | 4 +- .../templates/common/cmake/dev-mode.cmake | 8 +-- .../common/cmake/install-config.cmake | 2 +- .../common/cmake/install-rules.cmake | 58 ++++++++-------- .../templates/common/cmake/lint-targets.cmake | 10 +-- cmake-init/templates/common/cmake/lint.cmake | 10 +-- .../cmake/open-cpp-coverage.cmake.example | 4 +- .../templates/common/cmake/variables.cmake | 24 +++---- cmake-init/templates/common/docs/Doxyfile.in | 2 +- cmake-init/templates/common/docs/conf.py.in | 4 +- .../templates/common/example/CMakeLists.txt | 8 +-- .../templates/common/test/CMakeLists.txt | 20 +++--- .../templates/executable/CMakeLists.txt | 30 ++++----- .../templates/executable/source/lib.cpp | 2 +- .../templates/executable/test/CMakeLists.txt | 10 +-- .../executable/test/source/__name___test.cpp | 2 +- cmake-init/templates/header/CMakeLists.txt | 30 ++++----- .../header/include/__name__/__name__.hpp | 2 +- .../header/test/source/__name___test.cpp | 4 +- cmake-init/templates/shared/CMakeLists.txt | 44 ++++++------ .../shared/include/__name__/__name__.hpp | 6 +- .../templates/shared/source/__name__.cpp | 4 +- .../shared/test/source/__name___test.cpp | 4 +- cmake-init/templates/vcpkg/portfile.cmake | 8 +-- cmake-init/templates/vcpkg/vcpkg.json | 6 +- 46 files changed, 352 insertions(+), 293 deletions(-) create mode 100644 cmake-init/template.py diff --git a/cmake-init/cmake_init.py b/cmake-init/cmake_init.py index 3e08175..6457833 100644 --- a/cmake-init/cmake_init.py +++ b/cmake-init/cmake_init.py @@ -31,6 +31,8 @@ import sys import zipfile +from template import compile_template + __version__ = "0.25.0" is_windows = os.name == "nt" @@ -162,7 +164,6 @@ def ask(*args, **kwargs): "os": "win64" if is_windows else "unix", "c": cli_args.c, "cpp": not cli_args.c, - "suppress": False, "c_header": False, "include_source": False, "has_source": True, @@ -179,13 +180,14 @@ def ask(*args, **kwargs): ) d[key] = value d["examples"] = value - if d["type_id"] == "s" and d["cpp"]: - d["suppress"] = True if d["type_id"] == "e": d["include_source"] = True if d["type_id"] == "h": d["has_source"] = False d["c_header"] = d["c"] and d["type_id"] == "h" + d["exe"] = d["type_id"] == "e" + d["lib"] = d["type_id"] == "s" + d["header"] = d["type_id"] == "h" return d @@ -194,28 +196,16 @@ def mkdir(path): def write_file(path, d, overwrite, zip_path): - if not overwrite and os.path.exists(path): - return - - def replacer(match): - query = match.group(1) - expected = True - if query.endswith("_not"): - query = query[:-4] - expected = False - if query == "type": - mapping = {"exe": "e", "header": "h", "shared": "s"} - actual = mapping[match.group(2)] == d["type_id"] - if actual == expected: - return match.group(3) - elif query == "if" and d[match.group(2)] == expected: - return match.group(3) - return "" - - regex = re.compile("{((?:type|if)(?:_not)?) ([^}]+)}(.+?){end}", re.DOTALL) - contents = regex.sub(replacer, zip_path.read_text(encoding="UTF-8")) - with open(path, "w", encoding="UTF-8", newline="\n") as f: - f.write(contents % d) + if overwrite or not os.path.exists(path): + renderer = compile_template(zip_path.read_text(encoding="UTF-8"), d) + # noinspection PyBroadException + try: + contents = renderer() + except Exception: + print(f"Error while rendering {path}", file=sys.stderr) + raise + with open(path, "w", encoding="UTF-8", newline="\n") as f: + f.write(contents) def should_write_examples(d, at): @@ -354,6 +344,8 @@ def vcpkg(d, zip): print(f"""'{d["name"]}' already exists""", file=sys.stderr) exit(1) mkdir(path) + d["lib"] = d["type_id"] == "s" + d["header"] = d["type_id"] == "h" write_dir(path, d, False, zipfile.Path(zip, "templates/vcpkg/")) vcpkg_root = r"%VCPKG_ROOT:\=/%" if is_windows else "$VCPKG_ROOT" pwd = r"%cd:\=/%" if is_windows else "$PWD" diff --git a/cmake-init/template.py b/cmake-init/template.py new file mode 100644 index 0000000..9e63718 --- /dev/null +++ b/cmake-init/template.py @@ -0,0 +1,67 @@ +# cmake-init - The missing CMake project initializer +# Copyright (C) 2021 friendlyanon +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . + +# Website: https://github.com/friendlyanon/cmake-init + +import re + +__all__ = ["compile_template"] + +block_regex = re.compile( + r"(.*?)({% .+? %}|{= .+? =})|(.+?)\Z", + re.MULTILINE | re.DOTALL +) + + +def compile_template(template_source, globals): + depth = 0 + python_source = ["def f():\n _result = []"] + + def add_line(line): + python_source.append(" " * (depth + 1) + line) + + def add_repr(o): + add_line("_result.append(str(" + repr(o) + "))") + + for match in block_regex.finditer(template_source): + before, block, tail = match.groups() + if not block: + add_repr(tail) + continue + if block == "end": + depth -= 1 + continue + if before: + add_repr(before) + inner = block[3:-3] + if block[1:2] == "=": + add_repr(globals[inner]) + continue + if inner == "end": + depth -= 1 + continue + if inner == "else" or inner.startswith("elif "): + depth -= 1 + add_line(inner + ":") + depth += 1 + + if depth != 0: + raise SyntaxError("Block not properly terminated") + + add_line("return \"\".join(_result)") + locals = {} + exec("\n".join(python_source), globals, locals) + return locals["f"] diff --git a/cmake-init/templates/c/common/example/CMakeLists.txt b/cmake-init/templates/c/common/example/CMakeLists.txt index ea769c2..05bf865 100644 --- a/cmake-init/templates/c/common/example/CMakeLists.txt +++ b/cmake-init/templates/c/common/example/CMakeLists.txt @@ -1,20 +1,20 @@ cmake_minimum_required(VERSION 3.14) -project(%(name)sExamples C) +project({= name =}Examples C) include(../cmake/project-is-top-level.cmake) include(../cmake/folders.cmake) if(PROJECT_IS_TOP_LEVEL) - find_package(%(name)s REQUIRED) + find_package({= name =} REQUIRED) endif() add_custom_target(run-examples) function(add_example NAME) add_executable("${NAME}" "${NAME}.c") - target_link_libraries("${NAME}" PRIVATE %(name)s::%(name)s) - target_compile_features("${NAME}" PRIVATE c_std_%(std)s) + target_link_libraries("${NAME}" PRIVATE {= name =}::{= name =}) + target_compile_features("${NAME}" PRIVATE c_std_{= std =}) add_custom_target("run_${NAME}" COMMAND "${NAME}" VERBATIM) add_dependencies("run_${NAME}" "${NAME}") add_dependencies(run-examples "run_${NAME}") diff --git a/cmake-init/templates/c/executable/CMakeLists.txt b/cmake-init/templates/c/executable/CMakeLists.txt index 5ac643c..b641748 100644 --- a/cmake-init/templates/c/executable/CMakeLists.txt +++ b/cmake-init/templates/c/executable/CMakeLists.txt @@ -3,10 +3,10 @@ cmake_minimum_required(VERSION 3.14) include(cmake/prelude.cmake) project( - %(name)s - VERSION %(version)s - DESCRIPTION "%(description)s" - HOMEPAGE_URL "%(homepage)s" + {= name =} + VERSION {= version =} + DESCRIPTION "{= description =}" + HOMEPAGE_URL "{= homepage =}" LANGUAGES C ) @@ -16,32 +16,32 @@ include(cmake/variables.cmake) # ---- Declare library ---- add_library( - %(name)s_lib OBJECT + {= name =}_lib OBJECT source/lib.c ) target_include_directories( - %(name)s_lib ${warning_guard} + {= name =}_lib ${warning_guard} PUBLIC "$" ) -target_compile_features(%(name)s_lib PUBLIC c_std_%(std)s) +target_compile_features({= name =}_lib PUBLIC c_std_{= std =}) # ---- Declare executable ---- -add_executable(%(name)s_exe source/main.c) -add_executable(%(name)s::exe ALIAS %(name)s_exe) +add_executable({= name =}_exe source/main.c) +add_executable({= name =}::exe ALIAS {= name =}_exe) set_target_properties( - %(name)s_exe PROPERTIES - OUTPUT_NAME %(name)s + {= name =}_exe PROPERTIES + OUTPUT_NAME {= name =} EXPORT_NAME exe ) -target_compile_features(%(name)s_exe PRIVATE c_std_%(std)s) +target_compile_features({= name =}_exe PRIVATE c_std_{= std =}) -target_link_libraries(%(name)s_exe PRIVATE %(name)s_lib) +target_link_libraries({= name =}_exe PRIVATE {= name =}_lib) # ---- Install rules ---- @@ -51,12 +51,12 @@ endif() # ---- Developer mode ---- -if(NOT %(name)s_DEVELOPER_MODE) +if(NOT {= name =}_DEVELOPER_MODE) return() elseif(NOT PROJECT_IS_TOP_LEVEL) message( AUTHOR_WARNING - "Developer mode is intended for developers of %(name)s" + "Developer mode is intended for developers of {= name =}" ) endif() diff --git a/cmake-init/templates/c/executable/source/lib.c b/cmake-init/templates/c/executable/source/lib.c index 1e17f4d..e0294b7 100644 --- a/cmake-init/templates/c/executable/source/lib.c +++ b/cmake-init/templates/c/executable/source/lib.c @@ -3,6 +3,6 @@ library create_library() { library lib; - lib.name = "%(name)s"; + lib.name = "{= name =}"; return lib; } diff --git a/cmake-init/templates/c/executable/test/CMakeLists.txt b/cmake-init/templates/c/executable/test/CMakeLists.txt index 4c577b8..edd611d 100644 --- a/cmake-init/templates/c/executable/test/CMakeLists.txt +++ b/cmake-init/templates/c/executable/test/CMakeLists.txt @@ -2,12 +2,12 @@ # depends on being added from it, i.e. the testing is done only from the build # tree and is not feasible from an install location -project(%(name)sTests LANGUAGES C) +project({= name =}Tests LANGUAGES C) -add_executable(%(name)s_test source/%(name)s_test.c) -target_link_libraries(%(name)s_test PRIVATE %(name)s_lib) -target_compile_features(%(name)s_test PRIVATE c_std_%(std)s) +add_executable({= name =}_test source/{= name =}_test.c) +target_link_libraries({= name =}_test PRIVATE {= name =}_lib) +target_compile_features({= name =}_test PRIVATE c_std_{= std =}) -add_test(NAME %(name)s_test COMMAND %(name)s_test) +add_test(NAME {= name =}_test COMMAND {= name =}_test) add_folders(Test) diff --git a/cmake-init/templates/c/executable/test/source/__name___test.c b/cmake-init/templates/c/executable/test/source/__name___test.c index 694ebf2..1f3f3e6 100644 --- a/cmake-init/templates/c/executable/test/source/__name___test.c +++ b/cmake-init/templates/c/executable/test/source/__name___test.c @@ -9,5 +9,5 @@ int main(int argc, const char* argv[]) library lib = create_library(); - return strcmp(lib.name, "%(name)s") == 0 ? 0 : 1; + return strcmp(lib.name, "{= name =}") == 0 ? 0 : 1; } diff --git a/cmake-init/templates/c/header/CMakeLists.txt b/cmake-init/templates/c/header/CMakeLists.txt index 93358ff..6dd3b08 100644 --- a/cmake-init/templates/c/header/CMakeLists.txt +++ b/cmake-init/templates/c/header/CMakeLists.txt @@ -3,10 +3,10 @@ cmake_minimum_required(VERSION 3.14) include(cmake/prelude.cmake) project( - %(name)s - VERSION %(version)s - DESCRIPTION "%(description)s" - HOMEPAGE_URL "%(homepage)s" + {= name =} + VERSION {= version =} + DESCRIPTION "{= description =}" + HOMEPAGE_URL "{= homepage =}" LANGUAGES NONE ) @@ -15,45 +15,45 @@ include(cmake/variables.cmake) # ---- Declare library ---- -add_library(%(name)s_%(name)s INTERFACE) -add_library(%(name)s::%(name)s ALIAS %(name)s_%(name)s) +add_library({= name =}_{= name =} INTERFACE) +add_library({= name =}::{= name =} ALIAS {= name =}_{= name =}) set_property( - TARGET %(name)s_%(name)s PROPERTY - EXPORT_NAME %(name)s + TARGET {= name =}_{= name =} PROPERTY + EXPORT_NAME {= name =} ) target_include_directories( - %(name)s_%(name)s ${warning_guard} + {= name =}_{= name =} ${warning_guard} INTERFACE "$" ) -target_compile_features(%(name)s_%(name)s INTERFACE c_std_%(std)s) +target_compile_features({= name =}_{= name =} INTERFACE c_std_{= std =}) # ---- Install rules ---- if(NOT CMAKE_SKIP_INSTALL_RULES) include(cmake/install-rules.cmake) -endif(){if c_examples} +endif(){% if c_examples %} # ---- Examples ---- if(PROJECT_IS_TOP_LEVEL) - option(BUILD_EXAMPLES "Build examples tree." "${%(name)s_DEVELOPER_MODE}") + option(BUILD_EXAMPLES "Build examples tree." "${{= name =}_DEVELOPER_MODE}") if(BUILD_EXAMPLES) add_subdirectory(example) endif() -endif(){end} +endif(){% end %} # ---- Developer mode ---- -if(NOT %(name)s_DEVELOPER_MODE) +if(NOT {= name =}_DEVELOPER_MODE) return() elseif(NOT PROJECT_IS_TOP_LEVEL) message( AUTHOR_WARNING - "Developer mode is intended for developers of %(name)s" + "Developer mode is intended for developers of {= name =}" ) endif() diff --git a/cmake-init/templates/c/header/include/__name__/__name__.h b/cmake-init/templates/c/header/include/__name__/__name__.h index 86249a6..96a9a5b 100644 --- a/cmake-init/templates/c/header/include/__name__/__name__.h +++ b/cmake-init/templates/c/header/include/__name__/__name__.h @@ -9,10 +9,10 @@ extern "C" { */ const char* header_only_name(void); -#ifdef %(uc_name)s_IMPLEMENTATION +#ifdef {= uc_name =}_IMPLEMENTATION const char* header_only_name() { - return "%(name)s"; + return "{= name =}"; } #endif diff --git a/cmake-init/templates/c/header/test/source/__name___test.c b/cmake-init/templates/c/header/test/source/__name___test.c index 2d84ff1..a1959c5 100644 --- a/cmake-init/templates/c/header/test/source/__name___test.c +++ b/cmake-init/templates/c/header/test/source/__name___test.c @@ -1,4 +1,4 @@ -#include "%(name)s/%(name)s.h" +#include "{= name =}/{= name =}.h" #include @@ -7,5 +7,5 @@ int main(int argc, const char* argv[]) (void)argc; (void)argv; - return strcmp("%(name)s", header_only_name()) == 0 ? 0 : 1; + return strcmp("{= name =}", header_only_name()) == 0 ? 0 : 1; } diff --git a/cmake-init/templates/c/shared/CMakeLists.txt b/cmake-init/templates/c/shared/CMakeLists.txt index 3481d28..0514f77 100644 --- a/cmake-init/templates/c/shared/CMakeLists.txt +++ b/cmake-init/templates/c/shared/CMakeLists.txt @@ -3,10 +3,10 @@ cmake_minimum_required(VERSION 3.14) include(cmake/prelude.cmake) project( - %(name)s - VERSION %(version)s - DESCRIPTION "%(description)s" - HOMEPAGE_URL "%(homepage)s" + {= name =} + VERSION {= version =} + DESCRIPTION "{= description =}" + HOMEPAGE_URL "{= homepage =}" LANGUAGES C ) @@ -16,68 +16,68 @@ include(cmake/variables.cmake) # ---- Declare library ---- add_library( - %(name)s_%(name)s - source/%(name)s.c + {= name =}_{= name =} + source/{= name =}.c ) -add_library(%(name)s::%(name)s ALIAS %(name)s_%(name)s) +add_library({= name =}::{= name =} ALIAS {= name =}_{= name =}) include(GenerateExportHeader) generate_export_header( - %(name)s_%(name)s - BASE_NAME %(name)s - EXPORT_FILE_NAME export/%(name)s/%(name)s_export.h + {= name =}_{= name =} + BASE_NAME {= name =} + EXPORT_FILE_NAME export/{= name =}/{= name =}_export.h ) if(NOT BUILD_SHARED_LIBS) - target_compile_definitions(%(name)s_%(name)s PUBLIC %(uc_name)s_STATIC_DEFINE) + target_compile_definitions({= name =}_{= name =} PUBLIC {= uc_name =}_STATIC_DEFINE) endif() set_target_properties( - %(name)s_%(name)s PROPERTIES + {= name =}_{= name =} PROPERTIES C_VISIBILITY_PRESET hidden VERSION "${PROJECT_VERSION}" SOVERSION "${PROJECT_VERSION_MAJOR}" - EXPORT_NAME %(name)s - OUTPUT_NAME %(name)s + EXPORT_NAME {= name =} + OUTPUT_NAME {= name =} ) target_include_directories( - %(name)s_%(name)s ${warning_guard} + {= name =}_{= name =} ${warning_guard} PUBLIC "$" ) target_include_directories( - %(name)s_%(name)s SYSTEM + {= name =}_{= name =} SYSTEM PUBLIC "$" ) -target_compile_features(%(name)s_%(name)s PUBLIC c_std_%(std)s) +target_compile_features({= name =}_{= name =} PUBLIC c_std_{= std =}) # ---- Install rules ---- if(NOT CMAKE_SKIP_INSTALL_RULES) include(cmake/install-rules.cmake) -endif(){if c_examples} +endif(){% if c_examples %} # ---- Examples ---- if(PROJECT_IS_TOP_LEVEL) - option(BUILD_EXAMPLES "Build examples tree." "${%(name)s_DEVELOPER_MODE}") + option(BUILD_EXAMPLES "Build examples tree." "${{= name =}_DEVELOPER_MODE}") if(BUILD_EXAMPLES) add_subdirectory(example) endif() -endif(){end} +endif(){% end %} # ---- Developer mode ---- -if(NOT %(name)s_DEVELOPER_MODE) +if(NOT {= name =}_DEVELOPER_MODE) return() elseif(NOT PROJECT_IS_TOP_LEVEL) message( AUTHOR_WARNING - "Developer mode is intended for developers of %(name)s" + "Developer mode is intended for developers of {= name =}" ) endif() diff --git a/cmake-init/templates/c/shared/include/__name__/__name__.h b/cmake-init/templates/c/shared/include/__name__/__name__.h index 6b36fe0..88acd7d 100644 --- a/cmake-init/templates/c/shared/include/__name__/__name__.h +++ b/cmake-init/templates/c/shared/include/__name__/__name__.h @@ -1,6 +1,6 @@ #pragma once -#include "%(name)s/%(name)s_export.h" +#include "{= name =}/{= name =}_export.h" #ifdef __cplusplus extern "C" { @@ -9,7 +9,7 @@ extern "C" { /** * @brief Reports the name of the library */ -%(uc_name)s_EXPORT const char* exported_function(void); +{= uc_name =}_EXPORT const char* exported_function(void); #ifdef __cplusplus } // extern "C" diff --git a/cmake-init/templates/c/shared/source/__name__.c b/cmake-init/templates/c/shared/source/__name__.c index d042f20..5e83073 100644 --- a/cmake-init/templates/c/shared/source/__name__.c +++ b/cmake-init/templates/c/shared/source/__name__.c @@ -1,6 +1,6 @@ -#include "%(name)s/%(name)s.h" +#include "{= name =}/{= name =}.h" const char* exported_function() { - return "%(name)s"; + return "{= name =}"; } diff --git a/cmake-init/templates/c/shared/test/source/__name___test.c b/cmake-init/templates/c/shared/test/source/__name___test.c index abcec8f..6914c9b 100644 --- a/cmake-init/templates/c/shared/test/source/__name___test.c +++ b/cmake-init/templates/c/shared/test/source/__name___test.c @@ -1,4 +1,4 @@ -#include "%(name)s/%(name)s.h" +#include "{= name =}/{= name =}.h" #include @@ -7,5 +7,5 @@ int main(int argc, const char* argv[]) (void)argc; (void)argv; - return strcmp("%(name)s", exported_function()) == 0 ? 0 : 1; + return strcmp("{= name =}", exported_function()) == 0 ? 0 : 1; } diff --git a/cmake-init/templates/common/.clang-format b/cmake-init/templates/common/.clang-format index 02e3400..576c0ae 100644 --- a/cmake-init/templates/common/.clang-format +++ b/cmake-init/templates/common/.clang-format @@ -30,13 +30,13 @@ BraceWrapping: AfterCaseLabel: false AfterClass: true AfterControlStatement: MultiLine - AfterEnum: {if cpp}true{end}{if c}false{end} + AfterEnum: {% if cpp %}true{% else %}false{% end %} AfterFunction: true AfterNamespace: true AfterObjCDeclaration: false - AfterStruct: {if cpp}true{end}{if c}false{end} - AfterUnion: {if cpp}true{end}{if c}false{end} - AfterExternBlock: {if cpp}true{end}{if c}false{end} + AfterStruct: {% if cpp %}true{% else %}false{% end %} + AfterUnion: {% if cpp %}true{% else %}false{% end %} + AfterExternBlock: {% if cpp %}true{% else %}false{% end %} BeforeCatch: false BeforeElse: false BeforeLambdaBody: true @@ -71,16 +71,16 @@ ForEachMacros: - Q_FOREACH - BOOST_FOREACH IncludeBlocks: Regroup -IncludeCategories:{if cpp} +IncludeCategories:{% if cpp %} # Standard library headers come before anything else - Regex: '^<[a-z_]+>' - Priority: -1{end} - - Regex: '^<.+\.h{if cpp}(pp)?{end}>' - Priority: 1{if cpp} + Priority: -1{% end %} + - Regex: '^<.+\.h{% if cpp %}(pp)?{% end %}>' + Priority: 1{% if cpp %} - Regex: '^<.*' - Priority: 2{end} + Priority: 2{% end %} - Regex: '.*' - Priority: {if cpp}3{end}{if c}2{end} + Priority: {% if cpp %}3{% else %}2{% end %} IncludeIsMainRegex: '' IncludeIsMainSourceRegex: '' IndentCaseLabels: true diff --git a/cmake-init/templates/common/.codespellrc b/cmake-init/templates/common/.codespellrc index 52f299f..d431eb4 100644 --- a/cmake-init/templates/common/.codespellrc +++ b/cmake-init/templates/common/.codespellrc @@ -2,6 +2,6 @@ builtin = clear,rare,en-GB_to_en-US,names,informal,code check-filenames = check-hidden = -skip = */.git,*/build{if c},*/.codespellrc{end} -quiet-level = 2{if c} -ignore-regex = ^#include <(?:stdio)\.h>${end} +skip = */.git,*/build{% if c %},*/.codespellrc{% end %} +quiet-level = 2{% if c %} +ignore-regex = ^#include <(?:stdio)\.h>${% end %} diff --git a/cmake-init/templates/common/.github/workflows/ci.yml b/cmake-init/templates/common/.github/workflows/ci.yml index 053917c..3d7afec 100644 --- a/cmake-init/templates/common/.github/workflows/ci.yml +++ b/cmake-init/templates/common/.github/workflows/ci.yml @@ -75,7 +75,7 @@ jobs: - uses: actions/checkout@v2 - name: Configure - env: { C{if c}C{end}{if cpp}XX{end}: clang{if cpp}++{end}-11 } + env: { C{% if c %}C{% else %}XX{% end %}: clang{% if cpp %}++{% end %}-11 } run: cmake --preset=ci-sanitize - name: Build @@ -97,13 +97,13 @@ jobs: strategy: matrix: - os: [macos-latest, ubuntu-latest, windows-2022]{type shared} + os: [macos-latest, ubuntu-latest, windows-2022]{% if lib %} type: [shared, static] include: - { type: shared, shared: YES } - - { type: static, shared: NO }{end} + - { type: static, shared: NO }{% end %} runs-on: ${{ matrix.os }} @@ -116,8 +116,8 @@ jobs: - name: Configure shell: pwsh - run: cmake "--preset=ci-$("${{ matrix.os }}".split("-")[0])"{type shared} - -D BUILD_SHARED_LIBS=${{ matrix.shared }}{end} + run: cmake "--preset=ci-$("${{ matrix.os }}".split("-")[0])"{% if lib %} + -D BUILD_SHARED_LIBS=${{ matrix.shared }}{% end %} - name: Build run: cmake --build build --config Release -j 2 diff --git a/cmake-init/templates/common/BUILDING.md b/cmake-init/templates/common/BUILDING.md index c6411ab..41c5b1d 100644 --- a/cmake-init/templates/common/BUILDING.md +++ b/cmake-init/templates/common/BUILDING.md @@ -42,26 +42,26 @@ multi-configuration generator, like the Visual Studio ones: ```sh cmake --install build --config Release -```{type_not exe} +```{% if not exe %} ### CMake package This project exports a CMake package to be used with the [`find_package`][2] command of CMake: -* Package name: `%(name)s` -* Target name: `%(name)s::%(name)s +* Package name: `{= name =}` +* Target name: `{= name =}::{= name =} Example usage: ```cmake -find_package(%(name)s REQUIRED) +find_package({= name =} REQUIRED) # Declare the imported target as a build requirement using PRIVATE target_link_libraries( project_target PRIVATE - %(name)s::%(name)s + {= name =}::{= name =} ) -```{end} +```{% end %} -[1]: https://cmake.org/cmake/help/latest/manual/cmake.1.html#install-a-project{type_not exe} -[2]: https://cmake.org/cmake/help/latest/command/find_package.html{end} +[1]: https://cmake.org/cmake/help/latest/manual/cmake.1.html#install-a-project{% if not exe %} +[2]: https://cmake.org/cmake/help/latest/command/find_package.html{% end %} diff --git a/cmake-init/templates/common/CMakePresets.json b/cmake-init/templates/common/CMakePresets.json index 6e82328..51d35ee 100644 --- a/cmake-init/templates/common/CMakePresets.json +++ b/cmake-init/templates/common/CMakePresets.json @@ -26,21 +26,21 @@ "hidden": true, "inherits": "cmake-pedantic", "cacheVariables": { - "%(name)s_DEVELOPER_MODE": "ON" + "{= name =}_DEVELOPER_MODE": "ON" } }, { "name": "cppcheck", "hidden": true, "cacheVariables": { - "CMAKE_C{if cpp}XX{end}_CPPCHECK": "cppcheck;--inline-suppr" + "CMAKE_C{% if cpp %}XX{% end %}_CPPCHECK": "cppcheck;--inline-suppr" } }, { "name": "clang-tidy", "hidden": true, "cacheVariables": { - "CMAKE_C{if cpp}XX{end}_CLANG_TIDY": "clang-tidy;--header-filter=${sourceDir}/*" + "CMAKE_C{% if cpp %}XX{% end %}_CLANG_TIDY": "clang-tidy;--header-filter=${sourceDir}/*" } }, { @@ -48,23 +48,23 @@ "description": "This preset makes sure the project actually builds with at least the specified standard", "hidden": true, "cacheVariables": { - "CMAKE_C{if cpp}XX{end}_EXTENSIONS": "OFF", - "CMAKE_C{if cpp}XX{end}_STANDARD": "%(std)s", - "CMAKE_C{if cpp}XX{end}_STANDARD_REQUIRED": "ON" + "CMAKE_C{% if cpp %}XX{% end %}_EXTENSIONS": "OFF", + "CMAKE_C{% if cpp %}XX{% end %}_STANDARD": "{= std =}", + "CMAKE_C{% if cpp %}XX{% end %}_STANDARD_REQUIRED": "ON" } }, { "name": "flags-unix", "hidden": true, "cacheVariables": { - "CMAKE_C{if cpp}XX{end}_FLAGS": "-Wall -Wextra -Wpedantic -Wconversion -Wsign-conversion -Wcast-qual -Wshadow -Wformat=2 -Wundef -Werror=float-equal{if c} -Werror=strict-prototypes -Wwrite-strings{end}" + "CMAKE_C{% if cpp %}XX{% end %}_FLAGS": "-Wall -Wextra -Wpedantic -Wconversion -Wsign-conversion -Wcast-qual -Wshadow -Wformat=2 -Wundef -Werror=float-equal{% if c %} -Werror=strict-prototypes -Wwrite-strings{% end %}" } }, { "name": "flags-windows", "hidden": true, "cacheVariables": { - "CMAKE_C{if cpp}XX{end}_FLAGS": "/W4 /permissive- /utf-8 /volatile:iso{if cpp} /EHsc /Zc:__cplusplus /Zc:throwingNew{end}" + "CMAKE_C{% if cpp %}XX{% end %}_FLAGS": "/W4 /permissive- /utf-8 /volatile:iso{% if cpp %} /EHsc /Zc:__cplusplus /Zc:throwingNew{% end %}" } }, { @@ -91,7 +91,7 @@ "cacheVariables": { "ENABLE_COVERAGE": "ON", "CMAKE_BUILD_TYPE": "Coverage", - "CMAKE_C{if cpp}XX{end}_FLAGS_COVERAGE": "-Og -g --coverage -fkeep-inline-functions -fkeep-static-functions", + "CMAKE_C{% if cpp %}XX{% end %}_FLAGS_COVERAGE": "-Og -g --coverage -fkeep-inline-functions -fkeep-static-functions", "CMAKE_EXE_LINKER_FLAGS_COVERAGE": "--coverage", "CMAKE_SHARED_LINKER_FLAGS_COVERAGE": "--coverage" } @@ -109,7 +109,7 @@ "inherits": ["ci-unix", "dev-mode"], "cacheVariables": { "CMAKE_BUILD_TYPE": "Sanitize", - "CMAKE_C{if cpp}XX{end}_FLAGS_SANITIZE": "-O2 -g -fsanitize=address,undefined -fno-omit-frame-pointer -fno-common" + "CMAKE_C{% if cpp %}XX{% end %}_FLAGS_SANITIZE": "-O2 -g -fsanitize=address,undefined -fno-omit-frame-pointer -fno-common" } }, { diff --git a/cmake-init/templates/common/CMakeUserPresets.json b/cmake-init/templates/common/CMakeUserPresets.json index f3d27cb..d86941c 100644 --- a/cmake-init/templates/common/CMakeUserPresets.json +++ b/cmake-init/templates/common/CMakeUserPresets.json @@ -9,7 +9,7 @@ { "name": "dev-common", "hidden": true, - "inherits": ["dev-mode"{if use_clang_tidy}, "clang-tidy"{end}{if use_cppcheck}, "cppcheck"{end}], + "inherits": ["dev-mode"{% if use_clang_tidy %}, "clang-tidy"{% end %}{% if use_cppcheck %}, "cppcheck"{% end %}], "cacheVariables": { "BUILD_MCSS_DOCS": "ON" } @@ -30,7 +30,7 @@ { "name": "dev", "binaryDir": "${sourceDir}/build/dev", - "inherits": "dev-%(os)s" + "inherits": "dev-{= os =}" }, { "name": "dev-coverage", @@ -43,7 +43,7 @@ "name": "dev", "configurePreset": "dev", "configuration": "Debug", - "jobs": %(cpus)s + "jobs": {= cpus =} } ], "testPresets": [ @@ -55,7 +55,7 @@ "outputOnFailure": true }, "execution": { - "jobs": %(cpus)s + "jobs": {= cpus =} } } ] diff --git a/cmake-init/templates/common/HACKING.md b/cmake-init/templates/common/HACKING.md index 342a9b6..d113dfd 100644 --- a/cmake-init/templates/common/HACKING.md +++ b/cmake-init/templates/common/HACKING.md @@ -9,7 +9,7 @@ guide. ## Developer mode Build system targets that are only useful for developers of this project are -hidden if the `%(name)s_DEVELOPER_MODE` option is disabled. Enabling this +hidden if the `{= name =}_DEVELOPER_MODE` option is disabled. Enabling this option makes tests and other developer targets and options available. Not enabling this option means that you are a consumer of this project and thus you have no need for these targets and options. @@ -23,7 +23,7 @@ the project. As a developer, you are recommended to always have the [latest CMake version][2] installed to make use of the latest Quality-of-Life additions. -You have a few options to pass `%(name)s_DEVELOPER_MODE` to the configure +You have a few options to pass `{= name =}_DEVELOPER_MODE` to the configure command, but this project prefers to use presets. As a developer, you should create a `CMakeUserPresets.json` file at the root of diff --git a/cmake-init/templates/common/README.md b/cmake-init/templates/common/README.md index ddd3dc4..c813c0b 100644 --- a/cmake-init/templates/common/README.md +++ b/cmake-init/templates/common/README.md @@ -1,6 +1,6 @@ -# %(name)s +# {= name =} -This is the %(name)s project. +This is the {= name =} project. # Building and installing diff --git a/cmake-init/templates/common/cmake/dev-mode.cmake b/cmake-init/templates/common/cmake/dev-mode.cmake index 3927ea8..b2faedc 100644 --- a/cmake-init/templates/common/cmake/dev-mode.cmake +++ b/cmake-init/templates/common/cmake/dev-mode.cmake @@ -4,14 +4,14 @@ include(CTest) if(BUILD_TESTING) add_subdirectory(test) endif() -{type exe} +{% if exe %} add_custom_target( run-exe - COMMAND %(name)s_exe + COMMAND {= name =}_exe VERBATIM ) -add_dependencies(run-exe %(name)s_exe) -{end} +add_dependencies(run-exe {= name =}_exe) +{% end %} option(BUILD_MCSS_DOCS "Build documentation using Doxygen and m.css" OFF) if(BUILD_MCSS_DOCS) include(cmake/docs.cmake) diff --git a/cmake-init/templates/common/cmake/install-config.cmake b/cmake-init/templates/common/cmake/install-config.cmake index cf4663d..9e1186c 100644 --- a/cmake-init/templates/common/cmake/install-config.cmake +++ b/cmake-init/templates/common/cmake/install-config.cmake @@ -1 +1 @@ -include("${CMAKE_CURRENT_LIST_DIR}/%(name)sTargets.cmake") +include("${CMAKE_CURRENT_LIST_DIR}/{= name =}Targets.cmake") diff --git a/cmake-init/templates/common/cmake/install-rules.cmake b/cmake-init/templates/common/cmake/install-rules.cmake index ce007be..b98c2ad 100644 --- a/cmake-init/templates/common/cmake/install-rules.cmake +++ b/cmake-init/templates/common/cmake/install-rules.cmake @@ -1,77 +1,77 @@ if(PROJECT_IS_TOP_LEVEL) - set(CMAKE_INSTALL_INCLUDEDIR include/%(name)s CACHE PATH "") -endif(){type header} + set(CMAKE_INSTALL_INCLUDEDIR include/{= name =} CACHE PATH "") +endif(){% if header %} # Project is configured with no languages, so tell GNUInstallDirs the lib dir -set(CMAKE_INSTALL_LIBDIR lib CACHE PATH ""){end} +set(CMAKE_INSTALL_LIBDIR lib CACHE PATH ""){% end %} include(CMakePackageConfigHelpers) include(GNUInstallDirs) # find_package() call for consumers to find this project -set(package %(name)s){type shared} +set(package {= name =}){% if lib %} install( DIRECTORY include/ "${PROJECT_BINARY_DIR}/export/" DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}" - COMPONENT %(name)s_Development -){end}{type header} + COMPONENT {= name =}_Development +){% end %}{% if header %} install( DIRECTORY include/ DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}" - COMPONENT %(name)s_Development -){end} + COMPONENT {= name =}_Development +){% end %} install( - TARGETS %(name)s_{type_not exe}%(name)s{end}{type exe}exe{end} - EXPORT %(name)sTargets{type exe} - RUNTIME COMPONENT %(name)s_Runtime{end}{type shared} + TARGETS {= name =}_{% if not exe %}{= name =}{% else %}exe{% end %} + EXPORT {= name =}Targets{% if exe %} + RUNTIME COMPONENT {= name =}_Runtime{% end %}{% if lib %} RUNTIME # - COMPONENT %(name)s_Runtime + COMPONENT {= name =}_Runtime LIBRARY # - COMPONENT %(name)s_Runtime - NAMELINK_COMPONENT %(name)s_Development + COMPONENT {= name =}_Runtime + NAMELINK_COMPONENT {= name =}_Development ARCHIVE # - COMPONENT %(name)s_Development + COMPONENT {= name =}_Development INCLUDES # - DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}"{end}{type header} - INCLUDES DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}"{end} + DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}"{% end %}{% if header %} + INCLUDES DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}"{% end %} ) write_basic_package_version_file( "${package}ConfigVersion.cmake" - COMPATIBILITY SameMajorVersion{type header} - ARCH_INDEPENDENT{end} + COMPATIBILITY SameMajorVersion{% if header %} + ARCH_INDEPENDENT{% end %} ) # Allow package maintainers to freely override the path for the configs set( - %(name)s_INSTALL_CMAKEDIR "${CMAKE_INSTALL_DATADIR}/${package}" + {= name =}_INSTALL_CMAKEDIR "${CMAKE_INSTALL_DATADIR}/${package}" CACHE PATH "CMake package config location relative to the install prefix" ) -mark_as_advanced(%(name)s_INSTALL_CMAKEDIR) +mark_as_advanced({= name =}_INSTALL_CMAKEDIR) install( FILES cmake/install-config.cmake - DESTINATION "${%(name)s_INSTALL_CMAKEDIR}" + DESTINATION "${{= name =}_INSTALL_CMAKEDIR}" RENAME "${package}Config.cmake" - COMPONENT %(name)s_Development + COMPONENT {= name =}_Development ) install( FILES "${PROJECT_BINARY_DIR}/${package}ConfigVersion.cmake" - DESTINATION "${%(name)s_INSTALL_CMAKEDIR}" - COMPONENT %(name)s_Development + DESTINATION "${{= name =}_INSTALL_CMAKEDIR}" + COMPONENT {= name =}_Development ) install( - EXPORT %(name)sTargets - NAMESPACE %(name)s:: - DESTINATION "${%(name)s_INSTALL_CMAKEDIR}" - COMPONENT %(name)s_Development + EXPORT {= name =}Targets + NAMESPACE {= name =}:: + DESTINATION "${{= name =}_INSTALL_CMAKEDIR}" + COMPONENT {= name =}_Development ) if(PROJECT_IS_TOP_LEVEL) diff --git a/cmake-init/templates/common/cmake/lint-targets.cmake b/cmake-init/templates/common/cmake/lint-targets.cmake index cc7640d..8f2ce5c 100644 --- a/cmake-init/templates/common/cmake/lint-targets.cmake +++ b/cmake-init/templates/common/cmake/lint-targets.cmake @@ -1,10 +1,10 @@ set( FORMAT_PATTERNS - source/*.c{if cpp}pp{end} source/*.h{if cpp}pp{end} - include/*.h{if cpp}pp{end} - test/*.c{if cpp}pp{end} test/*.h{if cpp}pp{end}{if cpp_examples} - example/*.cpp example/*.hpp{end}{if c_examples} - example/*.c example/*.h{end} + source/*.c{% if cpp %}pp{% end %} source/*.h{% if cpp %}pp{% end %} + include/*.h{% if cpp %}pp{% end %} + test/*.c{% if cpp %}pp{% end %} test/*.h{% if cpp %}pp{% end %}{% if cpp_examples %} + example/*.cpp example/*.hpp{% end %}{% if c_examples %} + example/*.c example/*.h{% end %} CACHE STRING "; separated patterns relative to the project source dir to format" ) diff --git a/cmake-init/templates/common/cmake/lint.cmake b/cmake-init/templates/common/cmake/lint.cmake index 492dd03..b943760 100644 --- a/cmake-init/templates/common/cmake/lint.cmake +++ b/cmake-init/templates/common/cmake/lint.cmake @@ -9,11 +9,11 @@ endmacro() default(FORMAT_COMMAND clang-format) default( PATTERNS - source/*.c{if cpp}pp{end} source/*.h{if cpp}pp{end} - include/*.h{if cpp}pp{end} - test/*.c{if cpp}pp{end} test/*.h{if cpp}pp{end}{if cpp_examples} - example/*.cpp example/*.hpp{end}{if c_examples} - example/*.c example/*.h{end} + source/*.c{% if cpp %}pp{% end %} source/*.h{% if cpp %}pp{% end %} + include/*.h{% if cpp %}pp{% end %} + test/*.c{% if cpp %}pp{% end %} test/*.h{% if cpp %}pp{% end %}{% if cpp_examples %} + example/*.cpp example/*.hpp{% end %}{% if c_examples %} + example/*.c example/*.h{% end %} ) default(FIX NO) diff --git a/cmake-init/templates/common/cmake/open-cpp-coverage.cmake.example b/cmake-init/templates/common/cmake/open-cpp-coverage.cmake.example index 601f07c..878e35b 100644 --- a/cmake-init/templates/common/cmake/open-cpp-coverage.cmake.example +++ b/cmake-init/templates/common/cmake/open-cpp-coverage.cmake.example @@ -19,8 +19,8 @@ add_custom_target( --modules "${binary_dir}\\test" # This command is for the developer, so export as html instead of cobertura --export_type "html:${binary_dir}\\opencppcoverage" - # Source (not header) file locations{if has_source} - --sources "${source_dir}\\source"{end} + # Source (not header) file locations{% if has_source %} + --sources "${source_dir}\\source"{% end %} --sources "${source_dir}\\test\\source" # Working directory for CTest, which should be the binary directory --working_dir "${binary_dir}" diff --git a/cmake-init/templates/common/cmake/variables.cmake b/cmake-init/templates/common/cmake/variables.cmake index 8231acf..104533c 100644 --- a/cmake-init/templates/common/cmake/variables.cmake +++ b/cmake-init/templates/common/cmake/variables.cmake @@ -1,27 +1,27 @@ # ---- Developer mode ---- # Developer mode enables targets and code paths in the CMake scripts that are -# only relevant for the developer(s) of %(name)s +# only relevant for the developer(s) of {= name =} # Targets necessary to build the project must be provided unconditionally, so # consumers can trivially build and package the project if(PROJECT_IS_TOP_LEVEL) - option(%(name)s_DEVELOPER_MODE "Enable developer mode" OFF){type shared} - option(BUILD_SHARED_LIBS "Build shared libs." OFF){end} + option({= name =}_DEVELOPER_MODE "Enable developer mode" OFF){% if lib %} + option(BUILD_SHARED_LIBS "Build shared libs." OFF){% end %} endif() -{if suppress}# ---- Suppress C4251 on Windows ---- +{% if cpp and lib %}# ---- Suppress C4251 on Windows ---- -# Please see include/%(name)s/%(name)s.hpp for more details +# Please see include/{= name =}/{= name =}.hpp for more details set(pragma_suppress_c4251 " /* This needs to suppress only for MSVC */ #if defined(_MSC_VER) && !defined(__ICL) -# define %(uc_name)s_SUPPRESS_C4251 _Pragma(\"warning(suppress:4251)\") +# define {= uc_name =}_SUPPRESS_C4251 _Pragma(\"warning(suppress:4251)\") #else -# define %(uc_name)s_SUPPRESS_C4251 +# define {= uc_name =}_SUPPRESS_C4251 #endif ") -{end}# ---- Warning guard ---- +{% end %}# ---- Warning guard ---- # target_include_directories with the SYSTEM modifier will request the compiler # to omit warnings from the provided paths, if the compiler supports that @@ -30,12 +30,12 @@ set(pragma_suppress_c4251 " set(warning_guard "") if(NOT PROJECT_IS_TOP_LEVEL) option( - %(name)s_INCLUDES_WITH_SYSTEM - "Use SYSTEM modifier for %(name)s's includes, disabling warnings" + {= name =}_INCLUDES_WITH_SYSTEM + "Use SYSTEM modifier for {= name =}'s includes, disabling warnings" ON ) - mark_as_advanced(%(name)s_INCLUDES_WITH_SYSTEM) - if(%(name)s_INCLUDES_WITH_SYSTEM) + mark_as_advanced({= name =}_INCLUDES_WITH_SYSTEM) + if({= name =}_INCLUDES_WITH_SYSTEM) set(warning_guard SYSTEM) endif() endif() diff --git a/cmake-init/templates/common/docs/Doxyfile.in b/cmake-init/templates/common/docs/Doxyfile.in index 4ce634d..bd68c05 100644 --- a/cmake-init/templates/common/docs/Doxyfile.in +++ b/cmake-init/templates/common/docs/Doxyfile.in @@ -7,7 +7,7 @@ PROJECT_NAME = "@PROJECT_NAME@" PROJECT_NUMBER = "@PROJECT_VERSION@" # Add sources -INPUT = "@PROJECT_SOURCE_DIR@/README.md" "@PROJECT_SOURCE_DIR@/include"{if include_source} "@PROJECT_SOURCE_DIR@/source"{end} "@PROJECT_SOURCE_DIR@/docs/pages" +INPUT = "@PROJECT_SOURCE_DIR@/README.md" "@PROJECT_SOURCE_DIR@/include"{% if include_source %} "@PROJECT_SOURCE_DIR@/source"{% end %} "@PROJECT_SOURCE_DIR@/docs/pages" EXTRACT_ALL = YES RECURSIVE = YES OUTPUT_DIRECTORY = "@DOXYGEN_OUTPUT_DIRECTORY@" diff --git a/cmake-init/templates/common/docs/conf.py.in b/cmake-init/templates/common/docs/conf.py.in index 4976c26..0e5bf94 100644 --- a/cmake-init/templates/common/docs/conf.py.in +++ b/cmake-init/templates/common/docs/conf.py.in @@ -1,6 +1,6 @@ DOXYFILE = 'Doxyfile' LINKS_NAVBAR1 = [ - (None, 'pages', [(None, 'about')]),{if cpp} - (None, 'namespaces', []),{end} + (None, 'pages', [(None, 'about')]),{% if cpp %} + (None, 'namespaces', []),{% end %} ] diff --git a/cmake-init/templates/common/example/CMakeLists.txt b/cmake-init/templates/common/example/CMakeLists.txt index c525d4d..534dc90 100644 --- a/cmake-init/templates/common/example/CMakeLists.txt +++ b/cmake-init/templates/common/example/CMakeLists.txt @@ -1,20 +1,20 @@ cmake_minimum_required(VERSION 3.14) -project(%(name)sExamples CXX) +project({= name =}Examples CXX) include(../cmake/project-is-top-level.cmake) include(../cmake/folders.cmake) if(PROJECT_IS_TOP_LEVEL) - find_package(%(name)s REQUIRED) + find_package({= name =} REQUIRED) endif() add_custom_target(run-examples) function(add_example NAME) add_executable("${NAME}" "${NAME}.cpp") - target_link_libraries("${NAME}" PRIVATE %(name)s::%(name)s) - target_compile_features("${NAME}" PRIVATE cxx_std_%(std)s) + target_link_libraries("${NAME}" PRIVATE {= name =}::{= name =}) + target_compile_features("${NAME}" PRIVATE cxx_std_{= std =}) add_custom_target("run_${NAME}" COMMAND "${NAME}" VERBATIM) add_dependencies("run_${NAME}" "${NAME}") add_dependencies(run-examples "run_${NAME}") diff --git a/cmake-init/templates/common/test/CMakeLists.txt b/cmake-init/templates/common/test/CMakeLists.txt index 3413a66..83f1a37 100644 --- a/cmake-init/templates/common/test/CMakeLists.txt +++ b/cmake-init/templates/common/test/CMakeLists.txt @@ -1,25 +1,25 @@ cmake_minimum_required(VERSION 3.14) -project(%(name)sTests LANGUAGES C{if cpp}XX{end}) +project({= name =}Tests LANGUAGES C{% if cpp %}XX{% end %}) include(../cmake/project-is-top-level.cmake) include(../cmake/folders.cmake) include(../cmake/windows-set-path.cmake) if(PROJECT_IS_TOP_LEVEL) - find_package(%(name)s REQUIRED) + find_package({= name =} REQUIRED) enable_testing() endif() -add_executable(%(name)s_test source/%(name)s_test.c{if cpp}pp{end}) -target_link_libraries(%(name)s_test PRIVATE %(name)s::%(name)s) -target_compile_features(%(name)s_test PRIVATE c{if cpp}xx{end}_std_%(std)s){if c_header} +add_executable({= name =}_test source/{= name =}_test.c{% if cpp %}pp{% end %}) +target_link_libraries({= name =}_test PRIVATE {= name =}::{= name =}) +target_compile_features({= name =}_test PRIVATE c{% if cpp %}xx{% end %}_std_{= std =}){% if c_header %} set_property( - SOURCE source/%(name)s_test.c PROPERTY - COMPILE_DEFINITIONS %(uc_name)s_IMPLEMENTATION -){end} + SOURCE source/{= name =}_test.c PROPERTY + COMPILE_DEFINITIONS {= uc_name =}_IMPLEMENTATION +){% end %} -add_test(NAME %(name)s_test COMMAND %(name)s_test){type shared} -windows_set_path(%(name)s_test %(name)s::%(name)s){end} +add_test(NAME {= name =}_test COMMAND {= name =}_test){% if lib %} +windows_set_path({= name =}_test {= name =}::{= name =}){% end %} add_folders(Test) diff --git a/cmake-init/templates/executable/CMakeLists.txt b/cmake-init/templates/executable/CMakeLists.txt index f39e1da..2d0945c 100644 --- a/cmake-init/templates/executable/CMakeLists.txt +++ b/cmake-init/templates/executable/CMakeLists.txt @@ -3,10 +3,10 @@ cmake_minimum_required(VERSION 3.14) include(cmake/prelude.cmake) project( - %(name)s - VERSION %(version)s - DESCRIPTION "%(description)s" - HOMEPAGE_URL "%(homepage)s" + {= name =} + VERSION {= version =} + DESCRIPTION "{= description =}" + HOMEPAGE_URL "{= homepage =}" LANGUAGES CXX ) @@ -16,32 +16,32 @@ include(cmake/variables.cmake) # ---- Declare library ---- add_library( - %(name)s_lib OBJECT + {= name =}_lib OBJECT source/lib.cpp ) target_include_directories( - %(name)s_lib ${warning_guard} + {= name =}_lib ${warning_guard} PUBLIC "$" ) -target_compile_features(%(name)s_lib PUBLIC cxx_std_%(std)s) +target_compile_features({= name =}_lib PUBLIC cxx_std_{= std =}) # ---- Declare executable ---- -add_executable(%(name)s_exe source/main.cpp) -add_executable(%(name)s::exe ALIAS %(name)s_exe) +add_executable({= name =}_exe source/main.cpp) +add_executable({= name =}::exe ALIAS {= name =}_exe) set_target_properties( - %(name)s_exe PROPERTIES - OUTPUT_NAME %(name)s + {= name =}_exe PROPERTIES + OUTPUT_NAME {= name =} EXPORT_NAME exe ) -target_compile_features(%(name)s_exe PRIVATE cxx_std_%(std)s) +target_compile_features({= name =}_exe PRIVATE cxx_std_{= std =}) -target_link_libraries(%(name)s_exe PRIVATE %(name)s_lib) +target_link_libraries({= name =}_exe PRIVATE {= name =}_lib) # ---- Install rules ---- @@ -51,12 +51,12 @@ endif() # ---- Developer mode ---- -if(NOT %(name)s_DEVELOPER_MODE) +if(NOT {= name =}_DEVELOPER_MODE) return() elseif(NOT PROJECT_IS_TOP_LEVEL) message( AUTHOR_WARNING - "Developer mode is intended for developers of %(name)s" + "Developer mode is intended for developers of {= name =}" ) endif() diff --git a/cmake-init/templates/executable/source/lib.cpp b/cmake-init/templates/executable/source/lib.cpp index bc03c32..ba604e4 100644 --- a/cmake-init/templates/executable/source/lib.cpp +++ b/cmake-init/templates/executable/source/lib.cpp @@ -1,6 +1,6 @@ #include "lib.hpp" library::library() - : name("%(name)s") + : name("{= name =}") { } diff --git a/cmake-init/templates/executable/test/CMakeLists.txt b/cmake-init/templates/executable/test/CMakeLists.txt index 01da3b8..ba3622d 100644 --- a/cmake-init/templates/executable/test/CMakeLists.txt +++ b/cmake-init/templates/executable/test/CMakeLists.txt @@ -2,12 +2,12 @@ # depends on being added from it, i.e. the testing is done only from the build # tree and is not feasible from an install location -project(%(name)sTests LANGUAGES CXX) +project({= name =}Tests LANGUAGES CXX) -add_executable(%(name)s_test source/%(name)s_test.cpp) -target_link_libraries(%(name)s_test PRIVATE %(name)s_lib) -target_compile_features(%(name)s_test PRIVATE cxx_std_%(std)s) +add_executable({= name =}_test source/{= name =}_test.cpp) +target_link_libraries({= name =}_test PRIVATE {= name =}_lib) +target_compile_features({= name =}_test PRIVATE cxx_std_{= std =}) -add_test(NAME %(name)s_test COMMAND %(name)s_test) +add_test(NAME {= name =}_test COMMAND {= name =}_test) add_folders(Test) diff --git a/cmake-init/templates/executable/test/source/__name___test.cpp b/cmake-init/templates/executable/test/source/__name___test.cpp index 0526bc9..7c5e901 100644 --- a/cmake-init/templates/executable/test/source/__name___test.cpp +++ b/cmake-init/templates/executable/test/source/__name___test.cpp @@ -4,5 +4,5 @@ auto main() -> int { library lib; - return lib.name == "%(name)s" ? 0 : 1; + return lib.name == "{= name =}" ? 0 : 1; } diff --git a/cmake-init/templates/header/CMakeLists.txt b/cmake-init/templates/header/CMakeLists.txt index 8ed8df5..f496d41 100644 --- a/cmake-init/templates/header/CMakeLists.txt +++ b/cmake-init/templates/header/CMakeLists.txt @@ -3,10 +3,10 @@ cmake_minimum_required(VERSION 3.14) include(cmake/prelude.cmake) project( - %(name)s - VERSION %(version)s - DESCRIPTION "%(description)s" - HOMEPAGE_URL "%(homepage)s" + {= name =} + VERSION {= version =} + DESCRIPTION "{= description =}" + HOMEPAGE_URL "{= homepage =}" LANGUAGES NONE ) @@ -15,45 +15,45 @@ include(cmake/variables.cmake) # ---- Declare library ---- -add_library(%(name)s_%(name)s INTERFACE) -add_library(%(name)s::%(name)s ALIAS %(name)s_%(name)s) +add_library({= name =}_{= name =} INTERFACE) +add_library({= name =}::{= name =} ALIAS {= name =}_{= name =}) set_property( - TARGET %(name)s_%(name)s PROPERTY - EXPORT_NAME %(name)s + TARGET {= name =}_{= name =} PROPERTY + EXPORT_NAME {= name =} ) target_include_directories( - %(name)s_%(name)s ${warning_guard} + {= name =}_{= name =} ${warning_guard} INTERFACE "$" ) -target_compile_features(%(name)s_%(name)s INTERFACE cxx_std_%(std)s) +target_compile_features({= name =}_{= name =} INTERFACE cxx_std_{= std =}) # ---- Install rules ---- if(NOT CMAKE_SKIP_INSTALL_RULES) include(cmake/install-rules.cmake) -endif(){if cpp_examples} +endif(){% if cpp_examples %} # ---- Examples ---- if(PROJECT_IS_TOP_LEVEL) - option(BUILD_EXAMPLES "Build examples tree." "${%(name)s_DEVELOPER_MODE}") + option(BUILD_EXAMPLES "Build examples tree." "${{= name =}_DEVELOPER_MODE}") if(BUILD_EXAMPLES) add_subdirectory(example) endif() -endif(){end} +endif(){% end %} # ---- Developer mode ---- -if(NOT %(name)s_DEVELOPER_MODE) +if(NOT {= name =}_DEVELOPER_MODE) return() elseif(NOT PROJECT_IS_TOP_LEVEL) message( AUTHOR_WARNING - "Developer mode is intended for developers of %(name)s" + "Developer mode is intended for developers of {= name =}" ) endif() diff --git a/cmake-init/templates/header/include/__name__/__name__.hpp b/cmake-init/templates/header/include/__name__/__name__.hpp index c47c78f..3af4bec 100644 --- a/cmake-init/templates/header/include/__name__/__name__.hpp +++ b/cmake-init/templates/header/include/__name__/__name__.hpp @@ -7,5 +7,5 @@ */ inline auto name() -> std::string { - return "%(name)s"; + return "{= name =}"; } diff --git a/cmake-init/templates/header/test/source/__name___test.cpp b/cmake-init/templates/header/test/source/__name___test.cpp index b628a12..13482db 100644 --- a/cmake-init/templates/header/test/source/__name___test.cpp +++ b/cmake-init/templates/header/test/source/__name___test.cpp @@ -1,7 +1,7 @@ -#include "%(name)s/%(name)s.hpp" +#include "{= name =}/{= name =}.hpp" auto main() -> int { auto result = name(); - return result == "%(name)s" ? 0 : 1; + return result == "{= name =}" ? 0 : 1; } diff --git a/cmake-init/templates/shared/CMakeLists.txt b/cmake-init/templates/shared/CMakeLists.txt index 8532ce4..f67f282 100644 --- a/cmake-init/templates/shared/CMakeLists.txt +++ b/cmake-init/templates/shared/CMakeLists.txt @@ -3,10 +3,10 @@ cmake_minimum_required(VERSION 3.14) include(cmake/prelude.cmake) project( - %(name)s - VERSION %(version)s - DESCRIPTION "%(description)s" - HOMEPAGE_URL "%(homepage)s" + {= name =} + VERSION {= version =} + DESCRIPTION "{= description =}" + HOMEPAGE_URL "{= homepage =}" LANGUAGES CXX ) @@ -16,70 +16,70 @@ include(cmake/variables.cmake) # ---- Declare library ---- add_library( - %(name)s_%(name)s - source/%(name)s.cpp + {= name =}_{= name =} + source/{= name =}.cpp ) -add_library(%(name)s::%(name)s ALIAS %(name)s_%(name)s) +add_library({= name =}::{= name =} ALIAS {= name =}_{= name =}) include(GenerateExportHeader) generate_export_header( - %(name)s_%(name)s - BASE_NAME %(name)s - EXPORT_FILE_NAME export/%(name)s/%(name)s_export.hpp + {= name =}_{= name =} + BASE_NAME {= name =} + EXPORT_FILE_NAME export/{= name =}/{= name =}_export.hpp CUSTOM_CONTENT_FROM_VARIABLE pragma_suppress_c4251 ) if(NOT BUILD_SHARED_LIBS) - target_compile_definitions(%(name)s_%(name)s PUBLIC %(uc_name)s_STATIC_DEFINE) + target_compile_definitions({= name =}_{= name =} PUBLIC {= uc_name =}_STATIC_DEFINE) endif() set_target_properties( - %(name)s_%(name)s PROPERTIES + {= name =}_{= name =} PROPERTIES CXX_VISIBILITY_PRESET hidden VISIBILITY_INLINES_HIDDEN YES VERSION "${PROJECT_VERSION}" SOVERSION "${PROJECT_VERSION_MAJOR}" - EXPORT_NAME %(name)s - OUTPUT_NAME %(name)s + EXPORT_NAME {= name =} + OUTPUT_NAME {= name =} ) target_include_directories( - %(name)s_%(name)s ${warning_guard} + {= name =}_{= name =} ${warning_guard} PUBLIC "$" ) target_include_directories( - %(name)s_%(name)s SYSTEM + {= name =}_{= name =} SYSTEM PUBLIC "$" ) -target_compile_features(%(name)s_%(name)s PUBLIC cxx_std_%(std)s) +target_compile_features({= name =}_{= name =} PUBLIC cxx_std_{= std =}) # ---- Install rules ---- if(NOT CMAKE_SKIP_INSTALL_RULES) include(cmake/install-rules.cmake) -endif(){if cpp_examples} +endif(){% if cpp_examples %} # ---- Examples ---- if(PROJECT_IS_TOP_LEVEL) - option(BUILD_EXAMPLES "Build examples tree." "${%(name)s_DEVELOPER_MODE}") + option(BUILD_EXAMPLES "Build examples tree." "${{= name =}_DEVELOPER_MODE}") if(BUILD_EXAMPLES) add_subdirectory(example) endif() -endif(){end} +endif(){% end %} # ---- Developer mode ---- -if(NOT %(name)s_DEVELOPER_MODE) +if(NOT {= name =}_DEVELOPER_MODE) return() elseif(NOT PROJECT_IS_TOP_LEVEL) message( AUTHOR_WARNING - "Developer mode is intended for developers of %(name)s" + "Developer mode is intended for developers of {= name =}" ) endif() diff --git a/cmake-init/templates/shared/include/__name__/__name__.hpp b/cmake-init/templates/shared/include/__name__/__name__.hpp index 5dc965d..69e861f 100644 --- a/cmake-init/templates/shared/include/__name__/__name__.hpp +++ b/cmake-init/templates/shared/include/__name__/__name__.hpp @@ -2,7 +2,7 @@ #include -#include "%(name)s/%(name)s_export.hpp" +#include "{= name =}/{= name =}_export.hpp" /** * A note about the MSVC warning C4251: @@ -51,7 +51,7 @@ * * Please see the note above for considerations when creating shared libraries. */ -class %(uc_name)s_EXPORT exported_class +class {= uc_name =}_EXPORT exported_class { public: /** @@ -65,6 +65,6 @@ class %(uc_name)s_EXPORT exported_class auto name() const -> const char*; private: - %(uc_name)s_SUPPRESS_C4251 + {= uc_name =}_SUPPRESS_C4251 std::string m_name; }; diff --git a/cmake-init/templates/shared/source/__name__.cpp b/cmake-init/templates/shared/source/__name__.cpp index 36602e3..c49191c 100644 --- a/cmake-init/templates/shared/source/__name__.cpp +++ b/cmake-init/templates/shared/source/__name__.cpp @@ -1,9 +1,9 @@ #include -#include "%(name)s/%(name)s.hpp" +#include "{= name =}/{= name =}.hpp" exported_class::exported_class() - : m_name("%(name)s") + : m_name("{= name =}") { } diff --git a/cmake-init/templates/shared/test/source/__name___test.cpp b/cmake-init/templates/shared/test/source/__name___test.cpp index 9173648..e086034 100644 --- a/cmake-init/templates/shared/test/source/__name___test.cpp +++ b/cmake-init/templates/shared/test/source/__name___test.cpp @@ -1,10 +1,10 @@ #include -#include "%(name)s/%(name)s.hpp" +#include "{= name =}/{= name =}.hpp" auto main() -> int { exported_class e; - return std::string("%(name)s") == e.name() ? 0 : 1; + return std::string("{= name =}") == e.name() ? 0 : 1; } diff --git a/cmake-init/templates/vcpkg/portfile.cmake b/cmake-init/templates/vcpkg/portfile.cmake index 602df0c..98ff660 100644 --- a/cmake-init/templates/vcpkg/portfile.cmake +++ b/cmake-init/templates/vcpkg/portfile.cmake @@ -1,7 +1,7 @@ -{type header}# Header only libraries don't build anything, skip debug build +{% if header %}# Header only libraries don't build anything, skip debug build set(VCPKG_BUILD_TYPE release) -{end}# Please see +{% end %}# Please see # https://vcpkg.readthedocs.io/en/latest/maintainers/vcpkg_from_github/ for # details on how to fill out the arguments vcpkg_from_github( @@ -29,7 +29,7 @@ vcpkg_cmake_configure( "-D${name}_INSTALL_CMAKEDIR=share/${name}" ) -vcpkg_cmake_install(){type shared} +vcpkg_cmake_install(){% if lib %} # If the port's name and the CMake package's name are different, then we can # pass the package name here, otherwise no arguments are necessary @@ -37,7 +37,7 @@ vcpkg_cmake_config_fixup(PACKAGE_NAME "${name}") # Remove files that aren't just the build artifacts and empty folders file(REMOVE_RECURSE "${CURRENT_PACKAGES_DIR}/debug/include") -file(REMOVE_RECURSE "${CURRENT_PACKAGES_DIR}/debug/share"){end} +file(REMOVE_RECURSE "${CURRENT_PACKAGES_DIR}/debug/share"){% end %} # vcpkg requires a license file to be installed as well configure_file( diff --git a/cmake-init/templates/vcpkg/vcpkg.json b/cmake-init/templates/vcpkg/vcpkg.json index 125032b..73c854d 100644 --- a/cmake-init/templates/vcpkg/vcpkg.json +++ b/cmake-init/templates/vcpkg/vcpkg.json @@ -1,14 +1,14 @@ { - "name": "%(name)s", + "name": "{= name =}", "version-semver": "", "dependencies": [ { "name": "vcpkg-cmake", "host": true - }{type shared}, + }{% if lib %}, { "name": "vcpkg-cmake-config", "host": true - }{end} + }{% end %} ] }