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