From b976d797b09e9f9ff212da91c2c6b552596542bc Mon Sep 17 00:00:00 2001 From: Jakob Kaivo Date: Mon, 25 Apr 2022 20:27:40 -0400 Subject: use the current directory name for libraries if none is specified --- README.md | 3 ++- make.c | 27 +++++++++++++++++++++++++-- 2 files changed, 27 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index b9e9f4f..36c12ec 100644 --- a/README.md +++ b/README.md @@ -12,7 +12,8 @@ There are a few restrictions: - Only one target per source tree: a binary (identified by the source file containing main(), which will have the same basename without .c), or a - library (identified by the MAJE_LIB flag (see below). + library (identified by the MAJE_LIB flag (see below), or the current + directory name if MAJE_LIB is not specified). Usage ===== diff --git a/make.c b/make.c index 80c2eba..9a2e61e 100644 --- a/make.c +++ b/make.c @@ -1,11 +1,21 @@ #define _XOPEN_SOURCE 700 #include +#include #include #include #include +#include #include "maje.h" +#ifndef PATH_MAX +#ifdef _XOPEN_PATH_MAX +#define PATH_MAX _XOPEN_PATH_MAX +#else +#define PATH_MAX _POSIX_PATH_MAX +#endif +#endif + static void make_header(FILE *makefile, const char *target, struct majeflag *flags, int lib) { fprintf(makefile, ".POSIX:\n\n"); @@ -91,6 +101,16 @@ static void add_object(FILE *makefile, const struct majefile *src, const char *t free(fullobj); } +static char *cwd_basename(void) +{ + static char path[PATH_MAX]; + if (getcwd(path, sizeof(path)) == NULL) { + return NULL; + } + + return basename(path); +} + void make_makefile(const char *makepath, struct majefile *sources, const char *target) { FILE *makefile = fopen(makepath, "w"); @@ -119,8 +139,11 @@ void make_makefile(const char *makepath, struct majefile *sources, const char *t } if (lib == 1 && target == NULL) { - fprintf(stderr, "maje: couldn't determine binary or library name\n"); - exit(1); + target = cwd_basename(); + if (target == NULL) { + fprintf(stderr, "maje: couldn't determine binary or library name\n"); + exit(1); + } } make_header(makefile, target, flags, lib); -- cgit v1.2.1