2019-03-08 18:18:31 +01:00
|
|
|
/* SPDX-License-Identifier: ISC */
|
2019-03-07 22:57:30 +01:00
|
|
|
#include "buildstrategy.h"
|
|
|
|
|
2019-03-08 17:10:47 +01:00
|
|
|
typedef int (*line_handler_t)(const char *filename, size_t linenum,
|
|
|
|
const char *lhs, const char *rhs);
|
2019-03-07 22:57:30 +01:00
|
|
|
|
2019-03-08 17:10:47 +01:00
|
|
|
struct userdata {
|
|
|
|
line_handler_t cb;
|
|
|
|
};
|
2019-03-07 22:57:30 +01:00
|
|
|
|
2019-03-08 17:10:47 +01:00
|
|
|
static int handle_line(void *usr, const char *filename,
|
|
|
|
size_t linenum, char *line)
|
|
|
|
{
|
|
|
|
struct userdata *u = usr;
|
|
|
|
char *rhs;
|
2019-03-07 22:57:30 +01:00
|
|
|
|
2019-03-08 17:10:47 +01:00
|
|
|
rhs = strchr(line, ',');
|
|
|
|
if (rhs == NULL)
|
|
|
|
return 0;
|
2019-03-07 22:57:30 +01:00
|
|
|
|
2019-03-08 17:10:47 +01:00
|
|
|
*(rhs++) = '\0';
|
|
|
|
while (isspace(*rhs))
|
|
|
|
++rhs;
|
2019-03-07 22:57:30 +01:00
|
|
|
|
2019-03-08 17:10:47 +01:00
|
|
|
if (*line == '\0' || *rhs == '\0')
|
|
|
|
return 0;
|
2019-03-07 22:57:30 +01:00
|
|
|
|
2019-03-08 17:10:47 +01:00
|
|
|
return u->cb(filename, linenum, line, rhs);
|
|
|
|
}
|
2019-03-07 22:57:30 +01:00
|
|
|
|
2019-03-08 17:10:47 +01:00
|
|
|
static int foreach_line(const char *filename, line_handler_t cb)
|
|
|
|
{
|
|
|
|
struct userdata u = { cb };
|
2019-03-07 22:57:30 +01:00
|
|
|
|
2019-03-08 17:10:47 +01:00
|
|
|
return foreach_line_in_file(filename, &u, handle_line);
|
2019-03-07 22:57:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*****************************************************************************/
|
|
|
|
|
2019-03-08 17:10:47 +01:00
|
|
|
static int handle_depends(const char *filename, size_t linenum,
|
|
|
|
const char *sourcepkg, const char *binpkg)
|
2019-03-07 22:57:30 +01:00
|
|
|
{
|
|
|
|
source_pkg_t *src, *dep;
|
|
|
|
size_t count;
|
|
|
|
void *new;
|
2019-03-29 23:31:45 +01:00
|
|
|
(void)filename; (void)linenum;
|
2019-03-07 22:57:30 +01:00
|
|
|
|
2019-03-30 17:57:34 +01:00
|
|
|
src = src_pkg_get(sourcepkg);
|
2019-03-07 22:57:30 +01:00
|
|
|
if (src == NULL)
|
|
|
|
return 0;
|
|
|
|
|
2019-03-30 18:20:33 +01:00
|
|
|
dep = provider_get(sourcepkg, binpkg);
|
2019-03-29 23:31:45 +01:00
|
|
|
if (dep == NULL)
|
2019-03-07 22:57:30 +01:00
|
|
|
return -1;
|
|
|
|
|
|
|
|
if ((src->num_depends % 10) == 0) {
|
|
|
|
count = src->num_depends + 10;
|
|
|
|
|
|
|
|
new = realloc(src->depends, sizeof(src->depends[0]) * count);
|
|
|
|
if (new == NULL) {
|
|
|
|
fputs("out of memory\n", stderr);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
src->depends = new;
|
|
|
|
}
|
|
|
|
|
|
|
|
src->depends[src->num_depends++] = dep;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-03-08 17:10:47 +01:00
|
|
|
static int handle_provides(const char *filename, size_t linenum,
|
|
|
|
const char *sourcepkg, const char *binpkg)
|
2019-03-07 22:57:30 +01:00
|
|
|
{
|
2019-03-29 23:31:45 +01:00
|
|
|
(void)filename; (void)linenum;
|
|
|
|
|
2019-03-30 18:20:33 +01:00
|
|
|
return provider_add(sourcepkg, binpkg);
|
2019-03-29 23:31:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static int handle_prefere(const char *filename, size_t linenum,
|
|
|
|
const char *binpkg, const char *sourcepkg)
|
|
|
|
{
|
2019-03-30 18:20:33 +01:00
|
|
|
(void)filename; (void)linenum;
|
2019-03-07 22:57:30 +01:00
|
|
|
|
2019-03-30 18:20:33 +01:00
|
|
|
return provider_add_prefered(binpkg, sourcepkg);
|
2019-03-07 22:57:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*****************************************************************************/
|
|
|
|
|
|
|
|
static const struct option long_opts[] = {
|
2019-03-29 23:31:45 +01:00
|
|
|
{ "prefere", required_argument, NULL, 'P' },
|
2019-03-29 21:40:11 +01:00
|
|
|
{ "provides", required_argument, NULL, 'p' },
|
|
|
|
{ "depends", required_argument, NULL, 'd' },
|
2019-03-30 23:11:29 +01:00
|
|
|
{ "graph", no_argument, NULL, 'g' },
|
2019-03-07 22:57:30 +01:00
|
|
|
{ NULL, 0, NULL, 0 },
|
|
|
|
};
|
|
|
|
|
2019-03-30 23:11:29 +01:00
|
|
|
static const char *short_opts = "p:d:P:g";
|
2019-03-07 22:57:30 +01:00
|
|
|
|
2019-03-30 18:31:40 +01:00
|
|
|
static void pkg_mark_deps(source_pkg_t *pkg)
|
|
|
|
{
|
|
|
|
size_t i;
|
|
|
|
|
|
|
|
if (pkg->flags & FLAG_BUILD_PKG)
|
|
|
|
return;
|
|
|
|
|
|
|
|
pkg->flags |= FLAG_BUILD_PKG;
|
|
|
|
|
|
|
|
for (i = 0; i < pkg->num_depends; ++i) {
|
|
|
|
if ((pkg->depends[i]->flags & FLAG_BUILD_PKG) == 0)
|
|
|
|
pkg_mark_deps(pkg->depends[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static int cmd_buildstrategy(int argc, char **argv)
|
2019-03-07 22:57:30 +01:00
|
|
|
{
|
2019-03-29 23:31:45 +01:00
|
|
|
const char *provides = NULL, *depends = NULL, *prefere = NULL;
|
2019-03-30 23:11:29 +01:00
|
|
|
int i, ret = EXIT_FAILURE, mode = MODE_BUILD_ORDER;
|
2019-03-30 18:31:40 +01:00
|
|
|
source_pkg_t *pkg;
|
|
|
|
|
|
|
|
if (src_pkg_init())
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
|
|
|
|
if (provider_init())
|
|
|
|
goto out_src;
|
2019-03-07 22:57:30 +01:00
|
|
|
|
|
|
|
for (;;) {
|
|
|
|
i = getopt_long(argc, argv, short_opts, long_opts, NULL);
|
|
|
|
if (i == -1)
|
|
|
|
break;
|
|
|
|
|
|
|
|
switch (i) {
|
2019-03-29 23:31:45 +01:00
|
|
|
case 'P':
|
|
|
|
prefere = optarg;
|
|
|
|
break;
|
2019-03-07 22:57:30 +01:00
|
|
|
case 'p':
|
|
|
|
provides = optarg;
|
|
|
|
break;
|
|
|
|
case 'd':
|
|
|
|
depends = optarg;
|
|
|
|
break;
|
2019-03-30 23:11:29 +01:00
|
|
|
case 'g':
|
|
|
|
mode = MODE_BUILD_GRAPH;
|
|
|
|
break;
|
2019-03-07 22:57:30 +01:00
|
|
|
default:
|
|
|
|
goto fail_arg;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (optind >= argc) {
|
|
|
|
fputs("no packages specified\n", stderr);
|
|
|
|
goto fail_arg;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (provides == NULL) {
|
|
|
|
fputs("no source package provides specified\n", stderr);
|
|
|
|
goto fail_arg;
|
|
|
|
}
|
|
|
|
|
2019-03-29 23:31:45 +01:00
|
|
|
if (prefere != NULL && foreach_line(prefere, handle_prefere) != 0)
|
2019-03-30 18:31:40 +01:00
|
|
|
goto out;
|
2019-03-29 23:31:45 +01:00
|
|
|
|
|
|
|
if (foreach_line(provides, handle_provides))
|
2019-03-30 18:31:40 +01:00
|
|
|
goto out;
|
2019-03-29 23:31:45 +01:00
|
|
|
|
2019-03-07 22:57:30 +01:00
|
|
|
if (depends != NULL && foreach_line(depends, handle_depends) != 0)
|
2019-03-30 18:31:40 +01:00
|
|
|
goto out;
|
2019-03-30 17:57:34 +01:00
|
|
|
|
2019-03-07 22:57:30 +01:00
|
|
|
for (i = optind; i < argc; ++i) {
|
2019-03-30 18:20:33 +01:00
|
|
|
pkg = provider_get(NULL, argv[i]);
|
2019-03-29 23:31:45 +01:00
|
|
|
if (pkg == NULL)
|
2019-03-07 22:57:30 +01:00
|
|
|
goto out;
|
|
|
|
|
|
|
|
pkg_mark_deps(pkg);
|
|
|
|
}
|
|
|
|
|
2019-03-30 23:11:29 +01:00
|
|
|
switch (mode) {
|
|
|
|
case MODE_BUILD_GRAPH:
|
|
|
|
fputs("digraph buildgraph {\n\tcompound=true;\n", stdout);
|
|
|
|
src_pkg_print_graph_cluster();
|
|
|
|
fputs("}\n", stdout);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
if (src_pkg_output_build_order())
|
|
|
|
goto out;
|
|
|
|
break;
|
|
|
|
}
|
2019-03-07 22:57:30 +01:00
|
|
|
|
|
|
|
ret = EXIT_SUCCESS;
|
|
|
|
out:
|
2019-03-30 18:20:33 +01:00
|
|
|
provider_cleanup();
|
2019-03-30 18:31:40 +01:00
|
|
|
out_src:
|
2019-03-30 17:57:34 +01:00
|
|
|
src_pkg_cleanup();
|
2019-03-07 22:57:30 +01:00
|
|
|
return ret;
|
2019-03-30 18:31:40 +01:00
|
|
|
fail_arg:
|
|
|
|
tell_read_help(argv[0]);
|
|
|
|
goto out;
|
2019-03-07 22:57:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static command_t buildstrategy = {
|
|
|
|
.cmd = "buildstrategy",
|
|
|
|
.usage = "[OPTIONS...] <packages>",
|
|
|
|
.s_desc = "work out what source packages to build in what order",
|
|
|
|
.l_desc =
|
|
|
|
"The buildstrategy command takes as input a description of what binary\n"
|
|
|
|
"packages a source package needs in order to build and what binary packages\n"
|
|
|
|
"in turn produces in the process. The command then takes from the command\n"
|
|
|
|
"line arguments a list of desired binary packages and works out a minimal\n"
|
|
|
|
"set of source packages to build and in what order they should be built.\n"
|
|
|
|
"\n"
|
|
|
|
"Possible options:\n"
|
|
|
|
" --provides, -p <file> A two column CSV file. Each line contains the name\n"
|
|
|
|
" of a source package and a binary package that it\n"
|
|
|
|
" produces when built. If a source packages provides\n"
|
|
|
|
" multiple binary packages, use one line for each\n"
|
|
|
|
" binary package.\n"
|
|
|
|
" --depends, -d <file> A two column CSV file. Each line contains the name\n"
|
|
|
|
" of a source package and a binary package that it\n"
|
2019-03-29 23:31:45 +01:00
|
|
|
" requires to build.\n"
|
|
|
|
" --prefere, -P <file> A two column CSV file. Each line contains the name\n"
|
|
|
|
" of a binary package and a source package that\n"
|
|
|
|
" produces this binary. If the `--provides` file\n"
|
|
|
|
" specifies that more than one source pacakge\n"
|
|
|
|
" provides a binary package, this file contains the\n"
|
2019-03-30 23:11:29 +01:00
|
|
|
" prefered one we should use.\n"
|
|
|
|
" --graph, -g Instead of printing out an ordered list, produce\n"
|
|
|
|
" a dot graph of the source packages to be built.\n",
|
2019-03-07 22:57:30 +01:00
|
|
|
.run_cmd = cmd_buildstrategy,
|
|
|
|
};
|
|
|
|
|
|
|
|
REGISTER_COMMAND(buildstrategy)
|