Skip to content

Commit

Permalink
loader: handle "*" for various values
Browse files Browse the repository at this point in the history
This handles the case of "*", including the proper override for when a
language is specifically defined with its own values.

Fixes: #69
  • Loading branch information
dcbaker committed Sep 24, 2024
1 parent e81c79c commit f71b9fc
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 11 deletions.
26 changes: 15 additions & 11 deletions src/cps/loader.cpp
Original file line number Diff line number Diff line change
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 Down Expand Up @@ -150,12 +152,13 @@ namespace cps::loader {
return tl::unexpected(fmt::format("Section `{}` of `{}` is not an object", parent_name, name));
}

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

std::vector<Define> ret2;
for (auto && [k, v] : defines.at(lang).items()) {
if (v.is_null()) {
ret2.emplace_back(Define{k});
Expand All @@ -172,9 +175,10 @@ namespace cps::loader {
return ret2;
};

ret[KnownLanguages::c] = CPS_TRY(getter("c"));
ret[KnownLanguages::cxx] = CPS_TRY(getter("cxx"));
ret[KnownLanguages::fortran] = CPS_TRY(getter("fortran"));
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
12 changes: 12 additions & 0 deletions tests/cases/cps-config.toml
Original file line number Diff line number Diff line change
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"
57 changes: 57 additions & 0 deletions tests/cps-files/lib/cps/full.cps
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,63 @@
},
"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": [
"/usr/local/include",
"/opt/include"
],
"*": ["/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"
}
},
"default_components": [
Expand Down

0 comments on commit f71b9fc

Please sign in to comment.