]> git.ipfire.org Git - pakfire.git/commitdiff
build: Add exported variables to environment
authorMichael Tremer <michael.tremer@ipfire.org>
Wed, 2 Jun 2021 15:58:33 +0000 (15:58 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Wed, 2 Jun 2021 15:58:33 +0000 (15:58 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/libpakfire/build.c
src/libpakfire/include/pakfire/parser.h
src/libpakfire/parser.c

index 4ea9f985a731e181fb75e0385bc61543fc079176..56bdf4353b5b1e6bc3a3465102e0de8fd9fd49ef 100644 (file)
@@ -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);
 
index b0acd305130eaf47293c47ffc48ae5cad9f2c986..3097fd37f7a71515466848d06d33704d772e7524 100644 (file)
@@ -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 */
index 0b105e79cb75e0b67eaf5deffcc36e93d5710f86..ef44101c691466d3011b78a5bfd4c75177fe7281 100644 (file)
@@ -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;