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)
113 AVRDUDE_WRITE_FLASH = -U flash:w:$(TARGET).$(FORMAT_EXTENSION)
114 #AVRDUDE_WRITE_EEPROM = -U eeprom:w:$(TARGET).eep
116 # Optional AVRDUDE flags can be added in the makefile of the project
117 # (to adjust the baud rate, ...)
120 AVRDUDE_FLAGS = -p $(AVRDUDE_MCU) -P $(AVRDUDE_PORT) -c $(AVRDUDE_PROGRAMMER) $(AVRDUDE_FLAGS_OPT)
122 ifneq ($(AVRDUDE_DELAY),)
123 AVRDUDE_FLAGS += -i $(AVRDUDE_DELAY)
128 # Uncomment the following if you want avrdude's erase cycle counter.
129 # Note that this counter needs to be initialized first using -Yn,
130 # see avrdude manual.
133 # Uncomment the following if you do /not/ wish a verification to be
134 # performed after programming the device.
137 # Increase verbosity level. Please use this when submitting bug
138 # reports about avrdude. See <http://savannah.nongnu.org/projects/avrdude>
139 # to submit bug reports.
140 #AVRDUDE_FLAGS += -v -v
143 # Sets the baudrate. Comment this if you want to have the default baudrate
144 AVRDUDE_FLAGS += -b $(AVRDUDE_BAUDRATE)
146 AVARICE_WRITE_FLASH = --erase --program --file $(TARGET).$(FORMAT_EXTENSION)
147 #AVARICE_WRITE_EEPROM = XXX
149 export AVARICE_FLAGS = -P $(MCU) --jtag $(AVARICE_PORT) --$(AVARICE_PROGRAMMER)
152 # ---------------------------------------------------------------------------
155 # Define programs and commands.
160 OBJCOPY = avr-objcopy
161 OBJDUMP = avr-objdump
164 OUTPUT = $(TARGET).elf
165 OTHER_OUTPUT = $(TARGET).$(FORMAT_EXTENSION) $(TARGET).eep compiler_files/$(TARGET).lss compiler_files/$(TARGET).sym
172 SIZE = size --format=Berkeley
177 export REMOVE = rm -f
182 export AVRDUDE = avrdude
183 export AVARICE = avarice
184 HEXSIZE = $(SIZE) --target=$(FORMAT) $(OUTPUT)
185 ELFSIZE = $(SIZE) $(OUTPUT)
186 ELFMD5 = $(MD5) $(OUTPUT) | cut -b1-4
188 export AVRDUDE_PROGRAMMER
194 # ---------------------------------------------------------------------------
199 MSG_SIZE_BEFORE = Size before:
200 MSG_SIZE_AFTER = Size after:
201 MSG_FLASH = Creating load file for Flash:
202 MSG_EEPROM = Creating load file for EEPROM:
203 MSG_EXTENDED_LISTING = Creating Extended Listing:
204 MSG_SYMBOL_TABLE = Creating Symbol Table:
205 MSG_LINKING = Linking:
206 MSG_COMPILING = Compiling:
207 MSG_PREPROC = Preprocessing:
208 MSG_ASSEMBLING = Assembling:
209 MSG_CLEANING = Cleaning project:
210 MSG_MD5_BEFORE = Processing MD5:
211 MSG_MD5_AFTER = Processing MD5:
212 MSG_DEPCLEAN = Cleaning deps:
213 MSG_MODULE = ------ Compiling Module:
216 # ---------------------------------------------------------------------------
218 OBJ = $(addprefix compiler_files/,$(SRC:.c=.$(HOST).o) $(ASRC:.S=.$(HOST).o))
219 DEPS = $(addprefix compiler_files/,$(SRC:.c=.$(HOST).d))
221 MODULES_LIB = $(addprefix compiler_files/,$(notdir $(MODULES:=.$(HOST).a)))
223 PREPROC= $(addprefix compiler_files/,$(SRC:.c=.$(HOST).preproc))
228 # Variables n{\'e}cessaires pour les Makefile des modules
229 export AVERSIVE_DIR ABS_AVERSIVE_DIR
230 export CFLAGS EXTRAINCDIRS
234 all: compiler_files gccversion sizebefore md5sumbefore modules $(OUTPUT) $(OTHER_OUTPUT) sizeafter md5sumafter
236 # only compile project files
237 project: compiler_files gccversion sizebefore md5sumbefore $(OUTPUT) $(OTHER_OUTPUT) sizeafter md5sumafter
240 @mkdir -p compiler_files
243 # ------ Compilation/link/assemble targets
245 # Compile modules and create a library for each
251 @echo $(MSG_MODULE) $@
252 @$(MAKE) VPATH=$(ABS_AVERSIVE_DIR)/modules/$@ -f $(AVERSIVE_DIR)/modules/$@/Makefile
254 # Link: create ELF output file from object files.
255 $(OUTPUT): $(PREPROC) $(OBJ) $(MODULES_LIB)
257 @echo $(MSG_LINKING) $@
258 $(CC) $(OBJ) $(MODULES_LIB) --output $@ $(LDFLAGS)
261 # Compile: create object files from C source files.
262 compiler_files/%.$(HOST).preproc : %.c
264 @echo $(MSG_PREPROC) $<
265 $(CC) -E $(ALL_CFLAGS) $< -o $@
267 # Compile: create object files from C source files.
268 compiler_files/%.$(HOST).o : %.c
270 @echo $(MSG_COMPILING) $<
271 $(CC) -c $(ALL_CFLAGS) $(ABS_PROJECT_DIR)/$< -o $@
274 # Compile: create assembler files from C source files.
275 compiler_files/%.$(HOST).s : %.c
276 $(CC) -S $(ALL_CFLAGS) $< -o $@
279 # Assemble: create object files from assembler source files.
280 compiler_files/%.$(HOST).o : %.S
282 @echo $(MSG_ASSEMBLING) $<
283 $(CC) -c $(ASFLAGS) $< -o $@
287 # ------ Conversion/listings targets
289 # Create final output files (.hex, .eep) from ELF output file.
290 %.$(FORMAT_EXTENSION): %.elf
292 @echo $(MSG_FLASH) $@
293 $(OBJCOPY) -O $(FORMAT) -R .eeprom $< $@
297 @echo $(MSG_EEPROM) $@
298 -$(OBJCOPY) -j .eeprom --set-section-flags=.eeprom="alloc,load" \
299 --change-section-lma .eeprom=0 -O $(FORMAT) $< $@
301 # Create extended listing file from ELF output file.
302 compiler_files/%.lss: %.elf
304 @echo $(MSG_EXTENDED_LISTING) $@
305 $(OBJDUMP) -h -S $< > $@
307 # Create a symbol table from ELF output file.
308 compiler_files/%.sym: %.elf
310 @echo $(MSG_SYMBOL_TABLE) $@
314 # ------ utils targets
316 # Display size/md5 of file.
318 @if [ -f $(OUTPUT) ]; then echo; echo $(MSG_SIZE_BEFORE); $(ELFSIZE); echo; fi
321 @if [ -f $(OUTPUT) ]; then echo; echo $(MSG_SIZE_AFTER); $(ELFSIZE); echo; fi
324 @if [ -f $(OUTPUT) ]; then echo; echo $(MSG_MD5_BEFORE); $(ELFMD5); echo; fi
327 @if [ -f $(OUTPUT) ]; then echo; echo $(MSG_MD5_AFTER); $(ELFMD5); echo; fi
330 # Display compiler version information.
334 # Program the device.
335 program: $(TARGET).$(FORMAT_EXTENSION) $(TARGET).eep
336 @if [ "$(PROGRAMMER)" = "avrdude" ]; then \
337 echo $(AVRDUDE) -e $(AVRDUDE_FLAGS) $(AVRDUDE_WRITE_FLASH) $(AVRDUDE_WRITE_EEPROM) ;\
338 $(AVRDUDE) -e $(AVRDUDE_FLAGS) $(AVRDUDE_WRITE_FLASH) $(AVRDUDE_WRITE_EEPROM) $(AVRDUDE_FLAGS_SIGNATURE_CHECK) ;\
340 @if [ "$(PROGRAMMER)" = "avarice" ]; then \
341 echo $(AVARICE) $(AVARICE_FLAGS) $(AVARICE_WRITE_FLASH) $(AVARICE_WRITE_EEPROM) ;\
342 $(AVARICE) $(AVARICE_FLAGS) $(AVARICE_WRITE_FLASH) $(AVARICE_WRITE_EEPROM) ;\
345 # Program the device.
346 erase: $(TARGET).$(FORMAT_EXTENSION) $(TARGET).eep
347 @if [ "$(PROGRAMMER)" = "avrdude" ]; then \
348 echo $(AVRDUDE) $(AVRDUDE_FLAGS) -e ;\
349 $(AVRDUDE) $(AVRDUDE_FLAGS) -e ;\
351 @if [ "$(PROGRAMMER)" = "avarice" ]; then \
352 echo "Not supported now." ; \
357 @if [ "$(PROGRAMMER)" = "avrdude" ]; then \
358 echo $(AVRDUDE) $(AVRDUDE_FLAGS) ;\
359 $(AVRDUDE) $(AVRDUDE_FLAGS) ;\
361 @if [ "$(PROGRAMMER)" = "avarice" ]; then \
362 echo "Not supported now." ; \
365 debug: $(TARGET).$(FORMAT_EXTENSION)
366 @if [ "$(PROGRAMMER)" = "avrdude" ]; then \
367 echo "Cannot debug with avrdude" ; \
369 @if [ "$(PROGRAMMER)" = "avarice" ]; then \
370 echo $(AVARICE) $(AVARICE_FLAGS) :$(AVARICE_DEBUG_PORT) ;\
371 $(AVARICE) $(AVARICE_FLAGS) :$(AVARICE_DEBUG_PORT) ;\
375 @$(AVERSIVE_DIR)/config/prog_fuses.sh $(AVERSIVE_DIR)/config/fuses_defs/$(MCU)
378 # ------ config targets
381 @${SHELL} -n $(AVERSIVE_DIR)/config/config.in
382 @HELP_FILE=$(AVERSIVE_DIR)/config/Configure.help \
383 AUTOCONF_FILE=autoconf.h \
384 ${SHELL} $(AVERSIVE_DIR)/config/scripts/Configure $(AVERSIVE_DIR)/config/config.in
387 @${SHELL} -n $(AVERSIVE_DIR)/config/config.in
388 @HELP_FILE=$(AVERSIVE_DIR)/config/Configure.help \
389 AUTOCONF_FILE=autoconf.h \
390 ${SHELL} $(AVERSIVE_DIR)/config/scripts/Configure -d $(AVERSIVE_DIR)/config/config.in
393 @${SHELL} -n $(AVERSIVE_DIR)/config/config.in
394 @make -C $(AVERSIVE_DIR)/config/scripts/lxdialog all
395 @HELP_FILE=$(AVERSIVE_DIR)/config/Configure.help \
396 AUTOCONF_FILE=autoconf.h \
397 ${SHELL} $(AVERSIVE_DIR)/config/scripts/Menuconfig $(AVERSIVE_DIR)/config/config.in
399 # ------ clean targets
402 $(REMOVE) compiler_files/*
405 clean: depclean clean_list modules_clean
408 # clean modules files
409 modules_clean: $(patsubst %,%_clean,$(MODULES))
412 $(patsubst %,%_clean,$(MODULES)):
413 @$(MAKE) VPATH=$(ABS_AVERSIVE_DIR)/modules/$(@:_clean=) -f $(ABS_AVERSIVE_DIR)/modules/$(@:_clean=)/Makefile clean
418 @echo $(MSG_CLEANING)
419 $(REMOVE) $(OUTPUT) $(OTHER_OUTPUT)
420 $(REMOVE) compiler_files/$(TARGET).map
428 # ------ dependencies targets
430 depclean: dep_list modules_depclean
433 modules_depclean: $(patsubst %,%_depclean,$(MODULES))
436 $(patsubst %,%_depclean,$(MODULES)):
437 @$(MAKE) VPATH=$(ABS_AVERSIVE_DIR)/modules/$(@:_depclean=) -f $(ABS_AVERSIVE_DIR)/modules/$(@:_depclean=)/Makefile depclean
442 @echo $(MSG_DEPCLEAN)
446 # Automatically generate C source code dependencies.
447 compiler_files/%.$(HOST).d: %.c
448 @mkdir -p compiler_files ; \
450 for conf_file in .config autoconf.h .aversive_conf; do \
451 if [ ! -f $$conf_file ]; then \
452 echo "$$conf_file file is missing"; \
456 for module in `echo $(MODULES)`; do \
457 conf=`basename $$module"_config.h"`; \
458 if [ -f $$module/config/$$conf ]; then \
459 if [ ! -f $$conf ]; then \
460 echo "$$conf file is missing"; \
465 if [ $$error -eq 1 ]; then \
466 echo "Missing config files, please run make menuconfig or make config"; \
471 $(CC) -M $(CFLAGS) $< > $@.$$$$; \
472 sed 's,\($*\)\.o[ :]*,compiler_files/\1.$(HOST).o $@ : ,g' < $@.$$$$ > $@; \
477 # include the deps file only for other targets than menuconfig or config
478 ifeq (,$(findstring config,$(MAKECMDGOALS)))
479 ifeq (,$(wildcard .config))
480 $(error You need to call make config or make menuconfig first)
482 ifeq (,$(wildcard autoconf.h))
483 $(error Missing autoconf.h -- You need to call make noconfig)
485 ifeq (,$(wildcard .aversive_conf))
486 $(error Missing .aversive_conf -- You need to call make noconfig)
489 ifeq (,$(findstring mrproper,$(MAKECMDGOALS)))
490 -include $(addprefix compiler_files/,$(SRC:.c=.$(HOST).d))
493 ifneq (1,$(words $(MAKECMDGOALS)))
494 $(error You need to call make config or make menuconfig without other targets)
499 # ------ Listing of phony targets.
501 .PHONY : all sizebefore sizeafter gccversion \
502 clean clean_list program md5sumafter md5sumbefore \
503 depclean dep_list modules $(MODULES) \