75b664c5756a5622e8caaf9986df943860565535 — Jakob Kaivo 2 years ago 1a21f9e
make out-of-tree building more robust by removing the command line option for source directory
4 files changed, 54 insertions(+), 37 deletions(-)

M Makefile
M maje.c
M make.c
M Makefile => Makefile +27 -25
@@ 11,41 11,43 @@ LD=$(CC)
CFLAGS=-Wall -Wextra -Wpedantic -Werror -g

all: maje

	rm -f maje *.o

maje: includes.o
includes.o: maje.h
includes.o: includes.c
	$(CC) $(CFLAGS) -c includes.c
maje: $(OBJDIR)/includes.o
$(OBJDIR)/includes.o: $(SRCDIR)/maje.h
$(OBJDIR)/includes.o: $(SRCDIR)/includes.c
	$(CC) $(CFLAGS) -o $@ -c $(SRCDIR)/includes.c

maje: list.o
list.o: maje.h
list.o: list.c
	$(CC) $(CFLAGS) -c list.c
maje: $(OBJDIR)/list.o
$(OBJDIR)/list.o: $(SRCDIR)/maje.h
$(OBJDIR)/list.o: $(SRCDIR)/list.c
	$(CC) $(CFLAGS) -o $@ -c $(SRCDIR)/list.c

maje: main.o
main.o: maje.h
main.o: main.c
	$(CC) $(CFLAGS) -c main.c
maje: $(OBJDIR)/main.o
$(OBJDIR)/main.o: $(SRCDIR)/maje.h
$(OBJDIR)/main.o: $(SRCDIR)/main.c
	$(CC) $(CFLAGS) -o $@ -c $(SRCDIR)/main.c

maje: maje.o
maje.o: maje.h
maje.o: maje.c
	$(CC) $(CFLAGS) -c maje.c
maje: $(OBJDIR)/maje.o
$(OBJDIR)/maje.o: $(SRCDIR)/maje.h
$(OBJDIR)/maje.o: $(SRCDIR)/maje.c
	$(CC) $(CFLAGS) -o $@ -c $(SRCDIR)/maje.c

maje: make.o
make.o: maje.h
make.o: make.c
	$(CC) $(CFLAGS) -c make.c
maje: $(OBJDIR)/make.o
$(OBJDIR)/make.o: $(SRCDIR)/maje.h
$(OBJDIR)/make.o: $(SRCDIR)/make.c
	$(CC) $(CFLAGS) -o $@ -c $(SRCDIR)/make.c

maje: sources.o
sources.o: maje.h
sources.o: sources.c
	$(CC) $(CFLAGS) -c sources.c
maje: $(OBJDIR)/sources.o
$(OBJDIR)/sources.o: $(SRCDIR)/maje.h
$(OBJDIR)/sources.o: $(SRCDIR)/sources.c
	$(CC) $(CFLAGS) -o $@ -c $(SRCDIR)/sources.c

	$(LD) $(LDFLAGS) -o $@ *.o $(LDLIBS)
	$(LD) $(LDFLAGS) -o $@ $(OBJDIR)/*.o $(LDLIBS)

M README.md => README.md +17 -5
@@ 23,12 23,24 @@ Usage

Run Maje with:

    maje [-n] [dir]
    maje [-n]

By default, Maje will execute `make` when it is done creating the Makefile. Use
the `-n` option to prevent this.

By default, Maje generates a Makefile for sources in the current directory.
You can specify a directory as a command line parameter to generate a Makefile
to build binaries in a separate directory from the source. Maje will always
build Makefiles and binaries in the current directory.
Maje creates Makefiles with prefixes $(SRCDIR) and $(OBJDIR) for the source
directory and object directory, respectively. By default these are set to
`.` (the current directory). An out-of-tree build can be accomplished by 
first generating the Makefile in the source directory and manually specifying
the object directory as an option to `make`:

    maje -n
    mkdir obj
    make OBDIR=obj


    maje -n
    mkdir ../build
    cd ../build
    make -f ${OLDPWD}/Makefile SRCDIR=${OLDPWD}

M maje.c => maje.c +2 -2
@@ 8,7 8,7 @@

static void usage(char *progname)
	printf("usage: %s [-n] [dir]\n", progname);
	printf("usage: %s [-n]\n", progname);

int main(int argc, char *argv[])

@@ 29,7 29,7 @@ int main(int argc, char *argv[])

	if (optind < argc - 1) {
	if (optind < argc) {
		fprintf(stderr, "maje: extra operands\n");
		return 1;

M make.c => make.c +8 -5
@@ 20,6 20,8 @@ static void make_header(FILE *makefile, const char *target)
	fprintf(makefile, "CFLAGS=-Wall -Wextra -Wpedantic -Werror -g\n");
	fprintf(makefile, "LDFLAGS=\n");
	fprintf(makefile, "LDLIBS=\n");
	fprintf(makefile, "SRCDIR=.\n");
	fprintf(makefile, "OBJDIR=.\n");
	fprintf(makefile, "\n");

	fprintf(makefile, "all: %s\n\n", target);

@@ 34,12 36,13 @@ static void add_object(FILE *makefile, const struct majefile *src, const char *t
	char *obj = basename(fullobj);
	obj[strlen(obj) - 1] = 'o';

	fprintf(makefile, "%s: %s\n", target, obj);
	fprintf(makefile, "%s: $(OBJDIR)/%s\n", target, obj);
	for (struct majefile *inc = find_includes(src); inc != NULL; inc = inc->next) {
		fprintf(makefile, "%s: %s\n", obj, inc->path);
		fprintf(makefile, "$(OBJDIR)/%s: $(SRCDIR)/%s\n",
			obj, inc->path);
	fprintf(makefile, "%s: %s\n", obj, src->path);
	fprintf(makefile, "\t$(CC) $(CFLAGS) -c %s\n\n", src->path);
	fprintf(makefile, "$(OBJDIR)/%s: $(SRCDIR)/%s\n", obj, src->path);
	fprintf(makefile, "\t$(CC) $(CFLAGS) -o $@ -c $(SRCDIR)/%s\n\n", src->path);


@@ 58,7 61,7 @@ void make_makefile(const char *makepath, struct majefile *sources, const char *t

	fprintf(makefile, "%s:\n", target);
	fprintf(makefile, "\t$(LD) $(LDFLAGS) -o $@ *.o $(LDLIBS)\n");
	fprintf(makefile, "\t$(LD) $(LDFLAGS) -o $@ $(OBJDIR)/*.o $(LDLIBS)\n");