home · contact · privacy
Enforce C11 via Makefile, explicate POSIX dependencies in source files.
[plomrogue] / Makefile
index 56a158c56c346e524046b30dbd3c89ca4ed2ded4..96c3aca017841681d83132c3a49771eb9c4707d2 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -1,26 +1,42 @@
-TARGET=roguelike
+CC=gcc
+CFLAGS=-std=c11 -pedantic-errors -Wall -Werror -Wextra -Wformat-security -g
+TARGET_SERVER=roguelike-server
+TARGET_CLIENT=roguelike-client
 SRCDIR=src
 BUILDDIR=build
-CC=gcc
-CFLAGS=-Wall -g
+SERVERDIR=server
+CLIENTDIR=client
+COMMONDIR=common
+
+# Build object file lists by building object paths from all source file paths.
+SOURCES_SERVER=$(shell find $(SRCDIR)/$(SERVERDIR)/ -type f -name \*.c)
+SOURCES_CLIENT=$(shell find $(SRCDIR)/$(CLIENTDIR)/ -type f -name \*.c)
+SOURCES_COMMON=$(shell find $(SRCDIR)/$(COMMONDIR)/ -type f -name \*.c)
+OBJECTS_SERVER=$(patsubst $(SRCDIR)/%,$(BUILDDIR)/%,$(SOURCES_SERVER:.c=.o))
+OBJECTS_CLIENT=$(patsubst $(SRCDIR)/%,$(BUILDDIR)/%,$(SOURCES_CLIENT:.c=.o))
+OBJECTS_COMMON=$(patsubst $(SRCDIR)/%,$(BUILDDIR)/%,$(SOURCES_COMMON:.c=.o))
+
+# "make" without further specifications builds both server and client.
+default : $(TARGET_SERVER) $(TARGET_CLIENT)
+
+# "make roguelike-server" builds only the server.
+$(TARGET_SERVER) : $(OBJECTS_SERVER) $(OBJECTS_COMMON)
+       $(CC) $(CFLAGS) -o $(TARGET_SERVER) $(OBJECTS_SERVER) $(OBJECTS_COMMON)
 
-# Use all .c files below SRCDIR for sources. Build object file paths by
-# replacing SRCDIR with BUILDDIR and .c endings with .o endings.
-SOURCES=$(shell find $(SRCDIR)/ -type f -name \*.c)
-OBJECTS=$(patsubst $(SRCDIR)/%,$(BUILDDIR)/%,$(SOURCES:.c=.o))
+# "make roguelike-client" builds only the ncurses client.
+$(TARGET_CLIENT) : $(OBJECTS_CLIENT) $(OBJECTS_COMMON)
+       $(CC) $(CFLAGS) -o $(TARGET_CLIENT) $(OBJECTS_CLIENT) $(OBJECTS_COMMON)\
+        -lncurses
 
-# Linking TARGET demands generation of all OBJECTS files. To build them,
-# rely on this rule: Each file of path BUILDDIR/[name].o is compiled
-# from a file of path SRCDIR/[name].c. Build BUILDDIR as needed.
-$(TARGET): $(OBJECTS)
-       $(CC) $(CFLAGS) -o roguelike $(OBJECTS) -lncurses
-$(BUILDDIR)/%.o: $(SRCDIR)/%.c
-       mkdir -p $(BUILDDIR); $(CC) $(CFLAGS) -c $< -o $@
+# Build respective object file to any source file. Create build dirs as needed.
+$(BUILDDIR)/%.o : $(SRCDIR)/%.c
+       mkdir -p $(@D)
+       $(CC) $(CFLAGS) -c $< -o $@
 
-# Use "clean" to delete build and target files. Tell make that "clean"
-# is a "phony" target, i.e. this is not about building a file.
-.PHONY: clean
-clean:
-       rm $(OBJECTS)
-       rmdir $(BUILDDIR)
-       rm $(TARGET)
+# "make clean" to try to delete all files that could possibly have been built.
+# Declare target "phony", i.e. this is not about building a file.
+.PHONY : clean
+clean :
+       rm -rf $(BUILDDIR)
+       rm -f $(TARGET_SERVER)
+       rm -f $(TARGET_CLIENT)