TP_13 exo1: Commit the basic exercice structure with a smart buildsystem
authorJérôme Benoit <jerome.benoit@piment-noir.org>
Thu, 9 Mar 2017 11:12:01 +0000 (12:12 +0100)
committerJérôme Benoit <jerome.benoit@piment-noir.org>
Thu, 9 Mar 2017 11:12:01 +0000 (12:12 +0100)
Signed-off-by: Jérôme Benoit <jerome.benoit@piment-noir.org>
TP_13/exo1/Makefile [new file with mode: 0644]
TP_13/exo1/lib/array.c [new file with mode: 0644]
TP_13/exo1/lib/array.h [new file with mode: 0644]
TP_13/exo1/lib/io.c [new file with mode: 0644]
TP_13/exo1/lib/io.h [new file with mode: 0644]
TP_13/exo1/lib/sort.c [new file with mode: 0644]
TP_13/exo1/lib/sort.h [new file with mode: 0644]
TP_13/exo1/src/exo1.c [new file with mode: 0644]

diff --git a/TP_13/exo1/Makefile b/TP_13/exo1/Makefile
new file mode 100644 (file)
index 0000000..8168c45
--- /dev/null
@@ -0,0 +1,134 @@
+# Sample Makefile to build simple project.
+#
+# This Makefile expect all source files (.c) to be at the same level, in the
+# current working directory.
+#
+# It will automatically generate dependencies, compile all files, and produce a
+# binary using the provided name.
+#
+# Set BINARY_NAME to the name of the binary file to build.
+# 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=exo1
+BINARY_PATH:=src
+LIBRARY_NAME=libexo1
+LIBRARY_PATH:=lib
+#LDLIBS=-L$(LIBRARY_PATH) -l$(BINARY_NAME)
+BUILD_TYPE=debug
+
+# ====================================
+# DO NOT CHANGE STUFF BEYOND THIS LINE
+# ====================================
+
+all: $(BINARY_NAME) $(BINARY_NAME).dynamic $(BINARY_NAME).static
+
+CC=gcc
+LD=gcc
+AR=ar
+
+WARN_FLAGS = -Wall -Wextra
+STD_FLAG = -std=c11
+
+ifeq ($(BUILD_TYPE),debug)
+BUILDDIR := .build/debug
+DEBUG_FLAG = -g
+STRIP_FLAG =
+OPTI_FLAG = -O0
+else
+BUILDDIR := .build/release
+DEBUG_FLAG =
+STRIP_FLAG = -s
+OPTI_FLAG = -O3
+endif
+
+#FIXME: Add a way to better control the library build flag
+CFLAGS := -fPIC $(CFLAGS) $(WARN_FLAGS) $(STD_FLAG) $(OPTI_FLAG) $(DEBUG_FLAG)
+LDFLAGS := $(LDFLAGS) $(STRIP_FLAG)
+LIBLDFLAGS := -shared $(LDFLAGS)
+STATICLIBLDFLAGS := -static $(LDFLAGS)
+
+OBJDIR := $(BUILDDIR)/objs
+$(shell mkdir -p $(OBJDIR))
+
+SRCS=$(wildcard $(BINARY_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)/$(notdir $*).Td
+POSTCOMPILE = mv -f $(DEPDIR)/$(notdir $*).Td $(DEPDIR)/$(notdir $*).d
+
+print: 
+       @echo $(basename $(notdir $(SRCS)))
+       @echo $(notdir $(SRCS))
+       @echo $(DEPDIR)/%d
+       @echo $(wildcard $(patsubst %,$(DEPDIR)/%.d,$(basename $(SRCS))))
+       @echo $(OBJS) $(LIBOBJS)
+       @echo $(SRCS) $(LIBSRCS)
+       @echo $(POSTCOMPILE)
+
+$(LIBRARY_PATH)/$(LIBRARY_NAME).a: $(LIBOBJS)
+       @echo "[AR StO] $@"
+       @$(AR) rcs $@ $^
+
+$(LIBRARY_PATH)/$(LIBRARY_NAME).so: $(LIBOBJS)
+       @echo "[LD ShO] $@"
+       @$(LD) $(CFLAGS) $(LIBLDFLAGS) $^ $(LDLIBS) -o $@
+
+#$(BINARY_NAME): $(OBJS) $(LIBOBJS)
+#      @echo "[LD ] $@"
+#      $(LD) $(CFLAGS) $(LDFLAGS) $^ $(LDLIBS) -o $@
+
+$(BINARY_NAME): $(OBJS) $(LIBRARY_PATH)/$(LIBRARY_NAME).a
+       @echo "[LD ] $@"
+       @$(LD) $(CFLAGS) $(LDFLAGS) $^ $(LOCALLDLIBS) $(LDLIBS) -o $@
+
+LOCALLDLIBS=-L$(LIBRARY_PATH) -l$(BINARY_NAME)
+$(BINARY_NAME).static: $(OBJS) $(LIBRARY_PATH)/$(LIBRARY_NAME).a
+       @echo "[LD ] $@"
+       @$(LD) $(CFLAGS) $(STATICLIBLDFLAGS) $^ $(LOCALLDLIBS) $(LDLIBS) -o $@
+
+#$(BINARY_NAME).staticlocal: $(OBJS) $(LIBRARY_PATH)/$(LIBRARY_NAME).a
+#      @echo "[LD ] $@"
+#      @$(LD) $(CFLAGS) $(LDFLAGS) $^ $(LOCALLDLIBS) $(LDLIBS) -o $@
+
+$(BINARY_NAME).dynamic: $(OBJS) $(LIBRARY_PATH)/$(LIBRARY_NAME).so
+       @echo "[LD ] $@"
+       @$(LD) $(CFLAGS) $(LDFLAGS) $^ $(LOCALLDLIBS) $(LDLIBS) -o $@
+
+$(OBJS): $(SRCS) $(DEPDIR)/%.d
+       @echo "[C  ] $*"
+       @$(CC) $(DEPFLAGS) $(CFLAGS) -c $< -o $@
+       @$(POSTCOMPILE)
+
+include $(wildcard $(patsubst %,$(DEPDIR)/%.d,$(basename $(SRCS))))
+
+$(LIBOBJS): $(LIBSRCS) $(DEPDIR)/%.d
+       @echo "[C  ] $*"
+       @$(CC) $(DEPFLAGS) $(CFLAGS) -c $< -o $@
+       @$(POSTCOMPILE)
+
+include $(wildcard $(patsubst %,$(DEPDIR)/%.d,$(basename $(LIBSRCS))))
+
+$(DEPDIR)/%.d: ;
+
+.PRECIOUS: $(DEPDIR)/%.d
+
+clean:
+       @echo "[CLN]"
+       -@rm -r $(BUILDDIR)
+       -@rm $(BINARY_NAME)
+       -@rm $(BINARY_NAME).static
+       -@rm $(BINARY_NAME).dynamic
+       -@rm $(LIBRARY_PATH)/$(LIBRARY_NAME).a
+       -@rm $(LIBRARY_PATH)/$(LIBRARY_NAME).so
+
+disassemble: $(BINARY_NAME)
+       objdump -d $< | less
+
+symbols: $(BINARY_NAME)
+       objdump -t $< | sort | less
diff --git a/TP_13/exo1/lib/array.c b/TP_13/exo1/lib/array.c
new file mode 100644 (file)
index 0000000..e69de29
diff --git a/TP_13/exo1/lib/array.h b/TP_13/exo1/lib/array.h
new file mode 100644 (file)
index 0000000..e69de29
diff --git a/TP_13/exo1/lib/io.c b/TP_13/exo1/lib/io.c
new file mode 100644 (file)
index 0000000..e69de29
diff --git a/TP_13/exo1/lib/io.h b/TP_13/exo1/lib/io.h
new file mode 100644 (file)
index 0000000..e69de29
diff --git a/TP_13/exo1/lib/sort.c b/TP_13/exo1/lib/sort.c
new file mode 100644 (file)
index 0000000..e69de29
diff --git a/TP_13/exo1/lib/sort.h b/TP_13/exo1/lib/sort.h
new file mode 100644 (file)
index 0000000..e69de29
diff --git a/TP_13/exo1/src/exo1.c b/TP_13/exo1/src/exo1.c
new file mode 100644 (file)
index 0000000..06f5f8d
--- /dev/null
@@ -0,0 +1,7 @@
+#include <stdio.h>
+
+int main() {
+    printf("Hello world\n");
+
+    return 0;
+}