Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update CPS spec support to 0.12 #104

Merged
merged 6 commits into from
Sep 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 48 additions & 30 deletions src/cps/loader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ namespace cps::loader {

namespace {

constexpr static std::string_view CPS_VERSION = "0.10.0";
constexpr static std::string_view CPS_VERSION = "0.12.0";

template <typename T>
tl::expected<std::optional<T>, std::string>
Expand Down Expand Up @@ -116,12 +116,14 @@ namespace cps::loader {

const nlohmann::json & value = parent[name];
if (value.is_object()) {
// TODO: simplify this further, maybe with a loop?
auto && cb = [](auto && r) { return r.value_or(std::vector<std::string>{}); };
ret[KnownLanguages::c] = CPS_TRY(get_optional<std::vector<std::string>>(value, name, "c").map(cb));
ret[KnownLanguages::cxx] = CPS_TRY(get_optional<std::vector<std::string>>(value, name, "c++").map(cb));
auto && fallback = CPS_TRY(get_optional<std::vector<std::string>>(value, name, "*"))
.value_or(std::vector<std::string>{});
ret[KnownLanguages::c] =
CPS_TRY(get_optional<std::vector<std::string>>(value, name, "c")).value_or(fallback);
ret[KnownLanguages::cxx] =
CPS_TRY(get_optional<std::vector<std::string>>(value, name, "c++")).value_or(fallback);
ret[KnownLanguages::fortran] =
CPS_TRY(get_optional<std::vector<std::string>>(value, name, "fortran").map(cb));
CPS_TRY(get_optional<std::vector<std::string>>(value, name, "fortran")).value_or(fallback);
} else if (value.is_array()) {
std::vector<std::string> fin;
for (auto && v : value) {
Expand All @@ -140,22 +142,44 @@ namespace cps::loader {
template <>
tl::expected<Defines, std::string>
get_required<Defines>(const nlohmann::json & parent, std::string_view parent_name, const std::string & name) {
LangValues && lang = CPS_TRY(get_required<LangValues>(parent, parent_name, name));
Defines ret;
for (auto && [k, values] : lang) {
ret[k] = {};
for (auto && value : values) {
if (value.front() == '!') {
ret[k].emplace_back(Define{value.substr(1), false});
} else if (const size_t sep = value.find("="); sep != value.npos) {
std::string dkey = value.substr(0, sep);
std::string dvalue = value.substr(sep + 1);
ret[k].emplace_back(Define{dkey, dvalue});
} else {
ret[k].emplace_back(Define{value});
if (!parent.contains(name)) {
return ret;
}

const nlohmann::json & defines = parent[name];
if (!defines.is_object()) {
return tl::unexpected(fmt::format("Section `{}` of `{}` is not an object", parent_name, name));
}

const auto getter =
[&](const std::string & lang) -> tl::expected<std::optional<std::vector<Define>>, std::string> {
if (!defines.contains(lang)) {
return std::nullopt;
}

std::vector<Define> ret2;
for (auto && [k, v] : defines.at(lang).items()) {
if (v.is_null()) {
ret2.emplace_back(Define{k});
continue;
}
if (!v.is_string()) {
return tl::unexpected(fmt::format(
"key `{}` of language `{}` of section `{}` of `{}` has a value that is not a string", k,
lang, parent_name, name));
}
ret2.emplace_back(Define{k, v.get<std::string>()});
}
}

return ret2;
};

auto && fallback = CPS_TRY(getter("*")).value_or(std::vector<Define>{});
ret[KnownLanguages::c] = CPS_TRY(getter("c")).value_or(fallback);
ret[KnownLanguages::cxx] = CPS_TRY(getter("cxx")).value_or(fallback);
ret[KnownLanguages::fortran] = CPS_TRY(getter("fortran")).value_or(fallback);

return ret;
};

Expand Down Expand Up @@ -217,7 +241,7 @@ namespace cps::loader {
auto const type = CPS_TRY(get_required<std::string>(comp, name, "type").map(string_to_type));
auto const compile_flags = CPS_TRY(get_required<LangValues>(comp, name, "compile_flags"));
auto const includes = CPS_TRY(get_required<LangValues>(comp, name, "includes"));
auto const defines = CPS_TRY(get_required<Defines>(comp, name, "defines"));
auto const definitions = CPS_TRY(get_required<Defines>(comp, name, "definitions"));
auto const link_flags = CPS_TRY(get_optional<std::vector<std::string>>(comp, name, "link_flags"))
.value_or(std::vector<std::string>{});
auto const link_libraries =
Expand All @@ -241,7 +265,7 @@ namespace cps::loader {
.type = std::move(type),
.compile_flags = std::move(compile_flags),
.includes = std::move(includes),
.defines = std::move(defines),
.definitions = std::move(definitions),
.link_flags = std::move(link_flags),
.link_libraries = std::move(link_libraries),
.location = std::move(location),
Expand All @@ -259,17 +283,11 @@ namespace cps::loader {

} // namespace

Define::Define(std::string name_) : name{std::move(name_)}, value{}, define{true} {};
Define::Define(std::string name_, std::string value_)
: name{std::move(name_)}, value{std::move(value_)}, define{true} {};
Define::Define(std::string name_, bool define_) : name{std::move(name_)}, value{}, define{define_} {};

bool Define::is_undefine() const { return !define; }

bool Define::is_define() const { return define && value.empty(); }
Define::Define(std::string name_) : name{std::move(name_)}, value{std::nullopt} {};
Define::Define(std::string name_, std::string value_) : name{std::move(name_)}, value{std::move(value_)} {};

std::string Define::get_name() const { return name; }
std::string Define::get_value() const { return value; }
std::optional<std::string> Define::get_value() const { return value; }

Configuration::Configuration() = default;
Configuration::Configuration(LangValues cflags) : compile_flags{std::move(cflags)} {};
Expand Down
10 changes: 3 additions & 7 deletions src/cps/loader.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,17 +47,13 @@ namespace cps::loader {
public:
Define(std::string name);
Define(std::string name, std::string value);
Define(std::string name, bool define);

bool is_undefine() const;
bool is_define() const;
std::string get_name() const;
std::string get_value() const;
std::optional<std::string> get_value() const;

private:
std::string name;
std::string value;
bool define;
std::optional<std::string> value;
};

using LangValues = std::unordered_map<KnownLanguages, std::vector<std::string>>;
Expand All @@ -68,7 +64,7 @@ namespace cps::loader {
Type type;
LangValues compile_flags;
LangValues includes;
Defines defines;
Defines definitions;
// TODO: configurations
// TODO: std::vector<std::string> link_features;
std::vector<std::string> link_flags;
Expand Down
11 changes: 5 additions & 6 deletions src/cps/printer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,13 @@ namespace cps::printer {
}

if (conf.defines) {
if (auto && f = r.defines.find(loader::KnownLanguages::c); f != r.defines.end() && !f->second.empty()) {
if (auto && f = r.definitions.find(loader::KnownLanguages::c);
f != r.definitions.end() && !f->second.empty()) {
auto && transformer = [](auto && d) {
if (d.is_define()) {
return fmt::format("-D{}", d.get_name());
} else if (d.is_undefine()) {
return fmt::format("-U{}", d.get_name());
if (auto && v = d.get_value()) {
return fmt::format("-D{}={}", d.get_name(), v.value());
} else {
return fmt::format("-D{}={}", d.get_name(), d.get_value());
return fmt::format("-D{}", d.get_name());
}
};
args.reserve(args.size() + f->second.size());
Expand Down
4 changes: 2 additions & 2 deletions src/cps/search.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ namespace cps::search {
// 1. the provided version (or Compat-Version) is < the required version
// 2. This package lacks required components
if (requirements.version) {
// From the CPS spec, version 0.10.0, for package::version,
// From the CPS spec, version 0.12.0, for package::version,
// which as the same semantics as requirement::version:
//
// > If not provided, the CPS will not satisfy any request for
Expand Down Expand Up @@ -459,7 +459,7 @@ namespace cps::search {
// 2. if we do it at the search point we have to plumb overrides
// deep into that
merge_result<loader::KnownLanguages, std::string>(comp.includes, result.includes, prefix_replacer);
merge_result(comp.defines, result.defines);
merge_result(comp.definitions, result.definitions);
merge_result(comp.compile_flags, result.compile_flags);
merge_result(comp.link_libraries, result.link_libraries);
merge_result(comp.link_flags, result.link_flags);
Expand Down
2 changes: 1 addition & 1 deletion src/cps/search.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ namespace cps::search {
std::string version;
loader::LangValues includes;
loader::LangValues compile_flags;
loader::Defines defines;
loader::Defines definitions;
std::vector<std::string> link_flags;
std::vector<std::string> link_libraries;
std::vector<std::string> link_location;
Expand Down
16 changes: 14 additions & 2 deletions tests/cases/cps-config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@ expected = "0.0.1"
name = "cflags"
cps = "minimal"
args = ["flags", "--cflags"]
expected = "-fopenmp -I/usr/local/include -I/opt/include -DFOO=1 -DBAR=2 -UBAR -DOTHER"
expected = "-fopenmp -I/usr/local/include -I/opt/include -DFOO=1 -DBAR=2 -DOTHER"

[[case]]
name = "cflags-only-other"
cps = "minimal"
args = ["flags", "--cflags-only-other"]
expected = "-fopenmp -DFOO=1 -DBAR=2 -UBAR -DOTHER"
expected = "-fopenmp -DFOO=1 -DBAR=2 -DOTHER"

[[case]]
name = "cflags-only-I"
Expand Down Expand Up @@ -108,3 +108,15 @@ name = "component link-flags only other"
cps = "multiple-components"
args = ["flags", "--component", "link-flags", "--libs-only-other"]
expected = "-flto"

[[case]]
name = "Star components"
cps = "full"
args = ["flags", "--component", "star_values", "--cflags", "--print-errors"]
expected = "-fvectorize -I/usr/local/include -I/opt/include -DBAR=2 -DFOO=1 -DOTHER"

[[case]]
name = "Star components override by name"
cps = "full"
args = ["flags", "--component", "star_values_override", "--cflags", "--print-errors"]
expected = "-fvectorize -I/usr/local/include -I/opt/include -DBAR=2 -DFOO=1 -DOTHER"
4 changes: 2 additions & 2 deletions tests/cases/pkg-config-compat.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,13 @@ expected = "-flto"
name = "cflags"
cps = "full"
args = ["pkg-config", "--cflags"]
expected = "-fvectorize -I/usr/local/include -I/opt/include -DFOO=1 -DBAR=2 -UBAR -DOTHER"
expected = "-fvectorize -I/usr/local/include -I/opt/include -DFOO=1 -DBAR=2 -DOTHER"

[[case]]
name = "cflags-only-other"
cps = "full"
args = ["pkg-config", "--cflags-only-other"]
expected = "-fvectorize -DFOO=1 -DBAR=2 -UBAR -DOTHER"
expected = "-fvectorize -DFOO=1 -DBAR=2 -DOTHER"

[[case]]
name = "cflags-only-I"
Expand Down
2 changes: 1 addition & 1 deletion tests/cps-files/lib/cps/cps-path-not-set.cps.in
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "cps-path-not-set",
"cps_version": "0.10.0",
"cps_version": "0.12.0",
"version": "1.0.0",
"components": {
"default": {
Expand Down
2 changes: 1 addition & 1 deletion tests/cps-files/lib/cps/cps-path-set.cps.in
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "cps-path-set",
"cps_version": "0.10.0",
"cps_version": "0.12.0",
"cps_path": "@prefix@/${libdir}/cps/",
"version": "1.0.0",
"components": {
Expand Down
2 changes: 1 addition & 1 deletion tests/cps-files/lib/cps/diamond.cps
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "diamond",
"cps_version": "0.10.0",
"cps_version": "0.12.0",
"requires": {
"needs-components1": {},
"needs-components2": {}
Expand Down
85 changes: 71 additions & 14 deletions tests/cps-files/lib/cps/full.cps
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "full",
"cps_version": "0.10.0",
"cps_version": "0.12.0",
"version": "1.2.1",
"compat_version": "1.0.0",
"components": {
Expand All @@ -14,10 +14,10 @@
"/err"
]
},
"defines": {
"c": [
"-DFAIL"
]
"definitions": {
"c": {
"FAIL": null
}
},
"location": "fake"
},
Expand All @@ -32,17 +32,74 @@
"/opt/include"
]
},
"link_flags": ["-L/usr/lib/", "-lbar", "-flto"],
"defines": {
"link_flags": [
"-L/usr/lib/",
"-lbar",
"-flto"
],
"definitions": {
"c": {
"FOO": "1",
"BAR": "2",
"OTHER": null
}
},
"location": "/something/lib/libfoo.so.1.2.0",
"link_location": "/something/lib/libfoo.so"
},
"star_values": {
"type": "dylib",
"compile_flags": {
"*": ["-fvectorize"]
},
"includes": {
"*": [
"/usr/local/include",
"/opt/include"
]
},
"link_flags": [
"-L/usr/lib/",
"-lbar",
"-flto"
],
"definitions": {
"*": {
"FOO": "1",
"BAR": "2",
"OTHER": null
}
},
"location": "/something/lib/libfoo.so.1.2.0",
"link_location": "/something/lib/libfoo.so"
},
"star_values_override": {
"type": "dylib",
"compile_flags": {
"c": ["-fvectorize"],
"*": ["-bad-value"]
},
"includes": {
"c": [
"FOO=1",
"BAR=2",
"!BAR",
"OTHER"
"/usr/local/include",
"/opt/include"
],
"c++": [
"!FOO"
]
"*": ["/dev/null"]
},
"link_flags": [
"-L/usr/lib/",
"-lbar",
"-flto"
],
"definitions": {
"c": {
"FOO": "1",
"BAR": "2",
"OTHER": null
},
"*": {
"BAD": "value"
}
},
"location": "/something/lib/libfoo.so.1.2.0",
"link_location": "/something/lib/libfoo.so"
Expand Down
Loading
Loading