1 /*#############################################################################
3 # Pakfire - The IPFire package management system #
4 # Copyright (C) 2021 Pakfire development team #
6 # This program is free software: you can redistribute it and/or modify #
7 # it under the terms of the GNU General Public License as published by #
8 # the Free Software Foundation, either version 3 of the License, or #
9 # (at your option) any later version. #
11 # This program is distributed in the hope that it will be useful, #
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of #
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
14 # GNU General Public License for more details. #
16 # You should have received a copy of the GNU General Public License #
17 # along with this program. If not, see <http://www.gnu.org/licenses/>. #
19 #############################################################################*/
23 #include <sys/mount.h>
26 #include <uuid/uuid.h>
28 #define PCRE2_CODE_UNIT_WIDTH 8
31 #include <pakfire/build.h>
32 #include <pakfire/cgroup.h>
33 #include <pakfire/dependencies.h>
34 #include <pakfire/dist.h>
35 #include <pakfire/file.h>
36 #include <pakfire/i18n.h>
37 #include <pakfire/jail.h>
38 #include <pakfire/logging.h>
39 #include <pakfire/mount.h>
40 #include <pakfire/package.h>
41 #include <pakfire/packager.h>
42 #include <pakfire/parser.h>
43 #include <pakfire/private.h>
44 #include <pakfire/problem.h>
45 #include <pakfire/repo.h>
46 #include <pakfire/request.h>
47 #include <pakfire/scriptlet.h>
48 #include <pakfire/snapshot.h>
49 #include <pakfire/solution.h>
50 #include <pakfire/string.h>
51 #include <pakfire/util.h>
53 #define CCACHE_DIR "/var/cache/ccache"
55 // We guarantee 2 GiB of memory to every build container
56 #define PAKFIRE_BUILD_MEMORY_GUARANTEED (size_t)2 * 1024 * 1024 * 1024
58 // We allow only up to 2048 processes/threads for every build container
59 #define PAKFIRE_BUILD_PID_LIMIT (size_t)2048
61 struct pakfire_build
{
62 struct pakfire
* pakfire
;
70 char _id
[UUID_STR_LEN
];
72 char target
[PATH_MAX
];
78 struct pakfire_cgroup
* cgroup
;
81 struct pakfire_jail
* jail
;
83 // The build repository
84 struct pakfire_repo
* repo
;
86 // A list of all built packages
87 struct pakfire_packagelist
* packages
;
90 char buildroot
[PATH_MAX
];
97 "#!/bin/bash --login\n" \
106 static int pakfire_build_has_flag(struct pakfire_build
* build
, int flag
) {
107 return build
->flags
& flag
;
110 static time_t pakfire_build_duration(struct pakfire_build
* build
) {
111 // What time is it now?
112 time_t now
= time(NULL
);
118 return now
- build
->time_start
;
121 static int __pakfire_build_setup_repo(struct pakfire
* pakfire
,
122 struct pakfire_repo
* repo
, void* p
) {
127 struct pakfire_build
* build
= (struct pakfire_build
*)p
;
129 // Skip processing the installed repository
130 if (pakfire_repo_is_installed_repo(repo
))
133 // Skip processing any other internal repositories
134 if (pakfire_repo_is_internal(repo
))
137 const char* name
= pakfire_repo_get_name(repo
);
139 DEBUG(pakfire
, "Exporting repository configuration for '%s'\n", name
);
141 // Make path for configuration file
142 r
= pakfire_path(build
->pakfire
, path
, PAKFIRE_CONFIG_DIR
"/repos/%s.repo", name
);
144 ERROR(pakfire
, "Could not make repository configuration path for %s: %m\n", name
);
148 // Create the parent directory
149 r
= pakfire_mkparentdir(path
, 0755);
153 // Open the repository configuration
154 f
= fopen(path
, "w");
156 ERROR(pakfire
, "Could not open %s for writing: %m\n", path
);
160 // Write repository configuration
161 r
= pakfire_repo_write_config(repo
, f
);
163 ERROR(pakfire
, "Could not write repository configuration for %s: %m\n", name
);
167 // Bind-mount any local repositories
168 if (pakfire_repo_is_local(repo
)) {
169 const char* _path
= pakfire_repo_get_path(repo
);
171 // Bind-mount the repository data read-only
172 r
= pakfire_jail_bind(build
->jail
, _path
, _path
, MS_RDONLY
);
174 ERROR(pakfire
, "Could not bind-mount the repository at %s: %m\n", _path
);
187 This function enables the local repository so that it can be accessed by
188 a pakfire instance running inside the chroot.
190 static int pakfire_build_enable_repos(struct pakfire_build
* build
) {
191 return pakfire_repo_walk(build
->pakfire
, __pakfire_build_setup_repo
, build
);
195 Drops the user into a shell
197 static int pakfire_build_shell(struct pakfire_build
* build
) {
200 // Export local repository if running in interactive mode
201 r
= pakfire_build_enable_repos(build
);
206 return pakfire_jail_shell(build
->jail
);
209 static int pakfire_build_read_script(struct pakfire_build
* build
,
210 const char* filename
, char** buffer
, size_t* length
) {
215 // Compose the source path
216 r
= pakfire_path_join(path
, PAKFIRE_SCRIPTS_DIR
, filename
);
218 ERROR(build
->pakfire
, "Could not compose path for script '%s': %m\n", filename
);
222 DEBUG(build
->pakfire
, "Reading script from %s...\n", path
);
225 f
= fopen(path
, "r");
227 ERROR(build
->pakfire
, "Could not open script %s: %m\n", path
);
231 // Read the file into a the buffer
232 r
= pakfire_read_file_into_buffer(f
, buffer
, length
);
234 ERROR(build
->pakfire
, "Could not read script: %m\n");
245 static int pakfire_build_run_script(
246 struct pakfire_build
* build
,
247 const char* filename
,
249 pakfire_jail_communicate_in communicate_in
,
250 pakfire_jail_communicate_out communicate_out
,
257 DEBUG(build
->pakfire
, "Running build script '%s'...\n", filename
);
260 r
= pakfire_build_read_script(build
, filename
, &script
, &length
);
262 ERROR(build
->pakfire
, "Could not read script %s: %m\n", filename
);
266 // Execute the script
267 r
= pakfire_jail_exec_script(build
->jail
, script
, length
, args
,
268 communicate_in
, communicate_out
, data
);
270 ERROR(build
->pakfire
, "Script '%s' failed with status %d\n", filename
, r
);
279 struct pakfire_find_deps_ctx
{
280 struct pakfire_package
* pkg
;
282 struct pakfire_scriptlet
* scriptlet
;
284 const pcre2_code
* filter
;
286 struct pakfire_filelist
* filelist
;
290 static int pakfire_build_make_filter(struct pakfire_build
* build
, pcre2_code
** regex
,
291 struct pakfire_parser
* makefile
, const char* namespace, const char* filter
) {
292 char* pattern
= NULL
;
296 pattern
= pakfire_parser_get(makefile
, namespace, filter
);
298 // Nothing if to if there is no or an empty pattern
299 if (!pattern
|| !*pattern
)
302 DEBUG(build
->pakfire
, "Found filter for %s: %s\n", filter
, pattern
);
304 // Compile the regular expression
305 r
= pakfire_compile_regex(build
->pakfire
, regex
, pattern
);
316 static int pakfire_build_send_filelist(struct pakfire
* pakfire
, void* data
, int fd
) {
317 struct pakfire_find_deps_ctx
* ctx
= (struct pakfire_find_deps_ctx
*)data
;
318 struct pakfire_file
* file
= NULL
;
321 const size_t length
= pakfire_filelist_length(ctx
->filelist
);
323 // Check if we have reached the end of the filelist
324 if (ctx
->i
>= length
)
327 // Fetch the next file
328 file
= pakfire_filelist_get(ctx
->filelist
, ctx
->i
);
330 DEBUG(pakfire
, "Could not fetch file %d: %m\n", ctx
->i
);
335 // Fetch the path of the file
336 const char* path
= pakfire_file_get_path(file
);
338 ERROR(pakfire
, "Received a file with an empty path\n");
343 // Skip files that don't match what we are looking for
344 if (ctx
->class && !pakfire_file_matches_class(file
, ctx
->class))
347 // Write path to stdin
348 r
= dprintf(fd
, "%s\n", path
);
353 // Move on to the next file
361 pakfire_file_unref(file
);
366 static int pakfire_build_process_deps(struct pakfire
* pakfire
,
367 void* data
, int priority
, const char* buffer
, const size_t length
) {
368 const struct pakfire_find_deps_ctx
* ctx
= (struct pakfire_find_deps_ctx
*)data
;
370 pcre2_match_data
* match
= NULL
;
373 // Nothing to do for an empty buffer
374 if (!buffer
|| !*buffer
)
378 // Add every dependency that we have received
380 // Copy the dependency to the stack (and remove the trailing newline)
381 r
= pakfire_string_format(dep
, "%.*s", (int)length
- 1, buffer
);
385 DEBUG(pakfire
, "Processing dependency: %s\n", dep
);
387 // Filter out any dependencies that are provided by this package
388 if (ctx
->dep
== PAKFIRE_PKG_REQUIRES
) {
389 // If this is a file, we check if it is on the filelist
390 if (pakfire_filelist_contains(ctx
->filelist
, dep
))
393 // Otherwise check if this dependency is provided by this package
394 else if (pakfire_package_matches_dep(ctx
->pkg
, PAKFIRE_PKG_PROVIDES
, dep
))
398 // Check if this dependency should be filtered
400 match
= pcre2_match_data_create_from_pattern(ctx
->filter
, NULL
);
402 ERROR(pakfire
, "Could not allocate PCRE match data: %m\n");
407 r
= pcre2_jit_match(ctx
->filter
, (PCRE2_SPTR
)dep
, length
- 1,
411 if (r
== PCRE2_ERROR_NOMATCH
) {
418 // Fetch the error message
419 r
= pcre2_get_error_message(r
, (PCRE2_UCHAR
*)error
, sizeof(error
));
421 ERROR(pakfire
, "Could not fetch PCRE error message: %m\n");
426 ERROR(pakfire
, "Could not match the filter: %s\n", error
);
432 DEBUG(pakfire
, "Skipping dependency that has been filtered: %s\n", dep
);
439 r
= pakfire_package_add_dep(ctx
->pkg
, ctx
->dep
, buffer
);
441 ERROR(pakfire
, "Could not process dependency '%s': %m\n", buffer
);
446 // Send everything else to the default logger
448 ERROR(pakfire
, "%s\n", buffer
);
455 DEBUG(pakfire
, "Skipping dependency that is provided by the package itself: %s\n", dep
);
459 pcre2_match_data_free(match
);
465 This function is a special way to run a script.
467 It will pipe the filelist into the standard input of the called script
468 and will read any dependencies from the standard output.
470 static int pakfire_build_find_deps(struct pakfire_build
* build
,
471 struct pakfire_package
* pkg
, int dep
, const char* script
,
472 struct pakfire_filelist
* filelist
, int class, const pcre2_code
* filter
) {
473 // Construct the context
474 struct pakfire_find_deps_ctx ctx
= {
481 .filelist
= filelist
,
486 // Skip calling the script if class doesn't match
487 if (class && !pakfire_filelist_matches_class(filelist
, class)) {
488 DEBUG(build
->pakfire
, "Skipping calling %s as class does not match\n", script
);
492 // Pass the buildroot as first argument
493 const char* args
[] = {
494 pakfire_relpath(build
->pakfire
, build
->buildroot
),
499 r
= pakfire_build_run_script(build
, script
, args
,
500 pakfire_build_send_filelist
, pakfire_build_process_deps
, &ctx
);
502 ERROR(build
->pakfire
, "%s returned with error %d\n", script
, r
);
507 static int pakfire_build_find_dependencies(struct pakfire_build
* build
,
508 struct pakfire_parser
* makefile
, const char* namespace,
509 struct pakfire_package
* pkg
, struct pakfire_filelist
* filelist
) {
510 pcre2_code
* filter_provides
= NULL
;
511 pcre2_code
* filter_requires
= NULL
;
514 // Fetch the provides filter
515 r
= pakfire_build_make_filter(build
, &filter_provides
,
516 makefile
, namespace, "filter_provides");
518 ERROR(build
->pakfire
, "Provides filter is broken: %m\n");
522 // Fetch the requires filter
523 r
= pakfire_build_make_filter(build
, &filter_requires
,
524 makefile
, namespace, "filter_requires");
526 ERROR(build
->pakfire
, "Requires filter is broken: %m\n");
531 r
= pakfire_build_find_deps(build
, pkg
,
532 PAKFIRE_PKG_PROVIDES
, "find-provides", filelist
, 0, filter_provides
);
536 // Find all Perl provides
537 r
= pakfire_build_find_deps(build
, pkg
,
538 PAKFIRE_PKG_PROVIDES
, "perl.prov", filelist
, PAKFIRE_FILE_PERL
, filter_provides
);
543 r
= pakfire_build_find_deps(build
, pkg
,
544 PAKFIRE_PKG_REQUIRES
, "find-requires", filelist
, 0, filter_requires
);
548 // Find all Perl requires
549 r
= pakfire_build_find_deps(build
, pkg
,
550 PAKFIRE_PKG_REQUIRES
, "perl.req", filelist
, PAKFIRE_FILE_PERL
, filter_requires
);
556 pcre2_code_free(filter_provides
);
558 pcre2_code_free(filter_requires
);
563 static int __pakfire_build_package_mark_config_files(
564 struct pakfire
* pakfire
, struct pakfire_file
* file
, void* data
) {
565 char** configfiles
= (char**)data
;
568 // Skip anything that isn't a regular file
569 if (pakfire_file_get_type(file
) == S_IFREG
)
573 const char* path
= pakfire_file_get_path(file
);
575 // Check if any configfile entry matches against this
576 for (char** configfile
= configfiles
; *configfile
; configfile
++) {
577 if (pakfire_string_startswith(path
, *configfile
)) {
578 r
= pakfire_file_set_flags(file
, PAKFIRE_FILE_CONFIG
);
590 static int pakfire_build_package_mark_config_files(struct pakfire_build
* build
,
591 struct pakfire_filelist
* filelist
, struct pakfire_parser
* makefile
, const char* namespace) {
592 char** configfiles
= NULL
;
595 // Fetch configfiles from makefile
596 r
= pakfire_parser_get_filelist(makefile
, namespace, "configfiles", &configfiles
, NULL
);
598 // If configfiles is not set, there is nothing to do
602 r
= pakfire_filelist_walk(filelist
, __pakfire_build_package_mark_config_files
, configfiles
);
606 for (char** configfile
= configfiles
; *configfile
; configfile
++)
614 static int pakfire_build_package_add_files(struct pakfire_build
* build
,
615 struct pakfire_parser
* makefile
, const char* buildroot
, const char* namespace,
616 struct pakfire_package
* pkg
, struct pakfire_packager
* packager
) {
617 struct pakfire_filelist
* filelist
= NULL
;
618 char** includes
= NULL
;
619 char** excludes
= NULL
;
622 // Fetch filelist from makefile
623 r
= pakfire_parser_get_filelist(makefile
, namespace, "files", &includes
, &excludes
);
625 // No files to package?
626 if (!includes
&& !excludes
)
629 // Allocate a new filelist
630 r
= pakfire_filelist_create(&filelist
, build
->pakfire
);
635 r
= pakfire_filelist_scan(filelist
, build
->buildroot
,
636 (const char**)includes
, (const char**)excludes
);
640 DEBUG(build
->pakfire
, "%zu file(s) found\n", pakfire_filelist_length(filelist
));
642 // Nothing to do if the filelist is empty
643 if (pakfire_filelist_is_empty(filelist
))
647 pakfire_filelist_dump(filelist
, PAKFIRE_FILE_DUMP_FULL
);
650 r
= pakfire_build_find_dependencies(build
, makefile
, namespace, pkg
, filelist
);
652 ERROR(build
->pakfire
, "Finding dependencies failed: %m\n");
656 // Mark configuration files
657 r
= pakfire_build_package_mark_config_files(build
, filelist
, makefile
, namespace);
661 // Add all files to the package
662 r
= pakfire_packager_add_files(packager
, filelist
);
668 pakfire_filelist_unref(filelist
);
670 for (char** include
= includes
; *include
; include
++)
675 for (char** exclude
= excludes
; *exclude
; exclude
++)
683 static int pakfire_build_send_scriptlet(struct pakfire
* pakfire
, void* data
, int fd
) {
684 const struct pakfire_find_deps_ctx
* ctx
= (struct pakfire_find_deps_ctx
*)data
;
687 // Fetch the scriptlet
688 const char* p
= pakfire_scriptlet_get_data(ctx
->scriptlet
, &length
);
690 ERROR(pakfire
, "Could not fetch scriptlet: %m\n");
694 // Write it into the pipe
695 ssize_t bytes_written
= write(fd
, p
, length
);
696 if (bytes_written
< 0) {
697 ERROR(pakfire
, "Could not send scriptlet: %m\n");
704 static int pakfire_build_add_scriptlet_requires(struct pakfire_build
* build
,
705 struct pakfire_package
* pkg
, struct pakfire_scriptlet
* scriptlet
) {
708 struct pakfire_find_deps_ctx ctx
= {
710 .dep
= PAKFIRE_PKG_PREREQUIRES
,
711 .scriptlet
= scriptlet
,
714 // Find all pre-requires
715 r
= pakfire_build_run_script(build
, "find-prerequires", NULL
,
716 pakfire_build_send_scriptlet
, pakfire_build_process_deps
, &ctx
);
724 static int pakfire_build_package_add_scriptlet(struct pakfire_build
* build
,
725 struct pakfire_package
* pkg
, struct pakfire_packager
* packager
,
726 const char* type
, const char* data
) {
727 struct pakfire_scriptlet
* scriptlet
= NULL
;
731 // Wrap scriptlet into a shell script
732 r
= asprintf(&shell
, "#!/bin/sh\n\nset -e\n\n%s\n\nexit 0\n", data
);
736 // Create a scriptlet
737 r
= pakfire_scriptlet_create(&scriptlet
, build
->pakfire
, type
, shell
, 0);
741 // Add it to the package
742 r
= pakfire_packager_add_scriptlet(packager
, scriptlet
);
744 ERROR(build
->pakfire
, "Could not add scriptlet %s\n", type
);
748 // Add scriptlet requirements
749 r
= pakfire_build_add_scriptlet_requires(build
, pkg
, scriptlet
);
751 ERROR(build
->pakfire
, "Could not add scriptlet requirements: %m\n");
760 pakfire_scriptlet_unref(scriptlet
);
767 static int pakfire_build_package_add_scriptlets(struct pakfire_build
* build
,
768 struct pakfire_parser
* makefile
, const char* namespace,
769 struct pakfire_package
* pkg
, struct pakfire_packager
* packager
) {
773 for (const char** type
= pakfire_scriptlet_types
; *type
; type
++) {
774 r
= pakfire_string_format(name
, "scriptlet:%s", *type
);
778 // Fetch the scriptlet
779 char* data
= pakfire_parser_get(makefile
, namespace, name
);
783 // Add it to the package
784 r
= pakfire_build_package_add_scriptlet(build
, pkg
, packager
, *type
, data
);
796 static int pakfire_build_package(struct pakfire_build
* build
, struct pakfire_parser
* makefile
,
797 const char* buildroot
, const char* namespace) {
798 struct pakfire_package
* pkg
= NULL
;
799 struct pakfire_packager
* packager
= NULL
;
803 // Expand the handle into the package name
804 char* name
= pakfire_parser_expand(makefile
, "packages", namespace);
806 ERROR(build
->pakfire
, "Could not get package name: %m\n");
810 INFO(build
->pakfire
, "Building package '%s'...\n", name
);
811 DEBUG(build
->pakfire
, " buildroot = %s\n", buildroot
);
813 // Fetch build architecture
814 const char* arch
= pakfire_get_arch(build
->pakfire
);
818 // Fetch package from makefile
819 r
= pakfire_parser_create_package(makefile
, &pkg
, NULL
, namespace, arch
);
821 ERROR(build
->pakfire
, "Could not create package from makefile: %m\n");
826 const char* distribution
= pakfire_parser_get(makefile
, NULL
, "DISTRO_NAME");
828 r
= pakfire_package_set_string(pkg
, PAKFIRE_PKG_DISTRO
, distribution
);
834 pakfire_package_set_uuid(pkg
, PAKFIRE_PKG_BUILD_ID
, build
->id
);
836 // Set source package
837 const char* source_name
= pakfire_parser_get(makefile
, NULL
, "name");
839 r
= pakfire_package_set_string(pkg
, PAKFIRE_PKG_SOURCE_NAME
, source_name
);
845 const char* source_evr
= pakfire_parser_get(makefile
, NULL
, "evr");
847 r
= pakfire_package_set_string(pkg
, PAKFIRE_PKG_SOURCE_EVR
, source_evr
);
853 r
= pakfire_package_set_string(pkg
, PAKFIRE_PKG_SOURCE_ARCH
, "src");
858 r
= pakfire_packager_create(&packager
, build
->pakfire
, pkg
);
863 r
= pakfire_build_package_add_files(build
, makefile
, buildroot
, namespace,
869 r
= pakfire_build_package_add_scriptlets(build
, makefile
, namespace,
874 const char* path
= pakfire_repo_get_path(build
->repo
);
876 // Write the finished package
877 r
= pakfire_packager_finish_to_directory(packager
, path
, NULL
);
879 ERROR(build
->pakfire
, "pakfire_packager_finish() failed: %m\n");
883 // Cleanup all packaged files
884 r
= pakfire_packager_cleanup(packager
);
893 pakfire_packager_unref(packager
);
895 pakfire_package_unref(pkg
);
902 static int pakfire_build_package_dump(struct pakfire
* pakfire
,
903 struct pakfire_package
* pkg
, void* p
) {
904 char* dump
= pakfire_package_dump(pkg
, PAKFIRE_PKG_DUMP_LONG
);
908 INFO(pakfire
, "%s\n", dump
);
914 static int pakfire_build_packages(struct pakfire_build
* build
,
915 struct pakfire_parser
* makefile
) {
916 DEBUG(build
->pakfire
, "Creating packages...");
919 const char* buildroot
= pakfire_relpath(build
->pakfire
, build
->buildroot
);
921 // Fetch a list all all packages
922 char** packages
= pakfire_parser_list_namespaces(makefile
, "packages.package:*");
924 ERROR(build
->pakfire
, "Could not find any packages: %m\n");
928 unsigned int num_packages
= 0;
930 // Count how many packages we have
931 for (char** package
= packages
; *package
; package
++)
934 DEBUG(build
->pakfire
, "Found %d package(s)\n", num_packages
);
936 // Build packages in reverse order
937 for (int i
= num_packages
- 1; i
>= 0; i
--) {
938 r
= pakfire_build_package(build
, makefile
, buildroot
, packages
[i
]);
943 // Rescan the build repository to import all packages again
944 r
= pakfire_repo_scan(build
->repo
, 0);
948 // Create a new packagelist
949 r
= pakfire_packagelist_create(&build
->packages
, build
->pakfire
);
953 // Fetch all packages
954 r
= pakfire_repo_to_packagelist(build
->repo
, build
->packages
);
959 r
= pakfire_packagelist_walk(build
->packages
, pakfire_build_package_dump
, NULL
);
973 static int pakfire_build_stage(struct pakfire_build
* build
,
974 struct pakfire_parser
* makefile
, const char* stage
) {
977 // Prepare template for this stage
978 int r
= pakfire_string_format(template, TEMPLATE
, stage
);
982 // Fetch the environment
983 char** envp
= pakfire_parser_make_environ(makefile
);
985 // Create the build script
986 char* script
= pakfire_parser_expand(makefile
, "build", template);
988 ERROR(build
->pakfire
, "Could not generate the build script for stage '%s': %m\n",
993 INFO(build
->pakfire
, "Running build stage '%s'\n", stage
);
995 // Import environment
996 // XXX is this a good idea?
997 r
= pakfire_jail_import_env(build
->jail
, (const char**)envp
);
999 ERROR(build
->pakfire
, "Could not import environment: %m\n");
1004 r
= pakfire_jail_exec_script(build
->jail
, script
, strlen(script
), NULL
, NULL
, NULL
, NULL
);
1006 ERROR(build
->pakfire
, "Build stage '%s' failed with status %d\n", stage
, r
);
1011 for (char** e
= envp
; *e
; e
++)
1022 PAKFIRE_BUILD_CLEANUP_FILES
= (1 << 0),
1023 PAKFIRE_BUILD_ERROR_IF_NOT_EMPTY
= (1 << 1),
1027 This helper function takes a callback which is expected to add any files
1028 to the given filelist which will optionally be all removed after.
1030 static int pakfire_build_post_process_files(struct pakfire_build
* build
,
1031 struct pakfire_filelist
* filelist
, const char* description
,
1032 int (*callback
)(struct pakfire
* pakfire
, struct pakfire_file
* file
, void* data
),
1034 struct pakfire_filelist
* removees
= NULL
;
1037 // Create a filelist with objects that need to be removed
1038 r
= pakfire_filelist_create(&removees
, build
->pakfire
);
1042 // Find all files that need to be removed
1043 r
= pakfire_filelist_walk(filelist
, callback
, removees
);
1047 if (!pakfire_filelist_is_empty(removees
)) {
1049 INFO(build
->pakfire
, "%s\n", description
);
1051 // Show all files which will be removed
1052 pakfire_filelist_dump(removees
, PAKFIRE_FILE_DUMP_FULL
|PAKFIRE_FILE_DUMP_ISSUES
);
1054 // Remove all files on the removee list
1055 if (flags
& PAKFIRE_BUILD_CLEANUP_FILES
) {
1056 r
= pakfire_filelist_cleanup(removees
);
1060 // Remove all files from the filelist
1061 r
= pakfire_filelist_remove_all(filelist
, removees
);
1066 // Report an error if any files have been found
1067 if (flags
& PAKFIRE_BUILD_ERROR_IF_NOT_EMPTY
)
1073 pakfire_filelist_unref(removees
);
1078 static int __pakfire_build_remove_static_libraries(
1079 struct pakfire
* pakfire
, struct pakfire_file
* file
, void* data
) {
1080 struct pakfire_filelist
* removees
= (struct pakfire_filelist
*)data
;
1081 char path
[PATH_MAX
];
1084 // Find all static libraries
1085 if (pakfire_file_matches_class(file
, PAKFIRE_FILE_STATIC_LIBRARY
)) {
1086 // Copy the filename
1087 r
= pakfire_string_set(path
, pakfire_file_get_abspath(file
));
1091 // Remove the extension
1092 r
= pakfire_path_replace_extension(path
, "so");
1096 // Only delete if there is a shared object with the same name
1097 if (pakfire_path_exists(path
))
1098 return pakfire_filelist_add(removees
, file
);
1104 static int pakfire_build_post_remove_static_libraries(
1105 struct pakfire_build
* build
, struct pakfire_filelist
* filelist
) {
1106 return pakfire_build_post_process_files(build
, filelist
,
1107 "Removing static libaries...",
1108 __pakfire_build_remove_static_libraries
,
1109 PAKFIRE_BUILD_CLEANUP_FILES
);
1112 static int __pakfire_build_remove_libtool_archives(
1113 struct pakfire
* pakfire
, struct pakfire_file
* file
, void* data
) {
1114 struct pakfire_filelist
* removees
= (struct pakfire_filelist
*)data
;
1116 // Find all libtool archive files
1117 if (pakfire_file_matches_class(file
, PAKFIRE_FILE_LIBTOOL_ARCHIVE
))
1118 return pakfire_filelist_add(removees
, file
);
1123 static int pakfire_build_post_remove_libtool_archives(
1124 struct pakfire_build
* build
, struct pakfire_filelist
* filelist
) {
1125 return pakfire_build_post_process_files(build
, filelist
,
1126 "Removing libtool archives...",
1127 __pakfire_build_remove_libtool_archives
,
1128 PAKFIRE_BUILD_CLEANUP_FILES
);
1131 static int __pakfire_build_check_broken_symlinks(
1132 struct pakfire
* pakfire
, struct pakfire_file
* file
, void* data
) {
1133 struct pakfire_filelist
* broken
= (struct pakfire_filelist
*)data
;
1136 // Ignore anything that isn't a symlink
1137 switch (pakfire_file_get_type(file
)) {
1139 if (!pakfire_file_symlink_target_exists(file
)) {
1140 r
= pakfire_filelist_add(broken
, file
);
1147 // Ignore anything that isn't a symlink
1155 static int pakfire_build_post_check_broken_symlinks(
1156 struct pakfire_build
* build
, struct pakfire_filelist
* filelist
) {
1157 return pakfire_build_post_process_files(build
, filelist
,
1158 "Broken symlinks have been found:",
1159 __pakfire_build_check_broken_symlinks
,
1160 PAKFIRE_BUILD_ERROR_IF_NOT_EMPTY
);
1166 static int pakfire_build_post_check_buildroot(
1167 struct pakfire_build
* build
, struct pakfire_filelist
* filelist
) {
1168 const char* buildroot
= pakfire_relpath(build
->pakfire
, build
->buildroot
);
1170 // Nested function to keep a reference to buildroot
1171 int __pakfire_build_post_check_buildroot(
1172 struct pakfire
* pakfire
, struct pakfire_file
* file
, void* data
) {
1173 struct pakfire_filelist
* matches
= (struct pakfire_filelist
*)data
;
1176 if (pakfire_file_payload_matches(file
, buildroot
, strlen(buildroot
))) {
1177 r
= pakfire_filelist_add(matches
, file
);
1185 return pakfire_build_post_process_files(
1186 build
, filelist
, "Files containing BUILDROOT:",
1187 __pakfire_build_post_check_buildroot
, PAKFIRE_BUILD_ERROR_IF_NOT_EMPTY
);
1194 static int __pakfire_build_post_check_files(
1195 struct pakfire
* pakfire
, struct pakfire_file
* file
, void* data
) {
1196 struct pakfire_filelist
* broken
= (struct pakfire_filelist
*)data
;
1200 // Check file for issues
1201 r
= pakfire_file_check(file
, &issues
);
1203 ERROR(pakfire
, "%s: File Check failed: %m\n", pakfire_file_get_path(file
));
1207 // If any issues have been found, consider this file to be on the list
1209 r
= pakfire_filelist_add(broken
, file
);
1217 static int pakfire_build_post_check_files(
1218 struct pakfire_build
* build
, struct pakfire_filelist
* filelist
) {
1219 return pakfire_build_post_process_files(
1223 __pakfire_build_post_check_files
,
1224 PAKFIRE_BUILD_ERROR_IF_NOT_EMPTY
);
1227 static int pakfire_build_run_post_build_checks(struct pakfire_build
* build
) {
1228 struct pakfire_filelist
* filelist
= NULL
;
1231 // Create a filelist of all files in the build
1232 r
= pakfire_filelist_create(&filelist
, build
->pakfire
);
1234 ERROR(build
->pakfire
, "Could not create filelist: %m\n");
1238 // Scan for all files in BUILDROOT
1239 r
= pakfire_filelist_scan(filelist
, build
->buildroot
, NULL
, NULL
);
1243 // If the filelist is empty, we can are done
1244 if (pakfire_filelist_is_empty(filelist
)) {
1245 DEBUG(build
->pakfire
, "Empty BUILDROOT. Skipping post build checks...\n");
1250 // Remove any static libraries
1251 r
= pakfire_build_post_remove_static_libraries(build
, filelist
);
1255 // Remove any libtool archives
1256 r
= pakfire_build_post_remove_libtool_archives(build
, filelist
);
1260 // Check for any broken symlinks
1261 r
= pakfire_build_post_check_broken_symlinks(build
, filelist
);
1265 // Check for BUILDROOT
1266 r
= pakfire_build_post_check_buildroot(build
, filelist
);
1271 r
= pakfire_build_post_check_files(build
, filelist
);
1277 pakfire_filelist_unref(filelist
);
1282 static const char* post_build_scripts
[] = {
1285 "check-interpreters",
1286 "compress-man-pages",
1291 static int pakfire_build_run_post_build_scripts(struct pakfire_build
* build
) {
1293 const char* buildroot
= pakfire_relpath(build
->pakfire
, build
->buildroot
);
1295 // Set default arguments for build scripts
1296 const char* args
[] = {
1300 // Run them one by one
1301 for (const char** script
= post_build_scripts
; *script
; script
++) {
1302 int r
= pakfire_build_run_script(build
, *script
, args
, NULL
, NULL
, NULL
);
1310 static void pakfire_build_free(struct pakfire_build
* build
) {
1311 if (build
->packages
)
1312 pakfire_packagelist_unref(build
->packages
);
1315 pakfire_repo_clean(build
->repo
, PAKFIRE_REPO_CLEAN_FLAGS_DESTROY
);
1316 pakfire_repo_unref(build
->repo
);
1320 pakfire_jail_unref(build
->jail
);
1322 if (build
->cgroup
) {
1323 // Destroy the cgroup
1324 pakfire_cgroup_destroy(build
->cgroup
);
1327 pakfire_cgroup_unref(build
->cgroup
);
1330 pakfire_unref(build
->pakfire
);
1334 static int pakfire_build_parse_id(struct pakfire_build
* build
, const char* id
) {
1337 // Try parsing the Build ID
1339 r
= uuid_parse(id
, build
->id
);
1341 ERROR(build
->pakfire
, "Could not parse build ID '%s'\n", id
);
1346 // Otherwise initialize the Build ID with something random
1348 uuid_generate_random(build
->id
);
1351 // Store the ID as string, too
1352 uuid_unparse_lower(build
->id
, build
->_id
);
1358 Sets up a new cgroup for this build
1360 static int pakfire_build_setup_cgroup(struct pakfire_build
* build
) {
1361 struct pakfire_config
* config
= NULL
;
1362 char path
[PATH_MAX
];
1366 r
= pakfire_string_format(path
, "pakfire/build-%s", build
->_id
);
1368 ERROR(build
->pakfire
, "Could not compose path for cgroup: %m\n");
1372 // Create a new cgroup
1373 r
= pakfire_cgroup_open(&build
->cgroup
, build
->pakfire
, path
,
1374 PAKFIRE_CGROUP_ENABLE_ACCOUNTING
);
1376 ERROR(build
->pakfire
, "Could not create cgroup for build %s: %m\n", build
->_id
);
1381 config
= pakfire_get_config(build
->pakfire
);
1385 // Guarantee some minimum memory
1386 size_t memory_guaranteed
= pakfire_config_get_bytes(config
, "build",
1387 "memory_guaranteed", PAKFIRE_BUILD_MEMORY_GUARANTEED
);
1388 if (memory_guaranteed
) {
1389 r
= pakfire_cgroup_set_guaranteed_memory(build
->cgroup
, memory_guaranteed
);
1395 size_t memory_limit
= pakfire_config_get_bytes(config
, "build", "memory_limit", 0);
1397 r
= pakfire_cgroup_set_memory_limit(build
->cgroup
, memory_limit
);
1403 size_t pid_limit
= pakfire_config_get_int(config
, "build",
1404 "pid_limit", PAKFIRE_BUILD_PID_LIMIT
);
1406 r
= pakfire_cgroup_set_pid_limit(build
->cgroup
, pid_limit
);
1413 pakfire_config_unref(config
);
1419 Sets up a new jail for this build
1421 static int pakfire_build_setup_jail(struct pakfire_build
* build
) {
1424 // Create a new jail
1425 r
= pakfire_jail_create(&build
->jail
, build
->pakfire
, 0);
1427 ERROR(build
->pakfire
, "Could not create jail for build %s: %m\n", build
->_id
);
1431 // Connect the jail to our cgroup
1432 r
= pakfire_jail_set_cgroup(build
->jail
, build
->cgroup
);
1434 ERROR(build
->pakfire
, "Could not set cgroup for jail: %m\n");
1443 Sets up the ccache for this build
1445 static int pakfire_build_setup_ccache(struct pakfire_build
* build
) {
1446 char path
[PATH_MAX
];
1449 // Check if we want a ccache
1450 if (pakfire_build_has_flag(build
, PAKFIRE_BUILD_DISABLE_CCACHE
)) {
1451 DEBUG(build
->pakfire
, "ccache usage has been disabled for this build\n");
1453 // Set CCACHE_DISABLE=1 so that if ccache is installed, it will disable itself
1454 r
= pakfire_jail_set_env(build
->jail
, "CCACHE_DISABLE", "1");
1456 ERROR(build
->pakfire
, "Could not disable ccache: %m\n");
1464 r
= pakfire_cache_path(build
->pakfire
, path
, "%s", "ccache");
1466 ERROR(build
->pakfire
, "Could not compose ccache path: %m\n");
1470 DEBUG(build
->pakfire
, "Mounting ccache from %s\n", path
);
1472 // Ensure path exists
1473 r
= pakfire_mkdir(path
, 0755);
1474 if (r
&& errno
!= EEXIST
) {
1475 ERROR(build
->pakfire
, "Could not create ccache directory %s: %m\n", path
);
1479 // Bind-mount the directory
1480 r
= pakfire_jail_bind(build
->jail
, path
, CCACHE_DIR
, MS_NOSUID
|MS_NOEXEC
|MS_NODEV
);
1482 ERROR(build
->pakfire
, "Could not mount ccache: %m\n");
1489 static int pakfire_build_setup_repo(struct pakfire_build
* build
) {
1490 char path
[PATH_MAX
] = PAKFIRE_TMP_DIR
"/pakfire-build-repo.XXXXXX";
1494 // Create a new repository
1495 r
= pakfire_repo_create(&build
->repo
, build
->pakfire
, PAKFIRE_REPO_RESULT
);
1497 ERROR(build
->pakfire
, "Could not create repository %s: %m", PAKFIRE_REPO_RESULT
);
1502 pakfire_repo_set_description(build
->repo
, _("Build Repository"));
1504 // Create a temporary directory
1505 const char* p
= pakfire_mkdtemp(path
);
1507 ERROR(build
->pakfire
, "Could not create a the build repository: %m\n");
1512 r
= pakfire_string_format(url
, "file://%s", path
);
1517 pakfire_repo_set_baseurl(build
->repo
, url
);
1522 static int pakfire_build_set_time_start(struct pakfire_build
* build
) {
1523 const time_t now
= time(NULL
);
1526 ERROR(build
->pakfire
, "Could not fetch start time: %m\n");
1530 build
->time_start
= now
;
1535 PAKFIRE_EXPORT
int pakfire_build_create(struct pakfire_build
** build
,
1536 struct pakfire
* pakfire
, const char* id
, int flags
) {
1539 // Allocate build object
1540 struct pakfire_build
* b
= calloc(1, sizeof(*b
));
1544 // Reference pakfire
1545 b
->pakfire
= pakfire_ref(pakfire
);
1547 // Initialize reference counter
1554 r
= pakfire_build_set_time_start(b
);
1559 r
= pakfire_build_parse_id(b
, id
);
1564 r
= pakfire_build_setup_repo(b
);
1569 r
= pakfire_build_setup_cgroup(b
);
1574 r
= pakfire_build_setup_jail(b
);
1579 r
= pakfire_build_setup_ccache(b
);
1587 pakfire_build_free(b
);
1591 PAKFIRE_EXPORT
struct pakfire_build
* pakfire_build_ref(struct pakfire_build
* build
) {
1597 PAKFIRE_EXPORT
struct pakfire_build
* pakfire_build_unref(struct pakfire_build
* build
) {
1598 if (--build
->nrefs
> 0)
1601 pakfire_build_free(build
);
1605 PAKFIRE_EXPORT
int pakfire_build_set_target(
1606 struct pakfire_build
* build
, const char* target
) {
1607 return pakfire_string_set(build
->target
, target
);
1610 static int pakfire_build_install_packages(struct pakfire_build
* build
,
1611 int* snapshot_needs_update
) {
1614 const char* packages
[] = {
1621 // Install everything
1622 r
= pakfire_install(build
->pakfire
, 0, 0, packages
, NULL
, 0,
1623 &changed
, NULL
, NULL
);
1625 ERROR(build
->pakfire
, "Could not install build dependencies: %m\n");
1629 // Mark snapshot as changed if new packages were installed
1631 *snapshot_needs_update
= 1;
1633 // Update everything
1634 r
= pakfire_sync(build
->pakfire
, 0, 0, &changed
, NULL
, NULL
);
1636 ERROR(build
->pakfire
, "Could not update packages: %m\n");
1640 // Has anything changed?
1642 *snapshot_needs_update
= 1;
1648 Initializes the build environment
1650 static int pakfire_build_init(struct pakfire_build
* build
) {
1653 // Don't do it again
1655 DEBUG(build
->pakfire
, "Build environment has already been initialized\n");
1659 // Tells us whether we need to (re-)create the snapshot
1660 int snapshot_needs_update
= 0;
1663 if (!pakfire_build_has_flag(build
, PAKFIRE_BUILD_DISABLE_SNAPSHOT
)) {
1664 r
= pakfire_snapshot_restore(build
->pakfire
);
1669 // Install or update any build dependencies
1670 r
= pakfire_build_install_packages(build
, &snapshot_needs_update
);
1674 // Update the snapshot if there were changes
1675 if (!pakfire_build_has_flag(build
, PAKFIRE_BUILD_DISABLE_SNAPSHOT
) && snapshot_needs_update
) {
1676 // Store the snapshot
1677 r
= pakfire_snapshot_store(build
->pakfire
);
1682 // Mark as initialized
1688 static int pakfire_build_read_makefile(struct pakfire_build
* build
,
1689 struct pakfire_parser
** parser
, struct pakfire_package
* package
) {
1690 char path
[PATH_MAX
];
1693 struct pakfire_parser_error
* error
= NULL
;
1695 const char* nevra
= pakfire_package_get_string(package
, PAKFIRE_PKG_NEVRA
);
1696 const char* name
= pakfire_package_get_string(package
, PAKFIRE_PKG_NAME
);
1698 // Compose path to makefile
1699 r
= pakfire_path(build
->pakfire
, path
, "/usr/src/packages/%s/%s.nm", nevra
, name
);
1704 r
= pakfire_read_makefile(parser
, build
->pakfire
, path
, &error
);
1707 ERROR(build
->pakfire
, "Could not parse makefile %s: %s\n", path
,
1708 pakfire_parser_error_get_message(error
));
1710 ERROR(build
->pakfire
, "Could not parse makefile %s: %m\n", path
);
1717 const char* buildroot
= pakfire_relpath(build
->pakfire
, build
->buildroot
);
1719 pakfire_parser_set(*parser
, NULL
, "BUILDROOT", buildroot
, 0);
1723 pakfire_parser_error_unref(error
);
1728 static int pakfire_build_perform(struct pakfire_build
* build
,
1729 struct pakfire_parser
* makefile
) {
1732 // Prepare the build
1733 r
= pakfire_build_stage(build
, makefile
, "prepare");
1737 // Perform the build
1738 r
= pakfire_build_stage(build
, makefile
, "build");
1743 if (!pakfire_build_has_flag(build
, PAKFIRE_BUILD_DISABLE_TESTS
)) {
1744 r
= pakfire_build_stage(build
, makefile
, "test");
1749 // Install everything
1750 r
= pakfire_build_stage(build
, makefile
, "install");
1754 // Run post build checks
1755 r
= pakfire_build_run_post_build_checks(build
);
1757 ERROR(build
->pakfire
, "Post build checks failed\n");
1761 // Run post build scripts
1762 r
= pakfire_build_run_post_build_scripts(build
);
1770 // Drop to a shell for debugging
1771 if (pakfire_build_has_flag(build
, PAKFIRE_BUILD_INTERACTIVE
))
1772 pakfire_build_shell(build
);
1777 static int __pakfire_build_unpackaged_file(struct pakfire
* pakfire
,
1778 struct pakfire_file
* file
, void* p
) {
1779 char* s
= pakfire_file_dump(file
, PAKFIRE_FILE_DUMP_FULL
);
1781 ERROR(pakfire
, "%s\n", s
);
1788 static int pakfire_build_check_unpackaged_files(struct pakfire_build
* build
) {
1789 struct pakfire_filelist
* filelist
= NULL
;
1792 // Create a new filelist
1793 r
= pakfire_filelist_create(&filelist
, build
->pakfire
);
1797 // Scan for all files in BUILDROOT
1798 r
= pakfire_filelist_scan(filelist
, build
->buildroot
, NULL
, NULL
);
1802 if (!pakfire_filelist_is_empty(filelist
)) {
1803 ERROR(build
->pakfire
, "Unpackaged files found:\n");
1805 r
= pakfire_filelist_walk(filelist
, __pakfire_build_unpackaged_file
, NULL
);
1815 pakfire_filelist_unref(filelist
);
1820 static int pakfire_build_install_package(struct pakfire
* pakfire
,
1821 struct pakfire_package
* pkg
, void* p
) {
1822 struct pakfire_request
* request
= (struct pakfire_request
*)p
;
1824 return pakfire_request_add_package(request
, PAKFIRE_REQ_INSTALL
, pkg
,
1825 PAKFIRE_REQUEST_ESSENTIAL
);
1828 static int pakfire_build_install_test(struct pakfire_build
* build
) {
1829 struct pakfire_request
* request
= NULL
;
1830 struct pakfire_problem
* problem
= NULL
;
1831 struct pakfire_solution
* solution
= NULL
;
1832 const char* s
= NULL
;
1835 // Create a new request
1836 r
= pakfire_request_create(&request
, build
->pakfire
, 0);
1841 r
= pakfire_packagelist_walk(build
->packages
, pakfire_build_install_package
, request
);
1843 // Solve the request
1844 r
= pakfire_request_solve(request
);
1852 ERROR(build
->pakfire
, "Install test failed:\n");
1854 // Walk through all problems
1856 r
= pakfire_request_next_problem(request
, &problem
);
1860 // There are no more problems
1864 // Format the problem into something human-readable
1865 s
= pakfire_problem_to_string(problem
);
1869 ERROR(build
->pakfire
, " * %s\n", s
);
1871 // Walk through all solutions
1873 r
= pakfire_problem_next_solution(problem
, &solution
);
1877 // There are no more solutions
1881 // Format the solution into something human-readable
1882 s
= pakfire_solution_to_string(solution
);
1886 ERROR(build
->pakfire
, " * %s\n", s
);
1899 ERROR(build
->pakfire
, "Install test failed: %m\n");
1901 pakfire_request_unref(request
);
1903 pakfire_problem_unref(problem
);
1905 pakfire_solution_unref(solution
);
1910 static int pakfire_build_post_check(struct pakfire_build
* build
) {
1913 // Check for unpackaged files
1914 r
= pakfire_build_check_unpackaged_files(build
);
1918 // Perform install test
1919 r
= pakfire_build_install_test(build
);
1926 static int pakfire_build_copy_package(struct pakfire
* pakfire
,
1927 struct pakfire_package
* pkg
, void* p
) {
1928 struct pakfire_archive
* archive
= NULL
;
1929 char path
[PATH_MAX
];
1932 const char* target
= (const char*)p
;
1939 // Fetch the package filename
1940 const char* filename
= pakfire_package_get_string(pkg
, PAKFIRE_PKG_FILENAME
);
1946 // Format the destination path
1947 r
= pakfire_string_format(path
, "%s/%s", target
, filename
);
1952 archive
= pakfire_package_get_archive(pkg
);
1958 // Copy it to its destination
1959 r
= pakfire_archive_copy(archive
, path
);
1965 pakfire_archive_unref(archive
);
1970 static int pakfire_build_copy_packages(struct pakfire_build
* build
) {
1971 struct pakfire_repo
* local
= NULL
;
1974 DEBUG(build
->pakfire
, "Copying built packages\n");
1976 // Fetch local repository
1977 local
= pakfire_get_repo(build
->pakfire
, PAKFIRE_REPO_LOCAL
);
1979 // Copy all packages to the target path
1980 if (*build
->target
) {
1981 r
= pakfire_packagelist_walk(build
->packages
,
1982 pakfire_build_copy_package
, build
->target
);
1987 // If we have a local repository, we copy all packages to it
1989 const char* path
= pakfire_repo_get_path(local
);
1991 r
= pakfire_packagelist_walk(build
->packages
,
1992 pakfire_build_copy_package
, (void*)path
);
2000 pakfire_repo_unref(local
);
2005 static int pakfire_build_install_source_package(
2006 struct pakfire_build
* build
, struct pakfire_package
* package
) {
2007 struct pakfire_request
* request
= NULL
;
2008 struct pakfire_transaction
* transaction
= NULL
;
2011 // Create a new request
2012 r
= pakfire_request_create(&request
, build
->pakfire
, 0);
2017 r
= pakfire_request_add_package(request
, PAKFIRE_REQ_INSTALL
, package
,
2018 PAKFIRE_REQUEST_ESSENTIAL
);
2022 // Solve the request
2023 r
= pakfire_request_solve(request
);
2027 // Fetch the transaction
2028 r
= pakfire_request_get_transaction(request
, &transaction
);
2032 // Set how many packages have been changed
2033 const size_t changes
= pakfire_transaction_count(transaction
);
2035 // Sanity check to see if we actually try to install anything
2037 ERROR(build
->pakfire
, "The source package did not get installed\n");
2042 // Run the transaction
2043 r
= pakfire_transaction_run(transaction
, 0);
2049 pakfire_transaction_unref(transaction
);
2051 pakfire_request_unref(request
);
2056 PAKFIRE_EXPORT
int pakfire_build_exec(struct pakfire_build
* build
, const char* path
) {
2057 struct pakfire_package
* package
= NULL
;
2058 struct pakfire_parser
* makefile
= NULL
;
2059 char* buildroot
= NULL
;
2060 char duration
[TIME_STRING_MAX
];
2064 r
= pakfire_path(build
->pakfire
, build
->buildroot
, "%s",
2065 PAKFIRE_TMP_DIR
"/pakfire-buildroot.XXXXXX");
2069 // Open the source package
2070 r
= pakfire_commandline_add(build
->pakfire
, path
, &package
);
2074 const char* nevra
= pakfire_package_get_string(package
, PAKFIRE_PKG_NEVRA
);
2076 INFO(build
->pakfire
, "Building %s...\n", nevra
);
2078 // Initialize the build environment
2079 r
= pakfire_build_init(build
);
2083 // Install the source package
2084 r
= pakfire_build_install_source_package(build
, package
);
2086 ERROR(build
->pakfire
, "Could not install the source package: %m\n");
2091 buildroot
= pakfire_mkdtemp(build
->buildroot
);
2093 ERROR(build
->pakfire
, "Could not create BUILDROOT: %m\n");
2097 // Open the makefile
2098 r
= pakfire_build_read_makefile(build
, &makefile
, package
);
2102 // Perform the actual build
2103 r
= pakfire_build_perform(build
, makefile
);
2107 // Create the packages
2108 r
= pakfire_build_packages(build
, makefile
);
2110 ERROR(build
->pakfire
, "Could not create packages: %m\n");
2114 // Perform post build checks
2115 r
= pakfire_build_post_check(build
);
2119 // Copy packages to their destination
2120 r
= pakfire_build_copy_packages(build
);
2125 r
= pakfire_format_time(duration
, pakfire_build_duration(build
));
2129 INFO(build
->pakfire
, "Build successfully completed in %s\n", duration
);
2133 pakfire_parser_unref(makefile
);
2135 pakfire_package_unref(package
);
2137 // Cleanup buildroot
2139 pakfire_rmtree(buildroot
, 0);
2145 Compatibility function to keep the legacy API.
2147 PAKFIRE_EXPORT
int pakfire_build(struct pakfire
* pakfire
, const char* path
,
2148 const char* target
, const char* id
, int flags
) {
2149 struct pakfire_build
* build
= NULL
;
2152 // Check if path is set
2158 // Create a new build environment
2159 r
= pakfire_build_create(&build
, pakfire
, id
, flags
);
2165 r
= pakfire_build_set_target(build
, target
);
2171 r
= pakfire_build_exec(build
, path
);
2175 pakfire_build_unref(build
);
2180 int pakfire_build_clean(struct pakfire
* pakfire
, int flags
) {
2181 struct pakfire_repo
* local
= NULL
;
2184 // Fetch local repository
2185 local
= pakfire_get_repo(pakfire
, PAKFIRE_REPO_LOCAL
);
2187 ERROR(pakfire
, "Could not find repository %s: %m\n", PAKFIRE_REPO_LOCAL
);
2191 // Destroy everything in it
2192 r
= pakfire_repo_clean(local
, PAKFIRE_REPO_CLEAN_FLAGS_DESTROY
);
2198 pakfire_repo_unref(local
);
2204 This is a convenience function that sets up a build environment and
2205 then drops the user into an interactive shell.
2207 PAKFIRE_EXPORT
int pakfire_shell(struct pakfire
* pakfire
, const char** packages
, int flags
) {
2208 struct pakfire_build
* build
= NULL
;
2211 // Shells are always interactive
2212 flags
|= PAKFIRE_BUILD_INTERACTIVE
;
2214 // Create a new build environment
2215 r
= pakfire_build_create(&build
, pakfire
, NULL
, flags
);
2217 ERROR(pakfire
, "Could not create build: %m\n");
2221 // Initialize the build environment
2222 r
= pakfire_build_init(build
);
2226 // Install any additional packages
2228 r
= pakfire_install(build
->pakfire
, 0, 0, packages
, NULL
, 0, NULL
, NULL
, NULL
);
2234 r
= pakfire_build_shell(build
);
2238 pakfire_build_unref(build
);