1 /*#############################################################################
3 # Pakfire - The IPFire package management system #
4 # Copyright (C) 2021 Pakfire development team #
6 # This program is free software: you can redistribute it and/or modify #
7 # it under the terms of the GNU General Public License as published by #
8 # the Free Software Foundation, either version 3 of the License, or #
9 # (at your option) any later version. #
11 # This program is distributed in the hope that it will be useful, #
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of #
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
14 # GNU General Public License for more details. #
16 # You should have received a copy of the GNU General Public License #
17 # along with this program. If not, see <http://www.gnu.org/licenses/>. #
19 #############################################################################*/
23 #include <sys/mount.h>
25 #include <uuid/uuid.h>
27 #define PCRE2_CODE_UNIT_WIDTH 8
30 #include <pakfire/build.h>
31 #include <pakfire/cgroup.h>
32 #include <pakfire/dependencies.h>
33 #include <pakfire/dist.h>
34 #include <pakfire/file.h>
35 #include <pakfire/i18n.h>
36 #include <pakfire/jail.h>
37 #include <pakfire/logging.h>
38 #include <pakfire/mount.h>
39 #include <pakfire/package.h>
40 #include <pakfire/packager.h>
41 #include <pakfire/parser.h>
42 #include <pakfire/private.h>
43 #include <pakfire/problem.h>
44 #include <pakfire/repo.h>
45 #include <pakfire/request.h>
46 #include <pakfire/scriptlet.h>
47 #include <pakfire/snapshot.h>
48 #include <pakfire/solution.h>
49 #include <pakfire/string.h>
50 #include <pakfire/util.h>
52 #define CCACHE_DIR "/var/cache/ccache"
54 // We guarantee 2 GiB of memory to every build container
55 #define PAKFIRE_BUILD_MEMORY_GUARANTEED (size_t)2 * 1024 * 1024 * 1024
57 // We allow only up to 2048 processes/threads for every build container
58 #define PAKFIRE_BUILD_PID_LIMIT (size_t)2048
60 struct pakfire_build
{
61 struct pakfire
* pakfire
;
69 char _id
[UUID_STR_LEN
];
71 char target
[PATH_MAX
];
74 struct pakfire_cgroup
* cgroup
;
77 struct pakfire_jail
* jail
;
79 // The build repository
80 struct pakfire_repo
* repo
;
82 // A list of all built packages
83 struct pakfire_packagelist
* packages
;
86 char buildroot
[PATH_MAX
];
93 "#!/bin/bash --login\n" \
102 static int pakfire_build_has_flag(struct pakfire_build
* build
, int flag
) {
103 return build
->flags
& flag
;
106 static int __pakfire_build_setup_repo(struct pakfire
* pakfire
,
107 struct pakfire_repo
* repo
, void* p
) {
112 struct pakfire_build
* build
= (struct pakfire_build
*)p
;
114 // Skip processing the installed repository
115 if (pakfire_repo_is_installed_repo(repo
))
118 // Skip processing any other internal repositories
119 if (pakfire_repo_is_internal(repo
))
122 const char* name
= pakfire_repo_get_name(repo
);
124 DEBUG(pakfire
, "Exporting repository configuration for '%s'\n", name
);
126 // Make path for configuration file
127 r
= pakfire_path(build
->pakfire
, path
, PAKFIRE_CONFIG_DIR
"/repos/%s.repo", name
);
129 ERROR(pakfire
, "Could not make repository configuration path for %s: %m\n", name
);
133 // Create the parent directory
134 r
= pakfire_mkparentdir(path
, 0755);
138 // Open the repository configuration
139 f
= fopen(path
, "w");
141 ERROR(pakfire
, "Could not open %s for writing: %m\n", path
);
145 // Write repository configuration
146 r
= pakfire_repo_write_config(repo
, f
);
148 ERROR(pakfire
, "Could not write repository configuration for %s: %m\n", name
);
152 // Bind-mount any local repositories
153 if (pakfire_repo_is_local(repo
)) {
154 const char* _path
= pakfire_repo_get_path(repo
);
156 // Bind-mount the repository data read-only
157 r
= pakfire_jail_bind(build
->jail
, _path
, _path
, MS_RDONLY
);
159 ERROR(pakfire
, "Could not bind-mount the repository at %s: %m\n", _path
);
172 This function enables the local repository so that it can be accessed by
173 a pakfire instance running inside the chroot.
175 static int pakfire_build_enable_repos(struct pakfire_build
* build
) {
176 return pakfire_repo_walk(build
->pakfire
, __pakfire_build_setup_repo
, build
);
180 Drops the user into a shell
182 static int pakfire_build_shell(struct pakfire_build
* build
) {
185 // Export local repository if running in interactive mode
186 r
= pakfire_build_enable_repos(build
);
191 return pakfire_jail_shell(build
->jail
);
194 static int pakfire_build_read_script(struct pakfire_build
* build
,
195 const char* filename
, char** buffer
, size_t* length
) {
200 // Compose the source path
201 r
= pakfire_path_join(path
, PAKFIRE_SCRIPTS_DIR
, filename
);
203 ERROR(build
->pakfire
, "Could not compose path for script '%s': %m\n", filename
);
207 DEBUG(build
->pakfire
, "Reading script from %s...\n", path
);
210 f
= fopen(path
, "r");
212 ERROR(build
->pakfire
, "Could not open script %s: %m\n", path
);
216 // Read the file into a the buffer
217 r
= pakfire_read_file_into_buffer(f
, buffer
, length
);
219 ERROR(build
->pakfire
, "Could not read script: %m\n");
230 static int pakfire_build_run_script(
231 struct pakfire_build
* build
,
232 const char* filename
,
234 pakfire_jail_communicate_in communicate_in
,
235 pakfire_jail_communicate_out communicate_out
,
242 DEBUG(build
->pakfire
, "Running build script '%s'...\n", filename
);
245 r
= pakfire_build_read_script(build
, filename
, &script
, &length
);
247 ERROR(build
->pakfire
, "Could not read script %s: %m\n", filename
);
251 // Execute the script
252 r
= pakfire_jail_exec_script(build
->jail
, script
, length
, args
,
253 communicate_in
, communicate_out
, data
);
255 ERROR(build
->pakfire
, "Script '%s' failed with status %d\n", filename
, r
);
264 struct pakfire_find_deps_ctx
{
265 struct pakfire_package
* pkg
;
267 struct pakfire_scriptlet
* scriptlet
;
269 const pcre2_code
* filter
;
271 struct pakfire_filelist
* filelist
;
275 static int pakfire_build_make_filter(struct pakfire_build
* build
, pcre2_code
** regex
,
276 struct pakfire_parser
* makefile
, const char* namespace, const char* filter
) {
277 char* pattern
= NULL
;
281 pattern
= pakfire_parser_get(makefile
, namespace, filter
);
283 // Nothing if to if there is no or an empty pattern
284 if (!pattern
|| !*pattern
)
287 DEBUG(build
->pakfire
, "Found filter for %s: %s\n", filter
, pattern
);
289 // Compile the regular expression
290 r
= pakfire_compile_regex(build
->pakfire
, regex
, pattern
);
301 static int pakfire_build_send_filelist(struct pakfire
* pakfire
, void* data
, int fd
) {
302 struct pakfire_find_deps_ctx
* ctx
= (struct pakfire_find_deps_ctx
*)data
;
303 struct pakfire_file
* file
= NULL
;
306 const size_t length
= pakfire_filelist_size(ctx
->filelist
);
308 // Check if we have reached the end of the filelist
309 if (ctx
->i
>= length
)
312 // Fetch the next file
313 file
= pakfire_filelist_get(ctx
->filelist
, ctx
->i
);
315 DEBUG(pakfire
, "Could not fetch file %d: %m\n", ctx
->i
);
320 // Fetch the path of the file
321 const char* path
= pakfire_file_get_path(file
);
323 ERROR(pakfire
, "Received a file with an empty path\n");
328 // Skip files that don't match what we are looking for
329 if (ctx
->class && !pakfire_file_matches_class(file
, ctx
->class))
332 // Write path to stdin
333 r
= dprintf(fd
, "%s\n", path
);
338 // Move on to the next file
346 pakfire_file_unref(file
);
351 static int pakfire_build_process_deps(struct pakfire
* pakfire
,
352 void* data
, int priority
, const char* buffer
, const size_t length
) {
353 const struct pakfire_find_deps_ctx
* ctx
= (struct pakfire_find_deps_ctx
*)data
;
355 pcre2_match_data
* match
= NULL
;
358 // Nothing to do for an empty buffer
359 if (!buffer
|| !*buffer
)
363 // Add every dependency that we have received
365 // Copy the dependency to the stack (and remove the trailing newline)
366 r
= pakfire_string_format(dep
, "%.*s", (int)length
- 1, buffer
);
370 DEBUG(pakfire
, "Processing dependency: %s\n", dep
);
372 // Filter out any dependencies that are provided by this package
373 if (ctx
->dep
== PAKFIRE_PKG_REQUIRES
) {
374 // If this is a file, we check if it is on the filelist
375 if (pakfire_filelist_contains(ctx
->filelist
, dep
))
378 // Otherwise check if this dependency is provided by this package
379 else if (pakfire_package_matches_dep(ctx
->pkg
, PAKFIRE_PKG_PROVIDES
, dep
))
383 // Check if this dependency should be filtered
385 match
= pcre2_match_data_create_from_pattern(ctx
->filter
, NULL
);
387 ERROR(pakfire
, "Could not allocate PCRE match data: %m\n");
392 r
= pcre2_jit_match(ctx
->filter
, (PCRE2_SPTR
)dep
, length
- 1,
396 if (r
== PCRE2_ERROR_NOMATCH
) {
403 // Fetch the error message
404 r
= pcre2_get_error_message(r
, (PCRE2_UCHAR
*)error
, sizeof(error
));
406 ERROR(pakfire
, "Could not fetch PCRE error message: %m\n");
411 ERROR(pakfire
, "Could not match the filter: %s\n", error
);
417 DEBUG(pakfire
, "Skipping dependency that has been filtered: %s\n", dep
);
424 r
= pakfire_package_add_dep(ctx
->pkg
, ctx
->dep
, buffer
);
426 ERROR(pakfire
, "Could not process dependency '%s': %m\n", buffer
);
431 // Send everything else to the default logger
433 ERROR(pakfire
, "%s\n", buffer
);
440 DEBUG(pakfire
, "Skipping dependency that is provided by the package itself: %s\n", dep
);
444 pcre2_match_data_free(match
);
450 This function is a special way to run a script.
452 It will pipe the filelist into the standard input of the called script
453 and will read any dependencies from the standard output.
455 static int pakfire_build_find_deps(struct pakfire_build
* build
,
456 struct pakfire_package
* pkg
, int dep
, const char* script
,
457 struct pakfire_filelist
* filelist
, const int class, const pcre2_code
* filter
) {
459 // Construct the context
460 struct pakfire_find_deps_ctx ctx
= {
467 .filelist
= filelist
,
472 // Pass the buildroot as first argument
473 const char* args
[] = {
474 pakfire_relpath(build
->pakfire
, build
->buildroot
),
479 r
= pakfire_build_run_script(build
, script
, args
,
480 pakfire_build_send_filelist
, pakfire_build_process_deps
, &ctx
);
482 ERROR(build
->pakfire
, "%s returned with error %d\n", script
, r
);
487 static int pakfire_build_find_dependencies(struct pakfire_build
* build
,
488 struct pakfire_parser
* makefile
, const char* namespace,
489 struct pakfire_package
* pkg
, struct pakfire_filelist
* filelist
) {
490 pcre2_code
* filter_provides
= NULL
;
491 pcre2_code
* filter_requires
= NULL
;
494 // Fetch the provides filter
495 r
= pakfire_build_make_filter(build
, &filter_provides
,
496 makefile
, namespace, "filter_provides");
498 ERROR(build
->pakfire
, "Provides filter is broken: %m\n");
502 // Fetch the requires filter
503 r
= pakfire_build_make_filter(build
, &filter_requires
,
504 makefile
, namespace, "filter_requires");
506 ERROR(build
->pakfire
, "Requires filter is broken: %m\n");
511 r
= pakfire_build_find_deps(build
, pkg
,
512 PAKFIRE_PKG_PROVIDES
, "find-provides", filelist
, 0, filter_provides
);
516 // Find all Perl provides
517 r
= pakfire_build_find_deps(build
, pkg
,
518 PAKFIRE_PKG_PROVIDES
, "perl.prov", filelist
, PAKFIRE_FILE_PERL
, filter_provides
);
523 r
= pakfire_build_find_deps(build
, pkg
,
524 PAKFIRE_PKG_REQUIRES
, "find-requires", filelist
, 0, filter_requires
);
528 // Find all Perl requires
529 r
= pakfire_build_find_deps(build
, pkg
,
530 PAKFIRE_PKG_REQUIRES
, "perl.req", filelist
, PAKFIRE_FILE_PERL
, filter_requires
);
536 pcre2_code_free(filter_provides
);
538 pcre2_code_free(filter_requires
);
543 static int append_to_array(char*** array
, const char* s
) {
544 unsigned int length
= 0;
546 // Determine the length of the existing array
548 for (char** element
= *array
; *element
; element
++)
553 *array
= reallocarray(*array
, length
+ 2, sizeof(**array
));
557 // Copy the string to the heap
562 // Append p and terminate the array
563 (*array
)[length
] = p
;
564 (*array
)[length
+ 1] = NULL
;
569 static int pakfire_build_package_add_files(struct pakfire_build
* build
,
570 struct pakfire_parser
* makefile
, const char* buildroot
, const char* namespace,
571 struct pakfire_package
* pkg
, struct pakfire_packager
* packager
) {
572 struct pakfire_filelist
* filelist
= NULL
;
575 char** includes
= NULL
;
576 char** excludes
= NULL
;
579 // Fetch filelist from makefile
580 char* files
= pakfire_parser_get(makefile
, namespace, "files");
582 // No files to package?
586 const char* file
= strtok_r(files
, " \n", &p
);
588 // Split into includes and excludes
591 r
= append_to_array(&excludes
, file
+ 1);
593 r
= append_to_array(&includes
, file
);
597 // Move on to the next token
598 file
= strtok_r(NULL
, " \n", &p
);
601 // Allocate a new filelist
602 r
= pakfire_filelist_create(&filelist
, build
->pakfire
);
607 r
= pakfire_filelist_scan(filelist
, build
->buildroot
,
608 (const char**)includes
, (const char**)excludes
);
612 const size_t length
= pakfire_filelist_size(filelist
);
613 DEBUG(build
->pakfire
, "%zu file(s) found\n", length
);
615 // Nothing to do if the filelist is empty
620 pakfire_filelist_dump(filelist
, 1);
623 r
= pakfire_build_find_dependencies(build
, makefile
, namespace, pkg
, filelist
);
625 ERROR(build
->pakfire
, "Finding dependencies failed: %m\n");
629 // Add all files to the package
630 r
= pakfire_packager_add_files(packager
, filelist
);
636 pakfire_filelist_unref(filelist
);
640 for (char** include
= includes
; *include
; include
++)
645 for (char** exclude
= excludes
; *exclude
; exclude
++)
653 static int pakfire_build_send_scriptlet(struct pakfire
* pakfire
, void* data
, int fd
) {
654 const struct pakfire_find_deps_ctx
* ctx
= (struct pakfire_find_deps_ctx
*)data
;
657 // Fetch the scriptlet
658 const char* p
= pakfire_scriptlet_get_data(ctx
->scriptlet
, &length
);
660 ERROR(pakfire
, "Could not fetch scriptlet: %m\n");
664 // Write it into the pipe
665 ssize_t bytes_written
= write(fd
, p
, length
);
666 if (bytes_written
< 0) {
667 ERROR(pakfire
, "Could not send scriptlet: %m\n");
674 static int pakfire_build_add_scriptlet_requires(struct pakfire_build
* build
,
675 struct pakfire_package
* pkg
, struct pakfire_scriptlet
* scriptlet
) {
678 struct pakfire_find_deps_ctx ctx
= {
680 .dep
= PAKFIRE_PKG_PREREQUIRES
,
681 .scriptlet
= scriptlet
,
684 // Find all pre-requires
685 r
= pakfire_build_run_script(build
, "find-prerequires", NULL
,
686 pakfire_build_send_scriptlet
, pakfire_build_process_deps
, &ctx
);
694 static int pakfire_build_package_add_scriptlet(struct pakfire_build
* build
,
695 struct pakfire_package
* pkg
, struct pakfire_packager
* packager
,
696 const char* type
, const char* data
) {
697 struct pakfire_scriptlet
* scriptlet
= NULL
;
701 // Wrap scriptlet into a shell script
702 r
= asprintf(&shell
, "#!/bin/sh\n\nset -e\n\n%s\n\nexit 0\n", data
);
706 // Create a scriptlet
707 r
= pakfire_scriptlet_create(&scriptlet
, build
->pakfire
, type
, shell
, 0);
711 // Add it to the package
712 r
= pakfire_packager_add_scriptlet(packager
, scriptlet
);
714 ERROR(build
->pakfire
, "Could not add scriptlet %s\n", type
);
718 // Add scriptlet requirements
719 r
= pakfire_build_add_scriptlet_requires(build
, pkg
, scriptlet
);
721 ERROR(build
->pakfire
, "Could not add scriptlet requirements: %m\n");
730 pakfire_scriptlet_unref(scriptlet
);
737 static int pakfire_build_package_add_scriptlets(struct pakfire_build
* build
,
738 struct pakfire_parser
* makefile
, const char* namespace,
739 struct pakfire_package
* pkg
, struct pakfire_packager
* packager
) {
743 for (const char** type
= pakfire_scriptlet_types
; *type
; type
++) {
744 r
= pakfire_string_format(name
, "scriptlet:%s", *type
);
748 // Fetch the scriptlet
749 char* data
= pakfire_parser_get(makefile
, namespace, name
);
753 // Add it to the package
754 r
= pakfire_build_package_add_scriptlet(build
, pkg
, packager
, *type
, data
);
766 static int pakfire_build_package(struct pakfire_build
* build
, struct pakfire_parser
* makefile
,
767 const char* buildroot
, const char* namespace) {
768 struct pakfire_package
* pkg
= NULL
;
769 struct pakfire_packager
* packager
= NULL
;
773 // Expand the handle into the package name
774 char* name
= pakfire_parser_expand(makefile
, "packages", namespace);
776 ERROR(build
->pakfire
, "Could not get package name: %m\n");
780 INFO(build
->pakfire
, "Building package '%s'...\n", name
);
781 DEBUG(build
->pakfire
, " buildroot = %s\n", buildroot
);
783 // Fetch build architecture
784 const char* arch
= pakfire_get_arch(build
->pakfire
);
788 // Fetch package from makefile
789 r
= pakfire_parser_create_package(makefile
, &pkg
, NULL
, namespace, arch
);
791 ERROR(build
->pakfire
, "Could not create package from makefile: %m\n");
796 const char* distribution
= pakfire_parser_get(makefile
, NULL
, "DISTRO_NAME");
798 r
= pakfire_package_set_string(pkg
, PAKFIRE_PKG_DISTRO
, distribution
);
804 pakfire_package_set_uuid(pkg
, PAKFIRE_PKG_BUILD_ID
, build
->id
);
806 // Set source package
807 const char* source_name
= pakfire_parser_get(makefile
, NULL
, "name");
809 r
= pakfire_package_set_string(pkg
, PAKFIRE_PKG_SOURCE_NAME
, source_name
);
815 const char* source_evr
= pakfire_parser_get(makefile
, NULL
, "evr");
817 r
= pakfire_package_set_string(pkg
, PAKFIRE_PKG_SOURCE_EVR
, source_evr
);
823 r
= pakfire_package_set_string(pkg
, PAKFIRE_PKG_SOURCE_ARCH
, "src");
828 r
= pakfire_packager_create(&packager
, build
->pakfire
, pkg
);
833 r
= pakfire_build_package_add_files(build
, makefile
, buildroot
, namespace,
839 r
= pakfire_build_package_add_scriptlets(build
, makefile
, namespace,
844 const char* path
= pakfire_repo_get_path(build
->repo
);
846 // Write the finished package
847 r
= pakfire_packager_finish_to_directory(packager
, path
, NULL
);
849 ERROR(build
->pakfire
, "pakfire_packager_finish() failed: %m\n");
853 // Cleanup all packaged files
854 r
= pakfire_packager_cleanup(packager
);
863 pakfire_packager_unref(packager
);
865 pakfire_package_unref(pkg
);
872 static int pakfire_build_package_dump(struct pakfire
* pakfire
,
873 struct pakfire_package
* pkg
, void* p
) {
874 char* dump
= pakfire_package_dump(pkg
, PAKFIRE_PKG_DUMP_LONG
);
878 INFO(pakfire
, "%s\n", dump
);
884 static int pakfire_build_packages(struct pakfire_build
* build
,
885 struct pakfire_parser
* makefile
) {
886 DEBUG(build
->pakfire
, "Creating packages...");
889 const char* buildroot
= pakfire_relpath(build
->pakfire
, build
->buildroot
);
891 // Fetch a list all all packages
892 char** packages
= pakfire_parser_list_namespaces(makefile
, "packages.package:*");
894 ERROR(build
->pakfire
, "Could not find any packages: %m\n");
898 unsigned int num_packages
= 0;
900 // Count how many packages we have
901 for (char** package
= packages
; *package
; package
++)
904 DEBUG(build
->pakfire
, "Found %d package(s)\n", num_packages
);
906 // Build packages in reverse order
907 for (int i
= num_packages
- 1; i
>= 0; i
--) {
908 r
= pakfire_build_package(build
, makefile
, buildroot
, packages
[i
]);
913 // Rescan the build repository to import all packages again
914 r
= pakfire_repo_scan(build
->repo
, 0);
918 // Fetch all packages
919 r
= pakfire_repo_create_packagelist(build
->repo
, &build
->packages
);
924 r
= pakfire_packagelist_walk(build
->packages
, pakfire_build_package_dump
, NULL
);
938 static int pakfire_build_stage(struct pakfire_build
* build
,
939 struct pakfire_parser
* makefile
, const char* stage
) {
942 // Prepare template for this stage
943 int r
= pakfire_string_format(template, TEMPLATE
, stage
);
947 // Fetch the environment
948 char** envp
= pakfire_parser_make_environ(makefile
);
950 // Create the build script
951 char* script
= pakfire_parser_expand(makefile
, "build", template);
953 ERROR(build
->pakfire
, "Could not generate the build script for stage '%s': %m\n",
958 INFO(build
->pakfire
, "Running build stage '%s'\n", stage
);
960 // Import environment
961 // XXX is this a good idea?
962 r
= pakfire_jail_import_env(build
->jail
, (const char**)envp
);
964 ERROR(build
->pakfire
, "Could not import environment: %m\n");
969 r
= pakfire_jail_exec_script(build
->jail
, script
, strlen(script
), NULL
, NULL
, NULL
, NULL
);
971 ERROR(build
->pakfire
, "Build stage '%s' failed with status %d\n", stage
, r
);
976 for (char** e
= envp
; *e
; e
++)
986 static const char* post_build_scripts
[] = {
987 "remove-static-libs",
989 "check-unsafe-files",
995 "check-interpreters",
997 "compress-man-pages",
1002 static int pakfire_build_run_post_build_scripts(struct pakfire_build
* build
) {
1004 const char* buildroot
= pakfire_relpath(build
->pakfire
, build
->buildroot
);
1006 // Set default arguments for build scripts
1007 const char* args
[] = {
1011 // Run them one by one
1012 for (const char** script
= post_build_scripts
; *script
; script
++) {
1013 int r
= pakfire_build_run_script(build
, *script
, args
, NULL
, NULL
, NULL
);
1021 static void pakfire_build_free(struct pakfire_build
* build
) {
1022 if (build
->packages
)
1023 pakfire_packagelist_unref(build
->packages
);
1026 pakfire_repo_clean(build
->repo
, PAKFIRE_REPO_CLEAN_FLAGS_DESTROY
);
1027 pakfire_repo_unref(build
->repo
);
1031 pakfire_jail_unref(build
->jail
);
1033 if (build
->cgroup
) {
1034 // Destroy the cgroup
1035 pakfire_cgroup_destroy(build
->cgroup
);
1038 pakfire_cgroup_unref(build
->cgroup
);
1041 pakfire_unref(build
->pakfire
);
1045 static int pakfire_build_parse_id(struct pakfire_build
* build
, const char* id
) {
1048 // Try parsing the Build ID
1050 r
= uuid_parse(id
, build
->id
);
1052 ERROR(build
->pakfire
, "Could not parse build ID '%s'\n", id
);
1057 // Otherwise initialize the Build ID with something random
1059 uuid_generate_random(build
->id
);
1062 // Store the ID as string, too
1063 uuid_unparse_lower(build
->id
, build
->_id
);
1069 Sets up a new cgroup for this build
1071 static int pakfire_build_setup_cgroup(struct pakfire_build
* build
) {
1072 struct pakfire_config
* config
= NULL
;
1073 char path
[PATH_MAX
];
1077 r
= pakfire_string_format(path
, "pakfire/build-%s", build
->_id
);
1079 ERROR(build
->pakfire
, "Could not compose path for cgroup: %m\n");
1083 // Create a new cgroup
1084 r
= pakfire_cgroup_open(&build
->cgroup
, build
->pakfire
, path
,
1085 PAKFIRE_CGROUP_ENABLE_ACCOUNTING
);
1087 ERROR(build
->pakfire
, "Could not create cgroup for build %s: %m\n", build
->_id
);
1092 config
= pakfire_get_config(build
->pakfire
);
1096 // Guarantee some minimum memory
1097 size_t memory_guaranteed
= pakfire_config_get_bytes(config
, "build",
1098 "memory_guaranteed", PAKFIRE_BUILD_MEMORY_GUARANTEED
);
1099 if (memory_guaranteed
) {
1100 r
= pakfire_cgroup_set_guaranteed_memory(build
->cgroup
, memory_guaranteed
);
1106 size_t memory_limit
= pakfire_config_get_bytes(config
, "build", "memory_limit", 0);
1108 r
= pakfire_cgroup_set_memory_limit(build
->cgroup
, memory_limit
);
1114 size_t pid_limit
= pakfire_config_get_int(config
, "build",
1115 "pid_limit", PAKFIRE_BUILD_PID_LIMIT
);
1117 r
= pakfire_cgroup_set_pid_limit(build
->cgroup
, pid_limit
);
1124 pakfire_config_unref(config
);
1130 Sets up a new jail for this build
1132 static int pakfire_build_setup_jail(struct pakfire_build
* build
) {
1135 // Create a new jail
1136 r
= pakfire_jail_create(&build
->jail
, build
->pakfire
, 0);
1138 ERROR(build
->pakfire
, "Could not create jail for build %s: %m\n", build
->_id
);
1142 // Connect the jail to our cgroup
1143 r
= pakfire_jail_set_cgroup(build
->jail
, build
->cgroup
);
1145 ERROR(build
->pakfire
, "Could not set cgroup for jail: %m\n");
1154 Sets up the ccache for this build
1156 static int pakfire_build_setup_ccache(struct pakfire_build
* build
) {
1157 char path
[PATH_MAX
];
1160 // Check if we want a ccache
1161 if (pakfire_build_has_flag(build
, PAKFIRE_BUILD_DISABLE_CCACHE
)) {
1162 DEBUG(build
->pakfire
, "ccache usage has been disabled for this build\n");
1164 // Set CCACHE_DISABLE=1 so that if ccache is installed, it will disable itself
1165 r
= pakfire_jail_set_env(build
->jail
, "CCACHE_DISABLE", "1");
1167 ERROR(build
->pakfire
, "Could not disable ccache: %m\n");
1175 r
= pakfire_cache_path(build
->pakfire
, path
, "%s", "ccache");
1177 ERROR(build
->pakfire
, "Could not compose ccache path: %m\n");
1181 DEBUG(build
->pakfire
, "Mounting ccache from %s\n", path
);
1183 // Ensure path exists
1184 r
= pakfire_mkdir(path
, 0755);
1185 if (r
&& errno
!= EEXIST
) {
1186 ERROR(build
->pakfire
, "Could not create ccache directory %s: %m\n", path
);
1190 // Bind-mount the directory
1191 r
= pakfire_jail_bind(build
->jail
, path
, CCACHE_DIR
, MS_NOSUID
|MS_NOEXEC
|MS_NODEV
);
1193 ERROR(build
->pakfire
, "Could not mount ccache: %m\n");
1200 static int pakfire_build_setup_repo(struct pakfire_build
* build
) {
1201 char path
[PATH_MAX
] = PAKFIRE_TMP_DIR
"/pakfire-build-repo.XXXXXX";
1205 // Create a new repository
1206 r
= pakfire_repo_create(&build
->repo
, build
->pakfire
, PAKFIRE_REPO_RESULT
);
1208 ERROR(build
->pakfire
, "Could not create repository %s: %m", PAKFIRE_REPO_RESULT
);
1213 pakfire_repo_set_description(build
->repo
, _("Build Repository"));
1215 // Create a temporary directory
1216 const char* p
= pakfire_mkdtemp(path
);
1218 ERROR(build
->pakfire
, "Could not create a the build repository: %m\n");
1223 r
= pakfire_string_format(url
, "file://%s", path
);
1228 pakfire_repo_set_baseurl(build
->repo
, url
);
1233 PAKFIRE_EXPORT
int pakfire_build_create(struct pakfire_build
** build
,
1234 struct pakfire
* pakfire
, const char* id
, int flags
) {
1237 // Allocate build object
1238 struct pakfire_build
* b
= calloc(1, sizeof(*b
));
1242 // Reference pakfire
1243 b
->pakfire
= pakfire_ref(pakfire
);
1245 // Initialize reference counter
1252 r
= pakfire_build_parse_id(b
, id
);
1257 r
= pakfire_build_setup_repo(b
);
1262 r
= pakfire_build_setup_cgroup(b
);
1267 r
= pakfire_build_setup_jail(b
);
1272 r
= pakfire_build_setup_ccache(b
);
1280 pakfire_build_free(b
);
1284 PAKFIRE_EXPORT
struct pakfire_build
* pakfire_build_ref(struct pakfire_build
* build
) {
1290 PAKFIRE_EXPORT
struct pakfire_build
* pakfire_build_unref(struct pakfire_build
* build
) {
1291 if (--build
->nrefs
> 0)
1294 pakfire_build_free(build
);
1298 PAKFIRE_EXPORT
int pakfire_build_set_target(
1299 struct pakfire_build
* build
, const char* target
) {
1300 return pakfire_string_set(build
->target
, target
);
1303 static int pakfire_build_install_packages(struct pakfire_build
* build
,
1304 int* snapshot_needs_update
) {
1307 const char* packages
[] = {
1314 // Install everything
1315 r
= pakfire_install(build
->pakfire
, 0, 0, packages
, NULL
, 0,
1316 &changed
, NULL
, NULL
);
1318 ERROR(build
->pakfire
, "Could not install build dependencies: %m\n");
1322 // Mark snapshot as changed if new packages were installed
1324 *snapshot_needs_update
= 1;
1326 // Update everything
1327 r
= pakfire_sync(build
->pakfire
, 0, 0, &changed
, NULL
, NULL
);
1329 ERROR(build
->pakfire
, "Could not update packages: %m\n");
1333 // Has anything changed?
1335 *snapshot_needs_update
= 1;
1341 Initializes the build environment
1343 static int pakfire_build_init(struct pakfire_build
* build
) {
1344 char path
[PATH_MAX
];
1347 // Don't do it again
1349 DEBUG(build
->pakfire
, "Build environment has already been initialized\n");
1353 // Compose path for snapshot
1354 r
= pakfire_cache_path(build
->pakfire
, path
, "%s", "snapshot.tar.zst");
1356 ERROR(build
->pakfire
, "Could not compose snapshot path: %m\n");
1360 // Tells us whether we need to (re-)create the snapshot
1361 int snapshot_needs_update
= 0;
1364 if (!pakfire_build_has_flag(build
, PAKFIRE_BUILD_DISABLE_SNAPSHOT
)) {
1365 // Try restoring the snapshot
1366 r
= pakfire_snapshot_restore(build
->pakfire
, path
);
1367 if (r
&& errno
!= ENOENT
)
1371 // Install or update any build dependencies
1372 r
= pakfire_build_install_packages(build
, &snapshot_needs_update
);
1376 // Update the snapshot if there were changes
1377 if (!pakfire_build_has_flag(build
, PAKFIRE_BUILD_DISABLE_SNAPSHOT
) && snapshot_needs_update
) {
1378 // Create a new snapshot
1379 r
= pakfire_snapshot_create(build
->pakfire
, path
);
1384 // Mark as initialized
1390 static int pakfire_build_read_makefile(struct pakfire_build
* build
,
1391 struct pakfire_parser
** parser
, struct pakfire_package
* package
) {
1392 char path
[PATH_MAX
];
1395 struct pakfire_parser_error
* error
= NULL
;
1397 const char* nevra
= pakfire_package_get_string(package
, PAKFIRE_PKG_NEVRA
);
1398 const char* name
= pakfire_package_get_string(package
, PAKFIRE_PKG_NAME
);
1400 // Compose path to makefile
1401 r
= pakfire_path(build
->pakfire
, path
, "/usr/src/packages/%s/%s.nm", nevra
, name
);
1406 r
= pakfire_read_makefile(parser
, build
->pakfire
, path
, &error
);
1409 ERROR(build
->pakfire
, "Could not parse makefile %s: %s\n", path
,
1410 pakfire_parser_error_get_message(error
));
1412 ERROR(build
->pakfire
, "Could not parse makefile %s: %m\n", path
);
1419 const char* buildroot
= pakfire_relpath(build
->pakfire
, build
->buildroot
);
1421 pakfire_parser_set(*parser
, NULL
, "BUILDROOT", buildroot
, 0);
1425 pakfire_parser_error_unref(error
);
1430 static int pakfire_build_perform(struct pakfire_build
* build
,
1431 struct pakfire_parser
* makefile
) {
1434 // Prepare the build
1435 r
= pakfire_build_stage(build
, makefile
, "prepare");
1439 // Perform the build
1440 r
= pakfire_build_stage(build
, makefile
, "build");
1445 if (!pakfire_build_has_flag(build
, PAKFIRE_BUILD_DISABLE_TESTS
)) {
1446 r
= pakfire_build_stage(build
, makefile
, "test");
1451 // Install everything
1452 r
= pakfire_build_stage(build
, makefile
, "install");
1456 // Run post build scripts
1457 r
= pakfire_build_run_post_build_scripts(build
);
1465 // Drop to a shell for debugging
1466 if (pakfire_build_has_flag(build
, PAKFIRE_BUILD_INTERACTIVE
))
1467 pakfire_build_shell(build
);
1472 static int __pakfire_build_unpackaged_file(struct pakfire
* pakfire
,
1473 struct pakfire_file
* file
, void* p
) {
1474 char* s
= pakfire_file_dump(file
);
1476 ERROR(pakfire
, "%s\n", s
);
1483 static int pakfire_build_check_unpackaged_files(struct pakfire_build
* build
) {
1484 struct pakfire_filelist
* filelist
= NULL
;
1487 // Create a new filelist
1488 r
= pakfire_filelist_create(&filelist
, build
->pakfire
);
1492 // Scan for all files in BUILDROOT
1493 r
= pakfire_filelist_scan(filelist
, build
->buildroot
, NULL
, NULL
);
1497 const size_t length
= pakfire_filelist_size(filelist
);
1500 ERROR(build
->pakfire
, "Unpackaged files found:\n");
1502 r
= pakfire_filelist_walk(filelist
, __pakfire_build_unpackaged_file
, NULL
);
1512 pakfire_filelist_unref(filelist
);
1517 static int pakfire_build_install_package(struct pakfire
* pakfire
,
1518 struct pakfire_package
* pkg
, void* p
) {
1519 struct pakfire_request
* request
= (struct pakfire_request
*)p
;
1521 return pakfire_request_add_package(request
, PAKFIRE_REQ_INSTALL
, pkg
,
1522 PAKFIRE_REQUEST_ESSENTIAL
);
1525 static int pakfire_build_install_test(struct pakfire_build
* build
) {
1526 struct pakfire_request
* request
= NULL
;
1527 struct pakfire_problem
* problem
= NULL
;
1528 struct pakfire_solution
* solution
= NULL
;
1529 const char* s
= NULL
;
1532 // Create a new request
1533 r
= pakfire_request_create(&request
, build
->pakfire
, 0);
1538 r
= pakfire_packagelist_walk(build
->packages
, pakfire_build_install_package
, request
);
1540 // Solve the request
1541 r
= pakfire_request_solve(request
);
1549 ERROR(build
->pakfire
, "Install test failed:\n");
1551 // Walk through all problems
1553 r
= pakfire_request_next_problem(request
, &problem
);
1557 // There are no more problems
1561 // Format the problem into something human-readable
1562 s
= pakfire_problem_to_string(problem
);
1566 ERROR(build
->pakfire
, " * %s\n", s
);
1568 // Walk through all solutions
1570 r
= pakfire_problem_next_solution(problem
, &solution
);
1574 // There are no more solutions
1578 // Format the solution into something human-readable
1579 s
= pakfire_solution_to_string(solution
);
1583 ERROR(build
->pakfire
, " * %s\n", s
);
1596 ERROR(build
->pakfire
, "Install test failed: %m\n");
1598 pakfire_request_unref(request
);
1600 pakfire_problem_unref(problem
);
1602 pakfire_solution_unref(solution
);
1607 static int pakfire_build_post_check(struct pakfire_build
* build
) {
1610 // Check for unpackaged files
1611 r
= pakfire_build_check_unpackaged_files(build
);
1615 // Perform install test
1616 r
= pakfire_build_install_test(build
);
1623 static int pakfire_build_copy_package(struct pakfire
* pakfire
,
1624 struct pakfire_package
* pkg
, void* p
) {
1625 struct pakfire_archive
* archive
= NULL
;
1626 char path
[PATH_MAX
];
1629 const char* target
= (const char*)p
;
1636 // Fetch the package filename
1637 const char* filename
= pakfire_package_get_string(pkg
, PAKFIRE_PKG_FILENAME
);
1643 // Format the destination path
1644 r
= pakfire_string_format(path
, "%s/%s", target
, filename
);
1649 archive
= pakfire_package_get_archive(pkg
);
1655 // Copy it to its destination
1656 r
= pakfire_archive_copy(archive
, path
);
1662 pakfire_archive_unref(archive
);
1667 static int pakfire_build_copy_packages(struct pakfire_build
* build
) {
1668 struct pakfire_repo
* local
= NULL
;
1671 DEBUG(build
->pakfire
, "Copying built packages\n");
1673 // Fetch local repository
1674 local
= pakfire_get_repo(build
->pakfire
, PAKFIRE_REPO_LOCAL
);
1676 // Copy all packages to the target path
1677 if (*build
->target
) {
1678 r
= pakfire_packagelist_walk(build
->packages
,
1679 pakfire_build_copy_package
, build
->target
);
1684 // If we have a local repository, we copy all packages to it
1686 const char* path
= pakfire_repo_get_path(local
);
1688 r
= pakfire_packagelist_walk(build
->packages
,
1689 pakfire_build_copy_package
, (void*)path
);
1697 pakfire_repo_unref(local
);
1702 static int pakfire_build_install_source_package(
1703 struct pakfire_build
* build
, struct pakfire_package
* package
) {
1704 struct pakfire_request
* request
= NULL
;
1705 struct pakfire_transaction
* transaction
= NULL
;
1708 // Create a new request
1709 r
= pakfire_request_create(&request
, build
->pakfire
, 0);
1714 r
= pakfire_request_add_package(request
, PAKFIRE_REQ_INSTALL
, package
,
1715 PAKFIRE_REQUEST_ESSENTIAL
);
1719 // Solve the request
1720 r
= pakfire_request_solve(request
);
1724 // Fetch the transaction
1725 r
= pakfire_request_get_transaction(request
, &transaction
);
1729 // Set how many packages have been changed
1730 const size_t changes
= pakfire_transaction_count(transaction
);
1732 // Sanity check to see if we actually try to install anything
1734 ERROR(build
->pakfire
, "The source package did not get installed\n");
1739 // Run the transaction
1740 r
= pakfire_transaction_run(transaction
, 0);
1746 pakfire_transaction_unref(transaction
);
1748 pakfire_request_unref(request
);
1753 PAKFIRE_EXPORT
int pakfire_build_exec(struct pakfire_build
* build
, const char* path
) {
1754 struct pakfire_package
* package
= NULL
;
1755 struct pakfire_parser
* makefile
= NULL
;
1756 char* buildroot
= NULL
;
1760 r
= pakfire_path(build
->pakfire
, build
->buildroot
, "%s",
1761 PAKFIRE_TMP_DIR
"/pakfire-buildroot.XXXXXX");
1765 // Open the source package
1766 r
= pakfire_commandline_add(build
->pakfire
, path
, &package
);
1770 const char* nevra
= pakfire_package_get_string(package
, PAKFIRE_PKG_NEVRA
);
1772 INFO(build
->pakfire
, "Building %s...\n", nevra
);
1774 // Initialize the build environment
1775 r
= pakfire_build_init(build
);
1779 // Install the source package
1780 r
= pakfire_build_install_source_package(build
, package
);
1782 ERROR(build
->pakfire
, "Could not install the source package: %m\n");
1787 buildroot
= pakfire_mkdtemp(build
->buildroot
);
1789 ERROR(build
->pakfire
, "Could not create BUILDROOT: %m\n");
1793 // Open the makefile
1794 r
= pakfire_build_read_makefile(build
, &makefile
, package
);
1798 // Perform the actual build
1799 r
= pakfire_build_perform(build
, makefile
);
1803 // Create the packages
1804 r
= pakfire_build_packages(build
, makefile
);
1806 ERROR(build
->pakfire
, "Could not create packages: %m\n");
1810 // Perform post build checks
1811 r
= pakfire_build_post_check(build
);
1815 // Copy packages to their destination
1816 r
= pakfire_build_copy_packages(build
);
1822 pakfire_parser_unref(makefile
);
1824 pakfire_package_unref(package
);
1826 // Cleanup buildroot
1828 pakfire_rmtree(buildroot
, 0);
1834 Compatibility function to keep the legacy API.
1836 PAKFIRE_EXPORT
int pakfire_build(struct pakfire
* pakfire
, const char* path
,
1837 const char* target
, const char* id
, int flags
) {
1838 struct pakfire_build
* build
= NULL
;
1841 // Check if path is set
1847 // Create a new build environment
1848 r
= pakfire_build_create(&build
, pakfire
, id
, flags
);
1854 r
= pakfire_build_set_target(build
, target
);
1860 r
= pakfire_build_exec(build
, path
);
1864 pakfire_build_unref(build
);
1869 int pakfire_build_clean(struct pakfire
* pakfire
, int flags
) {
1870 struct pakfire_repo
* local
= NULL
;
1873 // Fetch local repository
1874 local
= pakfire_get_repo(pakfire
, PAKFIRE_REPO_LOCAL
);
1876 ERROR(pakfire
, "Could not find repository %s: %m\n", PAKFIRE_REPO_LOCAL
);
1880 // Destroy everything in it
1881 r
= pakfire_repo_clean(local
, PAKFIRE_REPO_CLEAN_FLAGS_DESTROY
);
1887 pakfire_repo_unref(local
);
1893 This is a convenience function that sets up a build environment and
1894 then drops the user into an interactive shell.
1896 PAKFIRE_EXPORT
int pakfire_shell(struct pakfire
* pakfire
, const char** packages
, int flags
) {
1897 struct pakfire_build
* build
= NULL
;
1900 // Shells are always interactive
1901 flags
|= PAKFIRE_BUILD_INTERACTIVE
;
1903 // Create a new build environment
1904 r
= pakfire_build_create(&build
, pakfire
, NULL
, flags
);
1906 ERROR(pakfire
, "Could not create build: %m\n");
1910 // Initialize the build environment
1911 r
= pakfire_build_init(build
);
1915 // Install any additional packages
1917 r
= pakfire_install(build
->pakfire
, 0, 0, packages
, NULL
, 0, NULL
, NULL
, NULL
);
1923 r
= pakfire_build_shell(build
);
1927 pakfire_build_unref(build
);