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