Makefile lib
From Attie's Wiki
(Difference between revisions)
m |
m |
||
(7 intermediate revisions by one user not shown) | |||
Line 1: | Line 1: | ||
− | This makefile | + | This makefile relies on 4 files: |
+ | {| | ||
+ | | <code>makefile</code> || the makefile | ||
+ | |- | ||
+ | | <code>libconfig.mk</code> || configuration for the library - source list, version, output name, system headers, release files, etc... | ||
+ | |- | ||
+ | | <code>buildconfig.mk</code> || configuration for the build environment - output directory and other internal configuration | ||
+ | |- | ||
+ | | <code>config.mk</code> || system / user configuration - install directory, toolchain, compile options, library options | ||
+ | |} | ||
− | + | It is also recommended that you have <code>ver.c</code> (see below) | |
− | < | + | |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
==makefile== | ==makefile== | ||
<source lang="text"> | <source lang="text"> | ||
− | + | ### main makefile | |
+ | ### it is recommended that you DO NOT change this file | ||
− | + | include config.mk | |
− | + | include libconfig.mk | |
− | + | include buildconfig.mk | |
RELEASE_ITEMS:=$(DESTDIR)/$(LIBOUT).so.$(LIBFULLREV) \ | RELEASE_ITEMS:=$(DESTDIR)/$(LIBOUT).so.$(LIBFULLREV) \ | ||
Line 55: | Line 29: | ||
$(RELEASE_FILES) | $(RELEASE_FILES) | ||
− | + | PDEPS:=makefile config.mk libconfig.mk buildconfig.mk | |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
############################################################################### | ############################################################################### | ||
− | .PHONY: all install install_dbg install_sudo install_dbg_sudo help clean | + | .PHONY: all install install_dbg install_sudo install_dbg_sudo help clean distclean new release .%.dir |
− | . | + | .PHONY: ALWAYS |
OBJS:=$(addprefix $(BUILDDIR)/,$(addsuffix .o,$(SRCS))) | OBJS:=$(addprefix $(BUILDDIR)/,$(addsuffix .o,$(SRCS))) | ||
Line 77: | Line 42: | ||
install: all | install: all | ||
− | sudo make install_sudo | + | @sudo make install_sudo |
install_dbg: all | install_dbg: all | ||
− | sudo make install_dbg_sudo | + | @sudo make install_dbg_sudo |
install_sudo: all $(addprefix $(SYS_INCDIR)/,$(SYS_HEADERS)) $(SYS_LIBDIR)/$(LIBOUT).so.$(LIBFULLREV) $(SYS_LIBDIR)/$(LIBOUT).a.$(LIBFULLREV) | install_sudo: all $(addprefix $(SYS_INCDIR)/,$(SYS_HEADERS)) $(SYS_LIBDIR)/$(LIBOUT).so.$(LIBFULLREV) $(SYS_LIBDIR)/$(LIBOUT).a.$(LIBFULLREV) | ||
Line 91: | Line 56: | ||
@echo " make clean - to remote all object files and start again" | @echo " make clean - to remote all object files and start again" | ||
@echo " make new - to perform a 'clean', followed by an 'all'" | @echo " make new - to perform a 'clean', followed by an 'all'" | ||
− | @echo " make | + | @echo " make distclean - to remove ALL generated files and directories" |
@echo " make release - to make a *.tar.bz2 file containing all files required to make use of $(LIBOUT)" | @echo " make release - to make a *.tar.bz2 file containing all files required to make use of $(LIBOUT)" | ||
@echo " make install - to install $(LIBOUT) on this system" | @echo " make install - to install $(LIBOUT) on this system" | ||
@echo " make install_dbg - to install $(LIBOUT) along with debug information on this system" | @echo " make install_dbg - to install $(LIBOUT) along with debug information on this system" | ||
@echo " make help - to display this help information" | @echo " make help - to display this help information" | ||
+ | @echo "" | ||
+ | @echo "environment:" | ||
+ | @echo " CROSS_COMPILE - set this to your toolchain's prefix (e.g: arm-none-linux-)" | ||
+ | @echo " CFLAGS - add flags for the compile step of the build" | ||
+ | @echo " CLINKS - add flags for the link step of the build" | ||
@echo "" | @echo "" | ||
@echo "other information:" | @echo "other information:" | ||
− | @echo " to modify settings for the build environment and control various aspects | + | @echo " to modify settings for the build environment and control various aspects of the resulting binary," |
− | @echo " edit ' | + | @echo " edit 'config.mk'" |
$(SYS_LIBDIR)/$(LIBOUT).%.$(LIBFULLREV): $(DESTDIR)/$(LIBOUT).%.$(LIBFULLREV) | $(SYS_LIBDIR)/$(LIBOUT).%.$(LIBFULLREV): $(DESTDIR)/$(LIBOUT).%.$(LIBFULLREV) | ||
− | + | install -g root -o root -m 755 -DT $^ $@ | |
− | + | ||
ln -fs $@ $(subst .$(LIBFULLREV),,$@) | ln -fs $@ $(subst .$(LIBFULLREV),,$@) | ||
$(SYS_LIBDIR)/$(LIBOUT).so.$(LIBFULLREV).dbg: $(DESTDIR)/$(LIBOUT).so.$(LIBFULLREV).dbg | $(SYS_LIBDIR)/$(LIBOUT).so.$(LIBFULLREV).dbg: $(DESTDIR)/$(LIBOUT).so.$(LIBFULLREV).dbg | ||
− | + | install -g root -o root -m 755 -DT $^ $@ | |
− | + | ||
$(SYS_INCDIR)/%.h: %.h | $(SYS_INCDIR)/%.h: %.h | ||
− | + | install -g root -o root -m 644 -DT $^ $@ | |
− | + | ||
new: clean | new: clean | ||
− | @$(MAKE) | + | @$(MAKE) all |
clean: | clean: | ||
− | rm - | + | rm -f $(BUILDDIR)/*.o |
rm -rdf $(DESTDIR)/* | rm -rdf $(DESTDIR)/* | ||
+ | $(MAKE) -C modes clean | ||
− | + | distclean: | |
rm -rdf $(BUILDDIR) .$(BUILDDIR).dir | rm -rdf $(BUILDDIR) .$(BUILDDIR).dir | ||
rm -rdf $(DESTDIR) .$(DESTDIR).dir | rm -rdf $(DESTDIR) .$(DESTDIR).dir | ||
+ | $(MAKE) -C modes distclean | ||
+ | |||
+ | tidy: | ||
+ | rm -f `find . -name '*~'` | ||
Line 138: | Line 110: | ||
$(DESTDIR)/$(LIBOUT).so: $(DESTDIR)/$(LIBOUT).so.$(LIBFULLREV) | $(DESTDIR)/$(LIBOUT).so: $(DESTDIR)/$(LIBOUT).so.$(LIBFULLREV) | ||
− | + | ln -fs `basename $^` $@ | |
$(DESTDIR)/$(LIBOUT).so.$(LIBFULLREV): .$(DESTDIR).dir $(DESTDIR)/$(LIBOUT).o | $(DESTDIR)/$(LIBOUT).so.$(LIBFULLREV): .$(DESTDIR).dir $(DESTDIR)/$(LIBOUT).o | ||
− | + | $(GCC) -shared -Wl,-soname,$(LIBOUT).so.$(LIBFULLREV) $(CLINKS) $(filter %.o,$^) -o $@ | |
− | + | $(OBJCOPY) --only-keep-debug $@ $@.dbg | |
− | + | $(OBJCOPY) --add-gnu-debuglink=$@.dbg $@ | |
− | + | $(OBJCOPY) --strip-debug $@ | |
− | + | ||
$(DESTDIR)/$(LIBOUT).a: $(DESTDIR)/$(LIBOUT).a.$(LIBFULLREV) | $(DESTDIR)/$(LIBOUT).a: $(DESTDIR)/$(LIBOUT).a.$(LIBFULLREV) | ||
− | + | ln -fs `basename $^` $@ | |
$(DESTDIR)/$(LIBOUT).a.$(LIBFULLREV): .$(DESTDIR).dir $(DESTDIR)/$(LIBOUT).o | $(DESTDIR)/$(LIBOUT).a.$(LIBFULLREV): .$(DESTDIR).dir $(DESTDIR)/$(LIBOUT).o | ||
− | + | $(AR) rcs $@ $(filter %.o,$^) | |
− | + | ||
$(DESTDIR)/$(LIBOUT).o: .$(DESTDIR).dir $(OBJS) | $(DESTDIR)/$(LIBOUT).o: .$(DESTDIR).dir $(OBJS) | ||
− | + | $(LD) -r $(filter %.o,$^) -o $@ | |
− | + | ||
− | $(BUILDDIR)/%.d: .$(BUILDDIR).dir %.c | + | $(BUILDDIR)/%.d: .$(BUILDDIR).dir %.c $(PDEPS) |
− | + | $(GCC) -MM -MT $(addprefix $(BUILDDIR)/,$*.o) $*.c -o $@ | |
− | + | ||
− | $(BUILDDIR)/ver.o: $(BUILDDIR)/ver.d | + | $(BUILDDIR)/ver.o: $(BUILDDIR)/ver.d $(wildcard %.c) $(wildcard %.h) $(PDEPS) |
− | + | $(GCC) $(CFLAGS) $(VER_DEFINES) ver.c -o $@ | |
− | + | ||
− | $(BUILDDIR)/%.o: $(BUILDDIR)/%.d | + | $(BUILDDIR)/%.o: $(BUILDDIR)/%.d $(PDEPS) |
− | + | $(GCC) $(CFLAGS) $*.c -o $@ | |
− | + | ||
include $(wildcard $(BUILDDIR)/*.d) | include $(wildcard $(BUILDDIR)/*.d) | ||
+ | </source> | ||
+ | |||
+ | ==libconfig.mk== | ||
+ | <source lang="text"> | ||
+ | ### internal configuration options | ||
+ | ### it is recommended that you DO NOT change this file, unless you are the maintainer of the library | ||
+ | |||
+ | LIBMAJ:= 0 | ||
+ | LIBMIN:= 0 | ||
+ | LIBREV:= 1 | ||
+ | |||
+ | LIBOUT:= libmine | ||
+ | |||
+ | LIBS:= pthread | ||
+ | |||
+ | SRCS:= mylib | ||
+ | |||
+ | SYS_HEADERS:= mylib.h | ||
+ | RELEASE_FILES:= HISTORY | ||
+ | |||
+ | VER_DEFINES= -DLIB_REVISION="\"$(LIBFULLREV)\"" \ | ||
+ | -DLIB_COMMIT="\"$(shell git log -1 --format="%H")\"" \ | ||
+ | -DLIB_COMMITTER="\"$(shell git log -1 --format="%cn <%ce>")\"" \ | ||
+ | -DLIB_BUILDTIME="\"$(shell date)\"" | ||
+ | </source> | ||
+ | |||
+ | ==buildconfig.mk== | ||
+ | <source lang="text"> | ||
+ | ### internal build configuration options | ||
+ | ### it is recommended that you DO NOT change this file | ||
+ | |||
+ | BUILDDIR:=.build | ||
+ | DESTDIR:=lib | ||
+ | LIBFULLREV:=$(LIBMAJ).$(LIBMIN).$(LIBREV) | ||
+ | |||
+ | AR:=$(CROSS_COMPILE)ar | ||
+ | LD:=$(CROSS_COMPILE)ld | ||
+ | GCC:=$(CROSS_COMPILE)gcc | ||
+ | OBJCOPY:=$(CROSS_COMPILE)objcopy | ||
+ | MAKE+=--no-print-directory | ||
+ | |||
+ | DEBUG:=-g | ||
+ | CFLAGS+=-Wall -Wstrict-prototypes -Wno-variadic-macros -c -fPIC $(DEBUG) $(addprefix -D,$(OPTIONS)) | ||
+ | CFLAGS+=-fvisibility=hidden | ||
+ | #CFLAGS+=-pedantic | ||
+ | CLINKS+=$(addprefix -l,$(LIBS)) $(DEBUG) | ||
+ | |||
+ | COMMA:=, | ||
+ | .PRECIOUS: .%.dir $(BUILDDIR)/%.d | ||
+ | </source> | ||
+ | |||
+ | ==config.mk== | ||
+ | <source lang="text"> | ||
+ | ### library configuration options | ||
+ | |||
+ | ### system install directories | ||
+ | SYS_LIBDIR:= /usr/lib | ||
+ | SYS_INCDIR:= /usr/include | ||
+ | |||
+ | ### setup a cross-compile toolchain (either here, or in the environment) | ||
+ | #CROSS_COMPILE?= | ||
+ | #CFLAGS+= | ||
+ | #CLINKS+= | ||
+ | |||
+ | ### un-comment to remove ALL logging (smaller & faster binary) | ||
+ | #OPTIONS+= DISABLE_LOGGING | ||
+ | </source> | ||
+ | |||
+ | ==ver.c== | ||
+ | <source lang="c"> | ||
+ | #define EXPORT __attribute__((visibility ("default"))) | ||
+ | |||
+ | EXPORT const char lib_revision[] = LIB_REVISION; | ||
+ | EXPORT const char lib_commit[] = LIB_COMMIT; | ||
+ | EXPORT const char lib_committer[] = LIB_COMMITTER; | ||
+ | EXPORT const char lib_buildtime[] = LIB_BUILDTIME; | ||
</source> | </source> |
Latest revision as of 10:49, 24 February 2012
This makefile relies on 4 files:
makefile |
the makefile |
libconfig.mk |
configuration for the library - source list, version, output name, system headers, release files, etc... |
buildconfig.mk |
configuration for the build environment - output directory and other internal configuration |
config.mk |
system / user configuration - install directory, toolchain, compile options, library options |
It is also recommended that you have ver.c
(see below)
Contents |
[edit] makefile
### main makefile ### it is recommended that you DO NOT change this file include config.mk include libconfig.mk include buildconfig.mk RELEASE_ITEMS:=$(DESTDIR)/$(LIBOUT).so.$(LIBFULLREV) \ $(DESTDIR)/$(LIBOUT).so \ $(DESTDIR)/$(LIBOUT).so.$(LIBFULLREV).dbg \ $(DESTDIR)/$(LIBOUT).a.$(LIBFULLREV) \ $(DESTDIR)/$(LIBOUT).a \ $(SYS_HEADERS) \ $(RELEASE_FILES) PDEPS:=makefile config.mk libconfig.mk buildconfig.mk ############################################################################### .PHONY: all install install_dbg install_sudo install_dbg_sudo help clean distclean new release .%.dir .PHONY: ALWAYS OBJS:=$(addprefix $(BUILDDIR)/,$(addsuffix .o,$(SRCS))) all: $(DESTDIR)/$(LIBOUT).so $(DESTDIR)/$(LIBOUT).a install: all @sudo make install_sudo install_dbg: all @sudo make install_dbg_sudo install_sudo: all $(addprefix $(SYS_INCDIR)/,$(SYS_HEADERS)) $(SYS_LIBDIR)/$(LIBOUT).so.$(LIBFULLREV) $(SYS_LIBDIR)/$(LIBOUT).a.$(LIBFULLREV) install_dbg_sudo: install_sudo $(SYS_LIBDIR)/$(LIBOUT).so.$(LIBFULLREV).dbg help: @echo "usage:" @echo " make [all] - to simply build $(LIBOUT)" @echo " make clean - to remote all object files and start again" @echo " make new - to perform a 'clean', followed by an 'all'" @echo " make distclean - to remove ALL generated files and directories" @echo " make release - to make a *.tar.bz2 file containing all files required to make use of $(LIBOUT)" @echo " make install - to install $(LIBOUT) on this system" @echo " make install_dbg - to install $(LIBOUT) along with debug information on this system" @echo " make help - to display this help information" @echo "" @echo "environment:" @echo " CROSS_COMPILE - set this to your toolchain's prefix (e.g: arm-none-linux-)" @echo " CFLAGS - add flags for the compile step of the build" @echo " CLINKS - add flags for the link step of the build" @echo "" @echo "other information:" @echo " to modify settings for the build environment and control various aspects of the resulting binary," @echo " edit 'config.mk'" $(SYS_LIBDIR)/$(LIBOUT).%.$(LIBFULLREV): $(DESTDIR)/$(LIBOUT).%.$(LIBFULLREV) install -g root -o root -m 755 -DT $^ $@ ln -fs $@ $(subst .$(LIBFULLREV),,$@) $(SYS_LIBDIR)/$(LIBOUT).so.$(LIBFULLREV).dbg: $(DESTDIR)/$(LIBOUT).so.$(LIBFULLREV).dbg install -g root -o root -m 755 -DT $^ $@ $(SYS_INCDIR)/%.h: %.h install -g root -o root -m 644 -DT $^ $@ new: clean @$(MAKE) all clean: rm -f $(BUILDDIR)/*.o rm -rdf $(DESTDIR)/* $(MAKE) -C modes clean distclean: rm -rdf $(BUILDDIR) .$(BUILDDIR).dir rm -rdf $(DESTDIR) .$(DESTDIR).dir $(MAKE) -C modes distclean tidy: rm -f `find . -name '*~'` release: all tar -cjvf $(LIBOUT)_v$(LIBFULLREV)_`date +%Y-%m-%d`_`git rev-parse --verify --short HEAD`_`uname -m`.tar.bz2 $(RELEASE_ITEMS) .%.dir: @if [ ! -d $* ]; then echo "mkdir -p $*"; mkdir -p $*; else echo "!mkdir $*"; fi @touch $@ $(DESTDIR)/$(LIBOUT).so: $(DESTDIR)/$(LIBOUT).so.$(LIBFULLREV) ln -fs `basename $^` $@ $(DESTDIR)/$(LIBOUT).so.$(LIBFULLREV): .$(DESTDIR).dir $(DESTDIR)/$(LIBOUT).o $(GCC) -shared -Wl,-soname,$(LIBOUT).so.$(LIBFULLREV) $(CLINKS) $(filter %.o,$^) -o $@ $(OBJCOPY) --only-keep-debug $@ $@.dbg $(OBJCOPY) --add-gnu-debuglink=$@.dbg $@ $(OBJCOPY) --strip-debug $@ $(DESTDIR)/$(LIBOUT).a: $(DESTDIR)/$(LIBOUT).a.$(LIBFULLREV) ln -fs `basename $^` $@ $(DESTDIR)/$(LIBOUT).a.$(LIBFULLREV): .$(DESTDIR).dir $(DESTDIR)/$(LIBOUT).o $(AR) rcs $@ $(filter %.o,$^) $(DESTDIR)/$(LIBOUT).o: .$(DESTDIR).dir $(OBJS) $(LD) -r $(filter %.o,$^) -o $@ $(BUILDDIR)/%.d: .$(BUILDDIR).dir %.c $(PDEPS) $(GCC) -MM -MT $(addprefix $(BUILDDIR)/,$*.o) $*.c -o $@ $(BUILDDIR)/ver.o: $(BUILDDIR)/ver.d $(wildcard %.c) $(wildcard %.h) $(PDEPS) $(GCC) $(CFLAGS) $(VER_DEFINES) ver.c -o $@ $(BUILDDIR)/%.o: $(BUILDDIR)/%.d $(PDEPS) $(GCC) $(CFLAGS) $*.c -o $@ include $(wildcard $(BUILDDIR)/*.d)
[edit] libconfig.mk
### internal configuration options ### it is recommended that you DO NOT change this file, unless you are the maintainer of the library LIBMAJ:= 0 LIBMIN:= 0 LIBREV:= 1 LIBOUT:= libmine LIBS:= pthread SRCS:= mylib SYS_HEADERS:= mylib.h RELEASE_FILES:= HISTORY VER_DEFINES= -DLIB_REVISION="\"$(LIBFULLREV)\"" \ -DLIB_COMMIT="\"$(shell git log -1 --format="%H")\"" \ -DLIB_COMMITTER="\"$(shell git log -1 --format="%cn <%ce>")\"" \ -DLIB_BUILDTIME="\"$(shell date)\""
[edit] buildconfig.mk
### internal build configuration options ### it is recommended that you DO NOT change this file BUILDDIR:=.build DESTDIR:=lib LIBFULLREV:=$(LIBMAJ).$(LIBMIN).$(LIBREV) AR:=$(CROSS_COMPILE)ar LD:=$(CROSS_COMPILE)ld GCC:=$(CROSS_COMPILE)gcc OBJCOPY:=$(CROSS_COMPILE)objcopy MAKE+=--no-print-directory DEBUG:=-g CFLAGS+=-Wall -Wstrict-prototypes -Wno-variadic-macros -c -fPIC $(DEBUG) $(addprefix -D,$(OPTIONS)) CFLAGS+=-fvisibility=hidden #CFLAGS+=-pedantic CLINKS+=$(addprefix -l,$(LIBS)) $(DEBUG) COMMA:=, .PRECIOUS: .%.dir $(BUILDDIR)/%.d
[edit] config.mk
### library configuration options ### system install directories SYS_LIBDIR:= /usr/lib SYS_INCDIR:= /usr/include ### setup a cross-compile toolchain (either here, or in the environment) #CROSS_COMPILE?= #CFLAGS+= #CLINKS+= ### un-comment to remove ALL logging (smaller & faster binary) #OPTIONS+= DISABLE_LOGGING
[edit] ver.c
#define EXPORT __attribute__((visibility ("default"))) EXPORT const char lib_revision[] = LIB_REVISION; EXPORT const char lib_commit[] = LIB_COMMIT; EXPORT const char lib_committer[] = LIB_COMMITTER; EXPORT const char lib_buildtime[] = LIB_BUILDTIME;