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