]> git.ipfire.org Git - pakfire.git/commitdiff
build: Add a result callback and use it in the CLI builder
authorMichael Tremer <michael.tremer@ipfire.org>
Tue, 28 Jan 2025 15:07:40 +0000 (15:07 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Tue, 28 Jan 2025 15:07:40 +0000 (15:07 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/cli/lib/build.c
src/pakfire/build.c
src/pakfire/build.h
src/pakfire/job.c

index 06e18f14375aa369c372046192bb2b7a202b2062..6f5ab8fe1461818fd52d13cb2eef649634510967 100644 (file)
 #include "command.h"
 #include "pakfire.h"
 
+#include <pakfire/archive.h>
 #include <pakfire/build.h>
+#include <pakfire/package.h>
 #include <pakfire/pakfire.h>
+#include <pakfire/path.h>
+#include <pakfire/repo.h>
 
 #define MAX_MAKEFILES 32
 
@@ -139,6 +143,61 @@ static void log_callback(void* data, int priority, const char* file, int line,
        pakfire_log_syslog(NULL, priority, file, line, function, format, args);
 }
 
+static int result_callback(struct pakfire_ctx* ctx, struct pakfire* pakfire,
+               struct pakfire_build* build, struct pakfire_archive* archive, void* data) {
+       const struct cli_local_args* local_args = data;
+       struct pakfire_package* pkg = NULL;
+       struct pakfire_repo* local = NULL;
+       char path[PATH_MAX];
+       int r;
+
+       // Fetch the package metadata
+       r = pakfire_archive_make_package(archive, NULL, &pkg);
+       if (r < 0)
+               goto ERROR;
+
+       // Fetch NEVRA
+       const char* nevra = pakfire_package_get_string(pkg, PAKFIRE_PKG_NEVRA);
+
+       // Fetch the local repository & import the archive
+       local = pakfire_get_repo(pakfire, PAKFIRE_REPO_LOCAL);
+       if (local) {
+               r = pakfire_repo_import_archive(local, archive);
+               if (r < 0) {
+                       ERROR(ctx, "Could not import %s to the local repository: %s\n",
+                                       nevra, strerror(-r));
+                       goto ERROR;
+               }
+       }
+
+       // Copy to the target (if given)
+       if (local_args->target) {
+               // Fetch the filename
+               const char* filename = pakfire_package_get_filename(pkg);
+
+               // Make the absolute target path
+               r = pakfire_path_append(path, local_args->target, filename);
+               if (r < 0)
+                       goto ERROR;
+
+               // Copy (or link) the package to the target path
+               r = pakfire_archive_link_or_copy(archive, path);
+               if (r < 0) {
+                       ERROR(ctx, "Could not copy %s to %s: %s\n",
+                                       nevra, path, strerror(-r));
+                       goto ERROR;
+               }
+       }
+
+ERROR:
+       if (local)
+               pakfire_repo_unref(local);
+       if (pkg)
+               pakfire_package_unref(pkg);
+
+       return r;
+}
+
 int cli_build(void* data, int argc, char* argv[]) {
        struct cli_global_args* global_args = data;
        struct cli_local_args local_args = {
@@ -193,7 +252,7 @@ int cli_build(void* data, int argc, char* argv[]) {
        // Process all packages
        for (unsigned int i = 0; i < local_args.num_makefiles; i++) {
                // Run the build
-               r = pakfire_build_exec(build, local_args.makefiles[i]);
+               r = pakfire_build_exec(build, local_args.makefiles[i], result_callback, &local_args);
                if (r) {
                        fprintf(stderr, "Could not build %s\n", local_args.makefiles[i]);
                        goto ERROR;
index 33fd64fee4e950a7eb39a81b48085de05e710e3f..4470d04c78e4d0eae42495986fa0d5165a6b9ea8 100644 (file)
@@ -2388,7 +2388,24 @@ static int pakfire_build_lint(struct pakfire_build* build) {
        return r;
 }
 
-int pakfire_build_exec(struct pakfire_build* build, const char* path) {
+struct pakfire_build_result {
+       struct pakfire_build* build;
+
+       // Callback
+       pakfire_build_result_callback callback;
+       void* data;
+};
+
+static int pakfire_build_result(struct pakfire_ctx* ctx, struct pakfire_package* pkg,
+               struct pakfire_archive* archive, void* data) {
+       const struct pakfire_build_result* result = data;
+       struct pakfire_build* build = result->build;
+
+       return result->callback(ctx, build->pakfire, build, archive, result->data);
+}
+
+int pakfire_build_exec(struct pakfire_build* build, const char* path,
+               pakfire_build_result_callback result_callback, void* data) {
        struct pakfire_package* package = NULL;
        struct pakfire_parser* makefile = NULL;
        char* problems = NULL;
@@ -2456,10 +2473,18 @@ int pakfire_build_exec(struct pakfire_build* build, const char* path) {
        if (r)
                goto ERROR;
 
-       // Copy packages to their destination
-       r = pakfire_build_copy_packages(build);
-       if (r)
-               goto ERROR;
+       // Call the result callback (if any)
+       if (result_callback) {
+               struct pakfire_build_result result = {
+                       .build    = build,
+                       .callback = result_callback,
+                       .data     = data,
+               };
+
+               r = pakfire_repo_walk_archives(build->repo, pakfire_build_result, &result, 0);
+               if (r < 0)
+                       goto ERROR;
+       }
 
        // Log duration
        r = pakfire_format_time(duration, pakfire_build_duration(build));
index 727a3f4177cba1425a7f0a257251745bb5d98ba6..fd2d2160d6fc2981d39f68b09d921f45374d6904 100644 (file)
@@ -21,6 +21,7 @@
 #ifndef PAKFIRE_BUILD_H
 #define PAKFIRE_BUILD_H
 
+#include <pakfire/archive.h>
 #include <pakfire/config.h>
 #include <pakfire/ctx.h>
 
@@ -33,6 +34,8 @@ enum pakfire_build_flags {
        PAKFIRE_BUILD_DISABLE_TESTS    = (1 << 3),
 };
 
+typedef int (*pakfire_build_result_callback)(struct pakfire_ctx* ctx, struct pakfire* pakfire,
+       struct pakfire_build* build, struct pakfire_archive* archive, void* data);
 
 int pakfire_build_create(struct pakfire_build** build, struct pakfire_ctx* ctx,
        struct pakfire_config* config, const char* arch, const char* id, int flags);
@@ -40,13 +43,13 @@ int pakfire_build_create(struct pakfire_build** build, struct pakfire_ctx* ctx,
 struct pakfire_build* pakfire_build_ref(struct pakfire_build* build);
 struct pakfire_build* pakfire_build_unref(struct pakfire_build* build);
 
-
 int pakfire_build_set_ccache_path(struct pakfire_build* build, const char* path);
 int pakfire_build_set_target(struct pakfire_build* build, const char* target);
 
 int pakfire_build_shell(struct pakfire_build* build, const char* argv[], const char** packages);
 
-int pakfire_build_exec(struct pakfire_build* build, const char* path);
+int pakfire_build_exec(struct pakfire_build* build, const char* path,
+       pakfire_build_result_callback result_callback, void* data);
 
 int pakfire_build_mkimage(struct pakfire_build* build,
        const char* type, FILE* f);
index dd133a13bc03fbaa8ec4e215a5fa2989902487e9..d764d94f3ed1f8e607f640f236696d71f613512b 100644 (file)
@@ -522,7 +522,7 @@ static int pakfire_job_child(struct pakfire_job* job) {
        // XXX need to set target
 
        // Run the build
-       r = pakfire_build_exec(build, job->pkg);
+       r = pakfire_build_exec(build, job->pkg, NULL, NULL);
 
 ERROR:
        if (build)