From: Michael Tremer Date: Sun, 5 Jan 2025 14:38:47 +0000 (+0000) Subject: build: Pass the configuration to Pakfire X-Git-Tag: 0.9.30~530 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=90566df08deb39950d343e9f84d3451701f3886f;p=pakfire.git build: Pass the configuration to Pakfire Signed-off-by: Michael Tremer --- diff --git a/src/cli/lib/build.c b/src/cli/lib/build.c index 464d71785..343d73fb1 100644 --- a/src/cli/lib/build.c +++ b/src/cli/lib/build.c @@ -26,6 +26,7 @@ #include "build.h" #include "color.h" #include "command.h" +#include "config.h" #include "pakfire.h" #include @@ -34,6 +35,7 @@ #define MAX_MAKEFILES 32 struct config { + const char* distro; const char* id; const char* target; enum { @@ -49,15 +51,17 @@ struct config { }; enum { - OPT_DISABLE_CCACHE = 1, - OPT_DISABLE_SNAPSHOT = 2, - OPT_DISABLE_TESTS = 3, - OPT_ID = 4, - OPT_NON_INTERACTIVE = 5, - OPT_TARGET = 6, + OPT_DISTRO = 1, + OPT_DISABLE_CCACHE = 2, + OPT_DISABLE_SNAPSHOT = 3, + OPT_DISABLE_TESTS = 4, + OPT_ID = 5, + OPT_NON_INTERACTIVE = 6, + OPT_TARGET = 7, }; static struct argp_option options[] = { + { "distro", OPT_DISTRO, "DISTRO", 0, "Distribution to build for", 1 }, { "disable-ccache", OPT_DISABLE_CCACHE, NULL, 0, "Disable the ccache", 0 }, { "disable-snapshot", OPT_DISABLE_SNAPSHOT, NULL, 0, "Do not use the snapshot", 0 }, { "disable-tests", OPT_DISABLE_TESTS, NULL, 0, "Do not run tests", 0 }, @@ -71,6 +75,10 @@ static error_t parse(int key, char* arg, struct argp_state* state, void* data) { struct config* config = data; switch (key) { + case OPT_DISTRO: + config->distro = arg; + break; + case OPT_DISABLE_CCACHE: config->flags &= ~BUILD_ENABLE_CCACHE; break; @@ -148,7 +156,9 @@ static void log_callback(void* data, int priority, const char* file, int line, int cli_build(void* data, int argc, char* argv[]) { struct cli_config* cli_config = data; struct pakfire_build* build = NULL; + struct pakfire_config* c = NULL; struct config config = { + .distro = NULL, .id = NULL, .target = NULL, .flags = @@ -184,8 +194,13 @@ int cli_build(void* data, int argc, char* argv[]) { if (!(config.flags & BUILD_ENABLE_TESTS)) build_flags |= PAKFIRE_BUILD_DISABLE_TESTS; + // Read distro configuration + r = cli_read_distro_config(&c, cli_config->ctx, config.distro); + if (r < 0) + goto ERROR; + // Setup the build environment - r = pakfire_build_create(&build, cli_config->ctx, cli_config->arch, config.id, build_flags); + r = pakfire_build_create(&build, cli_config->ctx, c, cli_config->arch, config.id, build_flags); if (r) { fprintf(stderr, "Could not setup the build environment: %m\n"); goto ERROR; @@ -213,6 +228,8 @@ int cli_build(void* data, int argc, char* argv[]) { ERROR: if (build) pakfire_build_unref(build); + if (c) + pakfire_config_unref(c); return r; } diff --git a/src/cli/lib/config.c b/src/cli/lib/config.c index 8e5d75f36..a948e3a95 100644 --- a/src/cli/lib/config.c +++ b/src/cli/lib/config.c @@ -20,6 +20,7 @@ #include #include +#include #include "config.h" @@ -41,3 +42,41 @@ const char* cli_get_default_distro(struct pakfire_ctx* ctx) { return distro; } + +int cli_read_distro_config(struct pakfire_config** config, + struct pakfire_ctx* ctx, const char* distro) { + struct pakfire_config* c = NULL; + char path[PATH_MAX]; + int r; + + // Fetch default + if (!distro) + distro = cli_get_default_distro(ctx); + + // XXX hard-coded path + + // Make the path + r = pakfire_string_format(path, "/etc/pakfire/distros/%s.conf", distro); + if (r < 0) + return r; + + // Create the configuration + r = pakfire_config_create(&c); + if (r < 0) + goto ERROR; + + // Read the configuration + r = pakfire_config_read_path(c, path); + if (r < 0) + goto ERROR; + + // Return the configuration + *config = c; + return 0; + +ERROR: + if (c) + pakfire_config_unref(c); + + return r; +} diff --git a/src/cli/lib/config.h b/src/cli/lib/config.h index f6a4da82f..2e63c92a8 100644 --- a/src/cli/lib/config.h +++ b/src/cli/lib/config.h @@ -21,8 +21,12 @@ #ifndef PAKFIRE_CLI_CONFIG_H #define PAKFIRE_CLI_CONFIG_H +#include #include const char* cli_get_default_distro(struct pakfire_ctx* ctx); +int cli_read_distro_config( + struct pakfire_config** config, struct pakfire_ctx* ctx, const char* distro); + #endif /* PAKFIRE_CLI_CONFIG_H */ diff --git a/src/cli/lib/image_create.c b/src/cli/lib/image_create.c index a4fa920fb..202bd3668 100644 --- a/src/cli/lib/image_create.c +++ b/src/cli/lib/image_create.c @@ -24,10 +24,12 @@ #include #include "command.h" +#include "config.h" #include "image_create.h" #include "pakfire.h" struct config { + const char* distro; const char* type; const char* path; }; @@ -36,10 +38,23 @@ static const char* args_doc = "image create TYPE PATH"; static const char* doc = "Creates images"; +enum { + OPT_DISTRO = 1, +}; + +static struct argp_option options[] = { + { "distro", OPT_DISTRO, "DISTRO", 0, "Distribution to build for", 1 }, + { NULL }, +}; + static error_t parse(int key, char* arg, struct argp_state* state, void* data) { struct config* config = data; switch (key) { + case OPT_DISTRO: + config->distro = arg; + break; + case ARGP_KEY_ARG: if (!config->type) config->type = arg; @@ -58,6 +73,7 @@ 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_config* cli_config = data; + struct pakfire_config* c = NULL; struct pakfire_build* build = NULL; FILE* f = NULL; int r; @@ -68,10 +84,15 @@ int cli_image_create(void* data, int argc, char* argv[]) { }; // Parse the command line - r = cli_parse(NULL, NULL, args_doc, doc, parse, 0, argc, argv, &config); + r = cli_parse(options, NULL, args_doc, doc, parse, 0, argc, argv, &config); if (r) goto ERROR; + // Read the distro configuration + r = cli_read_distro_config(&c, cli_config->ctx, config.distro); + if (r < 0) + goto ERROR; + f = fopen(config.path, "w"); if (!f) { fprintf(stderr, "Could not open %s: %m\n", config.path); @@ -80,7 +101,7 @@ int cli_image_create(void* data, int argc, char* argv[]) { } // Setup the build environment - r = pakfire_build_create(&build, cli_config->ctx, cli_config->arch, NULL, 0); + r = pakfire_build_create(&build, cli_config->ctx, c, cli_config->arch, NULL, 0); if (r) goto ERROR; @@ -92,6 +113,8 @@ int cli_image_create(void* data, int argc, char* argv[]) { ERROR: if (build) pakfire_build_unref(build); + if (c) + pakfire_config_unref(c); if (f) fclose(f); diff --git a/src/cli/lib/shell.c b/src/cli/lib/shell.c index aee039c41..4f2d3c512 100644 --- a/src/cli/lib/shell.c +++ b/src/cli/lib/shell.c @@ -21,6 +21,7 @@ #include #include "command.h" +#include "config.h" #include "pakfire.h" #include "shell.h" @@ -31,6 +32,8 @@ #define MAX_PACKAGES 128 struct config { + const char* distro; + enum { SHELL_ENABLE_SNAPSHOT = (1 << 0), } flags; @@ -48,11 +51,13 @@ static const char* doc = "Creates a build environment and launches an interactive shell in it"; enum { - OPT_DISABLE_SNAPSHOT = 1, - OPT_INSTALL = 2, + OPT_DISTRO = 1, + OPT_DISABLE_SNAPSHOT = 2, + OPT_INSTALL = 3, }; static struct argp_option options[] = { + { "distro", OPT_DISTRO, "DISTRO", 0, "Distribution to build for", 1 }, { "disable-snapshot", OPT_DISABLE_SNAPSHOT, NULL, 0, "Do not use the snapshot", 0 }, { "install", OPT_INSTALL, "PACKAGE", 0, "Install this package", 0 }, { NULL }, @@ -62,6 +67,10 @@ static error_t parse(int key, char* arg, struct argp_state* state, void* data) { struct config* config = data; switch (key) { + case OPT_DISTRO: + config->distro = arg; + break; + case OPT_DISABLE_SNAPSHOT: config->flags &= ~SHELL_ENABLE_SNAPSHOT; break; @@ -89,6 +98,7 @@ static error_t parse(int key, char* arg, struct argp_state* state, void* data) { int cli_shell(void* data, int argc, char* argv[]) { struct cli_config* cli_config = data; + struct pakfire_config* c = NULL; struct pakfire_build* build = NULL; int build_flags = PAKFIRE_BUILD_INTERACTIVE; int r; @@ -110,8 +120,13 @@ int cli_shell(void* data, int argc, char* argv[]) { if (config.flags & SHELL_ENABLE_SNAPSHOT) build_flags |= PAKFIRE_BUILD_ENABLE_SNAPSHOT; + // Read distro configuration + r = cli_read_distro_config(&c, cli_config->ctx, config.distro); + if (r < 0) + goto ERROR; + // Setup the build environment - r = pakfire_build_create(&build, cli_config->ctx, cli_config->arch, NULL, build_flags); + r = pakfire_build_create(&build, cli_config->ctx, c, cli_config->arch, NULL, build_flags); if (r) { fprintf(stderr, "Could not setup the build environment: %m\n"); goto ERROR; @@ -130,6 +145,8 @@ int cli_shell(void* data, int argc, char* argv[]) { ERROR: if (build) pakfire_build_unref(build); + if (c) + pakfire_config_unref(c); return r; } diff --git a/src/pakfire/build.c b/src/pakfire/build.c index 67effffbf..f9eb2ee0e 100644 --- a/src/pakfire/build.c +++ b/src/pakfire/build.c @@ -1993,7 +1993,8 @@ 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) { +static int pakfire_build_setup_pakfire( + struct pakfire_build* build, struct pakfire_config* config, const char* arch) { int flags = PAKFIRE_FLAGS_BUILD; int r; @@ -2002,15 +2003,15 @@ static int pakfire_build_setup_pakfire(struct pakfire_build* build, const char* flags |= PAKFIRE_USE_SNAPSHOT; // Create a new Pakfire instance - r = pakfire_create(&build->pakfire, build->ctx, NULL, arch, NULL, flags); + r = pakfire_create(&build->pakfire, build->ctx, config, arch, NULL, flags); if (r < 0) return r; return 0; } -int pakfire_build_create(struct pakfire_build** build, - struct pakfire_ctx* ctx, const char* arch, const char* id, int flags) { +int pakfire_build_create(struct pakfire_build** build, struct pakfire_ctx* ctx, + struct pakfire_config* config, const char* arch, const char* id, int flags) { int r; // Allocate build object @@ -2028,7 +2029,7 @@ int pakfire_build_create(struct pakfire_build** build, b->flags = flags; // Setup Pakfire - r = pakfire_build_setup_pakfire(b, arch); + r = pakfire_build_setup_pakfire(b, config, arch); if (r < 0) goto ERROR; diff --git a/src/pakfire/build.h b/src/pakfire/build.h index fceb1422d..f77fa3799 100644 --- a/src/pakfire/build.h +++ b/src/pakfire/build.h @@ -21,7 +21,8 @@ #ifndef PAKFIRE_BUILD_H #define PAKFIRE_BUILD_H -#include +#include +#include struct pakfire_build; @@ -36,8 +37,8 @@ typedef int (*pakfire_build_log_callback) (struct pakfire_build* build, void* data, int priority, int r, const char* file, int line, const char* function, const char* format, ...); -int pakfire_build_create(struct pakfire_build** build, - struct pakfire_ctx* ctx, const char* arch, const char* id, int flags); +int pakfire_build_create(struct pakfire_build** build, struct pakfire_ctx* ctx, + struct pakfire_config* config, 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 f337a359a..56279c392 100644 --- a/src/pakfire/job.c +++ b/src/pakfire/job.c @@ -420,9 +420,10 @@ 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_config* config = NULL; struct pakfire_build* build = NULL; char job_id[UUID_STR_LEN]; - FILE* conf = NULL; + int build_flags = 0; int r; // Fetch our PID @@ -456,22 +457,24 @@ static int pakfire_job_child(struct pakfire_job* job) { // Setup logging pakfire_ctx_set_log_callback(ctx, pakfire_job_log, job); - // Map the configuration - conf = fmemopen(job->conf, strlen(job->conf), "r"); - if (!conf) { - ERROR(ctx, "Could not map the configuration into memory: %m\n"); - r = -errno; + // Create a new configuration object + r = pakfire_config_create(&config); + if (r < 0) goto ERROR; - } - int build_flags = 0; + // Parse the configuration + r = pakfire_config_parse(config, job->conf, -1); + if (r < 0) { + ERROR(ctx, "Could not parse configuration: %s\n", strerror(-r)); + goto ERROR; + } // Disable the ccache if (!(job->flags & PAKFIRE_JOB_CCACHE)) build_flags |= PAKFIRE_BUILD_DISABLE_CCACHE; // Create a new build environment - r = pakfire_build_create(&build, ctx, job->arch, job_id, build_flags); + r = pakfire_build_create(&build, ctx, config, job->arch, job_id, build_flags); if (r) { ERROR(ctx, "Could not setup the build environment: %m\n"); r = -errno; @@ -499,10 +502,10 @@ static int pakfire_job_child(struct pakfire_job* job) { ERROR: if (build) pakfire_build_unref(build); + if (config) + pakfire_config_unref(config); if (ctx) pakfire_ctx_unref(ctx); - if (conf) - fclose(conf); return r; } diff --git a/src/pakfire/string.h b/src/pakfire/string.h index 42d72f00a..16e3d57fb 100644 --- a/src/pakfire/string.h +++ b/src/pakfire/string.h @@ -21,8 +21,6 @@ #ifndef PAKFIRE_STRING_H #define PAKFIRE_STRING_H -#ifdef PAKFIRE_PRIVATE - #include #include #include @@ -125,6 +123,4 @@ time_t pakfire_string_parse_interval(const char* buffer); int pakfire_string_is_url(const char* s); -#endif - #endif /* PAKFIRE_STRING_H */