Compare commits

..

11 Commits

Author SHA1 Message Date
Duncan Ogilvie ef537ce084 Bump to 0.2.13 2022-06-01 19:27:03 +02:00
Duncan Ogilvie 9cdd0f7344 Escape lists when generating commands 2022-06-01 19:26:41 +02:00
Duncan Ogilvie e69cf4d2b9 Add install.optional flag 2022-06-01 19:06:53 +02:00
Duncan Ogilvie e98a906231 Document the install.component option 2022-06-01 16:59:54 +02:00
Duncan Ogilvie 9a82f8c796 Temporarily remove the crt-linkage and library-linkage options
These require a lot more work to integrate properly with vcpkg, reimplement triplet detection and set a custom triplet
2022-06-01 16:57:09 +02:00
Duncan Ogilvie 13255c68cf Use FetchContent_MakeAvailable for vcpkg in case they add CMakeLists.txt 2022-06-01 16:53:38 +02:00
Duncan Ogilvie 5768460827 Update credits 2022-04-08 18:16:38 +02:00
Duncan Ogilvie 50d4a905b6 Merge pull request #55 from ZehMatt/fix/#54
Fix #54: Fix settings not being properly quoted
2022-04-01 12:49:02 +02:00
ζeh Matt f957cec2dc Update CMakeLists.txt for tests 2022-04-01 03:37:04 +03:00
Duncan Ogilvie 232e49e087 Add support for vcpkg CRT and library linkage customization 2022-04-01 02:27:41 +02:00
ζeh Matt 7408d42160 Fix #54: Fix settings not being properly quoted 2022-04-01 03:15:32 +03:00
16 changed files with 46 additions and 225 deletions
+1 -1
View File
@@ -25,7 +25,7 @@ project(cmkr
LANGUAGES
CXX
VERSION
0.2.12
0.2.13
DESCRIPTION
"CMakeLists generator from TOML"
)
+1
View File
@@ -70,3 +70,4 @@ arguments:
- 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 ❤️
+1 -1
View File
@@ -4,7 +4,7 @@ cmkr-include = false
[project]
name = "cmkr"
version = "0.2.12"
version = "0.2.13"
description = "CMakeLists generator from TOML"
languages = ["CXX"]
subdirs = ["third_party", "tests"]
+1 -1
View File
@@ -2,7 +2,7 @@ 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.12" CACHE STRING "cmkr git tag (this needs to be available forever)" FORCE)
set(CMKR_TAG "v0.2.13" 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
+4 -2
View File
@@ -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
```
+3 -11
View File
@@ -27,7 +27,7 @@ struct Option {
bool val = false;
};
struct FindPackage {
struct Package {
std::string name;
std::string condition;
std::string version;
@@ -128,6 +128,7 @@ struct Install {
std::vector<std::string> configs;
std::string destination;
std::string component;
bool optional = false;
};
struct Subdir {
@@ -151,14 +152,6 @@ struct Content {
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 {
// This is the CMake version required to use all cmkr versions.
std::string cmake_version = "3.15";
@@ -183,9 +176,8 @@ struct Project {
ConditionVector include_after;
std::vector<Setting> settings;
std::vector<Option> options;
std::vector<FindPackage> packages;
std::vector<Package> packages;
Vcpkg vcpkg;
Package package;
std::vector<Content> contents;
std::vector<Template> templates;
std::vector<Target> targets;
+22 -120
View File
@@ -25,16 +25,16 @@ static std::string format(const char *format, tsl::ordered_map<std::string, std:
return s;
}
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;
}
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) {
auto fullpath = full.string();
auto base_len = base.string().length();
auto delet = fullpath.substr(base_len + 1, fullpath.length() - base_len);
return delet;
};
auto stem = name.filename().stem().string();
auto ext = name.extension();
@@ -162,10 +162,11 @@ struct Command {
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) {
@@ -234,16 +235,9 @@ 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 (is_empty(kv.second)) {
if (kv.second.empty()) {
return true;
}
@@ -257,7 +251,7 @@ struct Command {
}
bool print_arg(const RawArg &arg) {
if (is_empty(arg.arg)) {
if (arg.arg.empty()) {
return true;
}
@@ -616,9 +610,6 @@ void generate_cmake(const char *path, const parser::Project *parent_project) {
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;
@@ -643,8 +634,12 @@ void generate_cmake(const char *path, const parser::Project *parent_project) {
cmd("include")("FetchContent");
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");
// 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
@@ -965,23 +960,7 @@ 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");
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.include_directories, target_scope);
target_cmd("target_include_directories", t.private_include_directories, "PRIVATE");
target_cmd("target_link_directories", t.link_directories, target_scope);
@@ -1066,89 +1045,12 @@ void generate_cmake(const char *path, const parser::Project *parent_project) {
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);
}
}
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";
+5 -27
View File
@@ -60,8 +60,7 @@ class TomlChecker {
TomlChecker(TomlChecker &&) = delete;
template <typename T>
bool optional(const toml::key &ky, Condition<T> &destination) {
auto found = false;
void optional(const toml::key &ky, Condition<T> &destination) {
// 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) {
@@ -70,11 +69,9 @@ 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;
}
}
@@ -85,19 +82,15 @@ class TomlChecker {
}
}
visit(ky);
return found;
}
template <typename T>
bool optional(const toml::key &ky, T &destination) {
auto found = false;
void optional(const toml::key &ky, T &destination) {
// 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>
@@ -346,7 +339,7 @@ Project::Project(const Project *parent, const std::string &path, bool build) {
using pkg_map = tsl::ordered_map<std::string, TomlBasicValue>;
const auto &pkgs = toml::find<pkg_map>(toml, "find-package");
for (const auto &itr : pkgs) {
FindPackage p;
Package p;
p.name = itr.first;
const auto &value = itr.second;
if (itr.second.is_string()) {
@@ -626,6 +619,7 @@ Project::Project(const Project *parent, const std::string &path, bool build) {
i.optional("configs", inst.configs);
i.required("destination", inst.destination);
i.optional("component", inst.component);
i.optional("optional", inst.optional);
installs.push_back(inst);
}
}
@@ -634,6 +628,7 @@ Project::Project(const Project *parent, const std::string &path, bool build) {
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;
@@ -656,23 +651,6 @@ Project::Project(const Project *parent, const std::string &path, bool build) {
}
}
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);
}
+8 -8
View File
@@ -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
)
-6
View File
@@ -45,9 +45,3 @@ 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
@@ -1,20 +0,0 @@
[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
@@ -1,9 +0,0 @@
#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
@@ -1,19 +0,0 @@
#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;
}
}