cobboard: prepare state machine
[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 +=
73 endif
74
75
76
77 #common asflags
78 ASFLAGS += 
79
80 ifeq ($(HOST),avr)
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
88 else
89 ASFLAGS +=
90 endif
91
92
93
94
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
100 ifeq ($(HOST),avr)
101 LDFLAGS += -mmcu=$(MCU) $(PRINTF_LDFLAGS)
102 LDFLAGS += -Wl,-Map=$(addprefix compiler_files/,$(TARGET).map),--cref
103 else
104 LDFLAGS += $(PTHREAD_LDFLAGS)
105 endif
106
107 LDFLAGS += $(MATH_LIB)
108
109
110 # AVRDUDE does not know the ATMEGA1281 for now, consider it a 128.
111 ifeq ($(MCU),atmega1281)
112 AVRDUDE_MCU = atmega128
113 else
114 AVRDUDE_MCU = $(MCU)
115 endif
116
117 AVRDUDE_WRITE_FLASH = -U flash:w:$(TARGET).$(FORMAT_EXTENSION)
118 #AVRDUDE_WRITE_EEPROM = -U eeprom:w:$(TARGET).eep
119
120 # Optional AVRDUDE flags can be added in the makefile of the project
121 # (to adjust the baud rate, ...)
122 AVRDUDE_FLAGS_OPT +=
123
124 AVRDUDE_FLAGS = -p $(AVRDUDE_MCU) -P $(AVRDUDE_PORT) -c $(AVRDUDE_PROGRAMMER) $(AVRDUDE_FLAGS_OPT)
125
126 ifneq ($(AVRDUDE_DELAY),)
127 AVRDUDE_FLAGS += -i $(AVRDUDE_DELAY)
128 endif
129
130 export AVRDUDE_FLAGS
131
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.
135 #AVRDUDE_ERASE += -y
136
137 # Uncomment the following if you do /not/ wish a verification to be
138 # performed after programming the device.
139 #AVRDUDE_FLAGS += -V
140
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
145
146
147 # Sets the baudrate. Comment this if you want to have the default baudrate
148 AVRDUDE_FLAGS += -b $(AVRDUDE_BAUDRATE)
149
150 AVARICE_WRITE_FLASH = --erase --program --file $(TARGET).$(FORMAT_EXTENSION)
151 #AVARICE_WRITE_EEPROM = XXX
152
153 export AVARICE_FLAGS = -P $(MCU) --jtag $(AVARICE_PORT) --$(AVARICE_PROGRAMMER)
154
155
156 # ---------------------------------------------------------------------------
157
158
159 # Define programs and commands.
160 ifeq ($(HOST),avr)
161 export CC = avr-gcc
162 export AS = avr-as
163 export AR = avr-ar
164 OBJCOPY = avr-objcopy
165 OBJDUMP = avr-objdump
166 SIZE = avr-size
167 NM = avr-nm
168 OUTPUT = $(TARGET).elf 
169 OTHER_OUTPUT = $(TARGET).$(FORMAT_EXTENSION) $(TARGET).eep compiler_files/$(TARGET).lss compiler_files/$(TARGET).sym 
170 else
171 export CC = gcc
172 export AS = as
173 export AR = ar
174 OBJCOPY = objcopy
175 OBJDUMP = objdump
176 SIZE = size --format=Berkeley
177 NM = nm
178 OUTPUT = $(TARGET)
179 endif
180 export HOSTCC = gcc
181 export REMOVE = rm -f
182 export COPY = cp
183 export SHELL = bash
184 DATE=`date`
185 MD5 = md5sum
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
191 export AVRDUDE_PORT
192 export AVRDUDE_PROGRAMMER
193 export MCU
194 export PROGRAMMER
195
196
197
198 # ---------------------------------------------------------------------------
199
200
201 # Define Messages
202 # English
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:
218
219
220 # ---------------------------------------------------------------------------
221
222 OBJ = $(addprefix compiler_files/,$(SRC:.c=.$(HOST).o) $(ASRC:.S=.$(HOST).o))
223 DEPS = $(addprefix compiler_files/,$(SRC:.c=.$(HOST).d))
224 LST = $(OBJ:.o=.lst)
225 MODULES_LIB = $(addprefix compiler_files/,$(notdir $(MODULES:=.$(HOST).a)))
226 ifneq ($(P),)
227 PREPROC= $(addprefix compiler_files/,$(SRC:.c=.$(HOST).preproc))
228 else
229 PREPROC=
230 endif
231
232 # Variables n{\'e}cessaires pour les Makefile des modules
233 export AVERSIVE_DIR ABS_AVERSIVE_DIR
234 export CFLAGS EXTRAINCDIRS
235
236
237 # Default target.
238 all: compiler_files gccversion sizebefore md5sumbefore modules $(OUTPUT) $(OTHER_OUTPUT) sizeafter md5sumafter
239
240 # only compile project files
241 project: compiler_files gccversion sizebefore md5sumbefore $(OUTPUT) $(OTHER_OUTPUT) sizeafter md5sumafter
242
243 compiler_files:
244         @mkdir -p compiler_files
245
246
247 # ------ Compilation/link/assemble targets
248
249 # Compile modules and create a library for each
250 modules: $(MODULES)
251
252
253 $(MODULES):
254         @echo
255         @echo $(MSG_MODULE) $@
256         @$(MAKE) VPATH=$(ABS_AVERSIVE_DIR)/modules/$@ -f $(AVERSIVE_DIR)/modules/$@/Makefile
257
258 # Link: create ELF output file from object files.
259 $(OUTPUT): $(PREPROC) $(OBJ) $(MODULES_LIB)
260         @echo
261         @echo $(MSG_LINKING) $@
262         $(CC) $(OBJ) $(MODULES_LIB) --output $@ $(LDFLAGS)
263
264
265 # Compile: create object files from C source files.
266 compiler_files/%.$(HOST).preproc : %.c
267         @echo
268         @echo $(MSG_PREPROC) $<
269         $(CC) -E $(ALL_CFLAGS) $< -o $@
270
271 # Compile: create object files from C source files.
272 compiler_files/%.$(HOST).o : %.c
273         @echo
274         @echo $(MSG_COMPILING) $<
275         $(CC) -c $(ALL_CFLAGS) $(ABS_PROJECT_DIR)/$< -o $@
276
277
278 # Compile: create assembler files from C source files.
279 compiler_files/%.$(HOST).s : %.c
280         $(CC) -S $(ALL_CFLAGS) $< -o $@
281
282
283 # Assemble: create object files from assembler source files.
284 compiler_files/%.$(HOST).o : %.S
285         @echo 
286         @echo $(MSG_ASSEMBLING) $<
287         $(CC) -c $(ASFLAGS) $< -o $@
288
289
290
291 # ------ Conversion/listings targets
292
293 # Create final output files (.hex, .eep) from ELF output file.
294 %.$(FORMAT_EXTENSION): %.elf
295         @echo
296         @echo $(MSG_FLASH) $@
297         $(OBJCOPY) -O $(FORMAT) -R .eeprom $< $@
298
299 %.eep: %.elf
300         @echo
301         @echo $(MSG_EEPROM) $@
302         -$(OBJCOPY) -j .eeprom --set-section-flags=.eeprom="alloc,load" \
303         --change-section-lma .eeprom=0 -O $(FORMAT) $< $@
304
305 # Create extended listing file from ELF output file.
306 compiler_files/%.lss: %.elf
307         @echo
308         @echo $(MSG_EXTENDED_LISTING) $@
309         $(OBJDUMP) -h -S $< > $@
310
311 # Create a symbol table from ELF output file.
312 compiler_files/%.sym: %.elf
313         @echo
314         @echo $(MSG_SYMBOL_TABLE) $@
315         $(NM) -n $< > $@
316
317
318 # ------ utils targets
319
320 # Display size/md5 of file.
321 sizebefore:
322         @if [ -f $(OUTPUT) ]; then echo; echo $(MSG_SIZE_BEFORE); $(ELFSIZE); echo; fi
323
324 sizeafter:
325         @if [ -f $(OUTPUT) ]; then echo; echo $(MSG_SIZE_AFTER); $(ELFSIZE); echo; fi
326
327 md5sumbefore:
328         @if [ -f $(OUTPUT) ]; then echo; echo $(MSG_MD5_BEFORE); $(ELFMD5); echo; fi
329
330 md5sumafter:
331         @if [ -f $(OUTPUT) ]; then echo; echo $(MSG_MD5_AFTER); $(ELFMD5); echo; fi
332
333
334 # Display compiler version information.
335 gccversion : 
336         @$(CC) --version
337
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) ;\
343         fi
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) ;\
347         fi
348
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 ;\
354         fi
355         @if [ "$(PROGRAMMER)" = "avarice" ]; then \
356           echo "Not supported now." ; \
357         fi
358
359 # reset the device.  
360 reset: 
361         @if [ "$(PROGRAMMER)" = "avrdude" ]; then \
362           echo $(AVRDUDE) $(AVRDUDE_FLAGS) ;\
363           $(AVRDUDE) $(AVRDUDE_FLAGS) ;\
364         fi
365         @if [ "$(PROGRAMMER)" = "avarice" ]; then \
366           echo "Not supported now." ; \
367         fi
368
369 debug: $(TARGET).$(FORMAT_EXTENSION)
370         @if [ "$(PROGRAMMER)" = "avrdude" ]; then \
371           echo "Cannot debug with avrdude" ; \
372         fi
373         @if [ "$(PROGRAMMER)" = "avarice" ]; then \
374           echo $(AVARICE) $(AVARICE_FLAGS) :$(AVARICE_DEBUG_PORT) ;\
375           $(AVARICE) $(AVARICE_FLAGS) :$(AVARICE_DEBUG_PORT) ;\
376         fi
377
378 fuse:
379         @$(AVERSIVE_DIR)/config/prog_fuses.sh $(AVERSIVE_DIR)/config/fuses_defs/$(MCU)
380
381
382 # ------ config targets
383
384 config:
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
389
390 noconfig:
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
395
396 menuconfig:
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
402
403 # ------ clean targets
404
405 mrproper: clean_list
406         $(REMOVE) compiler_files/*
407
408
409 clean: depclean clean_list modules_clean
410
411
412 # clean modules files
413 modules_clean: $(patsubst %,%_clean,$(MODULES))
414
415
416 $(patsubst %,%_clean,$(MODULES)):
417         @$(MAKE) VPATH=$(ABS_AVERSIVE_DIR)/modules/$(@:_clean=) -f $(ABS_AVERSIVE_DIR)/modules/$(@:_clean=)/Makefile clean
418
419
420 clean_list :
421         @echo
422         @echo $(MSG_CLEANING)
423         $(REMOVE) $(OUTPUT) $(OTHER_OUTPUT)
424         $(REMOVE) compiler_files/$(TARGET).map
425         $(REMOVE) $(OBJ)
426         $(REMOVE) $(PREPROC)
427         $(REMOVE) $(LST)
428
429
430
431
432 # ------ dependencies targets
433
434 depclean: dep_list modules_depclean
435
436
437 modules_depclean: $(patsubst %,%_depclean,$(MODULES))
438
439
440 $(patsubst %,%_depclean,$(MODULES)):
441         @$(MAKE) VPATH=$(ABS_AVERSIVE_DIR)/modules/$(@:_depclean=) -f $(ABS_AVERSIVE_DIR)/modules/$(@:_depclean=)/Makefile depclean
442
443
444 dep_list:
445         @echo
446         @echo $(MSG_DEPCLEAN)
447         $(REMOVE) $(DEPS)
448
449
450 # Automatically generate C source code dependencies. 
451 compiler_files/%.$(HOST).d: %.c
452         @mkdir -p compiler_files ; \
453         error=0; \
454         for conf_file in .config autoconf.h .aversive_conf; do \
455           if [ ! -f $$conf_file ]; then \
456             echo "$$conf_file file is missing"; \
457             error=1; \
458           fi ; \
459         done; \
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"; \
465               error=1; \
466             fi ; \
467           fi ; \
468         done; \
469         if [ $$error -eq 1 ]; then \
470           echo "Missing config files, please run make menuconfig or make config"; \
471           exit 1; \
472         fi
473         @echo Generating $@
474         @set -e; rm -f $@; \
475         $(CC) -M $(CFLAGS) $< > $@.$$$$; \
476         sed 's,\($*\)\.o[ :]*,compiler_files/\1.$(HOST).o $@ : ,g' < $@.$$$$ > $@; \
477         rm -f $@.$$$$
478
479
480
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)
485 endif
486 ifeq (,$(wildcard autoconf.h))
487 $(error Missing autoconf.h -- You need to call make noconfig)
488 endif
489 ifeq (,$(wildcard .aversive_conf))
490 $(error Missing .aversive_conf -- You need to call make noconfig)
491 endif
492
493 ifeq (,$(findstring mrproper,$(MAKECMDGOALS)))
494 -include $(addprefix compiler_files/,$(SRC:.c=.$(HOST).d))
495 endif
496 else
497 ifneq (1,$(words $(MAKECMDGOALS)))
498 $(error You need to call make config or make menuconfig without other targets)
499 endif
500 endif
501
502
503 # ------ Listing of phony targets.
504
505 .PHONY : all sizebefore sizeafter gccversion \
506         clean clean_list program md5sumafter md5sumbefore \
507         depclean dep_list modules $(MODULES)  \
508         menuconfig config
509