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