Compare commits

..

27 Commits

Author SHA1 Message Date
Duncan Ogilvie c76956450a Initial draft of [package] support 2022-03-20 21:19:07 +01:00
Duncan Ogilvie 1596a8143d Bump to 0.2.12 2022-03-06 06:07:55 +01:00
Duncan Ogilvie 9b0f18ee94 Fix a bug in fetch-content generation 2022-03-06 06:05:17 +01:00
Duncan Ogilvie 887086dc05 Credits 2022-02-23 21:32:58 +01:00
Duncan Ogilvie edc8e1e02e Bump to 0.2.11 2022-02-12 04:42:19 +01:00
Duncan Ogilvie 1f6e31e0ef Add optional CMKR_COMMIT_HASH for the paranoid 2022-02-12 04:39:34 +01:00
Duncan Ogilvie 4de1500bb3 Refactor cmake injection 2022-02-12 04:14:47 +01:00
Duncan Ogilvie 85370968f8 Improved fetch-content support
Closes #38
2022-02-12 04:05:51 +01:00
Duncan Ogilvie 06a4c04df6 Add support for 'shallow' in fetch-content 2022-02-12 03:10:25 +01:00
Duncan Ogilvie fef2a0215b Error when recursively globbing in the project root
Closes #48
2022-02-12 03:03:57 +01:00
Duncan Ogilvie fc74a5e802 Bump to 0.2.10 2022-02-12 02:45:10 +01:00
Duncan Ogilvie e48d402e21 Improve version bumping script 2022-02-12 02:44:45 +01:00
Duncan Ogilvie f6a245618e Merge pull request #53 from build-cpp/better-validation
Better TOML validation
2022-02-12 02:44:05 +01:00
Duncan Ogilvie c713606fd4 Error when using an unknown key in the root of the TOML
[error] Unknown key 'fetch-cntent'
 --> cmake.toml:2
   |
 2 | [fetch-cntent.blah]
   |  ~~~~~~~~~~~~
2022-02-12 02:19:37 +01:00
Duncan Ogilvie fe9b6587a9 Error when trying to parse an empty cmake.toml file 2022-02-12 02:07:30 +01:00
Duncan Ogilvie a2f8d9e5c9 Bump to 0.2.9 2022-02-11 02:51:18 +01:00
Duncan Ogilvie c7925e7110 Merge pull request #52 from build-cpp/fix-options
Do not omit non-optional documentation in option()
2022-02-11 02:49:21 +01:00
Duncan Ogilvie c778a5fe2b Do not omit non-optional documentation in option() 2022-02-11 02:42:20 +01:00
Duncan Ogilvie 16b2cbbb57 Add sitemap.xml and robots.txt 2022-02-10 01:20:54 +01:00
Duncan Ogilvie 2f5c7823be Redirect /getting-started to https://cmkr.build 2022-02-10 00:54:32 +01:00
Duncan Ogilvie a29b6a8aaa SEO-friendly permalink 2022-02-10 00:15:28 +01:00
Duncan Ogilvie 5ac8b15c9a Bump to 0.2.8 2022-02-02 17:25:10 +01:00
Duncan Ogilvie e7d9faa6bb Merge pull request #51 from cursey/improvement/install-component
Allow specifying install component name
2022-01-29 11:31:29 +01:00
cursey 6f8b07a801 Allow specifying install component name 2022-01-28 17:16:19 -08:00
Duncan Ogilvie e8c6ccb698 Minor improvements to bootstrapping code 2022-01-26 01:07:18 +01:00
Duncan Ogilvie 539ebc8f4b Host documentation on https://cmkr.build 2022-01-14 18:06:04 +01:00
Duncan Ogilvie 37ca385673 Show vcpkg version when configuring 2022-01-14 13:42:03 +01:00
20 changed files with 423 additions and 107 deletions
+1 -1
View File
@@ -25,7 +25,7 @@ project(cmkr
LANGUAGES
CXX
VERSION
0.2.7
0.2.12
DESCRIPTION
"CMakeLists generator from TOML"
)
+1
View File
@@ -69,3 +69,4 @@ 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` ❤️
+1 -1
View File
@@ -4,7 +4,7 @@ cmkr-include = false
[project]
name = "cmkr"
version = "0.2.7"
version = "0.2.12"
description = "CMakeLists generator from TOML"
languages = ["CXX"]
subdirs = ["third_party", "tests"]
+9 -1
View File
@@ -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
+21 -7
View File
@@ -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.7" CACHE STRING "cmkr git tag (this needs to be available forever)" FORCE)
set(CMKR_TAG "v0.2.12" 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)
@@ -62,6 +64,8 @@ if(DEFINED ENV{CMKR_CACHE} AND EXISTS "$ENV{CMKR_CACHE}")
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()
@@ -69,7 +73,7 @@ set(CMKR_DIRECTORY "${CMKR_DIRECTORY_PREFIX}${CMKR_TAG}")
set(CMKR_CACHED_EXECUTABLE "${CMKR_DIRECTORY}/bin/${CMKR_EXECUTABLE_NAME}")
# Handle upgrading logic
if(CMKR_EXECUTABLE AND NOT CMKR_CACHED_EXECUTABLE STREQUAL CMKR_EXECUTABLE)
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}")
message(AUTHOR_WARNING "[cmkr] Switching to cached cmkr: '${CMKR_CACHED_EXECUTABLE}'")
@@ -98,7 +102,7 @@ elseif(NOT CMKR_EXECUTABLE AND EXISTS "${CMKR_CACHED_EXECUTABLE}")
else()
set(CMKR_EXECUTABLE "${CMKR_CACHED_EXECUTABLE}" CACHE FILEPATH "Full path to cmkr executable" FORCE)
message(VERBOSE "[cmkr] Bootstrapping '${CMKR_EXECUTABLE}'")
message(STATUS "[cmkr] Fetching cmkr...")
if(EXISTS "${CMKR_DIRECTORY}")
cmkr_exec("${CMAKE_COMMAND}" -E rm -rf "${CMKR_DIRECTORY}")
@@ -112,6 +116,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
@@ -202,18 +216,18 @@ macro(cmkr)
# Copy the now-generated CMakeLists.txt to CMakerLists.txt
# This is done because you cannot include() a file you are currently in
configure_file(CMakeLists.txt "${CMKR_TEMP_FILE}" COPYONLY)
# Add the macro required for the hack at the start of the cmkr macro
set_source_files_properties("${CMKR_TEMP_FILE}" PROPERTIES
CMKR_CURRENT_LIST_FILE "${CMAKE_CURRENT_LIST_FILE}"
)
# 'Execute' the newly-generated CMakeLists.txt
include("${CMKR_TEMP_FILE}")
# Delete the generated file
file(REMOVE "${CMKR_TEMP_FILE}")
# Do not execute the rest of the original CMakeLists.txt
return()
endif()
+1
View File
@@ -0,0 +1 @@
cmkr.build
+1
View File
@@ -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"
+1
View File
@@ -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)
+5 -2
View File
@@ -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
@@ -18,4 +19,6 @@ gh_edit_link_text: "Edit this page on GitHub."
gh_edit_repository: "https://github.com/build-cpp/cmkr"
gh_edit_branch: "main"
gh_edit_view_mode: "edit"
gh_edit_source: docs
gh_edit_source: docs
production_url : https://cmkr.build
+10
View File
@@ -0,0 +1,10 @@
---
layout: null
permalink: /getting-started/
---
<html>
<head>
<meta http-equiv="refresh" content="0;url=https://cmkr.build">
</head>
</html>
+18 -2
View File
@@ -27,7 +27,7 @@ struct Option {
bool val = false;
};
struct Package {
struct FindPackage {
std::string name;
std::string condition;
std::string version;
@@ -127,11 +127,13 @@ struct Install {
std::vector<std::string> dirs;
std::vector<std::string> configs;
std::string destination;
std::string component;
};
struct Subdir {
std::string name;
std::string condition;
Condition<std::string> cmake_before;
Condition<std::string> cmake_after;
ConditionVector include_before;
@@ -142,6 +144,19 @@ 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 Package {
std::string name;
std::vector<std::string> targets;
std::vector<std::string> headers;
std::vector<std::string> dependencies;
std::string namespace_name;
};
struct Project {
@@ -168,8 +183,9 @@ struct Project {
ConditionVector include_after;
std::vector<Setting> settings;
std::vector<Option> options;
std::vector<Package> packages;
std::vector<FindPackage> packages;
Vcpkg vcpkg;
Package package;
std::vector<Content> contents;
std::vector<Template> templates;
std::vector<Target> targets;
+173 -58
View File
@@ -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,19 +25,23 @@ 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) {
std::vector<std::string> temp;
static std::string extract_suffix(const fs::path &base, const fs::path &full) {
auto fullpath = full.string();
auto base_len = base.string().length();
auto delet = fullpath.substr(base_len + 1, fullpath.length() - base_len);
return delet;
}
auto extract_suffix = [](const fs::path &base, const fs::path &full) {
auto fullpath = full.string();
auto base_len = base.string().length();
auto delet = fullpath.substr(base_len + 1, fullpath.length() - base_len);
return delet;
};
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 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);
}
@@ -162,6 +157,10 @@ 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) {
@@ -235,9 +234,16 @@ struct Command {
return true;
}
template <class T>
static bool is_empty(const T &v) {
return v.empty();
}
static bool is_empty(const char *v) { return *v == '\0'; }
template <class K, class V>
bool print_arg(const std::pair<K, V> &kv) {
if (kv.second.empty()) {
if (is_empty(kv.second)) {
return true;
}
@@ -251,7 +257,7 @@ struct Command {
}
bool print_arg(const RawArg &arg) {
if (arg.arg.empty()) {
if (is_empty(arg.arg)) {
return true;
}
@@ -398,6 +404,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 +486,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 +497,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 +576,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 +602,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 +612,22 @@ 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()) {
if (!root_project) {
throw std::runtime_error("[vcpkg] has to be in the project root");
}
// 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,7 +641,7 @@ 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");
cmd("include")("${vcpkg_SOURCE_DIR}/scripts/buildsystems/vcpkg.cmake");
@@ -698,6 +717,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 +728,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 +777,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 +813,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 +827,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 +837,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");
@@ -946,7 +965,23 @@ void generate_cmake(const char *path, const parser::Project *parent_project) {
target_cmd("target_compile_options", t.compile_options, target_scope);
target_cmd("target_compile_options", t.private_compile_options, "PRIVATE");
target_cmd("target_include_directories", t.include_directories, target_scope);
cmkr::parser::ConditionVector include_directories;
for (const auto &itr : t.include_directories) {
auto directories = itr.second;
if (target_scope == "PUBLIC" || target_scope == "INTERFACE") {
for (auto &directory : directories) {
// TODO: properly detect if this is a relative path
if (directory[0] != '$') {
directory = "$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/" + directory + ">";
}
}
}
include_directories[itr.first] = directories;
}
// TODO: this needs to be done cleaner, maybe the install command has some magic?
include_directories[""].push_back("$<INSTALL_INTERFACE:include>");
target_cmd("target_include_directories", include_directories, target_scope);
target_cmd("target_include_directories", t.private_include_directories, "PRIVATE");
target_cmd("target_link_directories", t.link_directories, target_scope);
@@ -980,14 +1015,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 +1053,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,12 +1061,94 @@ 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);
ConditionScope cs(gen, inst.condition);
cmd("install")(targets, dirs, files, configs, destination, component);
}
}
if (!project.package.targets.empty()) {
//TODO: generate an option to enable/disable installation
const auto &package = project.package;
if (!root_project) {
throw std::runtime_error("[package] has to be in the project root");
}
comment("Project packaging");
cmd("include")("CMakePackageConfigHelpers").endl();
comment("Install headers");
tsl::ordered_map<std::string, std::vector<std::string>> header_tree;
for (const auto &pattern : package.headers) {
auto pattern_path = fs::path(pattern);
auto pattern_parent = pattern_path.parent_path();
auto headers = expand_cmake_path(pattern_path, path, root_project);
for (const auto &header : headers) {
auto install_path = extract_suffix(pattern_parent, header);
auto install_destination = "include/" + fs::path(install_path).parent_path().string();
header_tree[install_destination].push_back(header);
}
}
for (const auto &itr : header_tree) {
cmd("install")(std::make_pair("FILES", itr.second), std::make_pair("DESTINATION", itr.first));
printf("Install destination: %s\n", itr.first.c_str());
for (const auto &header : itr.second) {
printf(" Header: %s\n", header.c_str());
}
}
endl();
comment("Install target outputs and generate target exports");
auto export_name = package.name + "Targets";
cmd("install")(std::make_pair("TARGETS", package.targets), std::make_pair("EXPORT", export_name), "LIBRARY", "ARCHIVE", "RUNTIME");
endl();
comment("Install generated target exports");
auto cmake_destination = "lib/cmake/" + package.name;
cmd("install")(std::make_pair("EXPORT", export_name), std::make_pair("FILE", export_name + ".cmake"),
std::make_pair("NAMESPACE", package.namespace_name), std::make_pair("DESTINATION", cmake_destination));
endl();
auto config_base = "${CMAKE_CURRENT_BINARY_DIR}/" + package.name;
std::vector<std::string> generated_scripts;
if (!project.project_version.empty()) {
comment("Generate package version scripts");
auto version_cmake = config_base + "ConfigVersion.cmake";
cmd("write_basic_package_version_file")(version_cmake, std::make_pair("VERSION", "${PROJECT_VERSION}"),
std::make_pair("COMPATIBILITY", "SameMajorVersion"));
endl();
generated_scripts.push_back(version_cmake);
}
comment("Generate package config scripts");
cmd("set")("PACKAGE_NAME", package.name);
// TODO: this needs some actual logic
cmd("set")("PACKAGE_DEPENDENCIES", package.dependencies);
auto config_cmake = config_base + "Config.cmake";
auto config_in = config_cmake + ".in";
cmd("file")("WRITE", config_in, RawArg(R"("include(CMakeFindDependencyMacro)\n\n")"));
cmd("file")("APPEND", config_in, RawArg(R"("string(REGEX MATCHALL \"[^;]+\" SEPARATE_DEPENDENCIES \"@PACKAGE_DEPENDENCIES@\")\n")"));
cmd("file")("APPEND", config_in, RawArg(R"("foreach(dependency ${SEPARATE_DEPENDENCIES})\n")"));
cmd("file")("APPEND", config_in, RawArg(R"(" string(REPLACE \" \" \";\" args \"\${dependency}\")\n")"));
cmd("file")("APPEND", config_in, RawArg(R"(" find_dependency(\${args})\n")"));
cmd("file")("APPEND", config_in, RawArg(R"("endforeach()\n\n")"));
cmd("file")("APPEND", config_in, RawArg(R"("include(\"\${CMAKE_CURRENT_LIST_DIR}/@PACKAGE_NAME@Targets.cmake\")\n")"));
cmd("configure_file")(config_in, config_cmake, "@ONLY");
generated_scripts.push_back(config_cmake);
endl();
comment("Install generated package scripts");
cmd("install")(std::make_pair("FILES", generated_scripts), std::make_pair("DESTINATION", cmake_destination));
endl();
}
// Generate CMakeLists.txt
auto list_path = fs::path(path) / "CMakeLists.txt";
+127 -35
View File
@@ -5,6 +5,7 @@
#include <stdexcept>
#include <toml.hpp>
#include <tsl/ordered_map.h>
#include <tsl/ordered_set.h>
namespace cmkr {
namespace parser {
@@ -49,8 +50,8 @@ 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)) {}
@@ -59,7 +60,8 @@ class TomlChecker {
TomlChecker(TomlChecker &&) = delete;
template <typename T>
void optional(const toml::key &ky, Condition<T> &destination) {
bool optional(const toml::key &ky, Condition<T> &destination) {
auto found = false;
// TODO: this algorithm in O(n) over the amount of keys, kinda bad
const auto &table = m_v.as_table();
for (const auto &itr : table) {
@@ -68,28 +70,34 @@ class TomlChecker {
if (value.is_table()) {
if (value.contains(ky)) {
destination[key] = toml::find<T>(value, ky);
found = true;
}
} else if (key == ky) {
destination[""] = toml::find<T>(m_v, ky);
found = true;
}
}
// 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);
return found;
}
template <typename T>
void optional(const toml::key &ky, T &destination) {
bool optional(const toml::key &ky, T &destination) {
auto found = false;
// TODO: this currently doesn't allow you to get an optional map<string, X>
if (m_v.contains(ky)) {
destination = toml::find<T>(m_v, ky);
found = true;
}
visit(ky);
return found;
}
template <typename T>
@@ -108,7 +116,9 @@ 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 +153,21 @@ 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,11 +342,11 @@ 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) {
Package p;
FindPackage p;
p.name = itr.first;
const auto &value = itr.second;
if (itr.second.is_string()) {
@@ -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,11 +625,12 @@ 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);
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);
@@ -581,7 +656,24 @@ Project::Project(const Project *parent, const std::string &path, bool build) {
}
}
checker.check(conditions);
if(checker.contains("package")) {
auto& p = checker.create(toml, "package");
if(!p.optional("name", package.name)) {
package.name = project_name;
}
p.required("targets", package.targets);
// TODO: should this be optional?
p.required("headers", package.headers);
// TODO: how to detect this should default to all dependencies vs no dependencies?
p.optional("dependencies", package.dependencies);
if(!p.optional("namespace", package.namespace_name)) {
package.namespace_name = project_name + "::";
} else {
// TODO: check if the namespace ends with "::"?
}
}
checker.check(conditions, true);
}
bool is_root_path(const std::string &path) {
+6
View File
@@ -45,3 +45,9 @@ name = "templates"
working-directory = "templates"
command = "$<TARGET_FILE:cmkr>"
arguments = ["build"]
[[test]]
name = "package"
working-directory = "package"
command = "$<TARGET_FILE:cmkr>"
arguments = ["build"]
+20
View File
@@ -0,0 +1,20 @@
[project]
name = "Utility"
version = "1.2"
[target.Utility]
type = "static"
alias = "Utility::Utility" # autogenerate this alias as ${PROJECT_NAME}::TargetName
sources = [
"src/Utility/*.cpp",
"include/Utility/*.hpp",
]
include-directories = ["include"]
compile-features = ["cxx_std_11"]
[package]
name = "Utility" # defaults to ${PROJECT_NAME}
targets = ["Utility"] # required
headers = ["include/**.hpp", "include/**.h"] # required
dependencies = [] # defaults to all the required packages
namespace = "Utility::" # defaults to ${PROJECT_NAME}::
+9
View File
@@ -0,0 +1,9 @@
#pragma once
#include <string>
namespace String
{
bool startsWith(const std::string& str, const std::string& prefix);
std::string reverse(const std::string& str);
}
+19
View File
@@ -0,0 +1,19 @@
#include <cctype>
#include <algorithm>
#include <Utility/String.hpp>
namespace String
{
bool startsWith(const std::string& str, const std::string& prefix)
{
return str.find(prefix) == 0;
}
std::string reverse(const std::string& str)
{
auto result = str;
std::reverse(result.begin(), result.end());
return result;
}
}