This commit is contained in:
LDA 2024-10-17 17:13:08 +02:00
commit d9b5141eb5

View file

@ -11,6 +11,7 @@
#include <sys/stat.h> #include <sys/stat.h>
#include <stdbool.h> #include <stdbool.h>
#include <dirent.h> #include <dirent.h>
#include <errno.h>
#include <unistd.h> #include <unistd.h>
#include <stdlib.h> #include <stdlib.h>
#include <limits.h> #include <limits.h>
@ -185,18 +186,34 @@ cmd_stdout(char *cmd)
FILE *f; FILE *f;
char *line = NULL; char *line = NULL;
size_t size; size_t size;
int result;
if (!cmd) if (!cmd)
{ {
return NULL; goto failure;
} }
if (!(f = popen(cmd, "r"))) if (!(f = popen(cmd, "r")))
{ {
return NULL; goto failure;
} }
getline(&line, &size, f); getline(&line, &size, f);
pclose(f); result = pclose(f);
if (result < 0)
{
perror("pclose(3)");
goto failure;
}
else if (result)
{
fprintf(stderr, "command exited with status %d: %s\n", result, cmd);
goto failure;
}
return line; return line;
failure:
free(line);
return NULL;
} }
static int static int
exec_code(char *program, char *argv[]) exec_code(char *program, char *argv[])
@ -293,7 +310,8 @@ collect_sources(char *dir, bool head, char *ext)
while ((ent = readdir(handle))) while ((ent = readdir(handle)))
{ {
char *name = ent->d_name; char *name = ent->d_name;
if (*name == '.') continue;
if (!strcmp(name, ".") || !strcmp(name, "..")) continue;
if (strlen(name) > strlen(ext) && if (strlen(name) > strlen(ext) &&
!strcmp(name + strlen(name) - strlen(ext), ext)) !strcmp(name + strlen(name) - strlen(ext), ext))
@ -305,22 +323,29 @@ collect_sources(char *dir, bool head, char *ext)
free(na); free(na);
continue; continue;
} }
if (!strchr(name, '.') &&
strcmp(name, "out") &&
strcmp(name, "Makefile"))
{ {
str_array_t *sub;
char *d1 = string_cat(dir, "/"); char *d1 = string_cat(dir, "/");
char *d2 = string_cat(d1, name); char *d2 = string_cat(d1, name);
size_t i; size_t i;
struct stat sb;
sub = collect_sources(d2, false, ext); if (stat(d2, &sb))
{
fprintf(stderr, "stat(2) %s: %s\n", d2, strerror(errno));
free(d2);
free(d1);
}
else if (S_ISDIR(sb.st_mode))
{
str_array_t *sub = collect_sources(d2, false, ext);
for (i = 0; i < str_array_len(sub); i++) for (i = 0; i < str_array_len(sub); i++)
{ {
char *file = str_array_get(sub, i); char *file = str_array_get(sub, i);
str_array_add(ret, file); str_array_add(ret, file);
} }
str_array_free(sub); str_array_free(sub);
}
free(d2); free(d2);
free(d1); free(d1);
} }
@ -417,10 +442,13 @@ main_build(int argc, char *argv[])
int opt; int opt;
str_array_t *sources, *images, *utils, *aya; str_array_t *sources, *images, *utils, *aya;
if (strchr(repo, '\n')) if (repo)
{ {
*(strchr(repo, '\n')) = '\0'; char *lf = strchr(repo, '\n');
*lf = '\0';
} }
else
repo = strdup("N/A");
while ((opt = getopt(argc, argv, "sl")) != -1) while ((opt = getopt(argc, argv, "sl")) != -1)
{ {
@ -555,6 +583,7 @@ main_build(int argc, char *argv[])
fprintf(makefile, " -static"); fprintf(makefile, " -static");
} }
fprintf(makefile, " $<"); fprintf(makefile, " $<");
fprintf(makefile, " -I $(CYTO_INC)");
fprintf(makefile, " -L $(CYTO_LIB)"); fprintf(makefile, " -L $(CYTO_LIB)");
if (with_static) if (with_static)
{ {