summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--README.md12
-rw-r--r--list.c2
-rw-r--r--maje.c11
-rw-r--r--maje.h2
-rw-r--r--make.c68
5 files changed, 70 insertions, 25 deletions
diff --git a/README.md b/README.md
index 4db8cff..b9e9f4f 100644
--- a/README.md
+++ b/README.md
@@ -10,13 +10,9 @@ There are a few restrictions:
- Source files must be named ending with .c.
-- Only executables are supported, not libraries (there must be a main()).
-
-- Only one executable per source tree is supported (exactly one main()).
-
-- Maje expects the resulting binary to have the same base name as the source
-file containing main() (e.g. if main() is in maje.c, the binary will be called
-maje).
+- 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).
Usage
=====
@@ -56,6 +52,8 @@ up to the first white space character as indicated:
MAJE_CFLAG Added to the CFLAGS macro
MAJE_LDFLAG Added to the LDFLAGS macro
MAJE_LDLIB Added to the LDLIBS macro
+ MAJE_LIB Used as the library basename (e.g. MAJE_LIB=foo
+ will create libfoo.a and libfoo.so)
Maje will find these strings anywhere in your source file, so long as they
are followed by an equal sign, one or more characters, and a whitespace
diff --git a/list.c b/list.c
index 2fa06ab..46d36a5 100644
--- a/list.c
+++ b/list.c
@@ -49,6 +49,8 @@ struct majeflag *insert_flag(struct majeflag *list, char *flag)
type = MAJE_LDFLAG;
} else if (!strcmp(flag, "MAJE_LDLIB")) {
type = MAJE_LDLIB;
+ } else if (!strcmp(flag, "MAJE_LIB")) {
+ type = MAJE_LIB;
} else {
fprintf(stderr, "maje: unkonwn directive '%s'\n", flag);
return list;
diff --git a/maje.c b/maje.c
index c67af52..a449d17 100644
--- a/maje.c
+++ b/maje.c
@@ -42,13 +42,12 @@ int main(int argc, char *argv[])
struct majefile *sources = find_source_files(srcdir);
char *mainname = find_main(sources);
- if (!mainname) {
- printf("libraries not yet supported\n");
- return 0;
+ char *target = NULL;
+ if (mainname) {
+ target = strdup(mainname);
+ target[strlen(target) - 2] = '\0';
+ target = basename(target);
}
- char *target = strdup(mainname);
- target[strlen(target) - 2] = '\0';
- target = basename(target);
FILE *makefile = fopen("Makefile", "w");
if (makefile == NULL) {
diff --git a/maje.h b/maje.h
index ae62e0e..920efa5 100644
--- a/maje.h
+++ b/maje.h
@@ -13,7 +13,7 @@ struct majefile {
struct majeflag {
struct majeflag *next;
struct majeflag *prev;
- enum { MAJE_CFLAG, MAJE_LDFLAG, MAJE_LDLIB } type;
+ enum { MAJE_CFLAG, MAJE_LDFLAG, MAJE_LDLIB, MAJE_LIB } type;
char flag[];
};
diff --git a/make.c b/make.c
index 13bc7eb..80c2eba 100644
--- a/make.c
+++ b/make.c
@@ -6,7 +6,7 @@
#include "maje.h"
-static void make_header(FILE *makefile, const char *target, struct majeflag *flags)
+static void make_header(FILE *makefile, const char *target, struct majeflag *flags, int lib)
{
fprintf(makefile, ".POSIX:\n\n");
fprintf(makefile, "# This Makefile was generated by maje\n");
@@ -43,26 +43,43 @@ static void make_header(FILE *makefile, const char *target, struct majeflag *fla
fprintf(makefile, "SRCDIR=.\n");
fprintf(makefile, "OBJDIR=.\n");
fprintf(makefile, "BINDIR=$(OBJDIR)\n");
+ fprintf(makefile, "LIBDIR=$(OBJDIR)\n");
fprintf(makefile, "DESTDIR=/usr/local\n");
fprintf(makefile, "\n");
- fprintf(makefile, "all: $(BINDIR)/%s\n\n", target);
+ fprintf(makefile, "all: ");
+ if (lib) {
+ fprintf(makefile, "$(LIBDIR)/lib%s.a $(LIBDIR)/lib%s.so", target, target);
+ } else {
+ fprintf(makefile, "$(BINDIR)/%s", target);
+ }
+ fprintf(makefile, "\n\n");
fprintf(makefile, "clean:\n");
fprintf(makefile, "\trm -f $(BINDIR)/%s $(OBJDIR)/*.o\n\n", target);
fprintf(makefile, "install: $(BINDIR)/%s\n", target);
- fprintf(makefile, "\tmkdir -p $(DESTDIR)/bin\n");
- fprintf(makefile, "\tcp $(BINDIR)/%s $(DESTDIR)/bin\n\n", target);
+ if (lib) {
+ fprintf(makefile, "\tmkdir -p $(DESTDIR)/lib\n");
+ fprintf(makefile, "\tcp $(LIBDIR)/lib%s.{a,so} $(DESTDIR)/lib\n\n", target);
+ } else {
+ fprintf(makefile, "\tmkdir -p $(DESTDIR)/bin\n");
+ fprintf(makefile, "\tcp $(BINDIR)/%s $(DESTDIR)/bin\n\n", target);
+ }
}
-static void add_object(FILE *makefile, const struct majefile *src, const char *target)
+static void add_object(FILE *makefile, const struct majefile *src, const char *target, int lib)
{
char *fullobj = strdup(src->path);
char *obj = basename(fullobj);
obj[strlen(obj) - 1] = 'o';
- fprintf(makefile, "$(BINDIR)/%s: $(OBJDIR)/%s\n", target, obj);
+ if (lib) {
+ fprintf(makefile, "$(LIBDIR)/lib%s.a $(LIBDIR)/lib%s.so: $(OBJDIR)/%s\n", target, target, obj);
+ } else {
+ fprintf(makefile, "$(BINDIR)/%s: $(OBJDIR)/%s\n", target, obj);
+ }
+
for (struct majefile *inc = find_includes(src); inc != NULL; inc = inc->next) {
fprintf(makefile, "$(OBJDIR)/%s: $(SRCDIR)/%s\n",
obj, inc->path);
@@ -87,14 +104,43 @@ void make_makefile(const char *makepath, struct majefile *sources, const char *t
flags = add_flags(src, flags);
}
- make_header(makefile, target, flags);
+ int lib = 0;
+ if (target == NULL) {
+ lib = 1;
+ for (struct majeflag *flag = flags; flag != NULL; flag = flag->next) {
+ if (flag->type == MAJE_LIB) {
+ if (target) {
+ fprintf(stderr, "maje: only one MAJE_LIB is supported\n");
+ exit(1);
+ }
+ target = flag->flag;
+ }
+ }
+ }
+
+ if (lib == 1 && target == NULL) {
+ fprintf(stderr, "maje: couldn't determine binary or library name\n");
+ exit(1);
+ }
+
+ make_header(makefile, target, flags, lib);
for (struct majefile *src = sources; src != NULL; src = src->next) {
- add_object(makefile, src, target);
+ add_object(makefile, src, target, lib);
}
- fprintf(makefile, "$(BINDIR)/%s:\n", target);
- fprintf(makefile, "\t@mkdir -p $(@D)\n");
- fprintf(makefile, "\t$(LD) $(LDFLAGS) -o $@ $(OBJDIR)/*.o $(LDLIBS)\n");
+ if (lib) {
+ fprintf(makefile, "$(LIBDIR)/lib%s.a:\n", target);
+ fprintf(makefile, "\t@mkdir -p $(@D)\n");
+ fprintf(makefile, "\t$(AR) $(ARFLAGS) $@ $(OBJDIR)/*.o\n\n");
+
+ fprintf(makefile, "$(LIBDIR)/lib%s.so:\n", target);
+ fprintf(makefile, "\t@mkdir -p $(@D)\n");
+ fprintf(makefile, "\t$(LD) -shared $(LDFLAGS) -o $@ $(OBJDIR)/*.o $(LDLIBS)\n\n");
+ } else {
+ fprintf(makefile, "$(BINDIR)/%s:\n", target);
+ fprintf(makefile, "\t@mkdir -p $(@D)\n");
+ fprintf(makefile, "\t$(LD) $(LDFLAGS) -o $@ $(OBJDIR)/*.o $(LDLIBS)\n");
+ }
fclose(makefile);
}