From: Michael Tremer Date: Sun, 5 Jan 2025 13:30:03 +0000 (+0000) Subject: build: Create and manage the Pakfire instance ourselves X-Git-Tag: 0.9.30~533 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=eccc1d5bce853ce9df1836e48595f158f9dcd91e;p=pakfire.git build: Create and manage the Pakfire instance ourselves This allows us to pass arguments around easier and keep the configuration closer together. Signed-off-by: Michael Tremer --- diff --git a/src/cli/lib/build.c b/src/cli/lib/build.c index 239b03c3a..464d71785 100644 --- a/src/cli/lib/build.c +++ b/src/cli/lib/build.c @@ -146,7 +146,7 @@ static void log_callback(void* data, int priority, const char* file, int line, } int cli_build(void* data, int argc, char* argv[]) { - struct pakfire* pakfire = NULL; + struct cli_config* cli_config = data; struct pakfire_build* build = NULL; struct config config = { .id = NULL, @@ -160,8 +160,6 @@ int cli_build(void* data, int argc, char* argv[]) { int build_flags = 0; int r; - struct cli_config* cli_config = data; - // Parse the command line r = cli_parse(options, NULL, NULL, NULL, parse, 0, argc, argv, &config); if (r) @@ -170,14 +168,9 @@ int cli_build(void* data, int argc, char* argv[]) { // Replace the logger pakfire_ctx_set_log_callback(cli_config->ctx, log_callback, NULL); - // Set Pakfire flags + // Use the snapshot? if (config.flags & BUILD_ENABLE_SNAPSHOT) - cli_config->flags |= PAKFIRE_USE_SNAPSHOT; - - // Setup pakfire - r = cli_setup_pakfire(&pakfire, cli_config); - if (r) - goto ERROR; + build_flags |= PAKFIRE_BUILD_ENABLE_SNAPSHOT; // Is the build interactive? if (config.flags & BUILD_INTERACTIVE) @@ -192,7 +185,7 @@ int cli_build(void* data, int argc, char* argv[]) { build_flags |= PAKFIRE_BUILD_DISABLE_TESTS; // Setup the build environment - r = pakfire_build_create(&build, pakfire, config.id, build_flags); + r = pakfire_build_create(&build, cli_config->ctx, cli_config->arch, config.id, build_flags); if (r) { fprintf(stderr, "Could not setup the build environment: %m\n"); goto ERROR; @@ -220,8 +213,6 @@ int cli_build(void* data, int argc, char* argv[]) { ERROR: if (build) pakfire_build_unref(build); - if (pakfire) - pakfire_unref(pakfire); return r; } diff --git a/src/cli/lib/image_create.c b/src/cli/lib/image_create.c index 090f37eee..a4fa920fb 100644 --- a/src/cli/lib/image_create.c +++ b/src/cli/lib/image_create.c @@ -57,13 +57,11 @@ static error_t parse(int key, char* arg, struct argp_state* state, void* data) { } int cli_image_create(void* data, int argc, char* argv[]) { - struct pakfire* pakfire = NULL; + struct cli_config* cli_config = data; struct pakfire_build* build = NULL; FILE* f = NULL; int r; - struct cli_config* cli_config = data; - struct config config = { .type = NULL, .path = NULL, @@ -81,13 +79,8 @@ int cli_image_create(void* data, int argc, char* argv[]) { goto ERROR; } - // Setup pakfire - r = cli_setup_pakfire(&pakfire, cli_config); - if (r) - goto ERROR; - // Setup the build environment - r = pakfire_build_create(&build, pakfire, NULL, 0); + r = pakfire_build_create(&build, cli_config->ctx, cli_config->arch, NULL, 0); if (r) goto ERROR; @@ -99,8 +92,6 @@ int cli_image_create(void* data, int argc, char* argv[]) { ERROR: if (build) pakfire_build_unref(build); - if (pakfire) - pakfire_unref(pakfire); if (f) fclose(f); diff --git a/src/cli/lib/shell.c b/src/cli/lib/shell.c index d13972ec6..aee039c41 100644 --- a/src/cli/lib/shell.c +++ b/src/cli/lib/shell.c @@ -88,12 +88,11 @@ static error_t parse(int key, char* arg, struct argp_state* state, void* data) { } int cli_shell(void* data, int argc, char* argv[]) { - struct pakfire* pakfire = NULL; + struct cli_config* cli_config = data; struct pakfire_build* build = NULL; + int build_flags = PAKFIRE_BUILD_INTERACTIVE; int r; - struct cli_config* cli_config = data; - struct config config = { .flags = SHELL_ENABLE_SNAPSHOT, .argv = {}, @@ -109,15 +108,10 @@ int cli_shell(void* data, int argc, char* argv[]) { // Enable snapshots? if (config.flags & SHELL_ENABLE_SNAPSHOT) - cli_config->flags |= PAKFIRE_USE_SNAPSHOT; - - // Setup pakfire - r = cli_setup_pakfire(&pakfire, cli_config); - if (r) - goto ERROR; + build_flags |= PAKFIRE_BUILD_ENABLE_SNAPSHOT; // Setup the build environment - r = pakfire_build_create(&build, pakfire, NULL, PAKFIRE_BUILD_INTERACTIVE); + r = pakfire_build_create(&build, cli_config->ctx, cli_config->arch, NULL, build_flags); if (r) { fprintf(stderr, "Could not setup the build environment: %m\n"); goto ERROR; @@ -136,8 +130,6 @@ int cli_shell(void* data, int argc, char* argv[]) { ERROR: if (build) pakfire_build_unref(build); - if (pakfire) - pakfire_unref(pakfire); return r; } diff --git a/src/pakfire/build.c b/src/pakfire/build.c index 47b76587c..67effffbf 100644 --- a/src/pakfire/build.c +++ b/src/pakfire/build.c @@ -1993,8 +1993,24 @@ static int pakfire_build_set_time_start(struct pakfire_build* build) { return r; } +static int pakfire_build_setup_pakfire(struct pakfire_build* build, const char* arch) { + int flags = PAKFIRE_FLAGS_BUILD; + int r; + + // Enable snapshot? + if (build->flags & PAKFIRE_BUILD_ENABLE_SNAPSHOT) + flags |= PAKFIRE_USE_SNAPSHOT; + + // Create a new Pakfire instance + r = pakfire_create(&build->pakfire, build->ctx, NULL, arch, NULL, flags); + if (r < 0) + return r; + + return 0; +} + int pakfire_build_create(struct pakfire_build** build, - struct pakfire* pakfire, const char* id, int flags) { + struct pakfire_ctx* ctx, const char* arch, const char* id, int flags) { int r; // Allocate build object @@ -2003,10 +2019,7 @@ int pakfire_build_create(struct pakfire_build** build, return 1; // Reference the context - b->ctx = pakfire_ctx(pakfire); - - // Reference pakfire - b->pakfire = pakfire_ref(pakfire); + b->ctx = pakfire_ctx_ref(ctx); // Initialize reference counter b->nrefs = 1; @@ -2014,6 +2027,11 @@ int pakfire_build_create(struct pakfire_build** build, // Copy flags b->flags = flags; + // Setup Pakfire + r = pakfire_build_setup_pakfire(b, arch); + if (r < 0) + goto ERROR; + // Create an environment r = pakfire_env_create(&b->env, b->ctx); if (r < 0) diff --git a/src/pakfire/build.h b/src/pakfire/build.h index 5250aa024..fceb1422d 100644 --- a/src/pakfire/build.h +++ b/src/pakfire/build.h @@ -26,9 +26,10 @@ struct pakfire_build; enum pakfire_build_flags { - PAKFIRE_BUILD_INTERACTIVE = (1 << 0), - PAKFIRE_BUILD_DISABLE_CCACHE = (1 << 1), - PAKFIRE_BUILD_DISABLE_TESTS = (1 << 2), + PAKFIRE_BUILD_ENABLE_SNAPSHOT = (1 << 0), + PAKFIRE_BUILD_INTERACTIVE = (1 << 1), + PAKFIRE_BUILD_DISABLE_CCACHE = (1 << 2), + PAKFIRE_BUILD_DISABLE_TESTS = (1 << 3), }; typedef int (*pakfire_build_log_callback) @@ -36,7 +37,7 @@ typedef int (*pakfire_build_log_callback) const char* file, int line, const char* function, const char* format, ...); int pakfire_build_create(struct pakfire_build** build, - struct pakfire* pakfire, const char* id, int flags); + struct pakfire_ctx* ctx, const char* arch, const char* id, int flags); struct pakfire_build* pakfire_build_ref(struct pakfire_build* build); struct pakfire_build* pakfire_build_unref(struct pakfire_build* build); diff --git a/src/pakfire/job.c b/src/pakfire/job.c index 69e6e87ef..f337a359a 100644 --- a/src/pakfire/job.c +++ b/src/pakfire/job.c @@ -420,7 +420,6 @@ static void pakfire_job_log(void* data, int priority, const char* file, static int pakfire_job_child(struct pakfire_job* job) { struct pakfire_ctx* ctx = NULL; - struct pakfire* pakfire = NULL; struct pakfire_build* build = NULL; char job_id[UUID_STR_LEN]; FILE* conf = NULL; @@ -465,14 +464,6 @@ static int pakfire_job_child(struct pakfire_job* job) { goto ERROR; } - // Create a new Pakfire instance - r = pakfire_create(&pakfire, ctx, NULL, job->arch, conf, PAKFIRE_FLAGS_BUILD); - if (r) { - ERROR(ctx, "Could not initialize Pakfire: %m\n"); - r = -errno; - goto ERROR; - } - int build_flags = 0; // Disable the ccache @@ -480,7 +471,7 @@ static int pakfire_job_child(struct pakfire_job* job) { build_flags |= PAKFIRE_BUILD_DISABLE_CCACHE; // Create a new build environment - r = pakfire_build_create(&build, pakfire, job_id, build_flags); + r = pakfire_build_create(&build, ctx, job->arch, job_id, build_flags); if (r) { ERROR(ctx, "Could not setup the build environment: %m\n"); r = -errno; @@ -508,8 +499,6 @@ static int pakfire_job_child(struct pakfire_job* job) { ERROR: if (build) pakfire_build_unref(build); - if (pakfire) - pakfire_unref(pakfire); if (ctx) pakfire_ctx_unref(ctx); if (conf)