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