From 70fcd6a5265cdc954a0ded171bde45eae090f381 Mon Sep 17 00:00:00 2001 From: Stone Tickle Date: Mon, 8 Jan 2024 11:20:58 -0500 Subject: [PATCH] add meson.project() for modules to get proj info --- include/functions/meson.h | 1 + src/functions/common.c | 2 +- src/functions/meson.c | 39 ++++++++++++++++++++ src/script/modules/_test.meson | 5 +++ tests/project/muon/script_module/meson.build | 5 +++ 5 files changed, 51 insertions(+), 1 deletion(-) diff --git a/include/functions/meson.h b/include/functions/meson.h index 896d5041d..35a3be86a 100644 --- a/include/functions/meson.h +++ b/include/functions/meson.h @@ -8,4 +8,5 @@ #include "functions/common.h" extern const struct func_impl impl_tbl_meson[]; +extern const struct func_impl impl_tbl_meson_internal[]; #endif diff --git a/src/functions/common.c b/src/functions/common.c index 128db720f..c6a530eed 100644 --- a/src/functions/common.c +++ b/src/functions/common.c @@ -871,7 +871,7 @@ const struct func_impl *kernel_func_tbl[language_mode_count] = { }; const struct func_impl *func_tbl[obj_type_count][language_mode_count] = { - [obj_meson] = { impl_tbl_meson, }, + [obj_meson] = { impl_tbl_meson, impl_tbl_meson_internal, }, [obj_subproject] = { impl_tbl_subproject }, [obj_number] = { impl_tbl_number, impl_tbl_number, }, [obj_dependency] = { impl_tbl_dependency }, diff --git a/src/functions/meson.c b/src/functions/meson.c index 00c450f32..7fcc3cdda 100644 --- a/src/functions/meson.c +++ b/src/functions/meson.c @@ -590,3 +590,42 @@ const struct func_impl impl_tbl_meson[] = { { "version", func_meson_version, tc_string }, { NULL, NULL }, }; + +static enum iteration_result +compiler_dict_to_str_dict_iter(struct workspace *wk, void *_ctx, obj k, obj v) +{ + obj_dict_set(wk, *(obj *)_ctx, make_str(wk, compiler_language_to_s(k)), v); + return ir_cont; +} + +static obj +compiler_dict_to_str_dict(struct workspace *wk, obj d) +{ + obj r; + make_obj(wk, &r, obj_dict); + obj_dict_foreach(wk, d, &r, compiler_dict_to_str_dict_iter); + + return r; +} + +static bool +func_meson_project(struct workspace *wk, obj _, uint32_t args_node, obj *res) +{ + if (!interp_args(wk, args_node, NULL, NULL, NULL)) { + return false; + } + + struct project *proj = current_project(wk); + + make_obj(wk, res, obj_dict); + obj_dict_set(wk, *res, make_str(wk, "opts"), proj->opts); + obj_dict_set(wk, *res, make_str(wk, "compilers"), compiler_dict_to_str_dict(wk, proj->compilers)); + obj_dict_set(wk, *res, make_str(wk, "args"), compiler_dict_to_str_dict(wk, proj->args)); + obj_dict_set(wk, *res, make_str(wk, "link_args"), compiler_dict_to_str_dict(wk, proj->link_args)); + return true; +} + +const struct func_impl impl_tbl_meson_internal[] = { + { "project", func_meson_project, tc_dict, true }, + { NULL, NULL }, +}; diff --git a/src/script/modules/_test.meson b/src/script/modules/_test.meson index b26e995d5..22f3635d0 100644 --- a/src/script/modules/_test.meson +++ b/src/script/modules/_test.meson @@ -100,6 +100,10 @@ func multi_return(k str) -> int return d[k] endfunc +func project() -> dict[any] + return meson.project() +endfunc + return { 'identity': identity, 'append_global': append_global, @@ -116,4 +120,5 @@ return { 'glob_arg': glob_arg, 'typed_list': typed_list, 'multi_return': multi_return, + 'project': project, } diff --git a/tests/project/muon/script_module/meson.build b/tests/project/muon/script_module/meson.build index f62131f52..eb8a6e440 100644 --- a/tests/project/muon/script_module/meson.build +++ b/tests/project/muon/script_module/meson.build @@ -83,3 +83,8 @@ m.typed_list(['1', '2']) assert(m.multi_return('a') == 1) assert(m.multi_return('b') == 2) assert(m.multi_return('c') == 0) + +# test meson.project() +assert('c' not in m.project()['compilers']) +add_languages('c') +assert('c' in m.project()['compilers'])