From: Michael Tremer Date: Sun, 5 Jan 2025 15:48:34 +0000 (+0000) Subject: CLI: Create a convenience function to set up a build environment X-Git-Tag: 0.9.30~525 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=d8751c8d0f3c38f552a454318aa94a2a866fc677;p=pakfire.git CLI: Create a convenience function to set up a build environment Signed-off-by: Michael Tremer --- diff --git a/src/cli/lib/build.c b/src/cli/lib/build.c index d7512823c..6995dbabc 100644 --- a/src/cli/lib/build.c +++ b/src/cli/lib/build.c @@ -26,7 +26,6 @@ #include "build.h" #include "color.h" #include "command.h" -#include "config.h" #include "pakfire.h" #include @@ -158,9 +157,8 @@ int cli_build(void* data, int argc, char* argv[]) { BUILD_ENABLE_SNAPSHOT | BUILD_ENABLE_TESTS, }; - struct pakfire_config* config = NULL; struct pakfire_build* build = NULL; - int build_flags = 0; + int flags = 0; int r; // Parse the command line @@ -173,31 +171,24 @@ int cli_build(void* data, int argc, char* argv[]) { // Use the snapshot? if (local_args.flags & BUILD_ENABLE_SNAPSHOT) - build_flags |= PAKFIRE_BUILD_ENABLE_SNAPSHOT; + flags |= PAKFIRE_BUILD_ENABLE_SNAPSHOT; // Is the build interactive? if (local_args.flags & BUILD_INTERACTIVE) - build_flags |= PAKFIRE_BUILD_INTERACTIVE; + flags |= PAKFIRE_BUILD_INTERACTIVE; // Enable ccache? if (!(local_args.flags & BUILD_ENABLE_CCACHE)) - build_flags |= PAKFIRE_BUILD_DISABLE_CCACHE; + flags |= PAKFIRE_BUILD_DISABLE_CCACHE; // Enable tests? if (!(local_args.flags & BUILD_ENABLE_TESTS)) - build_flags |= PAKFIRE_BUILD_DISABLE_TESTS; - - // Read configuration - r = cli_setup_config(&config, global_args); - if (r < 0) - goto ERROR; + flags |= PAKFIRE_BUILD_DISABLE_TESTS; // Setup the build environment - r = pakfire_build_create(&build, global_args->ctx, config, global_args->arch, local_args.id, build_flags); - if (r) { - fprintf(stderr, "Could not setup the build environment: %m\n"); + r = cli_setup_build(&build, global_args, flags); + if (r < 0) goto ERROR; - } // Set target if (local_args.target) { @@ -221,8 +212,6 @@ int cli_build(void* data, int argc, char* argv[]) { ERROR: if (build) pakfire_build_unref(build); - if (config) - pakfire_config_unref(config); return r; } diff --git a/src/cli/lib/image_create.c b/src/cli/lib/image_create.c index 50f7d700b..e82d7670b 100644 --- a/src/cli/lib/image_create.c +++ b/src/cli/lib/image_create.c @@ -24,7 +24,6 @@ #include #include "command.h" -#include "config.h" #include "image_create.h" #include "pakfire.h" @@ -61,7 +60,6 @@ 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 cli_global_args* global_args = data; struct cli_local_args local_args = {}; - struct pakfire_config* config = NULL; struct pakfire_build* build = NULL; FILE* f = NULL; int r; @@ -71,11 +69,6 @@ int cli_image_create(void* data, int argc, char* argv[]) { if (r) goto ERROR; - // Read configuration - r = cli_setup_config(&config, global_args); - if (r < 0) - goto ERROR; - f = fopen(local_args.path, "w"); if (!f) { fprintf(stderr, "Could not open %s: %m\n", local_args.path); @@ -84,8 +77,8 @@ int cli_image_create(void* data, int argc, char* argv[]) { } // Setup the build environment - r = pakfire_build_create(&build, global_args->ctx, config, global_args->arch, NULL, 0); - if (r) + r = cli_setup_build(&build, global_args, 0); + if (r < 0) goto ERROR; // Create the image @@ -96,8 +89,6 @@ int cli_image_create(void* data, int argc, char* argv[]) { ERROR: if (build) pakfire_build_unref(build); - if (config) - pakfire_config_unref(config); if (f) fclose(f); diff --git a/src/cli/lib/pakfire.c b/src/cli/lib/pakfire.c index bed149474..2764cac60 100644 --- a/src/cli/lib/pakfire.c +++ b/src/cli/lib/pakfire.c @@ -21,13 +21,13 @@ #include #include +#include #include #include #include #include "config.h" #include "pakfire.h" -#include "progressbar.h" static void cli_set_repo_enabled(struct pakfire* pakfire, const char* name, int enabled) { struct pakfire_repo* repo = NULL; @@ -111,3 +111,30 @@ ERROR: return r; } + +int cli_setup_build(struct pakfire_build** build, struct cli_global_args* args, int flags) { + struct pakfire_config* config = NULL; + struct pakfire_build* b = NULL; + int r; + + // Setup the configuration + r = cli_setup_config(&config, args); + if (r < 0) + goto ERROR; + + // Setup the build environment + r = pakfire_build_create(&b, args->ctx, config, args->arch, NULL, flags); + if (r < 0) { + fprintf(stderr, "Could not setup the build environment: %s\n", strerror(-r)); + goto ERROR; + } + + // Return pointer + *build = b; + +ERROR: + if (config) + pakfire_config_unref(config); + + return r; +} diff --git a/src/cli/lib/pakfire.h b/src/cli/lib/pakfire.h index 5214e39b2..47fef8ce3 100644 --- a/src/cli/lib/pakfire.h +++ b/src/cli/lib/pakfire.h @@ -21,6 +21,7 @@ #ifndef PAKFIRE_CLI_PAKFIRE_H #define PAKFIRE_CLI_PAKFIRE_H +#include #include #include #include @@ -46,5 +47,6 @@ struct cli_global_args { int cli_setup_config(struct pakfire_config** config, struct cli_global_args* args); int cli_setup_pakfire(struct pakfire** pakfire, struct cli_global_args* args); +int cli_setup_build(struct pakfire_build** build, struct cli_global_args* args, int flags); #endif /* PAKFIRE_CLI_PAKFIRE_H */ diff --git a/src/cli/lib/shell.c b/src/cli/lib/shell.c index cc6df2e8a..8c5bfdc38 100644 --- a/src/cli/lib/shell.c +++ b/src/cli/lib/shell.c @@ -21,7 +21,6 @@ #include #include "command.h" -#include "config.h" #include "pakfire.h" #include "shell.h" @@ -97,9 +96,8 @@ int cli_shell(void* data, int argc, char* argv[]) { .packages = {}, .num_packages = 0, }; - struct pakfire_config* config = NULL; struct pakfire_build* build = NULL; - int build_flags = PAKFIRE_BUILD_INTERACTIVE; + int flags = PAKFIRE_BUILD_INTERACTIVE; int r; // Parse the command line @@ -109,19 +107,12 @@ int cli_shell(void* data, int argc, char* argv[]) { // Enable snapshots? if (local_args.flags & SHELL_ENABLE_SNAPSHOT) - build_flags |= PAKFIRE_BUILD_ENABLE_SNAPSHOT; - - // Read configuration - r = cli_setup_config(&config, global_args); - if (r < 0) - goto ERROR; + flags |= PAKFIRE_BUILD_ENABLE_SNAPSHOT; // Setup the build environment - r = pakfire_build_create(&build, global_args->ctx, config, global_args->arch, NULL, build_flags); - if (r) { - fprintf(stderr, "Could not setup the build environment: %m\n"); + r = cli_setup_build(&build, global_args, flags); + if (r < 0) goto ERROR; - } // Install any additional packages if (local_args.num_packages) { @@ -136,8 +127,6 @@ int cli_shell(void* data, int argc, char* argv[]) { ERROR: if (build) pakfire_build_unref(build); - if (config) - pakfire_config_unref(config); return r; }