]>
Commit | Line | Data |
---|---|---|
27629aaf MT |
1 | /*############################################################################# |
2 | # # | |
3 | # Pakfire - The IPFire package management system # | |
4 | # Copyright (C) 2023 Pakfire development team # | |
5 | # # | |
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. # | |
10 | # # | |
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. # | |
15 | # # | |
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/>. # | |
18 | # # | |
19 | #############################################################################*/ | |
20 | ||
21 | #include <argp.h> | |
22 | ||
4e38be65 MT |
23 | #include <pakfire/buildservice.h> |
24 | ||
27629aaf MT |
25 | #include "client-build.h" |
26 | #include "command.h" | |
4e38be65 | 27 | #include "pakfire.h" |
27629aaf MT |
28 | |
29 | static const char* args_doc = "[OPTIONS...] MAKEFILES..."; | |
30 | ||
31 | static const char* doc = "Build packages remotely"; | |
32 | ||
33 | #define MAX_ARCHES 8 | |
34 | #define MAX_UPLOADS 32 | |
35 | #define MAX_PACKAGES 32 | |
36 | #define MAX_MAKEFILES 32 | |
37 | ||
38 | struct config { | |
39 | const char* repo; | |
40 | int flags; | |
41 | ||
42 | const char* arches[MAX_ARCHES]; | |
43 | unsigned int num_arches; | |
44 | ||
45 | // Packages | |
46 | char* packages[MAX_PACKAGES]; | |
47 | unsigned int num_packages; | |
48 | ||
49 | // Uploads | |
50 | char* uploads[MAX_UPLOADS]; | |
51 | unsigned int num_uploads; | |
52 | }; | |
53 | ||
54 | enum { | |
55 | OPT_ARCH = 1, | |
56 | OPT_DISABLE_TESTS = 2, | |
57 | OPT_REPO = 3, | |
58 | }; | |
59 | ||
60 | static struct argp_option options[] = { | |
61 | { "arch", OPT_ARCH, "ARCH", 0, "Build for this architecture", 0 }, | |
62 | { "disable-tests", OPT_DISABLE_TESTS, NULL, 0, "Do not run tests", 0 }, | |
63 | { "repo", OPT_REPO, "REPO", 0, "Build against this repository", 0 }, | |
64 | { NULL }, | |
65 | }; | |
66 | ||
67 | static error_t parse(int key, char* arg, struct argp_state* state, void* data) { | |
68 | struct config* config = data; | |
69 | ||
70 | switch (key) { | |
71 | case OPT_ARCH: | |
72 | if (config->num_arches >= MAX_ARCHES) | |
73 | return -ENOBUFS; | |
74 | ||
75 | config->arches[config->num_arches++] = arg; | |
76 | break; | |
77 | ||
78 | case OPT_DISABLE_TESTS: | |
79 | config->flags |= PAKFIRE_BUILD_DISABLE_TESTS; | |
80 | break; | |
81 | ||
82 | case OPT_REPO: | |
83 | config->repo = arg; | |
84 | break; | |
85 | ||
86 | case ARGP_KEY_ARG: | |
87 | if (config->num_packages >= MAX_PACKAGES) | |
88 | return -ENOBUFS; | |
89 | ||
90 | config->packages[config->num_packages++] = arg; | |
91 | break; | |
92 | ||
93 | default: | |
94 | return ARGP_ERR_UNKNOWN; | |
95 | } | |
96 | ||
97 | return 0; | |
98 | } | |
99 | ||
100 | int cli_client_build(void* data, int argc, char* argv[]) { | |
101 | struct pakfire_buildservice* service = NULL; | |
102 | struct config config = {}; | |
103 | char* upload = NULL; | |
104 | int r; | |
105 | ||
4e38be65 | 106 | struct cli_config* cli_config = data; |
27629aaf MT |
107 | |
108 | // Parse the command line | |
9b8d2c6e | 109 | r = cli_parse(options, NULL, args_doc, doc, parse, 0, argc, argv, &config); |
27629aaf MT |
110 | if (r) |
111 | goto ERROR; | |
112 | ||
113 | // Connect to the build service | |
4e38be65 | 114 | r = pakfire_buildservice_create(&service, cli_config->ctx); |
27629aaf MT |
115 | if (r) |
116 | goto ERROR; | |
117 | ||
118 | // Upload all packages | |
119 | for (unsigned int i = 0; i < config.num_packages; i++) { | |
120 | r = pakfire_buildservice_upload(service, config.packages[i], NULL, &upload); | |
121 | if (r) | |
122 | goto ERROR; | |
123 | ||
124 | // Store the upload ID | |
125 | config.uploads[config.num_uploads++] = upload; | |
126 | } | |
127 | ||
128 | // No uploads | |
129 | if (!config.num_uploads) | |
130 | goto ERROR; | |
131 | ||
132 | // Build all the things | |
133 | for (unsigned int i = 0; i < config.num_uploads; i++) { | |
134 | r = pakfire_buildservice_build(service, config.uploads[i], config.repo, | |
135 | config.arches, config.flags); | |
136 | if (r) | |
137 | goto ERROR; | |
138 | ||
139 | // Free the upload | |
140 | free(config.uploads[i]); | |
141 | config.uploads[i] = NULL; | |
142 | } | |
143 | ||
144 | ERROR: | |
145 | // Delete & free all uploads that could not be processed | |
146 | for (unsigned int i = 0; i < config.num_uploads; i++) { | |
147 | if (config.uploads[i]) { | |
148 | pakfire_buildservice_delete_upload(service, config.uploads[i]); | |
149 | free(config.uploads[i]); | |
150 | } | |
151 | } | |
152 | if (service) | |
153 | pakfire_buildservice_unref(service); | |
154 | ||
155 | return r; | |
156 | } |