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