vt100: include pgmspace.h as we use PROGMEM macro
[aversive.git] / mk / aversive_project.mk
1 # Microb Technology, Eirbot, Droids-corp 2007 - Zer0
2 # Makefile for projects
3
4 # Inspired by the WinAVR Sample makefile written by Eric
5 # B. Weddington, J÷rg Wunsch, et al.
6 #
7 # On command line:
8 #
9 # make all = Make software.
10 #
11 # make clean = Clean out built project files.
12 #
13 # make program = Download the hex file to the device, using avrdude/avarice.  Please
14 #                customize the settings below first!
15 #
16 # make filename.s = Just compile filename.c into the assembler code only
17 #
18 # To rebuild project do "make clean" then "make all".
19 #
20
21 # default HOST is avr
22 ifeq ($(H),)
23 export HOST=avr
24 else
25 export HOST=host
26 endif
27
28 # absolute path to avoid some editors from beeing confused with vpath when 
29 # searching files
30 ABS_AVERSIVE_DIR:=$(shell cd $(AVERSIVE_DIR) ; pwd)
31 ABS_PROJECT_DIR:=$(shell pwd)
32
33 # includes for modules
34 MODULES_INC = $(addprefix $(ABS_AVERSIVE_DIR)/modules/,$(MODULES))
35
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
43
44
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
50 CFLAGS += -g 
51 CFLAGS += -O$(OPT)
52 CFLAGS += -Wall -Wstrict-prototypes
53 CFLAGS += $(patsubst %,-I%,$(EXTRAINCDIRS)) 
54 CFLAGS += -std=gnu99
55
56 # specific arch flags
57 ifeq ($(HOST),avr)
58 CFLAGS += -funsigned-char -funsigned-bitfields -fpack-struct -fshort-enums -mmcu=$(MCU) 
59 else
60 CFLAGS += -DHOST_VERSION
61 endif
62
63
64 ALL_CFLAGS += $(CFLAGS)
65 # specific arch flags
66 ifeq ($(HOST),avr)
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))
71 else
72 ALL_CFLAGS += $(PTHREAD_CFLAGS)
73
74 endif
75
76
77
78 #common asflags
79 ASFLAGS += 
80
81 ifeq ($(HOST),avr)
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
89 else
90 ASFLAGS +=
91 endif
92
93
94
95
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
101 ifeq ($(HOST),avr)
102 LDFLAGS += -mmcu=$(MCU) $(PRINTF_LDFLAGS)
103 LDFLAGS += -Wl,-Map=$(addprefix compiler_files/,$(TARGET).map),--cref
104 else
105 LDFLAGS += $(PTHREAD_LDFLAGS)
106 endif
107
108 LDFLAGS += $(MATH_LIB)
109
110
111 AVRDUDE_MCU = $(MCU)
112
113 AVRDUDE_WRITE_FLASH = -U flash:w:$(TARGET).$(FORMAT_EXTENSION)
114 #AVRDUDE_WRITE_EEPROM = -U eeprom:w:$(TARGET).eep
115
116 # Optional AVRDUDE flags can be added in the makefile of the project
117 # (to adjust the baud rate, ...)
118 AVRDUDE_FLAGS_OPT +=
119
120 AVRDUDE_FLAGS = -p $(AVRDUDE_MCU) -P $(AVRDUDE_PORT) -c $(AVRDUDE_PROGRAMMER) $(AVRDUDE_FLAGS_OPT)
121
122 ifneq ($(AVRDUDE_DELAY),)
123 AVRDUDE_FLAGS += -i $(AVRDUDE_DELAY)
124 endif
125
126 export AVRDUDE_FLAGS
127
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.
131 #AVRDUDE_ERASE += -y
132
133 # Uncomment the following if you do /not/ wish a verification to be
134 # performed after programming the device.
135 #AVRDUDE_FLAGS += -V
136
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
141
142
143 # Sets the baudrate. Comment this if you want to have the default baudrate
144 AVRDUDE_FLAGS += -b $(AVRDUDE_BAUDRATE)
145
146 AVARICE_WRITE_FLASH = --erase --program --file $(TARGET).$(FORMAT_EXTENSION)
147 #AVARICE_WRITE_EEPROM = XXX
148
149 export AVARICE_FLAGS = -P $(MCU) --jtag $(AVARICE_PORT) --$(AVARICE_PROGRAMMER)
150
151
152 # ---------------------------------------------------------------------------
153
154
155 # Define programs and commands.
156 ifeq ($(HOST),avr)
157 export CC = avr-gcc
158 export AS = avr-as
159 export AR = avr-ar
160 OBJCOPY = avr-objcopy
161 OBJDUMP = avr-objdump
162 SIZE = avr-size
163 NM = avr-nm
164 OUTPUT = $(TARGET).elf 
165 OTHER_OUTPUT = $(TARGET).$(FORMAT_EXTENSION) $(TARGET).eep compiler_files/$(TARGET).lss compiler_files/$(TARGET).sym 
166 else
167 export CC = gcc
168 export AS = as
169 export AR = ar
170 OBJCOPY = objcopy
171 OBJDUMP = objdump
172 SIZE = size --format=Berkeley
173 NM = nm
174 OUTPUT = $(TARGET)
175 endif
176 export HOSTCC = gcc
177 export REMOVE = rm -f
178 export COPY = cp
179 export SHELL = bash
180 DATE=`date`
181 MD5 = md5sum
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
187 export AVRDUDE_PORT
188 export AVRDUDE_PROGRAMMER
189 export MCU
190 export PROGRAMMER
191
192
193
194 # ---------------------------------------------------------------------------
195
196
197 # Define Messages
198 # English
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:
214
215
216 # ---------------------------------------------------------------------------
217
218 OBJ = $(addprefix compiler_files/,$(SRC:.c=.$(HOST).o) $(ASRC:.S=.$(HOST).o))
219 DEPS = $(addprefix compiler_files/,$(SRC:.c=.$(HOST).d))
220 LST = $(OBJ:.o=.lst)
221 MODULES_LIB = $(addprefix compiler_files/,$(notdir $(MODULES:=.$(HOST).a)))
222 ifneq ($(P),)
223 PREPROC= $(addprefix compiler_files/,$(SRC:.c=.$(HOST).preproc))
224 else
225 PREPROC=
226 endif
227
228 # Variables n{\'e}cessaires pour les Makefile des modules
229 export AVERSIVE_DIR ABS_AVERSIVE_DIR
230 export CFLAGS EXTRAINCDIRS
231
232
233 # Default target.
234 all: compiler_files gccversion sizebefore md5sumbefore modules $(OUTPUT) $(OTHER_OUTPUT) sizeafter md5sumafter
235
236 # only compile project files
237 project: compiler_files gccversion sizebefore md5sumbefore $(OUTPUT) $(OTHER_OUTPUT) sizeafter md5sumafter
238
239 compiler_files:
240         @mkdir -p compiler_files
241
242
243 # ------ Compilation/link/assemble targets
244
245 # Compile modules and create a library for each
246 modules: $(MODULES)
247
248
249 $(MODULES):
250         @echo
251         @echo $(MSG_MODULE) $@
252         @$(MAKE) VPATH=$(ABS_AVERSIVE_DIR)/modules/$@ -f $(AVERSIVE_DIR)/modules/$@/Makefile
253
254 # Link: create ELF output file from object files.
255 $(OUTPUT): $(PREPROC) $(OBJ) $(MODULES_LIB)
256         @echo
257         @echo $(MSG_LINKING) $@
258         $(CC) $(OBJ) $(MODULES_LIB) --output $@ $(LDFLAGS)
259
260
261 # Compile: create object files from C source files.
262 compiler_files/%.$(HOST).preproc : %.c
263         @echo
264         @echo $(MSG_PREPROC) $<
265         $(CC) -E $(ALL_CFLAGS) $< -o $@
266
267 # Compile: create object files from C source files.
268 compiler_files/%.$(HOST).o : %.c
269         @echo
270         @echo $(MSG_COMPILING) $<
271         $(CC) -c $(ALL_CFLAGS) $(ABS_PROJECT_DIR)/$< -o $@
272
273
274 # Compile: create assembler files from C source files.
275 compiler_files/%.$(HOST).s : %.c
276         $(CC) -S $(ALL_CFLAGS) $< -o $@
277
278
279 # Assemble: create object files from assembler source files.
280 compiler_files/%.$(HOST).o : %.S
281         @echo 
282         @echo $(MSG_ASSEMBLING) $<
283         $(CC) -c $(ASFLAGS) $< -o $@
284
285
286
287 # ------ Conversion/listings targets
288
289 # Create final output files (.hex, .eep) from ELF output file.
290 %.$(FORMAT_EXTENSION): %.elf
291         @echo
292         @echo $(MSG_FLASH) $@
293         $(OBJCOPY) -O $(FORMAT) -R .eeprom $< $@
294
295 %.eep: %.elf
296         @echo
297         @echo $(MSG_EEPROM) $@
298         -$(OBJCOPY) -j .eeprom --set-section-flags=.eeprom="alloc,load" \
299         --change-section-lma .eeprom=0 -O $(FORMAT) $< $@
300
301 # Create extended listing file from ELF output file.
302 compiler_files/%.lss: %.elf
303         @echo
304         @echo $(MSG_EXTENDED_LISTING) $@
305         $(OBJDUMP) -h -S $< > $@
306
307 # Create a symbol table from ELF output file.
308 compiler_files/%.sym: %.elf
309         @echo
310         @echo $(MSG_SYMBOL_TABLE) $@
311         $(NM) -n $< > $@
312
313
314 # ------ utils targets
315
316 # Display size/md5 of file.
317 sizebefore:
318         @if [ -f $(OUTPUT) ]; then echo; echo $(MSG_SIZE_BEFORE); $(ELFSIZE); echo; fi
319
320 sizeafter:
321         @if [ -f $(OUTPUT) ]; then echo; echo $(MSG_SIZE_AFTER); $(ELFSIZE); echo; fi
322
323 md5sumbefore:
324         @if [ -f $(OUTPUT) ]; then echo; echo $(MSG_MD5_BEFORE); $(ELFMD5); echo; fi
325
326 md5sumafter:
327         @if [ -f $(OUTPUT) ]; then echo; echo $(MSG_MD5_AFTER); $(ELFMD5); echo; fi
328
329
330 # Display compiler version information.
331 gccversion : 
332         @$(CC) --version
333
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) ;\
339         fi
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) ;\
343         fi
344
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 ;\
350         fi
351         @if [ "$(PROGRAMMER)" = "avarice" ]; then \
352           echo "Not supported now." ; \
353         fi
354
355 # reset the device.  
356 reset: 
357         @if [ "$(PROGRAMMER)" = "avrdude" ]; then \
358           echo $(AVRDUDE) $(AVRDUDE_FLAGS) ;\
359           $(AVRDUDE) $(AVRDUDE_FLAGS) ;\
360         fi
361         @if [ "$(PROGRAMMER)" = "avarice" ]; then \
362           echo "Not supported now." ; \
363         fi
364
365 debug: $(TARGET).$(FORMAT_EXTENSION)
366         @if [ "$(PROGRAMMER)" = "avrdude" ]; then \
367           echo "Cannot debug with avrdude" ; \
368         fi
369         @if [ "$(PROGRAMMER)" = "avarice" ]; then \
370           echo $(AVARICE) $(AVARICE_FLAGS) :$(AVARICE_DEBUG_PORT) ;\
371           $(AVARICE) $(AVARICE_FLAGS) :$(AVARICE_DEBUG_PORT) ;\
372         fi
373
374 fuse:
375         @$(AVERSIVE_DIR)/config/prog_fuses.sh $(AVERSIVE_DIR)/config/fuses_defs/$(MCU)
376
377
378 # ------ config targets
379
380 config:
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
385
386 noconfig:
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
391
392 menuconfig:
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
398
399 # ------ clean targets
400
401 mrproper: clean_list
402         $(REMOVE) compiler_files/*
403
404
405 clean: depclean clean_list modules_clean
406
407
408 # clean modules files
409 modules_clean: $(patsubst %,%_clean,$(MODULES))
410
411
412 $(patsubst %,%_clean,$(MODULES)):
413         @$(MAKE) VPATH=$(ABS_AVERSIVE_DIR)/modules/$(@:_clean=) -f $(ABS_AVERSIVE_DIR)/modules/$(@:_clean=)/Makefile clean
414
415
416 clean_list :
417         @echo
418         @echo $(MSG_CLEANING)
419         $(REMOVE) $(OUTPUT) $(OTHER_OUTPUT)
420         $(REMOVE) compiler_files/$(TARGET).map
421         $(REMOVE) $(OBJ)
422         $(REMOVE) $(PREPROC)
423         $(REMOVE) $(LST)
424
425
426
427
428 # ------ dependencies targets
429
430 depclean: dep_list modules_depclean
431
432
433 modules_depclean: $(patsubst %,%_depclean,$(MODULES))
434
435
436 $(patsubst %,%_depclean,$(MODULES)):
437         @$(MAKE) VPATH=$(ABS_AVERSIVE_DIR)/modules/$(@:_depclean=) -f $(ABS_AVERSIVE_DIR)/modules/$(@:_depclean=)/Makefile depclean
438
439
440 dep_list:
441         @echo
442         @echo $(MSG_DEPCLEAN)
443         $(REMOVE) $(DEPS)
444
445
446 # Automatically generate C source code dependencies. 
447 compiler_files/%.$(HOST).d: %.c
448         @mkdir -p compiler_files ; \
449         error=0; \
450         for conf_file in .config autoconf.h .aversive_conf; do \
451           if [ ! -f $$conf_file ]; then \
452             echo "$$conf_file file is missing"; \
453             error=1; \
454           fi ; \
455         done; \
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"; \
461               error=1; \
462             fi ; \
463           fi ; \
464         done; \
465         if [ $$error -eq 1 ]; then \
466           echo "Missing config files, please run make menuconfig or make config"; \
467           exit 1; \
468         fi
469         @echo Generating $@
470         @set -e; rm -f $@; \
471         $(CC) -M $(CFLAGS) $< > $@.$$$$; \
472         sed 's,\($*\)\.o[ :]*,compiler_files/\1.$(HOST).o $@ : ,g' < $@.$$$$ > $@; \
473         rm -f $@.$$$$
474
475
476
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)
481 endif
482 ifeq (,$(wildcard autoconf.h))
483 $(error Missing autoconf.h -- You need to call make noconfig)
484 endif
485 ifeq (,$(wildcard .aversive_conf))
486 $(error Missing .aversive_conf -- You need to call make noconfig)
487 endif
488
489 ifeq (,$(findstring mrproper,$(MAKECMDGOALS)))
490 -include $(addprefix compiler_files/,$(SRC:.c=.$(HOST).d))
491 endif
492 else
493 ifneq (1,$(words $(MAKECMDGOALS)))
494 $(error You need to call make config or make menuconfig without other targets)
495 endif
496 endif
497
498
499 # ------ Listing of phony targets.
500
501 .PHONY : all sizebefore sizeafter gccversion \
502         clean clean_list program md5sumafter md5sumbefore \
503         depclean dep_list modules $(MODULES)  \
504         menuconfig config
505