examples: Ported to Snow Leopard
[simavr] / Makefile.common
1 #
2 # This makefile take each "at*" file, extracts it's part name
3 # And compile it into an ELF binary.
4 # It also disassemble it for debugging purposes.
5
6 # The code is compiled "optimized" to the max.
7
8 # The wierd "-Wl,--undefined=_mmcu,--section-start=.mmcu=0x910000"
9 # is used to tell the linker not to discard the .mmcu section,
10 # otherwise the --gc-sections will delete it.
11
12 #       Copyright 2008, 2009 Michel Pollet <buserror@gmail.com>
13 #
14 #       This file is part of simavr.
15 #
16 #       simavr is free software: you can redistribute it and/or modify
17 #       it under the terms of the GNU General Public License as published by
18 #       the Free Software Foundation, either version 3 of the License, or
19 #       (at your option) any later version.
20 #
21 #       simavr is distributed in the hope that it will be useful,
22 #       but WITHOUT ANY WARRANTY; without even the implied warranty of
23 #       MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24 #       GNU General Public License for more details.
25 #
26 #       You should have received a copy of the GNU General Public License
27 #       along with simavr.  If not, see <http://www.gnu.org/licenses/>.
28
29 CFLAGS  += -g --std=gnu99 -O2
30 CFLAGS  += ${patsubst %,-I%,${subst :, ,${IPATH}}}
31 LDFLAGS += -lelf 
32
33 ifeq (${shell uname}, Darwin)
34 AVR_ROOT := "/Applications/Arduino.app/Contents/Resources/Java/hardware/tools/avr/"
35 AVR_INC := ${AVR_ROOT}/avr-4/
36 AVR := ${AVR_ROOT}/bin/avr-
37 LFLAGS += -L/opt/local/lib
38 else
39 AVR_ROOT := /usr/lib/avr
40 AVR_INC := ${AVR_ROOT}
41 AVR := avr-
42 endif
43
44 # The code is compiled "optimized" to the max.
45
46 # The wierd "-Wl,--undefined=_mmcu,--section-start=.mmcu=0x910000"
47 # is used to tell the linker not to discard the .mmcu section,
48 # otherwise the --gc-sections will delete it.
49
50 %.hex: %.axf
51         @${AVR}objcopy -j .text -j .data -O ihex ${<} ${@}
52
53 %.s: %.axf
54         @${AVR}objdump -j .text -j .data -j .bss -d  ${<} > ${@}
55
56 # --mcall-prologues can be used here, but messes up debugging a little
57 %.axf: %.c 
58         @echo AVR-CC ${<}
59         @part=${<} ; part=$${part/_*}; \
60         ${AVR}gcc -Wall -gdwarf-2 -Os -std=gnu99 \
61                         -mmcu=$$part \
62                         -DF_CPU=8000000 \
63                         -fno-inline-small-functions \
64                         -ffunction-sections -fdata-sections \
65                         -Wl,--relax,--gc-sections \
66                         -Wl,--undefined=_mmcu,--section-start=.mmcu=0x910000 \
67                         -I../include -I../../include \
68                         ${<} -o ${@}
69         @${AVR}size ${@}|sed '1d'
70
71 OBJ = obj
72
73 ${OBJ}/%.o: %.c
74         @gcc $(CFLAGS) -MD \
75                 $<  -c -o $@
76         @echo CC $<
77
78 ${OBJ}: 
79         @mkdir -p ${OBJ}
80
81 # include the dependency files generated by gcc, if any
82 -include ${wildcard ${OBJ}/*.d}