Compare commits

...

45 Commits

Author SHA1 Message Date
Duncan Ogilvie 153bdbe591 Bump to 0.2.14
2 years ago
Duncan Ogilvie 97f1c5c1cc Fix a bug when there are regex characters in the path
2 years ago
Duncan Ogilvie 5b6d9c3826 Replace illegal characters in the project name with _ when doing cmkr init
2 years ago
Duncan Ogilvie a90988b81a Update the formatting to now allow functions on a single line
2 years ago
Duncan Ogilvie 3a1298f4e8
Merge pull request #60 from pmeerw/fix-warning
2 years ago
Peter Meerwald-Stadler 534f955827 error: fix warning, signedness of comparison
2 years ago
Duncan Ogilvie ef537ce084 Bump to 0.2.13
3 years ago
Duncan Ogilvie 9cdd0f7344 Escape lists when generating commands
3 years ago
Duncan Ogilvie e69cf4d2b9 Add install.optional flag
3 years ago
Duncan Ogilvie e98a906231 Document the install.component option
3 years ago
Duncan Ogilvie 9a82f8c796 Temporarily remove the crt-linkage and library-linkage options
3 years ago
Duncan Ogilvie 13255c68cf Use FetchContent_MakeAvailable for vcpkg in case they add CMakeLists.txt
3 years ago
Duncan Ogilvie 5768460827
Update credits
3 years ago
Duncan Ogilvie 50d4a905b6
Merge pull request #55 from ZehMatt/fix/#54
3 years ago
ζeh Matt f957cec2dc
Update CMakeLists.txt for tests
3 years ago
Duncan Ogilvie 232e49e087 Add support for vcpkg CRT and library linkage customization
3 years ago
ζeh Matt 7408d42160
Fix #54: Fix settings not being properly quoted
3 years ago
Duncan Ogilvie 1596a8143d Bump to 0.2.12
3 years ago
Duncan Ogilvie 9b0f18ee94 Fix a bug in fetch-content generation
3 years ago
Duncan Ogilvie 887086dc05
Credits
3 years ago
Duncan Ogilvie edc8e1e02e Bump to 0.2.11
3 years ago
Duncan Ogilvie 1f6e31e0ef Add optional CMKR_COMMIT_HASH for the paranoid
3 years ago
Duncan Ogilvie 4de1500bb3 Refactor cmake injection
3 years ago
Duncan Ogilvie 85370968f8 Improved fetch-content support
3 years ago
Duncan Ogilvie 06a4c04df6 Add support for 'shallow' in fetch-content
3 years ago
Duncan Ogilvie fef2a0215b Error when recursively globbing in the project root
3 years ago
Duncan Ogilvie fc74a5e802 Bump to 0.2.10
3 years ago
Duncan Ogilvie e48d402e21 Improve version bumping script
3 years ago
Duncan Ogilvie f6a245618e
Merge pull request #53 from build-cpp/better-validation
3 years ago
Duncan Ogilvie c713606fd4 Error when using an unknown key in the root of the TOML
3 years ago
Duncan Ogilvie fe9b6587a9 Error when trying to parse an empty cmake.toml file
3 years ago
Duncan Ogilvie a2f8d9e5c9 Bump to 0.2.9
3 years ago
Duncan Ogilvie c7925e7110
Merge pull request #52 from build-cpp/fix-options
3 years ago
Duncan Ogilvie c778a5fe2b Do not omit non-optional documentation in option()
3 years ago
Duncan Ogilvie 16b2cbbb57 Add sitemap.xml and robots.txt
3 years ago
Duncan Ogilvie 2f5c7823be Redirect /getting-started to https://cmkr.build
3 years ago
Duncan Ogilvie a29b6a8aaa SEO-friendly permalink
3 years ago
Duncan Ogilvie 5ac8b15c9a Bump to 0.2.8
3 years ago
Duncan Ogilvie e7d9faa6bb
Merge pull request #51 from cursey/improvement/install-component
3 years ago
cursey 6f8b07a801
Allow specifying install component name
3 years ago
Duncan Ogilvie e8c6ccb698 Minor improvements to bootstrapping code
3 years ago
Duncan Ogilvie 539ebc8f4b Host documentation on https://cmkr.build
3 years ago
Duncan Ogilvie 37ca385673 Show vcpkg version when configuring
3 years ago
Duncan Ogilvie 8ae029dab1 Bump to 0.2.7
3 years ago
Duncan Ogilvie fb16cc34b1 Fix a regression with invalid names being generated in vcpkg.json
3 years ago

@ -10,7 +10,7 @@ AlignTrailingComments: true
AllowAllParametersOfDeclarationOnNextLine: true
AllowShortBlocksOnASingleLine: false
AllowShortCaseLabelsOnASingleLine: false
AllowShortFunctionsOnASingleLine: true
AllowShortFunctionsOnASingleLine: false
AllowShortIfStatementsOnASingleLine: false
AllowShortLoopsOnASingleLine: false
AlwaysBreakAfterDefinitionReturnType: None

2
CMakeLists.txt generated

@ -25,7 +25,7 @@ project(cmkr
LANGUAGES
CXX
VERSION
0.2.6
0.2.14
DESCRIPTION
"CMakeLists generator from TOML"
)

@ -69,3 +69,5 @@ arguments:
- https://github.com/ToruNiina/toml11
- https://github.com/mpark/variant
- https://www.svgrepo.com/svg/192268/hammer
- https://github.com/can1357 for buying `cmkr.build` ❤️
- https://github.com/JustasMasiulis for fixing the dark theme ❤️

@ -4,7 +4,7 @@ cmkr-include = false
[project]
name = "cmkr"
version = "0.2.6"
version = "0.2.14"
description = "CMakeLists generator from TOML"
languages = ["CXX"]
subdirs = ["third_party", "tests"]

@ -12,6 +12,14 @@ if(NOT EXISTS "${CMAKE_SOURCE_DIR}/cmake/cmkr.cmake")
message(FATAL_ERROR "Cannot find cmkr.cmake")
endif()
# Validate branch
find_package(Git REQUIRED)
execute_process(COMMAND "${GIT_EXECUTABLE}" branch --show-current OUTPUT_VARIABLE GIT_BRANCH)
string(STRIP "${GIT_BRANCH}" GIT_BRANCH)
if(NOT GIT_BRANCH STREQUAL "main")
message(FATAL_ERROR "You need to be on the main branch, you are on: ${GIT_BRANCH}")
endif()
file(READ "${CMAKE_SOURCE_DIR}/cmake.toml" CMAKE_TOML)
string(FIND "${CMAKE_TOML}" "[project]" PROJECT_INDEX)
string(SUBSTRING "${CMAKE_TOML}" ${PROJECT_INDEX} -1 CMAKE_TOML_PROJECT)
@ -38,7 +46,7 @@ endif()
message(STATUS "Version ${OLDVERSION} -> ${NEWVERSION}")
find_program(CMKR_EXECUTABLE "cmkr" PATHS "${CMAKE_SOURCE_DIR}/build" NO_CACHE REQUIRED)
find_program(CMKR_EXECUTABLE "cmkr" PATHS "${CMAKE_SOURCE_DIR}/build" PATH_SUFFIXES Debug Release RelWithDebInfo MinSizeRel NO_CACHE REQUIRED)
message(STATUS "Found cmkr: ${CMKR_EXECUTABLE}")
# Replace version in cmake.toml

@ -2,7 +2,8 @@ include_guard()
# Change these defaults to point to your infrastructure if desired
set(CMKR_REPO "https://github.com/build-cpp/cmkr" CACHE STRING "cmkr git repository" FORCE)
set(CMKR_TAG "v0.2.6" CACHE STRING "cmkr git tag (this needs to be available forever)" FORCE)
set(CMKR_TAG "v0.2.14" CACHE STRING "cmkr git tag (this needs to be available forever)" FORCE)
set(CMKR_COMMIT_HASH "" CACHE STRING "cmkr git commit hash (optional)" FORCE)
# To bootstrap/generate a cmkr project: cmake -P cmkr.cmake
if(CMAKE_SCRIPT_MODE_FILE)
@ -15,6 +16,7 @@ endif()
set(CMKR_EXECUTABLE "" CACHE FILEPATH "cmkr executable")
set(CMKR_SKIP_GENERATION OFF CACHE BOOL "skip automatic cmkr generation")
set(CMKR_BUILD_TYPE "Debug" CACHE STRING "cmkr build configuration")
mark_as_advanced(CMKR_REPO CMKR_TAG CMKR_COMMIT_HASH CMKR_EXECUTABLE CMKR_SKIP_GENERATION CMKR_BUILD_TYPE)
# Disable cmkr if generation is disabled
if(DEFINED ENV{CI} OR CMKR_SKIP_GENERATION OR CMKR_BUILD_SKIP_GENERATION)
@ -56,22 +58,41 @@ else()
endif()
# Use cached cmkr if found
if(DEFINED ENV{CMKR_CACHE} AND EXISTS "$ENV{CMKR_CACHE}")
if(DEFINED ENV{CMKR_CACHE})
set(CMKR_DIRECTORY_PREFIX "$ENV{CMKR_CACHE}")
string(REPLACE "\\" "/" CMKR_DIRECTORY_PREFIX "${CMKR_DIRECTORY_PREFIX}")
if(NOT CMKR_DIRECTORY_PREFIX MATCHES "\\/$")
set(CMKR_DIRECTORY_PREFIX "${CMKR_DIRECTORY_PREFIX}/")
endif()
# Build in release mode for the cache
set(CMKR_BUILD_TYPE "Release")
else()
set(CMKR_DIRECTORY_PREFIX "${CMAKE_CURRENT_BINARY_DIR}/_cmkr_")
endif()
set(CMKR_DIRECTORY "${CMKR_DIRECTORY_PREFIX}${CMKR_TAG}")
set(CMKR_CACHED_EXECUTABLE "${CMKR_DIRECTORY}/bin/${CMKR_EXECUTABLE_NAME}")
# Helper function to check if a string starts with a prefix
# Cannot use MATCHES, see: https://github.com/build-cpp/cmkr/issues/61
function(cmkr_startswith str prefix result)
string(LENGTH "${prefix}" prefix_length)
string(LENGTH "${str}" str_length)
if(prefix_length LESS_EQUAL str_length)
string(SUBSTRING "${str}" 0 ${prefix_length} str_prefix)
if(prefix STREQUAL str_prefix)
set("${result}" ON PARENT_SCOPE)
return()
endif()
endif()
set("${result}" OFF PARENT_SCOPE)
endfunction()
# Handle upgrading logic
if(CMKR_EXECUTABLE AND NOT CMKR_CACHED_EXECUTABLE STREQUAL CMKR_EXECUTABLE)
if(CMKR_EXECUTABLE MATCHES "^${CMAKE_CURRENT_BINARY_DIR}/_cmkr")
if(DEFINED ENV{CMKR_CACHE} AND EXISTS "$ENV{CMKR_CACHE}")
cmkr_startswith("${CMKR_EXECUTABLE}" "${CMAKE_CURRENT_BINARY_DIR}/_cmkr" CMKR_STARTSWITH_BUILD)
cmkr_startswith("${CMKR_EXECUTABLE}" "${CMKR_DIRECTORY_PREFIX}" CMKR_STARTSWITH_CACHE)
if(CMKR_STARTSWITH_BUILD)
if(DEFINED ENV{CMKR_CACHE})
message(AUTHOR_WARNING "[cmkr] Switching to cached cmkr: '${CMKR_CACHED_EXECUTABLE}'")
if(EXISTS "${CMKR_CACHED_EXECUTABLE}")
set(CMKR_EXECUTABLE "${CMKR_CACHED_EXECUTABLE}" CACHE FILEPATH "Full path to cmkr executable" FORCE)
@ -82,7 +103,7 @@ if(CMKR_EXECUTABLE AND NOT CMKR_CACHED_EXECUTABLE STREQUAL CMKR_EXECUTABLE)
message(AUTHOR_WARNING "[cmkr] Upgrading '${CMKR_EXECUTABLE}' to '${CMKR_CACHED_EXECUTABLE}'")
unset(CMKR_EXECUTABLE CACHE)
endif()
elseif(DEFINED ENV{CMKR_CACHE} AND EXISTS "$ENV{CMKR_CACHE}" AND CMKR_EXECUTABLE MATCHES "^${CMKR_DIRECTORY_PREFIX}")
elseif(DEFINED ENV{CMKR_CACHE} AND CMKR_STARTSWITH_CACHE)
message(AUTHOR_WARNING "[cmkr] Upgrading cached '${CMKR_EXECUTABLE}' to '${CMKR_CACHED_EXECUTABLE}'")
unset(CMKR_EXECUTABLE CACHE)
endif()
@ -112,6 +133,16 @@ else()
${CMKR_REPO}
"${CMKR_DIRECTORY}"
)
if(CMKR_COMMIT_HASH)
execute_process(
COMMAND "${GIT_EXECUTABLE}" checkout -q "${CMKR_COMMIT_HASH}"
RESULT_VARIABLE CMKR_EXEC_RESULT
WORKING_DIRECTORY "${CMKR_DIRECTORY}"
)
if(NOT CMKR_EXEC_RESULT EQUAL 0)
message(FATAL_ERROR "Tag '${CMKR_TAG}' hash is not '${CMKR_COMMIT_HASH}'")
endif()
endif()
message(STATUS "[cmkr] Building cmkr (using system compiler)...")
cmkr_exec("${CMAKE_COMMAND}"
--no-warn-unused-cli

@ -0,0 +1 @@
cmkr.build

@ -3,6 +3,7 @@ gem "minima", "~> 2.5"
gem "github-pages", group: :jekyll_plugins
gem "jekyll-remote-theme"
gem "jekyll-sitemap", "~> 1.4.0"
platforms :mingw, :x64_mingw, :mswin, :jruby do
gem "tzinfo", "~> 1.2"

@ -271,6 +271,7 @@ PLATFORMS
DEPENDENCIES
github-pages
jekyll-remote-theme
jekyll-sitemap (~> 1.4.0)
just-the-docs
minima (~> 2.5)
tzinfo (~> 1.2)

@ -2,6 +2,7 @@ title: cmkr
plugins:
- jekyll-remote-theme
- jekyll-sitemap
# Automatically deduce dark theme preference https://github.com/pmarsceill/just-the-docs/pull/464
remote_theme: build-cpp/just-the-docs@light-switch
@ -9,7 +10,7 @@ search_enabled: true
color_scheme: light-or-dark
heading_anchors: true
aux_links:
"cmkr":
"GitHub":
- "https://github.com/build-cpp/cmkr"
aux_links_new_tab: true
@ -19,3 +20,5 @@ gh_edit_repository: "https://github.com/build-cpp/cmkr"
gh_edit_branch: "main"
gh_edit_view_mode: "edit"
gh_edit_source: docs
production_url : https://cmkr.build

@ -200,7 +200,9 @@ working-directory = "mytest-dir"
condition = "mycondition"
targets = ["mytarget", "mytest"]
destination = ["bin"]
component = "mycomponent"
files = ["content/my.png"]
dirs = [""]
configs = [""]
dirs = ["include"]
configs = ["Release", "Debug"]
optional = false
```

@ -0,0 +1,10 @@
---
layout: null
permalink: /getting-started/
---
<html>
<head>
<meta http-equiv="refresh" content="0;url=https://cmkr.build">
</head>
</html>

@ -127,11 +127,14 @@ struct Install {
std::vector<std::string> dirs;
std::vector<std::string> configs;
std::string destination;
std::string component;
bool optional = false;
};
struct Subdir {
std::string name;
std::string condition;
Condition<std::string> cmake_before;
Condition<std::string> cmake_after;
ConditionVector include_before;
@ -142,6 +145,11 @@ struct Content {
std::string name;
std::string condition;
tsl::ordered_map<std::string, std::string> arguments;
Condition<std::string> cmake_before;
Condition<std::string> cmake_after;
ConditionVector include_before;
ConditionVector include_after;
};
struct Project {

@ -13,15 +13,6 @@
namespace cmkr {
namespace gen {
static std::string to_upper(const std::string &str) {
std::string temp;
temp.reserve(str.size());
for (auto c : str) {
temp.push_back(::toupper(c));
}
return temp;
}
static std::string format(const char *format, tsl::ordered_map<std::string, std::string> variables) {
std::string s = format;
for (const auto &itr : variables) {
@ -34,7 +25,7 @@ static std::string format(const char *format, tsl::ordered_map<std::string, std:
return s;
}
static std::vector<std::string> expand_cmake_path(const fs::path &name, const fs::path &toml_dir) {
static std::vector<std::string> expand_cmake_path(const fs::path &name, const fs::path &toml_dir, bool root_project) {
std::vector<std::string> temp;
auto extract_suffix = [](const fs::path &base, const fs::path &full) {
@ -47,6 +38,10 @@ static std::vector<std::string> expand_cmake_path(const fs::path &name, const fs
auto stem = name.filename().stem().string();
auto ext = name.extension();
if (root_project && stem == "**" && name == name.filename()) {
throw std::runtime_error("Recursive globbing not allowed in project root: " + name.string());
}
if (stem == "*") {
for (const auto &f : fs::directory_iterator(toml_dir / name.parent_path(), fs::directory_options::follow_directory_symlink)) {
if (!f.is_directory() && f.path().extension() == ext) {
@ -71,11 +66,11 @@ static std::vector<std::string> expand_cmake_path(const fs::path &name, const fs
return temp;
}
static std::vector<std::string> expand_cmake_paths(const std::vector<std::string> &sources, const fs::path &toml_dir) {
static std::vector<std::string> expand_cmake_paths(const std::vector<std::string> &sources, const fs::path &toml_dir, bool root_project) {
// TODO: add duplicate checking
std::vector<std::string> result;
for (const auto &src : sources) {
auto expanded = expand_cmake_path(src, toml_dir);
auto expanded = expand_cmake_path(src, toml_dir, root_project);
for (const auto &f : expanded) {
result.push_back(f);
}
@ -94,8 +89,24 @@ static void create_file(const fs::path &path, const std::string &contents) {
ofs << contents;
}
// CMake target name rules: https://cmake.org/cmake/help/latest/policy/CMP0037.html [A-Za-z0-9_.+\-]
// TOML bare keys: non-empty strings composed only of [A-Za-z0-9_-]
// We replace all non-TOML bare key characters with _
static std::string escape_project_name(const std::string &name) {
std::string escaped;
escaped.reserve(name.length());
for (auto ch : name) {
if ((ch >= 'A' && ch <= 'Z') || (ch >= 'a' && ch <= 'z') || (ch >= '0' && ch <= '9') || ch == '_' || ch == '-') {
escaped += ch;
} else {
escaped += '_';
}
}
return escaped;
}
void generate_project(const std::string &type) {
const auto name = fs::current_path().stem().string();
const auto name = escape_project_name(fs::current_path().stem().string());
if (fs::exists(fs::current_path() / "cmake.toml")) {
throw std::runtime_error("Cannot initialize a project when cmake.toml already exists!");
}
@ -131,13 +142,17 @@ void generate_project(const std::string &type) {
struct CommandEndl {
std::stringstream &ss;
CommandEndl(std::stringstream &ss) : ss(ss) {}
void endl() { ss << '\n'; }
CommandEndl(std::stringstream &ss) : ss(ss) {
}
void endl() {
ss << '\n';
}
};
struct RawArg {
RawArg() = default;
RawArg(std::string arg) : arg(std::move(arg)) {}
RawArg(std::string arg) : arg(std::move(arg)) {
}
std::string arg;
};
@ -153,7 +168,8 @@ struct Command {
std::string post_comment;
Command(std::stringstream &ss, int depth, std::string command, std::string post_comment)
: ss(ss), depth(depth), command(std::move(command)), post_comment(std::move(post_comment)) {}
: ss(ss), depth(depth), command(std::move(command)), post_comment(std::move(post_comment)) {
}
~Command() noexcept(false) {
if (!generated) {
@ -162,11 +178,16 @@ struct Command {
}
static std::string quote(const std::string &str) {
// Quote an empty string
if (str.empty()) {
return "\"\"";
}
// Don't quote arguments that don't need quoting
if (str.find(' ') == std::string::npos && str.find('\"') == std::string::npos && str.find('/') == std::string::npos &&
str.find(';') == std::string::npos) {
// https://cmake.org/cmake/help/latest/manual/cmake-language.7.html#unquoted-argument
// NOTE: Normally '/' does not require quoting according to the documentation but this has been the case here
// previously, so for backwards compatibility its still here.
if (str.find_first_of("()#\"\\'> |/;") == str.npos)
return str;
}
std::string result;
result += "\"";
for (char ch : str) {
@ -317,7 +338,8 @@ static std::string tolf(const std::string &str) {
};
struct Generator {
Generator(const parser::Project &project) : project(project) {}
Generator(const parser::Project &project) : project(project) {
}
Generator(const Generator &) = delete;
const parser::Project &project;
@ -343,7 +365,9 @@ struct Generator {
return CommandEndl(ss);
}
void endl() { ss << '\n'; }
void endl() {
ss << '\n';
}
void inject_includes(const std::vector<std::string> &includes) {
if (!includes.empty()) {
@ -398,6 +422,14 @@ struct Generator {
}
}
}
void conditional_includes(const parser::ConditionVector &include) {
handle_condition(include, [this](const std::string &, const std::vector<std::string> &includes) { inject_includes(includes); });
}
void conditional_cmake(const parser::Condition<std::string> &cmake) {
handle_condition(cmake, [this](const std::string &, const std::string &cmake) { inject_cmake(cmake); });
}
};
struct ConditionScope {
@ -472,6 +504,9 @@ void generate_cmake(const char *path, const parser::Project *parent_project) {
throw std::runtime_error("No cmake.toml found!");
}
// Root project doesn't have a parent
auto root_project = parent_project == nullptr;
parser::Project project(parent_project, path, false);
Generator gen(project);
@ -480,16 +515,13 @@ void generate_cmake(const char *path, const parser::Project *parent_project) {
auto cmd = [&gen](const std::string &command) { return gen.cmd(command); };
auto comment = [&gen](const std::string &comment) { return gen.comment(comment); };
auto endl = [&gen]() { gen.endl(); };
auto inject_includes = [&gen](const std::vector<std::string> &includes) { gen.inject_includes(includes); };
auto inject_cmake = [&gen](const std::string &cmake) { gen.inject_cmake(cmake); };
std::string cmkr_url = "https://github.com/build-cpp/cmkr";
comment("This file is automatically generated from cmake.toml - DO NOT EDIT");
comment("See " + cmkr_url + " for more information");
endl();
// Root project doesn't have a parent
if (parent_project == nullptr) {
if (root_project) {
cmd("cmake_minimum_required")("VERSION", project.cmake_version).endl();
if (!project.allow_in_tree) {
@ -562,7 +594,7 @@ void generate_cmake(const char *path, const parser::Project *parent_project) {
if (!project.options.empty()) {
comment("Options");
for (const auto &opt : project.options) {
cmd("option")(opt.name, opt.comment, opt.val ? "ON" : "OFF");
cmd("option")(opt.name, RawArg(Command::quote(opt.comment)), opt.val ? "ON" : "OFF");
}
endl();
}
@ -588,8 +620,8 @@ void generate_cmake(const char *path, const parser::Project *parent_project) {
endl();
}
gen.handle_condition(project.include_before, [&](const std::string &, const std::vector<std::string> &includes) { inject_includes(includes); });
gen.handle_condition(project.cmake_before, [&](const std::string &, const std::string &cmake) { inject_cmake(cmake); });
gen.conditional_includes(project.include_before);
gen.conditional_cmake(project.cmake_before);
if (!project.project_name.empty()) {
auto languages = std::make_pair("LANGUAGES", project.project_languages);
@ -598,17 +630,19 @@ void generate_cmake(const char *path, const parser::Project *parent_project) {
cmd("project")(project.project_name, languages, version, description).endl();
}
gen.handle_condition(project.include_after, [&](const std::string &, const std::vector<std::string> &includes) { inject_includes(includes); });
gen.handle_condition(project.cmake_after, [&](const std::string &, const std::string &cmake) { inject_cmake(cmake); });
gen.conditional_includes(project.include_after);
gen.conditional_cmake(project.cmake_after);
if (!project.vcpkg.packages.empty()) {
// Allow the user to specify a url or derive it from the version
auto url = project.vcpkg.url;
auto version_name = url;
if (url.empty()) {
if (project.vcpkg.version.empty()) {
throw std::runtime_error("You need either [vcpkg].version or [vcpkg].url");
}
url = "https://github.com/microsoft/vcpkg/archive/refs/tags/" + project.vcpkg.version + ".tar.gz";
version_name = project.vcpkg.version;
}
// Show a nicer error than vcpkg when specifying an invalid package name
@ -622,11 +656,15 @@ void generate_cmake(const char *path, const parser::Project *parent_project) {
// clang-format off
cmd("if")("CMKR_ROOT_PROJECT", "AND", "NOT", "CMKR_DISABLE_VCPKG");
cmd("include")("FetchContent");
cmd("message")("STATUS", "Fetching vcpkg...");
cmd("message")("STATUS", "Fetching vcpkg (" + version_name + ")...");
cmd("FetchContent_Declare")("vcpkg", "URL", url);
cmd("FetchContent_MakeAvailable")("vcpkg");
// Not using FetchContent_MakeAvailable here in case vcpkg adds CMakeLists.txt
cmd("FetchContent_GetProperties")("vcpkg");
cmd("if")("NOT", "vcpkg_POPULATED");
cmd("FetchContent_Populate")("vcpkg");
cmd("include")("${vcpkg_SOURCE_DIR}/scripts/buildsystems/vcpkg.cmake");
cmd("endif")();
cmd("endif")();
endl();
// clang-format on
@ -689,7 +727,7 @@ void generate_cmake(const char *path, const parser::Project *parent_project) {
ofs << " ],\n";
ofs << " \"description\": \"" << escape(project.project_description) << "\",\n";
ofs << " \"name\": \"" << escape(project.project_name) << "\",\n";
ofs << " \"name\": \"" << escape(vcpkg_escape_identifier(project.project_name)) << "\",\n";
ofs << R"( "version-string": "")" << '\n';
ofs << "}\n";
}
@ -698,6 +736,10 @@ void generate_cmake(const char *path, const parser::Project *parent_project) {
cmd("include")("FetchContent").endl();
for (const auto &content : project.contents) {
ConditionScope cs(gen, content.condition);
gen.conditional_includes(content.include_before);
gen.conditional_cmake(content.cmake_before);
std::string version_info = "";
if (content.arguments.contains("GIT_TAG")) {
version_info = " (" + content.arguments.at("GIT_TAG") + ")";
@ -705,12 +747,11 @@ void generate_cmake(const char *path, const parser::Project *parent_project) {
version_info = " (" + content.arguments.at("SVN_REVISION") + ")";
}
cmd("message")("STATUS", "Fetching " + content.name + version_info + "...");
ss << "FetchContent_Declare(\n\t" << content.name << "\n";
for (const auto &arg : content.arguments) {
ss << "\t" << arg.first << "\n\t\t" << arg.second << "\n";
}
ss << ")\n";
cmd("FetchContent_Declare")(content.name, content.arguments);
cmd("FetchContent_MakeAvailable")(content.name).endl();
gen.conditional_includes(content.include_after);
gen.conditional_cmake(content.cmake_after);
}
}
@ -755,14 +796,13 @@ void generate_cmake(const char *path, const parser::Project *parent_project) {
for (const auto &subdir : project.subdirs) {
ConditionScope cs(gen, subdir.condition);
gen.handle_condition(subdir.include_before,
[&](const std::string &, const std::vector<std::string> &includes) { inject_includes(includes); });
gen.handle_condition(subdir.cmake_before, [&](const std::string &, const std::string &cmake) { inject_cmake(cmake); });
gen.conditional_includes(subdir.include_before);
gen.conditional_cmake(subdir.cmake_before);
add_subdir(subdir.name);
gen.handle_condition(subdir.include_after, [&](const std::string &, const std::vector<std::string> &includes) { inject_includes(includes); });
gen.handle_condition(subdir.cmake_after, [&](const std::string &, const std::string &cmake) { inject_cmake(cmake); });
gen.conditional_includes(subdir.include_after);
gen.conditional_cmake(subdir.cmake_after);
}
if (!project.targets.empty()) {
@ -792,14 +832,12 @@ void generate_cmake(const char *path, const parser::Project *parent_project) {
cmd("set")("CMKR_TARGET", target.name);
if (tmplate != nullptr) {
gen.handle_condition(tmplate->outline.include_before,
[&](const std::string &, const std::vector<std::string> &includes) { inject_includes(includes); });
gen.handle_condition(tmplate->outline.cmake_before, [&](const std::string &, const std::string &cmake) { inject_cmake(cmake); });
gen.conditional_includes(tmplate->outline.include_before);
gen.conditional_cmake(tmplate->outline.cmake_before);
}
gen.handle_condition(target.include_before,
[&](const std::string &, const std::vector<std::string> &includes) { inject_includes(includes); });
gen.handle_condition(target.cmake_before, [&](const std::string &, const std::string &cmake) { inject_cmake(cmake); });
gen.conditional_includes(target.include_before);
gen.conditional_cmake(target.cmake_before);
auto sources_var = target.name + "_SOURCES";
@ -808,7 +846,7 @@ void generate_cmake(const char *path, const parser::Project *parent_project) {
if (tmplate != nullptr) {
gen.handle_condition(tmplate->outline.sources, [&](const std::string &condition, const std::vector<std::string> &condition_sources) {
auto sources = expand_cmake_paths(condition_sources, path);
auto sources = expand_cmake_paths(condition_sources, path, root_project);
if (sources.empty()) {
auto source_key = condition.empty() ? "sources" : (condition + ".sources");
throw std::runtime_error(target.name + " " + source_key + " wildcard found 0 files");
@ -818,7 +856,7 @@ void generate_cmake(const char *path, const parser::Project *parent_project) {
}
gen.handle_condition(target.sources, [&](const std::string &condition, const std::vector<std::string> &condition_sources) {
auto sources = expand_cmake_paths(condition_sources, path);
auto sources = expand_cmake_paths(condition_sources, path, root_project);
if (sources.empty()) {
auto source_key = condition.empty() ? "sources" : (condition + ".sources");
throw std::runtime_error(target.name + " " + source_key + " wildcard found 0 files");
@ -980,14 +1018,12 @@ void generate_cmake(const char *path, const parser::Project *parent_project) {
});
}
gen.handle_condition(target.include_after,
[&](const std::string &, const std::vector<std::string> &includes) { inject_includes(includes); });
gen.handle_condition(target.cmake_after, [&](const std::string &, const std::string &cmake) { inject_cmake(cmake); });
gen.conditional_includes(target.include_after);
gen.conditional_cmake(target.cmake_after);
if (tmplate != nullptr) {
gen.handle_condition(tmplate->outline.include_after,
[&](const std::string &, const std::vector<std::string> &includes) { inject_includes(includes); });
gen.handle_condition(tmplate->outline.cmake_after, [&](const std::string &, const std::string &cmake) { inject_cmake(cmake); });
gen.conditional_includes(tmplate->outline.include_after);
gen.conditional_cmake(tmplate->outline.cmake_after);
}
cmd("unset")("CMKR_TARGET");
@ -1020,7 +1056,7 @@ void generate_cmake(const char *path, const parser::Project *parent_project) {
auto dirs = std::make_pair("DIRS", inst.dirs);
std::vector<std::string> files_data;
if (!inst.files.empty()) {
files_data = expand_cmake_paths(inst.files, path);
files_data = expand_cmake_paths(inst.files, path, root_project);
if (files_data.empty()) {
throw std::runtime_error("[[install]] files wildcard did not resolve to any files");
}
@ -1028,9 +1064,14 @@ void generate_cmake(const char *path, const parser::Project *parent_project) {
auto files = std::make_pair("FILES", inst.files);
auto configs = std::make_pair("CONFIGURATIONS", inst.configs);
auto destination = std::make_pair("DESTINATION", inst.destination);
auto component = std::make_pair("COMPONENT", inst.targets.empty() ? "" : inst.targets.front());
auto component_name = inst.component;
if (component_name.empty() && !inst.targets.empty()) {
component_name = inst.targets.front();
}
auto component = std::make_pair("COMPONENT", component_name);
auto optional = inst.optional ? "OPTIONAL" : "";
ConditionScope cs(gen, inst.condition);
cmd("install")(targets, dirs, files, configs, destination, component);
cmd("install")(targets, dirs, files, configs, destination, component, optional);
}
}

@ -1,15 +1,21 @@
#include "error.hpp"
#include <cassert>
#include <cstddef>
namespace cmkr {
namespace error {
Status::Status(Code ec) noexcept : ec_(ec) {}
Status::Status(Code ec) noexcept : ec_(ec) {
}
Status::operator int() const noexcept { return static_cast<int>(ec_); }
Status::operator int() const noexcept {
return static_cast<int>(ec_);
}
Status::Code Status::code() const noexcept { return ec_; }
Status::Code Status::code() const noexcept {
return ec_;
}
} // namespace error
} // namespace cmkr
@ -20,6 +26,6 @@ static const char *err_string[] = {
};
const char *cmkr_error_status(int i) {
assert(i >= 0 && i < (sizeof(err_string) / sizeof(*(err_string))));
assert(i >= 0 && static_cast<size_t>(i) < (sizeof(err_string) / sizeof(*(err_string))));
return err_string[i];
}

@ -4,7 +4,9 @@
namespace cmkr {
namespace help {
const char *version() noexcept { return "cmkr version " CMKR_VERSION; }
const char *version() noexcept {
return "cmkr version " CMKR_VERSION;
}
const char *message() noexcept {
return R"lit(
@ -22,6 +24,10 @@ arguments:
} // namespace help
} // namespace cmkr
const char *cmkr_help_version(void) { return cmkr::help::version(); }
const char *cmkr_help_version(void) {
return cmkr::help::version();
}
const char *cmkr_help_message(void) { return cmkr::help::message(); }
const char *cmkr_help_message(void) {
return cmkr::help::message();
}

@ -5,6 +5,7 @@
#include <stdexcept>
#include <toml.hpp>
#include <tsl/ordered_map.h>
#include <tsl/ordered_set.h>
namespace cmkr {
namespace parser {
@ -49,12 +50,14 @@ static std::string format_key_error(const std::string &error, const toml::key &k
class TomlChecker {
const TomlBasicValue &m_v;
tsl::ordered_map<toml::key, bool> m_visited;
tsl::ordered_map<toml::key, bool> m_conditionVisited;
tsl::ordered_set<toml::key> m_visited;
tsl::ordered_set<toml::key> m_conditionVisited;
public:
TomlChecker(const TomlBasicValue &v, const toml::key &ky) : m_v(toml::find(v, ky)) {}
TomlChecker(const TomlBasicValue &v) : m_v(v) {}
TomlChecker(const TomlBasicValue &v, const toml::key &ky) : m_v(toml::find(v, ky)) {
}
TomlChecker(const TomlBasicValue &v) : m_v(v) {
}
TomlChecker(const TomlChecker &) = delete;
TomlChecker(TomlChecker &&) = delete;
@ -77,7 +80,7 @@ class TomlChecker {
// Handle visiting logic
for (const auto &itr : destination) {
if (!itr.first.empty()) {
m_conditionVisited.emplace(itr.first, true);
m_conditionVisited.emplace(itr.first);
}
}
visit(ky);
@ -108,7 +111,13 @@ class TomlChecker {
return toml::find(m_v, ky);
}
void visit(const toml::key &ky) { m_visited.emplace(ky, true); }
void visit(const toml::key &ky) {
m_visited.emplace(ky);
}
bool visisted(const toml::key &ky) const {
return m_visited.contains(ky);
}
void check(const tsl::ordered_map<std::string, std::string> &conditions) const {
for (const auto &itr : m_v.as_table()) {
@ -143,14 +152,22 @@ class TomlChecker {
};
class TomlCheckerRoot {
const TomlBasicValue &m_root;
std::deque<TomlChecker> m_checkers;
tsl::ordered_set<toml::key> m_visisted;
bool m_checked = false;
public:
TomlCheckerRoot() = default;
TomlCheckerRoot(const TomlBasicValue &root) : m_root(root) {
}
TomlCheckerRoot(const TomlCheckerRoot &) = delete;
TomlCheckerRoot(TomlCheckerRoot &&) = delete;
bool contains(const toml::key &ky) {
m_visisted.emplace(ky);
return m_root.contains(ky);
}
TomlChecker &create(const TomlBasicValue &v) {
m_checkers.emplace_back(v);
return m_checkers.back();
@ -161,7 +178,14 @@ class TomlCheckerRoot {
return m_checkers.back();
}
void check(const tsl::ordered_map<std::string, std::string> &conditions) {
void check(const tsl::ordered_map<std::string, std::string> &conditions, bool check_root) {
if (check_root) {
for (const auto &itr : m_root.as_table()) {
if (!m_visisted.contains(itr.first)) {
throw std::runtime_error(format_key_error("Unknown key '" + itr.first + "'", itr.first, itr.second));
}
}
}
for (const auto &checker : m_checkers) {
checker.check(conditions);
}
@ -171,13 +195,16 @@ class TomlCheckerRoot {
Project::Project(const Project *parent, const std::string &path, bool build) {
const auto toml_path = fs::path(path) / "cmake.toml";
if (!fs::exists(toml_path)) {
throw std::runtime_error("No cmake.toml was found!");
throw std::runtime_error("File not found '" + toml_path.string() + "'");
}
const auto toml = toml::parse<toml::discard_comments, tsl::ordered_map, std::vector>(toml_path.string());
if (toml.size() == 0) {
throw std::runtime_error("Empty TOML '" + toml_path.string() + "'");
}
TomlCheckerRoot checker;
TomlCheckerRoot checker(toml);
if (toml.contains("cmake")) {
if (checker.contains("cmake")) {
auto &cmake = checker.create(toml, "cmake");
cmake.required("version", cmake_version);
@ -209,7 +236,7 @@ Project::Project(const Project *parent, const std::string &path, bool build) {
// Skip the rest of the parsing when building
if (build) {
checker.check(conditions);
checker.check(conditions, false);
return;
}
@ -231,14 +258,14 @@ Project::Project(const Project *parent, const std::string &path, bool build) {
templates = parent->templates;
}
if (toml.contains("conditions")) {
if (checker.contains("conditions")) {
auto conds = toml::find<decltype(conditions)>(toml, "conditions");
for (const auto &cond : conds) {
conditions[cond.first] = cond.second;
}
}
if (toml.contains("project")) {
if (checker.contains("project")) {
auto &project = checker.create(toml, "project");
project.required("name", project_name);
project.optional("version", project_version);
@ -251,7 +278,7 @@ Project::Project(const Project *parent, const std::string &path, bool build) {
project.optional("subdirs", project_subdirs);
}
if (toml.contains("subdir")) {
if (checker.contains("subdir")) {
const auto &subs = toml::find(toml, "subdir").as_table();
for (const auto &itr : subs) {
Subdir subdir;
@ -268,7 +295,7 @@ Project::Project(const Project *parent, const std::string &path, bool build) {
}
}
if (toml.contains("settings")) {
if (checker.contains("settings")) {
using set_map = tsl::ordered_map<std::string, TomlBasicValue>;
const auto &sets = toml::find<set_map>(toml, "settings");
for (const auto &itr : sets) {
@ -297,7 +324,7 @@ Project::Project(const Project *parent, const std::string &path, bool build) {
}
}
if (toml.contains("options")) {
if (checker.contains("options")) {
using opts_map = tsl::ordered_map<std::string, TomlBasicValue>;
const auto &opts = toml::find<opts_map>(toml, "options");
for (const auto &itr : opts) {
@ -315,7 +342,7 @@ Project::Project(const Project *parent, const std::string &path, bool build) {
}
}
if (toml.contains("find-package")) {
if (checker.contains("find-package")) {
using pkg_map = tsl::ordered_map<std::string, TomlBasicValue>;
const auto &pkgs = toml::find<pkg_map>(toml, "find-package");
for (const auto &itr : pkgs) {
@ -336,41 +363,88 @@ Project::Project(const Project *parent, const std::string &path, bool build) {
}
}
// TODO: perform checking here
if (toml.contains("fetch-content")) {
if (checker.contains("fetch-content")) {
const auto &fc = toml::find(toml, "fetch-content").as_table();
for (const auto &itr : fc) {
Content content;
content.name = itr.first;
auto &c = checker.create(itr.second);
c.optional("condition", content.condition);
c.optional("cmake-before", content.cmake_before);
c.optional("cmake-after", content.cmake_after);
c.optional("include-before", content.include_before);
c.optional("include-after", content.include_after);
for (const auto &argItr : itr.second.as_table()) {
auto key = argItr.first;
if (key == "condition") {
content.condition = argItr.second.as_string();
continue;
std::string value;
if (argItr.second.is_array()) {
for (const auto &list_val : argItr.second.as_array()) {
if (!value.empty()) {
value += ';';
}
value += list_val.as_string();
}
} else if (argItr.second.is_boolean()) {
value = argItr.second.as_boolean() ? "ON" : "OFF";
} else {
value = argItr.second.as_string();
}
auto is_cmake_arg = [](const std::string &s) {
for (auto c : s) {
if (!(std::isdigit(c) || std::isupper(c) || c == '_')) {
return false;
}
}
return true;
};
// https://cmake.org/cmake/help/latest/command/string.html#supported-hash-algorithms
tsl::ordered_set<std::string> hash_algorithms = {
"md5", "sha1", "sha224", "sha256", "sha384", "sha512", "sha3_224", "sha3_256", "sha3_384", "sha3_512",
};
auto key = argItr.first;
if (key == "git") {
key = "GIT_REPOSITORY";
} else if (key == "tag") {
key = "GIT_TAG";
} else if (key == "shallow") {
key = "GIT_SHALLOW";
} else if (key == "svn") {
key = "SVN_REPOSITORY";
} else if (key == "rev") {
key = "SVN_REVISION";
} else if (key == "url") {
key = "URL";
} else if (hash_algorithms.contains(key)) {
std::string algo;
for (auto c : key) {
algo.push_back(std::toupper(c));
}
key = "URL_HASH";
value = algo + "=" + value;
} else if (key == "hash") {
key = "URL_HASH";
} else {
// don't change arg
} else if (is_cmake_arg(key)) {
// allow passthrough of ExternalProject options
} else if (!c.visisted(key)) {
throw std::runtime_error(format_key_error("Unknown key '" + argItr.first + "'", argItr.first, argItr.second));
}
content.arguments.emplace(key, argItr.second.as_string());
// Make sure not to emit keys like "condition" in the FetchContent call
if (!c.visisted(key)) {
content.arguments.emplace(key, value);
}
c.visit(argItr.first);
}
contents.emplace_back(std::move(content));
}
}
if (toml.contains("bin")) {
if (checker.contains("bin")) {
throw std::runtime_error("[[bin]] has been renamed to [[target]]");
}
@ -489,7 +563,7 @@ Project::Project(const Project *parent, const std::string &path, bool build) {
return target;
};
if (toml.contains("template")) {
if (checker.contains("template")) {
const auto &ts = toml::find(toml, "template").as_table();
for (const auto &itr : ts) {
auto &t = checker.create(itr.second);
@ -517,7 +591,7 @@ Project::Project(const Project *parent, const std::string &path, bool build) {
}
}
if (toml.contains("target")) {
if (checker.contains("target")) {
const auto &ts = toml::find(toml, "target").as_table();
for (const auto &itr : ts) {
auto &t = checker.create(itr.second);
@ -525,7 +599,7 @@ Project::Project(const Project *parent, const std::string &path, bool build) {
}
}
if (toml.contains("test")) {
if (checker.contains("test")) {
const auto &ts = toml::find(toml, "test").as_array();
for (const auto &value : ts) {
auto &t = checker.create(value);
@ -540,7 +614,7 @@ Project::Project(const Project *parent, const std::string &path, bool build) {
}
}
if (toml.contains("install")) {
if (checker.contains("install")) {
const auto &is = toml::find(toml, "install").as_array();
for (const auto &value : is) {
auto &i = checker.create(value);
@ -551,14 +625,17 @@ Project::Project(const Project *parent, const std::string &path, bool build) {
i.optional("dirs", inst.dirs);
i.optional("configs", inst.configs);
i.required("destination", inst.destination);
i.optional("component", inst.component);
i.optional("optional", inst.optional);
installs.push_back(inst);
}
}
if (toml.contains("vcpkg")) {
if (checker.contains("vcpkg")) {
auto &v = checker.create(toml, "vcpkg");
v.optional("url", vcpkg.url);
v.optional("version", vcpkg.version);
for (const auto &p : v.find("packages").as_array()) {
Vcpkg::Package package;
const auto &package_str = p.as_string().str;
@ -581,7 +658,7 @@ Project::Project(const Project *parent, const std::string &path, bool build) {
}
}
checker.check(conditions);
checker.check(conditions, true);
}
bool is_root_path(const std::string &path) {

16
tests/CMakeLists.txt generated

@ -14,7 +14,7 @@ add_test(
WORKING_DIRECTORY
"${CMAKE_CURRENT_LIST_DIR}/basic"
COMMAND
$<TARGET_FILE:cmkr>
"$<TARGET_FILE:cmkr>"
build
)
@ -24,7 +24,7 @@ add_test(
WORKING_DIRECTORY
"${CMAKE_CURRENT_LIST_DIR}/interface"
COMMAND
$<TARGET_FILE:cmkr>
"$<TARGET_FILE:cmkr>"
build
)
@ -34,7 +34,7 @@ add_test(
WORKING_DIRECTORY
"${CMAKE_CURRENT_LIST_DIR}/fetch-content"
COMMAND
$<TARGET_FILE:cmkr>
"$<TARGET_FILE:cmkr>"
build
)
@ -44,7 +44,7 @@ add_test(
WORKING_DIRECTORY
"${CMAKE_CURRENT_LIST_DIR}/conditions"
COMMAND
$<TARGET_FILE:cmkr>
"$<TARGET_FILE:cmkr>"
build
)
@ -54,7 +54,7 @@ add_test(
WORKING_DIRECTORY
"${CMAKE_CURRENT_LIST_DIR}/vcpkg"
COMMAND
$<TARGET_FILE:cmkr>
"$<TARGET_FILE:cmkr>"
build
)
@ -64,7 +64,7 @@ add_test(
WORKING_DIRECTORY
"${CMAKE_CURRENT_LIST_DIR}/cxx-standard"
COMMAND
$<TARGET_FILE:cmkr>
"$<TARGET_FILE:cmkr>"
build
)
@ -74,7 +74,7 @@ add_test(
WORKING_DIRECTORY
"${CMAKE_CURRENT_LIST_DIR}/globbing"
COMMAND
$<TARGET_FILE:cmkr>
"$<TARGET_FILE:cmkr>"
build
)
@ -84,7 +84,7 @@ add_test(
WORKING_DIRECTORY
"${CMAKE_CURRENT_LIST_DIR}/templates"
COMMAND
$<TARGET_FILE:cmkr>
"$<TARGET_FILE:cmkr>"
build
)

Loading…
Cancel
Save