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>
26 #include <uuid/uuid.h>
28 #define PCRE2_CODE_UNIT_WIDTH 8
31 #include <pakfire/build.h>
32 #include <pakfire/cgroup.h>
33 #include <pakfire/dependencies.h>
34 #include <pakfire/dist.h>
35 #include <pakfire/file.h>
36 #include <pakfire/i18n.h>
37 #include <pakfire/jail.h>
38 #include <pakfire/logging.h>
39 #include <pakfire/mount.h>
40 #include <pakfire/package.h>
41 #include <pakfire/packager.h>
42 #include <pakfire/parser.h>
43 #include <pakfire/private.h>
44 #include <pakfire/problem.h>
45 #include <pakfire/repo.h>
46 #include <pakfire/scriptlet.h>
47 #include <pakfire/snapshot.h>
48 #include <pakfire/solution.h>
49 #include <pakfire/string.h>
50 #include <pakfire/transaction.h>
51 #include <pakfire/util.h>
53 #define CCACHE_DIR "/var/cache/ccache"
55 // We guarantee 2 GiB of memory to every build container
56 #define PAKFIRE_BUILD_MEMORY_GUARANTEED (size_t)2 * 1024 * 1024 * 1024
58 // We allow only up to 2048 processes/threads for every build container
59 #define PAKFIRE_BUILD_PID_LIMIT (size_t)2048
61 // A list of packages that is installed by default
62 static const char* PAKFIRE_BUILD_PACKAGES
[] = {
67 struct pakfire_build
{
68 struct pakfire
* pakfire
;
76 char _id
[UUID_STR_LEN
];
78 char target
[PATH_MAX
];
81 struct timespec time_start
;
84 struct pakfire_cgroup
* cgroup
;
87 struct pakfire_jail
* jail
;
89 // The build repository
90 struct pakfire_repo
* repo
;
92 // A list of all built packages
93 struct pakfire_packagelist
* packages
;
96 char buildroot
[PATH_MAX
];
99 char ccache_path
[PATH_MAX
];
105 struct pakfire_build_callbacks
{
107 pakfire_build_log_callback log
;
113 "#!/bin/bash --login\n" \
123 Convenience macro to call the logger callback
125 #define pakfire_build_log(build, priority, r, file, line, function, format, arg...) \
126 build->callbacks.log(build, build->callbacks.log_data, \
127 priority, r, file, line, function, format, ## arg)
129 #define BUILD_LOG_ERRNO(build, priority, r, arg...) \
130 pakfire_build_log(build, priority, r, __FILE__, __LINE__, __FUNCTION__, ## arg)
131 #define BUILD_LOG(build, priority, arg...) BUILD_LOG_ERRNO(build, priority, 0, ## arg)
133 #define BUILD_INFO_ERRNO(build, r, arg...) BUILD_LOG_ERRNO(build, LOG_INFO, r, ## arg)
134 #define BUILD_ERROR_ERRNO(build, r, arg...) BUILD_LOG_ERRNO(build, LOG_ERR, r, ## arg)
135 #define BUILD_DEBUG_ERRNO(build, r, arg...) BUILD_LOG_ERRNO(build, LOG_DEBUG, r, ## arg)
137 #define BUILD_INFO(build, arg...) BUILD_INFO_ERRNO(build, 0, ## arg)
138 #define BUILD_ERROR(build, arg...) BUILD_ERROR_ERRNO(build, 0, ## arg)
139 #define BUILD_DEBUG(build, arg...) BUILD_DEBUG_ERRNO(build, 0, ## arg)
141 static int pakfire_build_has_flag(struct pakfire_build
* build
, int flag
) {
142 return build
->flags
& flag
;
145 static double pakfire_build_duration(struct pakfire_build
* build
) {
149 // What time is it now?
150 r
= clock_gettime(CLOCK_MONOTONIC
, &now
);
154 return pakfire_timespec_delta(&now
, &build
->time_start
);
157 static int __pakfire_build_setup_repo(struct pakfire
* pakfire
,
158 struct pakfire_repo
* repo
, void* p
) {
163 struct pakfire_build
* build
= (struct pakfire_build
*)p
;
165 // Skip processing the installed repository
166 if (pakfire_repo_is_installed_repo(repo
))
169 // Skip processing any other internal repositories
170 if (pakfire_repo_is_internal(repo
))
173 const char* name
= pakfire_repo_get_name(repo
);
175 BUILD_DEBUG(build
, "Exporting repository configuration for '%s'\n", name
);
177 // Make path for configuration file
178 r
= pakfire_path(build
->pakfire
, path
, PAKFIRE_CONFIG_DIR
"/repos/%s.repo", name
);
180 BUILD_ERROR(build
, "Could not make repository configuration path for %s: %m\n", name
);
184 // Create the parent directory
185 r
= pakfire_mkparentdir(path
, 0755);
189 // Open the repository configuration
190 f
= fopen(path
, "w");
192 BUILD_ERROR(build
, "Could not open %s for writing: %m\n", path
);
196 // Write repository configuration
197 r
= pakfire_repo_write_config(repo
, f
);
199 BUILD_ERROR(build
, "Could not write repository configuration for %s: %m\n", name
);
203 // Bind-mount any local repositories
204 if (pakfire_repo_is_local(repo
)) {
205 const char* _path
= pakfire_repo_get_path(repo
);
207 // Bind-mount the repository data read-only
208 if (pakfire_path_exists(_path
)) {
209 r
= pakfire_jail_bind(build
->jail
, _path
, _path
, MS_RDONLY
);
211 BUILD_ERROR(build
, "Could not bind-mount the repository at %s: %m\n", _path
);
225 This function enables the local repository so that it can be accessed by
226 a pakfire instance running inside the chroot.
228 static int pakfire_build_enable_repos(struct pakfire_build
* build
) {
229 return pakfire_repo_walk(build
->pakfire
, __pakfire_build_setup_repo
, build
);
233 Drops the user into a shell
235 static int pakfire_build_shell(struct pakfire_build
* build
) {
238 // Export local repository if running in interactive mode
239 r
= pakfire_build_enable_repos(build
);
244 return pakfire_jail_shell(build
->jail
);
247 static int pakfire_build_read_script(struct pakfire_build
* build
,
248 const char* filename
, char** buffer
, size_t* length
) {
253 // Compose the source path
254 r
= pakfire_path_join(path
, PAKFIRE_SCRIPTS_DIR
, filename
);
256 ERROR(build
->pakfire
, "Could not compose path for script '%s': %m\n", filename
);
260 BUILD_DEBUG(build
, "Reading script from %s...\n", path
);
263 f
= fopen(path
, "r");
265 BUILD_ERROR(build
, "Could not open script %s: %m\n", path
);
269 // Read the file into a the buffer
270 r
= pakfire_read_file_into_buffer(f
, buffer
, length
);
272 BUILD_ERROR(build
, "Could not read script: %m\n");
283 static int pakfire_build_run_script(
284 struct pakfire_build
* build
,
285 const char* filename
,
287 pakfire_jail_communicate_in communicate_in
,
288 pakfire_jail_communicate_out communicate_out
,
295 BUILD_DEBUG(build
, "Running build script '%s'...\n", filename
);
298 r
= pakfire_build_read_script(build
, filename
, &script
, &length
);
300 ERROR(build
->pakfire
, "Could not read script %s: %m\n", filename
);
304 // Execute the script
305 r
= pakfire_jail_exec_script(build
->jail
, script
, length
, args
,
306 communicate_in
, communicate_out
, data
);
308 BUILD_ERROR(build
, "Script '%s' failed with status %d\n", filename
, r
);
316 struct pakfire_find_deps_ctx
{
317 struct pakfire_build
* build
;
318 struct pakfire_package
* pkg
;
320 struct pakfire_scriptlet
* scriptlet
;
322 const pcre2_code
* filter
;
324 struct pakfire_filelist
* filelist
;
328 static int pakfire_build_make_filter(struct pakfire_build
* build
, pcre2_code
** regex
,
329 struct pakfire_parser
* makefile
, const char* namespace, const char* filter
) {
330 char* pattern
= NULL
;
334 pattern
= pakfire_parser_get(makefile
, namespace, filter
);
336 // Nothing if to if there is no or an empty pattern
337 if (!pattern
|| !*pattern
)
340 DEBUG(build
->pakfire
, "Found filter for %s: %s\n", filter
, pattern
);
342 // Compile the regular expression
343 r
= pakfire_compile_regex(build
->pakfire
, regex
, pattern
);
354 static int pakfire_build_send_filelist(struct pakfire
* pakfire
, void* data
, int fd
) {
355 struct pakfire_find_deps_ctx
* ctx
= (struct pakfire_find_deps_ctx
*)data
;
356 struct pakfire_file
* file
= NULL
;
359 const size_t length
= pakfire_filelist_length(ctx
->filelist
);
361 // Check if we have reached the end of the filelist
362 if (ctx
->i
>= length
)
365 // Fetch the next file
366 file
= pakfire_filelist_get(ctx
->filelist
, ctx
->i
);
368 DEBUG(pakfire
, "Could not fetch file %d: %m\n", ctx
->i
);
373 // Fetch the path of the file
374 const char* path
= pakfire_file_get_path(file
);
376 ERROR(pakfire
, "Received a file with an empty path\n");
381 // Skip files that don't match what we are looking for
382 if (ctx
->class && !pakfire_file_matches_class(file
, ctx
->class))
385 // Write path to stdin
386 r
= dprintf(fd
, "%s\n", path
);
391 // Move on to the next file
399 pakfire_file_unref(file
);
404 static int pakfire_build_process_deps(struct pakfire
* pakfire
,
405 void* data
, int priority
, const char* buffer
, const size_t length
) {
406 const struct pakfire_find_deps_ctx
* ctx
= (struct pakfire_find_deps_ctx
*)data
;
408 pcre2_match_data
* match
= NULL
;
411 // Nothing to do for an empty buffer
412 if (!buffer
|| !*buffer
)
416 // Add every dependency that we have received
418 // Copy the dependency to the stack (and remove the trailing newline)
419 r
= pakfire_string_format(dep
, "%.*s", (int)length
- 1, buffer
);
423 BUILD_DEBUG(ctx
->build
, "Processing dependency: %s\n", dep
);
425 // Filter out any dependencies that are provided by this package
426 if (ctx
->dep
== PAKFIRE_PKG_REQUIRES
) {
427 // If this is a file, we check if it is on the filelist
428 if (pakfire_filelist_contains(ctx
->filelist
, dep
))
431 // Otherwise check if this dependency is provided by this package
432 else if (pakfire_package_matches_dep(ctx
->pkg
, PAKFIRE_PKG_PROVIDES
, dep
))
436 // Check if this dependency should be filtered
438 match
= pcre2_match_data_create_from_pattern(ctx
->filter
, NULL
);
440 ERROR(pakfire
, "Could not allocate PCRE match data: %m\n");
445 r
= pcre2_jit_match(ctx
->filter
, (PCRE2_SPTR
)dep
, length
- 1,
449 if (r
== PCRE2_ERROR_NOMATCH
) {
456 // Fetch the error message
457 r
= pcre2_get_error_message(r
, (PCRE2_UCHAR
*)error
, sizeof(error
));
459 BUILD_ERROR(ctx
->build
, "Could not fetch PCRE error message: %m\n");
464 BUILD_ERROR(ctx
->build
, "Could not match the filter: %s\n", error
);
470 BUILD_DEBUG(ctx
->build
, "Skipping dependency that has been filtered: %s\n", dep
);
477 r
= pakfire_package_add_dep(ctx
->pkg
, ctx
->dep
, buffer
);
479 BUILD_ERROR(ctx
->build
, "Could not process dependency '%s': %m\n", buffer
);
484 // Send everything else to the build logger
486 BUILD_ERROR(ctx
->build
, "%s\n", buffer
);
493 BUILD_DEBUG(ctx
->build
, "Skipping dependency that is provided by the package itself: %s\n", dep
);
497 pcre2_match_data_free(match
);
503 This function is a special way to run a script.
505 It will pipe the filelist into the standard input of the called script
506 and will read any dependencies from the standard output.
508 static int pakfire_build_find_deps(struct pakfire_build
* build
,
509 struct pakfire_package
* pkg
, int dep
, const char* script
,
510 struct pakfire_filelist
* filelist
, int class, const pcre2_code
* filter
) {
511 // Construct the context
512 struct pakfire_find_deps_ctx ctx
= {
520 .filelist
= filelist
,
525 // Skip calling the script if class doesn't match
526 if (class && !pakfire_filelist_matches_class(filelist
, class)) {
527 DEBUG(build
->pakfire
, "Skipping calling %s as class does not match\n", script
);
531 // Pass the buildroot as first argument
532 const char* args
[] = {
533 pakfire_relpath(build
->pakfire
, build
->buildroot
),
538 r
= pakfire_build_run_script(build
, script
, args
,
539 pakfire_build_send_filelist
, pakfire_build_process_deps
, &ctx
);
541 BUILD_ERROR(build
, "%s returned with error %d\n", script
, r
);
546 static int pakfire_build_find_dependencies(struct pakfire_build
* build
,
547 struct pakfire_parser
* makefile
, const char* namespace,
548 struct pakfire_package
* pkg
, struct pakfire_filelist
* filelist
) {
549 pcre2_code
* filter_provides
= NULL
;
550 pcre2_code
* filter_requires
= NULL
;
553 // Fetch the provides filter
554 r
= pakfire_build_make_filter(build
, &filter_provides
,
555 makefile
, namespace, "filter_provides");
557 ERROR(build
->pakfire
, "Provides filter is broken: %m\n");
561 // Fetch the requires filter
562 r
= pakfire_build_make_filter(build
, &filter_requires
,
563 makefile
, namespace, "filter_requires");
565 ERROR(build
->pakfire
, "Requires filter is broken: %m\n");
570 r
= pakfire_build_find_deps(build
, pkg
,
571 PAKFIRE_PKG_PROVIDES
, "find-provides", filelist
, 0, filter_provides
);
575 // Find all Perl provides
576 r
= pakfire_build_find_deps(build
, pkg
,
577 PAKFIRE_PKG_PROVIDES
, "perl.prov", filelist
, PAKFIRE_FILE_PERL
, filter_provides
);
582 r
= pakfire_build_find_deps(build
, pkg
,
583 PAKFIRE_PKG_REQUIRES
, "find-requires", filelist
, 0, filter_requires
);
587 // Find all Perl requires
588 r
= pakfire_build_find_deps(build
, pkg
,
589 PAKFIRE_PKG_REQUIRES
, "perl.req", filelist
, PAKFIRE_FILE_PERL
, filter_requires
);
595 pcre2_code_free(filter_provides
);
597 pcre2_code_free(filter_requires
);
602 static int __pakfire_build_package_mark_config_files(
603 struct pakfire
* pakfire
, struct pakfire_file
* file
, void* data
) {
604 char** configfiles
= (char**)data
;
607 // Skip anything that isn't a regular file
608 if (pakfire_file_get_type(file
) == S_IFREG
)
612 const char* path
= pakfire_file_get_path(file
);
614 // Check if any configfile entry matches against this
615 for (char** configfile
= configfiles
; *configfile
; configfile
++) {
616 if (pakfire_string_startswith(path
, *configfile
)) {
617 r
= pakfire_file_set_flags(file
, PAKFIRE_FILE_CONFIG
);
629 static int pakfire_build_package_mark_config_files(struct pakfire_build
* build
,
630 struct pakfire_filelist
* filelist
, struct pakfire_parser
* makefile
, const char* namespace) {
631 char** configfiles
= NULL
;
634 // Fetch configfiles from makefile
635 r
= pakfire_parser_get_filelist(makefile
, namespace, "configfiles", &configfiles
, NULL
);
637 // If configfiles is not set, there is nothing to do
641 r
= pakfire_filelist_walk(filelist
, __pakfire_build_package_mark_config_files
,
646 for (char** configfile
= configfiles
; *configfile
; configfile
++)
654 static int pakfire_build_package_add_files(struct pakfire_build
* build
,
655 struct pakfire_parser
* makefile
, const char* buildroot
, const char* namespace,
656 struct pakfire_package
* pkg
, struct pakfire_packager
* packager
) {
657 struct pakfire_filelist
* filelist
= NULL
;
658 char** includes
= NULL
;
659 char** excludes
= NULL
;
662 // Fetch filelist from makefile
663 r
= pakfire_parser_get_filelist(makefile
, namespace, "files", &includes
, &excludes
);
665 // No files to package?
666 if (!includes
&& !excludes
)
669 // Allocate a new filelist
670 r
= pakfire_filelist_create(&filelist
, build
->pakfire
);
675 r
= pakfire_filelist_scan(filelist
, build
->buildroot
,
676 (const char**)includes
, (const char**)excludes
, PAKFIRE_FILELIST_EXTENDED_MATCHING
);
680 BUILD_DEBUG(build
, "%zu file(s) found\n", pakfire_filelist_length(filelist
));
682 // Nothing to do if the filelist is empty
683 if (pakfire_filelist_is_empty(filelist
))
687 pakfire_filelist_dump(filelist
, PAKFIRE_FILE_DUMP_FULL
);
690 r
= pakfire_build_find_dependencies(build
, makefile
, namespace, pkg
, filelist
);
692 BUILD_ERROR(build
, "Finding dependencies failed: %m\n");
696 // Mark configuration files
697 r
= pakfire_build_package_mark_config_files(build
, filelist
, makefile
, namespace);
701 // Add all files to the package
702 r
= pakfire_packager_add_files(packager
, filelist
);
708 pakfire_filelist_unref(filelist
);
710 for (char** include
= includes
; *include
; include
++)
715 for (char** exclude
= excludes
; *exclude
; exclude
++)
723 static int pakfire_build_send_scriptlet(struct pakfire
* pakfire
, void* data
, int fd
) {
724 const struct pakfire_find_deps_ctx
* ctx
= (struct pakfire_find_deps_ctx
*)data
;
727 // Fetch the scriptlet
728 const char* p
= pakfire_scriptlet_get_data(ctx
->scriptlet
, &length
);
730 ERROR(pakfire
, "Could not fetch scriptlet: %m\n");
734 // Write it into the pipe
735 ssize_t bytes_written
= write(fd
, p
, length
);
736 if (bytes_written
< 0) {
737 ERROR(pakfire
, "Could not send scriptlet: %m\n");
744 static int pakfire_build_add_scriptlet_requires(struct pakfire_build
* build
,
745 struct pakfire_package
* pkg
, struct pakfire_scriptlet
* scriptlet
) {
748 struct pakfire_find_deps_ctx ctx
= {
751 .dep
= PAKFIRE_PKG_PREREQUIRES
,
752 .scriptlet
= scriptlet
,
755 // Find all pre-requires
756 r
= pakfire_build_run_script(build
, "find-prerequires", NULL
,
757 pakfire_build_send_scriptlet
, pakfire_build_process_deps
, &ctx
);
765 static int pakfire_build_package_add_scriptlet(struct pakfire_build
* build
,
766 struct pakfire_package
* pkg
, struct pakfire_packager
* packager
,
767 const char* type
, const char* data
) {
768 struct pakfire_scriptlet
* scriptlet
= NULL
;
772 // Wrap scriptlet into a shell script
773 r
= asprintf(&shell
, "#!/bin/sh\n\nset -e\n\n%s\n\nexit 0\n", data
);
777 // Create a scriptlet
778 r
= pakfire_scriptlet_create(&scriptlet
, build
->pakfire
, type
, shell
, 0);
782 // Add it to the package
783 r
= pakfire_packager_add_scriptlet(packager
, scriptlet
);
785 BUILD_ERROR(build
, "Could not add scriptlet %s\n", type
);
789 // Add scriptlet requirements
790 r
= pakfire_build_add_scriptlet_requires(build
, pkg
, scriptlet
);
792 BUILD_ERROR(build
, "Could not add scriptlet requirements: %m\n");
801 pakfire_scriptlet_unref(scriptlet
);
808 static int pakfire_build_package_add_scriptlets(struct pakfire_build
* build
,
809 struct pakfire_parser
* makefile
, const char* namespace,
810 struct pakfire_package
* pkg
, struct pakfire_packager
* packager
) {
814 for (const char** type
= pakfire_scriptlet_types
; *type
; type
++) {
815 r
= pakfire_string_format(name
, "scriptlet:%s", *type
);
819 // Fetch the scriptlet
820 char* data
= pakfire_parser_get(makefile
, namespace, name
);
824 // Add it to the package
825 r
= pakfire_build_package_add_scriptlet(build
, pkg
, packager
, *type
, data
);
837 static int pakfire_build_package(struct pakfire_build
* build
, struct pakfire_parser
* makefile
,
838 const char* buildroot
, const char* namespace) {
839 struct pakfire_package
* pkg
= NULL
;
840 struct pakfire_packager
* packager
= NULL
;
844 // Expand the handle into the package name
845 char* name
= pakfire_parser_expand(makefile
, "packages", namespace);
847 ERROR(build
->pakfire
, "Could not get package name: %m\n");
851 BUILD_INFO(build
, "Building package '%s'...\n", name
);
852 BUILD_DEBUG(build
, " buildroot = %s\n", buildroot
);
854 // Fetch build architecture
855 const char* arch
= pakfire_get_arch(build
->pakfire
);
859 // Fetch package from makefile
860 r
= pakfire_parser_create_package(makefile
, &pkg
, NULL
, namespace, arch
);
862 BUILD_ERROR(build
, "Could not create package from makefile: %m\n");
867 const char* distribution
= pakfire_parser_get(makefile
, NULL
, "DISTRO_NAME");
869 r
= pakfire_package_set_string(pkg
, PAKFIRE_PKG_DISTRO
, distribution
);
875 pakfire_package_set_uuid(pkg
, PAKFIRE_PKG_BUILD_ID
, build
->id
);
877 // Set source package
878 const char* source_name
= pakfire_parser_get(makefile
, NULL
, "name");
880 r
= pakfire_package_set_string(pkg
, PAKFIRE_PKG_SOURCE_NAME
, source_name
);
886 const char* source_evr
= pakfire_parser_get(makefile
, NULL
, "evr");
888 r
= pakfire_package_set_string(pkg
, PAKFIRE_PKG_SOURCE_EVR
, source_evr
);
894 r
= pakfire_package_set_string(pkg
, PAKFIRE_PKG_SOURCE_ARCH
, "src");
899 r
= pakfire_packager_create(&packager
, build
->pakfire
, pkg
);
904 r
= pakfire_build_package_add_files(build
, makefile
, buildroot
, namespace,
910 r
= pakfire_build_package_add_scriptlets(build
, makefile
, namespace,
915 const char* path
= pakfire_repo_get_path(build
->repo
);
917 // Write the finished package
918 r
= pakfire_packager_finish_to_directory(packager
, path
, NULL
);
920 BUILD_ERROR(build
, "pakfire_packager_finish() failed: %m\n");
924 // Cleanup all packaged files
925 r
= pakfire_packager_cleanup(packager
);
934 pakfire_packager_unref(packager
);
936 pakfire_package_unref(pkg
);
943 static int pakfire_build_package_dump(struct pakfire
* pakfire
,
944 struct pakfire_package
* pkg
, void* p
) {
945 struct pakfire_build
* build
= p
;
947 char* dump
= pakfire_package_dump(pkg
, PAKFIRE_PKG_DUMP_LONG
);
951 BUILD_INFO(build
, "%s\n", dump
);
957 static int pakfire_build_packages(struct pakfire_build
* build
,
958 struct pakfire_parser
* makefile
) {
959 BUILD_INFO(build
, "Creating packages...");
962 const char* buildroot
= pakfire_relpath(build
->pakfire
, build
->buildroot
);
964 // Fetch a list all all packages
965 char** packages
= pakfire_parser_list_namespaces(makefile
, "packages.package:*");
967 BUILD_ERROR(build
, "Could not find any packages: %m\n");
971 unsigned int num_packages
= 0;
973 // Count how many packages we have
974 for (char** package
= packages
; *package
; package
++)
977 DEBUG(build
->pakfire
, "Found %d package(s)\n", num_packages
);
979 // Build packages in reverse order
980 for (int i
= num_packages
- 1; i
>= 0; i
--) {
981 r
= pakfire_build_package(build
, makefile
, buildroot
, packages
[i
]);
986 // Rescan the build repository to import all packages again
987 r
= pakfire_repo_scan(build
->repo
, 0);
991 // Create a new packagelist
992 r
= pakfire_packagelist_create(&build
->packages
, build
->pakfire
);
996 // Fetch all packages
997 r
= pakfire_repo_to_packagelist(build
->repo
, build
->packages
);
1002 r
= pakfire_packagelist_walk(build
->packages
, pakfire_build_package_dump
, build
);
1016 static int pakfire_build_stage(struct pakfire_build
* build
,
1017 struct pakfire_parser
* makefile
, const char* stage
) {
1018 char template[1024];
1020 // Prepare template for this stage
1021 int r
= pakfire_string_format(template, TEMPLATE
, stage
);
1025 // Fetch the environment
1026 char** envp
= pakfire_parser_make_environ(makefile
);
1028 // Create the build script
1029 char* script
= pakfire_parser_expand(makefile
, "build", template);
1031 BUILD_ERROR(build
, "Could not generate the build script for stage '%s': %m\n", stage
);
1035 BUILD_INFO(build
, "Running build stage '%s'\n", stage
);
1037 // Import environment
1038 // XXX is this a good idea?
1039 r
= pakfire_jail_import_env(build
->jail
, (const char**)envp
);
1041 BUILD_ERROR(build
, "Could not import environment: %m\n");
1046 r
= pakfire_jail_exec_script(build
->jail
, script
, strlen(script
), NULL
, NULL
, NULL
, NULL
);
1048 BUILD_ERROR(build
, "Build stage '%s' failed with status %d\n", stage
, r
);
1052 for (char** e
= envp
; *e
; e
++)
1063 PAKFIRE_BUILD_CLEANUP_FILES
= (1 << 0),
1064 PAKFIRE_BUILD_ERROR_IF_NOT_EMPTY
= (1 << 1),
1065 PAKFIRE_BUILD_SHOW_PROGRESS
= (1 << 2),
1069 This helper function takes a callback which is expected to add any files
1070 to the given filelist which will optionally be all removed after.
1072 static int pakfire_build_post_process_files(struct pakfire_build
* build
,
1073 struct pakfire_filelist
* filelist
, const char* description
,
1074 int (*callback
)(struct pakfire
* pakfire
, struct pakfire_file
* file
, void* data
),
1076 struct pakfire_filelist
* removees
= NULL
;
1079 // Create a filelist with objects that need to be removed
1080 r
= pakfire_filelist_create(&removees
, build
->pakfire
);
1084 // Find all files that need to be removed
1085 r
= pakfire_filelist_walk(filelist
, callback
, removees
,
1086 (flags
& PAKFIRE_BUILD_SHOW_PROGRESS
) ? PAKFIRE_FILELIST_SHOW_PROGRESS
: 0);
1090 if (!pakfire_filelist_is_empty(removees
)) {
1092 BUILD_INFO(build
, "%s\n", description
);
1094 // Show all files which will be removed
1095 pakfire_filelist_dump(removees
, PAKFIRE_FILE_DUMP_FULL
|PAKFIRE_FILE_DUMP_ISSUES
);
1097 // Remove all files on the removee list
1098 if (flags
& PAKFIRE_BUILD_CLEANUP_FILES
) {
1099 r
= pakfire_filelist_cleanup(removees
, PAKFIRE_FILE_CLEANUP_TIDY
);
1103 // Remove all files from the filelist
1104 r
= pakfire_filelist_remove_all(filelist
, removees
);
1109 // Report an error if any files have been found
1110 if (flags
& PAKFIRE_BUILD_ERROR_IF_NOT_EMPTY
)
1116 pakfire_filelist_unref(removees
);
1121 static int __pakfire_build_remove_static_libraries(
1122 struct pakfire
* pakfire
, struct pakfire_file
* file
, void* data
) {
1123 struct pakfire_filelist
* removees
= (struct pakfire_filelist
*)data
;
1124 char path
[PATH_MAX
];
1127 // Find all static libraries
1128 if (pakfire_file_matches_class(file
, PAKFIRE_FILE_STATIC_LIBRARY
)) {
1129 // Copy the filename
1130 r
= pakfire_string_set(path
, pakfire_file_get_abspath(file
));
1134 // Remove the extension
1135 r
= pakfire_path_replace_extension(path
, "so");
1139 // Only delete if there is a shared object with the same name
1140 if (pakfire_path_exists(path
))
1141 return pakfire_filelist_add(removees
, file
);
1147 static int pakfire_build_post_remove_static_libraries(
1148 struct pakfire_build
* build
, struct pakfire_filelist
* filelist
) {
1149 return pakfire_build_post_process_files(build
, filelist
,
1150 "Removing static libaries...",
1151 __pakfire_build_remove_static_libraries
,
1152 PAKFIRE_BUILD_CLEANUP_FILES
);
1155 static int __pakfire_build_remove_libtool_archives(
1156 struct pakfire
* pakfire
, struct pakfire_file
* file
, void* data
) {
1157 struct pakfire_filelist
* removees
= (struct pakfire_filelist
*)data
;
1159 // Find all libtool archive files
1160 if (pakfire_file_matches_class(file
, PAKFIRE_FILE_LIBTOOL_ARCHIVE
))
1161 return pakfire_filelist_add(removees
, file
);
1166 static int pakfire_build_post_remove_libtool_archives(
1167 struct pakfire_build
* build
, struct pakfire_filelist
* filelist
) {
1168 return pakfire_build_post_process_files(build
, filelist
,
1169 "Removing libtool archives...",
1170 __pakfire_build_remove_libtool_archives
,
1171 PAKFIRE_BUILD_CLEANUP_FILES
);
1174 static int __pakfire_build_check_broken_symlinks(
1175 struct pakfire
* pakfire
, struct pakfire_file
* file
, void* data
) {
1176 struct pakfire_filelist
* broken
= (struct pakfire_filelist
*)data
;
1179 // Ignore anything that isn't a symlink
1180 switch (pakfire_file_get_type(file
)) {
1182 if (!pakfire_file_symlink_target_exists(file
)) {
1183 r
= pakfire_filelist_add(broken
, file
);
1190 // Ignore anything that isn't a symlink
1198 static int pakfire_build_post_check_broken_symlinks(
1199 struct pakfire_build
* build
, struct pakfire_filelist
* filelist
) {
1200 return pakfire_build_post_process_files(build
, filelist
,
1201 "Broken symlinks have been found:",
1202 __pakfire_build_check_broken_symlinks
,
1203 PAKFIRE_BUILD_ERROR_IF_NOT_EMPTY
);
1210 static int __pakfire_build_post_check_files(
1211 struct pakfire
* pakfire
, struct pakfire_file
* file
, void* data
) {
1212 struct pakfire_filelist
* broken
= (struct pakfire_filelist
*)data
;
1216 // Check file for issues
1217 r
= pakfire_file_check(file
, &issues
);
1219 //BUILD_ERROR(build, "%s: File Check failed: %m\n", pakfire_file_get_path(file));
1223 // If any issues have been found, consider this file to be on the list
1225 r
= pakfire_filelist_add(broken
, file
);
1233 static int pakfire_build_post_check_files(
1234 struct pakfire_build
* build
, struct pakfire_filelist
* filelist
) {
1235 return pakfire_build_post_process_files(
1239 __pakfire_build_post_check_files
,
1240 PAKFIRE_BUILD_ERROR_IF_NOT_EMPTY
|PAKFIRE_BUILD_SHOW_PROGRESS
);
1243 static int pakfire_build_run_post_build_checks(struct pakfire_build
* build
) {
1244 struct pakfire_filelist
* filelist
= NULL
;
1247 // Create a filelist of all files in the build
1248 r
= pakfire_filelist_create(&filelist
, build
->pakfire
);
1250 BUILD_ERROR(build
, "Could not create filelist: %m\n");
1254 // Scan for all files in BUILDROOT
1255 r
= pakfire_filelist_scan(filelist
, build
->buildroot
, NULL
, NULL
, 0);
1259 // If the filelist is empty, we can are done
1260 if (pakfire_filelist_is_empty(filelist
)) {
1261 DEBUG(build
->pakfire
, "Empty BUILDROOT. Skipping post build checks...\n");
1266 // Remove any static libraries
1267 r
= pakfire_build_post_remove_static_libraries(build
, filelist
);
1271 // Remove any libtool archives
1272 r
= pakfire_build_post_remove_libtool_archives(build
, filelist
);
1276 // Check for any broken symlinks
1277 r
= pakfire_build_post_check_broken_symlinks(build
, filelist
);
1282 r
= pakfire_build_post_check_files(build
, filelist
);
1288 pakfire_filelist_unref(filelist
);
1293 static const char* post_build_scripts
[] = {
1294 "compress-man-pages",
1299 static int pakfire_build_run_post_build_scripts(struct pakfire_build
* build
) {
1301 const char* buildroot
= pakfire_relpath(build
->pakfire
, build
->buildroot
);
1303 // Set default arguments for build scripts
1304 const char* args
[] = {
1308 // Run them one by one
1309 for (const char** script
= post_build_scripts
; *script
; script
++) {
1310 int r
= pakfire_build_run_script(build
, *script
, args
, NULL
, NULL
, NULL
);
1318 static void pakfire_build_free(struct pakfire_build
* build
) {
1319 if (build
->packages
)
1320 pakfire_packagelist_unref(build
->packages
);
1323 pakfire_repo_clean(build
->repo
, PAKFIRE_REPO_CLEAN_FLAGS_DESTROY
);
1324 pakfire_repo_unref(build
->repo
);
1328 pakfire_jail_unref(build
->jail
);
1330 if (build
->cgroup
) {
1331 // Destroy the cgroup
1332 pakfire_cgroup_destroy(build
->cgroup
);
1335 pakfire_cgroup_unref(build
->cgroup
);
1338 pakfire_unref(build
->pakfire
);
1342 static int pakfire_build_parse_id(struct pakfire_build
* build
, const char* id
) {
1345 // Try parsing the Build ID
1347 r
= uuid_parse(id
, build
->id
);
1349 ERROR(build
->pakfire
, "Could not parse build ID '%s'\n", id
);
1353 // Otherwise initialize the Build ID with something random
1355 uuid_generate_random(build
->id
);
1358 // Store the ID as string, too
1359 uuid_unparse_lower(build
->id
, build
->_id
);
1365 Sets up a new cgroup for this build
1367 static int pakfire_build_setup_cgroup(struct pakfire_build
* build
) {
1368 struct pakfire_config
* config
= NULL
;
1369 char path
[PATH_MAX
];
1373 r
= pakfire_string_format(path
, "pakfire/build-%s", build
->_id
);
1375 ERROR(build
->pakfire
, "Could not compose path for cgroup: %m\n");
1379 // Create a new cgroup
1380 r
= pakfire_cgroup_open(&build
->cgroup
, build
->pakfire
, path
,
1381 PAKFIRE_CGROUP_ENABLE_ACCOUNTING
);
1383 ERROR(build
->pakfire
, "Could not create cgroup for build %s: %m\n", build
->_id
);
1388 config
= pakfire_get_config(build
->pakfire
);
1392 // Guarantee some minimum memory
1393 size_t memory_guaranteed
= pakfire_config_get_bytes(config
, "build",
1394 "memory_guaranteed", PAKFIRE_BUILD_MEMORY_GUARANTEED
);
1395 if (memory_guaranteed
) {
1396 r
= pakfire_cgroup_set_guaranteed_memory(build
->cgroup
, memory_guaranteed
);
1402 size_t memory_limit
= pakfire_config_get_bytes(config
, "build", "memory_limit", 0);
1404 r
= pakfire_cgroup_set_memory_limit(build
->cgroup
, memory_limit
);
1410 size_t pid_limit
= pakfire_config_get_int(config
, "build",
1411 "pid_limit", PAKFIRE_BUILD_PID_LIMIT
);
1413 r
= pakfire_cgroup_set_pid_limit(build
->cgroup
, pid_limit
);
1420 pakfire_config_unref(config
);
1425 static int pakfire_build_default_log_callback(struct pakfire_build
* build
, void* data
,
1426 int priority
, int error
, const char* file
, int line
, const char* function
, const char* format
, ...) {
1427 char* buffer
= NULL
;
1431 // Only pass messages to the pakfire logger that are wanted
1432 if (pakfire_log_get_priority(build
->pakfire
) < priority
)
1435 // Format the message
1436 va_start(args
, format
);
1437 r
= vasprintf(&buffer
, format
, args
);
1440 // Fail if we could not format the string
1444 // Send the message to the upstream logger
1445 pakfire_log(build
->pakfire
, priority
, error
, file
, line
, function
, "%s", buffer
);
1454 static int pakfire_build_jail_log_callback(struct pakfire
* pakfire
,
1455 void* data
, int priority
, const char* line
, size_t length
) {
1456 struct pakfire_build
* build
= data
;
1458 // Get the runtime of the build
1459 const double t
= pakfire_build_duration(build
);
1463 const unsigned int h
= (unsigned int)t
/ 3600;
1464 const unsigned int m
= (unsigned int)t
% 3600 / 60;
1465 const unsigned int s
= (unsigned int)t
% 60;
1466 const unsigned int ms
= (unsigned int)(t
* 1000.0) % 1000;
1469 return BUILD_LOG(build
, priority
, "[%02d:%02d:%02d.%04d] %.*s", h
, m
, s
, ms
, length
, line
);
1472 return BUILD_LOG(build
, priority
, "[ %02d:%02d.%04d] %.*s", m
, s
, ms
, length
, line
);
1475 return BUILD_LOG(build
, priority
, "[ %02d.%04d] %.*s", s
, ms
, length
, line
);
1479 Sets up a new jail for this build
1481 static int pakfire_build_setup_jail(struct pakfire_build
* build
) {
1484 // Create a new jail
1485 r
= pakfire_jail_create(&build
->jail
, build
->pakfire
);
1487 ERROR(build
->pakfire
, "Could not create jail for build %s: %m\n", build
->_id
);
1491 // Configure our custom logging callback
1492 pakfire_jail_set_log_callback(build
->jail
, pakfire_build_jail_log_callback
, build
);
1494 // Connect the jail to our cgroup
1495 r
= pakfire_jail_set_cgroup(build
->jail
, build
->cgroup
);
1497 ERROR(build
->pakfire
, "Could not set cgroup for jail: %m\n");
1501 // Build everything with a slightly lower priority
1502 r
= pakfire_jail_nice(build
->jail
, 5);
1504 ERROR(build
->pakfire
, "Could not set nice level: %m\n");
1513 Sets up the ccache for this build
1515 static int pakfire_build_mount_ccache(struct pakfire_build
* build
) {
1518 // Do nothing if the ccache is disabled
1519 if (pakfire_build_has_flag(build
, PAKFIRE_BUILD_DISABLE_CCACHE
))
1522 // Check that the path is set
1523 if (!*build
->ccache_path
)
1526 // Make sure the path exists
1527 r
= pakfire_mkdir(build
->ccache_path
, 0755);
1529 ERROR(build
->pakfire
, "Could not create %s: %m\n", build
->ccache_path
);
1533 // Bind-mount the directory
1534 return pakfire_jail_bind(build
->jail
, build
->ccache_path
, CCACHE_DIR
,
1535 MS_NOSUID
|MS_NOEXEC
|MS_NODEV
);
1538 static int pakfire_build_setup_ccache(struct pakfire_build
* build
) {
1541 // Check if we want a ccache
1542 if (pakfire_build_has_flag(build
, PAKFIRE_BUILD_DISABLE_CCACHE
)) {
1543 DEBUG(build
->pakfire
, "ccache usage has been disabled for this build\n");
1545 // Set CCACHE_DISABLE=1 so that if ccache is installed, it will disable itself
1546 r
= pakfire_jail_set_env(build
->jail
, "CCACHE_DISABLE", "1");
1548 ERROR(build
->pakfire
, "Could not disable ccache: %m\n");
1556 r
= pakfire_jail_set_env(build
->jail
, "CCACHE_DIR", CCACHE_DIR
);
1558 ERROR(build
->pakfire
, "Could not set ccache directory: %m\n");
1562 // Set CCACHE_TEMPDIR
1563 r
= pakfire_jail_set_env(build
->jail
, "CCACHE_TEMPDIR", "/tmp");
1565 ERROR(build
->pakfire
, "Could not set ccache tempdir: %m\n");
1569 // Set a default path
1570 r
= pakfire_cache_path(build
->pakfire
, build
->ccache_path
, "%s", "ccache");
1577 static int pakfire_build_setup_repo(struct pakfire_build
* build
) {
1578 char path
[PATH_MAX
] = PAKFIRE_TMP_DIR
"/pakfire-build-repo.XXXXXX";
1582 // Create a new repository
1583 r
= pakfire_repo_create(&build
->repo
, build
->pakfire
, PAKFIRE_REPO_RESULT
);
1585 ERROR(build
->pakfire
, "Could not create repository %s: %m", PAKFIRE_REPO_RESULT
);
1590 pakfire_repo_set_description(build
->repo
, _("Build Repository"));
1592 // Create a temporary directory
1593 const char* p
= pakfire_mkdtemp(path
);
1595 ERROR(build
->pakfire
, "Could not create a the build repository: %m\n");
1600 r
= pakfire_string_format(url
, "file://%s", path
);
1605 pakfire_repo_set_baseurl(build
->repo
, url
);
1610 static int pakfire_build_set_time_start(struct pakfire_build
* build
) {
1613 // Fetch current time
1614 r
= clock_gettime(CLOCK_MONOTONIC
, &build
->time_start
);
1616 ERROR(build
->pakfire
, "Could not fetch start time: %m\n");
1621 PAKFIRE_EXPORT
int pakfire_build_create(struct pakfire_build
** build
,
1622 struct pakfire
* pakfire
, const char* id
, int flags
) {
1625 // Allocate build object
1626 struct pakfire_build
* b
= calloc(1, sizeof(*b
));
1630 // Reference pakfire
1631 b
->pakfire
= pakfire_ref(pakfire
);
1633 // Initialize reference counter
1639 // Set default log callback
1640 b
->callbacks
.log
= pakfire_build_default_log_callback
;
1643 r
= pakfire_build_set_time_start(b
);
1648 r
= pakfire_build_parse_id(b
, id
);
1653 r
= pakfire_build_setup_repo(b
);
1658 r
= pakfire_build_setup_cgroup(b
);
1663 r
= pakfire_build_setup_jail(b
);
1668 r
= pakfire_build_setup_ccache(b
);
1676 pakfire_build_free(b
);
1680 PAKFIRE_EXPORT
struct pakfire_build
* pakfire_build_ref(struct pakfire_build
* build
) {
1686 PAKFIRE_EXPORT
struct pakfire_build
* pakfire_build_unref(struct pakfire_build
* build
) {
1687 if (--build
->nrefs
> 0)
1690 pakfire_build_free(build
);
1694 PAKFIRE_EXPORT
void pakfire_build_set_log_callback(struct pakfire_build
* build
,
1695 pakfire_build_log_callback callback
, void* data
) {
1696 build
->callbacks
.log
= callback
;
1697 build
->callbacks
.log_data
= data
;
1700 PAKFIRE_EXPORT
int pakfire_build_set_ccache_path(
1701 struct pakfire_build
* build
, const char* path
) {
1702 // Check if this can be called
1703 if (pakfire_build_has_flag(build
, PAKFIRE_BUILD_DISABLE_CCACHE
))
1706 // Check input value
1707 if (!path
|| !*path
)
1711 return pakfire_string_set(build
->ccache_path
, path
);
1714 PAKFIRE_EXPORT
int pakfire_build_set_target(
1715 struct pakfire_build
* build
, const char* target
) {
1716 return pakfire_string_set(build
->target
, target
);
1719 static int pakfire_build_install_packages(
1720 struct pakfire_build
* build
, int* snapshot_needs_update
) {
1721 struct pakfire_transaction
* transaction
= NULL
;
1722 char* problems
= NULL
;
1725 // Create a new transaction
1726 r
= pakfire_transaction_create(&transaction
, build
->pakfire
, 0);
1730 // Install all build dependencies
1731 for (const char** p
= PAKFIRE_BUILD_PACKAGES
; *p
; p
++) {
1732 r
= pakfire_transaction_request(transaction
,
1733 PAKFIRE_JOB_INSTALL
, *p
, PAKFIRE_JOB_ESSENTIAL
);
1738 // Also update everything that has already been installed
1739 r
= pakfire_transaction_request(transaction
, PAKFIRE_JOB_SYNC
, NULL
, 0);
1743 // Solve the transaction
1744 r
= pakfire_transaction_solve(transaction
, 0, &problems
);
1746 BUILD_ERROR(build
, "Could not install build dependencies:\n%s\n", problems
);
1750 // If there are changes, we have to update the snapshot
1751 if (pakfire_transaction_count(transaction
))
1752 *snapshot_needs_update
= 1;
1754 // Run the transaction
1755 r
= pakfire_transaction_run(transaction
);
1761 pakfire_transaction_unref(transaction
);
1769 Initializes the build environment
1771 static int pakfire_build_init(struct pakfire_build
* build
) {
1774 // Don't do it again
1776 DEBUG(build
->pakfire
, "Build environment has already been initialized\n");
1780 const int use_snapshot
= !pakfire_build_has_flag(build
, PAKFIRE_BUILD_DISABLE_SNAPSHOT
);
1782 // Tells us whether we need to (re-)create the snapshot
1783 int snapshot_needs_update
= 0;
1787 r
= pakfire_snapshot_restore(build
->pakfire
);
1792 // Install or update any build dependencies
1793 r
= pakfire_build_install_packages(build
, &snapshot_needs_update
);
1797 // Update the snapshot if there were changes
1798 if (use_snapshot
&& snapshot_needs_update
) {
1799 // Store the snapshot
1800 r
= pakfire_snapshot_store(build
->pakfire
);
1805 // Mark as initialized
1811 static int pakfire_build_read_makefile(struct pakfire_build
* build
,
1812 struct pakfire_parser
** parser
, struct pakfire_package
* package
) {
1813 char path
[PATH_MAX
];
1816 struct pakfire_parser_error
* error
= NULL
;
1818 const char* nevra
= pakfire_package_get_string(package
, PAKFIRE_PKG_NEVRA
);
1819 const char* name
= pakfire_package_get_string(package
, PAKFIRE_PKG_NAME
);
1821 // Compose path to makefile
1822 r
= pakfire_path(build
->pakfire
, path
, "/usr/src/packages/%s/%s.nm", nevra
, name
);
1827 r
= pakfire_read_makefile(parser
, build
->pakfire
, path
, &error
);
1830 BUILD_ERROR(build
, "Could not parse makefile %s: %s\n", path
,
1831 pakfire_parser_error_get_message(error
));
1833 BUILD_ERROR(build
, "Could not parse makefile %s: %m\n", path
);
1840 const char* buildroot
= pakfire_relpath(build
->pakfire
, build
->buildroot
);
1842 pakfire_parser_set(*parser
, NULL
, "BUILDROOT", buildroot
, 0);
1846 pakfire_parser_error_unref(error
);
1851 static int pakfire_build_perform(struct pakfire_build
* build
,
1852 struct pakfire_parser
* makefile
) {
1855 // Prepare the build
1856 r
= pakfire_build_stage(build
, makefile
, "prepare");
1860 // Perform the build
1861 r
= pakfire_build_stage(build
, makefile
, "build");
1866 if (!pakfire_build_has_flag(build
, PAKFIRE_BUILD_DISABLE_TESTS
)) {
1867 r
= pakfire_build_stage(build
, makefile
, "test");
1872 // Install everything
1873 r
= pakfire_build_stage(build
, makefile
, "install");
1877 // Run post build checks
1878 r
= pakfire_build_run_post_build_checks(build
);
1880 BUILD_ERROR(build
, "Post build checks failed\n");
1884 // Run post build scripts
1885 r
= pakfire_build_run_post_build_scripts(build
);
1893 // Drop to a shell for debugging
1894 if (pakfire_build_has_flag(build
, PAKFIRE_BUILD_INTERACTIVE
))
1895 pakfire_build_shell(build
);
1900 static int __pakfire_build_unpackaged_file(struct pakfire
* pakfire
,
1901 struct pakfire_file
* file
, void* p
) {
1902 struct pakfire_build
* build
= p
;
1904 char* s
= pakfire_file_dump(file
, PAKFIRE_FILE_DUMP_FULL
);
1906 BUILD_ERROR(build
, "%s\n", s
);
1913 static int pakfire_build_check_unpackaged_files(struct pakfire_build
* build
) {
1914 struct pakfire_filelist
* filelist
= NULL
;
1917 // Create a new filelist
1918 r
= pakfire_filelist_create(&filelist
, build
->pakfire
);
1922 // Scan for all files in BUILDROOT
1923 r
= pakfire_filelist_scan(filelist
, build
->buildroot
, NULL
, NULL
, 0);
1927 if (!pakfire_filelist_is_empty(filelist
)) {
1928 BUILD_ERROR(build
, "Unpackaged files found:\n");
1930 r
= pakfire_filelist_walk(filelist
, __pakfire_build_unpackaged_file
, build
, 0);
1940 pakfire_filelist_unref(filelist
);
1945 static int pakfire_build_install_package(struct pakfire
* pakfire
,
1946 struct pakfire_package
* pkg
, void* p
) {
1947 struct pakfire_transaction
* transaction
= (struct pakfire_transaction
*)p
;
1949 return pakfire_transaction_request_package(transaction
,
1950 PAKFIRE_JOB_INSTALL
, pkg
, PAKFIRE_JOB_ESSENTIAL
);
1953 static int pakfire_build_install_test(struct pakfire_build
* build
) {
1954 struct pakfire_transaction
* transaction
= NULL
;
1955 char* problems
= NULL
;
1958 // Create a new transaction
1959 r
= pakfire_transaction_create(&transaction
, build
->pakfire
, 0);
1964 r
= pakfire_packagelist_walk(build
->packages
, pakfire_build_install_package
, transaction
);
1966 // Solve the request
1967 r
= pakfire_transaction_solve(transaction
, PAKFIRE_SOLVE_SHOW_SOLUTIONS
, &problems
);
1975 BUILD_ERROR(build
, "Install test failed:\n%s\n", problems
);
1980 BUILD_ERROR(build
, "Install test failed: %m\n");
1991 static int pakfire_build_post_check(struct pakfire_build
* build
) {
1994 // Check for unpackaged files
1995 r
= pakfire_build_check_unpackaged_files(build
);
1999 // Perform install test
2000 r
= pakfire_build_install_test(build
);
2007 static int pakfire_build_copy_package(struct pakfire
* pakfire
,
2008 struct pakfire_package
* pkg
, void* p
) {
2009 struct pakfire_archive
* archive
= NULL
;
2010 char path
[PATH_MAX
];
2013 const char* target
= (const char*)p
;
2018 // Fetch the package filename
2019 const char* filename
= pakfire_package_get_string(pkg
, PAKFIRE_PKG_FILENAME
);
2025 // Format the destination path
2026 r
= pakfire_string_format(path
, "%s/%s", target
, filename
);
2031 archive
= pakfire_package_get_archive(pkg
);
2037 // Copy it to its destination
2038 r
= pakfire_archive_copy(archive
, path
);
2044 pakfire_archive_unref(archive
);
2049 static int pakfire_build_copy_packages(struct pakfire_build
* build
) {
2050 struct pakfire_repo
* local
= NULL
;
2053 DEBUG(build
->pakfire
, "Copying built packages\n");
2055 // Fetch local repository
2056 local
= pakfire_get_repo(build
->pakfire
, PAKFIRE_REPO_LOCAL
);
2058 // Copy all packages to the target path
2059 if (*build
->target
) {
2060 r
= pakfire_packagelist_walk(build
->packages
,
2061 pakfire_build_copy_package
, build
->target
);
2066 // If we have a local repository, we copy all packages to it
2068 const char* path
= pakfire_repo_get_path(local
);
2070 r
= pakfire_packagelist_walk(build
->packages
,
2071 pakfire_build_copy_package
, (void*)path
);
2079 pakfire_repo_unref(local
);
2084 static int pakfire_build_install_source_package(
2085 struct pakfire_build
* build
, struct pakfire_package
* package
) {
2086 struct pakfire_transaction
* transaction
= NULL
;
2087 char* problems
= NULL
;
2090 // Create a new transaction
2091 r
= pakfire_transaction_create(&transaction
, build
->pakfire
, 0);
2095 // Request to install all essential packages
2096 for (const char** p
= PAKFIRE_BUILD_PACKAGES
; *p
; p
++) {
2097 r
= pakfire_transaction_request(transaction
,
2098 PAKFIRE_JOB_INSTALL
, *p
, PAKFIRE_JOB_ESSENTIAL
);
2103 // Add the source package
2104 r
= pakfire_transaction_request_package(transaction
,
2105 PAKFIRE_JOB_INSTALL
, package
, PAKFIRE_JOB_ESSENTIAL
);
2109 // Solve the transaction
2110 r
= pakfire_transaction_solve(transaction
, 0, &problems
);
2113 BUILD_ERROR(build
, "Could not install the source package:\n%s\n", problems
);
2118 // Set how many packages have been changed
2119 const size_t changes
= pakfire_transaction_count(transaction
);
2121 // Sanity check to see if we actually try to install anything
2123 BUILD_ERROR(build
, "The source package did not get installed\n");
2128 // Run the transaction
2129 r
= pakfire_transaction_run(transaction
);
2135 pakfire_transaction_unref(transaction
);
2142 PAKFIRE_EXPORT
int pakfire_build_exec(struct pakfire_build
* build
, const char* path
) {
2143 struct pakfire_package
* package
= NULL
;
2144 struct pakfire_parser
* makefile
= NULL
;
2145 char* buildroot
= NULL
;
2146 char* problems
= NULL
;
2147 char duration
[TIME_STRING_MAX
];
2150 // Fetch architecture
2151 const char* arch
= pakfire_get_arch(build
->pakfire
);
2154 r
= pakfire_path(build
->pakfire
, build
->buildroot
, "%s",
2155 PAKFIRE_TMP_DIR
"/pakfire-buildroot.XXXXXX");
2159 // Open the source package
2160 r
= pakfire_commandline_add(build
->pakfire
, path
, &package
);
2164 const char* nevra
= pakfire_package_get_string(package
, PAKFIRE_PKG_NEVRA
);
2165 const char* uuid
= pakfire_package_get_string(package
, PAKFIRE_PKG_UUID
);
2167 BUILD_INFO(build
, "Building %s (%s)...\n", nevra
, uuid
);
2169 // Check if this package can be build in this environment
2170 if (!pakfire_package_supports_build_arch(package
, arch
)) {
2171 ERROR(build
->pakfire
, "%s does not support being built on %s\n", nevra
, arch
);
2176 // Perform an install check to see whether we can build this at all
2177 r
= pakfire_package_installcheck(package
, &problems
, 0);
2179 BUILD_ERROR(build
, "Cannot build %s:\n%s\n", nevra
, problems
);
2183 // Initialize the build environment
2184 r
= pakfire_build_init(build
);
2188 // Install the source package
2189 r
= pakfire_build_install_source_package(build
, package
);
2191 BUILD_ERROR(build
, "Could not install the source package: %m\n");
2196 r
= pakfire_build_mount_ccache(build
);
2198 BUILD_ERROR(build
, "Could not mount the ccache: %m\n");
2203 buildroot
= pakfire_mkdtemp(build
->buildroot
);
2205 BUILD_ERROR(build
, "Could not create BUILDROOT: %m\n");
2209 // Open the makefile
2210 r
= pakfire_build_read_makefile(build
, &makefile
, package
);
2214 // Perform the actual build
2215 r
= pakfire_build_perform(build
, makefile
);
2219 // Create the packages
2220 r
= pakfire_build_packages(build
, makefile
);
2222 BUILD_ERROR(build
, "Could not create packages: %m\n");
2226 // Perform post build checks
2227 r
= pakfire_build_post_check(build
);
2231 // Copy packages to their destination
2232 r
= pakfire_build_copy_packages(build
);
2237 r
= pakfire_format_time(duration
, pakfire_build_duration(build
));
2241 BUILD_INFO(build
, "Build successfully completed in %s\n", duration
);
2245 pakfire_parser_unref(makefile
);
2247 pakfire_package_unref(package
);
2251 // Cleanup buildroot
2253 pakfire_rmtree(buildroot
, 0);
2258 static int pakfire_build_mkimage_install_deps(struct pakfire_build
* build
,
2260 struct pakfire_transaction
* transaction
= NULL
;
2261 char requires
[NAME_MAX
];
2262 char* problems
= NULL
;
2266 r
= pakfire_string_format(requires
, "mkimage(%s)", type
);
2270 // Create a new transaction
2271 r
= pakfire_transaction_create(&transaction
, build
->pakfire
, 0);
2273 ERROR(build
->pakfire
, "Could not create transaction: %m\n");
2277 // Add requires to the request
2278 r
= pakfire_transaction_request(transaction
,
2279 PAKFIRE_JOB_INSTALL
, requires
, PAKFIRE_JOB_ESSENTIAL
);
2281 ERROR(build
->pakfire
, "Could not add '%s' to the transaction: %m\n", requires
);
2285 // Solve the request
2286 r
= pakfire_transaction_solve(transaction
, 0, &problems
);
2288 ERROR(build
->pakfire
, "Could not solve the request:\n%s\n", problems
);
2292 // Run the transaction
2293 r
= pakfire_transaction_run(transaction
);
2299 pakfire_transaction_unref(transaction
);
2306 static int pakfire_build_collect_packages(struct pakfire_build
* build
, const char* path
) {
2307 struct pakfire_transaction
* transaction
= NULL
;
2308 char* problems
= NULL
;
2312 // Create a new transaction
2313 r
= pakfire_transaction_create(&transaction
, build
->pakfire
, 0);
2315 ERROR(build
->pakfire
, "Could not create transaction: %m\n");
2320 // Install the base system
2321 r
= pakfire_transaction_request(transaction
,
2322 PAKFIRE_JOB_INSTALL
, "ipfire-release", PAKFIRE_JOB_ESSENTIAL
);
2324 ERROR(build
->pakfire
, "Could not install 'ipfire-release': %m\n");
2328 // Solve the transaction
2329 r
= pakfire_transaction_solve(transaction
, 0, &problems
);
2331 ERROR(build
->pakfire
, "Could not solve request:\n%s\n", problems
);
2335 // Empty transaction?
2336 if (!pakfire_transaction_count(transaction
)) {
2337 ERROR(build
->pakfire
, "The transaction is unexpectedly empty\n");
2342 // Dump the transaction
2343 p
= pakfire_transaction_dump(transaction
, 80);
2345 ERROR(build
->pakfire
, "Could not dump the transaction: %m\n");
2351 INFO(build
->pakfire
, "%s\n", p
);
2353 // Download all packages
2354 r
= pakfire_transaction_download(transaction
);
2356 ERROR(build
->pakfire
, "Could not download the transaction: %m\n");
2360 // Create a repository with all packages in this transaction
2361 r
= pakfire_transaction_compose_repo(transaction
, NULL
, path
);
2363 ERROR(build
->pakfire
, "Could not create repository: %m\n");
2367 // XXX Should we perform installcheck here?
2371 pakfire_transaction_unref(transaction
);
2380 PAKFIRE_EXPORT
int pakfire_build_mkimage(struct pakfire_build
* build
,
2381 const char* type
, FILE* f
) {
2383 char packagesdir
[PATH_MAX
];
2384 char path
[PATH_MAX
];
2392 // Create a path inside the build environment
2393 r
= pakfire_path(build
->pakfire
, path
, "%s",
2394 PAKFIRE_TMP_DIR
"/pakfire-image.XXXXXX");
2398 // Allocate a temporary file
2399 t
= pakfire_mktemp(path
, 0600);
2401 ERROR(build
->pakfire
, "Could not allocate a temporary file: %m\n");
2406 // Create a path for all packages
2407 r
= pakfire_path(build
->pakfire
, packagesdir
, "%s",
2408 PAKFIRE_TMP_DIR
"/pakfire-packages.XXXXXX");
2412 p
= pakfire_mkdtemp(packagesdir
);
2416 // Collect all packages
2417 r
= pakfire_build_collect_packages(build
, packagesdir
);
2421 // Initialize the build environment
2422 r
= pakfire_build_init(build
);
2426 // Install all dependencies
2427 r
= pakfire_build_mkimage_install_deps(build
, type
);
2431 const char* args
[] = {
2433 pakfire_relpath(build
->pakfire
, path
),
2434 pakfire_relpath(build
->pakfire
, packagesdir
),
2438 // Run the mkimage script
2439 r
= pakfire_build_run_script(build
, "mkimage", args
, NULL
, NULL
, NULL
);
2443 // Copy data to its destination
2444 r
= pakfire_copy(build
->pakfire
, t
, f
);
2452 // Unlink the temporary file
2456 // Remove all packages
2457 pakfire_rmtree(packagesdir
, 0);
2462 int pakfire_build_clean(struct pakfire
* pakfire
, int flags
) {
2463 struct pakfire_repo
* local
= NULL
;
2466 // Fetch local repository
2467 local
= pakfire_get_repo(pakfire
, PAKFIRE_REPO_LOCAL
);
2469 ERROR(pakfire
, "Could not find repository %s: %m\n", PAKFIRE_REPO_LOCAL
);
2473 // Destroy everything in it
2474 r
= pakfire_repo_clean(local
, PAKFIRE_REPO_CLEAN_FLAGS_DESTROY
);
2480 pakfire_repo_unref(local
);
2485 static int pakfire_build_install(struct pakfire_build
* build
, const char** packages
) {
2486 struct pakfire_transaction
* transaction
= NULL
;
2487 char* problems
= NULL
;
2490 // Create a new transaction
2491 r
= pakfire_transaction_create(&transaction
, build
->pakfire
, 0);
2495 // Install all packages
2496 for (const char** p
= packages
; *p
; p
++) {
2497 r
= pakfire_transaction_request(transaction
, PAKFIRE_JOB_INSTALL
, *p
, 0);
2502 // Solve the transaction
2503 r
= pakfire_transaction_solve(transaction
, 0, &problems
);
2505 ERROR(build
->pakfire
, "Could not install packages:\n%s\n", problems
);
2509 // Run the transaction
2510 r
= pakfire_transaction_run(transaction
);
2516 pakfire_transaction_unref(transaction
);
2524 This is a convenience function that sets up a build environment and
2525 then drops the user into an interactive shell.
2527 PAKFIRE_EXPORT
int pakfire_shell(struct pakfire
* pakfire
, const char** packages
, int flags
) {
2528 struct pakfire_build
* build
= NULL
;
2531 // Shells are always interactive
2532 flags
|= PAKFIRE_BUILD_INTERACTIVE
;
2534 // Create a new build environment
2535 r
= pakfire_build_create(&build
, pakfire
, NULL
, flags
);
2537 ERROR(pakfire
, "Could not create build: %m\n");
2541 // Initialize the build environment
2542 r
= pakfire_build_init(build
);
2546 // Install any additional packages
2548 r
= pakfire_build_install(build
, packages
);
2554 r
= pakfire_build_shell(build
);
2558 pakfire_build_unref(build
);