From: Michael Tremer Date: Wed, 2 Jun 2021 15:58:33 +0000 (+0000) Subject: build: Add exported variables to environment X-Git-Tag: 0.9.28~1285^2~43 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=2ffd21f90372e9dd2b64ed80e5242fd6bcf1b46e;p=pakfire.git build: Add exported variables to environment Signed-off-by: Michael Tremer --- diff --git a/src/libpakfire/build.c b/src/libpakfire/build.c index 4ea9f985a..56bdf4353 100644 --- a/src/libpakfire/build.c +++ b/src/libpakfire/build.c @@ -420,6 +420,9 @@ static int pakfire_build_stage(Pakfire pakfire, PakfireParser makefile, const ch if (r < 0) return r; + // Fetch the environment + char** envp = pakfire_parser_make_environ(makefile); + // Create the build script char* script = pakfire_parser_expand(makefile, "build", template); if (!script) { @@ -430,13 +433,18 @@ static int pakfire_build_stage(Pakfire pakfire, PakfireParser makefile, const ch INFO(pakfire, "Running build stage '%s'\n", stage); - r = pakfire_execute_script(pakfire, script, strlen(script), NULL, NULL, 0, + r = pakfire_execute_script(pakfire, script, strlen(script), NULL, envp, 0, logging_callback, data); if (r) { ERROR(pakfire, "Build stage '%s' failed with status %d\n", stage, r); } ERROR: + if (envp) { + for (char** e = envp; *e; e++) + free(*e); + free(envp); + } if (script) free(script); diff --git a/src/libpakfire/include/pakfire/parser.h b/src/libpakfire/include/pakfire/parser.h index b0acd3051..3097fd37f 100644 --- a/src/libpakfire/include/pakfire/parser.h +++ b/src/libpakfire/include/pakfire/parser.h @@ -99,6 +99,8 @@ int pakfire_parser_apply_declaration( int pakfire_parser_parse_data(PakfireParser parent, const char* data, size_t len, struct pakfire_parser_error** error); +char** pakfire_parser_make_environ(PakfireParser parser); + #endif /* PAKFIRE_PRIVATE */ #endif /* PAKFIRE_PARSER_H */ diff --git a/src/libpakfire/parser.c b/src/libpakfire/parser.c index 0b105e79c..ef44101c6 100644 --- a/src/libpakfire/parser.c +++ b/src/libpakfire/parser.c @@ -822,6 +822,51 @@ PAKFIRE_EXPORT int pakfire_parser_set_namespace(PakfireParser parser, const char return 0; } +char** pakfire_parser_make_environ(PakfireParser parser) { + char** envp = NULL; + unsigned int num = 0; + + for (unsigned int i = 0; i < parser->num_declarations; i++) { + struct pakfire_parser_declaration* d = parser->declarations[i]; + if (!d) + continue; + + // Is the export flag set? + if (d->flags & PAKFIRE_PARSER_DECLARATION_EXPORT) { + char* buffer = NULL; + + char* value = pakfire_parser_expand(parser, d->namespace, d->value); + if (!value) + goto ERROR; + + // Build line + int r = asprintf(&buffer, "%s=%s", d->name, value); + free(value); + if (r < 0) + goto ERROR; + + // Extend the array + envp = reallocarray(envp, num + 2, sizeof(*envp)); + if (!envp) + goto ERROR; + + envp[num++] = buffer; + envp[num] = NULL; + } + } + + return envp; + +ERROR: + if (envp) { + for (char** e = envp; *e; e++) + free(*e); + free(envp); + } + + return NULL; +} + PAKFIRE_EXPORT int pakfire_parser_create_package(PakfireParser parser, PakfirePackage* pkg, PakfireRepo repo, const char* namespace, const char* default_arch) { int r = 1;