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))
81 # Optional assembler flags.
82 # -Wa,...: tell GCC to pass this to the assembler.
83 # -ahlms: create listing
84 # -gstabs: have the assembler create line number information
85 ASFLAGS += -mmcu=$(MCU) $(patsubst %,-I%,$(EXTRAINCDIRS))
86 ASFLAGS += -Wa,-gstabs
87 ASFLAGS += -x assembler-with-cpp
95 # Optional linker flags.
96 # -Wl,...: tell GCC to pass this to linker.
97 # -Map: create map file
98 # --cref: add cross reference to map file
99 # Some variables are generated by config, see in .aversive_conf
101 LDFLAGS += -mmcu=$(MCU) $(PRINTF_LDFLAGS)
102 LDFLAGS += -Wl,-Map=$(addprefix compiler_files/,$(TARGET).map),--cref
104 LDFLAGS += $(PTHREAD_LDFLAGS)
107 LDFLAGS += $(MATH_LIB)
110 # AVRDUDE does not know the ATMEGA1281 for now, consider it a 128.
111 ifeq ($(MCU),atmega1281)
112 AVRDUDE_MCU = atmega128
117 AVRDUDE_WRITE_FLASH = -U flash:w:$(TARGET).$(FORMAT_EXTENSION)
118 #AVRDUDE_WRITE_EEPROM = -U eeprom:w:$(TARGET).eep
120 # Optional AVRDUDE flags can be added in the makefile of the project
121 # (to adjust the baud rate, ...)
124 AVRDUDE_FLAGS = -p $(AVRDUDE_MCU) -P $(AVRDUDE_PORT) -c $(AVRDUDE_PROGRAMMER) $(AVRDUDE_FLAGS_OPT)
126 ifneq ($(AVRDUDE_DELAY),)
127 AVRDUDE_FLAGS += -i $(AVRDUDE_DELAY)
132 # Uncomment the following if you want avrdude's erase cycle counter.
133 # Note that this counter needs to be initialized first using -Yn,
134 # see avrdude manual.
137 # Uncomment the following if you do /not/ wish a verification to be
138 # performed after programming the device.
141 # Increase verbosity level. Please use this when submitting bug
142 # reports about avrdude. See <http://savannah.nongnu.org/projects/avrdude>
143 # to submit bug reports.
144 #AVRDUDE_FLAGS += -v -v
147 # Sets the baudrate. Comment this if you want to have the default baudrate
148 AVRDUDE_FLAGS += -b $(AVRDUDE_BAUDRATE)
150 AVARICE_WRITE_FLASH = --erase --program --file $(TARGET).$(FORMAT_EXTENSION)
151 #AVARICE_WRITE_EEPROM = XXX
153 export AVARICE_FLAGS = -P $(MCU) --jtag $(AVARICE_PORT) --$(AVARICE_PROGRAMMER)
156 # ---------------------------------------------------------------------------
159 # Define programs and commands.
164 OBJCOPY = avr-objcopy
165 OBJDUMP = avr-objdump
168 OUTPUT = $(TARGET).elf
169 OTHER_OUTPUT = $(TARGET).$(FORMAT_EXTENSION) $(TARGET).eep compiler_files/$(TARGET).lss compiler_files/$(TARGET).sym
176 SIZE = size --format=Berkeley
181 export REMOVE = rm -f
186 export AVRDUDE = avrdude
187 export AVARICE = avarice
188 HEXSIZE = $(SIZE) --target=$(FORMAT) $(OUTPUT)
189 ELFSIZE = $(SIZE) $(OUTPUT)
190 ELFMD5 = $(MD5) $(OUTPUT) | cut -b1-4
192 export AVRDUDE_PROGRAMMER
198 # ---------------------------------------------------------------------------
203 MSG_SIZE_BEFORE = Size before:
204 MSG_SIZE_AFTER = Size after:
205 MSG_FLASH = Creating load file for Flash:
206 MSG_EEPROM = Creating load file for EEPROM:
207 MSG_EXTENDED_LISTING = Creating Extended Listing:
208 MSG_SYMBOL_TABLE = Creating Symbol Table:
209 MSG_LINKING = Linking:
210 MSG_COMPILING = Compiling:
211 MSG_PREPROC = Preprocessing:
212 MSG_ASSEMBLING = Assembling:
213 MSG_CLEANING = Cleaning project:
214 MSG_MD5_BEFORE = Processing MD5:
215 MSG_MD5_AFTER = Processing MD5:
216 MSG_DEPCLEAN = Cleaning deps:
217 MSG_MODULE = ------ Compiling Module:
220 # ---------------------------------------------------------------------------
222 OBJ = $(addprefix compiler_files/,$(SRC:.c=.$(HOST).o) $(ASRC:.S=.$(HOST).o))
223 DEPS = $(addprefix compiler_files/,$(SRC:.c=.$(HOST).d))
225 MODULES_LIB = $(addprefix compiler_files/,$(notdir $(MODULES:=.$(HOST).a)))
227 PREPROC= $(addprefix compiler_files/,$(SRC:.c=.$(HOST).preproc))
232 # Variables n{\'e}cessaires pour les Makefile des modules
233 export AVERSIVE_DIR ABS_AVERSIVE_DIR
234 export CFLAGS EXTRAINCDIRS
238 all: compiler_files gccversion sizebefore md5sumbefore modules $(OUTPUT) $(OTHER_OUTPUT) sizeafter md5sumafter
240 # only compile project files
241 project: compiler_files gccversion sizebefore md5sumbefore $(OUTPUT) $(OTHER_OUTPUT) sizeafter md5sumafter
244 @mkdir -p compiler_files
247 # ------ Compilation/link/assemble targets
249 # Compile modules and create a library for each
255 @echo $(MSG_MODULE) $@
256 @$(MAKE) VPATH=$(ABS_AVERSIVE_DIR)/modules/$@ -f $(AVERSIVE_DIR)/modules/$@/Makefile
258 # Link: create ELF output file from object files.
259 $(OUTPUT): $(PREPROC) $(OBJ) $(MODULES_LIB)
261 @echo $(MSG_LINKING) $@
262 $(CC) $(OBJ) $(MODULES_LIB) --output $@ $(LDFLAGS)
265 # Compile: create object files from C source files.
266 compiler_files/%.$(HOST).preproc : %.c
268 @echo $(MSG_PREPROC) $<
269 $(CC) -E $(ALL_CFLAGS) $< -o $@
271 # Compile: create object files from C source files.
272 compiler_files/%.$(HOST).o : %.c
274 @echo $(MSG_COMPILING) $<
275 $(CC) -c $(ALL_CFLAGS) $(ABS_PROJECT_DIR)/$< -o $@
278 # Compile: create assembler files from C source files.
279 compiler_files/%.$(HOST).s : %.c
280 $(CC) -S $(ALL_CFLAGS) $< -o $@
283 # Assemble: create object files from assembler source files.
284 compiler_files/%.$(HOST).o : %.S
286 @echo $(MSG_ASSEMBLING) $<
287 $(CC) -c $(ASFLAGS) $< -o $@
291 # ------ Conversion/listings targets
293 # Create final output files (.hex, .eep) from ELF output file.
294 %.$(FORMAT_EXTENSION): %.elf
296 @echo $(MSG_FLASH) $@
297 $(OBJCOPY) -O $(FORMAT) -R .eeprom $< $@
301 @echo $(MSG_EEPROM) $@
302 -$(OBJCOPY) -j .eeprom --set-section-flags=.eeprom="alloc,load" \
303 --change-section-lma .eeprom=0 -O $(FORMAT) $< $@
305 # Create extended listing file from ELF output file.
306 compiler_files/%.lss: %.elf
308 @echo $(MSG_EXTENDED_LISTING) $@
309 $(OBJDUMP) -h -S $< > $@
311 # Create a symbol table from ELF output file.
312 compiler_files/%.sym: %.elf
314 @echo $(MSG_SYMBOL_TABLE) $@
318 # ------ utils targets
320 # Display size/md5 of file.
322 @if [ -f $(OUTPUT) ]; then echo; echo $(MSG_SIZE_BEFORE); $(ELFSIZE); echo; fi
325 @if [ -f $(OUTPUT) ]; then echo; echo $(MSG_SIZE_AFTER); $(ELFSIZE); echo; fi
328 @if [ -f $(OUTPUT) ]; then echo; echo $(MSG_MD5_BEFORE); $(ELFMD5); echo; fi
331 @if [ -f $(OUTPUT) ]; then echo; echo $(MSG_MD5_AFTER); $(ELFMD5); echo; fi
334 # Display compiler version information.
338 # Program the device.
339 program: $(TARGET).$(FORMAT_EXTENSION) $(TARGET).eep
340 @if [ "$(PROGRAMMER)" = "avrdude" ]; then \
341 echo $(AVRDUDE) -e $(AVRDUDE_FLAGS) $(AVRDUDE_WRITE_FLASH) $(AVRDUDE_WRITE_EEPROM) ;\
342 $(AVRDUDE) -e $(AVRDUDE_FLAGS) $(AVRDUDE_WRITE_FLASH) $(AVRDUDE_WRITE_EEPROM) $(AVRDUDE_FLAGS_SIGNATURE_CHECK) ;\
344 @if [ "$(PROGRAMMER)" = "avarice" ]; then \
345 echo $(AVARICE) $(AVARICE_FLAGS) $(AVARICE_WRITE_FLASH) $(AVARICE_WRITE_EEPROM) ;\
346 $(AVARICE) $(AVARICE_FLAGS) $(AVARICE_WRITE_FLASH) $(AVARICE_WRITE_EEPROM) ;\
349 # Program the device.
350 erase: $(TARGET).$(FORMAT_EXTENSION) $(TARGET).eep
351 @if [ "$(PROGRAMMER)" = "avrdude" ]; then \
352 echo $(AVRDUDE) $(AVRDUDE_FLAGS) -e ;\
353 $(AVRDUDE) $(AVRDUDE_FLAGS) -e ;\
355 @if [ "$(PROGRAMMER)" = "avarice" ]; then \
356 echo "Not supported now." ; \
361 @if [ "$(PROGRAMMER)" = "avrdude" ]; then \
362 echo $(AVRDUDE) $(AVRDUDE_FLAGS) ;\
363 $(AVRDUDE) $(AVRDUDE_FLAGS) ;\
365 @if [ "$(PROGRAMMER)" = "avarice" ]; then \
366 echo "Not supported now." ; \
369 debug: $(TARGET).$(FORMAT_EXTENSION)
370 @if [ "$(PROGRAMMER)" = "avrdude" ]; then \
371 echo "Cannot debug with avrdude" ; \
373 @if [ "$(PROGRAMMER)" = "avarice" ]; then \
374 echo $(AVARICE) $(AVARICE_FLAGS) :$(AVARICE_DEBUG_PORT) ;\
375 $(AVARICE) $(AVARICE_FLAGS) :$(AVARICE_DEBUG_PORT) ;\
379 @$(AVERSIVE_DIR)/config/prog_fuses.sh $(AVERSIVE_DIR)/config/fuses_defs/$(MCU)
382 # ------ config targets
385 @${SHELL} -n $(AVERSIVE_DIR)/config/config.in
386 @HELP_FILE=$(AVERSIVE_DIR)/config/Configure.help \
387 AUTOCONF_FILE=autoconf.h \
388 ${SHELL} $(AVERSIVE_DIR)/config/scripts/Configure $(AVERSIVE_DIR)/config/config.in
391 @${SHELL} -n $(AVERSIVE_DIR)/config/config.in
392 @HELP_FILE=$(AVERSIVE_DIR)/config/Configure.help \
393 AUTOCONF_FILE=autoconf.h \
394 ${SHELL} $(AVERSIVE_DIR)/config/scripts/Configure -d $(AVERSIVE_DIR)/config/config.in
397 @${SHELL} -n $(AVERSIVE_DIR)/config/config.in
398 @make -C $(AVERSIVE_DIR)/config/scripts/lxdialog all
399 @HELP_FILE=$(AVERSIVE_DIR)/config/Configure.help \
400 AUTOCONF_FILE=autoconf.h \
401 ${SHELL} $(AVERSIVE_DIR)/config/scripts/Menuconfig $(AVERSIVE_DIR)/config/config.in
403 # ------ clean targets
406 $(REMOVE) compiler_files/*
409 clean: depclean clean_list modules_clean
412 # clean modules files
413 modules_clean: $(patsubst %,%_clean,$(MODULES))
416 $(patsubst %,%_clean,$(MODULES)):
417 @$(MAKE) VPATH=$(ABS_AVERSIVE_DIR)/modules/$(@:_clean=) -f $(ABS_AVERSIVE_DIR)/modules/$(@:_clean=)/Makefile clean
422 @echo $(MSG_CLEANING)
423 $(REMOVE) $(OUTPUT) $(OTHER_OUTPUT)
424 $(REMOVE) compiler_files/$(TARGET).map
432 # ------ dependencies targets
434 depclean: dep_list modules_depclean
437 modules_depclean: $(patsubst %,%_depclean,$(MODULES))
440 $(patsubst %,%_depclean,$(MODULES)):
441 @$(MAKE) VPATH=$(ABS_AVERSIVE_DIR)/modules/$(@:_depclean=) -f $(ABS_AVERSIVE_DIR)/modules/$(@:_depclean=)/Makefile depclean
446 @echo $(MSG_DEPCLEAN)
450 # Automatically generate C source code dependencies.
451 compiler_files/%.$(HOST).d: %.c
452 @mkdir -p compiler_files ; \
454 for conf_file in .config autoconf.h .aversive_conf; do \
455 if [ ! -f $$conf_file ]; then \
456 echo "$$conf_file file is missing"; \
460 for module in `echo $(MODULES)`; do \
461 conf=`basename $$module"_config.h"`; \
462 if [ -f $$module/config/$$conf ]; then \
463 if [ ! -f $$conf ]; then \
464 echo "$$conf file is missing"; \
469 if [ $$error -eq 1 ]; then \
470 echo "Missing config files, please run make menuconfig or make config"; \
475 $(CC) -M $(CFLAGS) $< > $@.$$$$; \
476 sed 's,\($*\)\.o[ :]*,compiler_files/\1.$(HOST).o $@ : ,g' < $@.$$$$ > $@; \
481 # include the deps file only for other targets than menuconfig or config
482 ifeq (,$(findstring config,$(MAKECMDGOALS)))
483 ifeq (,$(wildcard .config))
484 $(error You need to call make config or make menuconfig first)
486 ifeq (,$(wildcard autoconf.h))
487 $(error Missing autoconf.h -- You need to call make noconfig)
489 ifeq (,$(wildcard .aversive_conf))
490 $(error Missing .aversive_conf -- You need to call make noconfig)
493 ifeq (,$(findstring mrproper,$(MAKECMDGOALS)))
494 -include $(addprefix compiler_files/,$(SRC:.c=.$(HOST).d))
497 ifneq (1,$(words $(MAKECMDGOALS)))
498 $(error You need to call make config or make menuconfig without other targets)
503 # ------ Listing of phony targets.
505 .PHONY : all sizebefore sizeafter gccversion \
506 clean clean_list program md5sumafter md5sumbefore \
507 depclean dep_list modules $(MODULES) \