From 3c486a7fc8899bd60729190df59ce1b03eb2d33c Mon Sep 17 00:00:00 2001 From: Michael Tremer Date: Tue, 28 Jan 2025 15:07:40 +0000 Subject: [PATCH] build: Add a result callback and use it in the CLI builder Signed-off-by: Michael Tremer --- src/cli/lib/build.c | 61 ++++++++++++++++++++++++++++++++++++++++++++- src/pakfire/build.c | 35 ++++++++++++++++++++++---- src/pakfire/build.h | 7 ++++-- src/pakfire/job.c | 2 +- 4 files changed, 96 insertions(+), 9 deletions(-) diff --git a/src/cli/lib/build.c b/src/cli/lib/build.c index 06e18f14..6f5ab8fe 100644 --- a/src/cli/lib/build.c +++ b/src/cli/lib/build.c @@ -28,8 +28,12 @@ #include "command.h" #include "pakfire.h" +#include #include +#include #include +#include +#include #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; diff --git a/src/pakfire/build.c b/src/pakfire/build.c index 33fd64fe..4470d04c 100644 --- a/src/pakfire/build.c +++ b/src/pakfire/build.c @@ -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)); diff --git a/src/pakfire/build.h b/src/pakfire/build.h index 727a3f41..fd2d2160 100644 --- a/src/pakfire/build.h +++ b/src/pakfire/build.h @@ -21,6 +21,7 @@ #ifndef PAKFIRE_BUILD_H #define PAKFIRE_BUILD_H +#include #include #include @@ -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); diff --git a/src/pakfire/job.c b/src/pakfire/job.c index dd133a13..d764d94f 100644 --- a/src/pakfire/job.c +++ b/src/pakfire/job.c @@ -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) -- 2.39.5