]> git.ipfire.org Git - pakfire.git/commitdiff
build: Pass the configuration to Pakfire
authorMichael Tremer <michael.tremer@ipfire.org>
Sun, 5 Jan 2025 14:38:47 +0000 (14:38 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Sun, 5 Jan 2025 14:38:47 +0000 (14:38 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/cli/lib/build.c
src/cli/lib/config.c
src/cli/lib/config.h
src/cli/lib/image_create.c
src/cli/lib/shell.c
src/pakfire/build.c
src/pakfire/build.h
src/pakfire/job.c
src/pakfire/string.h

index 464d7178563eff4b07ddb1cb0e9039b83815b340..343d73fb197ee878802f209c69e0ad1ce059c9a5 100644 (file)
@@ -26,6 +26,7 @@
 #include "build.h"
 #include "color.h"
 #include "command.h"
+#include "config.h"
 #include "pakfire.h"
 
 #include <pakfire/build.h>
@@ -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;
 }
index 8e5d75f36269afa1ea7586f2929bfa4cf804f1da..a948e3a9561179dac23d55aac7fd026adb7ebf4f 100644 (file)
@@ -20,6 +20,7 @@
 
 #include <pakfire/ctx.h>
 #include <pakfire/config.h>
+#include <pakfire/string.h>
 
 #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;
+}
index f6a4da82f8f2b676aadf2a50b8c83e7c470a793a..2e63c92a8f9fc6b9c19da5bfb51e54b01a1fd9e1 100644 (file)
 #ifndef PAKFIRE_CLI_CONFIG_H
 #define PAKFIRE_CLI_CONFIG_H
 
+#include <pakfire/config.h>
 #include <pakfire/ctx.h>
 
 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 */
index a4fa920fbf79270a5d99d8ea543687e26f502744..202bd36685133fc82ce705050de7857701d901f2 100644 (file)
 #include <pakfire/pakfire.h>
 
 #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);
 
index aee039c41d6fc08052ec1c624948cb38d9cb7ee6..4f2d3c512c06f2b13d44e3dba95427f794085ea3 100644 (file)
@@ -21,6 +21,7 @@
 #include <argp.h>
 
 #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;
 }
index 67effffbfcf61835336a72bb533beeaa8dbb2d77..f9eb2ee0e4fb5566e715acc463b94c1d81fe19db 100644 (file)
@@ -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;
 
index fceb1422d662016e4c9d958669059164bcd207f2..f77fa3799f55f2db4a9972cae6c8574741de36c9 100644 (file)
@@ -21,7 +21,8 @@
 #ifndef PAKFIRE_BUILD_H
 #define PAKFIRE_BUILD_H
 
-#include <pakfire/pakfire.h>
+#include <pakfire/config.h>
+#include <pakfire/ctx.h>
 
 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);
index f337a359ab5b169339a6cbf93c18c3db37381857..56279c392364364f7b67e1b2e2e4efca4b0e0109 100644 (file)
@@ -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;
 }
index 42d72f00ae28f32b51fc4959748a49f0f00865a8..16e3d57fbe181093a295c026fd5077abfc5ef05c 100644 (file)
@@ -21,8 +21,6 @@
 #ifndef PAKFIRE_STRING_H
 #define PAKFIRE_STRING_H
 
-#ifdef PAKFIRE_PRIVATE
-
 #include <ctype.h>
 #include <errno.h>
 #include <stdarg.h>
@@ -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 */