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 #include <pakfire/build.h>
28 #include <pakfire/cgroup.h>
29 #include <pakfire/dependencies.h>
30 #include <pakfire/dist.h>
31 #include <pakfire/file.h>
32 #include <pakfire/i18n.h>
33 #include <pakfire/jail.h>
34 #include <pakfire/logging.h>
35 #include <pakfire/mount.h>
36 #include <pakfire/package.h>
37 #include <pakfire/packager.h>
38 #include <pakfire/parser.h>
39 #include <pakfire/private.h>
40 #include <pakfire/problem.h>
41 #include <pakfire/repo.h>
42 #include <pakfire/request.h>
43 #include <pakfire/scriptlet.h>
44 #include <pakfire/snapshot.h>
45 #include <pakfire/solution.h>
46 #include <pakfire/string.h>
47 #include <pakfire/util.h>
49 #define CCACHE_DIR "/var/cache/ccache"
51 // We guarantee 2 GiB of memory to every build container
52 #define PAKFIRE_BUILD_MEMORY_GUARANTEED (size_t)2 * 1024 * 1024 * 1024
54 // We allow only up to 2048 processes/threads for every build container
55 #define PAKFIRE_BUILD_PID_LIMIT (size_t)2048
57 struct pakfire_build
{
58 struct pakfire
* pakfire
;
66 char _id
[UUID_STR_LEN
];
68 char target
[PATH_MAX
];
71 struct pakfire_cgroup
* cgroup
;
74 struct pakfire_jail
* jail
;
76 // The build repository
77 struct pakfire_repo
* repo
;
79 // A list of all built packages
80 struct pakfire_packagelist
* packages
;
83 char buildroot
[PATH_MAX
];
90 "#!/bin/bash --login\n" \
99 static int pakfire_build_has_flag(struct pakfire_build
* build
, int flag
) {
100 return build
->flags
& flag
;
103 static int __pakfire_build_setup_repo(struct pakfire
* pakfire
,
104 struct pakfire_repo
* repo
, void* p
) {
109 struct pakfire_build
* build
= (struct pakfire_build
*)p
;
111 // Skip processing the installed repository
112 if (pakfire_repo_is_installed_repo(repo
))
115 // Skip processing any other internal repositories
116 if (pakfire_repo_is_internal(repo
))
119 const char* name
= pakfire_repo_get_name(repo
);
121 DEBUG(pakfire
, "Exporting repository configuration for '%s'\n", name
);
123 // Make path for configuration file
124 r
= pakfire_path(build
->pakfire
, path
, PAKFIRE_CONFIG_DIR
"/repos/%s.repo", name
);
126 ERROR(pakfire
, "Could not make repository configuration path for %s: %m\n", name
);
130 // Create the parent directory
131 r
= pakfire_mkparentdir(path
, 0755);
135 // Open the repository configuration
136 f
= fopen(path
, "w");
138 ERROR(pakfire
, "Could not open %s for writing: %m\n", path
);
142 // Write repository configuration
143 r
= pakfire_repo_write_config(repo
, f
);
145 ERROR(pakfire
, "Could not write repository configuration for %s: %m\n", name
);
149 // Bind-mount any local repositories
150 if (pakfire_repo_is_local(repo
)) {
151 const char* _path
= pakfire_repo_get_path(repo
);
153 // Bind-mount the repository data read-only
154 r
= pakfire_jail_bind(build
->jail
, _path
, _path
, MS_RDONLY
);
156 ERROR(pakfire
, "Could not bind-mount the repository at %s: %m\n", _path
);
169 This function enables the local repository so that it can be accessed by
170 a pakfire instance running inside the chroot.
172 static int pakfire_build_enable_repos(struct pakfire_build
* build
) {
173 return pakfire_repo_walk(build
->pakfire
, __pakfire_build_setup_repo
, build
);
177 Drops the user into a shell
179 static int pakfire_build_shell(struct pakfire_build
* build
) {
182 // Export local repository if running in interactive mode
183 r
= pakfire_build_enable_repos(build
);
188 return pakfire_jail_shell(build
->jail
);
191 static int pakfire_build_read_script(struct pakfire_build
* build
,
192 const char* filename
, char** buffer
, size_t* length
) {
197 // Compose the source path
198 r
= pakfire_path_join(path
, PAKFIRE_SCRIPTS_DIR
, filename
);
200 ERROR(build
->pakfire
, "Could not compose path for script '%s': %m\n", filename
);
204 DEBUG(build
->pakfire
, "Reading script from %s...\n", path
);
207 f
= fopen(path
, "r");
209 ERROR(build
->pakfire
, "Could not open script %s: %m\n", path
);
213 // Read the file into a the buffer
214 r
= pakfire_read_file_into_buffer(f
, buffer
, length
);
216 ERROR(build
->pakfire
, "Could not read script: %m\n");
227 static int pakfire_build_run_script(
228 struct pakfire_build
* build
,
229 const char* filename
,
231 pakfire_jail_communicate_in communicate_in
,
232 pakfire_jail_communicate_out communicate_out
,
239 DEBUG(build
->pakfire
, "Running build script '%s'...\n", filename
);
242 r
= pakfire_build_read_script(build
, filename
, &script
, &length
);
244 ERROR(build
->pakfire
, "Could not read script %s: %m\n", filename
);
248 // Execute the script
249 r
= pakfire_jail_exec_script(build
->jail
, script
, length
, args
,
250 communicate_in
, communicate_out
, data
);
252 ERROR(build
->pakfire
, "Script '%s' failed with status %d\n", filename
, r
);
261 struct pakfire_find_deps_ctx
{
262 struct pakfire_package
* pkg
;
264 struct pakfire_scriptlet
* scriptlet
;
267 struct pakfire_filelist
* filelist
;
271 static int pakfire_build_send_filelist(struct pakfire
* pakfire
, void* data
, int fd
) {
272 struct pakfire_find_deps_ctx
* ctx
= (struct pakfire_find_deps_ctx
*)data
;
273 struct pakfire_file
* file
= NULL
;
276 const size_t length
= pakfire_filelist_size(ctx
->filelist
);
278 // Check if we have reached the end of the filelist
279 if (ctx
->i
>= length
)
282 // Fetch the next file
283 file
= pakfire_filelist_get(ctx
->filelist
, ctx
->i
);
285 DEBUG(pakfire
, "Could not fetch file %d: %m\n", ctx
->i
);
290 // Fetch the path of the file
291 const char* path
= pakfire_file_get_path(file
);
293 ERROR(pakfire
, "Received a file with an empty path\n");
298 // Skip files if we are performing pattern matching
299 if (ctx
->pattern
&& !pakfire_file_matches(file
, ctx
->pattern
))
302 // Write path to stdin
303 r
= dprintf(fd
, "%s\n", path
);
308 // Move on to the next file
316 pakfire_file_unref(file
);
321 static int pakfire_build_process_deps(struct pakfire
* pakfire
,
322 void* data
, int priority
, const char* buffer
, const size_t length
) {
323 const struct pakfire_find_deps_ctx
* ctx
= (struct pakfire_find_deps_ctx
*)data
;
327 // Nothing to do for an empty buffer
328 if (!buffer
|| !*buffer
)
332 // Add every dependency that we have received
334 // Copy the dependency to the stack (and remove the trailing newline)
335 r
= pakfire_string_format(dep
, "%.*s", (int)length
- 1, buffer
);
339 DEBUG(pakfire
, "Processing dependency: %s\n", dep
);
341 // Filter out any dependencies that are provided by this package
342 if (ctx
->dep
== PAKFIRE_PKG_REQUIRES
) {
343 // If this is a file, we check if it is on the filelist
344 if (pakfire_filelist_contains(ctx
->filelist
, dep
))
347 // Otherwise check if this dependency is provided by this package
348 else if (pakfire_package_matches_dep(ctx
->pkg
, PAKFIRE_PKG_PROVIDES
, dep
))
353 r
= pakfire_package_add_dep(ctx
->pkg
, ctx
->dep
, buffer
);
355 ERROR(pakfire
, "Could not process dependency '%s': %m\n", buffer
);
360 // Send everything else to the default logger
362 ERROR(pakfire
, "%s\n", buffer
);
369 DEBUG(pakfire
, "Skipping dependency that is provided by the package itself: %s\n", dep
);
375 This function is a special way to run a script.
377 It will pipe the filelist into the standard input of the called script
378 and will read any dependencies from the standard output.
380 static int pakfire_build_find_deps(struct pakfire_build
* build
,
381 struct pakfire_package
* pkg
, int dep
, const char* script
,
382 struct pakfire_filelist
* filelist
, const char* pattern
) {
384 // Construct the context
385 struct pakfire_find_deps_ctx ctx
= {
391 .filelist
= filelist
,
396 // Pass the buildroot as first argument
397 const char* args
[] = {
398 pakfire_relpath(build
->pakfire
, build
->buildroot
),
403 r
= pakfire_build_run_script(build
, script
, args
,
404 pakfire_build_send_filelist
, pakfire_build_process_deps
, &ctx
);
406 ERROR(build
->pakfire
, "%s returned with error %d\n", script
, r
);
411 static int pakfire_build_find_dependencies(struct pakfire_build
* build
,
412 struct pakfire_package
* pkg
, struct pakfire_filelist
* filelist
) {
416 r
= pakfire_build_find_deps(build
, pkg
,
417 PAKFIRE_PKG_PROVIDES
, "find-provides", filelist
, NULL
);
421 // Find all Perl provides
422 r
= pakfire_build_find_deps(build
, pkg
,
423 PAKFIRE_PKG_PROVIDES
, "perl.prov", filelist
, "*.pm");
428 r
= pakfire_build_find_deps(build
, pkg
,
429 PAKFIRE_PKG_REQUIRES
, "find-requires", filelist
, NULL
);
433 // Find all Perl requires
434 r
= pakfire_build_find_deps(build
, pkg
,
435 PAKFIRE_PKG_REQUIRES
, "perl.req", filelist
, "*.pm");
442 static int append_to_array(char*** array
, const char* s
) {
443 unsigned int length
= 0;
445 // Determine the length of the existing array
447 for (char** element
= *array
; *element
; element
++)
452 *array
= reallocarray(*array
, length
+ 2, sizeof(**array
));
456 // Copy the string to the heap
461 // Append p and terminate the array
462 (*array
)[length
] = p
;
463 (*array
)[length
+ 1] = NULL
;
468 static int pakfire_build_package_add_files(struct pakfire_build
* build
,
469 struct pakfire_parser
* makefile
, const char* buildroot
, const char* namespace,
470 struct pakfire_package
* pkg
, struct pakfire_packager
* packager
) {
471 struct pakfire_filelist
* filelist
= NULL
;
474 char** includes
= NULL
;
475 char** excludes
= NULL
;
478 // Fetch filelist from makefile
479 char* files
= pakfire_parser_get(makefile
, namespace, "files");
481 // No files to package?
485 const char* file
= strtok_r(files
, " \n", &p
);
487 // Split into includes and excludes
490 r
= append_to_array(&excludes
, file
+ 1);
492 r
= append_to_array(&includes
, file
);
496 // Move on to the next token
497 file
= strtok_r(NULL
, " \n", &p
);
500 // Allocate a new filelist
501 r
= pakfire_filelist_create(&filelist
, build
->pakfire
);
506 r
= pakfire_filelist_scan(filelist
, build
->buildroot
,
507 (const char**)includes
, (const char**)excludes
);
511 const size_t length
= pakfire_filelist_size(filelist
);
512 DEBUG(build
->pakfire
, "%zu file(s) found\n", length
);
514 // Nothing to do if the filelist is empty
519 pakfire_filelist_dump(filelist
, 1);
522 r
= pakfire_build_find_dependencies(build
, pkg
, filelist
);
524 ERROR(build
->pakfire
, "Finding dependencies failed: %m\n");
528 // Add all files to the package
529 r
= pakfire_packager_add_files(packager
, filelist
);
535 pakfire_filelist_unref(filelist
);
539 for (char** include
= includes
; *include
; include
++)
544 for (char** exclude
= excludes
; *exclude
; exclude
++)
552 static int pakfire_build_send_scriptlet(struct pakfire
* pakfire
, void* data
, int fd
) {
553 const struct pakfire_find_deps_ctx
* ctx
= (struct pakfire_find_deps_ctx
*)data
;
556 // Fetch the scriptlet
557 const char* p
= pakfire_scriptlet_get_data(ctx
->scriptlet
, &length
);
559 ERROR(pakfire
, "Could not fetch scriptlet: %m\n");
563 // Write it into the pipe
564 ssize_t bytes_written
= write(fd
, p
, length
);
565 if (bytes_written
< 0) {
566 ERROR(pakfire
, "Could not send scriptlet: %m\n");
573 static int pakfire_build_add_scriptlet_requires(struct pakfire_build
* build
,
574 struct pakfire_package
* pkg
, struct pakfire_scriptlet
* scriptlet
) {
577 struct pakfire_find_deps_ctx ctx
= {
579 .dep
= PAKFIRE_PKG_PREREQUIRES
,
580 .scriptlet
= scriptlet
,
583 // Find all pre-requires
584 r
= pakfire_build_run_script(build
, "find-prerequires", NULL
,
585 pakfire_build_send_scriptlet
, pakfire_build_process_deps
, &ctx
);
593 static int pakfire_build_package_add_scriptlet(struct pakfire_build
* build
,
594 struct pakfire_package
* pkg
, struct pakfire_packager
* packager
,
595 const char* type
, const char* data
) {
596 struct pakfire_scriptlet
* scriptlet
= NULL
;
600 // Wrap scriptlet into a shell script
601 r
= asprintf(&shell
, "#!/bin/sh\n\nset -e\n\n%s\n\nexit 0\n", data
);
605 // Create a scriptlet
606 r
= pakfire_scriptlet_create(&scriptlet
, build
->pakfire
, type
, shell
, 0);
610 // Add it to the package
611 r
= pakfire_packager_add_scriptlet(packager
, scriptlet
);
613 ERROR(build
->pakfire
, "Could not add scriptlet %s\n", type
);
617 // Add scriptlet requirements
618 r
= pakfire_build_add_scriptlet_requires(build
, pkg
, scriptlet
);
620 ERROR(build
->pakfire
, "Could not add scriptlet requirements: %m\n");
629 pakfire_scriptlet_unref(scriptlet
);
636 static int pakfire_build_package_add_scriptlets(struct pakfire_build
* build
,
637 struct pakfire_parser
* makefile
, const char* namespace,
638 struct pakfire_package
* pkg
, struct pakfire_packager
* packager
) {
642 for (const char** type
= pakfire_scriptlet_types
; *type
; type
++) {
643 r
= pakfire_string_format(name
, "scriptlet:%s", *type
);
647 // Fetch the scriptlet
648 char* data
= pakfire_parser_get(makefile
, namespace, name
);
652 // Add it to the package
653 r
= pakfire_build_package_add_scriptlet(build
, pkg
, packager
, *type
, data
);
665 static int pakfire_build_package(struct pakfire_build
* build
, struct pakfire_parser
* makefile
,
666 const char* buildroot
, const char* namespace) {
667 struct pakfire_package
* pkg
= NULL
;
668 struct pakfire_packager
* packager
= NULL
;
672 // Expand the handle into the package name
673 char* name
= pakfire_parser_expand(makefile
, "packages", namespace);
675 ERROR(build
->pakfire
, "Could not get package name: %m\n");
679 INFO(build
->pakfire
, "Building package '%s'...\n", name
);
680 DEBUG(build
->pakfire
, " buildroot = %s\n", buildroot
);
682 // Fetch build architecture
683 const char* arch
= pakfire_get_arch(build
->pakfire
);
687 // Fetch package from makefile
688 r
= pakfire_parser_create_package(makefile
, &pkg
, NULL
, namespace, arch
);
690 ERROR(build
->pakfire
, "Could not create package from makefile: %m\n");
695 const char* distribution
= pakfire_parser_get(makefile
, NULL
, "DISTRO_NAME");
697 r
= pakfire_package_set_string(pkg
, PAKFIRE_PKG_DISTRO
, distribution
);
703 pakfire_package_set_uuid(pkg
, PAKFIRE_PKG_BUILD_ID
, build
->id
);
705 // Set source package
706 const char* source_name
= pakfire_parser_get(makefile
, NULL
, "name");
708 r
= pakfire_package_set_string(pkg
, PAKFIRE_PKG_SOURCE_NAME
, source_name
);
714 const char* source_evr
= pakfire_parser_get(makefile
, NULL
, "evr");
716 r
= pakfire_package_set_string(pkg
, PAKFIRE_PKG_SOURCE_EVR
, source_evr
);
722 r
= pakfire_package_set_string(pkg
, PAKFIRE_PKG_SOURCE_ARCH
, "src");
727 r
= pakfire_packager_create(&packager
, build
->pakfire
, pkg
);
732 r
= pakfire_build_package_add_files(build
, makefile
, buildroot
, namespace,
738 r
= pakfire_build_package_add_scriptlets(build
, makefile
, namespace,
743 const char* path
= pakfire_repo_get_path(build
->repo
);
745 // Write the finished package
746 r
= pakfire_packager_finish_to_directory(packager
, path
, NULL
);
748 ERROR(build
->pakfire
, "pakfire_packager_finish() failed: %m\n");
752 // Cleanup all packaged files
753 r
= pakfire_packager_cleanup(packager
);
762 pakfire_packager_unref(packager
);
764 pakfire_package_unref(pkg
);
771 static int pakfire_build_package_dump(struct pakfire
* pakfire
,
772 struct pakfire_package
* pkg
, void* p
) {
773 char* dump
= pakfire_package_dump(pkg
, PAKFIRE_PKG_DUMP_LONG
);
777 INFO(pakfire
, "%s\n", dump
);
783 static int pakfire_build_packages(struct pakfire_build
* build
,
784 struct pakfire_parser
* makefile
) {
785 DEBUG(build
->pakfire
, "Creating packages...");
788 const char* buildroot
= pakfire_relpath(build
->pakfire
, build
->buildroot
);
790 // Fetch a list all all packages
791 char** packages
= pakfire_parser_list_namespaces(makefile
, "packages.package:*");
793 ERROR(build
->pakfire
, "Could not find any packages: %m\n");
797 unsigned int num_packages
= 0;
799 // Count how many packages we have
800 for (char** package
= packages
; *package
; package
++)
803 DEBUG(build
->pakfire
, "Found %d package(s)\n", num_packages
);
805 // Build packages in reverse order
806 for (int i
= num_packages
- 1; i
>= 0; i
--) {
807 r
= pakfire_build_package(build
, makefile
, buildroot
, packages
[i
]);
812 // Rescan the build repository to import all packages again
813 r
= pakfire_repo_scan(build
->repo
, 0);
817 // Fetch all packages
818 r
= pakfire_repo_create_packagelist(build
->repo
, &build
->packages
);
823 r
= pakfire_packagelist_walk(build
->packages
, pakfire_build_package_dump
, NULL
);
837 static int pakfire_build_stage(struct pakfire_build
* build
,
838 struct pakfire_parser
* makefile
, const char* stage
) {
841 // Prepare template for this stage
842 int r
= pakfire_string_format(template, TEMPLATE
, stage
);
846 // Fetch the environment
847 char** envp
= pakfire_parser_make_environ(makefile
);
849 // Create the build script
850 char* script
= pakfire_parser_expand(makefile
, "build", template);
852 ERROR(build
->pakfire
, "Could not generate the build script for stage '%s': %m\n",
857 INFO(build
->pakfire
, "Running build stage '%s'\n", stage
);
859 // Import environment
860 // XXX is this a good idea?
861 r
= pakfire_jail_import_env(build
->jail
, (const char**)envp
);
863 ERROR(build
->pakfire
, "Could not import environment: %m\n");
868 r
= pakfire_jail_exec_script(build
->jail
, script
, strlen(script
), NULL
, NULL
, NULL
, NULL
);
870 ERROR(build
->pakfire
, "Build stage '%s' failed with status %d\n", stage
, r
);
875 for (char** e
= envp
; *e
; e
++)
885 static const char* post_build_scripts
[] = {
886 "remove-static-libs",
888 "check-unsafe-files",
894 "check-interpreters",
896 "compress-man-pages",
901 static int pakfire_build_run_post_build_scripts(struct pakfire_build
* build
) {
903 const char* buildroot
= pakfire_relpath(build
->pakfire
, build
->buildroot
);
905 // Set default arguments for build scripts
906 const char* args
[] = {
910 // Run them one by one
911 for (const char** script
= post_build_scripts
; *script
; script
++) {
912 int r
= pakfire_build_run_script(build
, *script
, args
, NULL
, NULL
, NULL
);
920 static void pakfire_build_free(struct pakfire_build
* build
) {
922 pakfire_packagelist_unref(build
->packages
);
925 pakfire_repo_clean(build
->repo
, PAKFIRE_REPO_CLEAN_FLAGS_DESTROY
);
926 pakfire_repo_unref(build
->repo
);
930 pakfire_jail_unref(build
->jail
);
933 // Destroy the cgroup
934 pakfire_cgroup_destroy(build
->cgroup
);
937 pakfire_cgroup_unref(build
->cgroup
);
940 pakfire_unref(build
->pakfire
);
944 static int pakfire_build_parse_id(struct pakfire_build
* build
, const char* id
) {
947 // Try parsing the Build ID
949 r
= uuid_parse(id
, build
->id
);
951 ERROR(build
->pakfire
, "Could not parse build ID '%s'\n", id
);
956 // Otherwise initialize the Build ID with something random
958 uuid_generate_random(build
->id
);
961 // Store the ID as string, too
962 uuid_unparse_lower(build
->id
, build
->_id
);
968 Sets up a new cgroup for this build
970 static int pakfire_build_setup_cgroup(struct pakfire_build
* build
) {
971 struct pakfire_config
* config
= NULL
;
976 r
= pakfire_string_format(path
, "pakfire/build-%s", build
->_id
);
978 ERROR(build
->pakfire
, "Could not compose path for cgroup: %m\n");
982 // Create a new cgroup
983 r
= pakfire_cgroup_open(&build
->cgroup
, build
->pakfire
, path
,
984 PAKFIRE_CGROUP_ENABLE_ACCOUNTING
);
986 ERROR(build
->pakfire
, "Could not create cgroup for build %s: %m\n", build
->_id
);
991 config
= pakfire_get_config(build
->pakfire
);
995 // Guarantee some minimum memory
996 size_t memory_guaranteed
= pakfire_config_get_bytes(config
, "build",
997 "memory_guaranteed", PAKFIRE_BUILD_MEMORY_GUARANTEED
);
998 if (memory_guaranteed
) {
999 r
= pakfire_cgroup_set_guaranteed_memory(build
->cgroup
, memory_guaranteed
);
1005 size_t memory_limit
= pakfire_config_get_bytes(config
, "build", "memory_limit", 0);
1007 r
= pakfire_cgroup_set_memory_limit(build
->cgroup
, memory_limit
);
1013 size_t pid_limit
= pakfire_config_get_int(config
, "build",
1014 "pid_limit", PAKFIRE_BUILD_PID_LIMIT
);
1016 r
= pakfire_cgroup_set_pid_limit(build
->cgroup
, pid_limit
);
1023 pakfire_config_unref(config
);
1029 Sets up a new jail for this build
1031 static int pakfire_build_setup_jail(struct pakfire_build
* build
) {
1034 // Create a new jail
1035 r
= pakfire_jail_create(&build
->jail
, build
->pakfire
, 0);
1037 ERROR(build
->pakfire
, "Could not create jail for build %s: %m\n", build
->_id
);
1041 // Connect the jail to our cgroup
1042 r
= pakfire_jail_set_cgroup(build
->jail
, build
->cgroup
);
1044 ERROR(build
->pakfire
, "Could not set cgroup for jail: %m\n");
1053 Sets up the ccache for this build
1055 static int pakfire_build_setup_ccache(struct pakfire_build
* build
) {
1056 char path
[PATH_MAX
];
1059 // Check if we want a ccache
1060 if (pakfire_build_has_flag(build
, PAKFIRE_BUILD_DISABLE_CCACHE
)) {
1061 DEBUG(build
->pakfire
, "ccache usage has been disabled for this build\n");
1063 // Set CCACHE_DISABLE=1 so that if ccache is installed, it will disable itself
1064 r
= pakfire_jail_set_env(build
->jail
, "CCACHE_DISABLE", "1");
1066 ERROR(build
->pakfire
, "Could not disable ccache: %m\n");
1074 r
= pakfire_cache_path(build
->pakfire
, path
, "%s", "ccache");
1076 ERROR(build
->pakfire
, "Could not compose ccache path: %m\n");
1080 DEBUG(build
->pakfire
, "Mounting ccache from %s\n", path
);
1082 // Ensure path exists
1083 r
= pakfire_mkdir(path
, 0755);
1084 if (r
&& errno
!= EEXIST
) {
1085 ERROR(build
->pakfire
, "Could not create ccache directory %s: %m\n", path
);
1089 // Bind-mount the directory
1090 r
= pakfire_jail_bind(build
->jail
, path
, CCACHE_DIR
, MS_NOSUID
|MS_NOEXEC
|MS_NODEV
);
1092 ERROR(build
->pakfire
, "Could not mount ccache: %m\n");
1099 static int pakfire_build_setup_repo(struct pakfire_build
* build
) {
1100 char path
[PATH_MAX
] = PAKFIRE_TMP_DIR
"/pakfire-build-repo.XXXXXX";
1104 // Create a new repository
1105 r
= pakfire_repo_create(&build
->repo
, build
->pakfire
, PAKFIRE_REPO_RESULT
);
1107 ERROR(build
->pakfire
, "Could not create repository %s: %m", PAKFIRE_REPO_RESULT
);
1112 pakfire_repo_set_description(build
->repo
, _("Build Repository"));
1114 // Create a temporary directory
1115 const char* p
= pakfire_mkdtemp(path
);
1117 ERROR(build
->pakfire
, "Could not create a the build repository: %m\n");
1122 r
= pakfire_string_format(url
, "file://%s", path
);
1127 pakfire_repo_set_baseurl(build
->repo
, url
);
1132 PAKFIRE_EXPORT
int pakfire_build_create(struct pakfire_build
** build
,
1133 struct pakfire
* pakfire
, const char* id
, int flags
) {
1136 // Allocate build object
1137 struct pakfire_build
* b
= calloc(1, sizeof(*b
));
1141 // Reference pakfire
1142 b
->pakfire
= pakfire_ref(pakfire
);
1144 // Initialize reference counter
1151 r
= pakfire_build_parse_id(b
, id
);
1156 r
= pakfire_build_setup_repo(b
);
1161 r
= pakfire_build_setup_cgroup(b
);
1166 r
= pakfire_build_setup_jail(b
);
1171 r
= pakfire_build_setup_ccache(b
);
1179 pakfire_build_free(b
);
1183 PAKFIRE_EXPORT
struct pakfire_build
* pakfire_build_ref(struct pakfire_build
* build
) {
1189 PAKFIRE_EXPORT
struct pakfire_build
* pakfire_build_unref(struct pakfire_build
* build
) {
1190 if (--build
->nrefs
> 0)
1193 pakfire_build_free(build
);
1197 PAKFIRE_EXPORT
int pakfire_build_set_target(
1198 struct pakfire_build
* build
, const char* target
) {
1199 return pakfire_string_set(build
->target
, target
);
1202 static int pakfire_build_install_packages(struct pakfire_build
* build
,
1203 int* snapshot_needs_update
) {
1206 const char* packages
[] = {
1213 // Install everything
1214 r
= pakfire_install(build
->pakfire
, 0, 0, packages
, NULL
, 0,
1215 &changed
, NULL
, NULL
);
1217 ERROR(build
->pakfire
, "Could not install build dependencies: %m\n");
1221 // Mark snapshot as changed if new packages were installed
1223 *snapshot_needs_update
= 1;
1225 // Update everything
1226 r
= pakfire_sync(build
->pakfire
, 0, 0, &changed
, NULL
, NULL
);
1228 ERROR(build
->pakfire
, "Could not update packages: %m\n");
1232 // Has anything changed?
1234 *snapshot_needs_update
= 1;
1240 Initializes the build environment
1242 static int pakfire_build_init(struct pakfire_build
* build
) {
1243 char path
[PATH_MAX
];
1246 // Don't do it again
1248 DEBUG(build
->pakfire
, "Build environment has already been initialized\n");
1252 // Compose path for snapshot
1253 r
= pakfire_cache_path(build
->pakfire
, path
, "%s", "snapshot.tar.zst");
1255 ERROR(build
->pakfire
, "Could not compose snapshot path: %m\n");
1259 // Tells us whether we need to (re-)create the snapshot
1260 int snapshot_needs_update
= 0;
1263 if (!pakfire_build_has_flag(build
, PAKFIRE_BUILD_DISABLE_SNAPSHOT
)) {
1264 // Try restoring the snapshot
1265 r
= pakfire_snapshot_restore(build
->pakfire
, path
);
1266 if (r
&& errno
!= ENOENT
)
1270 // Install or update any build dependencies
1271 r
= pakfire_build_install_packages(build
, &snapshot_needs_update
);
1275 // Update the snapshot if there were changes
1276 if (!pakfire_build_has_flag(build
, PAKFIRE_BUILD_DISABLE_SNAPSHOT
) && snapshot_needs_update
) {
1277 // Create a new snapshot
1278 r
= pakfire_snapshot_create(build
->pakfire
, path
);
1283 // Mark as initialized
1289 static int pakfire_build_read_makefile(struct pakfire_build
* build
,
1290 struct pakfire_parser
** parser
, struct pakfire_package
* package
) {
1291 char path
[PATH_MAX
];
1294 struct pakfire_parser_error
* error
= NULL
;
1296 const char* nevra
= pakfire_package_get_string(package
, PAKFIRE_PKG_NEVRA
);
1297 const char* name
= pakfire_package_get_string(package
, PAKFIRE_PKG_NAME
);
1299 // Compose path to makefile
1300 r
= pakfire_path(build
->pakfire
, path
, "/usr/src/packages/%s/%s.nm", nevra
, name
);
1305 r
= pakfire_read_makefile(parser
, build
->pakfire
, path
, &error
);
1308 ERROR(build
->pakfire
, "Could not parse makefile %s: %s\n", path
,
1309 pakfire_parser_error_get_message(error
));
1311 ERROR(build
->pakfire
, "Could not parse makefile %s: %m\n", path
);
1318 const char* buildroot
= pakfire_relpath(build
->pakfire
, build
->buildroot
);
1320 pakfire_parser_set(*parser
, NULL
, "BUILDROOT", buildroot
, 0);
1324 pakfire_parser_error_unref(error
);
1329 static int pakfire_build_perform(struct pakfire_build
* build
,
1330 struct pakfire_parser
* makefile
) {
1333 // Prepare the build
1334 r
= pakfire_build_stage(build
, makefile
, "prepare");
1338 // Perform the build
1339 r
= pakfire_build_stage(build
, makefile
, "build");
1344 if (!pakfire_build_has_flag(build
, PAKFIRE_BUILD_DISABLE_TESTS
)) {
1345 r
= pakfire_build_stage(build
, makefile
, "test");
1350 // Install everything
1351 r
= pakfire_build_stage(build
, makefile
, "install");
1355 // Run post build scripts
1356 r
= pakfire_build_run_post_build_scripts(build
);
1364 // Drop to a shell for debugging
1365 if (pakfire_build_has_flag(build
, PAKFIRE_BUILD_INTERACTIVE
))
1366 pakfire_build_shell(build
);
1371 static int __pakfire_build_unpackaged_file(struct pakfire
* pakfire
,
1372 struct pakfire_file
* file
, void* p
) {
1373 char* s
= pakfire_file_dump(file
);
1375 ERROR(pakfire
, "%s\n", s
);
1382 static int pakfire_build_check_unpackaged_files(struct pakfire_build
* build
) {
1383 struct pakfire_filelist
* filelist
= NULL
;
1386 // Create a new filelist
1387 r
= pakfire_filelist_create(&filelist
, build
->pakfire
);
1391 // Scan for all files in BUILDROOT
1392 r
= pakfire_filelist_scan(filelist
, build
->buildroot
, NULL
, NULL
);
1396 const size_t length
= pakfire_filelist_size(filelist
);
1399 ERROR(build
->pakfire
, "Unpackaged files found:\n");
1401 r
= pakfire_filelist_walk(filelist
, NULL
,
1402 __pakfire_build_unpackaged_file
, NULL
);
1412 pakfire_filelist_unref(filelist
);
1417 static int pakfire_build_install_package(struct pakfire
* pakfire
,
1418 struct pakfire_package
* pkg
, void* p
) {
1419 struct pakfire_request
* request
= (struct pakfire_request
*)p
;
1421 return pakfire_request_add_package(request
, PAKFIRE_REQ_INSTALL
, pkg
,
1422 PAKFIRE_REQUEST_ESSENTIAL
);
1425 static int pakfire_build_install_test(struct pakfire_build
* build
) {
1426 struct pakfire_request
* request
= NULL
;
1427 struct pakfire_problem
* problem
= NULL
;
1428 struct pakfire_solution
* solution
= NULL
;
1429 const char* s
= NULL
;
1432 // Create a new request
1433 r
= pakfire_request_create(&request
, build
->pakfire
, 0);
1438 r
= pakfire_packagelist_walk(build
->packages
, pakfire_build_install_package
, request
);
1440 // Solve the request
1441 r
= pakfire_request_solve(request
);
1449 ERROR(build
->pakfire
, "Install test failed:\n");
1451 // Walk through all problems
1453 r
= pakfire_request_next_problem(request
, &problem
);
1457 // There are no more problems
1461 // Format the problem into something human-readable
1462 s
= pakfire_problem_to_string(problem
);
1466 ERROR(build
->pakfire
, " * %s\n", s
);
1468 // Walk through all solutions
1470 r
= pakfire_problem_next_solution(problem
, &solution
);
1474 // There are no more solutions
1478 // Format the solution into something human-readable
1479 s
= pakfire_solution_to_string(solution
);
1483 ERROR(build
->pakfire
, " * %s\n", s
);
1496 ERROR(build
->pakfire
, "Install test failed: %m\n");
1498 pakfire_request_unref(request
);
1500 pakfire_problem_unref(problem
);
1502 pakfire_solution_unref(solution
);
1507 static int pakfire_build_post_check(struct pakfire_build
* build
) {
1510 // Check for unpackaged files
1511 r
= pakfire_build_check_unpackaged_files(build
);
1515 // Perform install test
1516 r
= pakfire_build_install_test(build
);
1523 static int pakfire_build_copy_package(struct pakfire
* pakfire
,
1524 struct pakfire_package
* pkg
, void* p
) {
1525 struct pakfire_archive
* archive
= NULL
;
1526 char path
[PATH_MAX
];
1529 const char* target
= (const char*)p
;
1536 // Fetch the package filename
1537 const char* filename
= pakfire_package_get_string(pkg
, PAKFIRE_PKG_FILENAME
);
1543 // Format the destination path
1544 r
= pakfire_string_format(path
, "%s/%s", target
, filename
);
1549 archive
= pakfire_package_get_archive(pkg
);
1555 // Copy it to its destination
1556 r
= pakfire_archive_copy(archive
, path
);
1562 pakfire_archive_unref(archive
);
1567 static int pakfire_build_copy_packages(struct pakfire_build
* build
) {
1568 struct pakfire_repo
* local
= NULL
;
1571 DEBUG(build
->pakfire
, "Copying built packages\n");
1573 // Fetch local repository
1574 local
= pakfire_get_repo(build
->pakfire
, PAKFIRE_REPO_LOCAL
);
1576 // Copy all packages to the target path
1577 if (*build
->target
) {
1578 r
= pakfire_packagelist_walk(build
->packages
,
1579 pakfire_build_copy_package
, build
->target
);
1584 // If we have a local repository, we copy all packages to it
1586 const char* path
= pakfire_repo_get_path(local
);
1588 r
= pakfire_packagelist_walk(build
->packages
,
1589 pakfire_build_copy_package
, (void*)path
);
1597 pakfire_repo_unref(local
);
1602 static int pakfire_build_install_source_package(
1603 struct pakfire_build
* build
, struct pakfire_package
* package
) {
1604 struct pakfire_request
* request
= NULL
;
1605 struct pakfire_transaction
* transaction
= NULL
;
1608 // Create a new request
1609 r
= pakfire_request_create(&request
, build
->pakfire
, 0);
1614 r
= pakfire_request_add_package(request
, PAKFIRE_REQ_INSTALL
, package
,
1615 PAKFIRE_REQUEST_ESSENTIAL
);
1619 // Solve the request
1620 r
= pakfire_request_solve(request
);
1624 // Fetch the transaction
1625 r
= pakfire_request_get_transaction(request
, &transaction
);
1629 // Set how many packages have been changed
1630 const size_t changes
= pakfire_transaction_count(transaction
);
1632 // Sanity check to see if we actually try to install anything
1634 ERROR(build
->pakfire
, "The source package did not get installed\n");
1639 // Run the transaction
1640 r
= pakfire_transaction_run(transaction
, 0);
1646 pakfire_transaction_unref(transaction
);
1648 pakfire_request_unref(request
);
1653 PAKFIRE_EXPORT
int pakfire_build_exec(struct pakfire_build
* build
, const char* path
) {
1654 struct pakfire_package
* package
= NULL
;
1655 struct pakfire_parser
* makefile
= NULL
;
1656 char* buildroot
= NULL
;
1660 r
= pakfire_path(build
->pakfire
, build
->buildroot
, "%s",
1661 PAKFIRE_TMP_DIR
"/pakfire-buildroot.XXXXXX");
1665 // Open the source package
1666 r
= pakfire_commandline_add(build
->pakfire
, path
, &package
);
1670 const char* nevra
= pakfire_package_get_string(package
, PAKFIRE_PKG_NEVRA
);
1672 INFO(build
->pakfire
, "Building %s...\n", nevra
);
1674 // Initialize the build environment
1675 r
= pakfire_build_init(build
);
1679 // Install the source package
1680 r
= pakfire_build_install_source_package(build
, package
);
1682 ERROR(build
->pakfire
, "Could not install the source package: %m\n");
1687 buildroot
= pakfire_mkdtemp(build
->buildroot
);
1689 ERROR(build
->pakfire
, "Could not create BUILDROOT: %m\n");
1693 // Open the makefile
1694 r
= pakfire_build_read_makefile(build
, &makefile
, package
);
1698 // Perform the actual build
1699 r
= pakfire_build_perform(build
, makefile
);
1703 // Create the packages
1704 r
= pakfire_build_packages(build
, makefile
);
1706 ERROR(build
->pakfire
, "Could not create packages: %m\n");
1710 // Perform post build checks
1711 r
= pakfire_build_post_check(build
);
1715 // Copy packages to their destination
1716 r
= pakfire_build_copy_packages(build
);
1722 pakfire_parser_unref(makefile
);
1724 pakfire_package_unref(package
);
1726 // Cleanup buildroot
1728 pakfire_rmtree(buildroot
, 0);
1734 Compatibility function to keep the legacy API.
1736 PAKFIRE_EXPORT
int pakfire_build(struct pakfire
* pakfire
, const char* path
,
1737 const char* target
, const char* id
, int flags
) {
1738 struct pakfire_build
* build
= NULL
;
1741 // Check if path is set
1747 // Create a new build environment
1748 r
= pakfire_build_create(&build
, pakfire
, id
, flags
);
1754 r
= pakfire_build_set_target(build
, target
);
1760 r
= pakfire_build_exec(build
, path
);
1764 pakfire_build_unref(build
);
1769 int pakfire_build_clean(struct pakfire
* pakfire
, int flags
) {
1770 struct pakfire_repo
* local
= NULL
;
1773 // Fetch local repository
1774 local
= pakfire_get_repo(pakfire
, PAKFIRE_REPO_LOCAL
);
1776 ERROR(pakfire
, "Could not find repository %s: %m\n", PAKFIRE_REPO_LOCAL
);
1780 // Destroy everything in it
1781 r
= pakfire_repo_clean(local
, PAKFIRE_REPO_CLEAN_FLAGS_DESTROY
);
1787 pakfire_repo_unref(local
);
1793 This is a convenience function that sets up a build environment and
1794 then drops the user into an interactive shell.
1796 PAKFIRE_EXPORT
int pakfire_shell(struct pakfire
* pakfire
, const char** packages
, int flags
) {
1797 struct pakfire_build
* build
= NULL
;
1800 // Shells are always interactive
1801 flags
|= PAKFIRE_BUILD_INTERACTIVE
;
1803 // Create a new build environment
1804 r
= pakfire_build_create(&build
, pakfire
, NULL
, flags
);
1806 ERROR(pakfire
, "Could not create build: %m\n");
1810 // Initialize the build environment
1811 r
= pakfire_build_init(build
);
1815 // Install any additional packages
1817 r
= pakfire_install(build
->pakfire
, 0, 0, packages
, NULL
, 0, NULL
, NULL
, NULL
);
1823 r
= pakfire_build_shell(build
);
1827 pakfire_build_unref(build
);