]> git.ipfire.org Git - pakfire.git/commitdiff
build: Split heavy lifting into a separate exec function
authorMichael Tremer <michael.tremer@ipfire.org>
Mon, 8 Aug 2022 16:29:23 +0000 (16:29 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Mon, 8 Aug 2022 16:29:23 +0000 (16:29 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/libpakfire/build.c
src/libpakfire/include/pakfire/build.h
src/libpakfire/libpakfire.sym

index ea0c36f15ae36ea5aa92de6e604b7fbe61b130f2..9c248b8307c0d609f45ca12b75c0f4e507d37a9c 100644 (file)
@@ -960,50 +960,22 @@ PAKFIRE_EXPORT struct pakfire_build* pakfire_build_unref(struct pakfire_build* b
        return NULL;
 }
 
-PAKFIRE_EXPORT int pakfire_build(struct pakfire* pakfire, const char* path,
-               const char* target, const char* id, int flags) {
-       char makefiles[PATH_MAX];
-       char cwd[PATH_MAX];
-       uuid_t build_id;
-       glob_t buffer;
-       int r = 1;
-
-       // Check for valid input
-       if (!path) {
-               errno = EINVAL;
-               return 1;
-       }
-
-       DEBUG(pakfire, "Building %s...\n", path);
-
-       // Try parsing the Build ID
-       if (id) {
-               r = uuid_parse(id, build_id);
-               if (r) {
-                       ERROR(pakfire, "Could not parse build ID %s\n", id);
-                       return r;
-               }
+PAKFIRE_EXPORT int pakfire_build_set_target(
+               struct pakfire_build* build, const char* target) {
+       pakfire_string_set(build->target, target);
 
-       // Otherwise initialize the Build ID with something random
-       } else {
-               uuid_generate_random(build_id);
-       }
+       return 0;
+}
 
-       // The default target is the local repository path
-       if (!target) {
-               struct pakfire_repo* repo = pakfire_get_repo(pakfire, PAKFIRE_REPO_LOCAL);
-               if (repo) {
-                       target = pakfire_repo_get_path(repo);
-                       pakfire_repo_unref(repo);
+PAKFIRE_EXPORT int pakfire_build_exec(struct pakfire_build* build, const char* path) {
+       char makefiles[PATH_MAX];
+       glob_t buffer;
+       int r;
 
-               // If the repository could not be found, just write to the cwd
-               } else {
-                       target = getcwd(cwd, sizeof(cwd));
-               }
-       }
+       INFO(build->pakfire, "Building %s...\n", path);
 
        // Setup build environment
-       r = pakfire_build_setup(pakfire);
+       r = pakfire_build_setup(build->pakfire);
        if (r)
                return r;
 
@@ -1012,31 +984,32 @@ PAKFIRE_EXPORT int pakfire_build(struct pakfire* pakfire, const char* path,
        };
 
        // Install the package into the build environment
-       r = pakfire_install(pakfire, 0, 0, packages, NULL, PAKFIRE_REQUEST_ESSENTIAL,
+       r = pakfire_install(build->pakfire, 0, 0, packages, NULL, PAKFIRE_REQUEST_ESSENTIAL,
                NULL, NULL, NULL);
        if (r) {
-               ERROR(pakfire, "Could not install %s\n", path);
-               goto ERROR;
+               ERROR(build->pakfire, "Could not install %s\n", path);
+               return r;
        }
 
        // Where are the makefiles located?
-       r = pakfire_make_path(pakfire, makefiles, "/usr/src/packages/*/*.nm");
+       r = pakfire_make_path(build->pakfire, makefiles, "/usr/src/packages/*/*.nm");
        if (r < 0)
                goto ERROR;
 
        // Find all makefiles
        r = glob(makefiles, 0, NULL, &buffer);
        if (r) {
-               ERROR(pakfire, "glob() on %s failed: %m\n", makefiles);
+               ERROR(build->pakfire, "glob() on %s failed: %m\n", makefiles);
                globfree(&buffer);
                goto ERROR;
        }
 
        // Iterate over all makefiles
        for (unsigned int i = 0; i < buffer.gl_pathc; i++) {
-               r = pakfire_build_makefile(pakfire, buffer.gl_pathv[i], target, &build_id, flags);
+               r = pakfire_build_makefile(build->pakfire, buffer.gl_pathv[i], build->target,
+                       &build->id, build->flags);
                if (r) {
-                       ERROR(pakfire, "Could not build %s: %m\n", buffer.gl_pathv[i]);
+                       ERROR(build->pakfire, "Could not build %s: %m\n", buffer.gl_pathv[i]);
                        globfree(&buffer);
                        goto ERROR;
                }
@@ -1046,6 +1019,42 @@ ERROR:
        return r;
 }
 
+/*
+       Compatibility function to keep the legacy API.
+*/
+PAKFIRE_EXPORT int pakfire_build(struct pakfire* pakfire, const char* path,
+               const char* target, const char* id, int flags) {
+       struct pakfire_build* build = NULL;
+       int r;
+
+       // Check if path is set
+       if (!path) {
+               errno = EINVAL;
+               return 1;
+       }
+
+       // Create a new build environment
+       r = pakfire_build_create(&build, pakfire, id, flags);
+       if (r)
+               goto ERROR;
+
+       // Set target
+       if (target) {
+               r = pakfire_build_set_target(build, target);
+               if (r)
+                       goto ERROR;
+       }
+
+       // Run build
+       r = pakfire_build_exec(build, path);
+
+ERROR:
+       if (build)
+               pakfire_build_unref(build);
+
+       return r;
+}
+
 /*
        This function enables the local repository so that it can be access by
        a pakfire instance running inside the chroot.
index 2af00c746768301ba8ad475be5dea71ee2394d25..0f313dc301923df88aaae26ebb0c65c48649d0f6 100644 (file)
@@ -31,6 +31,10 @@ int pakfire_build_create(struct pakfire_build** build,
 struct pakfire_build* pakfire_build_ref(struct pakfire_build* build);
 struct pakfire_build* pakfire_build_unref(struct pakfire_build* build);
 
+int pakfire_build_set_target(struct pakfire_build* build, const char* target);
+
+int pakfire_build_exec(struct pakfire_build* build, const char* path);
+
 int pakfire_build(struct pakfire* pakfire, const char* path, const char* target,
        const char* id, int flags);
 int pakfire_shell(struct pakfire* pakfire, const char** packages);
index d527125abe96c298ce2b82abe37165228b418ca0..2bc802e3536632c470726bdc5e035c0032ba0305 100644 (file)
@@ -71,7 +71,9 @@ global:
        # build
        pakfire_build;
        pakfire_build_create;
+       pakfire_build_exec;
        pakfire_build_ref;
+       pakfire_build_set_target;
        pakfire_build_unref;
        pakfire_shell;