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