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
,
346 const char* args
[]) {
352 BUILD_DEBUG(build
, "Running build script '%s'...\n", filename
);
355 r
= pakfire_build_read_script(build
, filename
, &script
, &length
);
357 ERROR(build
->pakfire
, "Could not read script %s: %m\n", filename
);
361 // Execute the script
362 r
= pakfire_jail_exec_script(build
->jail
, script
, length
, args
);
364 BUILD_ERROR(build
, "Script '%s' failed with status %d\n", filename
, r
);
372 struct pakfire_find_deps_ctx
{
373 struct pakfire_build
* build
;
374 struct pakfire_package
* pkg
;
376 struct pakfire_scriptlet
* scriptlet
;
378 const pcre2_code
* filter
;
380 struct pakfire_filelist
* filelist
;
384 static int pakfire_build_make_filter(struct pakfire_build
* build
, pcre2_code
** regex
,
385 struct pakfire_parser
* makefile
, const char* namespace, const char* filter
) {
386 char* pattern
= NULL
;
390 pattern
= pakfire_parser_get(makefile
, namespace, filter
);
392 // Nothing if to if there is no or an empty pattern
393 if (!pattern
|| !*pattern
)
396 DEBUG(build
->pakfire
, "Found filter for %s: %s\n", filter
, pattern
);
398 // Compile the regular expression
399 r
= pakfire_compile_regex(build
->pakfire
, regex
, pattern
);
410 static int pakfire_build_send_filelist(struct pakfire_ctx
* ctx
,
411 struct pakfire_jail
* jail
, void* data
, int fd
) {
412 struct pakfire_find_deps_ctx
* p
= (struct pakfire_find_deps_ctx
*)data
;
413 struct pakfire_file
* file
= NULL
;
416 const size_t length
= pakfire_filelist_length(p
->filelist
);
418 // Check if we have reached the end of the filelist
422 // Fetch the next file
423 file
= pakfire_filelist_get(p
->filelist
, p
->i
);
425 CTX_DEBUG(ctx
, "Could not fetch file %u: %m\n", p
->i
);
430 // Fetch the path of the file
431 const char* path
= pakfire_file_get_path(file
);
433 CTX_ERROR(ctx
, "Received a file with an empty path\n");
438 // Skip files that don't match what we are looking for
439 if (p
->class && !pakfire_file_matches_class(file
, p
->class))
442 // Write path to stdin
443 r
= dprintf(fd
, "%s\n", path
);
448 // Move on to the next file
456 pakfire_file_unref(file
);
461 static int pakfire_build_process_deps(struct pakfire_ctx
* ctx
, struct pakfire_jail
* jail
,
462 void* data
, const char* buffer
, const size_t length
) {
463 const struct pakfire_find_deps_ctx
* p
= (struct pakfire_find_deps_ctx
*)data
;
465 pcre2_match_data
* match
= NULL
;
468 // Nothing to do for an empty buffer
469 if (!buffer
|| !*buffer
)
472 // Copy the dependency to the stack (and remove the trailing newline)
473 r
= pakfire_string_format(dep
, "%.*s", (int)length
- 1, buffer
);
477 BUILD_DEBUG(p
->build
, "Processing dependency: %s\n", dep
);
479 // Filter out any dependencies that are provided by this package
480 if (p
->dep
== PAKFIRE_PKG_REQUIRES
) {
481 // If this is a file, we check if it is on the filelist
482 if (pakfire_filelist_contains(p
->filelist
, dep
))
485 // Otherwise check if this dependency is provided by this package
486 else if (pakfire_package_matches_dep(p
->pkg
, PAKFIRE_PKG_PROVIDES
, dep
))
490 // Check if this dependency should be filtered
492 match
= pcre2_match_data_create_from_pattern(p
->filter
, NULL
);
494 CTX_ERROR(ctx
, "Could not allocate PCRE match data: %m\n");
499 r
= pcre2_jit_match(p
->filter
, (PCRE2_SPTR
)dep
, length
- 1,
503 if (r
== PCRE2_ERROR_NOMATCH
) {
510 // Fetch the error message
511 r
= pcre2_get_error_message(r
, (PCRE2_UCHAR
*)error
, sizeof(error
));
513 BUILD_ERROR(p
->build
, "Could not fetch PCRE error message: %m\n");
518 BUILD_ERROR(p
->build
, "Could not match the filter: %s\n", error
);
524 BUILD_DEBUG(p
->build
, "Skipping dependency that has been filtered: %s\n", dep
);
531 r
= pakfire_package_add_dep(p
->pkg
, p
->dep
, buffer
);
533 BUILD_ERROR(p
->build
, "Could not process dependency '%s': %m\n", buffer
);
540 BUILD_DEBUG(p
->build
, "Skipping dependency that is provided by the package itself: %s\n", dep
);
544 pcre2_match_data_free(match
);
550 This function is a special way to run a script.
552 It will pipe the filelist into the standard input of the called script
553 and will read any dependencies from the standard output.
555 static int pakfire_build_find_deps(struct pakfire_build
* build
,
556 struct pakfire_package
* pkg
, int dep
, const char* script
,
557 struct pakfire_filelist
* filelist
, int class, const pcre2_code
* filter
) {
558 // Construct the context
559 struct pakfire_find_deps_ctx ctx
= {
567 .filelist
= filelist
,
572 // Skip calling the script if class doesn't match
573 if (class && !pakfire_filelist_matches_class(filelist
, class)) {
574 DEBUG(build
->pakfire
, "Skipping calling %s as class does not match\n", script
);
578 // Pass the buildroot as first argument
579 const char* args
[] = {
580 pakfire_relpath(build
->pakfire
, build
->buildroot
),
585 pakfire_jail_set_stdin_callback(build
->jail
, pakfire_build_send_filelist
, &ctx
);
586 pakfire_jail_set_stdout_callback(build
->jail
, pakfire_build_process_deps
, &ctx
);
589 r
= pakfire_build_run_script(build
, script
, args
);
591 BUILD_ERROR(build
, "%s returned with error %d\n", script
, r
);
596 static int pakfire_build_find_dependencies(struct pakfire_build
* build
,
597 struct pakfire_parser
* makefile
, const char* namespace,
598 struct pakfire_package
* pkg
, struct pakfire_filelist
* filelist
) {
599 pcre2_code
* filter_provides
= NULL
;
600 pcre2_code
* filter_requires
= NULL
;
603 // Fetch the provides filter
604 r
= pakfire_build_make_filter(build
, &filter_provides
,
605 makefile
, namespace, "filter_provides");
607 ERROR(build
->pakfire
, "Provides filter is broken: %m\n");
611 // Fetch the requires filter
612 r
= pakfire_build_make_filter(build
, &filter_requires
,
613 makefile
, namespace, "filter_requires");
615 ERROR(build
->pakfire
, "Requires filter is broken: %m\n");
620 r
= pakfire_build_find_deps(build
, pkg
,
621 PAKFIRE_PKG_PROVIDES
, "find-provides", filelist
, 0, filter_provides
);
625 // Find all Perl provides
626 r
= pakfire_build_find_deps(build
, pkg
,
627 PAKFIRE_PKG_PROVIDES
, "perl.prov", filelist
, PAKFIRE_FILE_PERL
, filter_provides
);
632 r
= pakfire_build_find_deps(build
, pkg
,
633 PAKFIRE_PKG_REQUIRES
, "find-requires", filelist
, 0, filter_requires
);
637 // Find all Perl requires
638 r
= pakfire_build_find_deps(build
, pkg
,
639 PAKFIRE_PKG_REQUIRES
, "perl.req", filelist
, PAKFIRE_FILE_PERL
, filter_requires
);
645 pcre2_code_free(filter_provides
);
647 pcre2_code_free(filter_requires
);
652 static int __pakfire_build_package_mark_config_files(
653 struct pakfire
* pakfire
, struct pakfire_file
* file
, void* data
) {
654 char** configfiles
= (char**)data
;
657 // Skip anything that isn't a regular file
658 if (pakfire_file_get_type(file
) == S_IFREG
)
662 const char* path
= pakfire_file_get_path(file
);
664 // Check if any configfile entry matches against this
665 for (char** configfile
= configfiles
; *configfile
; configfile
++) {
666 if (pakfire_string_startswith(path
, *configfile
)) {
667 r
= pakfire_file_set_flags(file
, PAKFIRE_FILE_CONFIG
);
679 static int pakfire_build_package_mark_config_files(struct pakfire_build
* build
,
680 struct pakfire_filelist
* filelist
, struct pakfire_parser
* makefile
, const char* namespace) {
681 char** configfiles
= NULL
;
684 // Fetch configfiles from makefile
685 r
= pakfire_parser_get_filelist(makefile
, namespace, "configfiles", &configfiles
, NULL
);
687 // If configfiles is not set, there is nothing to do
691 r
= pakfire_filelist_walk(filelist
, __pakfire_build_package_mark_config_files
,
696 for (char** configfile
= configfiles
; *configfile
; configfile
++)
704 static int pakfire_build_package_add_files(struct pakfire_build
* build
,
705 struct pakfire_parser
* makefile
, const char* buildroot
, const char* namespace,
706 struct pakfire_package
* pkg
, struct pakfire_packager
* packager
) {
707 struct pakfire_filelist
* filelist
= NULL
;
708 char** includes
= NULL
;
709 char** excludes
= NULL
;
712 // Fetch filelist from makefile
713 r
= pakfire_parser_get_filelist(makefile
, namespace, "files", &includes
, &excludes
);
715 // No files to package?
716 if (!includes
&& !excludes
)
719 // Allocate a new filelist
720 r
= pakfire_filelist_create(&filelist
, build
->pakfire
);
725 r
= pakfire_filelist_scan(filelist
, build
->buildroot
,
726 (const char**)includes
, (const char**)excludes
, PAKFIRE_FILELIST_EXTENDED_MATCHING
);
730 BUILD_DEBUG(build
, "%zu file(s) found\n", pakfire_filelist_length(filelist
));
732 // Nothing to do if the filelist is empty
733 if (pakfire_filelist_is_empty(filelist
))
737 pakfire_filelist_dump(filelist
, PAKFIRE_FILE_DUMP_FULL
);
740 r
= pakfire_build_find_dependencies(build
, makefile
, namespace, pkg
, filelist
);
742 BUILD_ERROR(build
, "Finding dependencies failed: %m\n");
746 // Mark configuration files
747 r
= pakfire_build_package_mark_config_files(build
, filelist
, makefile
, namespace);
751 // Add all files to the package
752 r
= pakfire_packager_add_files(packager
, filelist
);
758 pakfire_filelist_unref(filelist
);
760 for (char** include
= includes
; *include
; include
++)
765 for (char** exclude
= excludes
; *exclude
; exclude
++)
773 static int pakfire_build_send_scriptlet(struct pakfire_ctx
* ctx
, struct pakfire_jail
* jail
,
774 void* data
, int fd
) {
775 const struct pakfire_find_deps_ctx
* __ctx
= (struct pakfire_find_deps_ctx
*)data
;
778 // Fetch the scriptlet
779 const char* p
= pakfire_scriptlet_get_data(__ctx
->scriptlet
, &length
);
781 CTX_ERROR(ctx
, "Could not fetch scriptlet: %m\n");
785 // Write it into the pipe
786 ssize_t bytes_written
= write(fd
, p
, length
);
787 if (bytes_written
< 0) {
788 CTX_ERROR(ctx
, "Could not send scriptlet: %m\n");
795 static int pakfire_build_add_scriptlet_requires(struct pakfire_build
* build
,
796 struct pakfire_package
* pkg
, struct pakfire_scriptlet
* scriptlet
) {
799 struct pakfire_find_deps_ctx ctx
= {
802 .dep
= PAKFIRE_PKG_PREREQUIRES
,
803 .scriptlet
= scriptlet
,
807 pakfire_jail_set_stdin_callback(build
->jail
, pakfire_build_send_scriptlet
, &ctx
);
808 pakfire_jail_set_stdout_callback(build
->jail
, pakfire_build_process_deps
, &ctx
);
810 // Find all pre-requires
811 r
= pakfire_build_run_script(build
, "find-prerequires", NULL
);
819 static int pakfire_build_package_add_scriptlet(struct pakfire_build
* build
,
820 struct pakfire_package
* pkg
, struct pakfire_packager
* packager
,
821 const char* type
, const char* data
) {
822 struct pakfire_scriptlet
* scriptlet
= NULL
;
826 // Wrap scriptlet into a shell script
827 r
= asprintf(&shell
, "#!/bin/sh\n\nset -e\n\n%s\n\nexit 0\n", data
);
831 // Create a scriptlet
832 r
= pakfire_scriptlet_create(&scriptlet
, build
->ctx
, type
, shell
, 0);
836 // Add it to the package
837 r
= pakfire_packager_add_scriptlet(packager
, scriptlet
);
839 BUILD_ERROR(build
, "Could not add scriptlet %s\n", type
);
843 // Add scriptlet requirements
844 r
= pakfire_build_add_scriptlet_requires(build
, pkg
, scriptlet
);
846 BUILD_ERROR(build
, "Could not add scriptlet requirements: %m\n");
855 pakfire_scriptlet_unref(scriptlet
);
862 static int pakfire_build_package_add_scriptlets(struct pakfire_build
* build
,
863 struct pakfire_parser
* makefile
, const char* namespace,
864 struct pakfire_package
* pkg
, struct pakfire_packager
* packager
) {
868 for (const char** type
= pakfire_scriptlet_types
; *type
; type
++) {
869 r
= pakfire_string_format(name
, "scriptlet:%s", *type
);
873 // Fetch the scriptlet
874 char* data
= pakfire_parser_get(makefile
, namespace, name
);
878 // Add it to the package
879 r
= pakfire_build_package_add_scriptlet(build
, pkg
, packager
, *type
, data
);
891 static int pakfire_build_package(struct pakfire_build
* build
, struct pakfire_parser
* makefile
,
892 const char* buildroot
, const char* namespace) {
893 struct pakfire_package
* pkg
= NULL
;
894 struct pakfire_packager
* packager
= NULL
;
898 // Expand the handle into the package name
899 char* name
= pakfire_parser_expand(makefile
, "packages", namespace);
901 ERROR(build
->pakfire
, "Could not get package name: %m\n");
905 BUILD_INFO(build
, "Building package '%s'...\n", name
);
906 BUILD_DEBUG(build
, " buildroot = %s\n", buildroot
);
908 // Fetch build architecture
909 const char* arch
= pakfire_get_arch(build
->pakfire
);
913 // Fetch package from makefile
914 r
= pakfire_parser_create_package(makefile
, &pkg
, NULL
, namespace, arch
);
916 BUILD_ERROR(build
, "Could not create package from makefile: %m\n");
921 const char* distribution
= pakfire_parser_get(makefile
, NULL
, "DISTRO_NAME");
923 r
= pakfire_package_set_string(pkg
, PAKFIRE_PKG_DISTRO
, distribution
);
929 pakfire_package_set_uuid(pkg
, PAKFIRE_PKG_BUILD_ID
, build
->id
);
931 // Set source package
932 const char* source_name
= pakfire_parser_get(makefile
, NULL
, "name");
934 r
= pakfire_package_set_string(pkg
, PAKFIRE_PKG_SOURCE_NAME
, source_name
);
940 const char* source_evr
= pakfire_parser_get(makefile
, NULL
, "evr");
942 r
= pakfire_package_set_string(pkg
, PAKFIRE_PKG_SOURCE_EVR
, source_evr
);
948 r
= pakfire_package_set_string(pkg
, PAKFIRE_PKG_SOURCE_ARCH
, "src");
953 r
= pakfire_packager_create(&packager
, build
->pakfire
, pkg
);
958 r
= pakfire_build_package_add_files(build
, makefile
, buildroot
, namespace,
964 r
= pakfire_build_package_add_scriptlets(build
, makefile
, namespace,
969 const char* path
= pakfire_repo_get_path(build
->repo
);
971 // Write the finished package
972 r
= pakfire_packager_finish_to_directory(packager
, path
, NULL
);
974 BUILD_ERROR(build
, "pakfire_packager_finish() failed: %m\n");
978 // Cleanup all packaged files
979 r
= pakfire_packager_cleanup(packager
);
988 pakfire_packager_unref(packager
);
990 pakfire_package_unref(pkg
);
997 static int pakfire_build_package_dump(struct pakfire_ctx
* ctx
,
998 struct pakfire_package
* pkg
, void* p
) {
999 struct pakfire_build
* build
= p
;
1001 char* dump
= pakfire_package_dump(pkg
, PAKFIRE_PKG_DUMP_LONG
);
1005 BUILD_INFO(build
, "%s\n", dump
);
1011 static int pakfire_build_packages(struct pakfire_build
* build
,
1012 struct pakfire_parser
* makefile
) {
1013 BUILD_INFO(build
, "Creating packages...");
1016 const char* buildroot
= pakfire_relpath(build
->pakfire
, build
->buildroot
);
1018 // Fetch a list all all packages
1019 char** packages
= pakfire_parser_list_namespaces(makefile
, "packages.package:*");
1021 BUILD_ERROR(build
, "Could not find any packages: %m\n");
1025 unsigned int num_packages
= 0;
1027 // Count how many packages we have
1028 for (char** package
= packages
; *package
; package
++)
1031 DEBUG(build
->pakfire
, "Found %u package(s)\n", num_packages
);
1033 // Build packages in reverse order
1034 for (int i
= num_packages
- 1; i
>= 0; i
--) {
1035 r
= pakfire_build_package(build
, makefile
, buildroot
, packages
[i
]);
1040 // Rescan the build repository to import all packages again
1041 r
= pakfire_repo_scan(build
->repo
, 0);
1045 // Create a new packagelist
1046 r
= pakfire_packagelist_create(&build
->packages
, build
->ctx
);
1050 // Fetch all packages
1051 r
= pakfire_repo_to_packagelist(build
->repo
, build
->packages
);
1056 r
= pakfire_packagelist_walk(build
->packages
, pakfire_build_package_dump
, build
);
1070 static int pakfire_build_stage(struct pakfire_build
* build
,
1071 struct pakfire_parser
* makefile
, const char* stage
) {
1072 char template[1024];
1074 // Prepare template for this stage
1075 int r
= pakfire_string_format(template, TEMPLATE
, stage
);
1079 // Fetch the environment
1080 char** envp
= pakfire_parser_make_environ(makefile
);
1082 // Create the build script
1083 char* script
= pakfire_parser_expand(makefile
, "build", template);
1085 BUILD_ERROR(build
, "Could not generate the build script for stage '%s': %m\n", stage
);
1089 BUILD_INFO(build
, "Running build stage '%s'\n", stage
);
1091 // Import environment
1092 // XXX is this a good idea?
1093 r
= pakfire_jail_import_env(build
->jail
, (const char**)envp
);
1095 BUILD_ERROR(build
, "Could not import environment: %m\n");
1099 #warning We are thworing away the output here. Do we want this?
1102 r
= pakfire_jail_exec_script(build
->jail
, script
, strlen(script
), NULL
);
1104 BUILD_ERROR(build
, "Build stage '%s' failed with status %d\n", stage
, r
);
1108 for (char** e
= envp
; *e
; e
++)
1119 PAKFIRE_BUILD_CLEANUP_FILES
= (1 << 0),
1120 PAKFIRE_BUILD_ERROR_IF_NOT_EMPTY
= (1 << 1),
1121 PAKFIRE_BUILD_SHOW_PROGRESS
= (1 << 2),
1125 This helper function takes a callback which is expected to add any files
1126 to the given filelist which will optionally be all removed after.
1128 static int pakfire_build_post_process_files(struct pakfire_build
* build
,
1129 struct pakfire_filelist
* filelist
, const char* description
,
1130 int (*callback
)(struct pakfire
* pakfire
, struct pakfire_file
* file
, void* data
),
1132 struct pakfire_filelist
* removees
= NULL
;
1135 // Create a filelist with objects that need to be removed
1136 r
= pakfire_filelist_create(&removees
, build
->pakfire
);
1140 // Find all files that need to be removed
1141 r
= pakfire_filelist_walk(filelist
, callback
, removees
,
1142 (flags
& PAKFIRE_BUILD_SHOW_PROGRESS
) ? PAKFIRE_FILELIST_SHOW_PROGRESS
: 0);
1146 if (!pakfire_filelist_is_empty(removees
)) {
1148 BUILD_INFO(build
, "%s\n", description
);
1150 // Show all files which will be removed
1151 pakfire_filelist_dump(removees
, PAKFIRE_FILE_DUMP_FULL
|PAKFIRE_FILE_DUMP_ISSUES
);
1153 // Remove all files on the removee list
1154 if (flags
& PAKFIRE_BUILD_CLEANUP_FILES
) {
1155 r
= pakfire_filelist_cleanup(removees
, PAKFIRE_FILE_CLEANUP_TIDY
);
1159 // Remove all files from the filelist
1160 r
= pakfire_filelist_remove_all(filelist
, removees
);
1165 // Report an error if any files have been found
1166 if (flags
& PAKFIRE_BUILD_ERROR_IF_NOT_EMPTY
)
1172 pakfire_filelist_unref(removees
);
1177 static int __pakfire_build_remove_static_libraries(
1178 struct pakfire
* pakfire
, struct pakfire_file
* file
, void* data
) {
1179 struct pakfire_filelist
* removees
= (struct pakfire_filelist
*)data
;
1180 char path
[PATH_MAX
];
1183 // Find all static libraries
1184 if (pakfire_file_matches_class(file
, PAKFIRE_FILE_STATIC_LIBRARY
)) {
1185 // Copy the filename
1186 r
= pakfire_string_set(path
, pakfire_file_get_abspath(file
));
1190 // Remove the extension
1191 r
= pakfire_path_replace_extension(path
, "so");
1195 // Only delete if there is a shared object with the same name
1196 if (pakfire_path_exists(path
))
1197 return pakfire_filelist_add(removees
, file
);
1203 static int pakfire_build_post_remove_static_libraries(
1204 struct pakfire_build
* build
, struct pakfire_filelist
* filelist
) {
1205 return pakfire_build_post_process_files(build
, filelist
,
1206 "Removing static libaries...",
1207 __pakfire_build_remove_static_libraries
,
1208 PAKFIRE_BUILD_CLEANUP_FILES
);
1211 static int __pakfire_build_remove_libtool_archives(
1212 struct pakfire
* pakfire
, struct pakfire_file
* file
, void* data
) {
1213 struct pakfire_filelist
* removees
= (struct pakfire_filelist
*)data
;
1215 // Find all libtool archive files
1216 if (pakfire_file_matches_class(file
, PAKFIRE_FILE_LIBTOOL_ARCHIVE
))
1217 return pakfire_filelist_add(removees
, file
);
1222 static int pakfire_build_post_remove_libtool_archives(
1223 struct pakfire_build
* build
, struct pakfire_filelist
* filelist
) {
1224 return pakfire_build_post_process_files(build
, filelist
,
1225 "Removing libtool archives...",
1226 __pakfire_build_remove_libtool_archives
,
1227 PAKFIRE_BUILD_CLEANUP_FILES
);
1230 static int __pakfire_build_check_broken_symlinks(
1231 struct pakfire
* pakfire
, struct pakfire_file
* file
, void* data
) {
1232 struct pakfire_filelist
* broken
= (struct pakfire_filelist
*)data
;
1235 // Ignore anything that isn't a symlink
1236 switch (pakfire_file_get_type(file
)) {
1238 if (!pakfire_file_symlink_target_exists(file
)) {
1239 r
= pakfire_filelist_add(broken
, file
);
1246 // Ignore anything that isn't a symlink
1254 static int pakfire_build_post_check_broken_symlinks(
1255 struct pakfire_build
* build
, struct pakfire_filelist
* filelist
) {
1256 return pakfire_build_post_process_files(build
, filelist
,
1257 "Broken symlinks have been found:",
1258 __pakfire_build_check_broken_symlinks
,
1259 PAKFIRE_BUILD_ERROR_IF_NOT_EMPTY
);
1266 static int __pakfire_build_post_check_files(
1267 struct pakfire
* pakfire
, struct pakfire_file
* file
, void* data
) {
1268 struct pakfire_filelist
* broken
= (struct pakfire_filelist
*)data
;
1272 // Check file for issues
1273 r
= pakfire_file_check(file
, &issues
);
1275 //BUILD_ERROR(build, "%s: File Check failed: %m\n", pakfire_file_get_path(file));
1279 // If any issues have been found, consider this file to be on the list
1281 r
= pakfire_filelist_add(broken
, file
);
1289 static int pakfire_build_post_check_files(
1290 struct pakfire_build
* build
, struct pakfire_filelist
* filelist
) {
1291 return pakfire_build_post_process_files(
1295 __pakfire_build_post_check_files
,
1296 PAKFIRE_BUILD_ERROR_IF_NOT_EMPTY
|PAKFIRE_BUILD_SHOW_PROGRESS
);
1299 static int pakfire_build_run_post_build_checks(struct pakfire_build
* build
) {
1300 struct pakfire_filelist
* filelist
= NULL
;
1303 // Create a filelist of all files in the build
1304 r
= pakfire_filelist_create(&filelist
, build
->pakfire
);
1306 BUILD_ERROR(build
, "Could not create filelist: %m\n");
1310 // Scan for all files in BUILDROOT
1311 r
= pakfire_filelist_scan(filelist
, build
->buildroot
, NULL
, NULL
, 0);
1315 // If the filelist is empty, we can are done
1316 if (pakfire_filelist_is_empty(filelist
)) {
1317 DEBUG(build
->pakfire
, "Empty BUILDROOT. Skipping post build checks...\n");
1322 // Remove any static libraries
1323 r
= pakfire_build_post_remove_static_libraries(build
, filelist
);
1327 // Remove any libtool archives
1328 r
= pakfire_build_post_remove_libtool_archives(build
, filelist
);
1332 // Check for any broken symlinks
1333 r
= pakfire_build_post_check_broken_symlinks(build
, filelist
);
1338 r
= pakfire_build_post_check_files(build
, filelist
);
1344 pakfire_filelist_unref(filelist
);
1349 static const char* post_build_scripts
[] = {
1350 "compress-man-pages",
1355 static int pakfire_build_run_post_build_scripts(struct pakfire_build
* build
) {
1357 const char* buildroot
= pakfire_relpath(build
->pakfire
, build
->buildroot
);
1359 // Set default arguments for build scripts
1360 const char* args
[] = {
1364 // Run them one by one
1365 for (const char** script
= post_build_scripts
; *script
; script
++) {
1366 int r
= pakfire_build_run_script(build
, *script
, args
);
1374 static void pakfire_build_free(struct pakfire_build
* build
) {
1375 if (build
->packages
)
1376 pakfire_packagelist_unref(build
->packages
);
1379 pakfire_repo_clean(build
->repo
, PAKFIRE_REPO_CLEAN_FLAGS_DESTROY
);
1380 pakfire_repo_unref(build
->repo
);
1384 pakfire_jail_unref(build
->jail
);
1386 if (build
->cgroup
) {
1387 // Destroy the cgroup
1388 pakfire_cgroup_destroy(build
->cgroup
);
1391 pakfire_cgroup_unref(build
->cgroup
);
1395 pakfire_unref(build
->pakfire
);
1397 pakfire_ctx_unref(build
->ctx
);
1401 static int pakfire_build_parse_id(struct pakfire_build
* build
, const char* id
) {
1404 // Try parsing the Build ID
1406 r
= uuid_parse(id
, build
->id
);
1408 ERROR(build
->pakfire
, "Could not parse build ID '%s'\n", id
);
1412 // Otherwise initialize the Build ID with something random
1414 uuid_generate_random(build
->id
);
1417 // Store the ID as string, too
1418 uuid_unparse_lower(build
->id
, build
->_id
);
1424 Sets up a new cgroup for this build
1426 static int pakfire_build_setup_cgroup(struct pakfire_build
* build
) {
1427 struct pakfire_config
* config
= NULL
;
1428 char path
[PATH_MAX
];
1432 r
= pakfire_string_format(path
, "pakfire/build-%s", build
->_id
);
1434 ERROR(build
->pakfire
, "Could not compose path for cgroup: %m\n");
1438 // Create a new cgroup
1439 r
= pakfire_cgroup_open(&build
->cgroup
, build
->ctx
, path
,
1440 PAKFIRE_CGROUP_ENABLE_ACCOUNTING
);
1442 ERROR(build
->pakfire
, "Could not create cgroup for build %s: %m\n", build
->_id
);
1447 config
= pakfire_get_config(build
->pakfire
);
1451 // Guarantee some minimum memory
1452 size_t memory_guaranteed
= pakfire_config_get_bytes(config
, "build",
1453 "memory_guaranteed", PAKFIRE_BUILD_MEMORY_GUARANTEED
);
1454 if (memory_guaranteed
) {
1455 r
= pakfire_cgroup_set_guaranteed_memory(build
->cgroup
, memory_guaranteed
);
1461 size_t memory_limit
= pakfire_config_get_bytes(config
, "build", "memory_limit", 0);
1463 r
= pakfire_cgroup_set_memory_limit(build
->cgroup
, memory_limit
);
1469 size_t pid_limit
= pakfire_config_get_int(config
, "build",
1470 "pid_limit", PAKFIRE_BUILD_PID_LIMIT
);
1472 r
= pakfire_cgroup_set_pid_limit(build
->cgroup
, pid_limit
);
1479 pakfire_config_unref(config
);
1485 Sets up a new jail for this build
1487 static int pakfire_build_setup_jail(struct pakfire_build
* build
) {
1490 // Create a new jail
1491 r
= pakfire_jail_create(&build
->jail
, build
->pakfire
);
1493 ERROR(build
->pakfire
, "Could not create jail for build %s: %m\n", build
->_id
);
1497 // Connect the jail to our cgroup
1498 r
= pakfire_jail_set_cgroup(build
->jail
, build
->cgroup
);
1500 ERROR(build
->pakfire
, "Could not set cgroup for jail: %m\n");
1504 // Build everything with a slightly lower priority
1505 r
= pakfire_jail_nice(build
->jail
, 5);
1507 ERROR(build
->pakfire
, "Could not set nice level: %m\n");
1516 Sets up the ccache for this build
1518 static int pakfire_build_mount_ccache(struct pakfire_build
* build
) {
1521 // Do nothing if the ccache is disabled
1522 if (pakfire_build_has_flag(build
, PAKFIRE_BUILD_DISABLE_CCACHE
))
1525 // Check that the path is set
1526 if (!*build
->ccache_path
)
1529 // Make sure the path exists
1530 r
= pakfire_mkdir(build
->ccache_path
, 0755);
1532 ERROR(build
->pakfire
, "Could not create %s: %m\n", build
->ccache_path
);
1536 // Bind-mount the directory
1537 return pakfire_jail_bind(build
->jail
, build
->ccache_path
, CCACHE_DIR
,
1538 MS_NOSUID
|MS_NOEXEC
|MS_NODEV
);
1541 static int pakfire_build_setup_ccache(struct pakfire_build
* build
) {
1544 // Check if we want a ccache
1545 if (pakfire_build_has_flag(build
, PAKFIRE_BUILD_DISABLE_CCACHE
)) {
1546 DEBUG(build
->pakfire
, "ccache usage has been disabled for this build\n");
1548 // Set CCACHE_DISABLE=1 so that if ccache is installed, it will disable itself
1549 r
= pakfire_jail_set_env(build
->jail
, "CCACHE_DISABLE", "1");
1551 ERROR(build
->pakfire
, "Could not disable ccache: %m\n");
1559 r
= pakfire_jail_set_env(build
->jail
, "CCACHE_DIR", CCACHE_DIR
);
1561 ERROR(build
->pakfire
, "Could not set ccache directory: %m\n");
1565 // Set CCACHE_TEMPDIR
1566 r
= pakfire_jail_set_env(build
->jail
, "CCACHE_TEMPDIR", "/tmp");
1568 ERROR(build
->pakfire
, "Could not set ccache tempdir: %m\n");
1572 // Set a default path
1573 r
= pakfire_cache_path(build
->pakfire
, build
->ccache_path
, "%s", "ccache");
1580 static int pakfire_build_setup_repo(struct pakfire_build
* build
) {
1581 char path
[PATH_MAX
] = PAKFIRE_TMP_DIR
"/pakfire-build-repo.XXXXXX";
1585 // Create a new repository
1586 r
= pakfire_repo_create(&build
->repo
, build
->pakfire
, PAKFIRE_REPO_RESULT
);
1588 ERROR(build
->pakfire
, "Could not create repository %s: %m", PAKFIRE_REPO_RESULT
);
1593 pakfire_repo_set_description(build
->repo
, _("Build Repository"));
1595 // Create a temporary directory
1596 const char* p
= pakfire_mkdtemp(path
);
1598 ERROR(build
->pakfire
, "Could not create a the build repository: %m\n");
1603 r
= pakfire_string_format(url
, "file://%s", path
);
1608 pakfire_repo_set_baseurl(build
->repo
, url
);
1613 static int pakfire_build_set_time_start(struct pakfire_build
* build
) {
1616 // Fetch current time
1617 r
= clock_gettime(CLOCK_MONOTONIC
, &build
->time_start
);
1619 ERROR(build
->pakfire
, "Could not fetch start time: %m\n");
1624 PAKFIRE_EXPORT
int pakfire_build_create(struct pakfire_build
** build
,
1625 struct pakfire
* pakfire
, const char* id
, int flags
) {
1628 // Allocate build object
1629 struct pakfire_build
* b
= calloc(1, sizeof(*b
));
1633 // Reference the context
1634 b
->ctx
= pakfire_ctx(pakfire
);
1636 // Reference pakfire
1637 b
->pakfire
= pakfire_ref(pakfire
);
1639 // Initialize reference counter
1646 r
= pakfire_build_set_time_start(b
);
1651 r
= pakfire_build_parse_id(b
, id
);
1656 r
= pakfire_build_setup_repo(b
);
1661 r
= pakfire_build_setup_cgroup(b
);
1666 r
= pakfire_build_setup_jail(b
);
1671 r
= pakfire_build_setup_ccache(b
);
1679 pakfire_build_free(b
);
1683 PAKFIRE_EXPORT
struct pakfire_build
* pakfire_build_ref(struct pakfire_build
* build
) {
1689 PAKFIRE_EXPORT
struct pakfire_build
* pakfire_build_unref(struct pakfire_build
* build
) {
1690 if (--build
->nrefs
> 0)
1693 pakfire_build_free(build
);
1697 PAKFIRE_EXPORT
void pakfire_build_set_log_callback(struct pakfire_build
* build
,
1698 pakfire_build_log_callback callback
, void* data
) {
1699 build
->callbacks
.log
= callback
;
1700 build
->callbacks
.log_data
= data
;
1703 PAKFIRE_EXPORT
int pakfire_build_set_ccache_path(
1704 struct pakfire_build
* build
, const char* path
) {
1705 // Check if this can be called
1706 if (pakfire_build_has_flag(build
, PAKFIRE_BUILD_DISABLE_CCACHE
))
1709 // Check input value
1710 if (!path
|| !*path
)
1714 return pakfire_string_set(build
->ccache_path
, path
);
1717 PAKFIRE_EXPORT
int pakfire_build_set_target(
1718 struct pakfire_build
* build
, const char* target
) {
1719 return pakfire_string_set(build
->target
, target
);
1722 static int pakfire_build_install_packages(
1723 struct pakfire_build
* build
, int* snapshot_needs_update
) {
1724 struct pakfire_transaction
* transaction
= NULL
;
1725 char* problems
= NULL
;
1728 // Create a new transaction
1729 r
= pakfire_transaction_create(&transaction
, build
->pakfire
, 0);
1733 // Install all build dependencies
1734 for (const char** p
= PAKFIRE_BUILD_PACKAGES
; *p
; p
++) {
1735 r
= pakfire_transaction_request(transaction
,
1736 PAKFIRE_JOB_INSTALL
, *p
, PAKFIRE_JOB_ESSENTIAL
);
1741 // Also update everything that has already been installed
1742 r
= pakfire_transaction_request(transaction
, PAKFIRE_JOB_SYNC
, NULL
, 0);
1746 // Solve the transaction
1747 r
= pakfire_transaction_solve(transaction
, 0, &problems
);
1749 BUILD_ERROR(build
, "Could not install build dependencies:\n%s\n", problems
);
1753 // If there are changes, we have to update the snapshot
1754 if (pakfire_transaction_count(transaction
))
1755 *snapshot_needs_update
= 1;
1757 // Run the transaction
1758 r
= pakfire_transaction_run(transaction
);
1764 pakfire_transaction_unref(transaction
);
1772 Initializes the build environment
1774 static int pakfire_build_init(struct pakfire_build
* build
) {
1777 // Don't do it again
1779 DEBUG(build
->pakfire
, "Build environment has already been initialized\n");
1783 const int use_snapshot
= !pakfire_build_has_flag(build
, PAKFIRE_BUILD_DISABLE_SNAPSHOT
);
1785 // Tells us whether we need to (re-)create the snapshot
1786 int snapshot_needs_update
= 0;
1790 r
= pakfire_snapshot_restore(build
->pakfire
);
1795 // Install or update any build dependencies
1796 r
= pakfire_build_install_packages(build
, &snapshot_needs_update
);
1800 // Update the snapshot if there were changes
1801 if (use_snapshot
&& snapshot_needs_update
) {
1802 // Store the snapshot
1803 r
= pakfire_snapshot_store(build
->pakfire
);
1808 // Mark as initialized
1814 static int pakfire_build_read_makefile(struct pakfire_build
* build
,
1815 struct pakfire_parser
** parser
, struct pakfire_package
* package
) {
1816 char path
[PATH_MAX
];
1819 struct pakfire_parser_error
* error
= NULL
;
1821 const char* nevra
= pakfire_package_get_string(package
, PAKFIRE_PKG_NEVRA
);
1822 const char* name
= pakfire_package_get_string(package
, PAKFIRE_PKG_NAME
);
1824 // Compose path to makefile
1825 r
= pakfire_path(build
->pakfire
, path
, "/usr/src/packages/%s/%s.nm", nevra
, name
);
1830 r
= pakfire_read_makefile(parser
, build
->pakfire
, path
, &error
);
1833 BUILD_ERROR(build
, "Could not parse makefile %s: %s\n", path
,
1834 pakfire_parser_error_get_message(error
));
1836 BUILD_ERROR(build
, "Could not parse makefile %s: %m\n", path
);
1843 const char* buildroot
= pakfire_relpath(build
->pakfire
, build
->buildroot
);
1845 pakfire_parser_set(*parser
, NULL
, "BUILDROOT", buildroot
, 0);
1849 pakfire_parser_error_unref(error
);
1854 static int pakfire_build_perform(struct pakfire_build
* build
,
1855 struct pakfire_parser
* makefile
) {
1858 // Prepare the build
1859 r
= pakfire_build_stage(build
, makefile
, "prepare");
1863 // Perform the build
1864 r
= pakfire_build_stage(build
, makefile
, "build");
1869 if (!pakfire_build_has_flag(build
, PAKFIRE_BUILD_DISABLE_TESTS
)) {
1870 r
= pakfire_build_stage(build
, makefile
, "test");
1875 // Install everything
1876 r
= pakfire_build_stage(build
, makefile
, "install");
1880 // Run post build checks
1881 r
= pakfire_build_run_post_build_checks(build
);
1883 BUILD_ERROR(build
, "Post build checks failed\n");
1887 // Run post build scripts
1888 r
= pakfire_build_run_post_build_scripts(build
);
1896 // Drop to a shell for debugging
1897 if (pakfire_build_has_flag(build
, PAKFIRE_BUILD_INTERACTIVE
))
1898 pakfire_build_shell(build
);
1903 static int __pakfire_build_unpackaged_file(struct pakfire
* pakfire
,
1904 struct pakfire_file
* file
, void* p
) {
1905 struct pakfire_build
* build
= p
;
1907 char* s
= pakfire_file_dump(file
, PAKFIRE_FILE_DUMP_FULL
);
1909 BUILD_ERROR(build
, "%s\n", s
);
1916 static int pakfire_build_check_unpackaged_files(struct pakfire_build
* build
) {
1917 struct pakfire_filelist
* filelist
= NULL
;
1920 // Create a new filelist
1921 r
= pakfire_filelist_create(&filelist
, build
->pakfire
);
1925 // Scan for all files in BUILDROOT
1926 r
= pakfire_filelist_scan(filelist
, build
->buildroot
, NULL
, NULL
, 0);
1930 if (!pakfire_filelist_is_empty(filelist
)) {
1931 BUILD_ERROR(build
, "Unpackaged files found:\n");
1933 r
= pakfire_filelist_walk(filelist
, __pakfire_build_unpackaged_file
, build
, 0);
1943 pakfire_filelist_unref(filelist
);
1948 static int pakfire_build_install_package(struct pakfire_ctx
* ctx
,
1949 struct pakfire_package
* pkg
, void* p
) {
1950 struct pakfire_transaction
* transaction
= (struct pakfire_transaction
*)p
;
1952 return pakfire_transaction_request_package(transaction
,
1953 PAKFIRE_JOB_INSTALL
, pkg
, PAKFIRE_JOB_ESSENTIAL
);
1956 static int pakfire_build_install_test(struct pakfire_build
* build
) {
1957 struct pakfire_transaction
* transaction
= NULL
;
1958 char* problems
= NULL
;
1961 // Create a new transaction
1962 r
= pakfire_transaction_create(&transaction
, build
->pakfire
, 0);
1967 r
= pakfire_packagelist_walk(build
->packages
, pakfire_build_install_package
, transaction
);
1969 // Solve the request
1970 r
= pakfire_transaction_solve(transaction
, PAKFIRE_SOLVE_SHOW_SOLUTIONS
, &problems
);
1978 BUILD_ERROR(build
, "Install test failed:\n%s\n", problems
);
1983 BUILD_ERROR(build
, "Install test failed: %m\n");
1994 static int pakfire_build_post_check(struct pakfire_build
* build
) {
1997 // Check for unpackaged files
1998 r
= pakfire_build_check_unpackaged_files(build
);
2002 // Perform install test
2003 r
= pakfire_build_install_test(build
);
2010 static int pakfire_build_copy_package(struct pakfire_ctx
* ctx
,
2011 struct pakfire_package
* pkg
, void* p
) {
2012 struct pakfire_archive
* archive
= NULL
;
2013 char path
[PATH_MAX
];
2016 const char* target
= (const char*)p
;
2021 // Fetch the package filename
2022 const char* filename
= pakfire_package_get_string(pkg
, PAKFIRE_PKG_FILENAME
);
2028 // Format the destination path
2029 r
= pakfire_string_format(path
, "%s/%s", target
, filename
);
2034 archive
= pakfire_package_get_archive(pkg
);
2040 // Copy it to its destination
2041 r
= pakfire_archive_copy(archive
, path
);
2047 pakfire_archive_unref(archive
);
2052 static int pakfire_build_copy_packages(struct pakfire_build
* build
) {
2053 struct pakfire_repo
* local
= NULL
;
2056 DEBUG(build
->pakfire
, "Copying built packages\n");
2058 // Fetch local repository
2059 local
= pakfire_get_repo(build
->pakfire
, PAKFIRE_REPO_LOCAL
);
2061 // Copy all packages to the target path
2062 if (*build
->target
) {
2063 r
= pakfire_packagelist_walk(build
->packages
,
2064 pakfire_build_copy_package
, build
->target
);
2069 // If we have a local repository, we copy all packages to it
2071 const char* path
= pakfire_repo_get_path(local
);
2073 r
= pakfire_packagelist_walk(build
->packages
,
2074 pakfire_build_copy_package
, (void*)path
);
2082 pakfire_repo_unref(local
);
2087 static int pakfire_build_install_source_package(
2088 struct pakfire_build
* build
, struct pakfire_package
* package
) {
2089 struct pakfire_transaction
* transaction
= NULL
;
2090 char* problems
= NULL
;
2093 // Create a new transaction
2094 r
= pakfire_transaction_create(&transaction
, build
->pakfire
, 0);
2098 // Request to install all essential packages
2099 for (const char** p
= PAKFIRE_BUILD_PACKAGES
; *p
; p
++) {
2100 r
= pakfire_transaction_request(transaction
,
2101 PAKFIRE_JOB_INSTALL
, *p
, PAKFIRE_JOB_ESSENTIAL
);
2106 // Add the source package
2107 r
= pakfire_transaction_request_package(transaction
,
2108 PAKFIRE_JOB_INSTALL
, package
, PAKFIRE_JOB_ESSENTIAL
);
2112 // Solve the transaction
2113 r
= pakfire_transaction_solve(transaction
, 0, &problems
);
2116 BUILD_ERROR(build
, "Could not install the source package:\n%s\n", problems
);
2121 // Set how many packages have been changed
2122 const size_t changes
= pakfire_transaction_count(transaction
);
2124 // Sanity check to see if we actually try to install anything
2126 BUILD_ERROR(build
, "The source package did not get installed\n");
2131 // Run the transaction
2132 r
= pakfire_transaction_run(transaction
);
2138 pakfire_transaction_unref(transaction
);
2145 PAKFIRE_EXPORT
int pakfire_build_exec(struct pakfire_build
* build
, const char* path
) {
2146 struct pakfire_package
* package
= NULL
;
2147 struct pakfire_parser
* makefile
= NULL
;
2148 char* buildroot
= NULL
;
2149 char* problems
= NULL
;
2150 char duration
[TIME_STRING_MAX
];
2153 // Fetch architecture
2154 const char* arch
= pakfire_get_arch(build
->pakfire
);
2157 r
= pakfire_path(build
->pakfire
, build
->buildroot
, "%s",
2158 PAKFIRE_TMP_DIR
"/pakfire-buildroot.XXXXXX");
2162 // Open the source package
2163 r
= pakfire_commandline_add(build
->pakfire
, path
, &package
);
2167 const char* nevra
= pakfire_package_get_string(package
, PAKFIRE_PKG_NEVRA
);
2168 const char* uuid
= pakfire_package_get_string(package
, PAKFIRE_PKG_UUID
);
2170 BUILD_INFO(build
, "Building %s (%s)...\n", nevra
, uuid
);
2172 // Check if this package can be build in this environment
2173 if (!pakfire_package_supports_build_arch(package
, arch
)) {
2174 ERROR(build
->pakfire
, "%s does not support being built on %s\n", nevra
, arch
);
2179 // Perform an install check to see whether we can build this at all
2180 r
= pakfire_package_installcheck(package
, &problems
, 0);
2182 BUILD_ERROR(build
, "Cannot build %s:\n%s\n", nevra
, problems
);
2186 // Initialize the build environment
2187 r
= pakfire_build_init(build
);
2191 // Install the source package
2192 r
= pakfire_build_install_source_package(build
, package
);
2194 BUILD_ERROR(build
, "Could not install the source package: %m\n");
2199 r
= pakfire_build_mount_ccache(build
);
2201 BUILD_ERROR(build
, "Could not mount the ccache: %m\n");
2206 buildroot
= pakfire_mkdtemp(build
->buildroot
);
2208 BUILD_ERROR(build
, "Could not create BUILDROOT: %m\n");
2212 // Open the makefile
2213 r
= pakfire_build_read_makefile(build
, &makefile
, package
);
2217 // Perform the actual build
2218 r
= pakfire_build_perform(build
, makefile
);
2222 // Create the packages
2223 r
= pakfire_build_packages(build
, makefile
);
2225 BUILD_ERROR(build
, "Could not create packages: %m\n");
2229 // Perform post build checks
2230 r
= pakfire_build_post_check(build
);
2234 // Copy packages to their destination
2235 r
= pakfire_build_copy_packages(build
);
2240 r
= pakfire_format_time(duration
, pakfire_build_duration(build
));
2244 BUILD_INFO(build
, "Build successfully completed in %s\n", duration
);
2248 pakfire_parser_unref(makefile
);
2250 pakfire_package_unref(package
);
2254 // Cleanup buildroot
2256 pakfire_rmtree(buildroot
, 0);
2261 static int pakfire_build_mkimage_install_deps(struct pakfire_build
* build
,
2263 struct pakfire_transaction
* transaction
= NULL
;
2264 char requires
[NAME_MAX
];
2265 char* problems
= NULL
;
2269 r
= pakfire_string_format(requires
, "mkimage(%s)", type
);
2273 // Create a new transaction
2274 r
= pakfire_transaction_create(&transaction
, build
->pakfire
, 0);
2276 ERROR(build
->pakfire
, "Could not create transaction: %m\n");
2280 // Add requires to the request
2281 r
= pakfire_transaction_request(transaction
,
2282 PAKFIRE_JOB_INSTALL
, requires
, PAKFIRE_JOB_ESSENTIAL
);
2284 ERROR(build
->pakfire
, "Could not add '%s' to the transaction: %m\n", requires
);
2288 // Solve the request
2289 r
= pakfire_transaction_solve(transaction
, 0, &problems
);
2291 ERROR(build
->pakfire
, "Could not solve the request:\n%s\n", problems
);
2295 // Run the transaction
2296 r
= pakfire_transaction_run(transaction
);
2302 pakfire_transaction_unref(transaction
);
2309 static int pakfire_build_collect_packages(struct pakfire_build
* build
, const char* path
) {
2310 struct pakfire_transaction
* transaction
= NULL
;
2311 char* problems
= NULL
;
2315 // Create a new transaction
2316 r
= pakfire_transaction_create(&transaction
, build
->pakfire
, 0);
2318 ERROR(build
->pakfire
, "Could not create transaction: %m\n");
2323 // Install the base system
2324 r
= pakfire_transaction_request(transaction
,
2325 PAKFIRE_JOB_INSTALL
, "ipfire-release", PAKFIRE_JOB_ESSENTIAL
);
2327 ERROR(build
->pakfire
, "Could not install 'ipfire-release': %m\n");
2331 // Solve the transaction
2332 r
= pakfire_transaction_solve(transaction
, 0, &problems
);
2334 ERROR(build
->pakfire
, "Could not solve request:\n%s\n", problems
);
2338 // Empty transaction?
2339 if (!pakfire_transaction_count(transaction
)) {
2340 ERROR(build
->pakfire
, "The transaction is unexpectedly empty\n");
2345 // Dump the transaction
2346 p
= pakfire_transaction_dump(transaction
, 80);
2348 ERROR(build
->pakfire
, "Could not dump the transaction: %m\n");
2354 INFO(build
->pakfire
, "%s\n", p
);
2356 // Download all packages
2357 r
= pakfire_transaction_download(transaction
);
2359 ERROR(build
->pakfire
, "Could not download the transaction: %m\n");
2363 // Create a repository with all packages in this transaction
2364 r
= pakfire_transaction_compose_repo(transaction
, NULL
, path
);
2366 ERROR(build
->pakfire
, "Could not create repository: %m\n");
2370 // XXX Should we perform installcheck here?
2374 pakfire_transaction_unref(transaction
);
2383 PAKFIRE_EXPORT
int pakfire_build_mkimage(struct pakfire_build
* build
,
2384 const char* type
, FILE* f
) {
2386 char packagesdir
[PATH_MAX
];
2387 char path
[PATH_MAX
];
2395 // Create a path inside the build environment
2396 r
= pakfire_path(build
->pakfire
, path
, "%s",
2397 PAKFIRE_TMP_DIR
"/pakfire-image.XXXXXX");
2401 // Allocate a temporary file
2402 t
= pakfire_mktemp(path
, 0600);
2404 ERROR(build
->pakfire
, "Could not allocate a temporary file: %m\n");
2409 // Create a path for all packages
2410 r
= pakfire_path(build
->pakfire
, packagesdir
, "%s",
2411 PAKFIRE_TMP_DIR
"/pakfire-packages.XXXXXX");
2415 p
= pakfire_mkdtemp(packagesdir
);
2419 // Collect all packages
2420 r
= pakfire_build_collect_packages(build
, packagesdir
);
2424 // Initialize the build environment
2425 r
= pakfire_build_init(build
);
2429 // Install all dependencies
2430 r
= pakfire_build_mkimage_install_deps(build
, type
);
2434 const char* args
[] = {
2436 pakfire_relpath(build
->pakfire
, path
),
2437 pakfire_relpath(build
->pakfire
, packagesdir
),
2441 // Run the mkimage script
2442 r
= pakfire_build_run_script(build
, "mkimage", args
);
2446 // Copy data to its destination
2447 r
= pakfire_copy(build
->pakfire
, t
, f
);
2455 // Unlink the temporary file
2459 // Remove all packages
2460 pakfire_rmtree(packagesdir
, 0);
2465 int pakfire_build_clean(struct pakfire
* pakfire
, int flags
) {
2466 struct pakfire_repo
* local
= NULL
;
2469 // Fetch local repository
2470 local
= pakfire_get_repo(pakfire
, PAKFIRE_REPO_LOCAL
);
2472 ERROR(pakfire
, "Could not find repository %s: %m\n", PAKFIRE_REPO_LOCAL
);
2476 // Destroy everything in it
2477 r
= pakfire_repo_clean(local
, PAKFIRE_REPO_CLEAN_FLAGS_DESTROY
);
2483 pakfire_repo_unref(local
);
2488 static int pakfire_build_install(struct pakfire_build
* build
, const char** packages
) {
2489 struct pakfire_transaction
* transaction
= NULL
;
2490 char* problems
= NULL
;
2493 // Create a new transaction
2494 r
= pakfire_transaction_create(&transaction
, build
->pakfire
, 0);
2498 // Install all packages
2499 for (const char** p
= packages
; *p
; p
++) {
2500 r
= pakfire_transaction_request(transaction
, PAKFIRE_JOB_INSTALL
, *p
, 0);
2505 // Solve the transaction
2506 r
= pakfire_transaction_solve(transaction
, 0, &problems
);
2508 ERROR(build
->pakfire
, "Could not install packages:\n%s\n", problems
);
2512 // Run the transaction
2513 r
= pakfire_transaction_run(transaction
);
2519 pakfire_transaction_unref(transaction
);
2527 This is a convenience function that sets up a build environment and
2528 then drops the user into an interactive shell.
2530 PAKFIRE_EXPORT
int pakfire_shell(struct pakfire
* pakfire
, const char** packages
, int flags
) {
2531 struct pakfire_build
* build
= NULL
;
2534 // Shells are always interactive
2535 flags
|= PAKFIRE_BUILD_INTERACTIVE
;
2537 // Create a new build environment
2538 r
= pakfire_build_create(&build
, pakfire
, NULL
, flags
);
2540 ERROR(pakfire
, "Could not create build: %m\n");
2544 // Initialize the build environment
2545 r
= pakfire_build_init(build
);
2549 // Install any additional packages
2551 r
= pakfire_build_install(build
, packages
);
2557 r
= pakfire_build_shell(build
);
2561 pakfire_build_unref(build
);