--- /dev/null
+/*#############################################################################
+# #
+# Pakfire - The IPFire package management system #
+# Copyright (C) 2023 Pakfire development team #
+# #
+# This program is free software: you can redistribute it and/or modify #
+# it under the terms of the GNU General Public License as published by #
+# the Free Software Foundation, either version 3 of the License, or #
+# (at your option) any later version. #
+# #
+# This program is distributed in the hope that it will be useful, #
+# but WITHOUT ANY WARRANTY; without even the implied warranty of #
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
+# GNU General Public License for more details. #
+# #
+# You should have received a copy of the GNU General Public License #
+# along with this program. If not, see <http://www.gnu.org/licenses/>. #
+# #
+#############################################################################*/
+
+#include <errno.h>
+#include <getopt.h>
+#include <stdlib.h>
+
+#include "build.h"
+#include "command.h"
+
+#include <pakfire/build.h>
+#include <pakfire/pakfire.h>
+
+struct config {
+ const char* id;
+ const char* target;
+ int flags;
+};
+
+static void help(void) {
+ printf(
+ "%s [OPTIONS...] build [OPTIONS...] MAKEFILE\n\n"
+ "Options:\n"
+ " --disable-ccache Disables using ccache\n"
+ " --disable-snapshot Do not use the snapshot\n"
+ " --disable-tests Do not run tests\n"
+ " --non-interactive Run the build non-interactively\n"
+ " --target Output all packages to this directory\n"
+ " -h --help Show help\n",
+ program_invocation_short_name
+ );
+
+ exit(0);
+}
+
+static int parse_argv(struct config* config, int argc, char* argv[]) {
+ enum {
+ ARG_DISABLE_CCACHE,
+ ARG_DISABLE_SNAPSHOT,
+ ARG_DISABLE_TESTS,
+ ARG_ID,
+ ARG_NON_INTERACTIVE,
+ ARG_TARGET,
+ };
+
+ static const struct option options[] = {
+ { "disable-ccache", no_argument, NULL, ARG_DISABLE_CCACHE },
+ { "disable-snapshot", no_argument, NULL, ARG_DISABLE_SNAPSHOT },
+ { "disable-tests", no_argument, NULL, ARG_DISABLE_TESTS },
+ { "id", required_argument, NULL, ARG_ID },
+ { "non-interactive", no_argument, NULL, ARG_NON_INTERACTIVE },
+ { "target", required_argument, NULL, ARG_TARGET },
+ { "help", no_argument, NULL, 'h' },
+ { NULL },
+ };
+ int c;
+
+ for (;;) {
+ c = getopt_long(argc, argv, "h", options, NULL);
+ if (c < 0)
+ break;
+
+ switch (c) {
+ case 'h':
+ help();
+
+ case ARG_DISABLE_CCACHE:
+ config->flags |= PAKFIRE_BUILD_DISABLE_CCACHE;
+ break;
+
+ case ARG_DISABLE_SNAPSHOT:
+ config->flags |= PAKFIRE_BUILD_DISABLE_SNAPSHOT;
+ break;
+
+ case ARG_DISABLE_TESTS:
+ config->flags |= PAKFIRE_BUILD_DISABLE_TESTS;
+ break;
+
+ case ARG_ID:
+ config->id = optarg;
+ break;
+
+ case ARG_NON_INTERACTIVE:
+ config->flags &= ~PAKFIRE_BUILD_INTERACTIVE;
+ break;
+
+ case ARG_TARGET:
+ config->target = optarg;
+ break;
+
+ case '?':
+ return -EINVAL;
+
+ default:
+ break;
+ }
+ }
+
+ return 0;
+}
+
+int cli_build(struct pakfire* pakfire, int argc, char* argv[]) {
+ struct pakfire_build* build = NULL;
+ struct config config = {
+ .id = NULL,
+ .target = NULL,
+ .flags = PAKFIRE_BUILD_INTERACTIVE,
+ };
+ int r;
+
+ // Parse commandline
+ r = parse_argv(&config, argc, argv);
+ if (r)
+ return r;
+
+ // Setup the build environment
+ r = pakfire_build_create(&build, pakfire, config.id, config.flags);
+ if (r) {
+ fprintf(stderr, "Could not setup the build environment: %m\n");
+ goto ERROR;
+ }
+
+ // Set target
+ if (config.target) {
+ r = pakfire_build_set_target(build, config.target);
+ if (r) {
+ fprintf(stderr, "Could not set target directory %s: %m\n", config.target);
+ goto ERROR;
+ }
+ }
+
+ // Process all packages
+ for (int i = optind; i < argc; i++) {
+ // XXX implement dist
+
+ // Run the build
+ r = pakfire_build_exec(build, argv[i]);
+ if (r) {
+ fprintf(stderr, "Could not build %s\n", argv[i]);
+ goto ERROR;
+ }
+ }
+
+ERROR:
+ if (build)
+ pakfire_build_unref(build);
+
+ return r;
+}
--- /dev/null
+/*#############################################################################
+# #
+# Pakfire - The IPFire package management system #
+# Copyright (C) 2023 Pakfire development team #
+# #
+# This program is free software: you can redistribute it and/or modify #
+# it under the terms of the GNU General Public License as published by #
+# the Free Software Foundation, either version 3 of the License, or #
+# (at your option) any later version. #
+# #
+# This program is distributed in the hope that it will be useful, #
+# but WITHOUT ANY WARRANTY; without even the implied warranty of #
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
+# GNU General Public License for more details. #
+# #
+# You should have received a copy of the GNU General Public License #
+# along with this program. If not, see <http://www.gnu.org/licenses/>. #
+# #
+#############################################################################*/
+
+#ifndef PAKFIRE_CLI_BUILD_H
+#define PAKFIRE_CLI_BUILD_H
+
+#include <pakfire/pakfire.h>
+
+int cli_build(struct pakfire* pakfire, int argc, char* argv[]);
+
+#endif /* PAKFIRE_CLI_BUILD_H */