From: fennecdjay Date: Fri, 31 Mar 2023 17:46:50 +0000 (+0200) Subject: :wrench: New embed stuff X-Git-Tag: nightly~168 X-Git-Url: http://10.10.0.4:5575/?a=commitdiff_plain;h=1e6bb4b1bf29b3bae7d22639b8ae37f8afe8b31d;p=gwion.git :wrench: New embed stuff --- diff --git a/Makefile b/Makefile index 360762b3..54a44f21 100644 --- a/Makefile +++ b/Makefile @@ -89,9 +89,9 @@ options-show: with_config: @bash scripts/embed.bash gwion.config.json - @touch src/main.c src/arg.c + @touch src/main.c @${MAKE} USE_CONFIG=1 - @touch src/main.c src/arg.c + @touch src/main.c almost_gwion: ${almost_obj} ${ALMOST_LIBS} diff --git a/include/arg.h b/include/arg.h index 93bfca89..04cb1f57 100644 --- a/include/arg.h +++ b/include/arg.h @@ -14,8 +14,14 @@ typedef struct CliArg_ { struct Vector_ lib; struct Vector_ config; struct SoundInfo_ *si; + char** (*config_args)(int*, char**); + void (*embed_libs)(Gwion); + void (*embed_scripts)(Gwion); bool loop; bool quit; + bool urc; + bool ulib; + bool uargs; enum COLOR color; } CliArg; diff --git a/scripts/embed.bash b/scripts/embed.bash index 7bba40f6..63b17fdb 100644 --- a/scripts/embed.bash +++ b/scripts/embed.bash @@ -27,6 +27,10 @@ config() { echo "$1" >> embed/embed.mk } +cflag() { + config "CFLAGS += $1" +} + has_func() { nm "$2" | grep "${1}_${3}" > /dev/null } @@ -92,6 +96,9 @@ not_null() { return 1 } +lenght() { + jq -rc "length" <<< "$1" +} array_is_ok() { not_null "$1" || return 1 length=$(jq -rc "length" <<< "$1") @@ -110,7 +117,7 @@ jq -rc '.[]' <<< "$libraries" | names=$(jq -c '.names' <<< "$lib") cflags=$(jq -c '.cflags' <<< "$lib") if [ "$cflags" != "null" ] - then config "CFLAGS += $cflags" + then cflag "$cflags" fi config "LDFLAGS += $path" if [ "$names" != "null" ] @@ -143,8 +150,8 @@ handle_script() { } scripts=$(jq -r '.scripts' <<< "$json") + handle_scripts() { - array_is_ok "$scripts" || return script_helper i=0 jq -r '.[]' <<< "$scripts" | @@ -153,11 +160,18 @@ handle_scripts() { done } embed() { - array_is_ok "$scripts" || array_is_ok "$libraries" || return - echo "ANN void gwion_embed(const Gwion gwion) {" >> embed/embed_foot - handle_libs - handle_scripts >> embed/embed_foot - echo "}" >> embed/embed_foot + array_is_ok "$libraries" && { + echo "ANN void gwion_embed_libs(const Gwion gwion) {" >> embed/embed_foot + handle_libs + echo "}" >> embed/embed_foot + cflag "-DGWION_EMBED_LIBS" + } + array_is_ok "$scripts" && { + echo "ANN void gwion_embed_scripts(const Gwion gwion) {" >> embed/embed_foot + handle_scripts >> embed/embed_foot + echo "}" >> embed/embed_foot + cflag "-DGWION_EMBED_SCRIPTS" + } } embed touch embed/embed_body @@ -169,37 +183,33 @@ in=$(jq -rc '.in' <<< "$audio") out=$(jq -rc '.out' <<< "$audio") samplerate=$(jq -rc '.samplerate' <<< "$audio") -not_null "$in" && config "CFLAGS += -DGWION_DEFAULT_NIN=$in" -not_null "$out" && config "CFLAGS += -DGWION_DEFAULT_NOUT=$out" -not_null "$samplerate" && config "CFLAGS += -DGWION_DEFAULT_SAMPLERATE=$samplerate" +not_null "$in" && cflag "-DGWION_DEFAULT_NIN=$in" +not_null "$out" && cflag "-DGWION_DEFAULT_NOUT=$out" +not_null "$samplerate" && cflag "-DGWION_DEFAULT_SAMPLERATE=$samplerate" args=$(jq -rc '.args' <<< "$json") { - count=0 - config "CFLAGS += -DGWION_CONFIG_ARGS" + cflag "-DGWION_CONFIG_ARGS" echo "static const char *config_argv[] = {" array_is_ok "$args" && { jq -rc '.[]' <<< "$args" | while read -r arg do echo " \"$arg\", " done - count=$((count+1)) } echo "};" - echo "static const int config_argc = $count;" + echo "static const int config_argc = $(lenght "$args");" cat << EOF -ANN const char** config_args(int *argc, char **const argv) { -#ifdef GWION_NO_UARGS - *argc = 0; -#endif +ANN const char** gwion_config_args(int *argc, char **const argv) { const int nargs = config_argc + *argc; const char ** args = malloc(nargs * SZ_INT); + args[0] = *argv; for(int i = 0; i < config_argc; i++) { - args[i] = config_argv[i]; + args[i + 1] = config_argv[i]; } - for(int i = 0; i < *argc; i++) { + for(int i = 1; i < *argc; i++) { args[i + config_argc] = argv[i]; } *argc = nargs; @@ -208,14 +218,11 @@ ANN const char** config_args(int *argc, char **const argv) { EOF } >> embed/embed.c -not_null "$libraries" || not_null "$scripts" && - config "CFLAGS += -DGWION_EMBED" - cflags=$(jq -rc '.cflags' <<< "$json") array_is_ok "$cflags" && { jq -rc '.[]' <<< "$cflags" | while read -r cflag - do config "CFLAGS += $cflag " + do cflag "$cflag" done } ldflags=$(jq -rc '.ldflags' <<< "$json") @@ -227,9 +234,11 @@ array_is_ok "$ldflags" && { } rc=$(jq -rc '.urc' <<< "$json") -[ "$rc" = "false" ] && config "CFLAGS += -DGWION_NO_URC" +[ "$rc" = "false" ] && cflag "-DGWION_HAS_URC=false" ulib=$(jq -rc '.ulib' <<< "$json") -[ "$ulib" = "false" ] && config "CFLAGS += -DGWION_NO_ULIB" +[ "$ulib" = "false" ] && cflag "-DGWION_HAS_ULIB=false" uargs=$(jq -rc '.uargs' <<< "$json") -[ "$uargs" = "false" ] && config "CFLAGS += -DGWION_NO_UARGS" +[ "$uargs" = "false" ] && cflag "-DGWION_HAS_UARGS=false" +name=$(jq -rc '.name' <<< "$json") +not_null "$name" && config "PRG := $name" : diff --git a/src/arg.c b/src/arg.c index 1997cf73..21c40d68 100644 --- a/src/arg.c +++ b/src/arg.c @@ -5,7 +5,6 @@ #include "soundinfo.h" #include "vm.h" #include "gwion.h" -#include "arg.h" #include "pass.h" #include "compile.h" #include "cmdapp.h" @@ -46,7 +45,6 @@ ANN static inline void config_end(const Vector config) { } } -#ifndef GWION_NO_ULIB ANN static m_str plug_dir(void) { const m_str home = getenv("HOME"); const size_t sz = strlen(home); @@ -56,7 +54,6 @@ ANN static m_str plug_dir(void) { strcpy(plug_dir + sz, pdir); return plug_dir; } -#endif enum arg_type { ARG_FILE, @@ -75,9 +72,7 @@ ANN static void arg_init(CliArg *arg) { vector_init(&arg->add); vector_init(&arg->lib); vector_init(&arg->config); -#ifndef GWION_NO_ULIB - vector_add(&arg->lib, (vtype)plug_dir()); -#endif + if(arg->ulib)vector_add(&arg->lib, (vtype)plug_dir()); arg->color = COLOR_AUTO; } @@ -377,23 +372,21 @@ ANN m_bool _arg_parse(struct ArgInternal *arg) { return GW_OK; } -#ifndef GWION_NO_URC ANN static void config_default(struct ArgInternal *arg) { char *home = getenv("HOME"); char c[strlen(home) + strlen(GWIONRC) + 2]; sprintf(c, "%s/%s", home, GWIONRC); config_parse(arg, c); } -#endif ANN m_bool arg_parse(const Gwion gwion, CliArg *a) { + if(!a->uargs) a->arg.argc = 1; + if(a->config_args) a->arg.argv = a->config_args(&a->arg.argc, a->arg.argv); struct ArgInternal arg = {.gwion = gwion, .arg = a}; arg_init(a); #ifdef __FUZZING return; #endif -#ifndef GWION_NO_URC - config_default(&arg); -#endif + if(a->urc) config_default(&arg); return _arg_parse(&arg); } diff --git a/src/gwion.c b/src/gwion.c index 27c14c03..26a9b6a0 100644 --- a/src/gwion.c +++ b/src/gwion.c @@ -76,12 +76,15 @@ ANN static Func gwion_locale(const Gwion gwion) { ANN static m_bool gwion_ok(const Gwion gwion, CliArg *arg) { CHECK_BB(plug_ini(gwion, &arg->lib)); shreduler_set_loop(gwion->vm->shreduler, arg->loop); + if(arg->embed_libs) arg->embed_libs(gwion); if (gwion_audio(gwion) > 0) { CHECK_BB(plug_run(gwion, &arg->mod)); if (type_engine_init(gwion)) { vector_add(&gwion->data->plugs->vec, (m_uint)gwion->env->global_nspc); gwion->vm->cleaner_shred = gwion_cleaner(gwion); gwion->emit->locale = gwion_locale(gwion); + if(arg->embed_scripts) arg->embed_scripts(gwion); + arg_compile(gwion, arg); return GW_OK; } } diff --git a/src/gwiondata.c b/src/gwiondata.c index 1de2eab6..4f6694bb 100644 --- a/src/gwiondata.c +++ b/src/gwiondata.c @@ -1,12 +1,12 @@ #include "gwion_util.h" #include "gwion_ast.h" #include "gwion_env.h" -#include "plug.h" #include "gwiondata.h" #include "vm.h" #include "instr.h" #include "emit.h" #include "gwion.h" +#include "plug.h" #include "specialid.h" #include "pass.h" diff --git a/src/main.c b/src/main.c index e64b6c6c..2df96ebe 100644 --- a/src/main.c +++ b/src/main.c @@ -44,34 +44,52 @@ int main(int argc, char **argv) { #else #ifdef GWION_CONFIG_ARGS -ANN char **config_args(int *, char **const); +ANN char **gwion_config_args(int *, char **const); +#else +#define gwion_config_args NULL #endif -#ifdef GWION_EMBED -ANN void gwion_embed(const Gwion); + +#ifdef GWION_EMBED_LIBS +ANN void gwion_embed_libs(Gwion); +#else +#define gwion_embed_libs NULL #endif -int main(int argc, char **argv) { -#ifndef GWION_CONFIG_ARGS - CliArg arg = {.arg = {.argc = argc, .argv = argv} }; +#ifdef GWION_EMBED_SCRIPTS +ANN void gwion_embed_scripts(Gwion); #else - char **config_argv = config_args(&argc, argv); - CliArg arg = {.arg = {.argc = argc, .argv = config_argv} }; +#define gwion_embed_scripts NULL +#endif + +#ifndef GWION_HAS_URC +#define GWION_HAS_URC true +#endif + +#ifndef GWION_HAS_ULIB +#define GWION_HAS_ULIB true #endif + +#ifndef GWION_HAS_UARGS +#define GWION_HAS_UARGS true +#endif + +int main(int argc, char **argv) { signal(SIGINT, sig); signal(SIGTERM, sig); - const m_bool ini = gwion_ini(&gwion, &arg); -#ifdef GWION_EMBED - gwion_embed(&gwion); -#endif - if(ini > 0) arg_compile(&gwion, &arg); + CliArg arg = { + .arg = {.argc = argc, .argv = argv}, + .urc = GWION_HAS_URC, + .ulib = GWION_HAS_ULIB, + .uargs = GWION_HAS_UARGS, + .config_args = gwion_config_args, + .embed_libs = gwion_embed_libs, + .embed_scripts = gwion_embed_scripts, + }; + const m_bool ini = gwion_ini(&gwion, &arg); arg_release(&arg); -#ifdef GWION_CONFIG_ARGS - free(config_argv); -#endif if (ini > 0) gwion_run(&gwion); gwion_end(&gwion); gwion.vm = NULL; exit(EXIT_SUCCESS); } - #endif diff --git a/src/plug.c b/src/plug.c index d6cb87aa..de0af8de 100644 --- a/src/plug.c +++ b/src/plug.c @@ -237,21 +237,20 @@ ANN m_bool plugin_ini(struct Gwion_ *gwion, const m_str iname, const loc_t loc) ANN gwdriver_t driver_ini(const struct Gwion_ *gwion, struct SoundInfo_ *si) { const Map map = &gwion->data->plugs->map; - m_str dname = si->arg; + m_str dname = strdup(si->arg); m_str opt = strchr(dname, '='); - const char c = opt ? *opt : '\0'; if (opt) *opt = '\0'; for (m_uint i = 0; i < map_size(map); ++i) { const m_str name = (m_str)VKEY(map, i); if (!strcmp(name, dname)) { const Plug plug = (Plug)VVAL(map, i); const gwdriver_t drv = plug->driver; - if (opt) *opt = c; + free(dname); return drv; } } - if (opt) *opt = c; gw_err("can't find driver '%s'\n", dname); + free(dname); return NULL; }