Merge pull request #27 from build-cpp/manifest

Add support for headers and vcpkg manifest mode
toml-checker archive_5ad6134e
Duncan Ogilvie 4 years ago committed by GitHub
commit 5ad6134e07
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

1
CMakeLists.txt generated

@ -125,6 +125,7 @@ target_link_libraries(cmkr PRIVATE
ghc_filesystem ghc_filesystem
mpark_variant mpark_variant
ordered_map ordered_map
nlohmann_json
) )
unset(CMKR_TARGET) unset(CMKR_TARGET)

@ -20,7 +20,7 @@ type = "executable"
sources = ["src/*.cpp", "include/*.hpp"] sources = ["src/*.cpp", "include/*.hpp"]
include-directories = ["include"] include-directories = ["include"]
compile-features = ["cxx_std_11"] compile-features = ["cxx_std_11"]
link-libraries = ["toml11", "ghc_filesystem", "mpark_variant", "ordered_map"] link-libraries = ["toml11", "ghc_filesystem", "mpark_variant", "ordered_map", "nlohmann_json"]
[[install]] [[install]]
targets = ["cmkr"] targets = ["cmkr"]

@ -0,0 +1,36 @@
---
# Automatically generated from tests/vcpkg/cmake.toml - DO NOT EDIT
layout: default
title: Dependencies from vcpkg
permalink: /examples/vcpkg
parent: Examples
nav_order: 4
---
# Dependencies from vcpkg
Downloads [fmt v7.1.3](https://fmt.dev/7.1.3/) using [vcpkg](https://vcpkg.io/) and links an `example` target to it:
```toml
[project]
name = "vcpkg"
description = "Dependencies from vcpkg"
# See https://github.com/microsoft/vcpkg/releases for vcpkg versions
# See https://vcpkg.io/en/packages.html for available packages
[vcpkg]
version = "2021.05.12"
packages = ["fmt"]
[find-package]
fmt = {}
[target.example]
type = "executable"
sources = ["src/main.cpp"]
link-libraries = ["fmt::fmt"]
```
The bootstrapping of vcpkg is fully automated and no user interaction is necessary. You can disable vcpkg by setting `CMKR_DISABLE_VCPKG=ON`.
<sup><sub>This page was automatically generated from [tests/vcpkg/cmake.toml](https://github.com/build-cpp/cmkr/tree/main/tests/vcpkg/cmake.toml).</sub></sup>

@ -34,6 +34,7 @@ struct Package {
struct Vcpkg { struct Vcpkg {
std::string version; std::string version;
std::string url;
std::vector<std::string> packages; std::vector<std::string> packages;
}; };
@ -64,6 +65,7 @@ struct Target {
ConditionVector link_libraries; ConditionVector link_libraries;
ConditionVector link_options; ConditionVector link_options;
ConditionVector precompile_headers; ConditionVector precompile_headers;
ConditionVector headers;
ConditionVector sources; ConditionVector sources;
std::string alias; std::string alias;

@ -181,6 +181,7 @@ CMake::CMake(const std::string &path, bool build) {
target.name = itr.first; target.name = itr.first;
target.type = to_enum<TargetType>(toml::find(t, "type").as_string(), "target type"); target.type = to_enum<TargetType>(toml::find(t, "type").as_string(), "target type");
get_optional(t, "headers", target.headers);
get_optional(t, "sources", target.sources); get_optional(t, "sources", target.sources);
get_optional(t, "compile-definitions", target.compile_definitions); get_optional(t, "compile-definitions", target.compile_definitions);
get_optional(t, "compile-features", target.compile_features); get_optional(t, "compile-features", target.compile_features);
@ -191,6 +192,12 @@ CMake::CMake(const std::string &path, bool build) {
get_optional(t, "link-options", target.link_options); get_optional(t, "link-options", target.link_options);
get_optional(t, "precompile-headers", target.precompile_headers); get_optional(t, "precompile-headers", target.precompile_headers);
if (!target.headers.empty()) {
auto &sources = target.sources.nth(0).value();
const auto &headers = target.headers.nth(0)->second;
sources.insert(sources.end(), headers.begin(), headers.end());
}
if (t.contains("alias")) { if (t.contains("alias")) {
target.alias = toml::find(t, "alias").as_string(); target.alias = toml::find(t, "alias").as_string();
} }
@ -250,15 +257,9 @@ CMake::CMake(const std::string &path, bool build) {
if (toml.contains("vcpkg")) { if (toml.contains("vcpkg")) {
const auto &v = toml::find(toml, "vcpkg"); const auto &v = toml::find(toml, "vcpkg");
vcpkg.version = toml::find(v, "version").as_string(); get_optional(v, "url", vcpkg.url);
get_optional(v, "version", vcpkg.version);
vcpkg.packages = toml::find<decltype(vcpkg.packages)>(v, "packages"); vcpkg.packages = toml::find<decltype(vcpkg.packages)>(v, "packages");
// This allows the user to use a custom pmm version if desired
if (contents.count("pmm") == 0) {
contents["pmm"]["url"] = "https://github.com/vector-of-bool/pmm/archive/refs/tags/1.5.1.tar.gz";
// Hack to not execute pmm's example CMakeLists.txt
contents["pmm"]["SOURCE_SUBDIR"] = "pmm";
}
} }
// Reasonable default conditions (you can override these if you desire) // Reasonable default conditions (you can override these if you desire)

@ -8,7 +8,10 @@
#include <cstdio> #include <cstdio>
#include <cstring> #include <cstring>
#include <fstream> #include <fstream>
#include <iomanip>
#include <new> #include <new>
#include <nlohmann/json.hpp>
#include <regex>
#include <sstream> #include <sstream>
#include <stdexcept> #include <stdexcept>
#include <string> #include <string>
@ -160,7 +163,7 @@ struct Command {
~Command() { ~Command() {
if (!generated) { if (!generated) {
assert(false && "Incorrect usage of cmd()"); assert(false && "Incorrect usage of cmd(), you probably forgot ()");
} }
} }
@ -403,6 +406,31 @@ struct Generator {
} }
}; };
static std::string vcpkg_escape_identifier(const std::string &name) {
// Do a reasonable effort to escape the project name for use with vcpkg
std::string escaped;
for (char ch : name) {
if ((ch & 0x80) != 0) {
throw std::runtime_error("Non-ASCII characters are not allowed in [project].name when using [vcpkg]");
}
if (ch == '_' || ch == ' ') {
ch = '-';
}
escaped += std::tolower(ch);
}
const std::regex reserved("prn|aux|nul|con|lpt[1-9]|com[1-9]|core|default");
const std::regex ok("[a-z0-9]+(-[a-z0-9]+)*");
std::cmatch m;
if (!std::regex_match(escaped.c_str(), m, reserved) && std::regex_match(escaped.c_str(), m, ok)) {
return escaped;
} else {
throw std::runtime_error("The escaped project name '" + escaped + "' is not usable with [vcpkg]");
}
}
int generate_cmake(const char *path, bool root) { int generate_cmake(const char *path, bool root) {
if (!fs::exists(fs::path(path) / "cmake.toml")) { if (!fs::exists(fs::path(path) / "cmake.toml")) {
throw std::runtime_error("No cmake.toml found!"); throw std::runtime_error("No cmake.toml found!");
@ -419,8 +447,9 @@ int generate_cmake(const char *path, bool root) {
auto inject_includes = [&gen](const std::vector<std::string> &includes) { gen.inject_includes(includes); }; 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); }; 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("This file is automatically generated from cmake.toml - DO NOT EDIT");
comment("See https://github.com/build-cpp/cmkr for more information"); comment("See " + cmkr_url + " for more information");
endl(); endl();
if (root) { if (root) {
@ -520,15 +549,45 @@ int generate_cmake(const char *path, bool root) {
} }
} }
if (!cmake.vcpkg.version.empty()) { if (!cmake.vcpkg.packages.empty()) {
assert("pmm is required in fetch-content for vcpkg to work" && cmake.contents.count("pmm") != 0); // Allow the user to specify a url or derive it from the version
comment("Bootstrap vcpkg"); auto url = cmake.vcpkg.url;
cmd("include")("${pmm_SOURCE_DIR}/pmm.cmake"); if (url.empty()) {
tsl::ordered_map<std::string, std::vector<std::string>> vcpkg_args; if (cmake.vcpkg.version.empty()) {
vcpkg_args["REVISION"] = {cmake.vcpkg.version}; throw std::runtime_error("You need either [vcpkg].version or [vcpkg].url");
vcpkg_args["REQUIRES"] = cmake.vcpkg.packages; }
auto vcpkg = std::make_pair("VCPKG", vcpkg_args); url = "https://github.com/microsoft/vcpkg/archive/refs/tags/" + cmake.vcpkg.version + ".tar.gz";
cmd("pmm")(vcpkg).endl(); }
// CMake to bootstrap vcpkg and download the packages
// clang-format off
cmd("if")("CMKR_ROOT_PROJECT", "AND", "NOT", "CMKR_DISABLE_VCPKG");
cmd("include")("FetchContent");
cmd("message")("STATUS", "Fetching vcpkg...");
cmd("FetchContent_Declare")("vcpkg", "URL", url);
cmd("FetchContent_MakeAvailable")("vcpkg");
cmd("include")("${vcpkg_SOURCE_DIR}/scripts/buildsystems/vcpkg.cmake");
cmd("endif")();
// clang-format on
// Generate vcpkg.json
using namespace nlohmann;
json j;
j["$cmkr"] = "This file is automatically generated from cmake.toml - DO NOT EDIT";
j["$cmkr-url"] = cmkr_url;
j["$schema"] = "https://raw.githubusercontent.com/microsoft/vcpkg/master/scripts/vcpkg.schema.json";
j["name"] = vcpkg_escape_identifier(cmake.project_name);
j["version-string"] = cmake.project_version;
j["dependencies"] = cmake.vcpkg.packages;
if (!cmake.project_description.empty()) {
j["description"] = cmake.project_description;
}
std::ofstream ofs("vcpkg.json");
if (!ofs) {
throw std::runtime_error("Failed to create a vcpkg.json manifest file!");
}
ofs << std::setw(2) << j << std::endl;
} }
if (!cmake.packages.empty()) { if (!cmake.packages.empty()) {
@ -683,7 +742,7 @@ int generate_cmake(const char *path, bool root) {
} }
if (!target.sources.empty()) { if (!target.sources.empty()) {
cmd("source_group")("TREE", "${CMAKE_CURRENT_SOURCE_DIR}", "FILES", "${" + target.name + "_SOURCES}").endl(); cmd("source_group")("TREE", "${CMAKE_CURRENT_SOURCE_DIR}", "FILES", "${" + sources_var + "}").endl();
} }
if (!target.alias.empty()) { if (!target.alias.empty()) {

10
tests/CMakeLists.txt generated

@ -48,3 +48,13 @@ add_test(
build build
) )
add_test(
NAME
vcpkg
WORKING_DIRECTORY
"${CMAKE_CURRENT_LIST_DIR}/vcpkg"
COMMAND
$<TARGET_FILE:cmkr>
build
)

@ -21,3 +21,9 @@ name = "conditions"
command = "$<TARGET_FILE:cmkr>" command = "$<TARGET_FILE:cmkr>"
working-directory = "conditions" working-directory = "conditions"
arguments = ["build"] arguments = ["build"]
[[test]]
name = "vcpkg"
command = "$<TARGET_FILE:cmkr>"
working-directory = "vcpkg"
arguments = ["build"]

@ -0,0 +1,21 @@
# Downloads [fmt v7.1.3](https://fmt.dev/7.1.3/) using [vcpkg](https://vcpkg.io/) and links an `example` target to it:
[project]
name = "vcpkg"
description = "Dependencies from vcpkg"
# See https://github.com/microsoft/vcpkg/releases for vcpkg versions
# See https://vcpkg.io/en/packages.html for available packages
[vcpkg]
version = "2021.05.12"
packages = ["fmt"]
[find-package]
fmt = {}
[target.example]
type = "executable"
sources = ["src/main.cpp"]
link-libraries = ["fmt::fmt"]
# The bootstrapping of vcpkg is fully automated and no user interaction is necessary. You can disable vcpkg by setting `CMKR_DISABLE_VCPKG=ON`.

@ -0,0 +1,6 @@
#include <fmt/core.h>
int main()
{
fmt::print("Hello, world!\n");
}

@ -0,0 +1,11 @@
{
"$cmkr": "This file is automatically generated from cmake.toml - DO NOT EDIT",
"$cmkr-url": "https://github.com/build-cpp/cmkr",
"$schema": "https://raw.githubusercontent.com/microsoft/vcpkg/master/scripts/vcpkg.schema.json",
"dependencies": [
"fmt"
],
"description": "Example of using vcpkg",
"name": "vcpkg",
"version-string": ""
}

4
third_party/CMakeLists.txt generated vendored

@ -16,3 +16,7 @@ target_include_directories(toml11 INTERFACE toml11-3.6.0)
# https://github.com/mpark/variant (BSL-1.0) # https://github.com/mpark/variant (BSL-1.0)
add_library(mpark_variant INTERFACE) add_library(mpark_variant INTERFACE)
target_include_directories(mpark_variant INTERFACE variant-1.4.0/include) target_include_directories(mpark_variant INTERFACE variant-1.4.0/include)
# https://github.com/nlohmann/json (MIT)
add_library(nlohmann_json INTERFACE)
target_include_directories(nlohmann_json INTERFACE nlohmann-3.9.1/include)

@ -0,0 +1,21 @@
MIT License
Copyright (c) 2013-2021 Niels Lohmann
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

File diff suppressed because it is too large Load Diff
Loading…
Cancel
Save