diff --git a/src/cps/loader.cpp b/src/cps/loader.cpp index 9617a42..4578684 100644 --- a/src/cps/loader.cpp +++ b/src/cps/loader.cpp @@ -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{}); }; - ret[KnownLanguages::c] = CPS_TRY(get_optional>(value, name, "c").map(cb)); - ret[KnownLanguages::cxx] = CPS_TRY(get_optional>(value, name, "c++").map(cb)); + auto && fallback = CPS_TRY(get_optional>(value, name, "*")) + .value_or(std::vector{}); + ret[KnownLanguages::c] = + CPS_TRY(get_optional>(value, name, "c")).value_or(fallback); + ret[KnownLanguages::cxx] = + CPS_TRY(get_optional>(value, name, "c++")).value_or(fallback); ret[KnownLanguages::fortran] = - CPS_TRY(get_optional>(value, name, "fortran").map(cb)); + CPS_TRY(get_optional>(value, name, "fortran")).value_or(fallback); } else if (value.is_array()) { std::vector fin; for (auto && v : value) { @@ -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::string> { - std::vector ret2; + const auto getter = + [&](const std::string & lang) -> tl::expected>, std::string> { if (!defines.contains(lang)) { - return ret2; + return std::nullopt; } + std::vector ret2; for (auto && [k, v] : defines.at(lang).items()) { if (v.is_null()) { ret2.emplace_back(Define{k}); @@ -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{}); + 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; }; diff --git a/tests/cases/cps-config.toml b/tests/cases/cps-config.toml index 4bddd10..5de331e 100644 --- a/tests/cases/cps-config.toml +++ b/tests/cases/cps-config.toml @@ -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" diff --git a/tests/cps-files/lib/cps/full.cps b/tests/cps-files/lib/cps/full.cps index e7c2be2..5f3fdac 100644 --- a/tests/cps-files/lib/cps/full.cps +++ b/tests/cps-files/lib/cps/full.cps @@ -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": [