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/config.h>
34 #include <pakfire/ctx.h>
35 #include <pakfire/dependencies.h>
36 #include <pakfire/dist.h>
37 #include <pakfire/file.h>
38 #include <pakfire/i18n.h>
39 #include <pakfire/jail.h>
40 #include <pakfire/logging.h>
41 #include <pakfire/mount.h>
42 #include <pakfire/package.h>
43 #include <pakfire/packager.h>
44 #include <pakfire/parser.h>
45 #include <pakfire/path.h>
46 #include <pakfire/private.h>
47 #include <pakfire/problem.h>
48 #include <pakfire/repo.h>
49 #include <pakfire/scriptlet.h>
50 #include <pakfire/snapshot.h>
51 #include <pakfire/solution.h>
52 #include <pakfire/string.h>
53 #include <pakfire/transaction.h>
54 #include <pakfire/util.h>
56 #define CCACHE_DIR "/var/cache/ccache"
58 // We guarantee 2 GiB of memory to every build container
59 #define PAKFIRE_BUILD_MEMORY_GUARANTEED (size_t)2 * 1024 * 1024 * 1024
61 // We allow only up to 2048 processes/threads for every build container
62 #define PAKFIRE_BUILD_PID_LIMIT (size_t)2048
64 // A list of packages that is installed by default
65 static const char* PAKFIRE_BUILD_PACKAGES
[] = {
70 struct pakfire_build
{
71 struct pakfire_ctx
* ctx
;
72 struct pakfire
* pakfire
;
80 char _id
[UUID_STR_LEN
];
82 char target
[PATH_MAX
];
85 struct timespec time_start
;
88 struct pakfire_cgroup
* cgroup
;
91 struct pakfire_jail
* jail
;
93 // The build repository
94 struct pakfire_repo
* repo
;
96 // A list of all built packages
97 struct pakfire_packagelist
* packages
;
100 char buildroot
[PATH_MAX
];
103 char ccache_path
[PATH_MAX
];
109 struct pakfire_build_callbacks
{
111 pakfire_build_log_callback log
;
117 "#!/bin/bash --login\n" \
127 Convenience macro to call the logger callback
129 static int pakfire_build_log(struct pakfire_build
* build
, int priority
, int error
,
130 const char* file
, int line
, const char* function
, const char* format
, ...) {
135 // Don't log messages of a lower loglevel
136 if (pakfire_ctx_get_log_level(build
->ctx
) < priority
)
140 va_start(args
, format
);
141 r
= vasprintf(&buffer
, format
, args
);
144 // Fail if we could not format the message
148 // Send everything to the context logger
149 pakfire_ctx_log(build
->ctx
, priority
, file
, line
, function
, "%s", buffer
);
151 // Call the build logger callback
152 if (build
->callbacks
.log
)
153 r
= build
->callbacks
.log(build
, build
->callbacks
.log_data
, priority
, error
,
154 file
, line
, function
, "%s", buffer
);
165 #define BUILD_LOG_ERRNO(build, priority, r, arg...) \
166 pakfire_build_log(build, priority, r, __FILE__, __LINE__, __FUNCTION__, ## arg)
167 #define BUILD_LOG(build, priority, arg...) BUILD_LOG_ERRNO(build, priority, 0, ## arg)
169 #define BUILD_INFO_ERRNO(build, r, arg...) BUILD_LOG_ERRNO(build, LOG_INFO, r, ## arg)
170 #define BUILD_ERROR_ERRNO(build, r, arg...) BUILD_LOG_ERRNO(build, LOG_ERR, r, ## arg)
171 #define BUILD_DEBUG_ERRNO(build, r, arg...) BUILD_LOG_ERRNO(build, LOG_DEBUG, r, ## arg)
173 #define BUILD_INFO(build, arg...) BUILD_INFO_ERRNO(build, 0, ## arg)
174 #define BUILD_ERROR(build, arg...) BUILD_ERROR_ERRNO(build, 0, ## arg)
175 #define BUILD_DEBUG(build, arg...) BUILD_DEBUG_ERRNO(build, 0, ## arg)
177 static int pakfire_build_has_flag(struct pakfire_build
* build
, int flag
) {
178 return build
->flags
& flag
;
181 static double pakfire_build_duration(struct pakfire_build
* build
) {
185 // What time is it now?
186 r
= clock_gettime(CLOCK_MONOTONIC
, &now
);
190 return pakfire_timespec_delta(&now
, &build
->time_start
);
193 static int pakfire_build_jail_log_callback(struct pakfire
* pakfire
,
194 void* data
, int priority
, const char* line
, size_t length
) {
195 struct pakfire_build
* build
= data
;
197 // Get the runtime of the build
198 const double t
= pakfire_build_duration(build
);
202 const unsigned int h
= (unsigned int)t
/ 3600;
203 const unsigned int m
= (unsigned int)t
% 3600 / 60;
204 const unsigned int s
= (unsigned int)t
% 60;
205 const unsigned int ms
= (unsigned int)(t
* 1000.0) % 1000;
208 return BUILD_LOG(build
, priority
, "[%02d:%02d:%02d.%04d] %.*s", h
, m
, s
, ms
, length
, line
);
211 return BUILD_LOG(build
, priority
, "[ %02d:%02d.%04d] %.*s", m
, s
, ms
, length
, line
);
214 return BUILD_LOG(build
, priority
, "[ %02d.%04d] %.*s", s
, ms
, length
, line
);
217 static int __pakfire_build_setup_repo(struct pakfire
* pakfire
,
218 struct pakfire_repo
* repo
, void* p
) {
223 struct pakfire_build
* build
= (struct pakfire_build
*)p
;
225 // Skip processing the installed repository
226 if (pakfire_repo_is_installed_repo(repo
))
229 // Skip processing any other internal repositories
230 if (pakfire_repo_is_internal(repo
))
233 const char* name
= pakfire_repo_get_name(repo
);
235 BUILD_DEBUG(build
, "Exporting repository configuration for '%s'\n", name
);
237 // Make path for configuration file
238 r
= pakfire_path(build
->pakfire
, path
, PAKFIRE_CONFIG_DIR
"/repos/%s.repo", name
);
240 BUILD_ERROR(build
, "Could not make repository configuration path for %s: %m\n", name
);
244 // Create the parent directory
245 r
= pakfire_mkparentdir(path
, 0755);
249 // Open the repository configuration
250 f
= fopen(path
, "w");
252 BUILD_ERROR(build
, "Could not open %s for writing: %m\n", path
);
256 // Write repository configuration
257 r
= pakfire_repo_write_config(repo
, f
);
259 BUILD_ERROR(build
, "Could not write repository configuration for %s: %m\n", name
);
263 // Bind-mount any local repositories
264 if (pakfire_repo_is_local(repo
)) {
265 const char* _path
= pakfire_repo_get_path(repo
);
267 // Bind-mount the repository data read-only
268 if (pakfire_path_exists(_path
)) {
269 r
= pakfire_jail_bind(build
->jail
, _path
, _path
, MS_RDONLY
);
271 BUILD_ERROR(build
, "Could not bind-mount the repository at %s: %m\n", _path
);
285 This function enables the local repository so that it can be accessed by
286 a pakfire instance running inside the chroot.
288 static int pakfire_build_enable_repos(struct pakfire_build
* build
) {
289 return pakfire_repo_walk(build
->pakfire
, __pakfire_build_setup_repo
, build
);
293 Drops the user into a shell
295 static int pakfire_build_shell(struct pakfire_build
* build
) {
298 // Export local repository if running in interactive mode
299 r
= pakfire_build_enable_repos(build
);
304 return pakfire_jail_shell(build
->jail
);
307 static int pakfire_build_read_script(struct pakfire_build
* build
,
308 const char* filename
, char** buffer
, size_t* length
) {
313 // Compose the source path
314 r
= pakfire_path_append(path
, PAKFIRE_SCRIPTS_DIR
, filename
);
316 ERROR(build
->pakfire
, "Could not compose path for script '%s': %m\n", filename
);
320 BUILD_DEBUG(build
, "Reading script from %s...\n", path
);
323 f
= fopen(path
, "r");
325 BUILD_ERROR(build
, "Could not open script %s: %m\n", path
);
329 // Read the file into a the buffer
330 r
= pakfire_read_file_into_buffer(f
, buffer
, length
);
332 BUILD_ERROR(build
, "Could not read script: %m\n");
343 static int pakfire_build_run_script(
344 struct pakfire_build
* build
,
345 const char* filename
,
347 pakfire_jail_communicate_in communicate_in
,
348 pakfire_jail_communicate_out communicate_out
,
355 BUILD_DEBUG(build
, "Running build script '%s'...\n", filename
);
358 r
= pakfire_build_read_script(build
, filename
, &script
, &length
);
360 ERROR(build
->pakfire
, "Could not read script %s: %m\n", filename
);
364 // Execute the script
365 r
= pakfire_jail_exec_script(build
->jail
, script
, length
, args
,
366 communicate_in
, communicate_out
, data
);
368 BUILD_ERROR(build
, "Script '%s' failed with status %d\n", filename
, r
);
376 struct pakfire_find_deps_ctx
{
377 struct pakfire_build
* build
;
378 struct pakfire_package
* pkg
;
380 struct pakfire_scriptlet
* scriptlet
;
382 const pcre2_code
* filter
;
384 struct pakfire_filelist
* filelist
;
388 static int pakfire_build_make_filter(struct pakfire_build
* build
, pcre2_code
** regex
,
389 struct pakfire_parser
* makefile
, const char* namespace, const char* filter
) {
390 char* pattern
= NULL
;
394 pattern
= pakfire_parser_get(makefile
, namespace, filter
);
396 // Nothing if to if there is no or an empty pattern
397 if (!pattern
|| !*pattern
)
400 DEBUG(build
->pakfire
, "Found filter for %s: %s\n", filter
, pattern
);
402 // Compile the regular expression
403 r
= pakfire_compile_regex(build
->pakfire
, regex
, pattern
);
414 static int pakfire_build_send_filelist(struct pakfire
* pakfire
, void* data
, int fd
) {
415 struct pakfire_find_deps_ctx
* ctx
= (struct pakfire_find_deps_ctx
*)data
;
416 struct pakfire_file
* file
= NULL
;
419 const size_t length
= pakfire_filelist_length(ctx
->filelist
);
421 // Check if we have reached the end of the filelist
422 if (ctx
->i
>= length
)
425 // Fetch the next file
426 file
= pakfire_filelist_get(ctx
->filelist
, ctx
->i
);
428 DEBUG(pakfire
, "Could not fetch file %u: %m\n", ctx
->i
);
433 // Fetch the path of the file
434 const char* path
= pakfire_file_get_path(file
);
436 ERROR(pakfire
, "Received a file with an empty path\n");
441 // Skip files that don't match what we are looking for
442 if (ctx
->class && !pakfire_file_matches_class(file
, ctx
->class))
445 // Write path to stdin
446 r
= dprintf(fd
, "%s\n", path
);
451 // Move on to the next file
459 pakfire_file_unref(file
);
464 static int pakfire_build_process_deps(struct pakfire
* pakfire
,
465 void* data
, int priority
, const char* buffer
, const size_t length
) {
466 const struct pakfire_find_deps_ctx
* ctx
= (struct pakfire_find_deps_ctx
*)data
;
468 pcre2_match_data
* match
= NULL
;
471 // Nothing to do for an empty buffer
472 if (!buffer
|| !*buffer
)
476 // Add every dependency that we have received
478 // Copy the dependency to the stack (and remove the trailing newline)
479 r
= pakfire_string_format(dep
, "%.*s", (int)length
- 1, buffer
);
483 BUILD_DEBUG(ctx
->build
, "Processing dependency: %s\n", dep
);
485 // Filter out any dependencies that are provided by this package
486 if (ctx
->dep
== PAKFIRE_PKG_REQUIRES
) {
487 // If this is a file, we check if it is on the filelist
488 if (pakfire_filelist_contains(ctx
->filelist
, dep
))
491 // Otherwise check if this dependency is provided by this package
492 else if (pakfire_package_matches_dep(ctx
->pkg
, PAKFIRE_PKG_PROVIDES
, dep
))
496 // Check if this dependency should be filtered
498 match
= pcre2_match_data_create_from_pattern(ctx
->filter
, NULL
);
500 ERROR(pakfire
, "Could not allocate PCRE match data: %m\n");
505 r
= pcre2_jit_match(ctx
->filter
, (PCRE2_SPTR
)dep
, length
- 1,
509 if (r
== PCRE2_ERROR_NOMATCH
) {
516 // Fetch the error message
517 r
= pcre2_get_error_message(r
, (PCRE2_UCHAR
*)error
, sizeof(error
));
519 BUILD_ERROR(ctx
->build
, "Could not fetch PCRE error message: %m\n");
524 BUILD_ERROR(ctx
->build
, "Could not match the filter: %s\n", error
);
530 BUILD_DEBUG(ctx
->build
, "Skipping dependency that has been filtered: %s\n", dep
);
537 r
= pakfire_package_add_dep(ctx
->pkg
, ctx
->dep
, buffer
);
539 BUILD_ERROR(ctx
->build
, "Could not process dependency '%s': %m\n", buffer
);
544 // Send everything else to the build logger
546 BUILD_ERROR(ctx
->build
, "%s\n", buffer
);
553 BUILD_DEBUG(ctx
->build
, "Skipping dependency that is provided by the package itself: %s\n", dep
);
557 pcre2_match_data_free(match
);
563 This function is a special way to run a script.
565 It will pipe the filelist into the standard input of the called script
566 and will read any dependencies from the standard output.
568 static int pakfire_build_find_deps(struct pakfire_build
* build
,
569 struct pakfire_package
* pkg
, int dep
, const char* script
,
570 struct pakfire_filelist
* filelist
, int class, const pcre2_code
* filter
) {
571 // Construct the context
572 struct pakfire_find_deps_ctx ctx
= {
580 .filelist
= filelist
,
585 // Skip calling the script if class doesn't match
586 if (class && !pakfire_filelist_matches_class(filelist
, class)) {
587 DEBUG(build
->pakfire
, "Skipping calling %s as class does not match\n", script
);
591 // Pass the buildroot as first argument
592 const char* args
[] = {
593 pakfire_relpath(build
->pakfire
, build
->buildroot
),
598 r
= pakfire_build_run_script(build
, script
, args
,
599 pakfire_build_send_filelist
, pakfire_build_process_deps
, &ctx
);
601 BUILD_ERROR(build
, "%s returned with error %d\n", script
, r
);
606 static int pakfire_build_find_dependencies(struct pakfire_build
* build
,
607 struct pakfire_parser
* makefile
, const char* namespace,
608 struct pakfire_package
* pkg
, struct pakfire_filelist
* filelist
) {
609 pcre2_code
* filter_provides
= NULL
;
610 pcre2_code
* filter_requires
= NULL
;
613 // Fetch the provides filter
614 r
= pakfire_build_make_filter(build
, &filter_provides
,
615 makefile
, namespace, "filter_provides");
617 ERROR(build
->pakfire
, "Provides filter is broken: %m\n");
621 // Fetch the requires filter
622 r
= pakfire_build_make_filter(build
, &filter_requires
,
623 makefile
, namespace, "filter_requires");
625 ERROR(build
->pakfire
, "Requires filter is broken: %m\n");
630 r
= pakfire_build_find_deps(build
, pkg
,
631 PAKFIRE_PKG_PROVIDES
, "find-provides", filelist
, 0, filter_provides
);
635 // Find all Perl provides
636 r
= pakfire_build_find_deps(build
, pkg
,
637 PAKFIRE_PKG_PROVIDES
, "perl.prov", filelist
, PAKFIRE_FILE_PERL
, filter_provides
);
642 r
= pakfire_build_find_deps(build
, pkg
,
643 PAKFIRE_PKG_REQUIRES
, "find-requires", filelist
, 0, filter_requires
);
647 // Find all Perl requires
648 r
= pakfire_build_find_deps(build
, pkg
,
649 PAKFIRE_PKG_REQUIRES
, "perl.req", filelist
, PAKFIRE_FILE_PERL
, filter_requires
);
655 pcre2_code_free(filter_provides
);
657 pcre2_code_free(filter_requires
);
662 static int __pakfire_build_package_mark_config_files(
663 struct pakfire
* pakfire
, struct pakfire_file
* file
, void* data
) {
664 char** configfiles
= (char**)data
;
667 // Skip anything that isn't a regular file
668 if (pakfire_file_get_type(file
) == S_IFREG
)
672 const char* path
= pakfire_file_get_path(file
);
674 // Check if any configfile entry matches against this
675 for (char** configfile
= configfiles
; *configfile
; configfile
++) {
676 if (pakfire_string_startswith(path
, *configfile
)) {
677 r
= pakfire_file_set_flags(file
, PAKFIRE_FILE_CONFIG
);
689 static int pakfire_build_package_mark_config_files(struct pakfire_build
* build
,
690 struct pakfire_filelist
* filelist
, struct pakfire_parser
* makefile
, const char* namespace) {
691 char** configfiles
= NULL
;
694 // Fetch configfiles from makefile
695 r
= pakfire_parser_get_filelist(makefile
, namespace, "configfiles", &configfiles
, NULL
);
697 // If configfiles is not set, there is nothing to do
701 r
= pakfire_filelist_walk(filelist
, __pakfire_build_package_mark_config_files
,
706 for (char** configfile
= configfiles
; *configfile
; configfile
++)
714 static int pakfire_build_package_add_files(struct pakfire_build
* build
,
715 struct pakfire_parser
* makefile
, const char* buildroot
, const char* namespace,
716 struct pakfire_package
* pkg
, struct pakfire_packager
* packager
) {
717 struct pakfire_filelist
* filelist
= NULL
;
718 char** includes
= NULL
;
719 char** excludes
= NULL
;
722 // Fetch filelist from makefile
723 r
= pakfire_parser_get_filelist(makefile
, namespace, "files", &includes
, &excludes
);
725 // No files to package?
726 if (!includes
&& !excludes
)
729 // Allocate a new filelist
730 r
= pakfire_filelist_create(&filelist
, build
->pakfire
);
735 r
= pakfire_filelist_scan(filelist
, build
->buildroot
,
736 (const char**)includes
, (const char**)excludes
, PAKFIRE_FILELIST_EXTENDED_MATCHING
);
740 BUILD_DEBUG(build
, "%zu file(s) found\n", pakfire_filelist_length(filelist
));
742 // Nothing to do if the filelist is empty
743 if (pakfire_filelist_is_empty(filelist
))
747 pakfire_filelist_dump(filelist
, PAKFIRE_FILE_DUMP_FULL
);
750 r
= pakfire_build_find_dependencies(build
, makefile
, namespace, pkg
, filelist
);
752 BUILD_ERROR(build
, "Finding dependencies failed: %m\n");
756 // Mark configuration files
757 r
= pakfire_build_package_mark_config_files(build
, filelist
, makefile
, namespace);
761 // Add all files to the package
762 r
= pakfire_packager_add_files(packager
, filelist
);
768 pakfire_filelist_unref(filelist
);
770 for (char** include
= includes
; *include
; include
++)
775 for (char** exclude
= excludes
; *exclude
; exclude
++)
783 static int pakfire_build_send_scriptlet(struct pakfire
* pakfire
, void* data
, int fd
) {
784 const struct pakfire_find_deps_ctx
* ctx
= (struct pakfire_find_deps_ctx
*)data
;
787 // Fetch the scriptlet
788 const char* p
= pakfire_scriptlet_get_data(ctx
->scriptlet
, &length
);
790 ERROR(pakfire
, "Could not fetch scriptlet: %m\n");
794 // Write it into the pipe
795 ssize_t bytes_written
= write(fd
, p
, length
);
796 if (bytes_written
< 0) {
797 ERROR(pakfire
, "Could not send scriptlet: %m\n");
804 static int pakfire_build_add_scriptlet_requires(struct pakfire_build
* build
,
805 struct pakfire_package
* pkg
, struct pakfire_scriptlet
* scriptlet
) {
808 struct pakfire_find_deps_ctx ctx
= {
811 .dep
= PAKFIRE_PKG_PREREQUIRES
,
812 .scriptlet
= scriptlet
,
815 // Find all pre-requires
816 r
= pakfire_build_run_script(build
, "find-prerequires", NULL
,
817 pakfire_build_send_scriptlet
, pakfire_build_process_deps
, &ctx
);
825 static int pakfire_build_package_add_scriptlet(struct pakfire_build
* build
,
826 struct pakfire_package
* pkg
, struct pakfire_packager
* packager
,
827 const char* type
, const char* data
) {
828 struct pakfire_scriptlet
* scriptlet
= NULL
;
832 // Wrap scriptlet into a shell script
833 r
= asprintf(&shell
, "#!/bin/sh\n\nset -e\n\n%s\n\nexit 0\n", data
);
837 // Create a scriptlet
838 r
= pakfire_scriptlet_create(&scriptlet
, build
->ctx
, type
, shell
, 0);
842 // Add it to the package
843 r
= pakfire_packager_add_scriptlet(packager
, scriptlet
);
845 BUILD_ERROR(build
, "Could not add scriptlet %s\n", type
);
849 // Add scriptlet requirements
850 r
= pakfire_build_add_scriptlet_requires(build
, pkg
, scriptlet
);
852 BUILD_ERROR(build
, "Could not add scriptlet requirements: %m\n");
861 pakfire_scriptlet_unref(scriptlet
);
868 static int pakfire_build_package_add_scriptlets(struct pakfire_build
* build
,
869 struct pakfire_parser
* makefile
, const char* namespace,
870 struct pakfire_package
* pkg
, struct pakfire_packager
* packager
) {
874 for (const char** type
= pakfire_scriptlet_types
; *type
; type
++) {
875 r
= pakfire_string_format(name
, "scriptlet:%s", *type
);
879 // Fetch the scriptlet
880 char* data
= pakfire_parser_get(makefile
, namespace, name
);
884 // Add it to the package
885 r
= pakfire_build_package_add_scriptlet(build
, pkg
, packager
, *type
, data
);
897 static int pakfire_build_package(struct pakfire_build
* build
, struct pakfire_parser
* makefile
,
898 const char* buildroot
, const char* namespace) {
899 struct pakfire_package
* pkg
= NULL
;
900 struct pakfire_packager
* packager
= NULL
;
904 // Expand the handle into the package name
905 char* name
= pakfire_parser_expand(makefile
, "packages", namespace);
907 ERROR(build
->pakfire
, "Could not get package name: %m\n");
911 BUILD_INFO(build
, "Building package '%s'...\n", name
);
912 BUILD_DEBUG(build
, " buildroot = %s\n", buildroot
);
914 // Fetch build architecture
915 const char* arch
= pakfire_get_arch(build
->pakfire
);
919 // Fetch package from makefile
920 r
= pakfire_parser_create_package(makefile
, &pkg
, NULL
, namespace, arch
);
922 BUILD_ERROR(build
, "Could not create package from makefile: %m\n");
927 const char* distribution
= pakfire_parser_get(makefile
, NULL
, "DISTRO_NAME");
929 r
= pakfire_package_set_string(pkg
, PAKFIRE_PKG_DISTRO
, distribution
);
935 pakfire_package_set_uuid(pkg
, PAKFIRE_PKG_BUILD_ID
, build
->id
);
937 // Set source package
938 const char* source_name
= pakfire_parser_get(makefile
, NULL
, "name");
940 r
= pakfire_package_set_string(pkg
, PAKFIRE_PKG_SOURCE_NAME
, source_name
);
946 const char* source_evr
= pakfire_parser_get(makefile
, NULL
, "evr");
948 r
= pakfire_package_set_string(pkg
, PAKFIRE_PKG_SOURCE_EVR
, source_evr
);
954 r
= pakfire_package_set_string(pkg
, PAKFIRE_PKG_SOURCE_ARCH
, "src");
959 r
= pakfire_packager_create(&packager
, build
->pakfire
, pkg
);
964 r
= pakfire_build_package_add_files(build
, makefile
, buildroot
, namespace,
970 r
= pakfire_build_package_add_scriptlets(build
, makefile
, namespace,
975 const char* path
= pakfire_repo_get_path(build
->repo
);
977 // Write the finished package
978 r
= pakfire_packager_finish_to_directory(packager
, path
, NULL
);
980 BUILD_ERROR(build
, "pakfire_packager_finish() failed: %m\n");
984 // Cleanup all packaged files
985 r
= pakfire_packager_cleanup(packager
);
994 pakfire_packager_unref(packager
);
996 pakfire_package_unref(pkg
);
1003 static int pakfire_build_package_dump(struct pakfire_ctx
* ctx
,
1004 struct pakfire_package
* pkg
, void* p
) {
1005 struct pakfire_build
* build
= p
;
1007 char* dump
= pakfire_package_dump(pkg
, PAKFIRE_PKG_DUMP_LONG
);
1011 BUILD_INFO(build
, "%s\n", dump
);
1017 static int pakfire_build_packages(struct pakfire_build
* build
,
1018 struct pakfire_parser
* makefile
) {
1019 BUILD_INFO(build
, "Creating packages...");
1022 const char* buildroot
= pakfire_relpath(build
->pakfire
, build
->buildroot
);
1024 // Fetch a list all all packages
1025 char** packages
= pakfire_parser_list_namespaces(makefile
, "packages.package:*");
1027 BUILD_ERROR(build
, "Could not find any packages: %m\n");
1031 unsigned int num_packages
= 0;
1033 // Count how many packages we have
1034 for (char** package
= packages
; *package
; package
++)
1037 DEBUG(build
->pakfire
, "Found %u package(s)\n", num_packages
);
1039 // Build packages in reverse order
1040 for (int i
= num_packages
- 1; i
>= 0; i
--) {
1041 r
= pakfire_build_package(build
, makefile
, buildroot
, packages
[i
]);
1046 // Rescan the build repository to import all packages again
1047 r
= pakfire_repo_scan(build
->repo
, 0);
1051 // Create a new packagelist
1052 r
= pakfire_packagelist_create(&build
->packages
, build
->ctx
);
1056 // Fetch all packages
1057 r
= pakfire_repo_to_packagelist(build
->repo
, build
->packages
);
1062 r
= pakfire_packagelist_walk(build
->packages
, pakfire_build_package_dump
, build
);
1076 static int pakfire_build_stage(struct pakfire_build
* build
,
1077 struct pakfire_parser
* makefile
, const char* stage
) {
1078 char template[1024];
1080 // Prepare template for this stage
1081 int r
= pakfire_string_format(template, TEMPLATE
, stage
);
1085 // Fetch the environment
1086 char** envp
= pakfire_parser_make_environ(makefile
);
1088 // Create the build script
1089 char* script
= pakfire_parser_expand(makefile
, "build", template);
1091 BUILD_ERROR(build
, "Could not generate the build script for stage '%s': %m\n", stage
);
1095 BUILD_INFO(build
, "Running build stage '%s'\n", stage
);
1097 // Import environment
1098 // XXX is this a good idea?
1099 r
= pakfire_jail_import_env(build
->jail
, (const char**)envp
);
1101 BUILD_ERROR(build
, "Could not import environment: %m\n");
1106 r
= pakfire_jail_exec_script(build
->jail
, script
, strlen(script
),
1107 NULL
, NULL
, pakfire_build_jail_log_callback
, build
);
1109 BUILD_ERROR(build
, "Build stage '%s' failed with status %d\n", stage
, r
);
1113 for (char** e
= envp
; *e
; e
++)
1124 PAKFIRE_BUILD_CLEANUP_FILES
= (1 << 0),
1125 PAKFIRE_BUILD_ERROR_IF_NOT_EMPTY
= (1 << 1),
1126 PAKFIRE_BUILD_SHOW_PROGRESS
= (1 << 2),
1130 This helper function takes a callback which is expected to add any files
1131 to the given filelist which will optionally be all removed after.
1133 static int pakfire_build_post_process_files(struct pakfire_build
* build
,
1134 struct pakfire_filelist
* filelist
, const char* description
,
1135 int (*callback
)(struct pakfire
* pakfire
, struct pakfire_file
* file
, void* data
),
1137 struct pakfire_filelist
* removees
= NULL
;
1140 // Create a filelist with objects that need to be removed
1141 r
= pakfire_filelist_create(&removees
, build
->pakfire
);
1145 // Find all files that need to be removed
1146 r
= pakfire_filelist_walk(filelist
, callback
, removees
,
1147 (flags
& PAKFIRE_BUILD_SHOW_PROGRESS
) ? PAKFIRE_FILELIST_SHOW_PROGRESS
: 0);
1151 if (!pakfire_filelist_is_empty(removees
)) {
1153 BUILD_INFO(build
, "%s\n", description
);
1155 // Show all files which will be removed
1156 pakfire_filelist_dump(removees
, PAKFIRE_FILE_DUMP_FULL
|PAKFIRE_FILE_DUMP_ISSUES
);
1158 // Remove all files on the removee list
1159 if (flags
& PAKFIRE_BUILD_CLEANUP_FILES
) {
1160 r
= pakfire_filelist_cleanup(removees
, PAKFIRE_FILE_CLEANUP_TIDY
);
1164 // Remove all files from the filelist
1165 r
= pakfire_filelist_remove_all(filelist
, removees
);
1170 // Report an error if any files have been found
1171 if (flags
& PAKFIRE_BUILD_ERROR_IF_NOT_EMPTY
)
1177 pakfire_filelist_unref(removees
);
1182 static int __pakfire_build_remove_static_libraries(
1183 struct pakfire
* pakfire
, struct pakfire_file
* file
, void* data
) {
1184 struct pakfire_filelist
* removees
= (struct pakfire_filelist
*)data
;
1185 char path
[PATH_MAX
];
1188 // Find all static libraries
1189 if (pakfire_file_matches_class(file
, PAKFIRE_FILE_STATIC_LIBRARY
)) {
1190 // Copy the filename
1191 r
= pakfire_string_set(path
, pakfire_file_get_abspath(file
));
1195 // Remove the extension
1196 r
= pakfire_path_replace_extension(path
, "so");
1200 // Only delete if there is a shared object with the same name
1201 if (pakfire_path_exists(path
))
1202 return pakfire_filelist_add(removees
, file
);
1208 static int pakfire_build_post_remove_static_libraries(
1209 struct pakfire_build
* build
, struct pakfire_filelist
* filelist
) {
1210 return pakfire_build_post_process_files(build
, filelist
,
1211 "Removing static libaries...",
1212 __pakfire_build_remove_static_libraries
,
1213 PAKFIRE_BUILD_CLEANUP_FILES
);
1216 static int __pakfire_build_remove_libtool_archives(
1217 struct pakfire
* pakfire
, struct pakfire_file
* file
, void* data
) {
1218 struct pakfire_filelist
* removees
= (struct pakfire_filelist
*)data
;
1220 // Find all libtool archive files
1221 if (pakfire_file_matches_class(file
, PAKFIRE_FILE_LIBTOOL_ARCHIVE
))
1222 return pakfire_filelist_add(removees
, file
);
1227 static int pakfire_build_post_remove_libtool_archives(
1228 struct pakfire_build
* build
, struct pakfire_filelist
* filelist
) {
1229 return pakfire_build_post_process_files(build
, filelist
,
1230 "Removing libtool archives...",
1231 __pakfire_build_remove_libtool_archives
,
1232 PAKFIRE_BUILD_CLEANUP_FILES
);
1235 static int __pakfire_build_check_broken_symlinks(
1236 struct pakfire
* pakfire
, struct pakfire_file
* file
, void* data
) {
1237 struct pakfire_filelist
* broken
= (struct pakfire_filelist
*)data
;
1240 // Ignore anything that isn't a symlink
1241 switch (pakfire_file_get_type(file
)) {
1243 if (!pakfire_file_symlink_target_exists(file
)) {
1244 r
= pakfire_filelist_add(broken
, file
);
1251 // Ignore anything that isn't a symlink
1259 static int pakfire_build_post_check_broken_symlinks(
1260 struct pakfire_build
* build
, struct pakfire_filelist
* filelist
) {
1261 return pakfire_build_post_process_files(build
, filelist
,
1262 "Broken symlinks have been found:",
1263 __pakfire_build_check_broken_symlinks
,
1264 PAKFIRE_BUILD_ERROR_IF_NOT_EMPTY
);
1271 static int __pakfire_build_post_check_files(
1272 struct pakfire
* pakfire
, struct pakfire_file
* file
, void* data
) {
1273 struct pakfire_filelist
* broken
= (struct pakfire_filelist
*)data
;
1277 // Check file for issues
1278 r
= pakfire_file_check(file
, &issues
);
1280 //BUILD_ERROR(build, "%s: File Check failed: %m\n", pakfire_file_get_path(file));
1284 // If any issues have been found, consider this file to be on the list
1286 r
= pakfire_filelist_add(broken
, file
);
1294 static int pakfire_build_post_check_files(
1295 struct pakfire_build
* build
, struct pakfire_filelist
* filelist
) {
1296 return pakfire_build_post_process_files(
1300 __pakfire_build_post_check_files
,
1301 PAKFIRE_BUILD_ERROR_IF_NOT_EMPTY
|PAKFIRE_BUILD_SHOW_PROGRESS
);
1304 static int pakfire_build_run_post_build_checks(struct pakfire_build
* build
) {
1305 struct pakfire_filelist
* filelist
= NULL
;
1308 // Create a filelist of all files in the build
1309 r
= pakfire_filelist_create(&filelist
, build
->pakfire
);
1311 BUILD_ERROR(build
, "Could not create filelist: %m\n");
1315 // Scan for all files in BUILDROOT
1316 r
= pakfire_filelist_scan(filelist
, build
->buildroot
, NULL
, NULL
, 0);
1320 // If the filelist is empty, we can are done
1321 if (pakfire_filelist_is_empty(filelist
)) {
1322 DEBUG(build
->pakfire
, "Empty BUILDROOT. Skipping post build checks...\n");
1327 // Remove any static libraries
1328 r
= pakfire_build_post_remove_static_libraries(build
, filelist
);
1332 // Remove any libtool archives
1333 r
= pakfire_build_post_remove_libtool_archives(build
, filelist
);
1337 // Check for any broken symlinks
1338 r
= pakfire_build_post_check_broken_symlinks(build
, filelist
);
1343 r
= pakfire_build_post_check_files(build
, filelist
);
1349 pakfire_filelist_unref(filelist
);
1354 static const char* post_build_scripts
[] = {
1355 "compress-man-pages",
1360 static int pakfire_build_run_post_build_scripts(struct pakfire_build
* build
) {
1362 const char* buildroot
= pakfire_relpath(build
->pakfire
, build
->buildroot
);
1364 // Set default arguments for build scripts
1365 const char* args
[] = {
1369 // Run them one by one
1370 for (const char** script
= post_build_scripts
; *script
; script
++) {
1371 int r
= pakfire_build_run_script(build
, *script
, args
, NULL
, NULL
, NULL
);
1379 static void pakfire_build_free(struct pakfire_build
* build
) {
1380 if (build
->packages
)
1381 pakfire_packagelist_unref(build
->packages
);
1384 pakfire_repo_clean(build
->repo
, PAKFIRE_REPO_CLEAN_FLAGS_DESTROY
);
1385 pakfire_repo_unref(build
->repo
);
1389 pakfire_jail_unref(build
->jail
);
1391 if (build
->cgroup
) {
1392 // Destroy the cgroup
1393 pakfire_cgroup_destroy(build
->cgroup
);
1396 pakfire_cgroup_unref(build
->cgroup
);
1400 pakfire_unref(build
->pakfire
);
1402 pakfire_ctx_unref(build
->ctx
);
1406 static int pakfire_build_parse_id(struct pakfire_build
* build
, const char* id
) {
1409 // Try parsing the Build ID
1411 r
= uuid_parse(id
, build
->id
);
1413 ERROR(build
->pakfire
, "Could not parse build ID '%s'\n", id
);
1417 // Otherwise initialize the Build ID with something random
1419 uuid_generate_random(build
->id
);
1422 // Store the ID as string, too
1423 uuid_unparse_lower(build
->id
, build
->_id
);
1429 Sets up a new cgroup for this build
1431 static int pakfire_build_setup_cgroup(struct pakfire_build
* build
) {
1432 struct pakfire_config
* config
= NULL
;
1433 char path
[PATH_MAX
];
1437 r
= pakfire_string_format(path
, "pakfire/build-%s", build
->_id
);
1439 ERROR(build
->pakfire
, "Could not compose path for cgroup: %m\n");
1443 // Create a new cgroup
1444 r
= pakfire_cgroup_open(&build
->cgroup
, build
->ctx
, path
,
1445 PAKFIRE_CGROUP_ENABLE_ACCOUNTING
);
1447 ERROR(build
->pakfire
, "Could not create cgroup for build %s: %m\n", build
->_id
);
1452 config
= pakfire_get_config(build
->pakfire
);
1456 // Guarantee some minimum memory
1457 size_t memory_guaranteed
= pakfire_config_get_bytes(config
, "build",
1458 "memory_guaranteed", PAKFIRE_BUILD_MEMORY_GUARANTEED
);
1459 if (memory_guaranteed
) {
1460 r
= pakfire_cgroup_set_guaranteed_memory(build
->cgroup
, memory_guaranteed
);
1466 size_t memory_limit
= pakfire_config_get_bytes(config
, "build", "memory_limit", 0);
1468 r
= pakfire_cgroup_set_memory_limit(build
->cgroup
, memory_limit
);
1474 size_t pid_limit
= pakfire_config_get_int(config
, "build",
1475 "pid_limit", PAKFIRE_BUILD_PID_LIMIT
);
1477 r
= pakfire_cgroup_set_pid_limit(build
->cgroup
, pid_limit
);
1484 pakfire_config_unref(config
);
1490 Sets up a new jail for this build
1492 static int pakfire_build_setup_jail(struct pakfire_build
* build
) {
1495 // Create a new jail
1496 r
= pakfire_jail_create(&build
->jail
, build
->pakfire
);
1498 ERROR(build
->pakfire
, "Could not create jail for build %s: %m\n", build
->_id
);
1502 // Connect the jail to our cgroup
1503 r
= pakfire_jail_set_cgroup(build
->jail
, build
->cgroup
);
1505 ERROR(build
->pakfire
, "Could not set cgroup for jail: %m\n");
1509 // Build everything with a slightly lower priority
1510 r
= pakfire_jail_nice(build
->jail
, 5);
1512 ERROR(build
->pakfire
, "Could not set nice level: %m\n");
1521 Sets up the ccache for this build
1523 static int pakfire_build_mount_ccache(struct pakfire_build
* build
) {
1526 // Do nothing if the ccache is disabled
1527 if (pakfire_build_has_flag(build
, PAKFIRE_BUILD_DISABLE_CCACHE
))
1530 // Check that the path is set
1531 if (!*build
->ccache_path
)
1534 // Make sure the path exists
1535 r
= pakfire_mkdir(build
->ccache_path
, 0755);
1537 ERROR(build
->pakfire
, "Could not create %s: %m\n", build
->ccache_path
);
1541 // Bind-mount the directory
1542 return pakfire_jail_bind(build
->jail
, build
->ccache_path
, CCACHE_DIR
,
1543 MS_NOSUID
|MS_NOEXEC
|MS_NODEV
);
1546 static int pakfire_build_setup_ccache(struct pakfire_build
* build
) {
1549 // Check if we want a ccache
1550 if (pakfire_build_has_flag(build
, PAKFIRE_BUILD_DISABLE_CCACHE
)) {
1551 DEBUG(build
->pakfire
, "ccache usage has been disabled for this build\n");
1553 // Set CCACHE_DISABLE=1 so that if ccache is installed, it will disable itself
1554 r
= pakfire_jail_set_env(build
->jail
, "CCACHE_DISABLE", "1");
1556 ERROR(build
->pakfire
, "Could not disable ccache: %m\n");
1564 r
= pakfire_jail_set_env(build
->jail
, "CCACHE_DIR", CCACHE_DIR
);
1566 ERROR(build
->pakfire
, "Could not set ccache directory: %m\n");
1570 // Set CCACHE_TEMPDIR
1571 r
= pakfire_jail_set_env(build
->jail
, "CCACHE_TEMPDIR", "/tmp");
1573 ERROR(build
->pakfire
, "Could not set ccache tempdir: %m\n");
1577 // Set a default path
1578 r
= pakfire_cache_path(build
->pakfire
, build
->ccache_path
, "%s", "ccache");
1585 static int pakfire_build_setup_repo(struct pakfire_build
* build
) {
1586 char path
[PATH_MAX
] = PAKFIRE_TMP_DIR
"/pakfire-build-repo.XXXXXX";
1590 // Create a new repository
1591 r
= pakfire_repo_create(&build
->repo
, build
->pakfire
, PAKFIRE_REPO_RESULT
);
1593 ERROR(build
->pakfire
, "Could not create repository %s: %m", PAKFIRE_REPO_RESULT
);
1598 pakfire_repo_set_description(build
->repo
, _("Build Repository"));
1600 // Create a temporary directory
1601 const char* p
= pakfire_mkdtemp(path
);
1603 ERROR(build
->pakfire
, "Could not create a the build repository: %m\n");
1608 r
= pakfire_string_format(url
, "file://%s", path
);
1613 pakfire_repo_set_baseurl(build
->repo
, url
);
1618 static int pakfire_build_set_time_start(struct pakfire_build
* build
) {
1621 // Fetch current time
1622 r
= clock_gettime(CLOCK_MONOTONIC
, &build
->time_start
);
1624 ERROR(build
->pakfire
, "Could not fetch start time: %m\n");
1629 PAKFIRE_EXPORT
int pakfire_build_create(struct pakfire_build
** build
,
1630 struct pakfire
* pakfire
, const char* id
, int flags
) {
1633 // Allocate build object
1634 struct pakfire_build
* b
= calloc(1, sizeof(*b
));
1638 // Reference the context
1639 b
->ctx
= pakfire_ctx(pakfire
);
1641 // Reference pakfire
1642 b
->pakfire
= pakfire_ref(pakfire
);
1644 // Initialize reference counter
1651 r
= pakfire_build_set_time_start(b
);
1656 r
= pakfire_build_parse_id(b
, id
);
1661 r
= pakfire_build_setup_repo(b
);
1666 r
= pakfire_build_setup_cgroup(b
);
1671 r
= pakfire_build_setup_jail(b
);
1676 r
= pakfire_build_setup_ccache(b
);
1684 pakfire_build_free(b
);
1688 PAKFIRE_EXPORT
struct pakfire_build
* pakfire_build_ref(struct pakfire_build
* build
) {
1694 PAKFIRE_EXPORT
struct pakfire_build
* pakfire_build_unref(struct pakfire_build
* build
) {
1695 if (--build
->nrefs
> 0)
1698 pakfire_build_free(build
);
1702 PAKFIRE_EXPORT
void pakfire_build_set_log_callback(struct pakfire_build
* build
,
1703 pakfire_build_log_callback callback
, void* data
) {
1704 build
->callbacks
.log
= callback
;
1705 build
->callbacks
.log_data
= data
;
1708 PAKFIRE_EXPORT
int pakfire_build_set_ccache_path(
1709 struct pakfire_build
* build
, const char* path
) {
1710 // Check if this can be called
1711 if (pakfire_build_has_flag(build
, PAKFIRE_BUILD_DISABLE_CCACHE
))
1714 // Check input value
1715 if (!path
|| !*path
)
1719 return pakfire_string_set(build
->ccache_path
, path
);
1722 PAKFIRE_EXPORT
int pakfire_build_set_target(
1723 struct pakfire_build
* build
, const char* target
) {
1724 return pakfire_string_set(build
->target
, target
);
1727 static int pakfire_build_install_packages(
1728 struct pakfire_build
* build
, int* snapshot_needs_update
) {
1729 struct pakfire_transaction
* transaction
= NULL
;
1730 char* problems
= NULL
;
1733 // Create a new transaction
1734 r
= pakfire_transaction_create(&transaction
, build
->pakfire
, 0);
1738 // Install all build dependencies
1739 for (const char** p
= PAKFIRE_BUILD_PACKAGES
; *p
; p
++) {
1740 r
= pakfire_transaction_request(transaction
,
1741 PAKFIRE_JOB_INSTALL
, *p
, PAKFIRE_JOB_ESSENTIAL
);
1746 // Also update everything that has already been installed
1747 r
= pakfire_transaction_request(transaction
, PAKFIRE_JOB_SYNC
, NULL
, 0);
1751 // Solve the transaction
1752 r
= pakfire_transaction_solve(transaction
, 0, &problems
);
1754 BUILD_ERROR(build
, "Could not install build dependencies:\n%s\n", problems
);
1758 // If there are changes, we have to update the snapshot
1759 if (pakfire_transaction_count(transaction
))
1760 *snapshot_needs_update
= 1;
1762 // Run the transaction
1763 r
= pakfire_transaction_run(transaction
);
1769 pakfire_transaction_unref(transaction
);
1777 Initializes the build environment
1779 static int pakfire_build_init(struct pakfire_build
* build
) {
1782 // Don't do it again
1784 DEBUG(build
->pakfire
, "Build environment has already been initialized\n");
1788 const int use_snapshot
= !pakfire_build_has_flag(build
, PAKFIRE_BUILD_DISABLE_SNAPSHOT
);
1790 // Tells us whether we need to (re-)create the snapshot
1791 int snapshot_needs_update
= 0;
1795 r
= pakfire_snapshot_restore(build
->pakfire
);
1800 // Install or update any build dependencies
1801 r
= pakfire_build_install_packages(build
, &snapshot_needs_update
);
1805 // Update the snapshot if there were changes
1806 if (use_snapshot
&& snapshot_needs_update
) {
1807 // Store the snapshot
1808 r
= pakfire_snapshot_store(build
->pakfire
);
1813 // Mark as initialized
1819 static int pakfire_build_read_makefile(struct pakfire_build
* build
,
1820 struct pakfire_parser
** parser
, struct pakfire_package
* package
) {
1821 char path
[PATH_MAX
];
1824 struct pakfire_parser_error
* error
= NULL
;
1826 const char* nevra
= pakfire_package_get_string(package
, PAKFIRE_PKG_NEVRA
);
1827 const char* name
= pakfire_package_get_string(package
, PAKFIRE_PKG_NAME
);
1829 // Compose path to makefile
1830 r
= pakfire_path(build
->pakfire
, path
, "/usr/src/packages/%s/%s.nm", nevra
, name
);
1835 r
= pakfire_read_makefile(parser
, build
->pakfire
, path
, &error
);
1838 BUILD_ERROR(build
, "Could not parse makefile %s: %s\n", path
,
1839 pakfire_parser_error_get_message(error
));
1841 BUILD_ERROR(build
, "Could not parse makefile %s: %m\n", path
);
1848 const char* buildroot
= pakfire_relpath(build
->pakfire
, build
->buildroot
);
1850 pakfire_parser_set(*parser
, NULL
, "BUILDROOT", buildroot
, 0);
1854 pakfire_parser_error_unref(error
);
1859 static int pakfire_build_perform(struct pakfire_build
* build
,
1860 struct pakfire_parser
* makefile
) {
1863 // Prepare the build
1864 r
= pakfire_build_stage(build
, makefile
, "prepare");
1868 // Perform the build
1869 r
= pakfire_build_stage(build
, makefile
, "build");
1874 if (!pakfire_build_has_flag(build
, PAKFIRE_BUILD_DISABLE_TESTS
)) {
1875 r
= pakfire_build_stage(build
, makefile
, "test");
1880 // Install everything
1881 r
= pakfire_build_stage(build
, makefile
, "install");
1885 // Run post build checks
1886 r
= pakfire_build_run_post_build_checks(build
);
1888 BUILD_ERROR(build
, "Post build checks failed\n");
1892 // Run post build scripts
1893 r
= pakfire_build_run_post_build_scripts(build
);
1901 // Drop to a shell for debugging
1902 if (pakfire_build_has_flag(build
, PAKFIRE_BUILD_INTERACTIVE
))
1903 pakfire_build_shell(build
);
1908 static int __pakfire_build_unpackaged_file(struct pakfire
* pakfire
,
1909 struct pakfire_file
* file
, void* p
) {
1910 struct pakfire_build
* build
= p
;
1912 char* s
= pakfire_file_dump(file
, PAKFIRE_FILE_DUMP_FULL
);
1914 BUILD_ERROR(build
, "%s\n", s
);
1921 static int pakfire_build_check_unpackaged_files(struct pakfire_build
* build
) {
1922 struct pakfire_filelist
* filelist
= NULL
;
1925 // Create a new filelist
1926 r
= pakfire_filelist_create(&filelist
, build
->pakfire
);
1930 // Scan for all files in BUILDROOT
1931 r
= pakfire_filelist_scan(filelist
, build
->buildroot
, NULL
, NULL
, 0);
1935 if (!pakfire_filelist_is_empty(filelist
)) {
1936 BUILD_ERROR(build
, "Unpackaged files found:\n");
1938 r
= pakfire_filelist_walk(filelist
, __pakfire_build_unpackaged_file
, build
, 0);
1948 pakfire_filelist_unref(filelist
);
1953 static int pakfire_build_install_package(struct pakfire_ctx
* ctx
,
1954 struct pakfire_package
* pkg
, void* p
) {
1955 struct pakfire_transaction
* transaction
= (struct pakfire_transaction
*)p
;
1957 return pakfire_transaction_request_package(transaction
,
1958 PAKFIRE_JOB_INSTALL
, pkg
, PAKFIRE_JOB_ESSENTIAL
);
1961 static int pakfire_build_install_test(struct pakfire_build
* build
) {
1962 struct pakfire_transaction
* transaction
= NULL
;
1963 char* problems
= NULL
;
1966 // Create a new transaction
1967 r
= pakfire_transaction_create(&transaction
, build
->pakfire
, 0);
1972 r
= pakfire_packagelist_walk(build
->packages
, pakfire_build_install_package
, transaction
);
1974 // Solve the request
1975 r
= pakfire_transaction_solve(transaction
, PAKFIRE_SOLVE_SHOW_SOLUTIONS
, &problems
);
1983 BUILD_ERROR(build
, "Install test failed:\n%s\n", problems
);
1988 BUILD_ERROR(build
, "Install test failed: %m\n");
1999 static int pakfire_build_post_check(struct pakfire_build
* build
) {
2002 // Check for unpackaged files
2003 r
= pakfire_build_check_unpackaged_files(build
);
2007 // Perform install test
2008 r
= pakfire_build_install_test(build
);
2015 static int pakfire_build_copy_package(struct pakfire_ctx
* ctx
,
2016 struct pakfire_package
* pkg
, void* p
) {
2017 struct pakfire_archive
* archive
= NULL
;
2018 char path
[PATH_MAX
];
2021 const char* target
= (const char*)p
;
2026 // Fetch the package filename
2027 const char* filename
= pakfire_package_get_string(pkg
, PAKFIRE_PKG_FILENAME
);
2033 // Format the destination path
2034 r
= pakfire_string_format(path
, "%s/%s", target
, filename
);
2039 archive
= pakfire_package_get_archive(pkg
);
2045 // Copy it to its destination
2046 r
= pakfire_archive_copy(archive
, path
);
2052 pakfire_archive_unref(archive
);
2057 static int pakfire_build_copy_packages(struct pakfire_build
* build
) {
2058 struct pakfire_repo
* local
= NULL
;
2061 DEBUG(build
->pakfire
, "Copying built packages\n");
2063 // Fetch local repository
2064 local
= pakfire_get_repo(build
->pakfire
, PAKFIRE_REPO_LOCAL
);
2066 // Copy all packages to the target path
2067 if (*build
->target
) {
2068 r
= pakfire_packagelist_walk(build
->packages
,
2069 pakfire_build_copy_package
, build
->target
);
2074 // If we have a local repository, we copy all packages to it
2076 const char* path
= pakfire_repo_get_path(local
);
2078 r
= pakfire_packagelist_walk(build
->packages
,
2079 pakfire_build_copy_package
, (void*)path
);
2087 pakfire_repo_unref(local
);
2092 static int pakfire_build_install_source_package(
2093 struct pakfire_build
* build
, struct pakfire_package
* package
) {
2094 struct pakfire_transaction
* transaction
= NULL
;
2095 char* problems
= NULL
;
2098 // Create a new transaction
2099 r
= pakfire_transaction_create(&transaction
, build
->pakfire
, 0);
2103 // Request to install all essential packages
2104 for (const char** p
= PAKFIRE_BUILD_PACKAGES
; *p
; p
++) {
2105 r
= pakfire_transaction_request(transaction
,
2106 PAKFIRE_JOB_INSTALL
, *p
, PAKFIRE_JOB_ESSENTIAL
);
2111 // Add the source package
2112 r
= pakfire_transaction_request_package(transaction
,
2113 PAKFIRE_JOB_INSTALL
, package
, PAKFIRE_JOB_ESSENTIAL
);
2117 // Solve the transaction
2118 r
= pakfire_transaction_solve(transaction
, 0, &problems
);
2121 BUILD_ERROR(build
, "Could not install the source package:\n%s\n", problems
);
2126 // Set how many packages have been changed
2127 const size_t changes
= pakfire_transaction_count(transaction
);
2129 // Sanity check to see if we actually try to install anything
2131 BUILD_ERROR(build
, "The source package did not get installed\n");
2136 // Run the transaction
2137 r
= pakfire_transaction_run(transaction
);
2143 pakfire_transaction_unref(transaction
);
2150 PAKFIRE_EXPORT
int pakfire_build_exec(struct pakfire_build
* build
, const char* path
) {
2151 struct pakfire_package
* package
= NULL
;
2152 struct pakfire_parser
* makefile
= NULL
;
2153 char* buildroot
= NULL
;
2154 char* problems
= NULL
;
2155 char duration
[TIME_STRING_MAX
];
2158 // Fetch architecture
2159 const char* arch
= pakfire_get_arch(build
->pakfire
);
2162 r
= pakfire_path(build
->pakfire
, build
->buildroot
, "%s",
2163 PAKFIRE_TMP_DIR
"/pakfire-buildroot.XXXXXX");
2167 // Open the source package
2168 r
= pakfire_commandline_add(build
->pakfire
, path
, &package
);
2172 const char* nevra
= pakfire_package_get_string(package
, PAKFIRE_PKG_NEVRA
);
2173 const char* uuid
= pakfire_package_get_string(package
, PAKFIRE_PKG_UUID
);
2175 BUILD_INFO(build
, "Building %s (%s)...\n", nevra
, uuid
);
2177 // Check if this package can be build in this environment
2178 if (!pakfire_package_supports_build_arch(package
, arch
)) {
2179 ERROR(build
->pakfire
, "%s does not support being built on %s\n", nevra
, arch
);
2184 // Perform an install check to see whether we can build this at all
2185 r
= pakfire_package_installcheck(package
, &problems
, 0);
2187 BUILD_ERROR(build
, "Cannot build %s:\n%s\n", nevra
, problems
);
2191 // Initialize the build environment
2192 r
= pakfire_build_init(build
);
2196 // Install the source package
2197 r
= pakfire_build_install_source_package(build
, package
);
2199 BUILD_ERROR(build
, "Could not install the source package: %m\n");
2204 r
= pakfire_build_mount_ccache(build
);
2206 BUILD_ERROR(build
, "Could not mount the ccache: %m\n");
2211 buildroot
= pakfire_mkdtemp(build
->buildroot
);
2213 BUILD_ERROR(build
, "Could not create BUILDROOT: %m\n");
2217 // Open the makefile
2218 r
= pakfire_build_read_makefile(build
, &makefile
, package
);
2222 // Perform the actual build
2223 r
= pakfire_build_perform(build
, makefile
);
2227 // Create the packages
2228 r
= pakfire_build_packages(build
, makefile
);
2230 BUILD_ERROR(build
, "Could not create packages: %m\n");
2234 // Perform post build checks
2235 r
= pakfire_build_post_check(build
);
2239 // Copy packages to their destination
2240 r
= pakfire_build_copy_packages(build
);
2245 r
= pakfire_format_time(duration
, pakfire_build_duration(build
));
2249 BUILD_INFO(build
, "Build successfully completed in %s\n", duration
);
2253 pakfire_parser_unref(makefile
);
2255 pakfire_package_unref(package
);
2259 // Cleanup buildroot
2261 pakfire_rmtree(buildroot
, 0);
2266 static int pakfire_build_mkimage_install_deps(struct pakfire_build
* build
,
2268 struct pakfire_transaction
* transaction
= NULL
;
2269 char requires
[NAME_MAX
];
2270 char* problems
= NULL
;
2274 r
= pakfire_string_format(requires
, "mkimage(%s)", type
);
2278 // Create a new transaction
2279 r
= pakfire_transaction_create(&transaction
, build
->pakfire
, 0);
2281 ERROR(build
->pakfire
, "Could not create transaction: %m\n");
2285 // Add requires to the request
2286 r
= pakfire_transaction_request(transaction
,
2287 PAKFIRE_JOB_INSTALL
, requires
, PAKFIRE_JOB_ESSENTIAL
);
2289 ERROR(build
->pakfire
, "Could not add '%s' to the transaction: %m\n", requires
);
2293 // Solve the request
2294 r
= pakfire_transaction_solve(transaction
, 0, &problems
);
2296 ERROR(build
->pakfire
, "Could not solve the request:\n%s\n", problems
);
2300 // Run the transaction
2301 r
= pakfire_transaction_run(transaction
);
2307 pakfire_transaction_unref(transaction
);
2314 static int pakfire_build_collect_packages(struct pakfire_build
* build
, const char* path
) {
2315 struct pakfire_transaction
* transaction
= NULL
;
2316 char* problems
= NULL
;
2320 // Create a new transaction
2321 r
= pakfire_transaction_create(&transaction
, build
->pakfire
, 0);
2323 ERROR(build
->pakfire
, "Could not create transaction: %m\n");
2328 // Install the base system
2329 r
= pakfire_transaction_request(transaction
,
2330 PAKFIRE_JOB_INSTALL
, "ipfire-release", PAKFIRE_JOB_ESSENTIAL
);
2332 ERROR(build
->pakfire
, "Could not install 'ipfire-release': %m\n");
2336 // Solve the transaction
2337 r
= pakfire_transaction_solve(transaction
, 0, &problems
);
2339 ERROR(build
->pakfire
, "Could not solve request:\n%s\n", problems
);
2343 // Empty transaction?
2344 if (!pakfire_transaction_count(transaction
)) {
2345 ERROR(build
->pakfire
, "The transaction is unexpectedly empty\n");
2350 // Dump the transaction
2351 p
= pakfire_transaction_dump(transaction
, 80);
2353 ERROR(build
->pakfire
, "Could not dump the transaction: %m\n");
2359 INFO(build
->pakfire
, "%s\n", p
);
2361 // Download all packages
2362 r
= pakfire_transaction_download(transaction
);
2364 ERROR(build
->pakfire
, "Could not download the transaction: %m\n");
2368 // Create a repository with all packages in this transaction
2369 r
= pakfire_transaction_compose_repo(transaction
, NULL
, path
);
2371 ERROR(build
->pakfire
, "Could not create repository: %m\n");
2375 // XXX Should we perform installcheck here?
2379 pakfire_transaction_unref(transaction
);
2388 PAKFIRE_EXPORT
int pakfire_build_mkimage(struct pakfire_build
* build
,
2389 const char* type
, FILE* f
) {
2391 char packagesdir
[PATH_MAX
];
2392 char path
[PATH_MAX
];
2400 // Create a path inside the build environment
2401 r
= pakfire_path(build
->pakfire
, path
, "%s",
2402 PAKFIRE_TMP_DIR
"/pakfire-image.XXXXXX");
2406 // Allocate a temporary file
2407 t
= pakfire_mktemp(path
, 0600);
2409 ERROR(build
->pakfire
, "Could not allocate a temporary file: %m\n");
2414 // Create a path for all packages
2415 r
= pakfire_path(build
->pakfire
, packagesdir
, "%s",
2416 PAKFIRE_TMP_DIR
"/pakfire-packages.XXXXXX");
2420 p
= pakfire_mkdtemp(packagesdir
);
2424 // Collect all packages
2425 r
= pakfire_build_collect_packages(build
, packagesdir
);
2429 // Initialize the build environment
2430 r
= pakfire_build_init(build
);
2434 // Install all dependencies
2435 r
= pakfire_build_mkimage_install_deps(build
, type
);
2439 const char* args
[] = {
2441 pakfire_relpath(build
->pakfire
, path
),
2442 pakfire_relpath(build
->pakfire
, packagesdir
),
2446 // Run the mkimage script
2447 r
= pakfire_build_run_script(build
, "mkimage", args
, NULL
, NULL
, NULL
);
2451 // Copy data to its destination
2452 r
= pakfire_copy(build
->pakfire
, t
, f
);
2460 // Unlink the temporary file
2464 // Remove all packages
2465 pakfire_rmtree(packagesdir
, 0);
2470 int pakfire_build_clean(struct pakfire
* pakfire
, int flags
) {
2471 struct pakfire_repo
* local
= NULL
;
2474 // Fetch local repository
2475 local
= pakfire_get_repo(pakfire
, PAKFIRE_REPO_LOCAL
);
2477 ERROR(pakfire
, "Could not find repository %s: %m\n", PAKFIRE_REPO_LOCAL
);
2481 // Destroy everything in it
2482 r
= pakfire_repo_clean(local
, PAKFIRE_REPO_CLEAN_FLAGS_DESTROY
);
2488 pakfire_repo_unref(local
);
2493 static int pakfire_build_install(struct pakfire_build
* build
, const char** packages
) {
2494 struct pakfire_transaction
* transaction
= NULL
;
2495 char* problems
= NULL
;
2498 // Create a new transaction
2499 r
= pakfire_transaction_create(&transaction
, build
->pakfire
, 0);
2503 // Install all packages
2504 for (const char** p
= packages
; *p
; p
++) {
2505 r
= pakfire_transaction_request(transaction
, PAKFIRE_JOB_INSTALL
, *p
, 0);
2510 // Solve the transaction
2511 r
= pakfire_transaction_solve(transaction
, 0, &problems
);
2513 ERROR(build
->pakfire
, "Could not install packages:\n%s\n", problems
);
2517 // Run the transaction
2518 r
= pakfire_transaction_run(transaction
);
2524 pakfire_transaction_unref(transaction
);
2532 This is a convenience function that sets up a build environment and
2533 then drops the user into an interactive shell.
2535 PAKFIRE_EXPORT
int pakfire_shell(struct pakfire
* pakfire
, const char** packages
, int flags
) {
2536 struct pakfire_build
* build
= NULL
;
2539 // Shells are always interactive
2540 flags
|= PAKFIRE_BUILD_INTERACTIVE
;
2542 // Create a new build environment
2543 r
= pakfire_build_create(&build
, pakfire
, NULL
, flags
);
2545 ERROR(pakfire
, "Could not create build: %m\n");
2549 // Initialize the build environment
2550 r
= pakfire_build_init(build
);
2554 // Install any additional packages
2556 r
= pakfire_build_install(build
, packages
);
2562 r
= pakfire_build_shell(build
);
2566 pakfire_build_unref(build
);