]> git.ipfire.org Git - people/ric9/pakfire.git/commitdiff
build: Create buildroot outside the build environment
authorMichael Tremer <michael.tremer@ipfire.org>
Sun, 5 Jan 2025 17:06:08 +0000 (17:06 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Sun, 5 Jan 2025 17:06:08 +0000 (17:06 +0000)
This will help us to avoid all the path maths and we will simply
bind-mount the temporary directory into the build environment instead.

Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/pakfire/build.c
src/pakfire/stripper.c

index f9eb2ee0e4fb5566e715acc463b94c1d81fe19db..8e6c492af4abecb5880abc623c3fab8df9ac9d73 100644 (file)
@@ -497,7 +497,6 @@ static int pakfire_build_process_pkgconfig_requires(
 
 static int pakfire_build_find_pkgconfig_provides(
                struct pakfire_ctx* ctx, struct pakfire_file* file, struct pakfire_find_deps_ctx* deps) {
-       const char* buildroot = NULL;
        int r;
 
        // Fetch the absolute path
@@ -505,15 +504,10 @@ static int pakfire_build_find_pkgconfig_provides(
        if (!path)
                return -EINVAL;
 
-       // Fetch the path to the buildroot
-       buildroot = pakfire_relpath(deps->build->pakfire, deps->build->buildroot);
-       if (!buildroot)
-               return -EINVAL;
-
        const char* argv[] = {
                "pkg-config",
                "--print-provides",
-               pakfire_relpath(deps->build->pakfire, path),
+               path,
                NULL,
        };
 
@@ -528,8 +522,8 @@ static int pakfire_build_find_pkgconfig_provides(
 
 static int pakfire_build_find_pkgconfig_requires(
                struct pakfire_ctx* ctx, struct pakfire_file* file, struct pakfire_find_deps_ctx* deps) {
+       const char* buildroot = deps->build->buildroot;
        struct pakfire_env* env = NULL;
-       const char* buildroot = NULL;
        int r;
 
        // Fetch the absolute path
@@ -537,11 +531,6 @@ static int pakfire_build_find_pkgconfig_requires(
        if (!path)
                return -EINVAL;
 
-       // Fetch the path to the buildroot
-       buildroot = pakfire_relpath(deps->build->pakfire, deps->build->buildroot);
-       if (!buildroot)
-               return -EINVAL;
-
        // This package now requires pkgconfig
        r = pakfire_package_add_dep(deps->pkg, PAKFIRE_PKG_REQUIRES, "pkgconfig");
        if (r < 0)
@@ -556,7 +545,7 @@ static int pakfire_build_find_pkgconfig_requires(
                "pkg-config",
                "--print-requires",
                "--print-requires-private",
-               pakfire_relpath(deps->build->pakfire, path),
+               path,
                NULL,
        };
 
@@ -849,7 +838,7 @@ static int pakfire_build_find_deps(struct pakfire_build* build,
 
        // Pass the buildroot as first argument
        const char* args[] = {
-               pakfire_relpath(build->pakfire, build->buildroot),
+               build->buildroot,
                NULL,
        };
 
@@ -1304,8 +1293,6 @@ static int pakfire_build_packages(struct pakfire_build* build,
        INFO(build->ctx, "Creating packages...");
        int r = 1;
 
-       const char* buildroot = pakfire_relpath(build->pakfire, build->buildroot);
-
        // Fetch a list all all packages
        char** packages = pakfire_parser_list_namespaces(makefile, "packages.package:*");
        if (!packages) {
@@ -1323,7 +1310,7 @@ static int pakfire_build_packages(struct pakfire_build* build,
 
        // Build packages in reverse order
        for (int i = num_packages - 1; i >= 0; i--) {
-               r = pakfire_build_package(build, makefile, buildroot, packages[i]);
+               r = pakfire_build_package(build, makefile, build->buildroot, packages[i]);
                if (r)
                        goto ERROR;
        }
@@ -1665,12 +1652,10 @@ static const char* post_build_scripts[] = {
 };
 
 static int pakfire_build_run_post_build_scripts(struct pakfire_build* build) {
-       // Fetch buildroot
-       const char* buildroot = pakfire_relpath(build->pakfire, build->buildroot);
-
        // Set default arguments for build scripts
        const char* args[] = {
-               buildroot, NULL
+               build->buildroot,
+               NULL,
        };
 
        // Run them one by one
@@ -1726,6 +1711,10 @@ static void pakfire_build_free(struct pakfire_build* build) {
                pakfire_cgroup_unref(build->cgroup);
        }
 
+       // Remove buildroot
+       if (*build->buildroot)
+               pakfire_rmtree(build->buildroot, 0);
+
        if (build->pakfire)
                pakfire_unref(build->pakfire);
        if (build->ctx)
@@ -2010,6 +1999,38 @@ static int pakfire_build_setup_pakfire(
        return 0;
 }
 
+/*
+       This creates a new temporary directory that we will bind-mount into the
+       build environment. That will save us lots of path conversions because the
+       path will always be the same, both inside and outside of the jail.
+*/
+static int pakfire_build_setup_buildroot(struct pakfire_build* build) {
+       char path[PATH_MAX] = PAKFIRE_TMP_DIR "/pakfire-buildroot.XXXXXX";
+       const char* buildroot = NULL;
+       int r;
+
+       // Create BUILDROOT
+       buildroot = pakfire_mkdtemp(path);
+       if (!buildroot) {
+               ERROR(build->ctx, "Could not create BUILDROOT: %m\n");
+               return -errno;
+       }
+
+       // Store the path
+       r = pakfire_string_set(build->buildroot, buildroot);
+       if (r < 0)
+               return r;
+
+       // Bind-mount into the jail
+       r = pakfire_jail_bind(build->jail, buildroot, buildroot, MS_NOSUID|MS_NOEXEC|MS_NODEV);
+       if (r < 0) {
+               ERROR(build->ctx, "Could not mount %s into the jail: %s\n", buildroot, strerror(-r));
+               return r;
+       }
+
+       return 0;
+}
+
 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;
@@ -2028,16 +2049,6 @@ int pakfire_build_create(struct pakfire_build** build, struct pakfire_ctx* ctx,
        // Copy flags
        b->flags = flags;
 
-       // Setup Pakfire
-       r = pakfire_build_setup_pakfire(b, config, arch);
-       if (r < 0)
-               goto ERROR;
-
-       // Create an environment
-       r = pakfire_env_create(&b->env, b->ctx);
-       if (r < 0)
-               goto ERROR;
-
        // Store start time
        r = pakfire_build_set_time_start(b);
        if (r)
@@ -2048,6 +2059,16 @@ int pakfire_build_create(struct pakfire_build** build, struct pakfire_ctx* ctx,
        if (r)
                goto ERROR;
 
+       // Setup Pakfire
+       r = pakfire_build_setup_pakfire(b, config, arch);
+       if (r < 0)
+               goto ERROR;
+
+       // Create an environment
+       r = pakfire_env_create(&b->env, b->ctx);
+       if (r < 0)
+               goto ERROR;
+
        // Setup repo
        r = pakfire_build_setup_repo(b);
        if (r)
@@ -2063,6 +2084,11 @@ int pakfire_build_create(struct pakfire_build** build, struct pakfire_ctx* ctx,
        if (r)
                goto ERROR;
 
+       // Create the buildroot
+       r = pakfire_build_setup_buildroot(b);
+       if (r < 0)
+               goto ERROR;
+
        // Setup ccache
        r = pakfire_build_setup_ccache(b);
        if (r)
@@ -2205,9 +2231,9 @@ static int pakfire_build_read_makefile(struct pakfire_build* build,
        }
 
        // Set BUILDROOT
-       const char* buildroot = pakfire_relpath(build->pakfire, build->buildroot);
-       if (buildroot)
-               pakfire_parser_set(*parser, NULL, "BUILDROOT", buildroot, 0);
+       r = pakfire_parser_set(*parser, NULL, "BUILDROOT", build->buildroot, 0);
+       if (r < 0)
+               goto ERROR;
 
 ERROR:
        if (error)
@@ -2554,7 +2580,6 @@ static int pakfire_build_lint(struct pakfire_build* build) {
 int pakfire_build_exec(struct pakfire_build* build, const char* path) {
        struct pakfire_package* package = NULL;
        struct pakfire_parser* makefile = NULL;
-       char* buildroot = NULL;
        char* problems = NULL;
        char duration[TIME_STRING_MAX];
        int r;
@@ -2562,12 +2587,6 @@ int pakfire_build_exec(struct pakfire_build* build, const char* path) {
        // Fetch architecture
        const char* arch = pakfire_get_arch(build->pakfire);
 
-       // Set buildroot
-       r = pakfire_path(build->pakfire, build->buildroot, "%s",
-               PAKFIRE_TMP_DIR "/pakfire-buildroot.XXXXXX");
-       if (r)
-               goto ERROR;
-
        // Open the source package
        r = pakfire_commandline_add(build->pakfire, path, &package);
        if (r)
@@ -2611,13 +2630,6 @@ int pakfire_build_exec(struct pakfire_build* build, const char* path) {
                goto ERROR;
        }
 
-       // Create BUILDROOT
-       buildroot = pakfire_mkdtemp(build->buildroot);
-       if (!buildroot) {
-               ERROR(build->ctx, "Could not create BUILDROOT: %m\n");
-               goto ERROR;
-       }
-
        // Open the makefile
        r = pakfire_build_read_makefile(build, &makefile, package);
        if (r)
@@ -2665,10 +2677,6 @@ ERROR:
        if (problems)
                free(problems);
 
-       // Cleanup buildroot
-       if (buildroot)
-               pakfire_rmtree(buildroot, 0);
-
        return r;
 }
 
index 941da714fc78d9c0adacfeaa1884ecc8f2cbb005..ec4ae95d134962d8534c4a9ab0ea0d11e2785d2d 100644 (file)
@@ -319,18 +319,6 @@ ERROR:
        return r;
 }
 
-#define pakfire_stripper_build_id_path(stripper, buffer, build_id) \
-       __pakfire_stripper_build_id_path(stripper, buffer, sizeof(buffer), build_id)
-
-static int __pakfire_stripper_build_id_path(struct pakfire_stripper* stripper,
-               char* buffer, size_t length, const char* build_id) {
-       const char* buildroot = pakfire_relpath(stripper->pakfire, stripper->path);
-
-       return __pakfire_path(stripper->pakfire, buffer, length,
-               "%s/usr/lib/debug/.build-id/%c%c/%s.debug",
-               buildroot, build_id[0], build_id[1], build_id + 2);
-}
-
 static int pakfire_stripper_strip_debug_sections(struct pakfire_stripper* stripper,
                struct pakfire_file* file, struct pakfire_elf* elf) {
        const char* build_id = NULL;
@@ -352,7 +340,8 @@ static int pakfire_stripper_strip_debug_sections(struct pakfire_stripper* stripp
        }
 
        // Make Build ID path
-       r = pakfire_stripper_build_id_path(stripper, build_id_path, build_id);
+       r = pakfire_string_format(build_id_path, "%s/usr/lib/debug/.build-id/%c%c/%s.debug",
+                       stripper->path, build_id[0], build_id[1], build_id + 2);
        if (r < 0)
                goto ERROR;
 
@@ -380,8 +369,11 @@ static int pakfire_stripper_strip_debug_sections(struct pakfire_stripper* stripp
                // (Re-)compress everything using Zstandard
                "--compress-debug-sections=zstd",
 
-               pakfire_relpath(stripper->pakfire, path),
-               pakfire_relpath(stripper->pakfire, build_id_path),
+               // Source File
+               path,
+
+               // Debug Info File
+               build_id_path,
                NULL,
        };
 
@@ -410,9 +402,12 @@ static int pakfire_stripper_strip_debug_sections(struct pakfire_stripper* stripp
 
                // Add the debuglink
                "--remove-section=.gnu_debuglink",
-               "--add-gnu-debuglink", pakfire_relpath(stripper->pakfire, build_id_path),
+               "--add-gnu-debuglink", build_id_path,
+
+               // The source file
+               path,
 
-               pakfire_relpath(stripper->pakfire, path),
+               // The stripped output
                pakfire_relpath(stripper->pakfire, tmppath),
                NULL,
        };