1 # Microb Technology, Eirbot, Droids-corp 2007 - Zer0
2 # Makefile for projects
4 # Inspired by the WinAVR Sample makefile written by Eric
5 # B. Weddington, J÷rg Wunsch, et al.
9 # make all = Make software.
11 # make clean = Clean out built project files.
13 # make program = Download the hex file to the device, using avrdude/avarice. Please
14 # customize the settings below first!
16 # make filename.s = Just compile filename.c into the assembler code only
18 # To rebuild project do "make clean" then "make all".
28 # absolute path to avoid some editors from beeing confused with vpath when
30 ABS_AVERSIVE_DIR:=$(shell cd $(AVERSIVE_DIR) ; pwd)
31 ABS_PROJECT_DIR:=$(shell pwd)
33 # includes for modules
34 MODULES_INC = $(addprefix $(ABS_AVERSIVE_DIR)/modules/,$(MODULES))
36 # List any extra directories to look for include files here.
37 # Each directory must be seperated by a space.
38 EXTRAINCDIRS += . $(ABS_AVERSIVE_DIR)/include $(ABS_AVERSIVE_DIR)/modules $(MODULES_INC)
39 # base/utils, base/wait and base/list are deprecated dirs, we need them for compatibility
40 EXTRAINCDIRS += $(ABS_AVERSIVE_DIR)/modules/base/utils
41 EXTRAINCDIRS += $(ABS_AVERSIVE_DIR)/modules/base/wait
42 EXTRAINCDIRS += $(ABS_AVERSIVE_DIR)/modules/base/list
45 # Optional compiler flags.
46 # -g: generate debugging information
47 # -O*: optimization level
48 # -f...: tuning, see gcc manual and avr-libc documentation
49 # -Wall...: warning level
52 CFLAGS += -Wall -Wstrict-prototypes
53 CFLAGS += $(patsubst %,-I%,$(EXTRAINCDIRS))
58 CFLAGS += -funsigned-char -funsigned-bitfields -fpack-struct -fshort-enums -mmcu=$(MCU)
60 CFLAGS += -DHOST_VERSION
64 ALL_CFLAGS += $(CFLAGS)
67 # Combine all necessary flags and optional flags.
68 # -Wa,...: tell GCC to pass this to the assembler.
69 # -ahlms: create assembler listing
70 ALL_CFLAGS += -Wa,-adhlns=$(addprefix compiler_files/,$(<:.c=.$(HOST).lst))
72 ALL_CFLAGS += $(PTHREAD_CFLAGS)
82 # Optional assembler flags.
83 # -Wa,...: tell GCC to pass this to the assembler.
84 # -ahlms: create listing
85 # -gstabs: have the assembler create line number information
86 ASFLAGS += -mmcu=$(MCU) $(patsubst %,-I%,$(EXTRAINCDIRS))
87 ASFLAGS += -Wa,-gstabs
88 ASFLAGS += -x assembler-with-cpp
96 # Optional linker flags.
97 # -Wl,...: tell GCC to pass this to linker.
98 # -Map: create map file
99 # --cref: add cross reference to map file
100 # Some variables are generated by config, see in .aversive_conf
102 LDFLAGS += -mmcu=$(MCU) $(PRINTF_LDFLAGS)
103 LDFLAGS += -Wl,-Map=$(addprefix compiler_files/,$(TARGET).map),--cref
105 LDFLAGS += $(PTHREAD_LDFLAGS)
108 LDFLAGS += $(MATH_LIB)
111 # AVRDUDE does not know the ATMEGA1281 for now, consider it a 128.
112 ifeq ($(MCU),atmega1281)
113 AVRDUDE_MCU = atmega128
118 AVRDUDE_WRITE_FLASH = -U flash:w:$(TARGET).$(FORMAT_EXTENSION)
119 #AVRDUDE_WRITE_EEPROM = -U eeprom:w:$(TARGET).eep
121 # Optional AVRDUDE flags can be added in the makefile of the project
122 # (to adjust the baud rate, ...)
125 AVRDUDE_FLAGS = -p $(AVRDUDE_MCU) -P $(AVRDUDE_PORT) -c $(AVRDUDE_PROGRAMMER) $(AVRDUDE_FLAGS_OPT)
127 ifneq ($(AVRDUDE_DELAY),)
128 AVRDUDE_FLAGS += -i $(AVRDUDE_DELAY)
133 # Uncomment the following if you want avrdude's erase cycle counter.
134 # Note that this counter needs to be initialized first using -Yn,
135 # see avrdude manual.
138 # Uncomment the following if you do /not/ wish a verification to be
139 # performed after programming the device.
142 # Increase verbosity level. Please use this when submitting bug
143 # reports about avrdude. See <http://savannah.nongnu.org/projects/avrdude>
144 # to submit bug reports.
145 #AVRDUDE_FLAGS += -v -v
148 # Sets the baudrate. Comment this if you want to have the default baudrate
149 AVRDUDE_FLAGS += -b $(AVRDUDE_BAUDRATE)
151 AVARICE_WRITE_FLASH = --erase --program --file $(TARGET).$(FORMAT_EXTENSION)
152 #AVARICE_WRITE_EEPROM = XXX
154 export AVARICE_FLAGS = -P $(MCU) --jtag $(AVARICE_PORT) --$(AVARICE_PROGRAMMER)
157 # ---------------------------------------------------------------------------
160 # Define programs and commands.
165 OBJCOPY = avr-objcopy
166 OBJDUMP = avr-objdump
169 OUTPUT = $(TARGET).elf
170 OTHER_OUTPUT = $(TARGET).$(FORMAT_EXTENSION) $(TARGET).eep compiler_files/$(TARGET).lss compiler_files/$(TARGET).sym
177 SIZE = size --format=Berkeley
182 export REMOVE = rm -f
187 export AVRDUDE = avrdude
188 export AVARICE = avarice
189 HEXSIZE = $(SIZE) --target=$(FORMAT) $(OUTPUT)
190 ELFSIZE = $(SIZE) $(OUTPUT)
191 ELFMD5 = $(MD5) $(OUTPUT) | cut -b1-4
193 export AVRDUDE_PROGRAMMER
199 # ---------------------------------------------------------------------------
204 MSG_SIZE_BEFORE = Size before:
205 MSG_SIZE_AFTER = Size after:
206 MSG_FLASH = Creating load file for Flash:
207 MSG_EEPROM = Creating load file for EEPROM:
208 MSG_EXTENDED_LISTING = Creating Extended Listing:
209 MSG_SYMBOL_TABLE = Creating Symbol Table:
210 MSG_LINKING = Linking:
211 MSG_COMPILING = Compiling:
212 MSG_PREPROC = Preprocessing:
213 MSG_ASSEMBLING = Assembling:
214 MSG_CLEANING = Cleaning project:
215 MSG_MD5_BEFORE = Processing MD5:
216 MSG_MD5_AFTER = Processing MD5:
217 MSG_DEPCLEAN = Cleaning deps:
218 MSG_MODULE = ------ Compiling Module:
221 # ---------------------------------------------------------------------------
223 OBJ = $(addprefix compiler_files/,$(SRC:.c=.$(HOST).o) $(ASRC:.S=.$(HOST).o))
224 DEPS = $(addprefix compiler_files/,$(SRC:.c=.$(HOST).d))
226 MODULES_LIB = $(addprefix compiler_files/,$(notdir $(MODULES:=.$(HOST).a)))
228 PREPROC= $(addprefix compiler_files/,$(SRC:.c=.$(HOST).preproc))
233 # Variables n{\'e}cessaires pour les Makefile des modules
234 export AVERSIVE_DIR ABS_AVERSIVE_DIR
235 export CFLAGS EXTRAINCDIRS
239 all: compiler_files gccversion sizebefore md5sumbefore modules $(OUTPUT) $(OTHER_OUTPUT) sizeafter md5sumafter
241 # only compile project files
242 project: compiler_files gccversion sizebefore md5sumbefore $(OUTPUT) $(OTHER_OUTPUT) sizeafter md5sumafter
245 @mkdir -p compiler_files
248 # ------ Compilation/link/assemble targets
250 # Compile modules and create a library for each
256 @echo $(MSG_MODULE) $@
257 @$(MAKE) VPATH=$(ABS_AVERSIVE_DIR)/modules/$@ -f $(AVERSIVE_DIR)/modules/$@/Makefile
259 # Link: create ELF output file from object files.
260 $(OUTPUT): $(PREPROC) $(OBJ) $(MODULES_LIB)
262 @echo $(MSG_LINKING) $@
263 $(CC) $(OBJ) $(MODULES_LIB) --output $@ $(LDFLAGS)
266 # Compile: create object files from C source files.
267 compiler_files/%.$(HOST).preproc : %.c
269 @echo $(MSG_PREPROC) $<
270 $(CC) -E $(ALL_CFLAGS) $< -o $@
272 # Compile: create object files from C source files.
273 compiler_files/%.$(HOST).o : %.c
275 @echo $(MSG_COMPILING) $<
276 $(CC) -c $(ALL_CFLAGS) $(ABS_PROJECT_DIR)/$< -o $@
279 # Compile: create assembler files from C source files.
280 compiler_files/%.$(HOST).s : %.c
281 $(CC) -S $(ALL_CFLAGS) $< -o $@
284 # Assemble: create object files from assembler source files.
285 compiler_files/%.$(HOST).o : %.S
287 @echo $(MSG_ASSEMBLING) $<
288 $(CC) -c $(ASFLAGS) $< -o $@
292 # ------ Conversion/listings targets
294 # Create final output files (.hex, .eep) from ELF output file.
295 %.$(FORMAT_EXTENSION): %.elf
297 @echo $(MSG_FLASH) $@
298 $(OBJCOPY) -O $(FORMAT) -R .eeprom $< $@
302 @echo $(MSG_EEPROM) $@
303 -$(OBJCOPY) -j .eeprom --set-section-flags=.eeprom="alloc,load" \
304 --change-section-lma .eeprom=0 -O $(FORMAT) $< $@
306 # Create extended listing file from ELF output file.
307 compiler_files/%.lss: %.elf
309 @echo $(MSG_EXTENDED_LISTING) $@
310 $(OBJDUMP) -h -S $< > $@
312 # Create a symbol table from ELF output file.
313 compiler_files/%.sym: %.elf
315 @echo $(MSG_SYMBOL_TABLE) $@
319 # ------ utils targets
321 # Display size/md5 of file.
323 @if [ -f $(OUTPUT) ]; then echo; echo $(MSG_SIZE_BEFORE); $(ELFSIZE); echo; fi
326 @if [ -f $(OUTPUT) ]; then echo; echo $(MSG_SIZE_AFTER); $(ELFSIZE); echo; fi
329 @if [ -f $(OUTPUT) ]; then echo; echo $(MSG_MD5_BEFORE); $(ELFMD5); echo; fi
332 @if [ -f $(OUTPUT) ]; then echo; echo $(MSG_MD5_AFTER); $(ELFMD5); echo; fi
335 # Display compiler version information.
339 # Program the device.
340 program: $(TARGET).$(FORMAT_EXTENSION) $(TARGET).eep
341 @if [ "$(PROGRAMMER)" = "avrdude" ]; then \
342 echo $(AVRDUDE) -e $(AVRDUDE_FLAGS) $(AVRDUDE_WRITE_FLASH) $(AVRDUDE_WRITE_EEPROM) ;\
343 $(AVRDUDE) -e $(AVRDUDE_FLAGS) $(AVRDUDE_WRITE_FLASH) $(AVRDUDE_WRITE_EEPROM) $(AVRDUDE_FLAGS_SIGNATURE_CHECK) ;\
345 @if [ "$(PROGRAMMER)" = "avarice" ]; then \
346 echo $(AVARICE) $(AVARICE_FLAGS) $(AVARICE_WRITE_FLASH) $(AVARICE_WRITE_EEPROM) ;\
347 $(AVARICE) $(AVARICE_FLAGS) $(AVARICE_WRITE_FLASH) $(AVARICE_WRITE_EEPROM) ;\
350 # Program the device.
351 erase: $(TARGET).$(FORMAT_EXTENSION) $(TARGET).eep
352 @if [ "$(PROGRAMMER)" = "avrdude" ]; then \
353 echo $(AVRDUDE) $(AVRDUDE_FLAGS) -e ;\
354 $(AVRDUDE) $(AVRDUDE_FLAGS) -e ;\
356 @if [ "$(PROGRAMMER)" = "avarice" ]; then \
357 echo "Not supported now." ; \
362 @if [ "$(PROGRAMMER)" = "avrdude" ]; then \
363 echo $(AVRDUDE) $(AVRDUDE_FLAGS) ;\
364 $(AVRDUDE) $(AVRDUDE_FLAGS) ;\
366 @if [ "$(PROGRAMMER)" = "avarice" ]; then \
367 echo "Not supported now." ; \
370 debug: $(TARGET).$(FORMAT_EXTENSION)
371 @if [ "$(PROGRAMMER)" = "avrdude" ]; then \
372 echo "Cannot debug with avrdude" ; \
374 @if [ "$(PROGRAMMER)" = "avarice" ]; then \
375 echo $(AVARICE) $(AVARICE_FLAGS) :$(AVARICE_DEBUG_PORT) ;\
376 $(AVARICE) $(AVARICE_FLAGS) :$(AVARICE_DEBUG_PORT) ;\
380 @$(AVERSIVE_DIR)/config/prog_fuses.sh $(AVERSIVE_DIR)/config/fuses_defs/$(MCU)
383 # ------ config targets
386 @${SHELL} -n $(AVERSIVE_DIR)/config/config.in
387 @HELP_FILE=$(AVERSIVE_DIR)/config/Configure.help \
388 AUTOCONF_FILE=autoconf.h \
389 ${SHELL} $(AVERSIVE_DIR)/config/scripts/Configure $(AVERSIVE_DIR)/config/config.in
392 @${SHELL} -n $(AVERSIVE_DIR)/config/config.in
393 @HELP_FILE=$(AVERSIVE_DIR)/config/Configure.help \
394 AUTOCONF_FILE=autoconf.h \
395 ${SHELL} $(AVERSIVE_DIR)/config/scripts/Configure -d $(AVERSIVE_DIR)/config/config.in
398 @${SHELL} -n $(AVERSIVE_DIR)/config/config.in
399 @make -C $(AVERSIVE_DIR)/config/scripts/lxdialog all
400 @HELP_FILE=$(AVERSIVE_DIR)/config/Configure.help \
401 AUTOCONF_FILE=autoconf.h \
402 ${SHELL} $(AVERSIVE_DIR)/config/scripts/Menuconfig $(AVERSIVE_DIR)/config/config.in
404 # ------ clean targets
407 $(REMOVE) compiler_files/*
410 clean: depclean clean_list modules_clean
413 # clean modules files
414 modules_clean: $(patsubst %,%_clean,$(MODULES))
417 $(patsubst %,%_clean,$(MODULES)):
418 @$(MAKE) VPATH=$(ABS_AVERSIVE_DIR)/modules/$(@:_clean=) -f $(ABS_AVERSIVE_DIR)/modules/$(@:_clean=)/Makefile clean
423 @echo $(MSG_CLEANING)
424 $(REMOVE) $(OUTPUT) $(OTHER_OUTPUT)
425 $(REMOVE) compiler_files/$(TARGET).map
433 # ------ dependencies targets
435 depclean: dep_list modules_depclean
438 modules_depclean: $(patsubst %,%_depclean,$(MODULES))
441 $(patsubst %,%_depclean,$(MODULES)):
442 @$(MAKE) VPATH=$(ABS_AVERSIVE_DIR)/modules/$(@:_depclean=) -f $(ABS_AVERSIVE_DIR)/modules/$(@:_depclean=)/Makefile depclean
447 @echo $(MSG_DEPCLEAN)
451 # Automatically generate C source code dependencies.
452 compiler_files/%.$(HOST).d: %.c
453 @mkdir -p compiler_files ; \
455 for conf_file in .config autoconf.h .aversive_conf; do \
456 if [ ! -f $$conf_file ]; then \
457 echo "$$conf_file file is missing"; \
461 for module in `echo $(MODULES)`; do \
462 conf=`basename $$module"_config.h"`; \
463 if [ -f $$module/config/$$conf ]; then \
464 if [ ! -f $$conf ]; then \
465 echo "$$conf file is missing"; \
470 if [ $$error -eq 1 ]; then \
471 echo "Missing config files, please run make menuconfig or make config"; \
476 $(CC) -M $(CFLAGS) $< > $@.$$$$; \
477 sed 's,\($*\)\.o[ :]*,compiler_files/\1.$(HOST).o $@ : ,g' < $@.$$$$ > $@; \
482 # include the deps file only for other targets than menuconfig or config
483 ifeq (,$(findstring config,$(MAKECMDGOALS)))
484 ifeq (,$(wildcard .config))
485 $(error You need to call make config or make menuconfig first)
487 ifeq (,$(wildcard autoconf.h))
488 $(error Missing autoconf.h -- You need to call make noconfig)
490 ifeq (,$(wildcard .aversive_conf))
491 $(error Missing .aversive_conf -- You need to call make noconfig)
494 ifeq (,$(findstring mrproper,$(MAKECMDGOALS)))
495 -include $(addprefix compiler_files/,$(SRC:.c=.$(HOST).d))
498 ifneq (1,$(words $(MAKECMDGOALS)))
499 $(error You need to call make config or make menuconfig without other targets)
504 # ------ Listing of phony targets.
506 .PHONY : all sizebefore sizeafter gccversion \
507 clean clean_list program md5sumafter md5sumbefore \
508 depclean dep_list modules $(MODULES) \