Makefile
From Attie's Wiki
(Difference between revisions)
m |
m |
||
Line 2: | Line 2: | ||
[[makefile_lib|Makefile for building a system library]]<br> | [[makefile_lib|Makefile for building a system library]]<br> | ||
[[makefile_simple|Super simple makefile]] | [[makefile_simple|Super simple makefile]] | ||
+ | |||
+ | ==Useful stuff== | ||
+ | ===Disable default/auto rules=== | ||
+ | <source lang="text"> | ||
+ | .SUFFIXES: | ||
+ | </source> | ||
+ | ===Disable auto remove of 'temporary' files=== | ||
+ | <source lang="text"> | ||
+ | .PRECIOUS: %.S # (or whatever extension is getting stupidly deleted) | ||
+ | </source> | ||
==Super-Simple makefile== | ==Super-Simple makefile== |
Revision as of 19:56, 1 July 2012
Makefile suitable for use with AVRs
Makefile for building a system library
Super simple makefile
Contents |
Useful stuff
Disable default/auto rules
.SUFFIXES:
Disable auto remove of 'temporary' files
.PRECIOUS: %.S # (or whatever extension is getting stupidly deleted)
Super-Simple makefile
CROSS_COMPILE?= CFLAGS+=-g CLINKS+=-g LIBS:= BINOUT:=main #--- no more configurables ---# SRCS:=$(wildcard *.c) HDRS:=$(wildcard *.h) OBJS:=$(SRCS:%.c=%.o) GCC:=$(CROSS_COMPILE)gcc CLINKS+=$(addprefix -l,$(LIBS)) which-makefile = $(CURDIR)/$(word $(words $(MAKEFILE_LIST)),$(MAKEFILE_LIST)) #--- phony targets ---# .PHONY: all run new clean all: $(BINOUT) run: $(BINOUT) $(shell readlink -f $<) new: clean @$(MAKE) --no-print-directory clean: rm -rf $(OBJS) $(BINOUT) #--- build system ---# main: $(OBJS) $(GCC) $(CLINKS) $(filter %.o,$(OBJS)) -o $@ $(OBJS): %.o: %.c $(SRCS) $(HDRS) $(call which-makefile) $(GCC) $(CFLAGS) $< -c -o $@
General purpose makefile
Usage:
-
make
- build the whole project -
make clean
- remove any object, binary, temporary (*~) files -
make spotless
- same as clean, but also removes any directories created during the build process -
make run
- build the project, and execute it -
make new
- re-build the whole project
# enable debugging? DEBUG:=-g # the output binary name (found in ./bin) # this is also used as a source file - %.c BINOUT:=main # when you type `make run` these args will be passed EXECARGS:= # the source files you wish to use (without the '.c') SRCS:= # any libraries you want to link with (without the 'lib'/'l' - e.g. "m rt") CLIBS:= ################################################################################### # There should be no need to change below here for most projects DIRS:=bin obj SRCS+=$(BINOUT) CC:=gcc CFLAGS:=$(DEBUG) -c -Wall CLINKS:=$(DEBUG) $(addprefix -l,$(CLIBS)) .PHONY: all new clean run spotless .dirs all: .dirs ./bin/$(BINOUT) new: clean ./bin/$(BINOUT) clean: rm -f *~ rm -f ./bin/$(BINOUT) rm -f ./obj/*.o run: ./bin/$(BINOUT) ./bin/$(BINOUT) $(EXECARGS) spotless: clean rm -rdf $(addprefix ./,$(DIRS)) .dirs .dirs: makefile mkdir -p $(addprefix ./,$(DIRS)) @touch .dirs ./bin/$(BINOUT): $(addprefix ./obj/,$(addsuffix .o,$(SRCS))) makefile $(CC) $(CLINKS) $(filter %.o,$^) -o $@ ./obj/%.o: %.c %.h makefile $(CC) $(CFLAGS) $*.c -o $@ ./obj/%.o: %.c makefile $(CC) $(CFLAGS) $*.c -o $@
Just compile everything
Replace the SRCS:=
line with this:
SRCS:=$(wildcard *.c) SRCS:=$(SRCS:.c=)
Useful Rules
Timestamp your binaries
File: makefile
.PHONY stamp stamp: sed -i -re "s/^(.*compileStamp[^=]*)=[ ]*[0-9]*(.*)$$/\1= `date +%s`\2/" stamp.c ./obj/stamp.o: makefile stamp ./stamp.c $(CC) $(CFLAGS) ./stamp.c -o $@
File: stamp.c
const char *svnRevision = "$Id$"; /* holds nice SVN info, this file should ALWAYS get comitted */ unsigned int compileStamp = 0; /* holds the unix timestamp of the most recent compile */
Provide an 'install' rule
LIBOUT:=libName VERSION:=1.0.0 SYSHEADERS:=mylib USRLIB:=/usr/lib USRINC:=/usr/include .PHONY: install install_su install: all @echo @echo ifneq ($(shell echo $$USER),root) @echo "#######################################################################################" @echo "### To Install this library I need the root password please!" @echo "#######################################################################################" endif su -c "make install_su --no-print-directory" @echo install_su: $(USRLIB)/$(LIBOUT).so.$(VERSION) $(addprefix $(USRINC)/,$(addsuffix .h,$(SYSHEADERS))) $(USRLIB)/$(LIBOUT).so.$(VERSION): ./lib/$(LIBOUT).so.$(VERSION) cp -f ./lib/$(LIBOUT).so.$(VERSION) $(USRLIB)/$(LIBOUT).so.$(VERSION) @chown root:root $(USRLIB)/$(LIBOUT).so.$(VERSION) @chmod 755 $(USRLIB)/$(LIBOUT).so.$(VERSION) ln -sf ./$(LIBOUT).so.$(VERSION) $(USRLIB)/$(LIBOUT).so.1 @chown root:root $(USRLIB)/$(LIBOUT).so.1 @chmod 755 $(USRLIB)/$(LIBOUT).so.1 ln -sf ./$(LIBOUT).so.$(VERSION) $(USRLIB)/$(LIBOUT).so @chown root:root $(USRLIB)/$(LIBOUT).so @chmod 755 $(USRLIB)/$(LIBOUT).so $(USRINC)/%.h: %.h cp -f $*.h $@ @chown root:root $@ @chmod 644 $@