Skip to content

Commit

Permalink
Merge pull request #27 from build-cpp/manifest
Browse files Browse the repository at this point in the history
Add support for headers and vcpkg manifest mode
  • Loading branch information
mrexodia authored May 31, 2021
2 parents 4988f14 + 7b20d8c commit 5ad6134
Show file tree
Hide file tree
Showing 14 changed files with 25,646 additions and 21 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion cmake.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ type = "executable"
sources = ["src/*.cpp", "include/*.hpp"]
include-directories = ["include"]
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]]
targets = ["cmkr"]
Expand Down
36 changes: 36 additions & 0 deletions docs/examples/vcpkg.md
Original file line number Diff line number Diff line change
@@ -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>
2 changes: 2 additions & 0 deletions include/cmake.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ struct Package {

struct Vcpkg {
std::string version;
std::string url;
std::vector<std::string> packages;
};

Expand Down Expand Up @@ -64,6 +65,7 @@ struct Target {
ConditionVector link_libraries;
ConditionVector link_options;
ConditionVector precompile_headers;
ConditionVector headers;
ConditionVector sources;

std::string alias;
Expand Down
17 changes: 9 additions & 8 deletions src/cmake.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,7 @@ CMake::CMake(const std::string &path, bool build) {
target.name = itr.first;
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, "compile-definitions", target.compile_definitions);
get_optional(t, "compile-features", target.compile_features);
Expand All @@ -191,6 +192,12 @@ CMake::CMake(const std::string &path, bool build) {
get_optional(t, "link-options", target.link_options);
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")) {
target.alias = toml::find(t, "alias").as_string();
}
Expand Down Expand Up @@ -250,15 +257,9 @@ CMake::CMake(const std::string &path, bool build) {

if (toml.contains("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");

// 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)
Expand Down
83 changes: 71 additions & 12 deletions src/gen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@
#include <cstdio>
#include <cstring>
#include <fstream>
#include <iomanip>
#include <new>
#include <nlohmann/json.hpp>
#include <regex>
#include <sstream>
#include <stdexcept>
#include <string>
Expand Down Expand Up @@ -160,7 +163,7 @@ struct Command {

~Command() {
if (!generated) {
assert(false && "Incorrect usage of cmd()");
assert(false && "Incorrect usage of cmd(), you probably forgot ()");
}
}

Expand Down Expand Up @@ -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) {
if (!fs::exists(fs::path(path) / "cmake.toml")) {
throw std::runtime_error("No cmake.toml found!");
Expand All @@ -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_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 https://github.com/build-cpp/cmkr for more information");
comment("See " + cmkr_url + " for more information");
endl();

if (root) {
Expand Down Expand Up @@ -520,15 +549,45 @@ int generate_cmake(const char *path, bool root) {
}
}

if (!cmake.vcpkg.version.empty()) {
assert("pmm is required in fetch-content for vcpkg to work" && cmake.contents.count("pmm") != 0);
comment("Bootstrap vcpkg");
cmd("include")("${pmm_SOURCE_DIR}/pmm.cmake");
tsl::ordered_map<std::string, std::vector<std::string>> vcpkg_args;
vcpkg_args["REVISION"] = {cmake.vcpkg.version};
vcpkg_args["REQUIRES"] = cmake.vcpkg.packages;
auto vcpkg = std::make_pair("VCPKG", vcpkg_args);
cmd("pmm")(vcpkg).endl();
if (!cmake.vcpkg.packages.empty()) {
// Allow the user to specify a url or derive it from the version
auto url = cmake.vcpkg.url;
if (url.empty()) {
if (cmake.vcpkg.version.empty()) {
throw std::runtime_error("You need either [vcpkg].version or [vcpkg].url");
}
url = "https://github.com/microsoft/vcpkg/archive/refs/tags/" + cmake.vcpkg.version + ".tar.gz";
}

// 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()) {
Expand Down Expand Up @@ -683,7 +742,7 @@ int generate_cmake(const char *path, bool root) {
}

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()) {
Expand Down
10 changes: 10 additions & 0 deletions tests/CMakeLists.txt

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions tests/cmake.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,10 @@ arguments = ["build"]
name = "conditions"
command = "$<TARGET_FILE:cmkr>"
working-directory = "conditions"
arguments = ["build"]

[[test]]
name = "vcpkg"
command = "$<TARGET_FILE:cmkr>"
working-directory = "vcpkg"
arguments = ["build"]
21 changes: 21 additions & 0 deletions tests/vcpkg/cmake.toml
Original file line number Diff line number Diff line change
@@ -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`.
6 changes: 6 additions & 0 deletions tests/vcpkg/src/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#include <fmt/core.h>

int main()
{
fmt::print("Hello, world!\n");
}
11 changes: 11 additions & 0 deletions tests/vcpkg/vcpkg.json
Original file line number Diff line number Diff line change
@@ -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 changes: 4 additions & 0 deletions third_party/CMakeLists.txt

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 21 additions & 0 deletions third_party/nlohmann-3.9.1/LICENSE
Original file line number Diff line number Diff line change
@@ -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.
Loading

0 comments on commit 5ad6134

Please sign in to comment.