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