TP 11 exo2: Reorganize the directories structure to make use of the
authorJérôme Benoit <jerome.benoit@piment-noir.org>
Thu, 9 Mar 2017 21:33:56 +0000 (22:33 +0100)
committerJérôme Benoit <jerome.benoit@piment-noir.org>
Thu, 9 Mar 2017 21:33:56 +0000 (22:33 +0100)
new and shiny Makefile cleaned up.

Signed-off-by: Jérôme Benoit <jerome.benoit@piment-noir.org>
TP_11/exo2/Makefile
TP_11/exo2/lib/clist.c [moved from TP_11/exo2/clist.c with 100% similarity]
TP_11/exo2/lib/clist.h [moved from TP_11/exo2/clist.h with 100% similarity]
TP_11/exo2/lib/ui.c [moved from TP_11/exo2/ui.c with 100% similarity]
TP_11/exo2/lib/ui.h [moved from TP_11/exo2/ui.h with 100% similarity]
TP_11/exo2/src/main.c [moved from TP_11/exo2/exo2.c with 100% similarity]

index 6f840e6e8bcda47f43c28d7a98d3c071b9197695..d0ec4dd1871f2c52dd228f03e01abd33c43b70dc 100644 (file)
@@ -1,26 +1,34 @@
 # Sample Makefile to build simple project.
 #
 # This Makefile expect all source files (.c) to be at the same level, in the
-# current working directory.
+# $(SRC_PATH) directory.
+#
+# This Makefile expect all embedded library files (.c) to be at the same level, in the
+# $(LIBRARY_PATH) directory.
 #
 # It will automatically generate dependencies, compile all files, and produce a
-# binary using the provided name.
+# binary using the provided name linked against the library if necessary.
 #
 # Set BINARY_NAME to the name of the binary file to build.
-# Set BUILD_TYPE to either debug or release
+# Set LIBRARY_NAME to the name of the library file to build.
+# The default path for the library code and object is lib.
+# By default the linker will look for $(BINARY_NAME) library name.
+# Set BUILD_TYPE to either debug or release.
 #
 # Automatic dependencies code from:
 # http://make.mad-scientist.net/papers/advanced-auto-dependency-generation/#tldr
 BINARY_NAME=exo2
-LIBOBJS=clist.o ui.o # object to put in the library
-LIBRARY_NAME=libtpC
+SRC_PATH:=src
+LIBRARY_NAME=libexo2
+LIBRARY_PATH:=lib
 BUILD_TYPE=debug
+#BUILD_TYPE=release
 
 # ====================================
 # DO NOT CHANGE STUFF BEYOND THIS LINE
 # ====================================
 
-all: $(BINARY_NAME) $(BINARY_NAME).static $(BINARY_NAME).staticlocal $(BINARY_NAME).dynamic
+all: $(BINARY_NAME) $(BINARY_NAME).dynamic $(BINARY_NAME).static
 
 CC=gcc
 LD=gcc
@@ -41,69 +49,80 @@ STRIP_FLAG = -s
 OPTI_FLAG = -O3
 endif
 
-#FIXME: Add a way to better control the library build
-CFLAGS := -fPIC $(CFLAGS) $(WARN_FLAGS) $(STD_FLAG) $(OPTI_FLAG) $(DEBUG_FLAG)
+# Putting header files in the source directory is not the purpose of this INCLUDES variable
+INCLUDES := $(INCLUDES) -I$(SRC_PATH) -I$(LIBRARY_PATH)
+CFLAGS := $(CFLAGS) $(WARN_FLAGS) $(STD_FLAG) $(OPTI_FLAG) $(DEBUG_FLAG) $(INCLUDES)
+LIBCFLAGS := -fPIC $(CFLAGS)
 LDFLAGS := $(LDFLAGS) $(STRIP_FLAG)
 LIBLDFLAGS := -shared $(LDFLAGS)
-STATICLDFLAGS := -static $(LDFLAGS)
+STATICLIBLDFLAGS := -static $(LDFLAGS)
+LDLIBS := $(LDLIBS) -L$(LIBRARY_PATH) -l$(BINARY_NAME)
 
 OBJDIR := $(BUILDDIR)/objs
 $(shell mkdir -p $(OBJDIR))
 
-SRCS=$(wildcard *.c)
-OBJS=$(patsubst %.c,$(OBJDIR)/%.o,$(SRCS))
-OBJSLIB=$(addprefix $(OBJDIR)/,$(LIBOBJS))
+SRCS=$(wildcard $(SRC_PATH)/*.c)
+LIBSRCS=$(wildcard $(LIBRARY_PATH)/*.c)
+OBJS=$(patsubst %.c,$(OBJDIR)/%.o,$(notdir $(SRCS)))
+LIBOBJS=$(patsubst %.c,$(OBJDIR)/%.o,$(notdir $(LIBSRCS)))
 
 DEPDIR := $(BUILDDIR)/deps
 $(shell mkdir -p $(DEPDIR))
-DEPFLAGS = -MT $@ -MMD -MP -MF $(DEPDIR)/$*.Td
-POSTCOMPILE = mv -f $(DEPDIR)/$*.Td $(DEPDIR)/$*.d
+DEPFLAGS = -MT $@ -MMD -MP -MF $(DEPDIR)/$(notdir $*).Td
+POSTCOMPILE = mv -f $(DEPDIR)/$(notdir $*).Td $(DEPDIR)/$(notdir $*).d
 
-$(LIBRARY_NAME).a: $(OBJSLIB)
+$(LIBRARY_PATH)/$(LIBRARY_NAME).a: $(LIBOBJS)
        @echo "[AR StO] $@"
        @$(AR) rcs $@ $^
 
-$(LIBRARY_NAME).so: $(OBJSLIB)
+$(LIBRARY_PATH)/$(LIBRARY_NAME).so: $(LIBOBJS)
        @echo "[LD ShO] $@"
-       @$(LD) $(CFLAGS) $(LIBLDFLAGS) $^ -o $@
+       @$(LD) $(LIBCFLAGS) $(LIBLDFLAGS) $^ -o $@
 
-$(BINARY_NAME): $(OBJS)
-       @echo "[LD ] $@"
-       @$(LD) $(CFLAGS) $(LDFLAGS) $^ $(LDLIBS) -o $@
+#$(BINARY_NAME): $(OBJS) $(LIBOBJS)
+#      @echo "[LD ] $@"
+#      @$(LD) $(CFLAGS) $(LDFLAGS) $^ $(LDLIBS) -o $@
 
-LOCALLDLIBS=-L. -ltpC
-$(BINARY_NAME).static: $(OBJDIR)/$(BINARY_NAME).o $(LIBRARY_NAME).a
+$(BINARY_NAME): $(OBJS) $(LIBRARY_PATH)/$(LIBRARY_NAME).a
        @echo "[LD ] $@"
-       @$(LD) $(CFLAGS) $(STATICLDFLAGS) $^ $(LOCALLDLIBS) $(LDLIBS) -o $@
+       @$(LD) $(CFLAGS) $(LDFLAGS) $^ $(LDLIBS) -o $@
 
-$(BINARY_NAME).staticlocal: $(OBJDIR)/$(BINARY_NAME).o $(LIBRARY_NAME).a
+$(BINARY_NAME).static: $(OBJS) $(LIBRARY_PATH)/$(LIBRARY_NAME).a
        @echo "[LD ] $@"
-       @$(LD) $(CFLAGS) $(LDFLAGS) $^ $(LOCALLDLIBS) $(LDLIBS) -o $@
+       @$(LD) $(CFLAGS) $(STATICLIBLDFLAGS) $^ $(LDLIBS) -o $@
 
-$(BINARY_NAME).dynamic: $(OBJDIR)/$(BINARY_NAME).o $(LIBRARY_NAME).so
+$(BINARY_NAME).dynamic: $(OBJS) $(LIBRARY_PATH)/$(LIBRARY_NAME).so
        @echo "[LD ] $@"
-       @$(LD) $(CFLAGS) $(LDFLAGS) $^ $(LOCALLDLIBS) $(LDLIBS) -o $@
+       @$(LD) $(CFLAGS) $(LDFLAGS) $^ $(LDLIBS) -o $@
 
-$(OBJDIR)/%.o: %.c $(DEPDIR)/%.d
-       @echo "[C  ] $*"
+$(OBJDIR)/%.o: $(SRC_PATH)/%.c $(DEPDIR)/%.d
+       @echo "[C  ] $(notdir $*)"
        @$(CC) $(DEPFLAGS) $(CFLAGS) -c $< -o $@
        @$(POSTCOMPILE)
 
+include $(wildcard $(patsubst %,$(DEPDIR)/%.d,$(basename $(SRCS))))
+
+$(OBJDIR)/%.o: $(LIBRARY_PATH)/%.c $(DEPDIR)/%.d
+       @echo "[C  ] $(notdir $*)"
+       @$(CC) $(DEPFLAGS) $(LIBCFLAGS) -c $< -o $@
+       @$(POSTCOMPILE)
+
+include $(wildcard $(patsubst %,$(DEPDIR)/%.d,$(basename $(LIBSRCS))))
+
 $(DEPDIR)/%.d: ;
 
 .PRECIOUS: $(DEPDIR)/%.d
 
-include $(wildcard $(patsubst %,$(DEPDIR)/%.d,$(basename $(SRCS))))
+#FIXME: add an install target
 
 clean:
        @echo "[CLN]"
        -@rm -r $(BUILDDIR)
        -@rm $(BINARY_NAME)
        -@rm $(BINARY_NAME).static
-       -@rm $(BINARY_NAME).staticlocal
        -@rm $(BINARY_NAME).dynamic
-       -@rm $(LIBRARY_NAME).a
-       -@rm $(LIBRARY_NAME).so
+       -@rm $(LIBRARY_PATH)/$(LIBRARY_NAME).a
+       -@rm $(LIBRARY_PATH)/$(LIBRARY_NAME).so
 
 disassemble: $(BINARY_NAME)
        objdump -d $< | less
similarity index 100%
rename from TP_11/exo2/clist.c
rename to TP_11/exo2/lib/clist.c
similarity index 100%
rename from TP_11/exo2/clist.h
rename to TP_11/exo2/lib/clist.h
similarity index 100%
rename from TP_11/exo2/ui.c
rename to TP_11/exo2/lib/ui.c
similarity index 100%
rename from TP_11/exo2/ui.h
rename to TP_11/exo2/lib/ui.h
similarity index 100%
rename from TP_11/exo2/exo2.c
rename to TP_11/exo2/src/main.c