From b70b3ff9b1679bb1e0a215b7acd9b6d55497a46b Mon Sep 17 00:00:00 2001 From: norly Date: Thu, 20 Jun 2013 01:56:24 +0100 Subject: [PATCH] Separate library code, build .a/.so --- Makefile | 38 ++++++++++++++++++--------- src/{ => libelfu}/elfops/check.c | 0 src/{ => libelfu}/modelops/check.c | 0 src/{ => libelfu}/modelops/clone.c | 0 src/{ => libelfu}/modelops/detour.c | 0 src/{ => libelfu}/modelops/dump.c | 0 src/{ => libelfu}/modelops/fromFile.c | 0 src/{ => libelfu}/modelops/layout.c | 0 src/{ => libelfu}/modelops/phdr.c | 0 src/{ => libelfu}/modelops/reladd.c | 0 src/{ => libelfu}/modelops/relocate.c | 0 src/{ => libelfu}/modelops/section.c | 0 src/{ => libelfu}/modelops/symtab.c | 0 src/{ => libelfu}/modelops/toFile.c | 0 14 files changed, 25 insertions(+), 13 deletions(-) rename src/{ => libelfu}/elfops/check.c (100%) rename src/{ => libelfu}/modelops/check.c (100%) rename src/{ => libelfu}/modelops/clone.c (100%) rename src/{ => libelfu}/modelops/detour.c (100%) rename src/{ => libelfu}/modelops/dump.c (100%) rename src/{ => libelfu}/modelops/fromFile.c (100%) rename src/{ => libelfu}/modelops/layout.c (100%) rename src/{ => libelfu}/modelops/phdr.c (100%) rename src/{ => libelfu}/modelops/reladd.c (100%) rename src/{ => libelfu}/modelops/relocate.c (100%) rename src/{ => libelfu}/modelops/section.c (100%) rename src/{ => libelfu}/modelops/symtab.c (100%) rename src/{ => libelfu}/modelops/toFile.c (100%) diff --git a/Makefile b/Makefile index c93b2ab..e063a29 100644 --- a/Makefile +++ b/Makefile @@ -1,4 +1,5 @@ -PROJ := elfucli +LIBNAME := elfu +EXENAME := elfucli LIBRARIES := libelf @@ -6,21 +7,31 @@ BUILDDIR := build INCLUDEDIR := include SRCDIR := src -EXE := $(BUILDDIR)/$(PROJ) +EXE := $(BUILDDIR)/$(EXENAME) +STATICLIB := $(BUILDDIR)/lib$(LIBNAME).a + +SHARED_VERMAJ := 0 +SHARED_VERMIN := 0 +SHARED_VERREV := 0 +SHAREDLIB := $(BUILDDIR)/lib$(LIBNAME).so.$(SHARED_VERMAJ).$(SHARED_VERMIN).$(SHARED_VERREV) + HEADERS := $(shell find $(INCLUDEDIR)/ -iname "*.h") HEADERS += $(shell find $(SRCDIR)/ -iname "*.h") -SOURCES := $(shell find $(SRCDIR)/ -iname "*.c") -OBJS := $(patsubst %.c, $(BUILDDIR)/%.o, $(SOURCES)) +ALLSRCS := $(shell find $(SRCDIR)/ -iname "*.c") +LIBSRCS := $(filter $(SRCDIR)/lib$(LIBNAME)/%.c, $(ALLSRCS)) +EXESRCS := $(filter-out $(SRCDIR)/lib$(LIBNAME)/%.c, $(ALLSRCS)) +LIBOBJS := $(patsubst %.c, $(BUILDDIR)/%.o, $(LIBSRCS)) +EXEOBJS := $(patsubst %.c, $(BUILDDIR)/%.o, $(EXESRCS)) INCLUDES := $(patsubst %, -I%, $(INCLUDEDIR) $(SRCDIR)) $(shell pkg-config --cflags-only-I $(LIBRARIES)) -CFLAGS := -g -Wall -pedantic -Wno-variadic-macros $(shell pkg-config --cflags-only-other $(LIBRARIES)) +CFLAGS := -g -Wall -pedantic -Wno-variadic-macros -fPIC $(shell pkg-config --cflags-only-other $(LIBRARIES)) LDFLAGS := $(shell pkg-config --libs $(LIBRARIES)) -.PHONY: default -default: $(EXE) +.PHONY: all +all: $(STATICLIB) $(SHAREDLIB) $(EXE) .PHONY: check @@ -30,13 +41,17 @@ check: $(EXE) .PHONY: debug debug: $(EXE) - gdb $(EXE) $(shell ps -e | sed "s/^ *\([0-9]\+\) .*$(PROJ).*$$/\1/g;te;d;:e") + gdb $(EXE) $(shell ps -e | sed "s/^ *\([0-9]\+\) .*$(EXENAME).*$$/\1/g;te;d;:e") -$(EXE): $(OBJS) - @if [ ! -d $(BUILDDIR) ] ; then echo "Error: Build dir '$(BUILDDIR)' does not exist." ; false ; fi +$(EXE): $(EXEOBJS) $(STATICLIB) gcc -o $@ $^ $(LDFLAGS) +$(SHAREDLIB): $(LIBOBJS) + gcc -shared -Wl,-soname,lib$(LIBNAME).so.$(SHARED_VERMAJ) -o $@ $^ $(LDFLAGS) + +$(STATICLIB): $(LIBOBJS) + ar rcs $@ $^ $(BUILDDIR)/$(SRCDIR)/%.o: $(SRCDIR)/%.c $(HEADERS) @if [ ! -d $(dir $@) ] ; then mkdir -p $(dir $@) ; fi @@ -45,9 +60,6 @@ $(BUILDDIR)/$(SRCDIR)/%.o: $(SRCDIR)/%.c $(HEADERS) .PHONY: clean clean: - rm -f $(STATICLIB) - rm -f $(OBJS) - rm -f $(TESTEXES) rm -rf $(BUILDDIR)/ make -C tests clean diff --git a/src/elfops/check.c b/src/libelfu/elfops/check.c similarity index 100% rename from src/elfops/check.c rename to src/libelfu/elfops/check.c diff --git a/src/modelops/check.c b/src/libelfu/modelops/check.c similarity index 100% rename from src/modelops/check.c rename to src/libelfu/modelops/check.c diff --git a/src/modelops/clone.c b/src/libelfu/modelops/clone.c similarity index 100% rename from src/modelops/clone.c rename to src/libelfu/modelops/clone.c diff --git a/src/modelops/detour.c b/src/libelfu/modelops/detour.c similarity index 100% rename from src/modelops/detour.c rename to src/libelfu/modelops/detour.c diff --git a/src/modelops/dump.c b/src/libelfu/modelops/dump.c similarity index 100% rename from src/modelops/dump.c rename to src/libelfu/modelops/dump.c diff --git a/src/modelops/fromFile.c b/src/libelfu/modelops/fromFile.c similarity index 100% rename from src/modelops/fromFile.c rename to src/libelfu/modelops/fromFile.c diff --git a/src/modelops/layout.c b/src/libelfu/modelops/layout.c similarity index 100% rename from src/modelops/layout.c rename to src/libelfu/modelops/layout.c diff --git a/src/modelops/phdr.c b/src/libelfu/modelops/phdr.c similarity index 100% rename from src/modelops/phdr.c rename to src/libelfu/modelops/phdr.c diff --git a/src/modelops/reladd.c b/src/libelfu/modelops/reladd.c similarity index 100% rename from src/modelops/reladd.c rename to src/libelfu/modelops/reladd.c diff --git a/src/modelops/relocate.c b/src/libelfu/modelops/relocate.c similarity index 100% rename from src/modelops/relocate.c rename to src/libelfu/modelops/relocate.c diff --git a/src/modelops/section.c b/src/libelfu/modelops/section.c similarity index 100% rename from src/modelops/section.c rename to src/libelfu/modelops/section.c diff --git a/src/modelops/symtab.c b/src/libelfu/modelops/symtab.c similarity index 100% rename from src/modelops/symtab.c rename to src/libelfu/modelops/symtab.c diff --git a/src/modelops/toFile.c b/src/libelfu/modelops/toFile.c similarity index 100% rename from src/modelops/toFile.c rename to src/libelfu/modelops/toFile.c -- 2.30.2