022f1f14ef65bd21ffbc20eb28d9e3e5a4b07b78
[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 # get the first character of what the compiler says it is
30 ARCH = ${shell $(CC) -dumpmachine | sed 's/\(.\).*/\1/'}
31
32 ifeq ($(ARCH), i)
33 CFLAGS  += -mfpmath=sse -msse2 -fPIC
34 endif
35
36 CFLAGS  += -g --std=gnu99
37 CFLAGS  += ${patsubst %,-I%,${subst :, ,${IPATH}}}
38 LDFLAGS += -lelf 
39
40 ifeq (${shell uname}, Darwin)
41 AVR_ROOT := "/Applications/Arduino.app/Contents/Resources/Java/hardware/tools/avr/"
42 AVR_INC := ${AVR_ROOT}/avr-4/
43 AVR := ${AVR_ROOT}/bin/avr-
44 LFLAGS += -L/opt/local/lib
45 else
46 AVR_ROOT := /usr/lib/avr
47 AVR_INC := ${AVR_ROOT}
48 AVR := avr-
49 endif
50
51 CC ?= gcc
52 AR ?= ar
53 RANLIB ?= ranlib
54
55 # The code is compiled "optimized" to the max.
56
57 # The wierd "-Wl,--undefined=_mmcu,--section-start=.mmcu=0x910000"
58 # is used to tell the linker not to discard the .mmcu section,
59 # otherwise the --gc-sections will delete it.
60
61 %.hex: %.axf
62         @${AVR}objcopy -j .text -j .data -j .eeprom -O ihex ${<} ${@}
63
64 %.s: %.axf
65         @${AVR}objdump -j .text -j .data -j .bss -d  ${<} > ${@}
66
67 # --mcall-prologues can be used here, but messes up debugging a little
68 %.axf: %.c 
69         @echo AVR-CC ${<}
70         @part=${<} ; part=$${part/_*}; \
71         ${AVR}gcc -Wall -gdwarf-2 -Os -std=gnu99 \
72                         -mmcu=$$part \
73                         -DF_CPU=8000000 \
74                         -fno-inline-small-functions \
75                         -ffunction-sections -fdata-sections \
76                         -Wl,--relax,--gc-sections \
77                         -Wl,--undefined=_mmcu,--section-start=.mmcu=0x910000 \
78                         -I../include -I../../include \
79                         ${^} -o ${@}
80         @${AVR}size ${@}|sed '1d'
81
82 OBJ = obj-${shell $(CC) -dumpmachine}
83
84 # this rule has precedence
85 ${OBJ}/sim_%.o : cores/sim_%.c
86 ifeq ($(V),1)
87         $(CC) $(CFLAGS) -MD \
88                 -I${AVR_INC}/include/ \
89                 $<  -c -o $@
90 else
91         @$(CC) $(CFLAGS) -MD \
92                 -I${AVR_INC}/include/ \
93                 $<  -c -o $@
94         @echo CORE $<
95 endif
96
97 ${OBJ}/%.o: %.c
98 ifeq ($(V),1)
99         $(CC) $(CFLAGS) -MD \
100                 $<  -c -o $@
101 else
102         @$(CC) $(CFLAGS) -MD \
103                 $<  -c -o $@
104         @echo CC $<
105 endif
106
107 ${OBJ}/%.elf:
108 ifeq ($(V),1)
109         echo $^ / $<
110         $(CC) -MD ${CFLAGS}  ${LFLAGS} -o $@ $^ $(LDFLAGS)
111 else
112         @echo LD $@
113         @$(CC) -MD ${CFLAGS}  ${LFLAGS} -o $@ $^ $(LDFLAGS)
114 endif
115
116 obj: ${OBJ}
117  
118 ${OBJ}: 
119         @mkdir -p ${OBJ}
120
121 clean-${OBJ}:
122         rm -rf ${OBJ}
123
124 # include the dependency files generated by gcc, if any
125 -include ${wildcard ${OBJ}/*.d}